Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 5c860e29 2018-03-12 stsp *
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp *
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5c860e29 2018-03-12 stsp */
18 5c860e29 2018-03-12 stsp
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 f42b1b34 2018-03-12 stsp
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 d2cdc636 2020-03-18 stsp #include <util.h>
42 5c860e29 2018-03-12 stsp
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 f8a36e22 2021-08-26 stsp #include "got_send.h"
55 404c43c4 2018-06-21 stsp #include "got_blame.h"
56 63219cd2 2019-01-04 stsp #include "got_privsep.h"
57 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
58 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
59 5c860e29 2018-03-12 stsp
60 5c860e29 2018-03-12 stsp #ifndef nitems
61 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 5c860e29 2018-03-12 stsp #endif
63 99437157 2018-11-11 stsp
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
65 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
66 99437157 2018-11-11 stsp
67 99437157 2018-11-11 stsp static void
68 99437157 2018-11-11 stsp catch_sigint(int signo)
69 99437157 2018-11-11 stsp {
70 99437157 2018-11-11 stsp sigint_received = 1;
71 99437157 2018-11-11 stsp }
72 99437157 2018-11-11 stsp
73 99437157 2018-11-11 stsp static void
74 99437157 2018-11-11 stsp catch_sigpipe(int signo)
75 99437157 2018-11-11 stsp {
76 99437157 2018-11-11 stsp sigpipe_received = 1;
77 99437157 2018-11-11 stsp }
78 5c860e29 2018-03-12 stsp
79 99437157 2018-11-11 stsp
80 8cfb4057 2019-07-09 stsp struct got_cmd {
81 820059fa 2020-09-25 stsp const char *cmd_name;
82 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
83 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
84 97b3a7be 2019-07-09 stsp const char *cmd_alias;
85 5c860e29 2018-03-12 stsp };
86 5c860e29 2018-03-12 stsp
87 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
88 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
89 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
90 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
91 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
92 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
93 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
95 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
96 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
97 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
98 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
99 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
100 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
101 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
102 d00136be 2019-03-26 stsp __dead static void usage_add(void);
103 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
104 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
105 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
106 f8a36e22 2021-08-26 stsp __dead static void usage_send(void);
107 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
108 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
109 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
110 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
111 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
112 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
113 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
114 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
115 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
116 5c860e29 2018-03-12 stsp
117 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
118 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
119 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
120 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
122 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
124 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
125 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
126 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
127 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
128 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
129 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
130 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
131 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
132 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
133 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
134 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
135 f8a36e22 2021-08-26 stsp static const struct got_error* cmd_send(int, char *[]);
136 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
137 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
138 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
139 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
140 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
141 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
142 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
143 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
144 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
145 5c860e29 2018-03-12 stsp
146 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
147 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
148 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
149 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
150 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
151 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
152 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
153 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
154 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
155 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
156 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
157 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
158 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
159 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
160 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
161 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
162 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
163 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
164 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
165 f8a36e22 2021-08-26 stsp { "send", cmd_send, usage_send, "se" },
166 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
167 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
168 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
169 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
170 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
171 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
172 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
173 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
174 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
175 5c860e29 2018-03-12 stsp };
176 ce5b7c56 2019-07-09 stsp
177 ce5b7c56 2019-07-09 stsp static void
178 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
179 ce5b7c56 2019-07-09 stsp {
180 6059809a 2020-12-17 stsp size_t i;
181 ce5b7c56 2019-07-09 stsp
182 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
183 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
184 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
185 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
186 ce5b7c56 2019-07-09 stsp }
187 6879ba42 2020-10-01 naddy fputc('\n', fp);
188 ce5b7c56 2019-07-09 stsp }
189 5c860e29 2018-03-12 stsp
190 ff69268e 2020-12-13 stsp __dead static void
191 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
192 ff69268e 2020-12-13 stsp {
193 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
194 ff69268e 2020-12-13 stsp }
195 ff69268e 2020-12-13 stsp
196 5c860e29 2018-03-12 stsp int
197 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
198 5c860e29 2018-03-12 stsp {
199 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
200 6059809a 2020-12-17 stsp size_t i;
201 5c860e29 2018-03-12 stsp int ch;
202 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
203 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
204 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
205 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
206 83cd27f8 2020-01-13 stsp };
207 5c860e29 2018-03-12 stsp
208 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
209 5c860e29 2018-03-12 stsp
210 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
211 5c860e29 2018-03-12 stsp switch (ch) {
212 1b6b95a8 2018-03-12 stsp case 'h':
213 1b6b95a8 2018-03-12 stsp hflag = 1;
214 53ccebc2 2019-07-30 stsp break;
215 53ccebc2 2019-07-30 stsp case 'V':
216 53ccebc2 2019-07-30 stsp Vflag = 1;
217 1b6b95a8 2018-03-12 stsp break;
218 5c860e29 2018-03-12 stsp default:
219 6879ba42 2020-10-01 naddy usage(hflag, 1);
220 5c860e29 2018-03-12 stsp /* NOTREACHED */
221 5c860e29 2018-03-12 stsp }
222 5c860e29 2018-03-12 stsp }
223 5c860e29 2018-03-12 stsp
224 5c860e29 2018-03-12 stsp argc -= optind;
225 5c860e29 2018-03-12 stsp argv += optind;
226 9814e6a3 2020-09-27 naddy optind = 1;
227 9814e6a3 2020-09-27 naddy optreset = 1;
228 53ccebc2 2019-07-30 stsp
229 53ccebc2 2019-07-30 stsp if (Vflag) {
230 53ccebc2 2019-07-30 stsp got_version_print_str();
231 6879ba42 2020-10-01 naddy return 0;
232 53ccebc2 2019-07-30 stsp }
233 5c860e29 2018-03-12 stsp
234 5c860e29 2018-03-12 stsp if (argc <= 0)
235 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
236 5c860e29 2018-03-12 stsp
237 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
238 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
239 99437157 2018-11-11 stsp
240 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
241 d7d4f210 2018-03-12 stsp const struct got_error *error;
242 d7d4f210 2018-03-12 stsp
243 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
244 5c860e29 2018-03-12 stsp
245 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
246 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
247 5c860e29 2018-03-12 stsp continue;
248 5c860e29 2018-03-12 stsp
249 1b6b95a8 2018-03-12 stsp if (hflag)
250 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
251 1b6b95a8 2018-03-12 stsp
252 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
253 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
254 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
255 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
256 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
257 70015d7a 2019-11-08 stsp !(sigint_received &&
258 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
259 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
260 d7d4f210 2018-03-12 stsp return 1;
261 d7d4f210 2018-03-12 stsp }
262 d7d4f210 2018-03-12 stsp
263 d7d4f210 2018-03-12 stsp return 0;
264 5c860e29 2018-03-12 stsp }
265 5c860e29 2018-03-12 stsp
266 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
267 6879ba42 2020-10-01 naddy list_commands(stderr);
268 5c860e29 2018-03-12 stsp return 1;
269 5c860e29 2018-03-12 stsp }
270 5c860e29 2018-03-12 stsp
271 4ed7e80c 2018-05-20 stsp __dead static void
272 6879ba42 2020-10-01 naddy usage(int hflag, int status)
273 5c860e29 2018-03-12 stsp {
274 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
275 6879ba42 2020-10-01 naddy
276 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
277 53ccebc2 2019-07-30 stsp getprogname());
278 ce5b7c56 2019-07-09 stsp if (hflag)
279 6879ba42 2020-10-01 naddy list_commands(fp);
280 6879ba42 2020-10-01 naddy exit(status);
281 5c860e29 2018-03-12 stsp }
282 5c860e29 2018-03-12 stsp
283 0266afb7 2019-01-04 stsp static const struct got_error *
284 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
285 e2ba3d07 2019-05-13 stsp {
286 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
287 e2ba3d07 2019-05-13 stsp const char *editor;
288 8920fa04 2019-08-18 stsp
289 8920fa04 2019-08-18 stsp *abspath = NULL;
290 e2ba3d07 2019-05-13 stsp
291 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
292 e2ba3d07 2019-05-13 stsp if (editor == NULL)
293 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
294 e2ba3d07 2019-05-13 stsp
295 0ee7065d 2019-05-13 stsp if (editor) {
296 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
297 0ee7065d 2019-05-13 stsp if (err)
298 0ee7065d 2019-05-13 stsp return err;
299 0ee7065d 2019-05-13 stsp }
300 e2ba3d07 2019-05-13 stsp
301 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
302 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
303 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
304 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
305 0ee7065d 2019-05-13 stsp }
306 0ee7065d 2019-05-13 stsp
307 e2ba3d07 2019-05-13 stsp return NULL;
308 e2ba3d07 2019-05-13 stsp }
309 e2ba3d07 2019-05-13 stsp
310 e2ba3d07 2019-05-13 stsp static const struct got_error *
311 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
312 c530dc23 2019-07-23 stsp const char *worktree_path)
313 0266afb7 2019-01-04 stsp {
314 163ce85a 2019-05-13 stsp const struct got_error *err;
315 0266afb7 2019-01-04 stsp
316 37c06ea4 2019-07-15 stsp #ifdef PROFILE
317 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
318 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
319 37c06ea4 2019-07-15 stsp #endif
320 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
321 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
322 0266afb7 2019-01-04 stsp
323 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
324 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
325 0266afb7 2019-01-04 stsp
326 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
327 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
328 0266afb7 2019-01-04 stsp
329 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
330 163ce85a 2019-05-13 stsp if (err != NULL)
331 163ce85a 2019-05-13 stsp return err;
332 0266afb7 2019-01-04 stsp
333 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
334 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
335 0266afb7 2019-01-04 stsp
336 0266afb7 2019-01-04 stsp return NULL;
337 2c7829a4 2019-06-17 stsp }
338 2c7829a4 2019-06-17 stsp
339 2c7829a4 2019-06-17 stsp __dead static void
340 2c7829a4 2019-06-17 stsp usage_init(void)
341 2c7829a4 2019-06-17 stsp {
342 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
343 2c7829a4 2019-06-17 stsp exit(1);
344 2c7829a4 2019-06-17 stsp }
345 2c7829a4 2019-06-17 stsp
346 2c7829a4 2019-06-17 stsp static const struct got_error *
347 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
348 2c7829a4 2019-06-17 stsp {
349 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
350 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
351 2c7829a4 2019-06-17 stsp int ch;
352 2c7829a4 2019-06-17 stsp
353 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
354 2c7829a4 2019-06-17 stsp switch (ch) {
355 2c7829a4 2019-06-17 stsp default:
356 2c7829a4 2019-06-17 stsp usage_init();
357 2c7829a4 2019-06-17 stsp /* NOTREACHED */
358 2c7829a4 2019-06-17 stsp }
359 2c7829a4 2019-06-17 stsp }
360 2c7829a4 2019-06-17 stsp
361 2c7829a4 2019-06-17 stsp argc -= optind;
362 2c7829a4 2019-06-17 stsp argv += optind;
363 2c7829a4 2019-06-17 stsp
364 2c7829a4 2019-06-17 stsp #ifndef PROFILE
365 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
366 2c7829a4 2019-06-17 stsp err(1, "pledge");
367 2c7829a4 2019-06-17 stsp #endif
368 2c7829a4 2019-06-17 stsp if (argc != 1)
369 bc20e173 2019-06-17 stsp usage_init();
370 2c7829a4 2019-06-17 stsp
371 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
372 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
373 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
374 2c7829a4 2019-06-17 stsp
375 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
376 2c7829a4 2019-06-17 stsp
377 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
378 2c7829a4 2019-06-17 stsp if (error &&
379 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
380 2c7829a4 2019-06-17 stsp goto done;
381 2c7829a4 2019-06-17 stsp
382 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
383 2c7829a4 2019-06-17 stsp if (error)
384 2c7829a4 2019-06-17 stsp goto done;
385 2c7829a4 2019-06-17 stsp
386 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
387 3ce1b845 2019-07-15 stsp done:
388 3ce1b845 2019-07-15 stsp free(repo_path);
389 3ce1b845 2019-07-15 stsp return error;
390 3ce1b845 2019-07-15 stsp }
391 3ce1b845 2019-07-15 stsp
392 3ce1b845 2019-07-15 stsp __dead static void
393 3ce1b845 2019-07-15 stsp usage_import(void)
394 3ce1b845 2019-07-15 stsp {
395 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
396 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
397 3ce1b845 2019-07-15 stsp exit(1);
398 3ce1b845 2019-07-15 stsp }
399 3ce1b845 2019-07-15 stsp
400 3ce1b845 2019-07-15 stsp int
401 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
402 3ce1b845 2019-07-15 stsp {
403 3ce1b845 2019-07-15 stsp pid_t pid;
404 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
405 3ce1b845 2019-07-15 stsp int st = -1;
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
408 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
409 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
410 3ce1b845 2019-07-15 stsp
411 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
412 3ce1b845 2019-07-15 stsp case -1:
413 3ce1b845 2019-07-15 stsp goto doneediting;
414 3ce1b845 2019-07-15 stsp case 0:
415 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
416 3ce1b845 2019-07-15 stsp _exit(127);
417 3ce1b845 2019-07-15 stsp }
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
420 3ce1b845 2019-07-15 stsp if (errno != EINTR)
421 3ce1b845 2019-07-15 stsp break;
422 3ce1b845 2019-07-15 stsp
423 3ce1b845 2019-07-15 stsp doneediting:
424 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
425 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
426 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
427 3ce1b845 2019-07-15 stsp
428 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
429 3ce1b845 2019-07-15 stsp errno = EINTR;
430 3ce1b845 2019-07-15 stsp return -1;
431 3ce1b845 2019-07-15 stsp }
432 3ce1b845 2019-07-15 stsp
433 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
434 3ce1b845 2019-07-15 stsp }
435 3ce1b845 2019-07-15 stsp
436 3ce1b845 2019-07-15 stsp static const struct got_error *
437 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
438 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
439 28cf319f 2021-01-28 stsp int require_modification)
440 3ce1b845 2019-07-15 stsp {
441 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
442 bfa12d5e 2020-09-26 stsp char *line = NULL;
443 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
444 bfa12d5e 2020-09-26 stsp ssize_t linelen;
445 3ce1b845 2019-07-15 stsp struct stat st, st2;
446 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
447 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
448 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
449 3ce1b845 2019-07-15 stsp
450 3ce1b845 2019-07-15 stsp *logmsg = NULL;
451 3ce1b845 2019-07-15 stsp
452 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
453 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
454 3ce1b845 2019-07-15 stsp
455 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
456 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
457 3ce1b845 2019-07-15 stsp
458 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
459 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
460 3ce1b845 2019-07-15 stsp
461 28cf319f 2021-01-28 stsp if (require_modification &&
462 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
463 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
464 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
465 3ce1b845 2019-07-15 stsp
466 bfa12d5e 2020-09-26 stsp /*
467 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
468 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
469 bfa12d5e 2020-09-26 stsp * has in fact been edited.
470 bfa12d5e 2020-09-26 stsp */
471 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
472 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
473 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
474 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
475 bfa12d5e 2020-09-26 stsp
476 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
477 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
478 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
479 bfa12d5e 2020-09-26 stsp goto done;
480 bfa12d5e 2020-09-26 stsp }
481 bfa12d5e 2020-09-26 stsp s = buf;
482 bfa12d5e 2020-09-26 stsp len = 0;
483 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
484 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
485 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
486 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
487 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
488 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
489 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
490 bfa12d5e 2020-09-26 stsp goto done;
491 bfa12d5e 2020-09-26 stsp }
492 bfa12d5e 2020-09-26 stsp }
493 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
494 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
495 bfa12d5e 2020-09-26 stsp len--;
496 bfa12d5e 2020-09-26 stsp }
497 bfa12d5e 2020-09-26 stsp
498 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
499 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
500 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
501 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
502 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
503 3ce1b845 2019-07-15 stsp
504 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
505 3ce1b845 2019-07-15 stsp if (fp == NULL) {
506 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
507 3ce1b845 2019-07-15 stsp goto done;
508 3ce1b845 2019-07-15 stsp }
509 bfa12d5e 2020-09-26 stsp
510 bfa12d5e 2020-09-26 stsp len = 0;
511 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
512 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
513 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
514 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
515 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
516 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
517 bfa12d5e 2020-09-26 stsp goto done;
518 bfa12d5e 2020-09-26 stsp }
519 3ce1b845 2019-07-15 stsp }
520 bfa12d5e 2020-09-26 stsp free(line);
521 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
522 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
523 bfa12d5e 2020-09-26 stsp goto done;
524 bfa12d5e 2020-09-26 stsp }
525 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
526 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
527 3ce1b845 2019-07-15 stsp len--;
528 3ce1b845 2019-07-15 stsp }
529 3ce1b845 2019-07-15 stsp
530 bfa12d5e 2020-09-26 stsp if (len == 0) {
531 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
532 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
533 bfa12d5e 2020-09-26 stsp goto done;
534 bfa12d5e 2020-09-26 stsp }
535 28cf319f 2021-01-28 stsp if (require_modification &&
536 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
537 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
538 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
539 3ce1b845 2019-07-15 stsp done:
540 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
541 bfa12d5e 2020-09-26 stsp free(buf);
542 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
543 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
544 3ce1b845 2019-07-15 stsp if (err) {
545 3ce1b845 2019-07-15 stsp free(*logmsg);
546 3ce1b845 2019-07-15 stsp *logmsg = NULL;
547 3ce1b845 2019-07-15 stsp }
548 3ce1b845 2019-07-15 stsp return err;
549 3ce1b845 2019-07-15 stsp }
550 3ce1b845 2019-07-15 stsp
551 3ce1b845 2019-07-15 stsp static const struct got_error *
552 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
553 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
554 3ce1b845 2019-07-15 stsp {
555 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
556 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
557 1601cb9f 2020-09-11 naddy int initial_content_len;
558 97972933 2020-09-11 stsp int fd = -1;
559 3ce1b845 2019-07-15 stsp
560 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
561 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
562 1601cb9f 2020-09-11 naddy branch_name);
563 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
564 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
565 3ce1b845 2019-07-15 stsp
566 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
567 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
568 3ce1b845 2019-07-15 stsp if (err)
569 3ce1b845 2019-07-15 stsp goto done;
570 3ce1b845 2019-07-15 stsp
571 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
572 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
573 97972933 2020-09-11 stsp goto done;
574 97972933 2020-09-11 stsp }
575 3ce1b845 2019-07-15 stsp
576 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
577 0d5bb276 2020-12-15 stsp initial_content_len, 1);
578 3ce1b845 2019-07-15 stsp done:
579 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
580 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
581 3ce1b845 2019-07-15 stsp free(initial_content);
582 59f86c76 2020-09-11 stsp if (err) {
583 59f86c76 2020-09-11 stsp free(*logmsg_path);
584 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
585 59f86c76 2020-09-11 stsp }
586 3ce1b845 2019-07-15 stsp return err;
587 3ce1b845 2019-07-15 stsp }
588 3ce1b845 2019-07-15 stsp
589 3ce1b845 2019-07-15 stsp static const struct got_error *
590 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
591 3ce1b845 2019-07-15 stsp {
592 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
593 3ce1b845 2019-07-15 stsp return NULL;
594 3ce1b845 2019-07-15 stsp }
595 3ce1b845 2019-07-15 stsp
596 3ce1b845 2019-07-15 stsp static const struct got_error *
597 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
598 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
599 84792843 2019-08-09 stsp {
600 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
601 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
602 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
603 84792843 2019-08-09 stsp
604 84792843 2019-08-09 stsp *author = NULL;
605 aba9c984 2019-09-08 stsp
606 50b0790e 2020-09-11 stsp if (worktree)
607 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
608 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
609 50b0790e 2020-09-11 stsp
610 50b0790e 2020-09-11 stsp /*
611 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
612 50b0790e 2020-09-11 stsp * significant to least significant:
613 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
614 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
615 50b0790e 2020-09-11 stsp * 3) repository's git config file
616 50b0790e 2020-09-11 stsp * 4) environment variables
617 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
618 50b0790e 2020-09-11 stsp */
619 50b0790e 2020-09-11 stsp
620 50b0790e 2020-09-11 stsp if (worktree_conf)
621 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
622 50b0790e 2020-09-11 stsp if (got_author == NULL)
623 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
624 84792843 2019-08-09 stsp if (got_author == NULL) {
625 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
626 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
627 c9956ddf 2019-09-08 stsp if (name && email) {
628 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
629 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
630 c9956ddf 2019-09-08 stsp return NULL;
631 c9956ddf 2019-09-08 stsp }
632 257add31 2020-09-09 stsp
633 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
634 257add31 2020-09-09 stsp if (got_author == NULL) {
635 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
636 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
637 257add31 2020-09-09 stsp repo);
638 257add31 2020-09-09 stsp if (name && email) {
639 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
640 257add31 2020-09-09 stsp == -1)
641 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
642 257add31 2020-09-09 stsp return NULL;
643 257add31 2020-09-09 stsp }
644 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
645 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
646 257add31 2020-09-09 stsp }
647 84792843 2019-08-09 stsp }
648 84792843 2019-08-09 stsp
649 aba9c984 2019-09-08 stsp *author = strdup(got_author);
650 aba9c984 2019-09-08 stsp if (*author == NULL)
651 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
652 84792843 2019-08-09 stsp
653 84792843 2019-08-09 stsp /*
654 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
655 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
656 84792843 2019-08-09 stsp */
657 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
658 84792843 2019-08-09 stsp got_author++;
659 aba9c984 2019-09-08 stsp if (*got_author != '<') {
660 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
661 aba9c984 2019-09-08 stsp goto done;
662 aba9c984 2019-09-08 stsp }
663 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
664 84792843 2019-08-09 stsp got_author++;
665 aba9c984 2019-09-08 stsp if (*got_author != '@') {
666 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
667 aba9c984 2019-09-08 stsp goto done;
668 aba9c984 2019-09-08 stsp }
669 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
670 84792843 2019-08-09 stsp got_author++;
671 84792843 2019-08-09 stsp if (*got_author != '>')
672 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
673 aba9c984 2019-09-08 stsp done:
674 aba9c984 2019-09-08 stsp if (err) {
675 aba9c984 2019-09-08 stsp free(*author);
676 aba9c984 2019-09-08 stsp *author = NULL;
677 aba9c984 2019-09-08 stsp }
678 aba9c984 2019-09-08 stsp return err;
679 c9956ddf 2019-09-08 stsp }
680 c9956ddf 2019-09-08 stsp
681 c9956ddf 2019-09-08 stsp static const struct got_error *
682 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
683 c9956ddf 2019-09-08 stsp {
684 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
685 c9956ddf 2019-09-08 stsp
686 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
687 c9956ddf 2019-09-08 stsp if (homedir) {
688 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
689 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
690 c9956ddf 2019-09-08 stsp
691 c9956ddf 2019-09-08 stsp }
692 c9956ddf 2019-09-08 stsp return NULL;
693 84792843 2019-08-09 stsp }
694 84792843 2019-08-09 stsp
695 84792843 2019-08-09 stsp static const struct got_error *
696 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
697 3ce1b845 2019-07-15 stsp {
698 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
699 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
700 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
701 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
702 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
703 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
704 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
705 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
706 3ce1b845 2019-07-15 stsp int ch;
707 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
708 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
709 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
710 3ce1b845 2019-07-15 stsp
711 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
712 3ce1b845 2019-07-15 stsp
713 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
714 3ce1b845 2019-07-15 stsp switch (ch) {
715 3ce1b845 2019-07-15 stsp case 'b':
716 3ce1b845 2019-07-15 stsp branch_name = optarg;
717 3ce1b845 2019-07-15 stsp break;
718 3ce1b845 2019-07-15 stsp case 'm':
719 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
720 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
721 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
722 3ce1b845 2019-07-15 stsp goto done;
723 3ce1b845 2019-07-15 stsp }
724 3ce1b845 2019-07-15 stsp break;
725 3ce1b845 2019-07-15 stsp case 'r':
726 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
727 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
728 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
729 9ba1d308 2019-10-21 stsp optarg);
730 3ce1b845 2019-07-15 stsp goto done;
731 3ce1b845 2019-07-15 stsp }
732 3ce1b845 2019-07-15 stsp break;
733 3ce1b845 2019-07-15 stsp case 'I':
734 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
735 3ce1b845 2019-07-15 stsp break;
736 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
737 3ce1b845 2019-07-15 stsp NULL);
738 3ce1b845 2019-07-15 stsp if (error)
739 3ce1b845 2019-07-15 stsp goto done;
740 3ce1b845 2019-07-15 stsp break;
741 3ce1b845 2019-07-15 stsp default:
742 b2b65d18 2019-08-22 stsp usage_import();
743 3ce1b845 2019-07-15 stsp /* NOTREACHED */
744 3ce1b845 2019-07-15 stsp }
745 3ce1b845 2019-07-15 stsp }
746 3ce1b845 2019-07-15 stsp
747 3ce1b845 2019-07-15 stsp argc -= optind;
748 3ce1b845 2019-07-15 stsp argv += optind;
749 3ce1b845 2019-07-15 stsp
750 3ce1b845 2019-07-15 stsp #ifndef PROFILE
751 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
752 aba9c984 2019-09-08 stsp "unveil",
753 3ce1b845 2019-07-15 stsp NULL) == -1)
754 3ce1b845 2019-07-15 stsp err(1, "pledge");
755 3ce1b845 2019-07-15 stsp #endif
756 3ce1b845 2019-07-15 stsp if (argc != 1)
757 3ce1b845 2019-07-15 stsp usage_import();
758 2c7829a4 2019-06-17 stsp
759 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
760 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
761 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
762 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
763 3ce1b845 2019-07-15 stsp }
764 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
765 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
766 c9956ddf 2019-09-08 stsp if (error)
767 c9956ddf 2019-09-08 stsp goto done;
768 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
769 3ce1b845 2019-07-15 stsp if (error)
770 3ce1b845 2019-07-15 stsp goto done;
771 aba9c984 2019-09-08 stsp
772 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
773 aba9c984 2019-09-08 stsp if (error)
774 aba9c984 2019-09-08 stsp return error;
775 e560b7e0 2019-11-28 stsp
776 e560b7e0 2019-11-28 stsp /*
777 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
778 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
779 e560b7e0 2019-11-28 stsp * an unintended typo.
780 e560b7e0 2019-11-28 stsp */
781 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
782 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
783 3ce1b845 2019-07-15 stsp
784 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
785 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
786 3ce1b845 2019-07-15 stsp goto done;
787 3ce1b845 2019-07-15 stsp }
788 3ce1b845 2019-07-15 stsp
789 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
790 3ce1b845 2019-07-15 stsp if (error) {
791 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
792 3ce1b845 2019-07-15 stsp goto done;
793 3ce1b845 2019-07-15 stsp } else {
794 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
795 3ce1b845 2019-07-15 stsp "import target branch already exists");
796 3ce1b845 2019-07-15 stsp goto done;
797 3ce1b845 2019-07-15 stsp }
798 3ce1b845 2019-07-15 stsp
799 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
800 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
801 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
802 3ce1b845 2019-07-15 stsp goto done;
803 3ce1b845 2019-07-15 stsp }
804 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
805 3ce1b845 2019-07-15 stsp
806 3ce1b845 2019-07-15 stsp /*
807 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
808 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
809 3ce1b845 2019-07-15 stsp */
810 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
811 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
812 3ce1b845 2019-07-15 stsp if (error)
813 3ce1b845 2019-07-15 stsp goto done;
814 8e158b01 2019-09-22 stsp free(logmsg);
815 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
816 ef293bdd 2019-10-21 stsp path_dir, refname);
817 ef293bdd 2019-10-21 stsp if (error) {
818 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
819 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
820 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
821 3ce1b845 2019-07-15 stsp goto done;
822 ef293bdd 2019-10-21 stsp }
823 3ce1b845 2019-07-15 stsp }
824 3ce1b845 2019-07-15 stsp
825 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
826 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
827 ef293bdd 2019-10-21 stsp if (logmsg_path)
828 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
829 3ce1b845 2019-07-15 stsp goto done;
830 ef293bdd 2019-10-21 stsp }
831 3ce1b845 2019-07-15 stsp
832 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
833 ef293bdd 2019-10-21 stsp if (error) {
834 ef293bdd 2019-10-21 stsp if (logmsg_path)
835 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
836 ef293bdd 2019-10-21 stsp goto done;
837 ef293bdd 2019-10-21 stsp }
838 ef293bdd 2019-10-21 stsp
839 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
840 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
841 ef293bdd 2019-10-21 stsp if (error) {
842 ef293bdd 2019-10-21 stsp if (logmsg_path)
843 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
844 3ce1b845 2019-07-15 stsp goto done;
845 ef293bdd 2019-10-21 stsp }
846 3ce1b845 2019-07-15 stsp
847 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
848 ef293bdd 2019-10-21 stsp if (error) {
849 ef293bdd 2019-10-21 stsp if (logmsg_path)
850 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
851 3ce1b845 2019-07-15 stsp goto done;
852 ef293bdd 2019-10-21 stsp }
853 3ce1b845 2019-07-15 stsp
854 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
855 ef293bdd 2019-10-21 stsp if (error) {
856 ef293bdd 2019-10-21 stsp if (logmsg_path)
857 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
858 3ce1b845 2019-07-15 stsp goto done;
859 ef293bdd 2019-10-21 stsp }
860 3ce1b845 2019-07-15 stsp
861 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
862 ef293bdd 2019-10-21 stsp if (error) {
863 ef293bdd 2019-10-21 stsp if (logmsg_path)
864 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
865 3ce1b845 2019-07-15 stsp goto done;
866 ef293bdd 2019-10-21 stsp }
867 3ce1b845 2019-07-15 stsp
868 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
869 3ce1b845 2019-07-15 stsp if (error) {
870 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
871 ef293bdd 2019-10-21 stsp if (logmsg_path)
872 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
873 3ce1b845 2019-07-15 stsp goto done;
874 ef293bdd 2019-10-21 stsp }
875 3ce1b845 2019-07-15 stsp
876 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
877 3ce1b845 2019-07-15 stsp branch_ref);
878 ef293bdd 2019-10-21 stsp if (error) {
879 ef293bdd 2019-10-21 stsp if (logmsg_path)
880 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
881 3ce1b845 2019-07-15 stsp goto done;
882 ef293bdd 2019-10-21 stsp }
883 3ce1b845 2019-07-15 stsp
884 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
885 ef293bdd 2019-10-21 stsp if (error) {
886 ef293bdd 2019-10-21 stsp if (logmsg_path)
887 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
888 3ce1b845 2019-07-15 stsp goto done;
889 ef293bdd 2019-10-21 stsp }
890 3ce1b845 2019-07-15 stsp }
891 3ce1b845 2019-07-15 stsp
892 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
893 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
894 2c7829a4 2019-06-17 stsp done:
895 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
896 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
897 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
898 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
899 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
900 8e158b01 2019-09-22 stsp free(logmsg);
901 ef293bdd 2019-10-21 stsp free(logmsg_path);
902 2c7829a4 2019-06-17 stsp free(repo_path);
903 3ce1b845 2019-07-15 stsp free(editor);
904 3ce1b845 2019-07-15 stsp free(refname);
905 3ce1b845 2019-07-15 stsp free(new_commit_id);
906 3ce1b845 2019-07-15 stsp free(id_str);
907 aba9c984 2019-09-08 stsp free(author);
908 c9956ddf 2019-09-08 stsp free(gitconfig_path);
909 3ce1b845 2019-07-15 stsp if (branch_ref)
910 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
911 3ce1b845 2019-07-15 stsp if (head_ref)
912 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
913 2c7829a4 2019-06-17 stsp return error;
914 93658fb9 2020-03-18 stsp }
915 93658fb9 2020-03-18 stsp
916 93658fb9 2020-03-18 stsp __dead static void
917 93658fb9 2020-03-18 stsp usage_clone(void)
918 93658fb9 2020-03-18 stsp {
919 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
920 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
921 93658fb9 2020-03-18 stsp exit(1);
922 93658fb9 2020-03-18 stsp }
923 892ac3b6 2020-03-18 stsp
924 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
925 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
926 892ac3b6 2020-03-18 stsp int last_p_indexed;
927 892ac3b6 2020-03-18 stsp int last_p_resolved;
928 68999b92 2020-03-18 stsp int verbosity;
929 04d9a9ec 2020-09-24 stsp
930 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
931 04d9a9ec 2020-09-24 stsp
932 04d9a9ec 2020-09-24 stsp int create_configs;
933 04d9a9ec 2020-09-24 stsp int configs_created;
934 04d9a9ec 2020-09-24 stsp struct {
935 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
936 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
937 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
938 04d9a9ec 2020-09-24 stsp const char *proto;
939 04d9a9ec 2020-09-24 stsp const char *host;
940 04d9a9ec 2020-09-24 stsp const char *port;
941 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
942 04d9a9ec 2020-09-24 stsp const char *git_url;
943 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
944 04d9a9ec 2020-09-24 stsp int mirror_references;
945 04d9a9ec 2020-09-24 stsp } config_info;
946 892ac3b6 2020-03-18 stsp };
947 93658fb9 2020-03-18 stsp
948 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
949 93658fb9 2020-03-18 stsp static const struct got_error *
950 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
951 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
952 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
953 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
954 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
955 04d9a9ec 2020-09-24 stsp
956 04d9a9ec 2020-09-24 stsp static const struct got_error *
957 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
958 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
959 531c3985 2020-03-18 stsp {
960 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
961 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
962 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
963 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
964 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
965 04d9a9ec 2020-09-24 stsp
966 04d9a9ec 2020-09-24 stsp /*
967 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
968 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
969 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
970 04d9a9ec 2020-09-24 stsp * we have all required information.
971 04d9a9ec 2020-09-24 stsp */
972 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
973 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
974 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
975 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
976 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
977 62d463ca 2020-10-20 naddy a->config_info.git_url,
978 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
979 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
980 62d463ca 2020-10-20 naddy a->config_info.symrefs,
981 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
982 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
983 04d9a9ec 2020-09-24 stsp if (err)
984 04d9a9ec 2020-09-24 stsp return err;
985 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
986 04d9a9ec 2020-09-24 stsp }
987 b2409d58 2020-03-18 stsp
988 68999b92 2020-03-18 stsp if (a->verbosity < 0)
989 68999b92 2020-03-18 stsp return NULL;
990 68999b92 2020-03-18 stsp
991 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
992 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
993 892ac3b6 2020-03-18 stsp fflush(stdout);
994 12d1281e 2020-03-19 stsp return NULL;
995 b2409d58 2020-03-18 stsp }
996 b2409d58 2020-03-18 stsp
997 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
998 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
999 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
1000 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
1001 892ac3b6 2020-03-18 stsp print_size = 1;
1002 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
1003 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1004 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1005 892ac3b6 2020-03-18 stsp }
1006 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1007 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1008 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1009 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1010 892ac3b6 2020-03-18 stsp print_indexed = 1;
1011 892ac3b6 2020-03-18 stsp print_size = 1;
1012 892ac3b6 2020-03-18 stsp }
1013 61cc1a7a 2020-03-18 stsp }
1014 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1015 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1016 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1017 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1018 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1019 892ac3b6 2020-03-18 stsp print_resolved = 1;
1020 892ac3b6 2020-03-18 stsp print_indexed = 1;
1021 892ac3b6 2020-03-18 stsp print_size = 1;
1022 892ac3b6 2020-03-18 stsp }
1023 61cc1a7a 2020-03-18 stsp }
1024 3168e5da 2020-09-10 stsp
1025 d2cdc636 2020-03-18 stsp }
1026 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1027 892ac3b6 2020-03-18 stsp printf("\r");
1028 892ac3b6 2020-03-18 stsp if (print_size)
1029 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1030 d715f13e 2020-03-19 stsp if (print_indexed)
1031 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1032 d715f13e 2020-03-19 stsp if (print_resolved)
1033 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1034 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1035 892ac3b6 2020-03-18 stsp fflush(stdout);
1036 892ac3b6 2020-03-18 stsp
1037 531c3985 2020-03-18 stsp return NULL;
1038 531c3985 2020-03-18 stsp }
1039 531c3985 2020-03-18 stsp
1040 531c3985 2020-03-18 stsp static const struct got_error *
1041 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1042 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1043 4ba14133 2020-03-20 stsp {
1044 4ba14133 2020-03-20 stsp const struct got_error *err;
1045 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1046 4ba14133 2020-03-20 stsp
1047 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1048 4ba14133 2020-03-20 stsp if (err)
1049 4ba14133 2020-03-20 stsp return err;
1050 4ba14133 2020-03-20 stsp
1051 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1052 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1053 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1054 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1055 6338a6a1 2020-03-21 stsp }
1056 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1057 4ba14133 2020-03-20 stsp return err;
1058 4ba14133 2020-03-20 stsp }
1059 4ba14133 2020-03-20 stsp
1060 4ba14133 2020-03-20 stsp static const struct got_error *
1061 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1062 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1063 41b0de12 2020-03-21 stsp {
1064 41b0de12 2020-03-21 stsp const struct got_error *err;
1065 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1066 41b0de12 2020-03-21 stsp
1067 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1068 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1069 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1070 41b0de12 2020-03-21 stsp
1071 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1072 41b0de12 2020-03-21 stsp }
1073 41b0de12 2020-03-21 stsp
1074 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1075 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1076 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1077 41b0de12 2020-03-21 stsp char *id_str;
1078 41b0de12 2020-03-21 stsp
1079 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1080 41b0de12 2020-03-21 stsp if (err)
1081 41b0de12 2020-03-21 stsp return err;
1082 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1083 41b0de12 2020-03-21 stsp free(id_str);
1084 41b0de12 2020-03-21 stsp }
1085 41b0de12 2020-03-21 stsp
1086 41b0de12 2020-03-21 stsp return NULL;
1087 6338a6a1 2020-03-21 stsp }
1088 6338a6a1 2020-03-21 stsp
1089 6338a6a1 2020-03-21 stsp static const struct got_error *
1090 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1091 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1092 6338a6a1 2020-03-21 stsp {
1093 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1094 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1095 6338a6a1 2020-03-21 stsp char *id_str;
1096 6338a6a1 2020-03-21 stsp
1097 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1098 6338a6a1 2020-03-21 stsp if (err)
1099 6338a6a1 2020-03-21 stsp return err;
1100 6338a6a1 2020-03-21 stsp
1101 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1102 6338a6a1 2020-03-21 stsp if (err)
1103 6338a6a1 2020-03-21 stsp goto done;
1104 6338a6a1 2020-03-21 stsp
1105 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1106 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1107 6338a6a1 2020-03-21 stsp
1108 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1109 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1110 6338a6a1 2020-03-21 stsp done:
1111 6338a6a1 2020-03-21 stsp free(id_str);
1112 6338a6a1 2020-03-21 stsp return err;
1113 0e4002ca 2020-03-21 stsp }
1114 0e4002ca 2020-03-21 stsp
1115 0e4002ca 2020-03-21 stsp static int
1116 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1117 0e4002ca 2020-03-21 stsp {
1118 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1119 0e4002ca 2020-03-21 stsp return 0;
1120 0e4002ca 2020-03-21 stsp refname += 5;
1121 0e4002ca 2020-03-21 stsp
1122 0e4002ca 2020-03-21 stsp /*
1123 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1124 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1125 0e4002ca 2020-03-21 stsp */
1126 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1127 0e4002ca 2020-03-21 stsp return 0;
1128 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1129 0e4002ca 2020-03-21 stsp return 0;
1130 0e4002ca 2020-03-21 stsp
1131 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1132 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1133 0e4002ca 2020-03-21 stsp
1134 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1135 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1136 0e4002ca 2020-03-21 stsp return 1;
1137 0e4002ca 2020-03-21 stsp
1138 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1139 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1140 41b0de12 2020-03-21 stsp }
1141 41b0de12 2020-03-21 stsp
1142 0e4002ca 2020-03-21 stsp static int
1143 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1144 0e4002ca 2020-03-21 stsp {
1145 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1146 0e4002ca 2020-03-21 stsp
1147 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1148 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1149 0e4002ca 2020-03-21 stsp return 1;
1150 0e4002ca 2020-03-21 stsp }
1151 0e4002ca 2020-03-21 stsp
1152 0e4002ca 2020-03-21 stsp return 0;
1153 0e4002ca 2020-03-21 stsp }
1154 0e4002ca 2020-03-21 stsp
1155 41b0de12 2020-03-21 stsp static const struct got_error *
1156 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1157 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1158 0e4002ca 2020-03-21 stsp {
1159 0e4002ca 2020-03-21 stsp const struct got_error *err;
1160 0e4002ca 2020-03-21 stsp char *remote_refname;
1161 0e4002ca 2020-03-21 stsp
1162 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1163 0e4002ca 2020-03-21 stsp refname += 5;
1164 0e4002ca 2020-03-21 stsp
1165 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1166 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1167 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1168 0e4002ca 2020-03-21 stsp
1169 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1170 0e4002ca 2020-03-21 stsp free(remote_refname);
1171 7c0b7f42 2020-09-24 stsp return err;
1172 7c0b7f42 2020-09-24 stsp }
1173 7c0b7f42 2020-09-24 stsp
1174 7c0b7f42 2020-09-24 stsp static const struct got_error *
1175 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1176 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1177 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1178 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1179 99495ddb 2021-01-10 stsp struct got_repository *repo)
1180 7c0b7f42 2020-09-24 stsp {
1181 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1182 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1183 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1184 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1185 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1186 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1187 7c0b7f42 2020-09-24 stsp ssize_t n;
1188 7c0b7f42 2020-09-24 stsp
1189 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1190 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1191 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1192 132af4a5 2021-01-05 stsp char *s;
1193 132af4a5 2021-01-05 stsp branchname = pe->path;
1194 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1195 132af4a5 2021-01-05 stsp branchname += 11;
1196 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1197 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1198 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1199 132af4a5 2021-01-05 stsp goto done;
1200 132af4a5 2021-01-05 stsp }
1201 132af4a5 2021-01-05 stsp free(branches);
1202 132af4a5 2021-01-05 stsp branches = s;
1203 132af4a5 2021-01-05 stsp }
1204 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1205 15d3c221 2021-01-05 stsp branchname = default_branch;
1206 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1207 15d3c221 2021-01-05 stsp branchname += 11;
1208 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1209 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1210 132af4a5 2021-01-05 stsp goto done;
1211 99495ddb 2021-01-10 stsp }
1212 99495ddb 2021-01-10 stsp }
1213 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1214 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1215 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1216 99495ddb 2021-01-10 stsp char *s;
1217 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1218 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1219 99495ddb 2021-01-10 stsp branchname += 5;
1220 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1221 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1222 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1223 99495ddb 2021-01-10 stsp goto done;
1224 99495ddb 2021-01-10 stsp }
1225 99495ddb 2021-01-10 stsp free(refs);
1226 99495ddb 2021-01-10 stsp refs = s;
1227 132af4a5 2021-01-05 stsp }
1228 15d3c221 2021-01-05 stsp }
1229 15d3c221 2021-01-05 stsp
1230 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1231 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1232 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1233 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1234 7c0b7f42 2020-09-24 stsp goto done;
1235 7c0b7f42 2020-09-24 stsp }
1236 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1237 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1238 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1239 7c0b7f42 2020-09-24 stsp goto done;
1240 7c0b7f42 2020-09-24 stsp }
1241 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1242 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1243 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1244 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1245 7c0b7f42 2020-09-24 stsp "%s%s%s"
1246 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1247 15d3c221 2021-01-05 stsp "%s%s%s"
1248 99495ddb 2021-01-10 stsp "%s%s%s"
1249 7c0b7f42 2020-09-24 stsp "%s"
1250 0c8b29c5 2021-01-05 stsp "%s"
1251 7c0b7f42 2020-09-24 stsp "}\n",
1252 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1253 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1254 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1255 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1256 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1257 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1258 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1259 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1260 7c0b7f42 2020-09-24 stsp goto done;
1261 7c0b7f42 2020-09-24 stsp }
1262 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1263 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1264 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1265 7c0b7f42 2020-09-24 stsp goto done;
1266 7c0b7f42 2020-09-24 stsp }
1267 7c0b7f42 2020-09-24 stsp
1268 7c0b7f42 2020-09-24 stsp done:
1269 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1270 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1271 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1272 132af4a5 2021-01-05 stsp free(branches);
1273 7c0b7f42 2020-09-24 stsp return err;
1274 7c0b7f42 2020-09-24 stsp }
1275 7c0b7f42 2020-09-24 stsp
1276 7c0b7f42 2020-09-24 stsp static const struct got_error *
1277 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1278 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1279 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1280 99495ddb 2021-01-10 stsp struct got_repository *repo)
1281 7c0b7f42 2020-09-24 stsp {
1282 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1283 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1284 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1285 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1286 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1287 56d0a753 2021-01-20 stsp const char *branchname;
1288 7c0b7f42 2020-09-24 stsp ssize_t n;
1289 7c0b7f42 2020-09-24 stsp
1290 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1291 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1292 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1293 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1294 7c0b7f42 2020-09-24 stsp goto done;
1295 7c0b7f42 2020-09-24 stsp }
1296 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1297 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1298 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1299 7c0b7f42 2020-09-24 stsp goto done;
1300 7c0b7f42 2020-09-24 stsp }
1301 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1302 56d0a753 2021-01-20 stsp if (mirror_references) {
1303 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1304 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1305 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1306 56d0a753 2021-01-20 stsp goto done;
1307 56d0a753 2021-01-20 stsp }
1308 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1309 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1310 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1311 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1312 7c0b7f42 2020-09-24 stsp goto done;
1313 132af4a5 2021-01-05 stsp }
1314 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1315 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1316 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1317 132af4a5 2021-01-05 stsp char *s;
1318 132af4a5 2021-01-05 stsp branchname = pe->path;
1319 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1320 132af4a5 2021-01-05 stsp branchname += 11;
1321 56d0a753 2021-01-20 stsp if (mirror_references) {
1322 56d0a753 2021-01-20 stsp if (asprintf(&s,
1323 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1324 56d0a753 2021-01-20 stsp branches ? branches : "",
1325 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1326 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1327 56d0a753 2021-01-20 stsp goto done;
1328 56d0a753 2021-01-20 stsp }
1329 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1330 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1331 132af4a5 2021-01-05 stsp branches ? branches : "",
1332 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1333 132af4a5 2021-01-05 stsp branchname) == -1) {
1334 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1335 132af4a5 2021-01-05 stsp goto done;
1336 132af4a5 2021-01-05 stsp }
1337 132af4a5 2021-01-05 stsp free(branches);
1338 132af4a5 2021-01-05 stsp branches = s;
1339 7c0b7f42 2020-09-24 stsp }
1340 7c0b7f42 2020-09-24 stsp } else {
1341 7c0b7f42 2020-09-24 stsp /*
1342 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1343 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1344 7c0b7f42 2020-09-24 stsp */
1345 04d9a9ec 2020-09-24 stsp if (default_branch) {
1346 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1347 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1348 7c0b7f42 2020-09-24 stsp branchname += 11;
1349 7c0b7f42 2020-09-24 stsp } else
1350 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1351 56d0a753 2021-01-20 stsp if (mirror_references) {
1352 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1353 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1354 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1355 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1356 56d0a753 2021-01-20 stsp goto done;
1357 56d0a753 2021-01-20 stsp }
1358 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1359 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1360 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1361 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1362 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1363 7c0b7f42 2020-09-24 stsp goto done;
1364 99495ddb 2021-01-10 stsp }
1365 99495ddb 2021-01-10 stsp }
1366 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1367 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1368 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1369 99495ddb 2021-01-10 stsp char *s;
1370 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1371 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1372 99495ddb 2021-01-10 stsp refname += 5;
1373 56d0a753 2021-01-20 stsp if (mirror_references) {
1374 56d0a753 2021-01-20 stsp if (asprintf(&s,
1375 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1376 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1377 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1378 56d0a753 2021-01-20 stsp goto done;
1379 56d0a753 2021-01-20 stsp }
1380 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1381 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1382 99495ddb 2021-01-10 stsp refs ? refs : "",
1383 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1384 99495ddb 2021-01-10 stsp refname) == -1) {
1385 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1386 99495ddb 2021-01-10 stsp goto done;
1387 99495ddb 2021-01-10 stsp }
1388 99495ddb 2021-01-10 stsp free(refs);
1389 99495ddb 2021-01-10 stsp refs = s;
1390 7c0b7f42 2020-09-24 stsp }
1391 132af4a5 2021-01-05 stsp }
1392 99495ddb 2021-01-10 stsp
1393 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1394 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1395 132af4a5 2021-01-05 stsp "\turl = %s\n"
1396 132af4a5 2021-01-05 stsp "%s"
1397 99495ddb 2021-01-10 stsp "%s"
1398 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1399 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1400 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1401 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1402 132af4a5 2021-01-05 stsp goto done;
1403 7c0b7f42 2020-09-24 stsp }
1404 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1405 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1406 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1407 7c0b7f42 2020-09-24 stsp goto done;
1408 7c0b7f42 2020-09-24 stsp }
1409 7c0b7f42 2020-09-24 stsp done:
1410 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1411 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1412 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1413 132af4a5 2021-01-05 stsp free(branches);
1414 0e4002ca 2020-03-21 stsp return err;
1415 0e4002ca 2020-03-21 stsp }
1416 0e4002ca 2020-03-21 stsp
1417 0e4002ca 2020-03-21 stsp static const struct got_error *
1418 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1419 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1420 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1421 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1422 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1423 04d9a9ec 2020-09-24 stsp {
1424 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1425 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1426 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1427 04d9a9ec 2020-09-24 stsp
1428 04d9a9ec 2020-09-24 stsp /*
1429 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1430 04d9a9ec 2020-09-24 stsp * one of those.
1431 04d9a9ec 2020-09-24 stsp */
1432 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1433 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1434 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1435 04d9a9ec 2020-09-24 stsp } else {
1436 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1437 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1438 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1439 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1440 04d9a9ec 2020-09-24 stsp
1441 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1442 04d9a9ec 2020-09-24 stsp continue;
1443 04d9a9ec 2020-09-24 stsp
1444 04d9a9ec 2020-09-24 stsp default_branch = target;
1445 04d9a9ec 2020-09-24 stsp break;
1446 04d9a9ec 2020-09-24 stsp }
1447 04d9a9ec 2020-09-24 stsp }
1448 04d9a9ec 2020-09-24 stsp
1449 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1450 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1451 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1452 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1453 04d9a9ec 2020-09-24 stsp if (err)
1454 04d9a9ec 2020-09-24 stsp return err;
1455 04d9a9ec 2020-09-24 stsp
1456 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1457 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1458 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1459 04d9a9ec 2020-09-24 stsp }
1460 04d9a9ec 2020-09-24 stsp
1461 04d9a9ec 2020-09-24 stsp static const struct got_error *
1462 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1463 93658fb9 2020-03-18 stsp {
1464 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1465 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1466 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1467 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1468 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1469 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1470 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1471 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1472 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1473 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1474 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1475 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1476 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1477 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1478 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1479 93658fb9 2020-03-18 stsp
1480 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1481 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1482 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1483 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1484 d9b4d0c0 2020-03-18 stsp
1485 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1486 93658fb9 2020-03-18 stsp switch (ch) {
1487 659e7fbd 2020-03-20 stsp case 'a':
1488 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1489 4ba14133 2020-03-20 stsp break;
1490 4ba14133 2020-03-20 stsp case 'b':
1491 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1492 4ba14133 2020-03-20 stsp optarg, NULL);
1493 4ba14133 2020-03-20 stsp if (error)
1494 4ba14133 2020-03-20 stsp return error;
1495 659e7fbd 2020-03-20 stsp break;
1496 41b0de12 2020-03-21 stsp case 'l':
1497 41b0de12 2020-03-21 stsp list_refs_only = 1;
1498 41b0de12 2020-03-21 stsp break;
1499 469dd726 2020-03-20 stsp case 'm':
1500 469dd726 2020-03-20 stsp mirror_references = 1;
1501 469dd726 2020-03-20 stsp break;
1502 68999b92 2020-03-18 stsp case 'v':
1503 68999b92 2020-03-18 stsp if (verbosity < 0)
1504 68999b92 2020-03-18 stsp verbosity = 0;
1505 68999b92 2020-03-18 stsp else if (verbosity < 3)
1506 68999b92 2020-03-18 stsp verbosity++;
1507 68999b92 2020-03-18 stsp break;
1508 68999b92 2020-03-18 stsp case 'q':
1509 68999b92 2020-03-18 stsp verbosity = -1;
1510 68999b92 2020-03-18 stsp break;
1511 0e4002ca 2020-03-21 stsp case 'R':
1512 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1513 0e4002ca 2020-03-21 stsp optarg, NULL);
1514 0e4002ca 2020-03-21 stsp if (error)
1515 0e4002ca 2020-03-21 stsp return error;
1516 0e4002ca 2020-03-21 stsp break;
1517 93658fb9 2020-03-18 stsp default:
1518 93658fb9 2020-03-18 stsp usage_clone();
1519 93658fb9 2020-03-18 stsp break;
1520 93658fb9 2020-03-18 stsp }
1521 93658fb9 2020-03-18 stsp }
1522 93658fb9 2020-03-18 stsp argc -= optind;
1523 93658fb9 2020-03-18 stsp argv += optind;
1524 39c64a6a 2020-03-18 stsp
1525 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1526 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1527 41b0de12 2020-03-21 stsp if (list_refs_only) {
1528 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1529 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1530 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1531 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1532 41b0de12 2020-03-21 stsp if (mirror_references)
1533 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1534 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1535 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1536 41b0de12 2020-03-21 stsp }
1537 4ba14133 2020-03-20 stsp
1538 93658fb9 2020-03-18 stsp uri = argv[0];
1539 9df6f38b 2020-03-18 stsp
1540 9df6f38b 2020-03-18 stsp if (argc == 1)
1541 93658fb9 2020-03-18 stsp dirname = NULL;
1542 9df6f38b 2020-03-18 stsp else if (argc == 2)
1543 93658fb9 2020-03-18 stsp dirname = argv[1];
1544 93658fb9 2020-03-18 stsp else
1545 93658fb9 2020-03-18 stsp usage_clone();
1546 09838ffc 2020-03-18 stsp
1547 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1548 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1549 39c64a6a 2020-03-18 stsp if (error)
1550 09f63084 2020-03-20 stsp goto done;
1551 09f63084 2020-03-20 stsp
1552 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1553 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1554 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1555 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1556 09838ffc 2020-03-18 stsp goto done;
1557 09f63084 2020-03-20 stsp }
1558 09838ffc 2020-03-18 stsp
1559 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1560 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1561 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1562 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1563 39c64a6a 2020-03-18 stsp err(1, "pledge");
1564 b46f3e71 2020-03-18 stsp #endif
1565 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1566 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1567 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1568 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1569 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1570 39c64a6a 2020-03-18 stsp err(1, "pledge");
1571 b46f3e71 2020-03-18 stsp #endif
1572 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1573 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1574 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1575 39c64a6a 2020-03-18 stsp goto done;
1576 39c64a6a 2020-03-18 stsp } else {
1577 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1578 39c64a6a 2020-03-18 stsp goto done;
1579 39c64a6a 2020-03-18 stsp }
1580 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1581 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1582 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1583 bb64b798 2020-03-18 stsp goto done;
1584 bb64b798 2020-03-18 stsp }
1585 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1586 bb64b798 2020-03-18 stsp } else
1587 bb64b798 2020-03-18 stsp repo_path = dirname;
1588 bb64b798 2020-03-18 stsp
1589 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1590 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1591 2751fe64 2020-09-24 stsp if (error &&
1592 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1593 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1594 41b0de12 2020-03-21 stsp goto done;
1595 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1596 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1597 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1598 41b0de12 2020-03-21 stsp goto done;
1599 2751fe64 2020-09-24 stsp }
1600 41b0de12 2020-03-21 stsp }
1601 bb64b798 2020-03-18 stsp
1602 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1603 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1604 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1605 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1606 ee448f5f 2020-03-18 stsp goto done;
1607 ee448f5f 2020-03-18 stsp }
1608 ee448f5f 2020-03-18 stsp }
1609 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1610 ee448f5f 2020-03-18 stsp if (error)
1611 ee448f5f 2020-03-18 stsp goto done;
1612 f79e6490 2020-04-19 stsp
1613 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1614 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1615 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1616 ee448f5f 2020-03-18 stsp
1617 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1618 9c52365f 2020-03-21 stsp server_path, verbosity);
1619 39c64a6a 2020-03-18 stsp if (error)
1620 bb64b798 2020-03-18 stsp goto done;
1621 bb64b798 2020-03-18 stsp
1622 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1623 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1624 2751fe64 2020-09-24 stsp if (error)
1625 2751fe64 2020-09-24 stsp goto done;
1626 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1627 2751fe64 2020-09-24 stsp if (error)
1628 2751fe64 2020-09-24 stsp goto done;
1629 2751fe64 2020-09-24 stsp }
1630 2751fe64 2020-09-24 stsp
1631 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1632 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1633 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1634 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1635 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1636 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1637 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1639 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1640 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1641 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1644 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1645 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1646 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1647 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1648 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1649 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1650 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1651 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1652 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1653 39c64a6a 2020-03-18 stsp if (error)
1654 d9b4d0c0 2020-03-18 stsp goto done;
1655 d9b4d0c0 2020-03-18 stsp
1656 41b0de12 2020-03-21 stsp if (list_refs_only) {
1657 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1658 41b0de12 2020-03-21 stsp goto done;
1659 41b0de12 2020-03-21 stsp }
1660 41b0de12 2020-03-21 stsp
1661 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1662 39c64a6a 2020-03-18 stsp if (error)
1663 d9b4d0c0 2020-03-18 stsp goto done;
1664 68999b92 2020-03-18 stsp if (verbosity >= 0)
1665 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1666 d9b4d0c0 2020-03-18 stsp free(id_str);
1667 d9b4d0c0 2020-03-18 stsp
1668 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1669 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1670 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1671 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1672 7ebc0570 2020-03-18 stsp char *remote_refname;
1673 0e4002ca 2020-03-21 stsp
1674 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1675 0e4002ca 2020-03-21 stsp !mirror_references) {
1676 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1677 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1678 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1679 0e4002ca 2020-03-21 stsp if (error)
1680 0e4002ca 2020-03-21 stsp goto done;
1681 0e4002ca 2020-03-21 stsp continue;
1682 0e4002ca 2020-03-21 stsp }
1683 668a20f6 2020-03-18 stsp
1684 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1685 39c64a6a 2020-03-18 stsp if (error)
1686 d9b4d0c0 2020-03-18 stsp goto done;
1687 d9b4d0c0 2020-03-18 stsp
1688 469dd726 2020-03-20 stsp if (mirror_references)
1689 469dd726 2020-03-20 stsp continue;
1690 469dd726 2020-03-20 stsp
1691 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1692 7ebc0570 2020-03-18 stsp continue;
1693 7ebc0570 2020-03-18 stsp
1694 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1695 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1696 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1697 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1698 7ebc0570 2020-03-18 stsp goto done;
1699 7ebc0570 2020-03-18 stsp }
1700 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1701 f298ae0f 2020-03-25 stsp free(remote_refname);
1702 39c64a6a 2020-03-18 stsp if (error)
1703 d9b4d0c0 2020-03-18 stsp goto done;
1704 d9b4d0c0 2020-03-18 stsp }
1705 d9b4d0c0 2020-03-18 stsp
1706 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1707 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1708 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1709 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1710 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1711 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1712 d9b4d0c0 2020-03-18 stsp
1713 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1714 d9b4d0c0 2020-03-18 stsp continue;
1715 d9b4d0c0 2020-03-18 stsp
1716 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1717 39c64a6a 2020-03-18 stsp if (error) {
1718 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1719 55330abe 2020-03-20 stsp error = NULL;
1720 d9b4d0c0 2020-03-18 stsp continue;
1721 55330abe 2020-03-20 stsp }
1722 d9b4d0c0 2020-03-18 stsp goto done;
1723 d9b4d0c0 2020-03-18 stsp }
1724 d9b4d0c0 2020-03-18 stsp
1725 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1726 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1727 f298ae0f 2020-03-25 stsp if (error)
1728 f298ae0f 2020-03-25 stsp goto done;
1729 f298ae0f 2020-03-25 stsp
1730 f298ae0f 2020-03-25 stsp if (mirror_references)
1731 f298ae0f 2020-03-25 stsp continue;
1732 f298ae0f 2020-03-25 stsp
1733 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1734 f298ae0f 2020-03-25 stsp continue;
1735 f298ae0f 2020-03-25 stsp
1736 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1737 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1738 f298ae0f 2020-03-25 stsp refname) == -1) {
1739 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1740 f298ae0f 2020-03-25 stsp goto done;
1741 f298ae0f 2020-03-25 stsp }
1742 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1743 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1744 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1745 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1746 f298ae0f 2020-03-25 stsp free(remote_refname);
1747 f298ae0f 2020-03-25 stsp goto done;
1748 f298ae0f 2020-03-25 stsp }
1749 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1750 f298ae0f 2020-03-25 stsp if (error) {
1751 f298ae0f 2020-03-25 stsp free(remote_refname);
1752 f298ae0f 2020-03-25 stsp free(remote_target);
1753 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1754 f298ae0f 2020-03-25 stsp error = NULL;
1755 f298ae0f 2020-03-25 stsp continue;
1756 f298ae0f 2020-03-25 stsp }
1757 f298ae0f 2020-03-25 stsp goto done;
1758 f298ae0f 2020-03-25 stsp }
1759 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1760 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1761 f298ae0f 2020-03-25 stsp free(remote_refname);
1762 f298ae0f 2020-03-25 stsp free(remote_target);
1763 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1764 39c64a6a 2020-03-18 stsp if (error)
1765 d9b4d0c0 2020-03-18 stsp goto done;
1766 4ba14133 2020-03-20 stsp }
1767 4ba14133 2020-03-20 stsp if (pe == NULL) {
1768 4ba14133 2020-03-20 stsp /*
1769 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1770 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1771 4ba14133 2020-03-20 stsp * which could be fetched instead.
1772 4ba14133 2020-03-20 stsp */
1773 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1774 4ba14133 2020-03-20 stsp const char *target = pe->path;
1775 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1776 d9b4d0c0 2020-03-18 stsp
1777 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1778 4ba14133 2020-03-20 stsp if (error) {
1779 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1780 4ba14133 2020-03-20 stsp error = NULL;
1781 4ba14133 2020-03-20 stsp continue;
1782 4ba14133 2020-03-20 stsp }
1783 4ba14133 2020-03-20 stsp goto done;
1784 4ba14133 2020-03-20 stsp }
1785 4ba14133 2020-03-20 stsp
1786 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1787 04d9a9ec 2020-09-24 stsp verbosity, repo);
1788 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1789 4ba14133 2020-03-20 stsp if (error)
1790 4ba14133 2020-03-20 stsp goto done;
1791 4ba14133 2020-03-20 stsp break;
1792 4ba14133 2020-03-20 stsp }
1793 659e7fbd 2020-03-20 stsp }
1794 659e7fbd 2020-03-20 stsp
1795 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1796 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1797 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1798 09838ffc 2020-03-18 stsp done:
1799 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1800 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1801 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1802 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1803 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1804 9c52365f 2020-03-21 stsp }
1805 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1806 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1807 1d0f4054 2021-06-17 stsp if (repo) {
1808 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1809 1d0f4054 2021-06-17 stsp if (error == NULL)
1810 1d0f4054 2021-06-17 stsp error = close_err;
1811 1d0f4054 2021-06-17 stsp }
1812 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1813 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1814 d9b4d0c0 2020-03-18 stsp free(pe->data);
1815 d9b4d0c0 2020-03-18 stsp }
1816 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1817 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1818 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1819 d9b4d0c0 2020-03-18 stsp free(pe->data);
1820 d9b4d0c0 2020-03-18 stsp }
1821 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1822 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1823 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1824 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1825 09838ffc 2020-03-18 stsp free(proto);
1826 09838ffc 2020-03-18 stsp free(host);
1827 09838ffc 2020-03-18 stsp free(port);
1828 09838ffc 2020-03-18 stsp free(server_path);
1829 09838ffc 2020-03-18 stsp free(repo_name);
1830 bb64b798 2020-03-18 stsp free(default_destdir);
1831 b46f3e71 2020-03-18 stsp free(git_url);
1832 39c64a6a 2020-03-18 stsp return error;
1833 7848a0e1 2020-03-19 stsp }
1834 7848a0e1 2020-03-19 stsp
1835 7848a0e1 2020-03-19 stsp static const struct got_error *
1836 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1837 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1838 7848a0e1 2020-03-19 stsp {
1839 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1840 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1841 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1842 7848a0e1 2020-03-19 stsp
1843 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1844 7848a0e1 2020-03-19 stsp if (err)
1845 7848a0e1 2020-03-19 stsp goto done;
1846 7848a0e1 2020-03-19 stsp
1847 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1848 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1849 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1850 88609724 2020-03-21 stsp if (err)
1851 88609724 2020-03-21 stsp goto done;
1852 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1853 88609724 2020-03-21 stsp goto done;
1854 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1855 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1856 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1857 db6d8ad8 2020-03-21 stsp }
1858 db6d8ad8 2020-03-21 stsp goto done;
1859 db6d8ad8 2020-03-21 stsp }
1860 db6d8ad8 2020-03-21 stsp
1861 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1862 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1863 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1864 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1865 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1866 688f11b3 2020-03-21 stsp }
1867 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1868 7848a0e1 2020-03-19 stsp if (err)
1869 7848a0e1 2020-03-19 stsp goto done;
1870 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1871 e8a967e0 2020-03-21 stsp if (err)
1872 e8a967e0 2020-03-21 stsp goto done;
1873 7848a0e1 2020-03-19 stsp } else {
1874 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1875 7848a0e1 2020-03-19 stsp if (err)
1876 7848a0e1 2020-03-19 stsp goto done;
1877 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1878 6338a6a1 2020-03-21 stsp goto done;
1879 6338a6a1 2020-03-21 stsp
1880 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1881 6338a6a1 2020-03-21 stsp if (err)
1882 6338a6a1 2020-03-21 stsp goto done;
1883 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1884 6338a6a1 2020-03-21 stsp if (err)
1885 6338a6a1 2020-03-21 stsp goto done;
1886 7848a0e1 2020-03-19 stsp }
1887 6338a6a1 2020-03-21 stsp
1888 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1889 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1890 6338a6a1 2020-03-21 stsp new_id_str);
1891 7848a0e1 2020-03-19 stsp done:
1892 7848a0e1 2020-03-19 stsp free(old_id);
1893 7848a0e1 2020-03-19 stsp free(new_id_str);
1894 7848a0e1 2020-03-19 stsp return err;
1895 2ab43947 2020-03-18 stsp }
1896 f1bcca34 2020-03-25 stsp
1897 f1bcca34 2020-03-25 stsp static const struct got_error *
1898 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1899 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1900 f1bcca34 2020-03-25 stsp {
1901 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1902 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1903 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1904 f1bcca34 2020-03-25 stsp
1905 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1906 bcf34b0e 2020-03-26 stsp if (err) {
1907 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1908 bcf34b0e 2020-03-26 stsp return err;
1909 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1910 bcf34b0e 2020-03-26 stsp if (err)
1911 bcf34b0e 2020-03-26 stsp goto done;
1912 2ab43947 2020-03-18 stsp
1913 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1914 bcf34b0e 2020-03-26 stsp if (err)
1915 bcf34b0e 2020-03-26 stsp goto done;
1916 f1bcca34 2020-03-25 stsp
1917 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1918 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1919 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1920 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1921 bcf34b0e 2020-03-26 stsp } else {
1922 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1923 f1bcca34 2020-03-25 stsp
1924 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1925 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1926 bcf34b0e 2020-03-26 stsp goto done;
1927 bcf34b0e 2020-03-26 stsp
1928 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1929 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1930 bcf34b0e 2020-03-26 stsp if (err)
1931 bcf34b0e 2020-03-26 stsp goto done;
1932 bcf34b0e 2020-03-26 stsp
1933 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1934 bcf34b0e 2020-03-26 stsp if (err)
1935 bcf34b0e 2020-03-26 stsp goto done;
1936 bcf34b0e 2020-03-26 stsp
1937 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1938 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1939 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1940 bcf34b0e 2020-03-26 stsp
1941 bcf34b0e 2020-03-26 stsp }
1942 f1bcca34 2020-03-25 stsp done:
1943 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1944 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1945 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1946 bcf34b0e 2020-03-26 stsp err = unlock_err;
1947 bcf34b0e 2020-03-26 stsp }
1948 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1949 f1bcca34 2020-03-25 stsp return err;
1950 f1bcca34 2020-03-25 stsp }
1951 f1bcca34 2020-03-25 stsp
1952 2ab43947 2020-03-18 stsp __dead static void
1953 7848a0e1 2020-03-19 stsp usage_fetch(void)
1954 7848a0e1 2020-03-19 stsp {
1955 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1956 161728eb 2021-07-24 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1957 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1958 13f12b09 2020-03-21 stsp getprogname());
1959 7848a0e1 2020-03-19 stsp exit(1);
1960 7848a0e1 2020-03-19 stsp }
1961 7848a0e1 2020-03-19 stsp
1962 7848a0e1 2020-03-19 stsp static const struct got_error *
1963 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1964 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1965 f21ec2f0 2020-03-21 stsp {
1966 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1967 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1968 3789fd73 2020-03-26 stsp char *id_str = NULL;
1969 3789fd73 2020-03-26 stsp
1970 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1971 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1972 3789fd73 2020-03-26 stsp if (err)
1973 3789fd73 2020-03-26 stsp return err;
1974 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1975 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1976 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1977 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1978 3789fd73 2020-03-26 stsp }
1979 3789fd73 2020-03-26 stsp } else {
1980 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1981 3789fd73 2020-03-26 stsp if (err)
1982 3789fd73 2020-03-26 stsp return err;
1983 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1984 3789fd73 2020-03-26 stsp if (err)
1985 3789fd73 2020-03-26 stsp goto done;
1986 3168e5da 2020-09-10 stsp
1987 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1988 3789fd73 2020-03-26 stsp if (err)
1989 3789fd73 2020-03-26 stsp goto done;
1990 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1991 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1992 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1993 3789fd73 2020-03-26 stsp }
1994 3789fd73 2020-03-26 stsp }
1995 3789fd73 2020-03-26 stsp done:
1996 3789fd73 2020-03-26 stsp free(id);
1997 3789fd73 2020-03-26 stsp free(id_str);
1998 3789fd73 2020-03-26 stsp return NULL;
1999 3789fd73 2020-03-26 stsp }
2000 3789fd73 2020-03-26 stsp
2001 3789fd73 2020-03-26 stsp static const struct got_error *
2002 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
2003 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2004 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2005 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2006 3789fd73 2020-03-26 stsp {
2007 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2008 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2009 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2010 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2011 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2012 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2013 f21ec2f0 2020-03-21 stsp
2014 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2015 f21ec2f0 2020-03-21 stsp
2016 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2017 3789fd73 2020-03-26 stsp == -1)
2018 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2019 3789fd73 2020-03-26 stsp
2020 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2021 f21ec2f0 2020-03-21 stsp if (err)
2022 3789fd73 2020-03-26 stsp goto done;
2023 f21ec2f0 2020-03-21 stsp
2024 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2025 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2026 f21ec2f0 2020-03-21 stsp
2027 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
2028 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2029 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2030 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2031 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2032 3789fd73 2020-03-26 stsp continue;
2033 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2034 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2035 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2036 3789fd73 2020-03-26 stsp goto done;
2037 3789fd73 2020-03-26 stsp }
2038 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2039 3789fd73 2020-03-26 stsp continue;
2040 3789fd73 2020-03-26 stsp }
2041 f21ec2f0 2020-03-21 stsp
2042 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2043 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2044 f21ec2f0 2020-03-21 stsp break;
2045 f21ec2f0 2020-03-21 stsp }
2046 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2047 f21ec2f0 2020-03-21 stsp continue;
2048 f21ec2f0 2020-03-21 stsp
2049 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2050 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2051 3789fd73 2020-03-26 stsp break;
2052 3789fd73 2020-03-26 stsp }
2053 3789fd73 2020-03-26 stsp if (pe != NULL)
2054 3789fd73 2020-03-26 stsp continue;
2055 f21ec2f0 2020-03-21 stsp
2056 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2057 f21ec2f0 2020-03-21 stsp if (err)
2058 f21ec2f0 2020-03-21 stsp break;
2059 3789fd73 2020-03-26 stsp
2060 3789fd73 2020-03-26 stsp if (local_refname) {
2061 3789fd73 2020-03-26 stsp struct got_reference *ref;
2062 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2063 3789fd73 2020-03-26 stsp if (err) {
2064 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2065 3789fd73 2020-03-26 stsp break;
2066 3789fd73 2020-03-26 stsp free(local_refname);
2067 3789fd73 2020-03-26 stsp local_refname = NULL;
2068 3789fd73 2020-03-26 stsp continue;
2069 3789fd73 2020-03-26 stsp }
2070 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2071 3789fd73 2020-03-26 stsp if (err)
2072 3789fd73 2020-03-26 stsp break;
2073 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2074 3789fd73 2020-03-26 stsp got_ref_close(ref);
2075 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2076 3789fd73 2020-03-26 stsp err = unlock_err;
2077 3789fd73 2020-03-26 stsp break;
2078 3789fd73 2020-03-26 stsp }
2079 3789fd73 2020-03-26 stsp
2080 3789fd73 2020-03-26 stsp free(local_refname);
2081 3789fd73 2020-03-26 stsp local_refname = NULL;
2082 6338a6a1 2020-03-21 stsp }
2083 f21ec2f0 2020-03-21 stsp }
2084 3789fd73 2020-03-26 stsp done:
2085 3789fd73 2020-03-26 stsp free(remote_namespace);
2086 3789fd73 2020-03-26 stsp free(local_refname);
2087 0e4002ca 2020-03-21 stsp return err;
2088 0e4002ca 2020-03-21 stsp }
2089 0e4002ca 2020-03-21 stsp
2090 0e4002ca 2020-03-21 stsp static const struct got_error *
2091 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2092 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2093 0e4002ca 2020-03-21 stsp {
2094 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2095 0e4002ca 2020-03-21 stsp char *remote_refname;
2096 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2097 0e4002ca 2020-03-21 stsp
2098 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2099 0e4002ca 2020-03-21 stsp refname += 5;
2100 0e4002ca 2020-03-21 stsp
2101 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2102 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2103 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2104 f21ec2f0 2020-03-21 stsp
2105 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2106 0e4002ca 2020-03-21 stsp if (err) {
2107 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2108 0e4002ca 2020-03-21 stsp goto done;
2109 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2110 0e4002ca 2020-03-21 stsp } else {
2111 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2112 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2113 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2114 9f142382 2020-03-21 stsp err = unlock_err;
2115 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2116 0e4002ca 2020-03-21 stsp }
2117 0e4002ca 2020-03-21 stsp done:
2118 0e4002ca 2020-03-21 stsp free(remote_refname);
2119 161728eb 2021-07-24 stsp return err;
2120 161728eb 2021-07-24 stsp }
2121 161728eb 2021-07-24 stsp
2122 161728eb 2021-07-24 stsp static const struct got_error *
2123 161728eb 2021-07-24 stsp delete_ref(struct got_repository *repo, struct got_reference *ref)
2124 161728eb 2021-07-24 stsp {
2125 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2126 161728eb 2021-07-24 stsp struct got_object_id *id = NULL;
2127 161728eb 2021-07-24 stsp char *id_str = NULL;
2128 161728eb 2021-07-24 stsp const char *target;
2129 161728eb 2021-07-24 stsp
2130 161728eb 2021-07-24 stsp if (got_ref_is_symbolic(ref)) {
2131 161728eb 2021-07-24 stsp target = got_ref_get_symref_target(ref);
2132 161728eb 2021-07-24 stsp } else {
2133 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2134 161728eb 2021-07-24 stsp if (err)
2135 161728eb 2021-07-24 stsp goto done;
2136 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2137 161728eb 2021-07-24 stsp if (err)
2138 161728eb 2021-07-24 stsp goto done;
2139 161728eb 2021-07-24 stsp target = id_str;
2140 161728eb 2021-07-24 stsp }
2141 161728eb 2021-07-24 stsp
2142 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2143 161728eb 2021-07-24 stsp if (err)
2144 161728eb 2021-07-24 stsp goto done;
2145 161728eb 2021-07-24 stsp
2146 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2147 161728eb 2021-07-24 stsp done:
2148 161728eb 2021-07-24 stsp free(id);
2149 161728eb 2021-07-24 stsp free(id_str);
2150 f21ec2f0 2020-03-21 stsp return err;
2151 f21ec2f0 2020-03-21 stsp }
2152 f21ec2f0 2020-03-21 stsp
2153 f21ec2f0 2020-03-21 stsp static const struct got_error *
2154 161728eb 2021-07-24 stsp delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2155 161728eb 2021-07-24 stsp {
2156 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2157 161728eb 2021-07-24 stsp struct got_reflist_head refs;
2158 161728eb 2021-07-24 stsp struct got_reflist_entry *re;
2159 161728eb 2021-07-24 stsp char *prefix;
2160 161728eb 2021-07-24 stsp
2161 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2162 161728eb 2021-07-24 stsp
2163 161728eb 2021-07-24 stsp if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2164 161728eb 2021-07-24 stsp err = got_error_from_errno("asprintf");
2165 161728eb 2021-07-24 stsp goto done;
2166 161728eb 2021-07-24 stsp }
2167 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2168 161728eb 2021-07-24 stsp if (err)
2169 161728eb 2021-07-24 stsp goto done;
2170 161728eb 2021-07-24 stsp
2171 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2172 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2173 161728eb 2021-07-24 stsp done:
2174 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2175 161728eb 2021-07-24 stsp return err;
2176 161728eb 2021-07-24 stsp }
2177 161728eb 2021-07-24 stsp
2178 161728eb 2021-07-24 stsp static const struct got_error *
2179 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2180 7848a0e1 2020-03-19 stsp {
2181 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2182 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2183 7848a0e1 2020-03-19 stsp const char *remote_name;
2184 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2185 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2186 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2187 7848a0e1 2020-03-19 stsp int nremotes;
2188 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2189 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2190 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2191 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2192 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2193 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2194 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2195 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2196 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2197 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2198 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2199 161728eb 2021-07-24 stsp int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2200 7848a0e1 2020-03-19 stsp
2201 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2202 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2203 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2204 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2205 7848a0e1 2020-03-19 stsp
2206 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2207 7848a0e1 2020-03-19 stsp switch (ch) {
2208 659e7fbd 2020-03-20 stsp case 'a':
2209 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2210 4ba14133 2020-03-20 stsp break;
2211 4ba14133 2020-03-20 stsp case 'b':
2212 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2213 4ba14133 2020-03-20 stsp optarg, NULL);
2214 4ba14133 2020-03-20 stsp if (error)
2215 4ba14133 2020-03-20 stsp return error;
2216 41b0de12 2020-03-21 stsp break;
2217 f21ec2f0 2020-03-21 stsp case 'd':
2218 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2219 f21ec2f0 2020-03-21 stsp break;
2220 41b0de12 2020-03-21 stsp case 'l':
2221 41b0de12 2020-03-21 stsp list_refs_only = 1;
2222 659e7fbd 2020-03-20 stsp break;
2223 7848a0e1 2020-03-19 stsp case 'r':
2224 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2225 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2226 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2227 7848a0e1 2020-03-19 stsp optarg);
2228 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2229 7848a0e1 2020-03-19 stsp break;
2230 db6d8ad8 2020-03-21 stsp case 't':
2231 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2232 db6d8ad8 2020-03-21 stsp break;
2233 7848a0e1 2020-03-19 stsp case 'v':
2234 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2235 7848a0e1 2020-03-19 stsp verbosity = 0;
2236 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2237 7848a0e1 2020-03-19 stsp verbosity++;
2238 7848a0e1 2020-03-19 stsp break;
2239 7848a0e1 2020-03-19 stsp case 'q':
2240 7848a0e1 2020-03-19 stsp verbosity = -1;
2241 7848a0e1 2020-03-19 stsp break;
2242 0e4002ca 2020-03-21 stsp case 'R':
2243 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2244 0e4002ca 2020-03-21 stsp optarg, NULL);
2245 0e4002ca 2020-03-21 stsp if (error)
2246 0e4002ca 2020-03-21 stsp return error;
2247 0e4002ca 2020-03-21 stsp break;
2248 161728eb 2021-07-24 stsp case 'X':
2249 161728eb 2021-07-24 stsp delete_remote = 1;
2250 161728eb 2021-07-24 stsp break;
2251 7848a0e1 2020-03-19 stsp default:
2252 7848a0e1 2020-03-19 stsp usage_fetch();
2253 7848a0e1 2020-03-19 stsp break;
2254 7848a0e1 2020-03-19 stsp }
2255 7848a0e1 2020-03-19 stsp }
2256 7848a0e1 2020-03-19 stsp argc -= optind;
2257 7848a0e1 2020-03-19 stsp argv += optind;
2258 7848a0e1 2020-03-19 stsp
2259 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2260 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2261 41b0de12 2020-03-21 stsp if (list_refs_only) {
2262 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2263 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2264 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2265 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2266 f21ec2f0 2020-03-21 stsp if (delete_refs)
2267 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2268 161728eb 2021-07-24 stsp if (delete_remote)
2269 161728eb 2021-07-24 stsp option_conflict('l', 'X');
2270 41b0de12 2020-03-21 stsp }
2271 161728eb 2021-07-24 stsp if (delete_remote) {
2272 161728eb 2021-07-24 stsp if (fetch_all_branches)
2273 161728eb 2021-07-24 stsp option_conflict('X', 'a');
2274 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_branches))
2275 161728eb 2021-07-24 stsp option_conflict('X', 'b');
2276 161728eb 2021-07-24 stsp if (delete_refs)
2277 161728eb 2021-07-24 stsp option_conflict('X', 'd');
2278 161728eb 2021-07-24 stsp if (replace_tags)
2279 161728eb 2021-07-24 stsp option_conflict('X', 't');
2280 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_refs))
2281 161728eb 2021-07-24 stsp option_conflict('X', 'R');
2282 161728eb 2021-07-24 stsp }
2283 161728eb 2021-07-24 stsp
2284 161728eb 2021-07-24 stsp if (argc == 0) {
2285 161728eb 2021-07-24 stsp if (delete_remote)
2286 161728eb 2021-07-24 stsp errx(1, "-X option requires a remote name");
2287 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2288 161728eb 2021-07-24 stsp } else if (argc == 1)
2289 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2290 7848a0e1 2020-03-19 stsp else
2291 7848a0e1 2020-03-19 stsp usage_fetch();
2292 7848a0e1 2020-03-19 stsp
2293 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2294 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2295 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2296 7848a0e1 2020-03-19 stsp goto done;
2297 7848a0e1 2020-03-19 stsp }
2298 7848a0e1 2020-03-19 stsp
2299 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2300 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2301 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2302 7848a0e1 2020-03-19 stsp goto done;
2303 7848a0e1 2020-03-19 stsp else
2304 7848a0e1 2020-03-19 stsp error = NULL;
2305 7848a0e1 2020-03-19 stsp if (worktree) {
2306 7848a0e1 2020-03-19 stsp repo_path =
2307 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2308 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2309 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2310 7848a0e1 2020-03-19 stsp if (error)
2311 7848a0e1 2020-03-19 stsp goto done;
2312 7848a0e1 2020-03-19 stsp } else {
2313 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2314 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2315 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2316 7848a0e1 2020-03-19 stsp goto done;
2317 7848a0e1 2020-03-19 stsp }
2318 7848a0e1 2020-03-19 stsp }
2319 7848a0e1 2020-03-19 stsp }
2320 7848a0e1 2020-03-19 stsp
2321 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2322 7848a0e1 2020-03-19 stsp if (error)
2323 7848a0e1 2020-03-19 stsp goto done;
2324 7848a0e1 2020-03-19 stsp
2325 161728eb 2021-07-24 stsp if (delete_remote) {
2326 161728eb 2021-07-24 stsp error = delete_refs_for_remote(repo, remote_name);
2327 161728eb 2021-07-24 stsp goto done; /* nothing else to do */
2328 161728eb 2021-07-24 stsp }
2329 161728eb 2021-07-24 stsp
2330 50b0790e 2020-09-11 stsp if (worktree) {
2331 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2332 50b0790e 2020-09-11 stsp if (worktree_conf) {
2333 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2334 50b0790e 2020-09-11 stsp worktree_conf);
2335 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2336 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2337 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2338 50b0790e 2020-09-11 stsp break;
2339 54eb00d5 2020-10-20 stsp }
2340 50b0790e 2020-09-11 stsp }
2341 50b0790e 2020-09-11 stsp }
2342 7848a0e1 2020-03-19 stsp }
2343 50b0790e 2020-09-11 stsp if (remote == NULL) {
2344 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2345 50b0790e 2020-09-11 stsp if (repo_conf) {
2346 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2347 50b0790e 2020-09-11 stsp repo_conf);
2348 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2349 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2350 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2351 50b0790e 2020-09-11 stsp break;
2352 54eb00d5 2020-10-20 stsp }
2353 50b0790e 2020-09-11 stsp }
2354 50b0790e 2020-09-11 stsp }
2355 50b0790e 2020-09-11 stsp }
2356 50b0790e 2020-09-11 stsp if (remote == NULL) {
2357 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2358 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2359 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2360 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2361 257add31 2020-09-09 stsp break;
2362 54eb00d5 2020-10-20 stsp }
2363 257add31 2020-09-09 stsp }
2364 7848a0e1 2020-03-19 stsp }
2365 50b0790e 2020-09-11 stsp if (remote == NULL) {
2366 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2367 50b0790e 2020-09-11 stsp goto done;
2368 b8adfa55 2020-09-25 stsp }
2369 b8adfa55 2020-09-25 stsp
2370 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2371 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2372 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2373 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2374 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2375 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2376 99495ddb 2021-01-10 stsp }
2377 99495ddb 2021-01-10 stsp }
2378 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2379 99495ddb 2021-01-10 stsp for (i = 0; i < remote->nrefs; i++) {
2380 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2381 99495ddb 2021-01-10 stsp remote->refs[i], NULL);
2382 b8adfa55 2020-09-25 stsp }
2383 50b0790e 2020-09-11 stsp }
2384 7848a0e1 2020-03-19 stsp
2385 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2386 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2387 7848a0e1 2020-03-19 stsp if (error)
2388 7848a0e1 2020-03-19 stsp goto done;
2389 7848a0e1 2020-03-19 stsp
2390 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2391 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2392 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2393 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2394 7848a0e1 2020-03-19 stsp err(1, "pledge");
2395 7848a0e1 2020-03-19 stsp #endif
2396 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2397 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2398 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2399 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2400 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2401 7848a0e1 2020-03-19 stsp err(1, "pledge");
2402 7848a0e1 2020-03-19 stsp #endif
2403 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2404 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2405 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2406 7848a0e1 2020-03-19 stsp goto done;
2407 7848a0e1 2020-03-19 stsp } else {
2408 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2409 7848a0e1 2020-03-19 stsp goto done;
2410 7848a0e1 2020-03-19 stsp }
2411 7848a0e1 2020-03-19 stsp
2412 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2413 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2414 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2415 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2416 7848a0e1 2020-03-19 stsp goto done;
2417 7848a0e1 2020-03-19 stsp }
2418 7848a0e1 2020-03-19 stsp }
2419 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2420 7848a0e1 2020-03-19 stsp if (error)
2421 7848a0e1 2020-03-19 stsp goto done;
2422 f79e6490 2020-04-19 stsp
2423 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2424 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2425 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2426 7848a0e1 2020-03-19 stsp
2427 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2428 9c52365f 2020-03-21 stsp server_path, verbosity);
2429 7848a0e1 2020-03-19 stsp if (error)
2430 7848a0e1 2020-03-19 stsp goto done;
2431 7848a0e1 2020-03-19 stsp
2432 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2433 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2434 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2435 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2436 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2437 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2438 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2439 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2440 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2441 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2442 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2443 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2444 7848a0e1 2020-03-19 stsp if (error)
2445 7848a0e1 2020-03-19 stsp goto done;
2446 7848a0e1 2020-03-19 stsp
2447 41b0de12 2020-03-21 stsp if (list_refs_only) {
2448 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2449 41b0de12 2020-03-21 stsp goto done;
2450 41b0de12 2020-03-21 stsp }
2451 41b0de12 2020-03-21 stsp
2452 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2453 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2454 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2455 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2456 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2457 984065c8 2020-03-19 stsp if (error)
2458 984065c8 2020-03-19 stsp goto done;
2459 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2460 984065c8 2020-03-19 stsp free(id_str);
2461 984065c8 2020-03-19 stsp id_str = NULL;
2462 984065c8 2020-03-19 stsp }
2463 7848a0e1 2020-03-19 stsp
2464 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2465 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2466 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2467 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2468 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2469 7848a0e1 2020-03-19 stsp char *remote_refname;
2470 7848a0e1 2020-03-19 stsp
2471 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2472 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2473 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2474 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2475 0e4002ca 2020-03-21 stsp if (error)
2476 0e4002ca 2020-03-21 stsp goto done;
2477 0e4002ca 2020-03-21 stsp continue;
2478 0e4002ca 2020-03-21 stsp }
2479 0e4002ca 2020-03-21 stsp
2480 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2481 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2482 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2483 7848a0e1 2020-03-19 stsp if (error) {
2484 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2485 7848a0e1 2020-03-19 stsp goto done;
2486 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2487 6338a6a1 2020-03-21 stsp repo);
2488 7848a0e1 2020-03-19 stsp if (error)
2489 7848a0e1 2020-03-19 stsp goto done;
2490 7848a0e1 2020-03-19 stsp } else {
2491 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2492 db6d8ad8 2020-03-21 stsp verbosity, repo);
2493 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2494 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2495 9f142382 2020-03-21 stsp error = unlock_err;
2496 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2497 7848a0e1 2020-03-19 stsp if (error)
2498 7848a0e1 2020-03-19 stsp goto done;
2499 7848a0e1 2020-03-19 stsp }
2500 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2501 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2502 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2503 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2504 7848a0e1 2020-03-19 stsp goto done;
2505 7848a0e1 2020-03-19 stsp }
2506 7848a0e1 2020-03-19 stsp
2507 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2508 7848a0e1 2020-03-19 stsp if (error) {
2509 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2510 7848a0e1 2020-03-19 stsp goto done;
2511 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2512 688f11b3 2020-03-21 stsp verbosity, repo);
2513 7848a0e1 2020-03-19 stsp if (error)
2514 7848a0e1 2020-03-19 stsp goto done;
2515 7848a0e1 2020-03-19 stsp } else {
2516 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2517 db6d8ad8 2020-03-21 stsp verbosity, repo);
2518 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2519 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2520 9f142382 2020-03-21 stsp error = unlock_err;
2521 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2522 7848a0e1 2020-03-19 stsp if (error)
2523 7848a0e1 2020-03-19 stsp goto done;
2524 7848a0e1 2020-03-19 stsp }
2525 2ec30c80 2020-03-20 stsp
2526 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2527 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2528 2ec30c80 2020-03-20 stsp if (error) {
2529 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2530 2ec30c80 2020-03-20 stsp goto done;
2531 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2532 6338a6a1 2020-03-21 stsp repo);
2533 2ec30c80 2020-03-20 stsp if (error)
2534 2ec30c80 2020-03-20 stsp goto done;
2535 9f142382 2020-03-21 stsp } else {
2536 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2537 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2538 9f142382 2020-03-21 stsp error = unlock_err;
2539 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2540 9f142382 2020-03-21 stsp }
2541 7848a0e1 2020-03-19 stsp }
2542 7848a0e1 2020-03-19 stsp }
2543 f1bcca34 2020-03-25 stsp if (delete_refs) {
2544 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2545 3789fd73 2020-03-26 stsp verbosity, repo);
2546 f1bcca34 2020-03-25 stsp if (error)
2547 f1bcca34 2020-03-25 stsp goto done;
2548 f1bcca34 2020-03-25 stsp }
2549 f1bcca34 2020-03-25 stsp
2550 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2551 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2552 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2553 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2554 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2555 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2556 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2557 f1bcca34 2020-03-25 stsp
2558 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2559 f1bcca34 2020-03-25 stsp continue;
2560 f1bcca34 2020-03-25 stsp
2561 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2562 f1bcca34 2020-03-25 stsp continue;
2563 f1bcca34 2020-03-25 stsp
2564 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2565 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2566 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2567 f1bcca34 2020-03-25 stsp goto done;
2568 f1bcca34 2020-03-25 stsp }
2569 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2570 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2571 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2572 f1bcca34 2020-03-25 stsp free(remote_refname);
2573 f1bcca34 2020-03-25 stsp goto done;
2574 f1bcca34 2020-03-25 stsp }
2575 f1bcca34 2020-03-25 stsp
2576 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2577 f1bcca34 2020-03-25 stsp 0);
2578 f1bcca34 2020-03-25 stsp if (error) {
2579 f1bcca34 2020-03-25 stsp free(remote_refname);
2580 f1bcca34 2020-03-25 stsp free(remote_target);
2581 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2582 f1bcca34 2020-03-25 stsp error = NULL;
2583 f1bcca34 2020-03-25 stsp continue;
2584 f1bcca34 2020-03-25 stsp }
2585 f1bcca34 2020-03-25 stsp goto done;
2586 f1bcca34 2020-03-25 stsp }
2587 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2588 f1bcca34 2020-03-25 stsp verbosity, repo);
2589 f1bcca34 2020-03-25 stsp free(remote_refname);
2590 f1bcca34 2020-03-25 stsp free(remote_target);
2591 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2592 f1bcca34 2020-03-25 stsp if (error)
2593 f1bcca34 2020-03-25 stsp goto done;
2594 f1bcca34 2020-03-25 stsp }
2595 f1bcca34 2020-03-25 stsp }
2596 7848a0e1 2020-03-19 stsp done:
2597 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2598 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2599 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2600 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2601 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2602 9c52365f 2020-03-21 stsp }
2603 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2604 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2605 1d0f4054 2021-06-17 stsp if (repo) {
2606 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2607 1d0f4054 2021-06-17 stsp if (error == NULL)
2608 1d0f4054 2021-06-17 stsp error = close_err;
2609 1d0f4054 2021-06-17 stsp }
2610 7848a0e1 2020-03-19 stsp if (worktree)
2611 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2612 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2613 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2614 7848a0e1 2020-03-19 stsp free(pe->data);
2615 7848a0e1 2020-03-19 stsp }
2616 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2617 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2618 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2619 7848a0e1 2020-03-19 stsp free(pe->data);
2620 7848a0e1 2020-03-19 stsp }
2621 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2622 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2623 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2624 7848a0e1 2020-03-19 stsp free(id_str);
2625 7848a0e1 2020-03-19 stsp free(cwd);
2626 7848a0e1 2020-03-19 stsp free(repo_path);
2627 7848a0e1 2020-03-19 stsp free(pack_hash);
2628 7848a0e1 2020-03-19 stsp free(proto);
2629 7848a0e1 2020-03-19 stsp free(host);
2630 7848a0e1 2020-03-19 stsp free(port);
2631 7848a0e1 2020-03-19 stsp free(server_path);
2632 7848a0e1 2020-03-19 stsp free(repo_name);
2633 7848a0e1 2020-03-19 stsp return error;
2634 7848a0e1 2020-03-19 stsp }
2635 7848a0e1 2020-03-19 stsp
2636 7848a0e1 2020-03-19 stsp
2637 7848a0e1 2020-03-19 stsp __dead static void
2638 2ab43947 2020-03-18 stsp usage_checkout(void)
2639 2ab43947 2020-03-18 stsp {
2640 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2641 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2642 2ab43947 2020-03-18 stsp exit(1);
2643 2ab43947 2020-03-18 stsp }
2644 2ab43947 2020-03-18 stsp
2645 2ab43947 2020-03-18 stsp static void
2646 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2647 2ab43947 2020-03-18 stsp {
2648 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2649 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2650 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2651 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2652 2ab43947 2020-03-18 stsp getprogname());
2653 2ab43947 2020-03-18 stsp }
2654 2ab43947 2020-03-18 stsp
2655 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2656 2ab43947 2020-03-18 stsp const char *worktree_path;
2657 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2658 2ab43947 2020-03-18 stsp };
2659 2ab43947 2020-03-18 stsp
2660 2ab43947 2020-03-18 stsp static const struct got_error *
2661 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2662 2ab43947 2020-03-18 stsp {
2663 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2664 2ab43947 2020-03-18 stsp
2665 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2666 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2667 2ab43947 2020-03-18 stsp return NULL;
2668 2ab43947 2020-03-18 stsp
2669 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2670 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2671 2ab43947 2020-03-18 stsp return NULL;
2672 2ab43947 2020-03-18 stsp }
2673 2ab43947 2020-03-18 stsp
2674 2ab43947 2020-03-18 stsp while (path[0] == '/')
2675 2ab43947 2020-03-18 stsp path++;
2676 2ab43947 2020-03-18 stsp
2677 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2678 2ab43947 2020-03-18 stsp return NULL;
2679 2ab43947 2020-03-18 stsp }
2680 2ab43947 2020-03-18 stsp
2681 2ab43947 2020-03-18 stsp static const struct got_error *
2682 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2683 2ab43947 2020-03-18 stsp {
2684 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2685 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2686 2ab43947 2020-03-18 stsp return NULL;
2687 2ab43947 2020-03-18 stsp }
2688 2ab43947 2020-03-18 stsp
2689 2ab43947 2020-03-18 stsp static const struct got_error *
2690 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2691 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2692 2ab43947 2020-03-18 stsp struct got_repository *repo)
2693 2ab43947 2020-03-18 stsp {
2694 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2695 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2696 2ab43947 2020-03-18 stsp
2697 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2698 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2699 2ab43947 2020-03-18 stsp if (err)
2700 2ab43947 2020-03-18 stsp return err;
2701 2ab43947 2020-03-18 stsp
2702 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2703 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2704 2ab43947 2020-03-18 stsp
2705 2ab43947 2020-03-18 stsp /*
2706 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2707 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2708 2ab43947 2020-03-18 stsp *
2709 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2710 2ab43947 2020-03-18 stsp *
2711 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2712 2ab43947 2020-03-18 stsp * \ /
2713 2ab43947 2020-03-18 stsp * C E
2714 2ab43947 2020-03-18 stsp * \ /
2715 2ab43947 2020-03-18 stsp * B (yca)
2716 2ab43947 2020-03-18 stsp * |
2717 2ab43947 2020-03-18 stsp * A
2718 2ab43947 2020-03-18 stsp *
2719 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2720 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2721 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2722 2ab43947 2020-03-18 stsp */
2723 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2724 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2725 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2726 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2727 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2728 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2729 2ab43947 2020-03-18 stsp
2730 2ab43947 2020-03-18 stsp free(yca_id);
2731 2ab43947 2020-03-18 stsp return NULL;
2732 2ab43947 2020-03-18 stsp }
2733 2ab43947 2020-03-18 stsp
2734 2ab43947 2020-03-18 stsp static const struct got_error *
2735 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2736 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2737 2ab43947 2020-03-18 stsp struct got_repository *repo)
2738 2ab43947 2020-03-18 stsp {
2739 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2740 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2741 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2742 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2743 2ab43947 2020-03-18 stsp
2744 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2745 2ab43947 2020-03-18 stsp if (err)
2746 2ab43947 2020-03-18 stsp goto done;
2747 2ab43947 2020-03-18 stsp
2748 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2749 2ab43947 2020-03-18 stsp is_same_branch = 1;
2750 2ab43947 2020-03-18 stsp goto done;
2751 2ab43947 2020-03-18 stsp }
2752 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2753 2ab43947 2020-03-18 stsp is_same_branch = 1;
2754 2ab43947 2020-03-18 stsp goto done;
2755 2ab43947 2020-03-18 stsp }
2756 2ab43947 2020-03-18 stsp
2757 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2758 2ab43947 2020-03-18 stsp if (err)
2759 2ab43947 2020-03-18 stsp goto done;
2760 2ab43947 2020-03-18 stsp
2761 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2762 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2763 2ab43947 2020-03-18 stsp if (err)
2764 2ab43947 2020-03-18 stsp goto done;
2765 2ab43947 2020-03-18 stsp
2766 2ab43947 2020-03-18 stsp for (;;) {
2767 2ab43947 2020-03-18 stsp struct got_object_id *id;
2768 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2769 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2770 2ab43947 2020-03-18 stsp if (err) {
2771 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2772 2ab43947 2020-03-18 stsp err = NULL;
2773 2ab43947 2020-03-18 stsp break;
2774 2ab43947 2020-03-18 stsp }
2775 2ab43947 2020-03-18 stsp
2776 2ab43947 2020-03-18 stsp if (id) {
2777 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2778 2ab43947 2020-03-18 stsp break;
2779 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2780 2ab43947 2020-03-18 stsp is_same_branch = 1;
2781 2ab43947 2020-03-18 stsp break;
2782 2ab43947 2020-03-18 stsp }
2783 2ab43947 2020-03-18 stsp }
2784 2ab43947 2020-03-18 stsp }
2785 2ab43947 2020-03-18 stsp done:
2786 2ab43947 2020-03-18 stsp if (graph)
2787 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2788 2ab43947 2020-03-18 stsp free(head_commit_id);
2789 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2790 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2791 2ab43947 2020-03-18 stsp return err;
2792 a367ff0f 2019-05-14 stsp }
2793 8069f636 2019-01-12 stsp
2794 4ed7e80c 2018-05-20 stsp static const struct got_error *
2795 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2796 4b6c9460 2020-03-05 stsp {
2797 4b6c9460 2020-03-05 stsp static char msg[512];
2798 4b6c9460 2020-03-05 stsp const char *branch_name;
2799 4b6c9460 2020-03-05 stsp
2800 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2801 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2802 4b6c9460 2020-03-05 stsp else
2803 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2804 4b6c9460 2020-03-05 stsp
2805 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2806 4b6c9460 2020-03-05 stsp branch_name += 11;
2807 4b6c9460 2020-03-05 stsp
2808 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2809 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2810 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2811 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2812 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2813 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2814 4b6c9460 2020-03-05 stsp
2815 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2816 4b6c9460 2020-03-05 stsp }
2817 4b6c9460 2020-03-05 stsp
2818 4b6c9460 2020-03-05 stsp static const struct got_error *
2819 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2820 c09a553d 2018-03-12 stsp {
2821 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2822 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2823 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2824 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2825 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2826 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2827 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2828 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2829 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2830 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2831 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2832 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2833 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2834 f2ea84fa 2019-07-27 stsp
2835 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2836 c09a553d 2018-03-12 stsp
2837 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2838 0bb8a95e 2018-03-12 stsp switch (ch) {
2839 08573d5b 2019-05-14 stsp case 'b':
2840 08573d5b 2019-05-14 stsp branch_name = optarg;
2841 08573d5b 2019-05-14 stsp break;
2842 8069f636 2019-01-12 stsp case 'c':
2843 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2844 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2845 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2846 bb51a5b4 2020-01-13 stsp break;
2847 bb51a5b4 2020-01-13 stsp case 'E':
2848 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2849 8069f636 2019-01-12 stsp break;
2850 0bb8a95e 2018-03-12 stsp case 'p':
2851 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2852 0bb8a95e 2018-03-12 stsp break;
2853 0bb8a95e 2018-03-12 stsp default:
2854 2deda0b9 2019-03-07 stsp usage_checkout();
2855 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2856 0bb8a95e 2018-03-12 stsp }
2857 0bb8a95e 2018-03-12 stsp }
2858 0bb8a95e 2018-03-12 stsp
2859 0bb8a95e 2018-03-12 stsp argc -= optind;
2860 0bb8a95e 2018-03-12 stsp argv += optind;
2861 0bb8a95e 2018-03-12 stsp
2862 6715a751 2018-03-16 stsp #ifndef PROFILE
2863 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2864 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2865 c09a553d 2018-03-12 stsp err(1, "pledge");
2866 6715a751 2018-03-16 stsp #endif
2867 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2868 c47340da 2020-09-20 stsp char *base, *dotgit;
2869 f0207f6a 2020-10-19 stsp const char *path;
2870 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2871 76089277 2018-04-01 stsp if (repo_path == NULL)
2872 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2873 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2874 76089277 2018-04-01 stsp if (cwd == NULL) {
2875 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2876 76089277 2018-04-01 stsp goto done;
2877 76089277 2018-04-01 stsp }
2878 c47340da 2020-09-20 stsp if (path_prefix[0])
2879 f0207f6a 2020-10-19 stsp path = path_prefix;
2880 c47340da 2020-09-20 stsp else
2881 f0207f6a 2020-10-19 stsp path = repo_path;
2882 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2883 f0207f6a 2020-10-19 stsp if (error)
2884 c47340da 2020-09-20 stsp goto done;
2885 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2886 c09a553d 2018-03-12 stsp if (dotgit)
2887 c09a553d 2018-03-12 stsp *dotgit = '\0';
2888 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2889 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2890 f0207f6a 2020-10-19 stsp free(base);
2891 76089277 2018-04-01 stsp goto done;
2892 c09a553d 2018-03-12 stsp }
2893 f0207f6a 2020-10-19 stsp free(base);
2894 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2895 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2896 76089277 2018-04-01 stsp if (repo_path == NULL) {
2897 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2898 76089277 2018-04-01 stsp goto done;
2899 76089277 2018-04-01 stsp }
2900 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2901 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2902 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2903 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2904 b4b3a7dd 2019-07-22 stsp argv[1]);
2905 b4b3a7dd 2019-07-22 stsp goto done;
2906 b4b3a7dd 2019-07-22 stsp }
2907 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2908 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2909 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2910 b4b3a7dd 2019-07-22 stsp goto done;
2911 b4b3a7dd 2019-07-22 stsp }
2912 76089277 2018-04-01 stsp }
2913 c09a553d 2018-03-12 stsp } else
2914 c09a553d 2018-03-12 stsp usage_checkout();
2915 c09a553d 2018-03-12 stsp
2916 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2917 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2918 13bfb272 2019-05-10 jcs
2919 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2920 c09a553d 2018-03-12 stsp if (error != NULL)
2921 c02c541e 2019-03-29 stsp goto done;
2922 c02c541e 2019-03-29 stsp
2923 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2924 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2925 c530dc23 2019-07-23 stsp if (error) {
2926 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2927 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2928 c530dc23 2019-07-23 stsp goto done;
2929 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2930 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2931 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2932 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2933 c530dc23 2019-07-23 stsp goto done;
2934 c530dc23 2019-07-23 stsp }
2935 c530dc23 2019-07-23 stsp }
2936 c530dc23 2019-07-23 stsp
2937 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2938 c02c541e 2019-03-29 stsp if (error)
2939 c09a553d 2018-03-12 stsp goto done;
2940 8069f636 2019-01-12 stsp
2941 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2942 c09a553d 2018-03-12 stsp if (error != NULL)
2943 c09a553d 2018-03-12 stsp goto done;
2944 c09a553d 2018-03-12 stsp
2945 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2946 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2947 c09a553d 2018-03-12 stsp goto done;
2948 c09a553d 2018-03-12 stsp
2949 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2950 c09a553d 2018-03-12 stsp if (error != NULL)
2951 c09a553d 2018-03-12 stsp goto done;
2952 c09a553d 2018-03-12 stsp
2953 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2954 e5dc7198 2018-12-29 stsp path_prefix);
2955 e5dc7198 2018-12-29 stsp if (error != NULL)
2956 e5dc7198 2018-12-29 stsp goto done;
2957 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2958 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2959 49520a32 2018-12-29 stsp goto done;
2960 49520a32 2018-12-29 stsp }
2961 49520a32 2018-12-29 stsp
2962 8069f636 2019-01-12 stsp if (commit_id_str) {
2963 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2964 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2965 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2966 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2967 84de9106 2020-12-26 stsp NULL);
2968 84de9106 2020-12-26 stsp if (error)
2969 84de9106 2020-12-26 stsp goto done;
2970 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2971 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2972 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2973 30837e32 2019-07-25 stsp if (error)
2974 8069f636 2019-01-12 stsp goto done;
2975 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2976 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2977 8069f636 2019-01-12 stsp if (error != NULL) {
2978 8069f636 2019-01-12 stsp free(commit_id);
2979 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2980 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2981 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2982 4b6c9460 2020-03-05 stsp }
2983 8069f636 2019-01-12 stsp goto done;
2984 8069f636 2019-01-12 stsp }
2985 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2986 4b6c9460 2020-03-05 stsp if (error) {
2987 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2988 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2989 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2990 4b6c9460 2020-03-05 stsp }
2991 45d344f6 2019-05-14 stsp goto done;
2992 4b6c9460 2020-03-05 stsp }
2993 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2994 8069f636 2019-01-12 stsp commit_id);
2995 8069f636 2019-01-12 stsp free(commit_id);
2996 8069f636 2019-01-12 stsp if (error)
2997 8069f636 2019-01-12 stsp goto done;
2998 8069f636 2019-01-12 stsp }
2999 8069f636 2019-01-12 stsp
3000 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
3001 f2ea84fa 2019-07-27 stsp if (error)
3002 f2ea84fa 2019-07-27 stsp goto done;
3003 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
3004 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
3005 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3006 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
3007 c09a553d 2018-03-12 stsp if (error != NULL)
3008 c09a553d 2018-03-12 stsp goto done;
3009 c09a553d 2018-03-12 stsp
3010 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
3011 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
3012 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
3013 c09a553d 2018-03-12 stsp done:
3014 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3015 8069f636 2019-01-12 stsp free(commit_id_str);
3016 76089277 2018-04-01 stsp free(repo_path);
3017 507dc3bb 2018-12-29 stsp free(worktree_path);
3018 c47340da 2020-09-20 stsp free(cwd);
3019 507dc3bb 2018-12-29 stsp return error;
3020 9627c110 2020-04-18 stsp }
3021 9627c110 2020-04-18 stsp
3022 9627c110 2020-04-18 stsp struct got_update_progress_arg {
3023 9627c110 2020-04-18 stsp int did_something;
3024 9627c110 2020-04-18 stsp int conflicts;
3025 9627c110 2020-04-18 stsp int obstructed;
3026 9627c110 2020-04-18 stsp int not_updated;
3027 9627c110 2020-04-18 stsp };
3028 9627c110 2020-04-18 stsp
3029 9627c110 2020-04-18 stsp void
3030 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3031 9627c110 2020-04-18 stsp {
3032 9627c110 2020-04-18 stsp if (!upa->did_something)
3033 9627c110 2020-04-18 stsp return;
3034 9627c110 2020-04-18 stsp
3035 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
3036 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3037 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
3038 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
3039 9627c110 2020-04-18 stsp upa->obstructed);
3040 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
3041 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
3042 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
3043 507dc3bb 2018-12-29 stsp }
3044 507dc3bb 2018-12-29 stsp
3045 507dc3bb 2018-12-29 stsp __dead static void
3046 507dc3bb 2018-12-29 stsp usage_update(void)
3047 507dc3bb 2018-12-29 stsp {
3048 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
3049 507dc3bb 2018-12-29 stsp getprogname());
3050 507dc3bb 2018-12-29 stsp exit(1);
3051 507dc3bb 2018-12-29 stsp }
3052 507dc3bb 2018-12-29 stsp
3053 1ee397ad 2019-07-12 stsp static const struct got_error *
3054 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
3055 507dc3bb 2018-12-29 stsp {
3056 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3057 784955db 2019-01-12 stsp
3058 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
3059 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
3060 1ee397ad 2019-07-12 stsp return NULL;
3061 507dc3bb 2018-12-29 stsp
3062 9627c110 2020-04-18 stsp upa->did_something = 1;
3063 a484d721 2019-06-10 stsp
3064 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
3065 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
3066 1ee397ad 2019-07-12 stsp return NULL;
3067 a484d721 2019-06-10 stsp
3068 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
3069 9627c110 2020-04-18 stsp upa->conflicts++;
3070 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
3071 9627c110 2020-04-18 stsp upa->obstructed++;
3072 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
3073 9627c110 2020-04-18 stsp upa->not_updated++;
3074 9627c110 2020-04-18 stsp
3075 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3076 507dc3bb 2018-12-29 stsp path++;
3077 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
3078 1ee397ad 2019-07-12 stsp return NULL;
3079 be7061eb 2018-12-30 stsp }
3080 be7061eb 2018-12-30 stsp
3081 be7061eb 2018-12-30 stsp static const struct got_error *
3082 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
3083 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3084 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3085 a1fb16d8 2019-05-24 stsp {
3086 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3087 a1fb16d8 2019-05-24 stsp char *base_id_str;
3088 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3089 a1fb16d8 2019-05-24 stsp
3090 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3091 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3092 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3093 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3094 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3095 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3096 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3097 a1fb16d8 2019-05-24 stsp }
3098 a1fb16d8 2019-05-24 stsp
3099 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3100 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3101 a1fb16d8 2019-05-24 stsp if (err) {
3102 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3103 a1fb16d8 2019-05-24 stsp return err;
3104 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3105 a1fb16d8 2019-05-24 stsp }
3106 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3107 a1fb16d8 2019-05-24 stsp return NULL;
3108 a1fb16d8 2019-05-24 stsp
3109 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3110 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3111 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3112 a1fb16d8 2019-05-24 stsp if (err)
3113 a1fb16d8 2019-05-24 stsp return err;
3114 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3115 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3116 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3117 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3118 0ebf8283 2019-07-24 stsp return NULL;
3119 0ebf8283 2019-07-24 stsp }
3120 0ebf8283 2019-07-24 stsp
3121 0ebf8283 2019-07-24 stsp static const struct got_error *
3122 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3123 0ebf8283 2019-07-24 stsp {
3124 0ebf8283 2019-07-24 stsp const struct got_error *err;
3125 0ebf8283 2019-07-24 stsp int in_progress;
3126 0ebf8283 2019-07-24 stsp
3127 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3128 0ebf8283 2019-07-24 stsp if (err)
3129 0ebf8283 2019-07-24 stsp return err;
3130 0ebf8283 2019-07-24 stsp if (in_progress)
3131 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3132 0ebf8283 2019-07-24 stsp
3133 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3134 0ebf8283 2019-07-24 stsp if (err)
3135 0ebf8283 2019-07-24 stsp return err;
3136 0ebf8283 2019-07-24 stsp if (in_progress)
3137 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3138 0ebf8283 2019-07-24 stsp
3139 a1fb16d8 2019-05-24 stsp return NULL;
3140 a5edda0a 2019-07-27 stsp }
3141 a5edda0a 2019-07-27 stsp
3142 a5edda0a 2019-07-27 stsp static const struct got_error *
3143 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3144 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3145 a5edda0a 2019-07-27 stsp {
3146 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3147 a5edda0a 2019-07-27 stsp char *path;
3148 a5edda0a 2019-07-27 stsp int i;
3149 a5edda0a 2019-07-27 stsp
3150 a5edda0a 2019-07-27 stsp if (argc == 0) {
3151 a5edda0a 2019-07-27 stsp path = strdup("");
3152 a5edda0a 2019-07-27 stsp if (path == NULL)
3153 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3154 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3155 a5edda0a 2019-07-27 stsp }
3156 a5edda0a 2019-07-27 stsp
3157 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3158 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3159 a5edda0a 2019-07-27 stsp if (err)
3160 a5edda0a 2019-07-27 stsp break;
3161 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3162 a5edda0a 2019-07-27 stsp if (err) {
3163 a5edda0a 2019-07-27 stsp free(path);
3164 a5edda0a 2019-07-27 stsp break;
3165 a5edda0a 2019-07-27 stsp }
3166 a5edda0a 2019-07-27 stsp }
3167 a5edda0a 2019-07-27 stsp
3168 a5edda0a 2019-07-27 stsp return err;
3169 a1fb16d8 2019-05-24 stsp }
3170 a1fb16d8 2019-05-24 stsp
3171 a1fb16d8 2019-05-24 stsp static const struct got_error *
3172 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3173 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3174 fa51e947 2020-03-27 stsp {
3175 fa51e947 2020-03-27 stsp const struct got_error *err;
3176 fa51e947 2020-03-27 stsp struct got_repository *repo;
3177 fa51e947 2020-03-27 stsp static char msg[512];
3178 fa51e947 2020-03-27 stsp
3179 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3180 fa51e947 2020-03-27 stsp if (err)
3181 fa51e947 2020-03-27 stsp return orig_err;
3182 fa51e947 2020-03-27 stsp
3183 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3184 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3185 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3186 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3187 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3188 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3189 fa51e947 2020-03-27 stsp got_repo_close(repo);
3190 fa51e947 2020-03-27 stsp return err;
3191 fa51e947 2020-03-27 stsp }
3192 fa51e947 2020-03-27 stsp
3193 fa51e947 2020-03-27 stsp static const struct got_error *
3194 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3195 507dc3bb 2018-12-29 stsp {
3196 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3197 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3198 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3199 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3200 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3201 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3202 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3203 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3204 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3205 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3206 9627c110 2020-04-18 stsp int ch;
3207 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3208 507dc3bb 2018-12-29 stsp
3209 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3210 f2ea84fa 2019-07-27 stsp
3211 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3212 507dc3bb 2018-12-29 stsp switch (ch) {
3213 024e9686 2019-05-14 stsp case 'b':
3214 024e9686 2019-05-14 stsp branch_name = optarg;
3215 024e9686 2019-05-14 stsp break;
3216 507dc3bb 2018-12-29 stsp case 'c':
3217 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3218 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3219 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3220 507dc3bb 2018-12-29 stsp break;
3221 507dc3bb 2018-12-29 stsp default:
3222 2deda0b9 2019-03-07 stsp usage_update();
3223 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3224 507dc3bb 2018-12-29 stsp }
3225 507dc3bb 2018-12-29 stsp }
3226 507dc3bb 2018-12-29 stsp
3227 507dc3bb 2018-12-29 stsp argc -= optind;
3228 507dc3bb 2018-12-29 stsp argv += optind;
3229 507dc3bb 2018-12-29 stsp
3230 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3231 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3232 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3233 507dc3bb 2018-12-29 stsp err(1, "pledge");
3234 507dc3bb 2018-12-29 stsp #endif
3235 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3236 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3237 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3238 c4cdcb68 2019-04-03 stsp goto done;
3239 c4cdcb68 2019-04-03 stsp }
3240 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3241 fa51e947 2020-03-27 stsp if (error) {
3242 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3243 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3244 fa51e947 2020-03-27 stsp worktree_path);
3245 7d5807f4 2019-07-11 stsp goto done;
3246 fa51e947 2020-03-27 stsp }
3247 7d5807f4 2019-07-11 stsp
3248 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3249 f2ea84fa 2019-07-27 stsp if (error)
3250 f2ea84fa 2019-07-27 stsp goto done;
3251 507dc3bb 2018-12-29 stsp
3252 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3253 c9956ddf 2019-09-08 stsp NULL);
3254 507dc3bb 2018-12-29 stsp if (error != NULL)
3255 507dc3bb 2018-12-29 stsp goto done;
3256 507dc3bb 2018-12-29 stsp
3257 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3258 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3259 087fb88c 2019-08-04 stsp if (error)
3260 087fb88c 2019-08-04 stsp goto done;
3261 087fb88c 2019-08-04 stsp
3262 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3263 0266afb7 2019-01-04 stsp if (error)
3264 0266afb7 2019-01-04 stsp goto done;
3265 0266afb7 2019-01-04 stsp
3266 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3267 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3268 024e9686 2019-05-14 stsp if (error != NULL)
3269 024e9686 2019-05-14 stsp goto done;
3270 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3271 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3272 9c4b8182 2019-01-02 stsp if (error != NULL)
3273 9c4b8182 2019-01-02 stsp goto done;
3274 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3275 507dc3bb 2018-12-29 stsp if (error != NULL)
3276 507dc3bb 2018-12-29 stsp goto done;
3277 507dc3bb 2018-12-29 stsp } else {
3278 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3279 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3280 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3281 84de9106 2020-12-26 stsp NULL);
3282 84de9106 2020-12-26 stsp if (error)
3283 84de9106 2020-12-26 stsp goto done;
3284 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3285 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3286 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3287 04f57cb3 2019-07-25 stsp free(commit_id_str);
3288 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3289 30837e32 2019-07-25 stsp if (error)
3290 a297e751 2019-07-11 stsp goto done;
3291 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3292 a297e751 2019-07-11 stsp if (error)
3293 507dc3bb 2018-12-29 stsp goto done;
3294 507dc3bb 2018-12-29 stsp }
3295 35c965b2 2018-12-29 stsp
3296 a1fb16d8 2019-05-24 stsp if (branch_name) {
3297 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3298 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3299 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3300 f2ea84fa 2019-07-27 stsp continue;
3301 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3302 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3303 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3304 024e9686 2019-05-14 stsp goto done;
3305 024e9686 2019-05-14 stsp }
3306 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3307 024e9686 2019-05-14 stsp if (error)
3308 024e9686 2019-05-14 stsp goto done;
3309 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3310 3aef623b 2019-10-15 stsp repo);
3311 a367ff0f 2019-05-14 stsp free(head_commit_id);
3312 024e9686 2019-05-14 stsp if (error != NULL)
3313 024e9686 2019-05-14 stsp goto done;
3314 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3315 a367ff0f 2019-05-14 stsp if (error)
3316 a367ff0f 2019-05-14 stsp goto done;
3317 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3318 024e9686 2019-05-14 stsp if (error)
3319 024e9686 2019-05-14 stsp goto done;
3320 024e9686 2019-05-14 stsp } else {
3321 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3322 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3323 a367ff0f 2019-05-14 stsp if (error != NULL) {
3324 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3325 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3326 024e9686 2019-05-14 stsp goto done;
3327 a367ff0f 2019-05-14 stsp }
3328 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3329 a367ff0f 2019-05-14 stsp if (error)
3330 a367ff0f 2019-05-14 stsp goto done;
3331 024e9686 2019-05-14 stsp }
3332 507dc3bb 2018-12-29 stsp
3333 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3334 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3335 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3336 507dc3bb 2018-12-29 stsp commit_id);
3337 507dc3bb 2018-12-29 stsp if (error)
3338 507dc3bb 2018-12-29 stsp goto done;
3339 507dc3bb 2018-12-29 stsp }
3340 507dc3bb 2018-12-29 stsp
3341 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3342 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3343 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3344 507dc3bb 2018-12-29 stsp if (error != NULL)
3345 507dc3bb 2018-12-29 stsp goto done;
3346 9c4b8182 2019-01-02 stsp
3347 9627c110 2020-04-18 stsp if (upa.did_something)
3348 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3349 784955db 2019-01-12 stsp else
3350 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3351 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3352 507dc3bb 2018-12-29 stsp done:
3353 c09a553d 2018-03-12 stsp free(worktree_path);
3354 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3355 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3356 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3357 507dc3bb 2018-12-29 stsp free(commit_id);
3358 9c4b8182 2019-01-02 stsp free(commit_id_str);
3359 c09a553d 2018-03-12 stsp return error;
3360 c09a553d 2018-03-12 stsp }
3361 c09a553d 2018-03-12 stsp
3362 f42b1b34 2018-03-12 stsp static const struct got_error *
3363 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3364 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3365 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3366 5c860e29 2018-03-12 stsp {
3367 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3368 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3369 79109fed 2018-03-27 stsp
3370 44392932 2019-08-25 stsp if (blob_id1) {
3371 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3372 44392932 2019-08-25 stsp if (err)
3373 44392932 2019-08-25 stsp goto done;
3374 44392932 2019-08-25 stsp }
3375 79109fed 2018-03-27 stsp
3376 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3377 44392932 2019-08-25 stsp if (err)
3378 44392932 2019-08-25 stsp goto done;
3379 79109fed 2018-03-27 stsp
3380 44392932 2019-08-25 stsp while (path[0] == '/')
3381 44392932 2019-08-25 stsp path++;
3382 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3383 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3384 44392932 2019-08-25 stsp done:
3385 44392932 2019-08-25 stsp if (blob1)
3386 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3387 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3388 44392932 2019-08-25 stsp return err;
3389 44392932 2019-08-25 stsp }
3390 44392932 2019-08-25 stsp
3391 44392932 2019-08-25 stsp static const struct got_error *
3392 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3393 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3394 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3395 44392932 2019-08-25 stsp {
3396 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3397 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3398 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3399 44392932 2019-08-25 stsp
3400 44392932 2019-08-25 stsp if (tree_id1) {
3401 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3402 79109fed 2018-03-27 stsp if (err)
3403 44392932 2019-08-25 stsp goto done;
3404 79109fed 2018-03-27 stsp }
3405 79109fed 2018-03-27 stsp
3406 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3407 0f2b3dca 2018-12-22 stsp if (err)
3408 0f2b3dca 2018-12-22 stsp goto done;
3409 0f2b3dca 2018-12-22 stsp
3410 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3411 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3412 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3413 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3414 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3415 fe621944 2020-11-10 stsp arg.nlines = 0;
3416 44392932 2019-08-25 stsp while (path[0] == '/')
3417 44392932 2019-08-25 stsp path++;
3418 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3419 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3420 0f2b3dca 2018-12-22 stsp done:
3421 79109fed 2018-03-27 stsp if (tree1)
3422 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3423 366e0a5f 2019-10-10 stsp if (tree2)
3424 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3425 44392932 2019-08-25 stsp return err;
3426 44392932 2019-08-25 stsp }
3427 44392932 2019-08-25 stsp
3428 44392932 2019-08-25 stsp static const struct got_error *
3429 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3430 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3431 0208f208 2020-05-05 stsp {
3432 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3433 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3434 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3435 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3436 0208f208 2020-05-05 stsp
3437 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3438 0208f208 2020-05-05 stsp if (qid != NULL) {
3439 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3440 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3441 0208f208 2020-05-05 stsp qid->id);
3442 0208f208 2020-05-05 stsp if (err)
3443 0208f208 2020-05-05 stsp return err;
3444 0208f208 2020-05-05 stsp
3445 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3446 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3447 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3448 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3449 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3450 aa8b5dd0 2021-08-01 stsp }
3451 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3452 0208f208 2020-05-05 stsp
3453 0208f208 2020-05-05 stsp }
3454 0208f208 2020-05-05 stsp
3455 0208f208 2020-05-05 stsp if (tree_id1) {
3456 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3457 0208f208 2020-05-05 stsp if (err)
3458 0208f208 2020-05-05 stsp goto done;
3459 0208f208 2020-05-05 stsp }
3460 0208f208 2020-05-05 stsp
3461 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3462 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3463 0208f208 2020-05-05 stsp if (err)
3464 0208f208 2020-05-05 stsp goto done;
3465 0208f208 2020-05-05 stsp
3466 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3467 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3468 0208f208 2020-05-05 stsp done:
3469 0208f208 2020-05-05 stsp if (tree1)
3470 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3471 0208f208 2020-05-05 stsp if (tree2)
3472 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3473 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3474 0208f208 2020-05-05 stsp return err;
3475 0208f208 2020-05-05 stsp }
3476 0208f208 2020-05-05 stsp
3477 0208f208 2020-05-05 stsp static const struct got_error *
3478 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3479 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3480 44392932 2019-08-25 stsp {
3481 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3482 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3483 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3484 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3485 44392932 2019-08-25 stsp struct got_object_qid *qid;
3486 44392932 2019-08-25 stsp
3487 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3488 44392932 2019-08-25 stsp if (qid != NULL) {
3489 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3490 44392932 2019-08-25 stsp qid->id);
3491 44392932 2019-08-25 stsp if (err)
3492 44392932 2019-08-25 stsp return err;
3493 44392932 2019-08-25 stsp }
3494 44392932 2019-08-25 stsp
3495 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3496 44392932 2019-08-25 stsp int obj_type;
3497 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3498 44392932 2019-08-25 stsp if (err)
3499 44392932 2019-08-25 stsp goto done;
3500 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3501 44392932 2019-08-25 stsp if (err) {
3502 44392932 2019-08-25 stsp free(obj_id2);
3503 44392932 2019-08-25 stsp goto done;
3504 44392932 2019-08-25 stsp }
3505 44392932 2019-08-25 stsp if (pcommit) {
3506 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3507 44392932 2019-08-25 stsp qid->id, path);
3508 44392932 2019-08-25 stsp if (err) {
3509 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3510 2e8c69d1 2020-05-04 stsp free(obj_id2);
3511 2e8c69d1 2020-05-04 stsp goto done;
3512 2e8c69d1 2020-05-04 stsp }
3513 2e8c69d1 2020-05-04 stsp } else {
3514 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3515 2e8c69d1 2020-05-04 stsp if (err) {
3516 2e8c69d1 2020-05-04 stsp free(obj_id2);
3517 2e8c69d1 2020-05-04 stsp goto done;
3518 2e8c69d1 2020-05-04 stsp }
3519 44392932 2019-08-25 stsp }
3520 44392932 2019-08-25 stsp }
3521 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3522 44392932 2019-08-25 stsp if (err) {
3523 44392932 2019-08-25 stsp free(obj_id2);
3524 44392932 2019-08-25 stsp goto done;
3525 44392932 2019-08-25 stsp }
3526 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3527 44392932 2019-08-25 stsp switch (obj_type) {
3528 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3529 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3530 64453f7e 2020-11-21 stsp 0, 0, repo);
3531 44392932 2019-08-25 stsp break;
3532 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3533 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3534 64453f7e 2020-11-21 stsp 0, 0, repo);
3535 44392932 2019-08-25 stsp break;
3536 44392932 2019-08-25 stsp default:
3537 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3538 44392932 2019-08-25 stsp break;
3539 44392932 2019-08-25 stsp }
3540 44392932 2019-08-25 stsp free(obj_id1);
3541 44392932 2019-08-25 stsp free(obj_id2);
3542 44392932 2019-08-25 stsp } else {
3543 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3544 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3545 44392932 2019-08-25 stsp if (err)
3546 44392932 2019-08-25 stsp goto done;
3547 579bd556 2020-10-24 stsp if (pcommit) {
3548 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3549 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3550 579bd556 2020-10-24 stsp if (err)
3551 579bd556 2020-10-24 stsp goto done;
3552 579bd556 2020-10-24 stsp }
3553 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3554 64453f7e 2020-11-21 stsp id_str2);
3555 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3556 64453f7e 2020-11-21 stsp repo);
3557 44392932 2019-08-25 stsp }
3558 44392932 2019-08-25 stsp done:
3559 0f2b3dca 2018-12-22 stsp free(id_str1);
3560 0f2b3dca 2018-12-22 stsp free(id_str2);
3561 44392932 2019-08-25 stsp if (pcommit)
3562 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3563 79109fed 2018-03-27 stsp return err;
3564 79109fed 2018-03-27 stsp }
3565 79109fed 2018-03-27 stsp
3566 4bb494d5 2018-06-16 stsp static char *
3567 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3568 6c281f94 2018-06-11 stsp {
3569 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3570 09867e48 2019-08-13 stsp char *p, *s;
3571 09867e48 2019-08-13 stsp
3572 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3573 09867e48 2019-08-13 stsp if (tm == NULL)
3574 09867e48 2019-08-13 stsp return NULL;
3575 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3576 09867e48 2019-08-13 stsp if (s == NULL)
3577 09867e48 2019-08-13 stsp return NULL;
3578 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3579 6c281f94 2018-06-11 stsp if (p)
3580 6c281f94 2018-06-11 stsp *p = '\0';
3581 6c281f94 2018-06-11 stsp return s;
3582 6c281f94 2018-06-11 stsp }
3583 dc424a06 2019-08-07 stsp
3584 6841bf13 2019-11-29 kn static const struct got_error *
3585 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3586 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3587 6841bf13 2019-11-29 kn {
3588 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3589 6841bf13 2019-11-29 kn regmatch_t regmatch;
3590 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3591 6841bf13 2019-11-29 kn
3592 6841bf13 2019-11-29 kn *have_match = 0;
3593 6841bf13 2019-11-29 kn
3594 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3595 6841bf13 2019-11-29 kn if (err)
3596 6841bf13 2019-11-29 kn return err;
3597 6841bf13 2019-11-29 kn
3598 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3599 6841bf13 2019-11-29 kn if (err)
3600 6841bf13 2019-11-29 kn goto done;
3601 6841bf13 2019-11-29 kn
3602 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3603 6841bf13 2019-11-29 kn *have_match = 1;
3604 6841bf13 2019-11-29 kn done:
3605 6841bf13 2019-11-29 kn free(id_str);
3606 6841bf13 2019-11-29 kn free(logmsg);
3607 6841bf13 2019-11-29 kn return err;
3608 6841bf13 2019-11-29 kn }
3609 6841bf13 2019-11-29 kn
3610 0208f208 2020-05-05 stsp static void
3611 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3612 0208f208 2020-05-05 stsp regex_t *regex)
3613 0208f208 2020-05-05 stsp {
3614 0208f208 2020-05-05 stsp regmatch_t regmatch;
3615 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3616 0208f208 2020-05-05 stsp
3617 0208f208 2020-05-05 stsp *have_match = 0;
3618 0208f208 2020-05-05 stsp
3619 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3620 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3621 0208f208 2020-05-05 stsp *have_match = 1;
3622 0208f208 2020-05-05 stsp break;
3623 0208f208 2020-05-05 stsp }
3624 0208f208 2020-05-05 stsp }
3625 0208f208 2020-05-05 stsp }
3626 0208f208 2020-05-05 stsp
3627 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3628 6c281f94 2018-06-11 stsp
3629 888b7d99 2020-12-26 stsp static const struct got_error*
3630 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3631 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3632 888b7d99 2020-12-26 stsp {
3633 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3634 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3635 888b7d99 2020-12-26 stsp char *s;
3636 888b7d99 2020-12-26 stsp const char *name;
3637 5c860e29 2018-03-12 stsp
3638 888b7d99 2020-12-26 stsp *refs_str = NULL;
3639 888b7d99 2020-12-26 stsp
3640 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3641 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3642 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3643 a436ad14 2019-08-13 stsp int cmp;
3644 a436ad14 2019-08-13 stsp
3645 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3646 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3647 d9498b20 2019-02-05 stsp continue;
3648 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3649 199a4027 2019-02-02 stsp name += 5;
3650 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3651 7143d404 2019-03-12 stsp continue;
3652 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3653 e34f9ed6 2019-02-02 stsp name += 6;
3654 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3655 141c2bff 2019-02-04 stsp name += 8;
3656 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3657 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3658 4343a07f 2020-04-24 stsp continue;
3659 4343a07f 2020-04-24 stsp }
3660 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3661 48cae60d 2020-09-22 stsp if (err)
3662 888b7d99 2020-12-26 stsp break;
3663 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3664 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3665 5d844a1e 2019-08-13 stsp if (err) {
3666 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3667 48cae60d 2020-09-22 stsp free(ref_id);
3668 888b7d99 2020-12-26 stsp break;
3669 48cae60d 2020-09-22 stsp }
3670 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3671 5d844a1e 2019-08-13 stsp err = NULL;
3672 5d844a1e 2019-08-13 stsp tag = NULL;
3673 5d844a1e 2019-08-13 stsp }
3674 a436ad14 2019-08-13 stsp }
3675 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3676 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3677 48cae60d 2020-09-22 stsp free(ref_id);
3678 a436ad14 2019-08-13 stsp if (tag)
3679 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3680 a436ad14 2019-08-13 stsp if (cmp != 0)
3681 a436ad14 2019-08-13 stsp continue;
3682 888b7d99 2020-12-26 stsp s = *refs_str;
3683 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3684 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3685 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3686 199a4027 2019-02-02 stsp free(s);
3687 888b7d99 2020-12-26 stsp *refs_str = NULL;
3688 888b7d99 2020-12-26 stsp break;
3689 199a4027 2019-02-02 stsp }
3690 199a4027 2019-02-02 stsp free(s);
3691 199a4027 2019-02-02 stsp }
3692 888b7d99 2020-12-26 stsp
3693 888b7d99 2020-12-26 stsp return err;
3694 888b7d99 2020-12-26 stsp }
3695 888b7d99 2020-12-26 stsp
3696 888b7d99 2020-12-26 stsp static const struct got_error *
3697 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3698 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3699 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3700 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3701 e600f124 2021-03-21 stsp const char *custom_refs_str)
3702 888b7d99 2020-12-26 stsp {
3703 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3704 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3705 888b7d99 2020-12-26 stsp char datebuf[26];
3706 888b7d99 2020-12-26 stsp time_t committer_time;
3707 888b7d99 2020-12-26 stsp const char *author, *committer;
3708 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3709 888b7d99 2020-12-26 stsp
3710 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3711 8bf5b3c9 2018-03-17 stsp if (err)
3712 8bf5b3c9 2018-03-17 stsp return err;
3713 788c352e 2018-06-16 stsp
3714 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3715 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3716 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3717 e600f124 2021-03-21 stsp if (refs) {
3718 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3719 e600f124 2021-03-21 stsp if (err)
3720 e600f124 2021-03-21 stsp goto done;
3721 e600f124 2021-03-21 stsp }
3722 888b7d99 2020-12-26 stsp }
3723 888b7d99 2020-12-26 stsp
3724 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3725 e600f124 2021-03-21 stsp if (custom_refs_str)
3726 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3727 e600f124 2021-03-21 stsp else
3728 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3729 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3730 832c249c 2018-06-10 stsp free(id_str);
3731 d3d493d7 2019-02-21 stsp id_str = NULL;
3732 d3d493d7 2019-02-21 stsp free(refs_str);
3733 d3d493d7 2019-02-21 stsp refs_str = NULL;
3734 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3735 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3736 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3737 09867e48 2019-08-13 stsp if (datestr)
3738 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3739 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3740 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3741 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3742 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3743 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3744 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3745 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3746 3fe1abad 2018-06-10 stsp int n = 1;
3747 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3748 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
3749 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3750 a0603db2 2018-06-10 stsp if (err)
3751 888b7d99 2020-12-26 stsp goto done;
3752 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3753 a0603db2 2018-06-10 stsp free(id_str);
3754 888b7d99 2020-12-26 stsp id_str = NULL;
3755 a0603db2 2018-06-10 stsp }
3756 a0603db2 2018-06-10 stsp }
3757 832c249c 2018-06-10 stsp
3758 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3759 5943eee2 2019-08-13 stsp if (err)
3760 888b7d99 2020-12-26 stsp goto done;
3761 8bf5b3c9 2018-03-17 stsp
3762 621015ac 2018-07-23 stsp logmsg = logmsg0;
3763 832c249c 2018-06-10 stsp do {
3764 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3765 832c249c 2018-06-10 stsp if (line)
3766 832c249c 2018-06-10 stsp printf(" %s\n", line);
3767 832c249c 2018-06-10 stsp } while (line);
3768 621015ac 2018-07-23 stsp free(logmsg0);
3769 832c249c 2018-06-10 stsp
3770 0208f208 2020-05-05 stsp if (changed_paths) {
3771 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3772 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3773 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3774 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3775 0208f208 2020-05-05 stsp }
3776 0208f208 2020-05-05 stsp printf("\n");
3777 0208f208 2020-05-05 stsp }
3778 971751ac 2018-03-27 stsp if (show_patch) {
3779 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3780 971751ac 2018-03-27 stsp if (err == 0)
3781 971751ac 2018-03-27 stsp printf("\n");
3782 971751ac 2018-03-27 stsp }
3783 07862c20 2018-09-15 stsp
3784 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3785 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3786 888b7d99 2020-12-26 stsp done:
3787 888b7d99 2020-12-26 stsp free(id_str);
3788 888b7d99 2020-12-26 stsp free(refs_str);
3789 07862c20 2018-09-15 stsp return err;
3790 f42b1b34 2018-03-12 stsp }
3791 5c860e29 2018-03-12 stsp
3792 f42b1b34 2018-03-12 stsp static const struct got_error *
3793 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3794 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3795 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3796 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3797 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3798 f42b1b34 2018-03-12 stsp {
3799 f42b1b34 2018-03-12 stsp const struct got_error *err;
3800 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3801 6841bf13 2019-11-29 kn regex_t regex;
3802 6841bf13 2019-11-29 kn int have_match;
3803 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3804 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3805 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3806 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3807 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3808 dbec59df 2020-04-18 stsp
3809 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3810 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3811 372ccdbb 2018-06-10 stsp
3812 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3813 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3814 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3815 6841bf13 2019-11-29 kn
3816 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3817 f42b1b34 2018-03-12 stsp if (err)
3818 f42b1b34 2018-03-12 stsp return err;
3819 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3820 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3821 372ccdbb 2018-06-10 stsp if (err)
3822 fcc85cad 2018-07-22 stsp goto done;
3823 656b1f76 2019-05-11 jcs for (;;) {
3824 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3825 8bf5b3c9 2018-03-17 stsp
3826 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3827 84453469 2018-11-11 stsp break;
3828 84453469 2018-11-11 stsp
3829 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3830 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3831 372ccdbb 2018-06-10 stsp if (err) {
3832 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3833 9ba79e04 2018-06-11 stsp err = NULL;
3834 ee780d5c 2020-01-04 stsp break;
3835 7e665116 2018-04-02 stsp }
3836 b43fbaa0 2018-06-11 stsp if (id == NULL)
3837 7e665116 2018-04-02 stsp break;
3838 b43fbaa0 2018-06-11 stsp
3839 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3840 b43fbaa0 2018-06-11 stsp if (err)
3841 fcc85cad 2018-07-22 stsp break;
3842 6841bf13 2019-11-29 kn
3843 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3844 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3845 0208f208 2020-05-05 stsp if (err)
3846 0208f208 2020-05-05 stsp break;
3847 0208f208 2020-05-05 stsp }
3848 0208f208 2020-05-05 stsp
3849 6841bf13 2019-11-29 kn if (search_pattern) {
3850 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3851 6841bf13 2019-11-29 kn if (err) {
3852 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3853 6841bf13 2019-11-29 kn break;
3854 6841bf13 2019-11-29 kn }
3855 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3856 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3857 0208f208 2020-05-05 stsp &changed_paths, &regex);
3858 6841bf13 2019-11-29 kn if (have_match == 0) {
3859 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3860 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3861 0208f208 2020-05-05 stsp free((char *)pe->path);
3862 0208f208 2020-05-05 stsp free(pe->data);
3863 0208f208 2020-05-05 stsp }
3864 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3865 6841bf13 2019-11-29 kn continue;
3866 6841bf13 2019-11-29 kn }
3867 6841bf13 2019-11-29 kn }
3868 6841bf13 2019-11-29 kn
3869 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3870 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3871 dbec59df 2020-04-18 stsp if (err)
3872 dbec59df 2020-04-18 stsp break;
3873 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3874 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3875 dbec59df 2020-04-18 stsp } else {
3876 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3877 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3878 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3879 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3880 dbec59df 2020-04-18 stsp if (err)
3881 dbec59df 2020-04-18 stsp break;
3882 dbec59df 2020-04-18 stsp }
3883 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3884 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3885 7e665116 2018-04-02 stsp break;
3886 0208f208 2020-05-05 stsp
3887 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3888 0208f208 2020-05-05 stsp free((char *)pe->path);
3889 0208f208 2020-05-05 stsp free(pe->data);
3890 0208f208 2020-05-05 stsp }
3891 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3892 31cedeaf 2018-09-15 stsp }
3893 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3894 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
3895 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3896 dbec59df 2020-04-18 stsp if (err)
3897 dbec59df 2020-04-18 stsp break;
3898 502b9684 2020-07-31 stsp if (show_changed_paths) {
3899 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3900 502b9684 2020-07-31 stsp commit, repo);
3901 502b9684 2020-07-31 stsp if (err)
3902 502b9684 2020-07-31 stsp break;
3903 502b9684 2020-07-31 stsp }
3904 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3905 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3906 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3907 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3908 dbec59df 2020-04-18 stsp if (err)
3909 dbec59df 2020-04-18 stsp break;
3910 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3911 502b9684 2020-07-31 stsp free((char *)pe->path);
3912 502b9684 2020-07-31 stsp free(pe->data);
3913 502b9684 2020-07-31 stsp }
3914 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3915 dbec59df 2020-04-18 stsp }
3916 dbec59df 2020-04-18 stsp }
3917 fcc85cad 2018-07-22 stsp done:
3918 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
3919 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
3920 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3921 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3922 dbec59df 2020-04-18 stsp }
3923 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3924 0208f208 2020-05-05 stsp free((char *)pe->path);
3925 0208f208 2020-05-05 stsp free(pe->data);
3926 0208f208 2020-05-05 stsp }
3927 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3928 6841bf13 2019-11-29 kn if (search_pattern)
3929 6841bf13 2019-11-29 kn regfree(&regex);
3930 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3931 f42b1b34 2018-03-12 stsp return err;
3932 f42b1b34 2018-03-12 stsp }
3933 5c860e29 2018-03-12 stsp
3934 4ed7e80c 2018-05-20 stsp __dead static void
3935 6f3d1eb0 2018-03-12 stsp usage_log(void)
3936 6f3d1eb0 2018-03-12 stsp {
3937 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3938 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3939 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3940 6f3d1eb0 2018-03-12 stsp exit(1);
3941 b1ebc001 2019-08-13 stsp }
3942 b1ebc001 2019-08-13 stsp
3943 b1ebc001 2019-08-13 stsp static int
3944 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3945 b1ebc001 2019-08-13 stsp {
3946 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3947 b1ebc001 2019-08-13 stsp long long n;
3948 b1ebc001 2019-08-13 stsp const char *errstr;
3949 b1ebc001 2019-08-13 stsp
3950 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3951 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3952 b1ebc001 2019-08-13 stsp return 0;
3953 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3954 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3955 b1ebc001 2019-08-13 stsp return 0;
3956 b1ebc001 2019-08-13 stsp return n;
3957 6f3d1eb0 2018-03-12 stsp }
3958 6f3d1eb0 2018-03-12 stsp
3959 4ed7e80c 2018-05-20 stsp static const struct got_error *
3960 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3961 f42b1b34 2018-03-12 stsp {
3962 f42b1b34 2018-03-12 stsp const struct got_error *error;
3963 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3964 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3965 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3966 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3967 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3968 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3969 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3970 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3971 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3972 64a96a6d 2018-04-01 stsp const char *errstr;
3973 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3974 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
3975 1b3893a2 2019-03-18 stsp
3976 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3977 5c860e29 2018-03-12 stsp
3978 6715a751 2018-03-16 stsp #ifndef PROFILE
3979 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3980 6098196c 2019-01-04 stsp NULL)
3981 ad242220 2018-09-08 stsp == -1)
3982 f42b1b34 2018-03-12 stsp err(1, "pledge");
3983 6715a751 2018-03-16 stsp #endif
3984 79109fed 2018-03-27 stsp
3985 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3986 b1ebc001 2019-08-13 stsp
3987 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3988 79109fed 2018-03-27 stsp switch (ch) {
3989 79109fed 2018-03-27 stsp case 'p':
3990 79109fed 2018-03-27 stsp show_patch = 1;
3991 d142fc45 2018-04-01 stsp break;
3992 0208f208 2020-05-05 stsp case 'P':
3993 0208f208 2020-05-05 stsp show_changed_paths = 1;
3994 0208f208 2020-05-05 stsp break;
3995 d142fc45 2018-04-01 stsp case 'c':
3996 d142fc45 2018-04-01 stsp start_commit = optarg;
3997 64a96a6d 2018-04-01 stsp break;
3998 c0cc5c62 2018-10-18 stsp case 'C':
3999 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4000 4a8520aa 2018-10-18 stsp &errstr);
4001 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4002 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4003 c0cc5c62 2018-10-18 stsp break;
4004 64a96a6d 2018-04-01 stsp case 'l':
4005 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
4006 64a96a6d 2018-04-01 stsp if (errstr != NULL)
4007 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
4008 cc54c501 2019-07-15 stsp break;
4009 48c8c60d 2020-01-27 stsp case 'b':
4010 48c8c60d 2020-01-27 stsp log_branches = 1;
4011 a0603db2 2018-06-10 stsp break;
4012 04ca23f4 2018-07-16 stsp case 'r':
4013 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4014 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
4015 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4016 9ba1d308 2019-10-21 stsp optarg);
4017 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4018 04ca23f4 2018-07-16 stsp break;
4019 dbec59df 2020-04-18 stsp case 'R':
4020 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4021 dbec59df 2020-04-18 stsp break;
4022 6841bf13 2019-11-29 kn case 's':
4023 6841bf13 2019-11-29 kn search_pattern = optarg;
4024 6841bf13 2019-11-29 kn break;
4025 d1fe46f9 2020-04-18 stsp case 'x':
4026 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4027 d1fe46f9 2020-04-18 stsp break;
4028 79109fed 2018-03-27 stsp default:
4029 2deda0b9 2019-03-07 stsp usage_log();
4030 79109fed 2018-03-27 stsp /* NOTREACHED */
4031 79109fed 2018-03-27 stsp }
4032 79109fed 2018-03-27 stsp }
4033 79109fed 2018-03-27 stsp
4034 79109fed 2018-03-27 stsp argc -= optind;
4035 79109fed 2018-03-27 stsp argv += optind;
4036 f42b1b34 2018-03-12 stsp
4037 dc1edbfa 2019-11-29 kn if (diff_context == -1)
4038 dc1edbfa 2019-11-29 kn diff_context = 3;
4039 dc1edbfa 2019-11-29 kn else if (!show_patch)
4040 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
4041 dc1edbfa 2019-11-29 kn
4042 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
4043 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
4044 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4045 04ca23f4 2018-07-16 stsp goto done;
4046 04ca23f4 2018-07-16 stsp }
4047 cffc0aa4 2019-02-05 stsp
4048 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
4049 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
4050 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4051 50f2fada 2020-04-24 stsp goto done;
4052 50f2fada 2020-04-24 stsp error = NULL;
4053 50f2fada 2020-04-24 stsp }
4054 cffc0aa4 2019-02-05 stsp
4055 603cdeb0 2020-10-22 stsp if (argc == 1) {
4056 e7301579 2019-03-18 stsp if (worktree) {
4057 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4058 e7301579 2019-03-18 stsp argv[0]);
4059 e7301579 2019-03-18 stsp if (error)
4060 e7301579 2019-03-18 stsp goto done;
4061 e7301579 2019-03-18 stsp } else {
4062 e7301579 2019-03-18 stsp path = strdup(argv[0]);
4063 e7301579 2019-03-18 stsp if (path == NULL) {
4064 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4065 e7301579 2019-03-18 stsp goto done;
4066 e7301579 2019-03-18 stsp }
4067 e7301579 2019-03-18 stsp }
4068 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4069 cbd1af7a 2019-03-18 stsp usage_log();
4070 cbd1af7a 2019-03-18 stsp
4071 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
4072 5486daa2 2019-05-11 stsp repo_path = worktree ?
4073 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4074 5486daa2 2019-05-11 stsp }
4075 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
4076 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4077 cffc0aa4 2019-02-05 stsp goto done;
4078 04ca23f4 2018-07-16 stsp }
4079 6098196c 2019-01-04 stsp
4080 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4081 d7d4f210 2018-03-12 stsp if (error != NULL)
4082 04ca23f4 2018-07-16 stsp goto done;
4083 f42b1b34 2018-03-12 stsp
4084 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4085 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4086 c02c541e 2019-03-29 stsp if (error)
4087 c02c541e 2019-03-29 stsp goto done;
4088 c02c541e 2019-03-29 stsp
4089 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4090 84de9106 2020-12-26 stsp if (error)
4091 84de9106 2020-12-26 stsp goto done;
4092 84de9106 2020-12-26 stsp
4093 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4094 888b7d99 2020-12-26 stsp if (error)
4095 888b7d99 2020-12-26 stsp goto done;
4096 888b7d99 2020-12-26 stsp
4097 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4098 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4099 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4100 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4101 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4102 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4103 3235492e 2018-04-01 stsp if (error != NULL)
4104 d1fe46f9 2020-04-18 stsp goto done;
4105 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4106 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4107 3235492e 2018-04-01 stsp if (error != NULL)
4108 d1fe46f9 2020-04-18 stsp goto done;
4109 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4110 d1fe46f9 2020-04-18 stsp start_id);
4111 d1fe46f9 2020-04-18 stsp if (error != NULL)
4112 d1fe46f9 2020-04-18 stsp goto done;
4113 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4114 3235492e 2018-04-01 stsp } else {
4115 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4116 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4117 d1fe46f9 2020-04-18 stsp if (error != NULL)
4118 d1fe46f9 2020-04-18 stsp goto done;
4119 3235492e 2018-04-01 stsp }
4120 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4121 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4122 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4123 d1fe46f9 2020-04-18 stsp if (error != NULL)
4124 d1fe46f9 2020-04-18 stsp goto done;
4125 d1fe46f9 2020-04-18 stsp }
4126 04ca23f4 2018-07-16 stsp
4127 deeabeae 2019-08-27 stsp if (worktree) {
4128 603cdeb0 2020-10-22 stsp /*
4129 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4130 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4131 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4132 603cdeb0 2020-10-22 stsp */
4133 603cdeb0 2020-10-22 stsp if (path) {
4134 603cdeb0 2020-10-22 stsp const char *prefix;
4135 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4136 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4137 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4138 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4139 603cdeb0 2020-10-22 stsp goto done;
4140 603cdeb0 2020-10-22 stsp }
4141 603cdeb0 2020-10-22 stsp }
4142 deeabeae 2019-08-27 stsp } else
4143 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4144 8fa913ec 2020-11-14 stsp path ? path : "");
4145 04ca23f4 2018-07-16 stsp if (error != NULL)
4146 04ca23f4 2018-07-16 stsp goto done;
4147 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4148 04ca23f4 2018-07-16 stsp free(path);
4149 04ca23f4 2018-07-16 stsp path = in_repo_path;
4150 04ca23f4 2018-07-16 stsp }
4151 199a4027 2019-02-02 stsp
4152 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4153 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4154 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4155 04ca23f4 2018-07-16 stsp done:
4156 04ca23f4 2018-07-16 stsp free(path);
4157 04ca23f4 2018-07-16 stsp free(repo_path);
4158 04ca23f4 2018-07-16 stsp free(cwd);
4159 cffc0aa4 2019-02-05 stsp if (worktree)
4160 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4161 ad242220 2018-09-08 stsp if (repo) {
4162 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4163 ad242220 2018-09-08 stsp if (error == NULL)
4164 1d0f4054 2021-06-17 stsp error = close_err;
4165 ad242220 2018-09-08 stsp }
4166 888b7d99 2020-12-26 stsp if (refs_idmap)
4167 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4168 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4169 8bf5b3c9 2018-03-17 stsp return error;
4170 5c860e29 2018-03-12 stsp }
4171 5c860e29 2018-03-12 stsp
4172 4ed7e80c 2018-05-20 stsp __dead static void
4173 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4174 3f8b7d6a 2018-04-01 stsp {
4175 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4176 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
4177 3f8b7d6a 2018-04-01 stsp exit(1);
4178 b00d56cd 2018-04-01 stsp }
4179 b00d56cd 2018-04-01 stsp
4180 b72f483a 2019-02-05 stsp struct print_diff_arg {
4181 b72f483a 2019-02-05 stsp struct got_repository *repo;
4182 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4183 b72f483a 2019-02-05 stsp int diff_context;
4184 3fc0c068 2019-02-10 stsp const char *id_str;
4185 3fc0c068 2019-02-10 stsp int header_shown;
4186 98eaaa12 2019-08-03 stsp int diff_staged;
4187 63035f9f 2019-10-06 stsp int ignore_whitespace;
4188 64453f7e 2020-11-21 stsp int force_text_diff;
4189 b72f483a 2019-02-05 stsp };
4190 39449a05 2020-07-23 stsp
4191 39449a05 2020-07-23 stsp /*
4192 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4193 39449a05 2020-07-23 stsp * it as content to the diff engine.
4194 39449a05 2020-07-23 stsp */
4195 39449a05 2020-07-23 stsp static const struct got_error *
4196 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4197 39449a05 2020-07-23 stsp const char *abspath)
4198 39449a05 2020-07-23 stsp {
4199 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4200 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4201 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4202 39449a05 2020-07-23 stsp
4203 39449a05 2020-07-23 stsp *fd = -1;
4204 39449a05 2020-07-23 stsp
4205 39449a05 2020-07-23 stsp if (dirfd != -1) {
4206 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4207 39449a05 2020-07-23 stsp if (target_len == -1)
4208 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4209 39449a05 2020-07-23 stsp } else {
4210 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4211 39449a05 2020-07-23 stsp if (target_len == -1)
4212 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4213 39449a05 2020-07-23 stsp }
4214 39449a05 2020-07-23 stsp
4215 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4216 39449a05 2020-07-23 stsp if (*fd == -1)
4217 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4218 39449a05 2020-07-23 stsp
4219 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4220 39449a05 2020-07-23 stsp if (outlen == -1) {
4221 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4222 39449a05 2020-07-23 stsp goto done;
4223 39449a05 2020-07-23 stsp }
4224 39449a05 2020-07-23 stsp
4225 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4226 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4227 39449a05 2020-07-23 stsp goto done;
4228 39449a05 2020-07-23 stsp }
4229 39449a05 2020-07-23 stsp done:
4230 39449a05 2020-07-23 stsp if (err) {
4231 39449a05 2020-07-23 stsp close(*fd);
4232 39449a05 2020-07-23 stsp *fd = -1;
4233 39449a05 2020-07-23 stsp }
4234 39449a05 2020-07-23 stsp return err;
4235 39449a05 2020-07-23 stsp }
4236 b72f483a 2019-02-05 stsp
4237 4ed7e80c 2018-05-20 stsp static const struct got_error *
4238 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4239 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4240 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4241 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4242 b72f483a 2019-02-05 stsp {
4243 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4244 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4245 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4246 12463d8b 2019-12-13 stsp int fd = -1;
4247 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4248 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4249 b72f483a 2019-02-05 stsp struct stat sb;
4250 b72f483a 2019-02-05 stsp
4251 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4252 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4253 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4254 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4255 98eaaa12 2019-08-03 stsp return NULL;
4256 98eaaa12 2019-08-03 stsp } else {
4257 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4258 98eaaa12 2019-08-03 stsp return NULL;
4259 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4260 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4261 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4262 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4263 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4264 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4265 98eaaa12 2019-08-03 stsp return NULL;
4266 98eaaa12 2019-08-03 stsp }
4267 3fc0c068 2019-02-10 stsp
4268 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4269 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4270 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4271 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4272 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4273 3fc0c068 2019-02-10 stsp }
4274 b72f483a 2019-02-05 stsp
4275 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4276 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4277 98eaaa12 2019-08-03 stsp switch (staged_status) {
4278 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4279 98eaaa12 2019-08-03 stsp label1 = path;
4280 98eaaa12 2019-08-03 stsp label2 = path;
4281 98eaaa12 2019-08-03 stsp break;
4282 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4283 98eaaa12 2019-08-03 stsp label2 = path;
4284 98eaaa12 2019-08-03 stsp break;
4285 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4286 98eaaa12 2019-08-03 stsp label1 = path;
4287 98eaaa12 2019-08-03 stsp break;
4288 98eaaa12 2019-08-03 stsp default:
4289 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4290 98eaaa12 2019-08-03 stsp }
4291 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4292 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4293 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4294 98eaaa12 2019-08-03 stsp }
4295 98eaaa12 2019-08-03 stsp
4296 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4297 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4298 4ce46740 2019-08-08 stsp char *id_str;
4299 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4300 408b4ebc 2019-08-03 stsp 8192);
4301 4ce46740 2019-08-08 stsp if (err)
4302 4ce46740 2019-08-08 stsp goto done;
4303 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4304 4ce46740 2019-08-08 stsp if (err)
4305 4ce46740 2019-08-08 stsp goto done;
4306 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4307 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4308 4ce46740 2019-08-08 stsp free(id_str);
4309 4ce46740 2019-08-08 stsp goto done;
4310 4ce46740 2019-08-08 stsp }
4311 4ce46740 2019-08-08 stsp free(id_str);
4312 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4313 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4314 4ce46740 2019-08-08 stsp if (err)
4315 4ce46740 2019-08-08 stsp goto done;
4316 4ce46740 2019-08-08 stsp }
4317 d00136be 2019-03-26 stsp
4318 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4319 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4320 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4321 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4322 2ec1f75b 2019-03-26 stsp goto done;
4323 2ec1f75b 2019-03-26 stsp }
4324 b72f483a 2019-02-05 stsp
4325 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4326 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4327 12463d8b 2019-12-13 stsp if (fd == -1) {
4328 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4329 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4330 39449a05 2020-07-23 stsp abspath);
4331 39449a05 2020-07-23 stsp goto done;
4332 39449a05 2020-07-23 stsp }
4333 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4334 39449a05 2020-07-23 stsp de_name, abspath);
4335 39449a05 2020-07-23 stsp if (err)
4336 39449a05 2020-07-23 stsp goto done;
4337 12463d8b 2019-12-13 stsp }
4338 12463d8b 2019-12-13 stsp } else {
4339 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4340 12463d8b 2019-12-13 stsp if (fd == -1) {
4341 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4342 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4343 39449a05 2020-07-23 stsp abspath);
4344 39449a05 2020-07-23 stsp goto done;
4345 39449a05 2020-07-23 stsp }
4346 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4347 39449a05 2020-07-23 stsp de_name, abspath);
4348 39449a05 2020-07-23 stsp if (err)
4349 39449a05 2020-07-23 stsp goto done;
4350 12463d8b 2019-12-13 stsp }
4351 12463d8b 2019-12-13 stsp }
4352 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4353 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4354 2ec1f75b 2019-03-26 stsp goto done;
4355 2ec1f75b 2019-03-26 stsp }
4356 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4357 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4358 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4359 2ec1f75b 2019-03-26 stsp goto done;
4360 2ec1f75b 2019-03-26 stsp }
4361 12463d8b 2019-12-13 stsp fd = -1;
4362 2ec1f75b 2019-03-26 stsp } else
4363 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4364 b72f483a 2019-02-05 stsp
4365 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4366 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4367 b72f483a 2019-02-05 stsp done:
4368 b72f483a 2019-02-05 stsp if (blob1)
4369 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4370 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4371 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4372 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4373 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4374 b72f483a 2019-02-05 stsp free(abspath);
4375 927df6b7 2019-02-10 stsp return err;
4376 927df6b7 2019-02-10 stsp }
4377 927df6b7 2019-02-10 stsp
4378 927df6b7 2019-02-10 stsp static const struct got_error *
4379 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4380 b00d56cd 2018-04-01 stsp {
4381 b00d56cd 2018-04-01 stsp const struct got_error *error;
4382 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4383 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4384 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4385 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4386 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4387 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4388 15a94983 2018-12-23 stsp int type1, type2;
4389 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4390 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4391 c0cc5c62 2018-10-18 stsp const char *errstr;
4392 927df6b7 2019-02-10 stsp char *path = NULL;
4393 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4394 b00d56cd 2018-04-01 stsp
4395 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4396 84de9106 2020-12-26 stsp
4397 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4398 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4399 25eccc22 2019-01-04 stsp NULL) == -1)
4400 b00d56cd 2018-04-01 stsp err(1, "pledge");
4401 b00d56cd 2018-04-01 stsp #endif
4402 b00d56cd 2018-04-01 stsp
4403 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4404 b00d56cd 2018-04-01 stsp switch (ch) {
4405 64453f7e 2020-11-21 stsp case 'a':
4406 64453f7e 2020-11-21 stsp force_text_diff = 1;
4407 64453f7e 2020-11-21 stsp break;
4408 c0cc5c62 2018-10-18 stsp case 'C':
4409 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4410 dfcab68b 2019-11-29 kn &errstr);
4411 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4412 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4413 c0cc5c62 2018-10-18 stsp break;
4414 b72f483a 2019-02-05 stsp case 'r':
4415 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4416 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4417 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4418 9ba1d308 2019-10-21 stsp optarg);
4419 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4420 b72f483a 2019-02-05 stsp break;
4421 98eaaa12 2019-08-03 stsp case 's':
4422 98eaaa12 2019-08-03 stsp diff_staged = 1;
4423 63035f9f 2019-10-06 stsp break;
4424 63035f9f 2019-10-06 stsp case 'w':
4425 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4426 98eaaa12 2019-08-03 stsp break;
4427 b00d56cd 2018-04-01 stsp default:
4428 2deda0b9 2019-03-07 stsp usage_diff();
4429 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4430 b00d56cd 2018-04-01 stsp }
4431 b00d56cd 2018-04-01 stsp }
4432 b00d56cd 2018-04-01 stsp
4433 b00d56cd 2018-04-01 stsp argc -= optind;
4434 b00d56cd 2018-04-01 stsp argv += optind;
4435 b00d56cd 2018-04-01 stsp
4436 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4437 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4438 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4439 b72f483a 2019-02-05 stsp goto done;
4440 b72f483a 2019-02-05 stsp }
4441 927df6b7 2019-02-10 stsp if (argc <= 1) {
4442 b72f483a 2019-02-05 stsp if (repo_path)
4443 b72f483a 2019-02-05 stsp errx(1,
4444 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4445 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4446 fa51e947 2020-03-27 stsp if (error) {
4447 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4448 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4449 fa51e947 2020-03-27 stsp cwd);
4450 1f03b8da 2020-03-20 stsp goto done;
4451 fa51e947 2020-03-27 stsp }
4452 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4453 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4454 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4455 927df6b7 2019-02-10 stsp goto done;
4456 927df6b7 2019-02-10 stsp }
4457 927df6b7 2019-02-10 stsp if (argc == 1) {
4458 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4459 cbd1af7a 2019-03-18 stsp argv[0]);
4460 927df6b7 2019-02-10 stsp if (error)
4461 927df6b7 2019-02-10 stsp goto done;
4462 927df6b7 2019-02-10 stsp } else {
4463 927df6b7 2019-02-10 stsp path = strdup("");
4464 927df6b7 2019-02-10 stsp if (path == NULL) {
4465 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4466 927df6b7 2019-02-10 stsp goto done;
4467 927df6b7 2019-02-10 stsp }
4468 927df6b7 2019-02-10 stsp }
4469 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4470 98eaaa12 2019-08-03 stsp if (diff_staged)
4471 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4472 98eaaa12 2019-08-03 stsp "objects in repository");
4473 15a94983 2018-12-23 stsp id_str1 = argv[0];
4474 15a94983 2018-12-23 stsp id_str2 = argv[1];
4475 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4476 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4477 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4478 30db809c 2019-06-05 stsp goto done;
4479 1a1242a9 2021-04-01 kn repo_path = strdup(worktree ?
4480 1a1242a9 2021-04-01 kn got_worktree_get_repo_path(worktree) : cwd);
4481 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4482 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4483 1a1242a9 2021-04-01 kn goto done;
4484 30db809c 2019-06-05 stsp }
4485 30db809c 2019-06-05 stsp }
4486 b00d56cd 2018-04-01 stsp } else
4487 b00d56cd 2018-04-01 stsp usage_diff();
4488 b00d56cd 2018-04-01 stsp
4489 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4490 76089277 2018-04-01 stsp free(repo_path);
4491 b00d56cd 2018-04-01 stsp if (error != NULL)
4492 b00d56cd 2018-04-01 stsp goto done;
4493 b00d56cd 2018-04-01 stsp
4494 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4495 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4496 c02c541e 2019-03-29 stsp if (error)
4497 c02c541e 2019-03-29 stsp goto done;
4498 c02c541e 2019-03-29 stsp
4499 30db809c 2019-06-05 stsp if (argc <= 1) {
4500 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4501 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4502 b72f483a 2019-02-05 stsp char *id_str;
4503 72ea6654 2019-07-27 stsp
4504 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4505 72ea6654 2019-07-27 stsp
4506 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4507 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4508 b72f483a 2019-02-05 stsp if (error)
4509 b72f483a 2019-02-05 stsp goto done;
4510 b72f483a 2019-02-05 stsp arg.repo = repo;
4511 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4512 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4513 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4514 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4515 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4516 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4517 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4518 b72f483a 2019-02-05 stsp
4519 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4520 72ea6654 2019-07-27 stsp if (error)
4521 72ea6654 2019-07-27 stsp goto done;
4522 72ea6654 2019-07-27 stsp
4523 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4524 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4525 b72f483a 2019-02-05 stsp free(id_str);
4526 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4527 b72f483a 2019-02-05 stsp goto done;
4528 b72f483a 2019-02-05 stsp }
4529 84de9106 2020-12-26 stsp
4530 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4531 84de9106 2020-12-26 stsp if (error)
4532 84de9106 2020-12-26 stsp return error;
4533 b72f483a 2019-02-05 stsp
4534 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4535 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4536 0adb7196 2019-08-11 stsp if (error)
4537 0adb7196 2019-08-11 stsp goto done;
4538 b00d56cd 2018-04-01 stsp
4539 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4540 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4541 0adb7196 2019-08-11 stsp if (error)
4542 0adb7196 2019-08-11 stsp goto done;
4543 b00d56cd 2018-04-01 stsp
4544 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4545 15a94983 2018-12-23 stsp if (error)
4546 15a94983 2018-12-23 stsp goto done;
4547 15a94983 2018-12-23 stsp
4548 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4549 15a94983 2018-12-23 stsp if (error)
4550 15a94983 2018-12-23 stsp goto done;
4551 15a94983 2018-12-23 stsp
4552 15a94983 2018-12-23 stsp if (type1 != type2) {
4553 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4554 b00d56cd 2018-04-01 stsp goto done;
4555 b00d56cd 2018-04-01 stsp }
4556 b00d56cd 2018-04-01 stsp
4557 15a94983 2018-12-23 stsp switch (type1) {
4558 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4559 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4560 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4561 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4562 b00d56cd 2018-04-01 stsp break;
4563 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4564 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4565 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4566 64453f7e 2020-11-21 stsp repo, stdout);
4567 b00d56cd 2018-04-01 stsp break;
4568 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4569 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4570 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4571 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4572 64453f7e 2020-11-21 stsp stdout);
4573 b00d56cd 2018-04-01 stsp break;
4574 b00d56cd 2018-04-01 stsp default:
4575 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4576 b00d56cd 2018-04-01 stsp }
4577 b00d56cd 2018-04-01 stsp done:
4578 5e70831e 2019-05-28 stsp free(label1);
4579 5e70831e 2019-05-28 stsp free(label2);
4580 15a94983 2018-12-23 stsp free(id1);
4581 15a94983 2018-12-23 stsp free(id2);
4582 927df6b7 2019-02-10 stsp free(path);
4583 b72f483a 2019-02-05 stsp if (worktree)
4584 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4585 ad242220 2018-09-08 stsp if (repo) {
4586 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4587 ad242220 2018-09-08 stsp if (error == NULL)
4588 1d0f4054 2021-06-17 stsp error = close_err;
4589 ad242220 2018-09-08 stsp }
4590 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4591 b00d56cd 2018-04-01 stsp return error;
4592 404c43c4 2018-06-21 stsp }
4593 404c43c4 2018-06-21 stsp
4594 404c43c4 2018-06-21 stsp __dead static void
4595 404c43c4 2018-06-21 stsp usage_blame(void)
4596 404c43c4 2018-06-21 stsp {
4597 9270e621 2019-02-05 stsp fprintf(stderr,
4598 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4599 404c43c4 2018-06-21 stsp getprogname());
4600 404c43c4 2018-06-21 stsp exit(1);
4601 b00d56cd 2018-04-01 stsp }
4602 b00d56cd 2018-04-01 stsp
4603 28315671 2019-08-14 stsp struct blame_line {
4604 28315671 2019-08-14 stsp int annotated;
4605 28315671 2019-08-14 stsp char *id_str;
4606 82f6abb8 2019-08-14 stsp char *committer;
4607 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4608 28315671 2019-08-14 stsp };
4609 28315671 2019-08-14 stsp
4610 28315671 2019-08-14 stsp struct blame_cb_args {
4611 28315671 2019-08-14 stsp struct blame_line *lines;
4612 28315671 2019-08-14 stsp int nlines;
4613 7ef28ff8 2019-08-14 stsp int nlines_prec;
4614 28315671 2019-08-14 stsp int lineno_cur;
4615 28315671 2019-08-14 stsp off_t *line_offsets;
4616 28315671 2019-08-14 stsp FILE *f;
4617 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4618 28315671 2019-08-14 stsp };
4619 28315671 2019-08-14 stsp
4620 404c43c4 2018-06-21 stsp static const struct got_error *
4621 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4622 28315671 2019-08-14 stsp {
4623 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4624 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4625 28315671 2019-08-14 stsp struct blame_line *bline;
4626 82f6abb8 2019-08-14 stsp char *line = NULL;
4627 28315671 2019-08-14 stsp size_t linesize = 0;
4628 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4629 28315671 2019-08-14 stsp off_t offset;
4630 bcb49d15 2019-08-14 stsp struct tm tm;
4631 bcb49d15 2019-08-14 stsp time_t committer_time;
4632 28315671 2019-08-14 stsp
4633 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4634 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4635 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4636 28315671 2019-08-14 stsp
4637 28315671 2019-08-14 stsp if (sigint_received)
4638 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4639 28315671 2019-08-14 stsp
4640 2839f8b9 2019-08-18 stsp if (lineno == -1)
4641 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4642 2839f8b9 2019-08-18 stsp
4643 28315671 2019-08-14 stsp /* Annotate this line. */
4644 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4645 28315671 2019-08-14 stsp if (bline->annotated)
4646 28315671 2019-08-14 stsp return NULL;
4647 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4648 28315671 2019-08-14 stsp if (err)
4649 28315671 2019-08-14 stsp return err;
4650 82f6abb8 2019-08-14 stsp
4651 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4652 82f6abb8 2019-08-14 stsp if (err)
4653 82f6abb8 2019-08-14 stsp goto done;
4654 82f6abb8 2019-08-14 stsp
4655 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4656 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4657 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4658 bcb49d15 2019-08-14 stsp goto done;
4659 bcb49d15 2019-08-14 stsp }
4660 bcb49d15 2019-08-14 stsp
4661 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4662 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4663 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4664 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4665 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4666 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4667 82f6abb8 2019-08-14 stsp goto done;
4668 82f6abb8 2019-08-14 stsp }
4669 28315671 2019-08-14 stsp bline->annotated = 1;
4670 28315671 2019-08-14 stsp
4671 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4672 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4673 28315671 2019-08-14 stsp if (!bline->annotated)
4674 82f6abb8 2019-08-14 stsp goto done;
4675 28315671 2019-08-14 stsp
4676 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4677 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4678 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4679 82f6abb8 2019-08-14 stsp goto done;
4680 82f6abb8 2019-08-14 stsp }
4681 28315671 2019-08-14 stsp
4682 28315671 2019-08-14 stsp while (bline->annotated) {
4683 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4684 82f6abb8 2019-08-14 stsp size_t len;
4685 82f6abb8 2019-08-14 stsp
4686 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4687 28315671 2019-08-14 stsp if (ferror(a->f))
4688 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4689 28315671 2019-08-14 stsp break;
4690 28315671 2019-08-14 stsp }
4691 82f6abb8 2019-08-14 stsp
4692 82f6abb8 2019-08-14 stsp committer = bline->committer;
4693 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4694 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4695 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4696 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4697 82f6abb8 2019-08-14 stsp if (at)
4698 82f6abb8 2019-08-14 stsp *at = '\0';
4699 82f6abb8 2019-08-14 stsp len = strlen(committer);
4700 82f6abb8 2019-08-14 stsp if (len >= 9)
4701 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4702 28315671 2019-08-14 stsp
4703 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4704 28315671 2019-08-14 stsp if (nl)
4705 28315671 2019-08-14 stsp *nl = '\0';
4706 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4707 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4708 28315671 2019-08-14 stsp
4709 28315671 2019-08-14 stsp a->lineno_cur++;
4710 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4711 28315671 2019-08-14 stsp }
4712 82f6abb8 2019-08-14 stsp done:
4713 82f6abb8 2019-08-14 stsp if (commit)
4714 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4715 28315671 2019-08-14 stsp free(line);
4716 28315671 2019-08-14 stsp return err;
4717 28315671 2019-08-14 stsp }
4718 28315671 2019-08-14 stsp
4719 28315671 2019-08-14 stsp static const struct got_error *
4720 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4721 404c43c4 2018-06-21 stsp {
4722 404c43c4 2018-06-21 stsp const struct got_error *error;
4723 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4724 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4725 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4726 0587e10c 2020-07-23 stsp char *link_target = NULL;
4727 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4728 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4729 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4730 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4731 28315671 2019-08-14 stsp struct blame_cb_args bca;
4732 28315671 2019-08-14 stsp int ch, obj_type, i;
4733 be659d10 2020-11-18 stsp off_t filesize;
4734 90f3c347 2019-08-19 stsp
4735 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4736 404c43c4 2018-06-21 stsp
4737 404c43c4 2018-06-21 stsp #ifndef PROFILE
4738 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4739 36e2fb66 2019-01-04 stsp NULL) == -1)
4740 404c43c4 2018-06-21 stsp err(1, "pledge");
4741 404c43c4 2018-06-21 stsp #endif
4742 404c43c4 2018-06-21 stsp
4743 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4744 404c43c4 2018-06-21 stsp switch (ch) {
4745 404c43c4 2018-06-21 stsp case 'c':
4746 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4747 404c43c4 2018-06-21 stsp break;
4748 66bea077 2018-08-02 stsp case 'r':
4749 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4750 66bea077 2018-08-02 stsp if (repo_path == NULL)
4751 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4752 9ba1d308 2019-10-21 stsp optarg);
4753 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4754 66bea077 2018-08-02 stsp break;
4755 404c43c4 2018-06-21 stsp default:
4756 2deda0b9 2019-03-07 stsp usage_blame();
4757 404c43c4 2018-06-21 stsp /* NOTREACHED */
4758 404c43c4 2018-06-21 stsp }
4759 404c43c4 2018-06-21 stsp }
4760 404c43c4 2018-06-21 stsp
4761 404c43c4 2018-06-21 stsp argc -= optind;
4762 404c43c4 2018-06-21 stsp argv += optind;
4763 404c43c4 2018-06-21 stsp
4764 a39318fd 2018-08-02 stsp if (argc == 1)
4765 404c43c4 2018-06-21 stsp path = argv[0];
4766 a39318fd 2018-08-02 stsp else
4767 404c43c4 2018-06-21 stsp usage_blame();
4768 404c43c4 2018-06-21 stsp
4769 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4770 66bea077 2018-08-02 stsp if (cwd == NULL) {
4771 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4772 66bea077 2018-08-02 stsp goto done;
4773 66bea077 2018-08-02 stsp }
4774 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4775 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4776 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4777 66bea077 2018-08-02 stsp goto done;
4778 0c06baac 2019-02-05 stsp else
4779 0c06baac 2019-02-05 stsp error = NULL;
4780 0c06baac 2019-02-05 stsp if (worktree) {
4781 0c06baac 2019-02-05 stsp repo_path =
4782 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4783 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4784 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4785 1f03b8da 2020-03-20 stsp if (error)
4786 1f03b8da 2020-03-20 stsp goto done;
4787 1f03b8da 2020-03-20 stsp }
4788 0c06baac 2019-02-05 stsp } else {
4789 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4790 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4791 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4792 0c06baac 2019-02-05 stsp goto done;
4793 0c06baac 2019-02-05 stsp }
4794 66bea077 2018-08-02 stsp }
4795 66bea077 2018-08-02 stsp }
4796 36e2fb66 2019-01-04 stsp
4797 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4798 c02c541e 2019-03-29 stsp if (error != NULL)
4799 404c43c4 2018-06-21 stsp goto done;
4800 404c43c4 2018-06-21 stsp
4801 0c06baac 2019-02-05 stsp if (worktree) {
4802 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4803 01740607 2020-11-04 stsp char *p;
4804 01740607 2020-11-04 stsp
4805 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4806 01740607 2020-11-04 stsp if (error)
4807 01740607 2020-11-04 stsp goto done;
4808 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4809 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4810 01740607 2020-11-04 stsp p) == -1) {
4811 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4812 01740607 2020-11-04 stsp free(p);
4813 0c06baac 2019-02-05 stsp goto done;
4814 0c06baac 2019-02-05 stsp }
4815 0c06baac 2019-02-05 stsp free(p);
4816 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4817 0c06baac 2019-02-05 stsp } else {
4818 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4819 01740607 2020-11-04 stsp if (error)
4820 01740607 2020-11-04 stsp goto done;
4821 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4822 0c06baac 2019-02-05 stsp }
4823 0c06baac 2019-02-05 stsp if (error)
4824 66bea077 2018-08-02 stsp goto done;
4825 66bea077 2018-08-02 stsp
4826 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4827 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4828 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4829 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4830 404c43c4 2018-06-21 stsp if (error != NULL)
4831 66bea077 2018-08-02 stsp goto done;
4832 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4833 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4834 404c43c4 2018-06-21 stsp if (error != NULL)
4835 66bea077 2018-08-02 stsp goto done;
4836 404c43c4 2018-06-21 stsp } else {
4837 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4838 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4839 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4840 84de9106 2020-12-26 stsp NULL);
4841 84de9106 2020-12-26 stsp if (error)
4842 84de9106 2020-12-26 stsp goto done;
4843 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4844 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4845 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4846 30837e32 2019-07-25 stsp if (error)
4847 66bea077 2018-08-02 stsp goto done;
4848 404c43c4 2018-06-21 stsp }
4849 404c43c4 2018-06-21 stsp
4850 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4851 0587e10c 2020-07-23 stsp commit_id, repo);
4852 0587e10c 2020-07-23 stsp if (error)
4853 0587e10c 2020-07-23 stsp goto done;
4854 0587e10c 2020-07-23 stsp
4855 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4856 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4857 28315671 2019-08-14 stsp if (error)
4858 28315671 2019-08-14 stsp goto done;
4859 28315671 2019-08-14 stsp
4860 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4861 28315671 2019-08-14 stsp if (error)
4862 28315671 2019-08-14 stsp goto done;
4863 28315671 2019-08-14 stsp
4864 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4865 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4866 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4867 28315671 2019-08-14 stsp goto done;
4868 28315671 2019-08-14 stsp }
4869 28315671 2019-08-14 stsp
4870 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4871 28315671 2019-08-14 stsp if (error)
4872 28315671 2019-08-14 stsp goto done;
4873 28315671 2019-08-14 stsp bca.f = got_opentemp();
4874 28315671 2019-08-14 stsp if (bca.f == NULL) {
4875 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4876 28315671 2019-08-14 stsp goto done;
4877 28315671 2019-08-14 stsp }
4878 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4879 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4880 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4881 28315671 2019-08-14 stsp goto done;
4882 28315671 2019-08-14 stsp
4883 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4884 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4885 b02560ec 2019-08-19 stsp bca.nlines--;
4886 b02560ec 2019-08-19 stsp
4887 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4888 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4889 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4890 28315671 2019-08-14 stsp goto done;
4891 28315671 2019-08-14 stsp }
4892 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4893 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4894 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4895 7ef28ff8 2019-08-14 stsp while (i > 0) {
4896 7ef28ff8 2019-08-14 stsp i /= 10;
4897 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4898 7ef28ff8 2019-08-14 stsp }
4899 82f6abb8 2019-08-14 stsp bca.repo = repo;
4900 7ef28ff8 2019-08-14 stsp
4901 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4902 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4903 404c43c4 2018-06-21 stsp done:
4904 66bea077 2018-08-02 stsp free(in_repo_path);
4905 0587e10c 2020-07-23 stsp free(link_target);
4906 66bea077 2018-08-02 stsp free(repo_path);
4907 66bea077 2018-08-02 stsp free(cwd);
4908 404c43c4 2018-06-21 stsp free(commit_id);
4909 28315671 2019-08-14 stsp free(obj_id);
4910 28315671 2019-08-14 stsp if (blob)
4911 28315671 2019-08-14 stsp got_object_blob_close(blob);
4912 0c06baac 2019-02-05 stsp if (worktree)
4913 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4914 ad242220 2018-09-08 stsp if (repo) {
4915 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4916 ad242220 2018-09-08 stsp if (error == NULL)
4917 1d0f4054 2021-06-17 stsp error = close_err;
4918 ad242220 2018-09-08 stsp }
4919 3affba96 2019-09-22 stsp if (bca.lines) {
4920 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4921 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4922 3affba96 2019-09-22 stsp free(bline->id_str);
4923 3affba96 2019-09-22 stsp free(bline->committer);
4924 3affba96 2019-09-22 stsp }
4925 3affba96 2019-09-22 stsp free(bca.lines);
4926 28315671 2019-08-14 stsp }
4927 28315671 2019-08-14 stsp free(bca.line_offsets);
4928 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4929 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4930 404c43c4 2018-06-21 stsp return error;
4931 5de5890b 2018-10-18 stsp }
4932 5de5890b 2018-10-18 stsp
4933 5de5890b 2018-10-18 stsp __dead static void
4934 5de5890b 2018-10-18 stsp usage_tree(void)
4935 5de5890b 2018-10-18 stsp {
4936 c1669e2e 2019-01-09 stsp fprintf(stderr,
4937 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4938 5de5890b 2018-10-18 stsp getprogname());
4939 5de5890b 2018-10-18 stsp exit(1);
4940 5de5890b 2018-10-18 stsp }
4941 5de5890b 2018-10-18 stsp
4942 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4943 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4944 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4945 c1669e2e 2019-01-09 stsp {
4946 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4947 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4948 848d6979 2019-08-12 stsp const char *modestr = "";
4949 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4950 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4951 5de5890b 2018-10-18 stsp
4952 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4953 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4954 c1669e2e 2019-01-09 stsp path++;
4955 c1669e2e 2019-01-09 stsp
4956 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4957 63c5ca5d 2019-08-24 stsp modestr = "$";
4958 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4959 0d6c6ee3 2020-05-20 stsp int i;
4960 0d6c6ee3 2020-05-20 stsp
4961 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4962 0d6c6ee3 2020-05-20 stsp if (err)
4963 0d6c6ee3 2020-05-20 stsp return err;
4964 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4965 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4966 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4967 0d6c6ee3 2020-05-20 stsp }
4968 0d6c6ee3 2020-05-20 stsp
4969 848d6979 2019-08-12 stsp modestr = "@";
4970 0d6c6ee3 2020-05-20 stsp }
4971 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4972 848d6979 2019-08-12 stsp modestr = "/";
4973 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4974 848d6979 2019-08-12 stsp modestr = "*";
4975 848d6979 2019-08-12 stsp
4976 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4977 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4978 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4979 0d6c6ee3 2020-05-20 stsp
4980 0d6c6ee3 2020-05-20 stsp free(link_target);
4981 0d6c6ee3 2020-05-20 stsp return NULL;
4982 c1669e2e 2019-01-09 stsp }
4983 c1669e2e 2019-01-09 stsp
4984 5de5890b 2018-10-18 stsp static const struct got_error *
4985 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4986 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4987 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4988 5de5890b 2018-10-18 stsp {
4989 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4990 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4991 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4992 56e0773d 2019-11-28 stsp int nentries, i;
4993 5de5890b 2018-10-18 stsp
4994 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4995 5de5890b 2018-10-18 stsp if (err)
4996 5de5890b 2018-10-18 stsp goto done;
4997 5de5890b 2018-10-18 stsp
4998 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4999 5de5890b 2018-10-18 stsp if (err)
5000 5de5890b 2018-10-18 stsp goto done;
5001 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
5002 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
5003 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5004 5de5890b 2018-10-18 stsp char *id = NULL;
5005 84453469 2018-11-11 stsp
5006 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5007 84453469 2018-11-11 stsp break;
5008 84453469 2018-11-11 stsp
5009 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
5010 5de5890b 2018-10-18 stsp if (show_ids) {
5011 5de5890b 2018-10-18 stsp char *id_str;
5012 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5013 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5014 5de5890b 2018-10-18 stsp if (err)
5015 5de5890b 2018-10-18 stsp goto done;
5016 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
5017 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5018 5de5890b 2018-10-18 stsp free(id_str);
5019 5de5890b 2018-10-18 stsp goto done;
5020 5de5890b 2018-10-18 stsp }
5021 5de5890b 2018-10-18 stsp free(id_str);
5022 5de5890b 2018-10-18 stsp }
5023 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5024 5de5890b 2018-10-18 stsp free(id);
5025 0d6c6ee3 2020-05-20 stsp if (err)
5026 0d6c6ee3 2020-05-20 stsp goto done;
5027 c1669e2e 2019-01-09 stsp
5028 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5029 c1669e2e 2019-01-09 stsp char *child_path;
5030 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
5031 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
5032 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
5033 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5034 c1669e2e 2019-01-09 stsp goto done;
5035 c1669e2e 2019-01-09 stsp }
5036 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
5037 c1669e2e 2019-01-09 stsp root_path, repo);
5038 c1669e2e 2019-01-09 stsp free(child_path);
5039 c1669e2e 2019-01-09 stsp if (err)
5040 c1669e2e 2019-01-09 stsp goto done;
5041 c1669e2e 2019-01-09 stsp }
5042 5de5890b 2018-10-18 stsp }
5043 5de5890b 2018-10-18 stsp done:
5044 5de5890b 2018-10-18 stsp if (tree)
5045 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
5046 5de5890b 2018-10-18 stsp free(tree_id);
5047 5de5890b 2018-10-18 stsp return err;
5048 404c43c4 2018-06-21 stsp }
5049 404c43c4 2018-06-21 stsp
5050 5de5890b 2018-10-18 stsp static const struct got_error *
5051 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5052 5de5890b 2018-10-18 stsp {
5053 5de5890b 2018-10-18 stsp const struct got_error *error;
5054 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
5055 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
5056 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
5057 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5058 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
5059 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
5060 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
5061 5de5890b 2018-10-18 stsp int ch;
5062 5de5890b 2018-10-18 stsp
5063 5de5890b 2018-10-18 stsp #ifndef PROFILE
5064 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5065 0f8d269b 2019-01-04 stsp NULL) == -1)
5066 5de5890b 2018-10-18 stsp err(1, "pledge");
5067 5de5890b 2018-10-18 stsp #endif
5068 5de5890b 2018-10-18 stsp
5069 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5070 5de5890b 2018-10-18 stsp switch (ch) {
5071 5de5890b 2018-10-18 stsp case 'c':
5072 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5073 5de5890b 2018-10-18 stsp break;
5074 5de5890b 2018-10-18 stsp case 'r':
5075 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
5076 5de5890b 2018-10-18 stsp if (repo_path == NULL)
5077 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5078 9ba1d308 2019-10-21 stsp optarg);
5079 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5080 5de5890b 2018-10-18 stsp break;
5081 5de5890b 2018-10-18 stsp case 'i':
5082 5de5890b 2018-10-18 stsp show_ids = 1;
5083 5de5890b 2018-10-18 stsp break;
5084 c1669e2e 2019-01-09 stsp case 'R':
5085 c1669e2e 2019-01-09 stsp recurse = 1;
5086 c1669e2e 2019-01-09 stsp break;
5087 5de5890b 2018-10-18 stsp default:
5088 2deda0b9 2019-03-07 stsp usage_tree();
5089 5de5890b 2018-10-18 stsp /* NOTREACHED */
5090 5de5890b 2018-10-18 stsp }
5091 5de5890b 2018-10-18 stsp }
5092 5de5890b 2018-10-18 stsp
5093 5de5890b 2018-10-18 stsp argc -= optind;
5094 5de5890b 2018-10-18 stsp argv += optind;
5095 5de5890b 2018-10-18 stsp
5096 5de5890b 2018-10-18 stsp if (argc == 1)
5097 5de5890b 2018-10-18 stsp path = argv[0];
5098 5de5890b 2018-10-18 stsp else if (argc > 1)
5099 5de5890b 2018-10-18 stsp usage_tree();
5100 5de5890b 2018-10-18 stsp else
5101 7a2c19d6 2019-02-05 stsp path = NULL;
5102 7a2c19d6 2019-02-05 stsp
5103 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5104 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5105 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5106 9bf7a39b 2019-02-05 stsp goto done;
5107 9bf7a39b 2019-02-05 stsp }
5108 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5109 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5110 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5111 7a2c19d6 2019-02-05 stsp goto done;
5112 7a2c19d6 2019-02-05 stsp else
5113 7a2c19d6 2019-02-05 stsp error = NULL;
5114 7a2c19d6 2019-02-05 stsp if (worktree) {
5115 7a2c19d6 2019-02-05 stsp repo_path =
5116 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5117 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5118 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5119 7a2c19d6 2019-02-05 stsp if (error)
5120 7a2c19d6 2019-02-05 stsp goto done;
5121 7a2c19d6 2019-02-05 stsp } else {
5122 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5123 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5124 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5125 7a2c19d6 2019-02-05 stsp goto done;
5126 7a2c19d6 2019-02-05 stsp }
5127 5de5890b 2018-10-18 stsp }
5128 5de5890b 2018-10-18 stsp }
5129 5de5890b 2018-10-18 stsp
5130 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5131 5de5890b 2018-10-18 stsp if (error != NULL)
5132 c02c541e 2019-03-29 stsp goto done;
5133 c02c541e 2019-03-29 stsp
5134 4fedbf4c 2020-11-07 stsp if (worktree) {
5135 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5136 4fedbf4c 2020-11-07 stsp char *p;
5137 5de5890b 2018-10-18 stsp
5138 4fedbf4c 2020-11-07 stsp if (path == NULL)
5139 4fedbf4c 2020-11-07 stsp path = "";
5140 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5141 4fedbf4c 2020-11-07 stsp if (error)
5142 4fedbf4c 2020-11-07 stsp goto done;
5143 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5144 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5145 4fedbf4c 2020-11-07 stsp p) == -1) {
5146 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5147 9bf7a39b 2019-02-05 stsp free(p);
5148 4fedbf4c 2020-11-07 stsp goto done;
5149 4fedbf4c 2020-11-07 stsp }
5150 4fedbf4c 2020-11-07 stsp free(p);
5151 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5152 4fedbf4c 2020-11-07 stsp if (error)
5153 4fedbf4c 2020-11-07 stsp goto done;
5154 4fedbf4c 2020-11-07 stsp } else {
5155 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5156 4fedbf4c 2020-11-07 stsp if (error)
5157 4fedbf4c 2020-11-07 stsp goto done;
5158 4fedbf4c 2020-11-07 stsp if (path == NULL)
5159 9bf7a39b 2019-02-05 stsp path = "/";
5160 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5161 9bf7a39b 2019-02-05 stsp if (error != NULL)
5162 9bf7a39b 2019-02-05 stsp goto done;
5163 9bf7a39b 2019-02-05 stsp }
5164 5de5890b 2018-10-18 stsp
5165 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5166 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5167 4e0a20a4 2020-03-23 tracey if (worktree)
5168 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5169 4e0a20a4 2020-03-23 tracey else
5170 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5171 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5172 5de5890b 2018-10-18 stsp if (error != NULL)
5173 5de5890b 2018-10-18 stsp goto done;
5174 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5175 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5176 5de5890b 2018-10-18 stsp if (error != NULL)
5177 5de5890b 2018-10-18 stsp goto done;
5178 5de5890b 2018-10-18 stsp } else {
5179 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5180 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5181 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5182 84de9106 2020-12-26 stsp NULL);
5183 84de9106 2020-12-26 stsp if (error)
5184 84de9106 2020-12-26 stsp goto done;
5185 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5186 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5187 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5188 30837e32 2019-07-25 stsp if (error)
5189 5de5890b 2018-10-18 stsp goto done;
5190 5de5890b 2018-10-18 stsp }
5191 5de5890b 2018-10-18 stsp
5192 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5193 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5194 5de5890b 2018-10-18 stsp done:
5195 5de5890b 2018-10-18 stsp free(in_repo_path);
5196 5de5890b 2018-10-18 stsp free(repo_path);
5197 5de5890b 2018-10-18 stsp free(cwd);
5198 5de5890b 2018-10-18 stsp free(commit_id);
5199 7a2c19d6 2019-02-05 stsp if (worktree)
5200 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5201 5de5890b 2018-10-18 stsp if (repo) {
5202 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5203 5de5890b 2018-10-18 stsp if (error == NULL)
5204 1d0f4054 2021-06-17 stsp error = close_err;
5205 5de5890b 2018-10-18 stsp }
5206 5de5890b 2018-10-18 stsp return error;
5207 5de5890b 2018-10-18 stsp }
5208 5de5890b 2018-10-18 stsp
5209 6bad629b 2019-02-04 stsp __dead static void
5210 6bad629b 2019-02-04 stsp usage_status(void)
5211 6bad629b 2019-02-04 stsp {
5212 f6343036 2021-06-22 stsp fprintf(stderr, "usage: %s status [-I] [-s status-codes ] [path ...]\n",
5213 081470ac 2020-08-13 stsp getprogname());
5214 6bad629b 2019-02-04 stsp exit(1);
5215 6bad629b 2019-02-04 stsp }
5216 5c860e29 2018-03-12 stsp
5217 b72f483a 2019-02-05 stsp static const struct got_error *
5218 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5219 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5220 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5221 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5222 6bad629b 2019-02-04 stsp {
5223 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5224 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5225 081470ac 2020-08-13 stsp if (arg) {
5226 081470ac 2020-08-13 stsp char *status_codes = arg;
5227 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
5228 081470ac 2020-08-13 stsp int i;
5229 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5230 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
5231 081470ac 2020-08-13 stsp staged_status == status_codes[i])
5232 081470ac 2020-08-13 stsp break;
5233 081470ac 2020-08-13 stsp }
5234 081470ac 2020-08-13 stsp if (i == ncodes)
5235 081470ac 2020-08-13 stsp return NULL;
5236 081470ac 2020-08-13 stsp }
5237 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5238 b72f483a 2019-02-05 stsp return NULL;
5239 6bad629b 2019-02-04 stsp }
5240 5c860e29 2018-03-12 stsp
5241 6bad629b 2019-02-04 stsp static const struct got_error *
5242 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5243 6bad629b 2019-02-04 stsp {
5244 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5245 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5246 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5247 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
5248 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5249 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5250 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5251 5c860e29 2018-03-12 stsp
5252 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5253 72ea6654 2019-07-27 stsp
5254 f6343036 2021-06-22 stsp while ((ch = getopt(argc, argv, "Is:")) != -1) {
5255 6bad629b 2019-02-04 stsp switch (ch) {
5256 f6343036 2021-06-22 stsp case 'I':
5257 f6343036 2021-06-22 stsp no_ignores = 1;
5258 f6343036 2021-06-22 stsp break;
5259 081470ac 2020-08-13 stsp case 's':
5260 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5261 081470ac 2020-08-13 stsp switch (optarg[i]) {
5262 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5263 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5264 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5265 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5266 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5267 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5268 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5269 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5270 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5271 081470ac 2020-08-13 stsp break;
5272 081470ac 2020-08-13 stsp default:
5273 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5274 081470ac 2020-08-13 stsp optarg[i]);
5275 081470ac 2020-08-13 stsp }
5276 081470ac 2020-08-13 stsp }
5277 081470ac 2020-08-13 stsp status_codes = optarg;
5278 081470ac 2020-08-13 stsp break;
5279 5c860e29 2018-03-12 stsp default:
5280 2deda0b9 2019-03-07 stsp usage_status();
5281 6bad629b 2019-02-04 stsp /* NOTREACHED */
5282 5c860e29 2018-03-12 stsp }
5283 5c860e29 2018-03-12 stsp }
5284 5c860e29 2018-03-12 stsp
5285 6bad629b 2019-02-04 stsp argc -= optind;
5286 6bad629b 2019-02-04 stsp argv += optind;
5287 5c860e29 2018-03-12 stsp
5288 6bad629b 2019-02-04 stsp #ifndef PROFILE
5289 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5290 6bad629b 2019-02-04 stsp NULL) == -1)
5291 6bad629b 2019-02-04 stsp err(1, "pledge");
5292 f42b1b34 2018-03-12 stsp #endif
5293 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5294 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5295 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5296 927df6b7 2019-02-10 stsp goto done;
5297 927df6b7 2019-02-10 stsp }
5298 927df6b7 2019-02-10 stsp
5299 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5300 fa51e947 2020-03-27 stsp if (error) {
5301 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5302 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5303 a5edda0a 2019-07-27 stsp goto done;
5304 fa51e947 2020-03-27 stsp }
5305 6bad629b 2019-02-04 stsp
5306 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5307 c9956ddf 2019-09-08 stsp NULL);
5308 6bad629b 2019-02-04 stsp if (error != NULL)
5309 6bad629b 2019-02-04 stsp goto done;
5310 6bad629b 2019-02-04 stsp
5311 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5312 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5313 087fb88c 2019-08-04 stsp if (error)
5314 087fb88c 2019-08-04 stsp goto done;
5315 087fb88c 2019-08-04 stsp
5316 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5317 6bad629b 2019-02-04 stsp if (error)
5318 6bad629b 2019-02-04 stsp goto done;
5319 6bad629b 2019-02-04 stsp
5320 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5321 f6343036 2021-06-22 stsp print_status, status_codes, check_cancelled, NULL);
5322 6bad629b 2019-02-04 stsp done:
5323 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5324 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5325 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5326 927df6b7 2019-02-10 stsp free(cwd);
5327 d0eebce4 2019-03-11 stsp return error;
5328 d0eebce4 2019-03-11 stsp }
5329 d0eebce4 2019-03-11 stsp
5330 d0eebce4 2019-03-11 stsp __dead static void
5331 d0eebce4 2019-03-11 stsp usage_ref(void)
5332 d0eebce4 2019-03-11 stsp {
5333 d0eebce4 2019-03-11 stsp fprintf(stderr,
5334 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5335 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5336 d0eebce4 2019-03-11 stsp getprogname());
5337 d0eebce4 2019-03-11 stsp exit(1);
5338 d0eebce4 2019-03-11 stsp }
5339 d0eebce4 2019-03-11 stsp
5340 d0eebce4 2019-03-11 stsp static const struct got_error *
5341 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5342 d0eebce4 2019-03-11 stsp {
5343 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5344 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5345 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5346 d0eebce4 2019-03-11 stsp
5347 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5348 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5349 d0eebce4 2019-03-11 stsp if (err)
5350 d0eebce4 2019-03-11 stsp return err;
5351 d0eebce4 2019-03-11 stsp
5352 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5353 d0eebce4 2019-03-11 stsp char *refstr;
5354 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5355 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5356 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5357 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5358 d0eebce4 2019-03-11 stsp free(refstr);
5359 d0eebce4 2019-03-11 stsp }
5360 d0eebce4 2019-03-11 stsp
5361 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5362 d0eebce4 2019-03-11 stsp return NULL;
5363 d0eebce4 2019-03-11 stsp }
5364 d0eebce4 2019-03-11 stsp
5365 d0eebce4 2019-03-11 stsp static const struct got_error *
5366 161728eb 2021-07-24 stsp delete_ref_by_name(struct got_repository *repo, const char *refname)
5367 d0eebce4 2019-03-11 stsp {
5368 161728eb 2021-07-24 stsp const struct got_error *err;
5369 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5370 d0eebce4 2019-03-11 stsp
5371 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5372 d0eebce4 2019-03-11 stsp if (err)
5373 d0eebce4 2019-03-11 stsp return err;
5374 993f033b 2021-07-16 stsp
5375 161728eb 2021-07-24 stsp err = delete_ref(repo, ref);
5376 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5377 d0eebce4 2019-03-11 stsp return err;
5378 d0eebce4 2019-03-11 stsp }
5379 d0eebce4 2019-03-11 stsp
5380 d0eebce4 2019-03-11 stsp static const struct got_error *
5381 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5382 d0eebce4 2019-03-11 stsp {
5383 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5384 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5385 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5386 d1644381 2019-07-14 stsp
5387 d1644381 2019-07-14 stsp /*
5388 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5389 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5390 d1644381 2019-07-14 stsp * an unintended typo.
5391 d1644381 2019-07-14 stsp */
5392 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5393 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5394 d0eebce4 2019-03-11 stsp
5395 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5396 dd88155e 2019-06-29 stsp repo);
5397 d83d9d5c 2019-05-13 stsp if (err) {
5398 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5399 d0eebce4 2019-03-11 stsp
5400 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5401 d83d9d5c 2019-05-13 stsp return err;
5402 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5403 d83d9d5c 2019-05-13 stsp if (err)
5404 d83d9d5c 2019-05-13 stsp return err;
5405 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5406 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5407 d83d9d5c 2019-05-13 stsp if (err)
5408 d83d9d5c 2019-05-13 stsp return err;
5409 d83d9d5c 2019-05-13 stsp }
5410 d83d9d5c 2019-05-13 stsp
5411 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5412 d0eebce4 2019-03-11 stsp if (err)
5413 d0eebce4 2019-03-11 stsp goto done;
5414 d0eebce4 2019-03-11 stsp
5415 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5416 d0eebce4 2019-03-11 stsp done:
5417 d0eebce4 2019-03-11 stsp if (ref)
5418 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5419 d0eebce4 2019-03-11 stsp free(id);
5420 d1c1ae5f 2019-08-12 stsp return err;
5421 d1c1ae5f 2019-08-12 stsp }
5422 d1c1ae5f 2019-08-12 stsp
5423 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5424 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5425 d1c1ae5f 2019-08-12 stsp {
5426 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5427 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5428 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5429 d1c1ae5f 2019-08-12 stsp
5430 d1c1ae5f 2019-08-12 stsp /*
5431 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5432 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5433 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5434 d1c1ae5f 2019-08-12 stsp */
5435 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5436 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5437 d1c1ae5f 2019-08-12 stsp
5438 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5439 d1c1ae5f 2019-08-12 stsp if (err)
5440 d1c1ae5f 2019-08-12 stsp return err;
5441 d1c1ae5f 2019-08-12 stsp
5442 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5443 d1c1ae5f 2019-08-12 stsp if (err)
5444 d1c1ae5f 2019-08-12 stsp goto done;
5445 d1c1ae5f 2019-08-12 stsp
5446 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5447 d1c1ae5f 2019-08-12 stsp done:
5448 d1c1ae5f 2019-08-12 stsp if (target_ref)
5449 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5450 d1c1ae5f 2019-08-12 stsp if (ref)
5451 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5452 d0eebce4 2019-03-11 stsp return err;
5453 d0eebce4 2019-03-11 stsp }
5454 d0eebce4 2019-03-11 stsp
5455 d0eebce4 2019-03-11 stsp static const struct got_error *
5456 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5457 d0eebce4 2019-03-11 stsp {
5458 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5459 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5460 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5461 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5462 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5463 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5464 b2070a3f 2020-03-22 stsp char *refname = NULL;
5465 d0eebce4 2019-03-11 stsp
5466 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5467 d0eebce4 2019-03-11 stsp switch (ch) {
5468 e31abbf2 2020-03-22 stsp case 'c':
5469 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5470 e31abbf2 2020-03-22 stsp break;
5471 d0eebce4 2019-03-11 stsp case 'd':
5472 e31abbf2 2020-03-22 stsp do_delete = 1;
5473 d0eebce4 2019-03-11 stsp break;
5474 d0eebce4 2019-03-11 stsp case 'r':
5475 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5476 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5477 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5478 9ba1d308 2019-10-21 stsp optarg);
5479 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5480 d0eebce4 2019-03-11 stsp break;
5481 d0eebce4 2019-03-11 stsp case 'l':
5482 d0eebce4 2019-03-11 stsp do_list = 1;
5483 d1c1ae5f 2019-08-12 stsp break;
5484 d1c1ae5f 2019-08-12 stsp case 's':
5485 e31abbf2 2020-03-22 stsp symref_target = optarg;
5486 d0eebce4 2019-03-11 stsp break;
5487 d0eebce4 2019-03-11 stsp default:
5488 d0eebce4 2019-03-11 stsp usage_ref();
5489 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5490 d0eebce4 2019-03-11 stsp }
5491 d0eebce4 2019-03-11 stsp }
5492 d0eebce4 2019-03-11 stsp
5493 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5494 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5495 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5496 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5497 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5498 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5499 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5500 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5501 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5502 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5503 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5504 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5505 d0eebce4 2019-03-11 stsp
5506 d0eebce4 2019-03-11 stsp argc -= optind;
5507 d0eebce4 2019-03-11 stsp argv += optind;
5508 d0eebce4 2019-03-11 stsp
5509 e31abbf2 2020-03-22 stsp if (do_list) {
5510 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5511 d0eebce4 2019-03-11 stsp usage_ref();
5512 b2070a3f 2020-03-22 stsp if (argc == 1) {
5513 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5514 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5515 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5516 b2070a3f 2020-03-22 stsp goto done;
5517 b2070a3f 2020-03-22 stsp }
5518 b2070a3f 2020-03-22 stsp }
5519 e31abbf2 2020-03-22 stsp } else {
5520 e31abbf2 2020-03-22 stsp if (argc != 1)
5521 e31abbf2 2020-03-22 stsp usage_ref();
5522 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5523 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5524 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5525 b2070a3f 2020-03-22 stsp goto done;
5526 b2070a3f 2020-03-22 stsp }
5527 e31abbf2 2020-03-22 stsp }
5528 b2070a3f 2020-03-22 stsp
5529 b2070a3f 2020-03-22 stsp if (refname)
5530 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5531 d0eebce4 2019-03-11 stsp
5532 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5533 e0b57350 2019-03-12 stsp if (do_list) {
5534 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5535 e0b57350 2019-03-12 stsp NULL) == -1)
5536 e0b57350 2019-03-12 stsp err(1, "pledge");
5537 e0b57350 2019-03-12 stsp } else {
5538 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5539 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5540 e0b57350 2019-03-12 stsp err(1, "pledge");
5541 e0b57350 2019-03-12 stsp }
5542 d0eebce4 2019-03-11 stsp #endif
5543 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5544 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5545 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5546 d0eebce4 2019-03-11 stsp goto done;
5547 d0eebce4 2019-03-11 stsp }
5548 d0eebce4 2019-03-11 stsp
5549 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5550 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5551 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5552 d0eebce4 2019-03-11 stsp goto done;
5553 d0eebce4 2019-03-11 stsp else
5554 d0eebce4 2019-03-11 stsp error = NULL;
5555 d0eebce4 2019-03-11 stsp if (worktree) {
5556 d0eebce4 2019-03-11 stsp repo_path =
5557 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5558 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5559 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5560 d0eebce4 2019-03-11 stsp if (error)
5561 d0eebce4 2019-03-11 stsp goto done;
5562 d0eebce4 2019-03-11 stsp } else {
5563 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5564 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5565 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5566 d0eebce4 2019-03-11 stsp goto done;
5567 d0eebce4 2019-03-11 stsp }
5568 d0eebce4 2019-03-11 stsp }
5569 d0eebce4 2019-03-11 stsp }
5570 d0eebce4 2019-03-11 stsp
5571 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5572 d0eebce4 2019-03-11 stsp if (error != NULL)
5573 d0eebce4 2019-03-11 stsp goto done;
5574 d0eebce4 2019-03-11 stsp
5575 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5576 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5577 c02c541e 2019-03-29 stsp if (error)
5578 c02c541e 2019-03-29 stsp goto done;
5579 c02c541e 2019-03-29 stsp
5580 d0eebce4 2019-03-11 stsp if (do_list)
5581 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5582 e31abbf2 2020-03-22 stsp else if (do_delete)
5583 161728eb 2021-07-24 stsp error = delete_ref_by_name(repo, refname);
5584 e31abbf2 2020-03-22 stsp else if (symref_target)
5585 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5586 e31abbf2 2020-03-22 stsp else {
5587 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5588 e31abbf2 2020-03-22 stsp usage_ref();
5589 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5590 e31abbf2 2020-03-22 stsp }
5591 4e759de4 2019-06-26 stsp done:
5592 b2070a3f 2020-03-22 stsp free(refname);
5593 1d0f4054 2021-06-17 stsp if (repo) {
5594 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5595 1d0f4054 2021-06-17 stsp if (error == NULL)
5596 1d0f4054 2021-06-17 stsp error = close_err;
5597 1d0f4054 2021-06-17 stsp }
5598 4e759de4 2019-06-26 stsp if (worktree)
5599 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5600 4e759de4 2019-06-26 stsp free(cwd);
5601 4e759de4 2019-06-26 stsp free(repo_path);
5602 4e759de4 2019-06-26 stsp return error;
5603 4e759de4 2019-06-26 stsp }
5604 4e759de4 2019-06-26 stsp
5605 4e759de4 2019-06-26 stsp __dead static void
5606 4e759de4 2019-06-26 stsp usage_branch(void)
5607 4e759de4 2019-06-26 stsp {
5608 4e759de4 2019-06-26 stsp fprintf(stderr,
5609 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5610 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5611 4e759de4 2019-06-26 stsp exit(1);
5612 4e759de4 2019-06-26 stsp }
5613 4e759de4 2019-06-26 stsp
5614 4e759de4 2019-06-26 stsp static const struct got_error *
5615 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5616 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5617 4e99b47e 2019-10-04 stsp {
5618 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5619 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5620 4e99b47e 2019-10-04 stsp char *refstr;
5621 4e99b47e 2019-10-04 stsp
5622 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5623 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5624 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5625 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5626 4e99b47e 2019-10-04 stsp
5627 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5628 4e99b47e 2019-10-04 stsp if (err)
5629 4e99b47e 2019-10-04 stsp return err;
5630 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5631 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5632 4e99b47e 2019-10-04 stsp marker = "* ";
5633 4e99b47e 2019-10-04 stsp else
5634 4e99b47e 2019-10-04 stsp marker = "~ ";
5635 4e99b47e 2019-10-04 stsp free(id);
5636 4e99b47e 2019-10-04 stsp }
5637 4e99b47e 2019-10-04 stsp
5638 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5639 4e99b47e 2019-10-04 stsp refname += 11;
5640 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5641 4e99b47e 2019-10-04 stsp refname += 18;
5642 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5643 34d4e04c 2021-02-08 stsp refname += 13;
5644 4e99b47e 2019-10-04 stsp
5645 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5646 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5647 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5648 4e99b47e 2019-10-04 stsp
5649 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5650 4e99b47e 2019-10-04 stsp free(refstr);
5651 4e99b47e 2019-10-04 stsp return NULL;
5652 4e99b47e 2019-10-04 stsp }
5653 4e99b47e 2019-10-04 stsp
5654 4e99b47e 2019-10-04 stsp static const struct got_error *
5655 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5656 ad89fa31 2019-10-04 stsp {
5657 ad89fa31 2019-10-04 stsp const char *refname;
5658 ad89fa31 2019-10-04 stsp
5659 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5660 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5661 ad89fa31 2019-10-04 stsp
5662 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5663 ad89fa31 2019-10-04 stsp
5664 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5665 ad89fa31 2019-10-04 stsp refname += 11;
5666 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5667 ad89fa31 2019-10-04 stsp refname += 18;
5668 ad89fa31 2019-10-04 stsp
5669 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5670 ad89fa31 2019-10-04 stsp
5671 ad89fa31 2019-10-04 stsp return NULL;
5672 ad89fa31 2019-10-04 stsp }
5673 ad89fa31 2019-10-04 stsp
5674 ad89fa31 2019-10-04 stsp static const struct got_error *
5675 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5676 4e759de4 2019-06-26 stsp {
5677 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5678 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5679 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5680 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5681 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5682 4e759de4 2019-06-26 stsp
5683 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5684 ba882ee3 2019-07-11 stsp
5685 4e99b47e 2019-10-04 stsp if (worktree) {
5686 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5687 4e99b47e 2019-10-04 stsp worktree);
5688 4e99b47e 2019-10-04 stsp if (err)
5689 4e99b47e 2019-10-04 stsp return err;
5690 4e99b47e 2019-10-04 stsp
5691 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5692 4e99b47e 2019-10-04 stsp worktree);
5693 4e99b47e 2019-10-04 stsp if (err)
5694 4e99b47e 2019-10-04 stsp return err;
5695 4e99b47e 2019-10-04 stsp
5696 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5697 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5698 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5699 4e99b47e 2019-10-04 stsp if (err)
5700 4e99b47e 2019-10-04 stsp return err;
5701 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5702 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5703 4e99b47e 2019-10-04 stsp }
5704 4e99b47e 2019-10-04 stsp }
5705 4e99b47e 2019-10-04 stsp
5706 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5707 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5708 4e759de4 2019-06-26 stsp if (err)
5709 4e759de4 2019-06-26 stsp return err;
5710 4e759de4 2019-06-26 stsp
5711 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5712 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5713 4e759de4 2019-06-26 stsp
5714 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5715 34d4e04c 2021-02-08 stsp
5716 34d4e04c 2021-02-08 stsp err = got_ref_list(&refs, repo, "refs/remotes",
5717 34d4e04c 2021-02-08 stsp got_ref_cmp_by_name, NULL);
5718 34d4e04c 2021-02-08 stsp if (err)
5719 34d4e04c 2021-02-08 stsp return err;
5720 34d4e04c 2021-02-08 stsp
5721 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
5722 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
5723 34d4e04c 2021-02-08 stsp
5724 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
5725 34d4e04c 2021-02-08 stsp
5726 4e759de4 2019-06-26 stsp return NULL;
5727 4e759de4 2019-06-26 stsp }
5728 4e759de4 2019-06-26 stsp
5729 4e759de4 2019-06-26 stsp static const struct got_error *
5730 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5731 45cd4e47 2019-08-25 stsp const char *branch_name)
5732 4e759de4 2019-06-26 stsp {
5733 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5734 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5735 2f1457c6 2021-08-27 stsp char *refname, *remote_refname = NULL;
5736 4e759de4 2019-06-26 stsp
5737 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/", 5) == 0)
5738 2f1457c6 2021-08-27 stsp branch_name += 5;
5739 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "heads/", 6) == 0)
5740 2f1457c6 2021-08-27 stsp branch_name += 6;
5741 2f1457c6 2021-08-27 stsp else if (strncmp(branch_name, "remotes/", 8) == 0)
5742 2f1457c6 2021-08-27 stsp branch_name += 8;
5743 2f1457c6 2021-08-27 stsp
5744 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5745 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5746 4e759de4 2019-06-26 stsp
5747 2f1457c6 2021-08-27 stsp if (asprintf(&remote_refname, "refs/remotes/%s",
5748 2f1457c6 2021-08-27 stsp branch_name) == -1) {
5749 2f1457c6 2021-08-27 stsp err = got_error_from_errno("asprintf");
5750 4e759de4 2019-06-26 stsp goto done;
5751 2f1457c6 2021-08-27 stsp }
5752 4e759de4 2019-06-26 stsp
5753 2f1457c6 2021-08-27 stsp err = got_ref_open(&ref, repo, refname, 0);
5754 2f1457c6 2021-08-27 stsp if (err) {
5755 2f1457c6 2021-08-27 stsp const struct got_error *err2;
5756 2f1457c6 2021-08-27 stsp if (err->code != GOT_ERR_NOT_REF)
5757 2f1457c6 2021-08-27 stsp goto done;
5758 2f1457c6 2021-08-27 stsp /*
5759 2f1457c6 2021-08-27 stsp * Keep 'err' intact such that if neither branch exists
5760 2f1457c6 2021-08-27 stsp * we report "refs/heads" rather than "refs/remotes" in
5761 2f1457c6 2021-08-27 stsp * our error message.
5762 2f1457c6 2021-08-27 stsp */
5763 2f1457c6 2021-08-27 stsp err2 = got_ref_open(&ref, repo, remote_refname, 0);
5764 2f1457c6 2021-08-27 stsp if (err2)
5765 2f1457c6 2021-08-27 stsp goto done;
5766 2f1457c6 2021-08-27 stsp err = NULL;
5767 2f1457c6 2021-08-27 stsp }
5768 2f1457c6 2021-08-27 stsp
5769 45cd4e47 2019-08-25 stsp if (worktree &&
5770 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5771 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5772 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5773 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5774 45cd4e47 2019-08-25 stsp goto done;
5775 45cd4e47 2019-08-25 stsp }
5776 45cd4e47 2019-08-25 stsp
5777 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5778 4e759de4 2019-06-26 stsp done:
5779 45cd4e47 2019-08-25 stsp if (ref)
5780 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5781 4e759de4 2019-06-26 stsp free(refname);
5782 2f1457c6 2021-08-27 stsp free(remote_refname);
5783 4e759de4 2019-06-26 stsp return err;
5784 4e759de4 2019-06-26 stsp }
5785 4e759de4 2019-06-26 stsp
5786 4e759de4 2019-06-26 stsp static const struct got_error *
5787 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5788 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5789 4e759de4 2019-06-26 stsp {
5790 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5791 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5792 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5793 d3f84d51 2019-07-11 stsp
5794 d3f84d51 2019-07-11 stsp /*
5795 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5796 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5797 d3f84d51 2019-07-11 stsp * an unintended typo.
5798 d3f84d51 2019-07-11 stsp */
5799 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5800 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5801 2f1457c6 2021-08-27 stsp
5802 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
5803 2f1457c6 2021-08-27 stsp branch_name += 11;
5804 4e759de4 2019-06-26 stsp
5805 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5806 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5807 62d463ca 2020-10-20 naddy goto done;
5808 4e759de4 2019-06-26 stsp }
5809 4e759de4 2019-06-26 stsp
5810 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5811 4e759de4 2019-06-26 stsp if (err == NULL) {
5812 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5813 4e759de4 2019-06-26 stsp goto done;
5814 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5815 4e759de4 2019-06-26 stsp goto done;
5816 4e759de4 2019-06-26 stsp
5817 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5818 4e759de4 2019-06-26 stsp if (err)
5819 4e759de4 2019-06-26 stsp goto done;
5820 4e759de4 2019-06-26 stsp
5821 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5822 d0eebce4 2019-03-11 stsp done:
5823 4e759de4 2019-06-26 stsp if (ref)
5824 4e759de4 2019-06-26 stsp got_ref_close(ref);
5825 4e759de4 2019-06-26 stsp free(base_refname);
5826 4e759de4 2019-06-26 stsp free(refname);
5827 4e759de4 2019-06-26 stsp return err;
5828 4e759de4 2019-06-26 stsp }
5829 4e759de4 2019-06-26 stsp
5830 4e759de4 2019-06-26 stsp static const struct got_error *
5831 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5832 4e759de4 2019-06-26 stsp {
5833 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5834 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5835 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5836 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5837 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5838 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5839 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5840 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5841 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5842 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5843 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5844 4e759de4 2019-06-26 stsp
5845 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5846 da76fce2 2020-02-24 stsp
5847 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5848 4e759de4 2019-06-26 stsp switch (ch) {
5849 a74f7e83 2019-11-10 stsp case 'c':
5850 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5851 a74f7e83 2019-11-10 stsp break;
5852 4e759de4 2019-06-26 stsp case 'd':
5853 4e759de4 2019-06-26 stsp delref = optarg;
5854 4e759de4 2019-06-26 stsp break;
5855 4e759de4 2019-06-26 stsp case 'r':
5856 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5857 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5858 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5859 9ba1d308 2019-10-21 stsp optarg);
5860 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5861 4e759de4 2019-06-26 stsp break;
5862 4e759de4 2019-06-26 stsp case 'l':
5863 4e759de4 2019-06-26 stsp do_list = 1;
5864 da76fce2 2020-02-24 stsp break;
5865 da76fce2 2020-02-24 stsp case 'n':
5866 da76fce2 2020-02-24 stsp do_update = 0;
5867 4e759de4 2019-06-26 stsp break;
5868 4e759de4 2019-06-26 stsp default:
5869 4e759de4 2019-06-26 stsp usage_branch();
5870 4e759de4 2019-06-26 stsp /* NOTREACHED */
5871 4e759de4 2019-06-26 stsp }
5872 4e759de4 2019-06-26 stsp }
5873 4e759de4 2019-06-26 stsp
5874 4e759de4 2019-06-26 stsp if (do_list && delref)
5875 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
5876 4e759de4 2019-06-26 stsp
5877 4e759de4 2019-06-26 stsp argc -= optind;
5878 4e759de4 2019-06-26 stsp argv += optind;
5879 ad89fa31 2019-10-04 stsp
5880 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5881 ad89fa31 2019-10-04 stsp do_show = 1;
5882 4e759de4 2019-06-26 stsp
5883 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5884 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5885 a74f7e83 2019-11-10 stsp
5886 4e759de4 2019-06-26 stsp if (do_list || delref) {
5887 4e759de4 2019-06-26 stsp if (argc > 0)
5888 4e759de4 2019-06-26 stsp usage_branch();
5889 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5890 4e759de4 2019-06-26 stsp usage_branch();
5891 4e759de4 2019-06-26 stsp
5892 4e759de4 2019-06-26 stsp #ifndef PROFILE
5893 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5894 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5895 4e759de4 2019-06-26 stsp NULL) == -1)
5896 4e759de4 2019-06-26 stsp err(1, "pledge");
5897 4e759de4 2019-06-26 stsp } else {
5898 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5899 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5900 4e759de4 2019-06-26 stsp err(1, "pledge");
5901 4e759de4 2019-06-26 stsp }
5902 4e759de4 2019-06-26 stsp #endif
5903 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5904 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5905 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5906 4e759de4 2019-06-26 stsp goto done;
5907 4e759de4 2019-06-26 stsp }
5908 4e759de4 2019-06-26 stsp
5909 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5910 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5911 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5912 4e759de4 2019-06-26 stsp goto done;
5913 4e759de4 2019-06-26 stsp else
5914 4e759de4 2019-06-26 stsp error = NULL;
5915 4e759de4 2019-06-26 stsp if (worktree) {
5916 4e759de4 2019-06-26 stsp repo_path =
5917 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5918 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5919 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5920 4e759de4 2019-06-26 stsp if (error)
5921 4e759de4 2019-06-26 stsp goto done;
5922 4e759de4 2019-06-26 stsp } else {
5923 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5924 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5925 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5926 4e759de4 2019-06-26 stsp goto done;
5927 4e759de4 2019-06-26 stsp }
5928 4e759de4 2019-06-26 stsp }
5929 4e759de4 2019-06-26 stsp }
5930 4e759de4 2019-06-26 stsp
5931 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5932 4e759de4 2019-06-26 stsp if (error != NULL)
5933 4e759de4 2019-06-26 stsp goto done;
5934 4e759de4 2019-06-26 stsp
5935 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5936 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5937 4e759de4 2019-06-26 stsp if (error)
5938 4e759de4 2019-06-26 stsp goto done;
5939 4e759de4 2019-06-26 stsp
5940 ad89fa31 2019-10-04 stsp if (do_show)
5941 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5942 ad89fa31 2019-10-04 stsp else if (do_list)
5943 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5944 4e759de4 2019-06-26 stsp else if (delref)
5945 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5946 4e759de4 2019-06-26 stsp else {
5947 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5948 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5949 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5950 84de9106 2020-12-26 stsp NULL);
5951 84de9106 2020-12-26 stsp if (error)
5952 84de9106 2020-12-26 stsp goto done;
5953 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5954 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5955 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5956 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5957 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5958 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5959 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5960 a74f7e83 2019-11-10 stsp if (error)
5961 a74f7e83 2019-11-10 stsp goto done;
5962 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5963 da76fce2 2020-02-24 stsp if (error)
5964 da76fce2 2020-02-24 stsp goto done;
5965 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5966 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5967 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5968 da76fce2 2020-02-24 stsp
5969 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5970 da76fce2 2020-02-24 stsp if (error)
5971 da76fce2 2020-02-24 stsp goto done;
5972 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5973 da76fce2 2020-02-24 stsp worktree);
5974 da76fce2 2020-02-24 stsp if (error)
5975 da76fce2 2020-02-24 stsp goto done;
5976 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5977 da76fce2 2020-02-24 stsp == -1) {
5978 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5979 da76fce2 2020-02-24 stsp goto done;
5980 da76fce2 2020-02-24 stsp }
5981 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5982 da76fce2 2020-02-24 stsp free(branch_refname);
5983 da76fce2 2020-02-24 stsp if (error)
5984 da76fce2 2020-02-24 stsp goto done;
5985 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5986 da76fce2 2020-02-24 stsp repo);
5987 da76fce2 2020-02-24 stsp if (error)
5988 da76fce2 2020-02-24 stsp goto done;
5989 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5990 da76fce2 2020-02-24 stsp commit_id);
5991 da76fce2 2020-02-24 stsp if (error)
5992 da76fce2 2020-02-24 stsp goto done;
5993 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5994 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5995 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5996 9627c110 2020-04-18 stsp NULL);
5997 da76fce2 2020-02-24 stsp if (error)
5998 da76fce2 2020-02-24 stsp goto done;
5999 9627c110 2020-04-18 stsp if (upa.did_something)
6000 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
6001 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6002 da76fce2 2020-02-24 stsp }
6003 4e759de4 2019-06-26 stsp }
6004 4e759de4 2019-06-26 stsp done:
6005 da76fce2 2020-02-24 stsp if (ref)
6006 da76fce2 2020-02-24 stsp got_ref_close(ref);
6007 1d0f4054 2021-06-17 stsp if (repo) {
6008 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6009 1d0f4054 2021-06-17 stsp if (error == NULL)
6010 1d0f4054 2021-06-17 stsp error = close_err;
6011 1d0f4054 2021-06-17 stsp }
6012 d0eebce4 2019-03-11 stsp if (worktree)
6013 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
6014 d0eebce4 2019-03-11 stsp free(cwd);
6015 d0eebce4 2019-03-11 stsp free(repo_path);
6016 da76fce2 2020-02-24 stsp free(commit_id);
6017 da76fce2 2020-02-24 stsp free(commit_id_str);
6018 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
6019 da76fce2 2020-02-24 stsp free((char *)pe->path);
6020 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
6021 d00136be 2019-03-26 stsp return error;
6022 d00136be 2019-03-26 stsp }
6023 d00136be 2019-03-26 stsp
6024 8e7bd50a 2019-08-22 stsp
6025 d00136be 2019-03-26 stsp __dead static void
6026 8e7bd50a 2019-08-22 stsp usage_tag(void)
6027 8e7bd50a 2019-08-22 stsp {
6028 8e7bd50a 2019-08-22 stsp fprintf(stderr,
6029 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
6030 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
6031 8e7bd50a 2019-08-22 stsp exit(1);
6032 b8bad2ba 2019-08-23 stsp }
6033 b8bad2ba 2019-08-23 stsp
6034 b8bad2ba 2019-08-23 stsp #if 0
6035 b8bad2ba 2019-08-23 stsp static const struct got_error *
6036 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6037 b8bad2ba 2019-08-23 stsp {
6038 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
6039 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
6040 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
6041 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
6042 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
6043 b8bad2ba 2019-08-23 stsp
6044 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
6045 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
6046 b8bad2ba 2019-08-23 stsp if (se == NULL) {
6047 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6048 b8bad2ba 2019-08-23 stsp if (err)
6049 b8bad2ba 2019-08-23 stsp return err;
6050 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6051 b8bad2ba 2019-08-23 stsp continue;
6052 b8bad2ba 2019-08-23 stsp } else {
6053 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6054 b8bad2ba 2019-08-23 stsp if (err)
6055 b8bad2ba 2019-08-23 stsp break;
6056 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
6057 b8bad2ba 2019-08-23 stsp free(re_id);
6058 b8bad2ba 2019-08-23 stsp if (err)
6059 b8bad2ba 2019-08-23 stsp break;
6060 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
6061 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
6062 b8bad2ba 2019-08-23 stsp }
6063 b8bad2ba 2019-08-23 stsp
6064 b8bad2ba 2019-08-23 stsp while (se) {
6065 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6066 b8bad2ba 2019-08-23 stsp if (err)
6067 b8bad2ba 2019-08-23 stsp break;
6068 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
6069 b8bad2ba 2019-08-23 stsp free(se_id);
6070 b8bad2ba 2019-08-23 stsp if (err)
6071 b8bad2ba 2019-08-23 stsp break;
6072 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
6073 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
6074 b8bad2ba 2019-08-23 stsp
6075 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6076 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6077 b8bad2ba 2019-08-23 stsp if (err)
6078 b8bad2ba 2019-08-23 stsp return err;
6079 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6080 b8bad2ba 2019-08-23 stsp break;
6081 b8bad2ba 2019-08-23 stsp }
6082 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6083 b8bad2ba 2019-08-23 stsp continue;
6084 b8bad2ba 2019-08-23 stsp }
6085 b8bad2ba 2019-08-23 stsp }
6086 b8bad2ba 2019-08-23 stsp done:
6087 b8bad2ba 2019-08-23 stsp return err;
6088 8e7bd50a 2019-08-22 stsp }
6089 b8bad2ba 2019-08-23 stsp #endif
6090 b8bad2ba 2019-08-23 stsp
6091 b8bad2ba 2019-08-23 stsp static const struct got_error *
6092 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
6093 8e7bd50a 2019-08-22 stsp {
6094 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
6095 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
6096 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6097 8e7bd50a 2019-08-22 stsp
6098 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6099 8e7bd50a 2019-08-22 stsp
6100 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6101 8e7bd50a 2019-08-22 stsp if (err)
6102 8e7bd50a 2019-08-22 stsp return err;
6103 8e7bd50a 2019-08-22 stsp
6104 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6105 8e7bd50a 2019-08-22 stsp const char *refname;
6106 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6107 8e7bd50a 2019-08-22 stsp char datebuf[26];
6108 d4efa91b 2020-01-14 stsp const char *tagger;
6109 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6110 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6111 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6112 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6113 8e7bd50a 2019-08-22 stsp
6114 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6115 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6116 8e7bd50a 2019-08-22 stsp continue;
6117 8e7bd50a 2019-08-22 stsp refname += 10;
6118 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6119 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6120 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6121 8e7bd50a 2019-08-22 stsp break;
6122 8e7bd50a 2019-08-22 stsp }
6123 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6124 8e7bd50a 2019-08-22 stsp free(refstr);
6125 8e7bd50a 2019-08-22 stsp
6126 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6127 8e7bd50a 2019-08-22 stsp if (err)
6128 8e7bd50a 2019-08-22 stsp break;
6129 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6130 d4efa91b 2020-01-14 stsp if (err) {
6131 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6132 d4efa91b 2020-01-14 stsp free(id);
6133 d4efa91b 2020-01-14 stsp break;
6134 d4efa91b 2020-01-14 stsp }
6135 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6136 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6137 d4efa91b 2020-01-14 stsp if (err) {
6138 d4efa91b 2020-01-14 stsp free(id);
6139 d4efa91b 2020-01-14 stsp break;
6140 d4efa91b 2020-01-14 stsp }
6141 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6142 d4efa91b 2020-01-14 stsp tagger_time =
6143 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6144 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6145 d4efa91b 2020-01-14 stsp free(id);
6146 d4efa91b 2020-01-14 stsp if (err)
6147 d4efa91b 2020-01-14 stsp break;
6148 d4efa91b 2020-01-14 stsp } else {
6149 d4efa91b 2020-01-14 stsp free(id);
6150 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6151 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6152 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6153 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6154 d4efa91b 2020-01-14 stsp if (err)
6155 d4efa91b 2020-01-14 stsp break;
6156 d4efa91b 2020-01-14 stsp }
6157 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6158 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6159 2417344c 2019-08-23 stsp if (datestr)
6160 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6161 d4efa91b 2020-01-14 stsp if (commit)
6162 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6163 d4efa91b 2020-01-14 stsp else {
6164 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6165 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6166 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6167 d4efa91b 2020-01-14 stsp id_str);
6168 d4efa91b 2020-01-14 stsp break;
6169 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6170 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6171 d4efa91b 2020-01-14 stsp id_str);
6172 d4efa91b 2020-01-14 stsp break;
6173 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6174 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6175 d4efa91b 2020-01-14 stsp id_str);
6176 d4efa91b 2020-01-14 stsp break;
6177 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6178 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6179 d4efa91b 2020-01-14 stsp id_str);
6180 d4efa91b 2020-01-14 stsp break;
6181 d4efa91b 2020-01-14 stsp default:
6182 d4efa91b 2020-01-14 stsp break;
6183 d4efa91b 2020-01-14 stsp }
6184 8e7bd50a 2019-08-22 stsp }
6185 8e7bd50a 2019-08-22 stsp free(id_str);
6186 d4efa91b 2020-01-14 stsp if (commit) {
6187 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6188 d4efa91b 2020-01-14 stsp if (err)
6189 d4efa91b 2020-01-14 stsp break;
6190 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6191 d4efa91b 2020-01-14 stsp } else {
6192 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6193 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6194 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6195 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6196 d4efa91b 2020-01-14 stsp break;
6197 d4efa91b 2020-01-14 stsp }
6198 8e7bd50a 2019-08-22 stsp }
6199 8e7bd50a 2019-08-22 stsp
6200 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6201 8e7bd50a 2019-08-22 stsp do {
6202 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6203 8e7bd50a 2019-08-22 stsp if (line)
6204 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6205 8e7bd50a 2019-08-22 stsp } while (line);
6206 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6207 8e7bd50a 2019-08-22 stsp }
6208 8e7bd50a 2019-08-22 stsp
6209 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6210 8e7bd50a 2019-08-22 stsp return NULL;
6211 8e7bd50a 2019-08-22 stsp }
6212 8e7bd50a 2019-08-22 stsp
6213 8e7bd50a 2019-08-22 stsp static const struct got_error *
6214 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6215 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6216 8e7bd50a 2019-08-22 stsp {
6217 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6218 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6219 f372d5cd 2019-10-21 stsp char *editor = NULL;
6220 1601cb9f 2020-09-11 naddy int initial_content_len;
6221 8e7bd50a 2019-08-22 stsp int fd = -1;
6222 8e7bd50a 2019-08-22 stsp
6223 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6224 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6225 8e7bd50a 2019-08-22 stsp goto done;
6226 8e7bd50a 2019-08-22 stsp }
6227 8e7bd50a 2019-08-22 stsp
6228 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6229 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6230 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6231 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6232 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6233 8e7bd50a 2019-08-22 stsp goto done;
6234 8e7bd50a 2019-08-22 stsp }
6235 8e7bd50a 2019-08-22 stsp
6236 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6237 8e7bd50a 2019-08-22 stsp if (err)
6238 8e7bd50a 2019-08-22 stsp goto done;
6239 8e7bd50a 2019-08-22 stsp
6240 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6241 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6242 97972933 2020-09-11 stsp goto done;
6243 97972933 2020-09-11 stsp }
6244 8e7bd50a 2019-08-22 stsp
6245 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6246 8e7bd50a 2019-08-22 stsp if (err)
6247 8e7bd50a 2019-08-22 stsp goto done;
6248 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6249 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6250 8e7bd50a 2019-08-22 stsp done:
6251 8e7bd50a 2019-08-22 stsp free(initial_content);
6252 8e7bd50a 2019-08-22 stsp free(template);
6253 8e7bd50a 2019-08-22 stsp free(editor);
6254 8e7bd50a 2019-08-22 stsp
6255 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6256 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6257 97972933 2020-09-11 stsp
6258 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6259 59f86c76 2020-09-11 stsp if (err == NULL)
6260 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6261 59f86c76 2020-09-11 stsp if (err) {
6262 59f86c76 2020-09-11 stsp free(*tagmsg);
6263 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6264 8e7bd50a 2019-08-22 stsp }
6265 8e7bd50a 2019-08-22 stsp return err;
6266 8e7bd50a 2019-08-22 stsp }
6267 8e7bd50a 2019-08-22 stsp
6268 8e7bd50a 2019-08-22 stsp static const struct got_error *
6269 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6270 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6271 8e7bd50a 2019-08-22 stsp {
6272 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6273 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6274 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6275 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6276 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6277 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6278 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6279 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6280 8e7bd50a 2019-08-22 stsp
6281 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6282 84de9106 2020-12-26 stsp
6283 8e7bd50a 2019-08-22 stsp /*
6284 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6285 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6286 8e7bd50a 2019-08-22 stsp * an unintended typo.
6287 8e7bd50a 2019-08-22 stsp */
6288 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6289 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6290 8e7bd50a 2019-08-22 stsp
6291 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6292 8e7bd50a 2019-08-22 stsp if (err)
6293 8e7bd50a 2019-08-22 stsp return err;
6294 8e7bd50a 2019-08-22 stsp
6295 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6296 84de9106 2020-12-26 stsp if (err)
6297 84de9106 2020-12-26 stsp goto done;
6298 84de9106 2020-12-26 stsp
6299 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6300 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6301 8e7bd50a 2019-08-22 stsp if (err)
6302 8e7bd50a 2019-08-22 stsp goto done;
6303 8e7bd50a 2019-08-22 stsp
6304 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6305 8e7bd50a 2019-08-22 stsp if (err)
6306 8e7bd50a 2019-08-22 stsp goto done;
6307 8e7bd50a 2019-08-22 stsp
6308 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6309 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6310 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6311 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6312 62d463ca 2020-10-20 naddy goto done;
6313 8e7bd50a 2019-08-22 stsp }
6314 8e7bd50a 2019-08-22 stsp tag_name += 10;
6315 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6316 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6317 62d463ca 2020-10-20 naddy goto done;
6318 8e7bd50a 2019-08-22 stsp }
6319 8e7bd50a 2019-08-22 stsp
6320 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6321 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6322 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6323 8e7bd50a 2019-08-22 stsp goto done;
6324 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6325 8e7bd50a 2019-08-22 stsp goto done;
6326 8e7bd50a 2019-08-22 stsp
6327 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6328 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6329 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6330 f372d5cd 2019-10-21 stsp if (err) {
6331 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6332 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6333 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6334 8e7bd50a 2019-08-22 stsp goto done;
6335 f372d5cd 2019-10-21 stsp }
6336 8e7bd50a 2019-08-22 stsp }
6337 8e7bd50a 2019-08-22 stsp
6338 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6339 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6340 f372d5cd 2019-10-21 stsp if (err) {
6341 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6342 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6343 8e7bd50a 2019-08-22 stsp goto done;
6344 f372d5cd 2019-10-21 stsp }
6345 8e7bd50a 2019-08-22 stsp
6346 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6347 f372d5cd 2019-10-21 stsp if (err) {
6348 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6349 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6350 8e7bd50a 2019-08-22 stsp goto done;
6351 f372d5cd 2019-10-21 stsp }
6352 8e7bd50a 2019-08-22 stsp
6353 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6354 f372d5cd 2019-10-21 stsp if (err) {
6355 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6356 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6357 f372d5cd 2019-10-21 stsp goto done;
6358 f372d5cd 2019-10-21 stsp }
6359 8e7bd50a 2019-08-22 stsp
6360 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6361 f372d5cd 2019-10-21 stsp if (err) {
6362 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6363 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6364 f372d5cd 2019-10-21 stsp goto done;
6365 8e7bd50a 2019-08-22 stsp }
6366 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6367 8e7bd50a 2019-08-22 stsp done:
6368 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6369 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6370 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6371 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6372 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6373 f372d5cd 2019-10-21 stsp free(tag_id_str);
6374 8e7bd50a 2019-08-22 stsp if (ref)
6375 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6376 8e7bd50a 2019-08-22 stsp free(commit_id);
6377 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6378 8e7bd50a 2019-08-22 stsp free(refname);
6379 8e7bd50a 2019-08-22 stsp free(tagmsg);
6380 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6381 aba9c984 2019-09-08 stsp free(tagger);
6382 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6383 8e7bd50a 2019-08-22 stsp return err;
6384 8e7bd50a 2019-08-22 stsp }
6385 8e7bd50a 2019-08-22 stsp
6386 8e7bd50a 2019-08-22 stsp static const struct got_error *
6387 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6388 8e7bd50a 2019-08-22 stsp {
6389 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6390 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6391 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6392 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6393 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6394 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6395 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6396 8e7bd50a 2019-08-22 stsp
6397 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6398 8e7bd50a 2019-08-22 stsp switch (ch) {
6399 80106605 2020-02-24 stsp case 'c':
6400 80106605 2020-02-24 stsp commit_id_arg = optarg;
6401 80106605 2020-02-24 stsp break;
6402 8e7bd50a 2019-08-22 stsp case 'm':
6403 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6404 8e7bd50a 2019-08-22 stsp break;
6405 8e7bd50a 2019-08-22 stsp case 'r':
6406 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6407 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6408 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6409 9ba1d308 2019-10-21 stsp optarg);
6410 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6411 8e7bd50a 2019-08-22 stsp break;
6412 8e7bd50a 2019-08-22 stsp case 'l':
6413 8e7bd50a 2019-08-22 stsp do_list = 1;
6414 8e7bd50a 2019-08-22 stsp break;
6415 8e7bd50a 2019-08-22 stsp default:
6416 8e7bd50a 2019-08-22 stsp usage_tag();
6417 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6418 8e7bd50a 2019-08-22 stsp }
6419 8e7bd50a 2019-08-22 stsp }
6420 8e7bd50a 2019-08-22 stsp
6421 8e7bd50a 2019-08-22 stsp argc -= optind;
6422 8e7bd50a 2019-08-22 stsp argv += optind;
6423 8e7bd50a 2019-08-22 stsp
6424 8e7bd50a 2019-08-22 stsp if (do_list) {
6425 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6426 775ce909 2020-03-22 stsp errx(1,
6427 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6428 8e7bd50a 2019-08-22 stsp if (tagmsg)
6429 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6430 8e7bd50a 2019-08-22 stsp if (argc > 0)
6431 8e7bd50a 2019-08-22 stsp usage_tag();
6432 80106605 2020-02-24 stsp } else if (argc != 1)
6433 8e7bd50a 2019-08-22 stsp usage_tag();
6434 80106605 2020-02-24 stsp
6435 a2887370 2019-08-23 stsp tag_name = argv[0];
6436 8e7bd50a 2019-08-22 stsp
6437 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6438 8e7bd50a 2019-08-22 stsp if (do_list) {
6439 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6440 8e7bd50a 2019-08-22 stsp NULL) == -1)
6441 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6442 8e7bd50a 2019-08-22 stsp } else {
6443 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6444 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6445 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6446 8e7bd50a 2019-08-22 stsp }
6447 8e7bd50a 2019-08-22 stsp #endif
6448 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6449 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6450 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6451 8e7bd50a 2019-08-22 stsp goto done;
6452 8e7bd50a 2019-08-22 stsp }
6453 8e7bd50a 2019-08-22 stsp
6454 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6455 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6456 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6457 8e7bd50a 2019-08-22 stsp goto done;
6458 8e7bd50a 2019-08-22 stsp else
6459 8e7bd50a 2019-08-22 stsp error = NULL;
6460 8e7bd50a 2019-08-22 stsp if (worktree) {
6461 8e7bd50a 2019-08-22 stsp repo_path =
6462 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6463 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6464 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6465 8e7bd50a 2019-08-22 stsp if (error)
6466 8e7bd50a 2019-08-22 stsp goto done;
6467 8e7bd50a 2019-08-22 stsp } else {
6468 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6469 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6470 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6471 8e7bd50a 2019-08-22 stsp goto done;
6472 8e7bd50a 2019-08-22 stsp }
6473 8e7bd50a 2019-08-22 stsp }
6474 8e7bd50a 2019-08-22 stsp }
6475 8e7bd50a 2019-08-22 stsp
6476 8e7bd50a 2019-08-22 stsp if (do_list) {
6477 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6478 c9956ddf 2019-09-08 stsp if (error != NULL)
6479 c9956ddf 2019-09-08 stsp goto done;
6480 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6481 8e7bd50a 2019-08-22 stsp if (error)
6482 8e7bd50a 2019-08-22 stsp goto done;
6483 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6484 8e7bd50a 2019-08-22 stsp } else {
6485 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6486 c9956ddf 2019-09-08 stsp if (error)
6487 c9956ddf 2019-09-08 stsp goto done;
6488 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6489 c9956ddf 2019-09-08 stsp if (error != NULL)
6490 c9956ddf 2019-09-08 stsp goto done;
6491 c9956ddf 2019-09-08 stsp
6492 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6493 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6494 8e7bd50a 2019-08-22 stsp if (error)
6495 8e7bd50a 2019-08-22 stsp goto done;
6496 8e7bd50a 2019-08-22 stsp }
6497 8e7bd50a 2019-08-22 stsp
6498 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6499 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6500 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6501 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6502 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6503 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6504 8e7bd50a 2019-08-22 stsp if (error)
6505 8e7bd50a 2019-08-22 stsp goto done;
6506 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6507 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6508 8e7bd50a 2019-08-22 stsp if (error)
6509 8e7bd50a 2019-08-22 stsp goto done;
6510 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6511 8e7bd50a 2019-08-22 stsp free(commit_id);
6512 8e7bd50a 2019-08-22 stsp if (error)
6513 8e7bd50a 2019-08-22 stsp goto done;
6514 8e7bd50a 2019-08-22 stsp }
6515 8e7bd50a 2019-08-22 stsp
6516 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6517 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6518 8e7bd50a 2019-08-22 stsp }
6519 8e7bd50a 2019-08-22 stsp done:
6520 1d0f4054 2021-06-17 stsp if (repo) {
6521 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6522 1d0f4054 2021-06-17 stsp if (error == NULL)
6523 1d0f4054 2021-06-17 stsp error = close_err;
6524 1d0f4054 2021-06-17 stsp }
6525 8e7bd50a 2019-08-22 stsp if (worktree)
6526 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6527 8e7bd50a 2019-08-22 stsp free(cwd);
6528 8e7bd50a 2019-08-22 stsp free(repo_path);
6529 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6530 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6531 8e7bd50a 2019-08-22 stsp return error;
6532 8e7bd50a 2019-08-22 stsp }
6533 8e7bd50a 2019-08-22 stsp
6534 8e7bd50a 2019-08-22 stsp __dead static void
6535 d00136be 2019-03-26 stsp usage_add(void)
6536 d00136be 2019-03-26 stsp {
6537 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6538 022fae89 2019-12-06 tracey getprogname());
6539 d00136be 2019-03-26 stsp exit(1);
6540 d00136be 2019-03-26 stsp }
6541 d00136be 2019-03-26 stsp
6542 d00136be 2019-03-26 stsp static const struct got_error *
6543 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6544 4e68cba3 2019-11-23 stsp {
6545 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6546 4e68cba3 2019-11-23 stsp path++;
6547 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6548 4e68cba3 2019-11-23 stsp return NULL;
6549 4e68cba3 2019-11-23 stsp }
6550 4e68cba3 2019-11-23 stsp
6551 4e68cba3 2019-11-23 stsp static const struct got_error *
6552 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6553 d00136be 2019-03-26 stsp {
6554 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6555 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6556 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6557 1dd54920 2019-05-11 stsp char *cwd = NULL;
6558 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6559 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6560 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6561 1dd54920 2019-05-11 stsp
6562 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6563 d00136be 2019-03-26 stsp
6564 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6565 d00136be 2019-03-26 stsp switch (ch) {
6566 022fae89 2019-12-06 tracey case 'I':
6567 022fae89 2019-12-06 tracey no_ignores = 1;
6568 022fae89 2019-12-06 tracey break;
6569 4e68cba3 2019-11-23 stsp case 'R':
6570 4e68cba3 2019-11-23 stsp can_recurse = 1;
6571 4e68cba3 2019-11-23 stsp break;
6572 d00136be 2019-03-26 stsp default:
6573 d00136be 2019-03-26 stsp usage_add();
6574 d00136be 2019-03-26 stsp /* NOTREACHED */
6575 d00136be 2019-03-26 stsp }
6576 d00136be 2019-03-26 stsp }
6577 d00136be 2019-03-26 stsp
6578 d00136be 2019-03-26 stsp argc -= optind;
6579 d00136be 2019-03-26 stsp argv += optind;
6580 d00136be 2019-03-26 stsp
6581 43012d58 2019-07-14 stsp #ifndef PROFILE
6582 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6583 43012d58 2019-07-14 stsp NULL) == -1)
6584 43012d58 2019-07-14 stsp err(1, "pledge");
6585 43012d58 2019-07-14 stsp #endif
6586 723c305c 2019-05-11 jcs if (argc < 1)
6587 d00136be 2019-03-26 stsp usage_add();
6588 d00136be 2019-03-26 stsp
6589 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6590 d00136be 2019-03-26 stsp if (cwd == NULL) {
6591 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6592 d00136be 2019-03-26 stsp goto done;
6593 d00136be 2019-03-26 stsp }
6594 723c305c 2019-05-11 jcs
6595 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6596 fa51e947 2020-03-27 stsp if (error) {
6597 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6598 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6599 d00136be 2019-03-26 stsp goto done;
6600 fa51e947 2020-03-27 stsp }
6601 d00136be 2019-03-26 stsp
6602 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6603 c9956ddf 2019-09-08 stsp NULL);
6604 031a5338 2019-03-26 stsp if (error != NULL)
6605 031a5338 2019-03-26 stsp goto done;
6606 031a5338 2019-03-26 stsp
6607 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6608 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6609 d00136be 2019-03-26 stsp if (error)
6610 d00136be 2019-03-26 stsp goto done;
6611 d00136be 2019-03-26 stsp
6612 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6613 6d022e97 2019-08-04 stsp if (error)
6614 022fae89 2019-12-06 tracey goto done;
6615 022fae89 2019-12-06 tracey
6616 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6617 4e68cba3 2019-11-23 stsp char *ondisk_path;
6618 4e68cba3 2019-11-23 stsp struct stat sb;
6619 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6620 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6621 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6622 62d463ca 2020-10-20 naddy pe->path) == -1) {
6623 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6624 4e68cba3 2019-11-23 stsp goto done;
6625 4e68cba3 2019-11-23 stsp }
6626 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6627 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6628 4e68cba3 2019-11-23 stsp free(ondisk_path);
6629 4e68cba3 2019-11-23 stsp continue;
6630 4e68cba3 2019-11-23 stsp }
6631 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6632 4e68cba3 2019-11-23 stsp ondisk_path);
6633 4e68cba3 2019-11-23 stsp free(ondisk_path);
6634 4e68cba3 2019-11-23 stsp goto done;
6635 4e68cba3 2019-11-23 stsp }
6636 4e68cba3 2019-11-23 stsp free(ondisk_path);
6637 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6638 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6639 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6640 4e68cba3 2019-11-23 stsp goto done;
6641 4e68cba3 2019-11-23 stsp }
6642 4e68cba3 2019-11-23 stsp }
6643 4e68cba3 2019-11-23 stsp }
6644 022fae89 2019-12-06 tracey
6645 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6646 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6647 d00136be 2019-03-26 stsp done:
6648 1d0f4054 2021-06-17 stsp if (repo) {
6649 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6650 1d0f4054 2021-06-17 stsp if (error == NULL)
6651 1d0f4054 2021-06-17 stsp error = close_err;
6652 1d0f4054 2021-06-17 stsp }
6653 d00136be 2019-03-26 stsp if (worktree)
6654 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6655 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6656 1dd54920 2019-05-11 stsp free((char *)pe->path);
6657 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6658 2ec1f75b 2019-03-26 stsp free(cwd);
6659 2ec1f75b 2019-03-26 stsp return error;
6660 2ec1f75b 2019-03-26 stsp }
6661 2ec1f75b 2019-03-26 stsp
6662 2ec1f75b 2019-03-26 stsp __dead static void
6663 648e4ef7 2019-07-09 stsp usage_remove(void)
6664 2ec1f75b 2019-03-26 stsp {
6665 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6666 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6667 2ec1f75b 2019-03-26 stsp exit(1);
6668 2a06fe5f 2019-08-24 stsp }
6669 2a06fe5f 2019-08-24 stsp
6670 2a06fe5f 2019-08-24 stsp static const struct got_error *
6671 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6672 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6673 2a06fe5f 2019-08-24 stsp {
6674 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6675 f2a9dc41 2019-12-13 tracey path++;
6676 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6677 2a06fe5f 2019-08-24 stsp return NULL;
6678 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6679 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6680 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6681 2a06fe5f 2019-08-24 stsp return NULL;
6682 2ec1f75b 2019-03-26 stsp }
6683 2ec1f75b 2019-03-26 stsp
6684 2ec1f75b 2019-03-26 stsp static const struct got_error *
6685 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6686 2ec1f75b 2019-03-26 stsp {
6687 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6688 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6689 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6690 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6691 17ed4618 2019-06-02 stsp char *cwd = NULL;
6692 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6693 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6694 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6695 2ec1f75b 2019-03-26 stsp
6696 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6697 17ed4618 2019-06-02 stsp
6698 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6699 2ec1f75b 2019-03-26 stsp switch (ch) {
6700 2ec1f75b 2019-03-26 stsp case 'f':
6701 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6702 2ec1f75b 2019-03-26 stsp break;
6703 70e3e7f5 2019-12-13 tracey case 'k':
6704 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6705 70e3e7f5 2019-12-13 tracey break;
6706 f2a9dc41 2019-12-13 tracey case 'R':
6707 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6708 f2a9dc41 2019-12-13 tracey break;
6709 766841c2 2020-08-13 stsp case 's':
6710 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6711 766841c2 2020-08-13 stsp switch (optarg[i]) {
6712 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6713 766841c2 2020-08-13 stsp delete_local_mods = 1;
6714 766841c2 2020-08-13 stsp break;
6715 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6716 766841c2 2020-08-13 stsp break;
6717 766841c2 2020-08-13 stsp default:
6718 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6719 766841c2 2020-08-13 stsp optarg[i]);
6720 766841c2 2020-08-13 stsp }
6721 766841c2 2020-08-13 stsp }
6722 766841c2 2020-08-13 stsp status_codes = optarg;
6723 766841c2 2020-08-13 stsp break;
6724 2ec1f75b 2019-03-26 stsp default:
6725 f2a9dc41 2019-12-13 tracey usage_remove();
6726 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6727 2ec1f75b 2019-03-26 stsp }
6728 2ec1f75b 2019-03-26 stsp }
6729 2ec1f75b 2019-03-26 stsp
6730 2ec1f75b 2019-03-26 stsp argc -= optind;
6731 2ec1f75b 2019-03-26 stsp argv += optind;
6732 2ec1f75b 2019-03-26 stsp
6733 43012d58 2019-07-14 stsp #ifndef PROFILE
6734 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6735 43012d58 2019-07-14 stsp NULL) == -1)
6736 43012d58 2019-07-14 stsp err(1, "pledge");
6737 43012d58 2019-07-14 stsp #endif
6738 17ed4618 2019-06-02 stsp if (argc < 1)
6739 648e4ef7 2019-07-09 stsp usage_remove();
6740 2ec1f75b 2019-03-26 stsp
6741 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6742 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6743 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6744 2ec1f75b 2019-03-26 stsp goto done;
6745 2ec1f75b 2019-03-26 stsp }
6746 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6747 fa51e947 2020-03-27 stsp if (error) {
6748 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6749 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6750 2ec1f75b 2019-03-26 stsp goto done;
6751 fa51e947 2020-03-27 stsp }
6752 2ec1f75b 2019-03-26 stsp
6753 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6754 c9956ddf 2019-09-08 stsp NULL);
6755 2af4a041 2019-05-11 jcs if (error)
6756 2ec1f75b 2019-03-26 stsp goto done;
6757 2ec1f75b 2019-03-26 stsp
6758 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6759 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6760 2ec1f75b 2019-03-26 stsp if (error)
6761 2ec1f75b 2019-03-26 stsp goto done;
6762 2ec1f75b 2019-03-26 stsp
6763 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6764 6d022e97 2019-08-04 stsp if (error)
6765 6d022e97 2019-08-04 stsp goto done;
6766 17ed4618 2019-06-02 stsp
6767 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6768 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6769 f2a9dc41 2019-12-13 tracey struct stat sb;
6770 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6771 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6772 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6773 62d463ca 2020-10-20 naddy pe->path) == -1) {
6774 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6775 f2a9dc41 2019-12-13 tracey goto done;
6776 f2a9dc41 2019-12-13 tracey }
6777 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6778 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6779 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6780 f2a9dc41 2019-12-13 tracey continue;
6781 f2a9dc41 2019-12-13 tracey }
6782 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6783 f2a9dc41 2019-12-13 tracey ondisk_path);
6784 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6785 f2a9dc41 2019-12-13 tracey goto done;
6786 f2a9dc41 2019-12-13 tracey }
6787 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6788 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6789 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6790 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6791 f2a9dc41 2019-12-13 tracey goto done;
6792 f2a9dc41 2019-12-13 tracey }
6793 f2a9dc41 2019-12-13 tracey }
6794 f2a9dc41 2019-12-13 tracey }
6795 f2a9dc41 2019-12-13 tracey
6796 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6797 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6798 766841c2 2020-08-13 stsp repo, keep_on_disk);
6799 a129376b 2019-03-28 stsp done:
6800 1d0f4054 2021-06-17 stsp if (repo) {
6801 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6802 1d0f4054 2021-06-17 stsp if (error == NULL)
6803 1d0f4054 2021-06-17 stsp error = close_err;
6804 1d0f4054 2021-06-17 stsp }
6805 a129376b 2019-03-28 stsp if (worktree)
6806 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6807 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6808 17ed4618 2019-06-02 stsp free((char *)pe->path);
6809 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6810 a129376b 2019-03-28 stsp free(cwd);
6811 a129376b 2019-03-28 stsp return error;
6812 a129376b 2019-03-28 stsp }
6813 a129376b 2019-03-28 stsp
6814 a129376b 2019-03-28 stsp __dead static void
6815 a129376b 2019-03-28 stsp usage_revert(void)
6816 a129376b 2019-03-28 stsp {
6817 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6818 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6819 a129376b 2019-03-28 stsp exit(1);
6820 a129376b 2019-03-28 stsp }
6821 a129376b 2019-03-28 stsp
6822 1ee397ad 2019-07-12 stsp static const struct got_error *
6823 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6824 a129376b 2019-03-28 stsp {
6825 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6826 fb9704af 2020-01-27 stsp return NULL;
6827 fb9704af 2020-01-27 stsp
6828 a129376b 2019-03-28 stsp while (path[0] == '/')
6829 a129376b 2019-03-28 stsp path++;
6830 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6831 33aa809d 2019-08-08 stsp return NULL;
6832 33aa809d 2019-08-08 stsp }
6833 33aa809d 2019-08-08 stsp
6834 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6835 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6836 33aa809d 2019-08-08 stsp const char *action;
6837 33aa809d 2019-08-08 stsp };
6838 33aa809d 2019-08-08 stsp
6839 33aa809d 2019-08-08 stsp static const struct got_error *
6840 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6841 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6842 33aa809d 2019-08-08 stsp {
6843 33aa809d 2019-08-08 stsp char *line = NULL;
6844 33aa809d 2019-08-08 stsp size_t linesize = 0;
6845 33aa809d 2019-08-08 stsp ssize_t linelen;
6846 33aa809d 2019-08-08 stsp
6847 33aa809d 2019-08-08 stsp switch (status) {
6848 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6849 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6850 33aa809d 2019-08-08 stsp break;
6851 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6852 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6853 33aa809d 2019-08-08 stsp break;
6854 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6855 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6856 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6857 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6858 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6859 33aa809d 2019-08-08 stsp printf("%s", line);
6860 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6861 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6862 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6863 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6864 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6865 33aa809d 2019-08-08 stsp break;
6866 33aa809d 2019-08-08 stsp default:
6867 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6868 33aa809d 2019-08-08 stsp }
6869 33aa809d 2019-08-08 stsp
6870 33aa809d 2019-08-08 stsp return NULL;
6871 33aa809d 2019-08-08 stsp }
6872 33aa809d 2019-08-08 stsp
6873 33aa809d 2019-08-08 stsp static const struct got_error *
6874 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6875 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6876 33aa809d 2019-08-08 stsp {
6877 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6878 33aa809d 2019-08-08 stsp char *line = NULL;
6879 33aa809d 2019-08-08 stsp size_t linesize = 0;
6880 33aa809d 2019-08-08 stsp ssize_t linelen;
6881 33aa809d 2019-08-08 stsp int resp = ' ';
6882 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6883 33aa809d 2019-08-08 stsp
6884 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6885 33aa809d 2019-08-08 stsp
6886 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6887 33aa809d 2019-08-08 stsp char *nl;
6888 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6889 33aa809d 2019-08-08 stsp a->action);
6890 33aa809d 2019-08-08 stsp if (err)
6891 33aa809d 2019-08-08 stsp return err;
6892 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6893 33aa809d 2019-08-08 stsp if (linelen == -1) {
6894 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6895 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6896 33aa809d 2019-08-08 stsp return NULL;
6897 33aa809d 2019-08-08 stsp }
6898 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6899 33aa809d 2019-08-08 stsp if (nl)
6900 33aa809d 2019-08-08 stsp *nl = '\0';
6901 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6902 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6903 33aa809d 2019-08-08 stsp printf("y\n");
6904 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6905 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6906 33aa809d 2019-08-08 stsp printf("n\n");
6907 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6908 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6909 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6910 33aa809d 2019-08-08 stsp printf("q\n");
6911 33aa809d 2019-08-08 stsp } else
6912 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6913 33aa809d 2019-08-08 stsp free(line);
6914 33aa809d 2019-08-08 stsp return NULL;
6915 33aa809d 2019-08-08 stsp }
6916 33aa809d 2019-08-08 stsp
6917 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6918 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6919 33aa809d 2019-08-08 stsp a->action);
6920 33aa809d 2019-08-08 stsp if (err)
6921 33aa809d 2019-08-08 stsp return err;
6922 33aa809d 2019-08-08 stsp resp = getchar();
6923 33aa809d 2019-08-08 stsp if (resp == '\n')
6924 33aa809d 2019-08-08 stsp resp = getchar();
6925 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6926 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6927 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6928 33aa809d 2019-08-08 stsp resp = ' ';
6929 33aa809d 2019-08-08 stsp }
6930 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6931 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6932 33aa809d 2019-08-08 stsp resp = ' ';
6933 33aa809d 2019-08-08 stsp }
6934 33aa809d 2019-08-08 stsp }
6935 33aa809d 2019-08-08 stsp
6936 33aa809d 2019-08-08 stsp if (resp == 'y')
6937 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6938 33aa809d 2019-08-08 stsp else if (resp == 'n')
6939 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6940 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6941 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6942 33aa809d 2019-08-08 stsp
6943 1ee397ad 2019-07-12 stsp return NULL;
6944 a129376b 2019-03-28 stsp }
6945 a129376b 2019-03-28 stsp
6946 33aa809d 2019-08-08 stsp
6947 a129376b 2019-03-28 stsp static const struct got_error *
6948 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6949 a129376b 2019-03-28 stsp {
6950 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6951 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6952 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6953 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6954 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6955 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6956 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6957 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6958 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6959 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6960 a129376b 2019-03-28 stsp
6961 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6962 e20a8b6f 2019-06-04 stsp
6963 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6964 a129376b 2019-03-28 stsp switch (ch) {
6965 33aa809d 2019-08-08 stsp case 'p':
6966 33aa809d 2019-08-08 stsp pflag = 1;
6967 33aa809d 2019-08-08 stsp break;
6968 33aa809d 2019-08-08 stsp case 'F':
6969 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6970 33aa809d 2019-08-08 stsp break;
6971 0f6d7415 2019-08-08 stsp case 'R':
6972 0f6d7415 2019-08-08 stsp can_recurse = 1;
6973 0f6d7415 2019-08-08 stsp break;
6974 a129376b 2019-03-28 stsp default:
6975 a129376b 2019-03-28 stsp usage_revert();
6976 a129376b 2019-03-28 stsp /* NOTREACHED */
6977 a129376b 2019-03-28 stsp }
6978 a129376b 2019-03-28 stsp }
6979 a129376b 2019-03-28 stsp
6980 a129376b 2019-03-28 stsp argc -= optind;
6981 a129376b 2019-03-28 stsp argv += optind;
6982 a129376b 2019-03-28 stsp
6983 43012d58 2019-07-14 stsp #ifndef PROFILE
6984 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6985 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6986 43012d58 2019-07-14 stsp err(1, "pledge");
6987 43012d58 2019-07-14 stsp #endif
6988 e20a8b6f 2019-06-04 stsp if (argc < 1)
6989 a129376b 2019-03-28 stsp usage_revert();
6990 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6991 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6992 a129376b 2019-03-28 stsp
6993 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6994 a129376b 2019-03-28 stsp if (cwd == NULL) {
6995 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6996 a129376b 2019-03-28 stsp goto done;
6997 a129376b 2019-03-28 stsp }
6998 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6999 fa51e947 2020-03-27 stsp if (error) {
7000 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7001 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
7002 2ec1f75b 2019-03-26 stsp goto done;
7003 fa51e947 2020-03-27 stsp }
7004 a129376b 2019-03-28 stsp
7005 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7006 c9956ddf 2019-09-08 stsp NULL);
7007 a129376b 2019-03-28 stsp if (error != NULL)
7008 a129376b 2019-03-28 stsp goto done;
7009 a129376b 2019-03-28 stsp
7010 33aa809d 2019-08-08 stsp if (patch_script_path) {
7011 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7012 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
7013 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
7014 33aa809d 2019-08-08 stsp patch_script_path);
7015 33aa809d 2019-08-08 stsp goto done;
7016 33aa809d 2019-08-08 stsp }
7017 33aa809d 2019-08-08 stsp }
7018 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7019 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7020 a129376b 2019-03-28 stsp if (error)
7021 a129376b 2019-03-28 stsp goto done;
7022 a129376b 2019-03-28 stsp
7023 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7024 6d022e97 2019-08-04 stsp if (error)
7025 6d022e97 2019-08-04 stsp goto done;
7026 00db391e 2019-08-03 stsp
7027 0f6d7415 2019-08-08 stsp if (!can_recurse) {
7028 0f6d7415 2019-08-08 stsp char *ondisk_path;
7029 0f6d7415 2019-08-08 stsp struct stat sb;
7030 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
7031 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
7032 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
7033 62d463ca 2020-10-20 naddy pe->path) == -1) {
7034 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
7035 0f6d7415 2019-08-08 stsp goto done;
7036 0f6d7415 2019-08-08 stsp }
7037 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
7038 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
7039 0f6d7415 2019-08-08 stsp free(ondisk_path);
7040 0f6d7415 2019-08-08 stsp continue;
7041 0f6d7415 2019-08-08 stsp }
7042 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
7043 0f6d7415 2019-08-08 stsp ondisk_path);
7044 0f6d7415 2019-08-08 stsp free(ondisk_path);
7045 0f6d7415 2019-08-08 stsp goto done;
7046 0f6d7415 2019-08-08 stsp }
7047 0f6d7415 2019-08-08 stsp free(ondisk_path);
7048 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
7049 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7050 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
7051 0f6d7415 2019-08-08 stsp goto done;
7052 0f6d7415 2019-08-08 stsp }
7053 0f6d7415 2019-08-08 stsp }
7054 0f6d7415 2019-08-08 stsp }
7055 0f6d7415 2019-08-08 stsp
7056 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7057 33aa809d 2019-08-08 stsp cpa.action = "revert";
7058 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7059 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7060 2ec1f75b 2019-03-26 stsp done:
7061 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7062 33aa809d 2019-08-08 stsp error == NULL)
7063 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7064 1d0f4054 2021-06-17 stsp if (repo) {
7065 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7066 1d0f4054 2021-06-17 stsp if (error == NULL)
7067 1d0f4054 2021-06-17 stsp error = close_err;
7068 1d0f4054 2021-06-17 stsp }
7069 2ec1f75b 2019-03-26 stsp if (worktree)
7070 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
7071 2ec1f75b 2019-03-26 stsp free(path);
7072 d00136be 2019-03-26 stsp free(cwd);
7073 6bad629b 2019-02-04 stsp return error;
7074 c4296144 2019-05-09 stsp }
7075 c4296144 2019-05-09 stsp
7076 c4296144 2019-05-09 stsp __dead static void
7077 c4296144 2019-05-09 stsp usage_commit(void)
7078 c4296144 2019-05-09 stsp {
7079 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7080 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
7081 c4296144 2019-05-09 stsp exit(1);
7082 33ad4cbe 2019-05-12 jcs }
7083 33ad4cbe 2019-05-12 jcs
7084 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
7085 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
7086 28cf319f 2021-01-28 stsp const char *prepared_log;
7087 28cf319f 2021-01-28 stsp int non_interactive;
7088 e2ba3d07 2019-05-13 stsp const char *editor;
7089 e0870e44 2019-05-13 stsp const char *worktree_path;
7090 76d98825 2019-06-03 stsp const char *branch_name;
7091 314a6357 2019-05-13 stsp const char *repo_path;
7092 e0870e44 2019-05-13 stsp char *logmsg_path;
7093 e2ba3d07 2019-05-13 stsp
7094 e2ba3d07 2019-05-13 stsp };
7095 28cf319f 2021-01-28 stsp
7096 28cf319f 2021-01-28 stsp static const struct got_error *
7097 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
7098 28cf319f 2021-01-28 stsp {
7099 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
7100 28cf319f 2021-01-28 stsp FILE *f = NULL;
7101 28cf319f 2021-01-28 stsp struct stat sb;
7102 28cf319f 2021-01-28 stsp size_t r;
7103 28cf319f 2021-01-28 stsp
7104 28cf319f 2021-01-28 stsp *logmsg = NULL;
7105 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7106 28cf319f 2021-01-28 stsp
7107 28cf319f 2021-01-28 stsp f = fopen(path, "r");
7108 28cf319f 2021-01-28 stsp if (f == NULL)
7109 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
7110 28cf319f 2021-01-28 stsp
7111 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
7112 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
7113 28cf319f 2021-01-28 stsp goto done;
7114 28cf319f 2021-01-28 stsp }
7115 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
7116 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7117 28cf319f 2021-01-28 stsp goto done;
7118 28cf319f 2021-01-28 stsp }
7119 28cf319f 2021-01-28 stsp
7120 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
7121 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
7122 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
7123 28cf319f 2021-01-28 stsp goto done;
7124 28cf319f 2021-01-28 stsp }
7125 28cf319f 2021-01-28 stsp
7126 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7127 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7128 28cf319f 2021-01-28 stsp if (ferror(f))
7129 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7130 28cf319f 2021-01-28 stsp else
7131 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7132 28cf319f 2021-01-28 stsp goto done;
7133 28cf319f 2021-01-28 stsp }
7134 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7135 28cf319f 2021-01-28 stsp done:
7136 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7137 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7138 28cf319f 2021-01-28 stsp if (err) {
7139 28cf319f 2021-01-28 stsp free(*logmsg);
7140 28cf319f 2021-01-28 stsp *logmsg = NULL;
7141 28cf319f 2021-01-28 stsp }
7142 28cf319f 2021-01-28 stsp return err;
7143 28cf319f 2021-01-28 stsp
7144 28cf319f 2021-01-28 stsp }
7145 e0870e44 2019-05-13 stsp
7146 33ad4cbe 2019-05-12 jcs static const struct got_error *
7147 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7148 33ad4cbe 2019-05-12 jcs void *arg)
7149 33ad4cbe 2019-05-12 jcs {
7150 76d98825 2019-06-03 stsp char *initial_content = NULL;
7151 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7152 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7153 e0870e44 2019-05-13 stsp char *template = NULL;
7154 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7155 1601cb9f 2020-09-11 naddy int initial_content_len;
7156 97972933 2020-09-11 stsp int fd = -1;
7157 33ad4cbe 2019-05-12 jcs size_t len;
7158 33ad4cbe 2019-05-12 jcs
7159 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7160 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7161 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7162 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7163 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7164 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7165 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7166 33ad4cbe 2019-05-12 jcs return NULL;
7167 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7168 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7169 33ad4cbe 2019-05-12 jcs
7170 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7171 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7172 76d98825 2019-06-03 stsp
7173 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7174 28cf319f 2021-01-28 stsp if (err)
7175 28cf319f 2021-01-28 stsp goto done;
7176 28cf319f 2021-01-28 stsp
7177 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7178 28cf319f 2021-01-28 stsp char *msg;
7179 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7180 28cf319f 2021-01-28 stsp if (err)
7181 28cf319f 2021-01-28 stsp goto done;
7182 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7183 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7184 28cf319f 2021-01-28 stsp free(msg);
7185 28cf319f 2021-01-28 stsp goto done;
7186 28cf319f 2021-01-28 stsp }
7187 28cf319f 2021-01-28 stsp free(msg);
7188 28cf319f 2021-01-28 stsp }
7189 28cf319f 2021-01-28 stsp
7190 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7191 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7192 1601cb9f 2020-09-11 naddy a->branch_name);
7193 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7194 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7195 e0870e44 2019-05-13 stsp goto done;
7196 28cf319f 2021-01-28 stsp }
7197 33ad4cbe 2019-05-12 jcs
7198 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7199 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7200 97972933 2020-09-11 stsp goto done;
7201 97972933 2020-09-11 stsp }
7202 33ad4cbe 2019-05-12 jcs
7203 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7204 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7205 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7206 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7207 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7208 33ad4cbe 2019-05-12 jcs }
7209 33ad4cbe 2019-05-12 jcs
7210 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7211 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7212 33ad4cbe 2019-05-12 jcs done:
7213 76d98825 2019-06-03 stsp free(initial_content);
7214 e0870e44 2019-05-13 stsp free(template);
7215 314a6357 2019-05-13 stsp
7216 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7217 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7218 97972933 2020-09-11 stsp
7219 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7220 59f86c76 2020-09-11 stsp if (err == NULL)
7221 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7222 59f86c76 2020-09-11 stsp if (err) {
7223 59f86c76 2020-09-11 stsp free(*logmsg);
7224 59f86c76 2020-09-11 stsp *logmsg = NULL;
7225 3ce1b845 2019-07-15 stsp }
7226 33ad4cbe 2019-05-12 jcs return err;
7227 6bad629b 2019-02-04 stsp }
7228 c4296144 2019-05-09 stsp
7229 c4296144 2019-05-09 stsp static const struct got_error *
7230 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7231 c4296144 2019-05-09 stsp {
7232 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7233 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7234 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7235 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7236 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7237 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7238 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7239 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7240 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7241 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7242 28cf319f 2021-01-28 stsp int allow_bad_symlinks = 0, non_interactive = 0;
7243 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7244 5c1e53bc 2019-07-28 stsp
7245 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7246 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7247 c4296144 2019-05-09 stsp
7248 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7249 c4296144 2019-05-09 stsp switch (ch) {
7250 28cf319f 2021-01-28 stsp case 'F':
7251 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7252 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7253 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7254 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7255 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7256 28cf319f 2021-01-28 stsp optarg);
7257 28cf319f 2021-01-28 stsp break;
7258 c4296144 2019-05-09 stsp case 'm':
7259 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7260 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7261 c4296144 2019-05-09 stsp logmsg = optarg;
7262 28cf319f 2021-01-28 stsp break;
7263 28cf319f 2021-01-28 stsp case 'N':
7264 28cf319f 2021-01-28 stsp non_interactive = 1;
7265 c4296144 2019-05-09 stsp break;
7266 35213c7c 2020-07-23 stsp case 'S':
7267 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7268 35213c7c 2020-07-23 stsp break;
7269 c4296144 2019-05-09 stsp default:
7270 c4296144 2019-05-09 stsp usage_commit();
7271 c4296144 2019-05-09 stsp /* NOTREACHED */
7272 c4296144 2019-05-09 stsp }
7273 c4296144 2019-05-09 stsp }
7274 c4296144 2019-05-09 stsp
7275 c4296144 2019-05-09 stsp argc -= optind;
7276 c4296144 2019-05-09 stsp argv += optind;
7277 c4296144 2019-05-09 stsp
7278 43012d58 2019-07-14 stsp #ifndef PROFILE
7279 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7280 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7281 43012d58 2019-07-14 stsp err(1, "pledge");
7282 43012d58 2019-07-14 stsp #endif
7283 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7284 c4296144 2019-05-09 stsp if (cwd == NULL) {
7285 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7286 c4296144 2019-05-09 stsp goto done;
7287 c4296144 2019-05-09 stsp }
7288 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7289 fa51e947 2020-03-27 stsp if (error) {
7290 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7291 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7292 7d5807f4 2019-07-11 stsp goto done;
7293 fa51e947 2020-03-27 stsp }
7294 7d5807f4 2019-07-11 stsp
7295 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7296 c4296144 2019-05-09 stsp if (error)
7297 a698f62e 2019-07-25 stsp goto done;
7298 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7299 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7300 c4296144 2019-05-09 stsp goto done;
7301 a698f62e 2019-07-25 stsp }
7302 5c1e53bc 2019-07-28 stsp
7303 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7304 916f288c 2019-07-30 stsp worktree);
7305 5c1e53bc 2019-07-28 stsp if (error)
7306 5c1e53bc 2019-07-28 stsp goto done;
7307 c4296144 2019-05-09 stsp
7308 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7309 c9956ddf 2019-09-08 stsp if (error)
7310 c9956ddf 2019-09-08 stsp goto done;
7311 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7312 c9956ddf 2019-09-08 stsp gitconfig_path);
7313 c4296144 2019-05-09 stsp if (error != NULL)
7314 c4296144 2019-05-09 stsp goto done;
7315 c4296144 2019-05-09 stsp
7316 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7317 aba9c984 2019-09-08 stsp if (error)
7318 aba9c984 2019-09-08 stsp return error;
7319 aba9c984 2019-09-08 stsp
7320 314a6357 2019-05-13 stsp /*
7321 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7322 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7323 314a6357 2019-05-13 stsp */
7324 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7325 314a6357 2019-05-13 stsp error = get_editor(&editor);
7326 314a6357 2019-05-13 stsp else
7327 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7328 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7329 c4296144 2019-05-09 stsp if (error)
7330 c4296144 2019-05-09 stsp goto done;
7331 c4296144 2019-05-09 stsp
7332 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7333 087fb88c 2019-08-04 stsp if (error)
7334 087fb88c 2019-08-04 stsp goto done;
7335 087fb88c 2019-08-04 stsp
7336 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7337 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7338 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7339 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7340 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7341 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7342 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7343 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7344 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7345 916f288c 2019-07-30 stsp goto done;
7346 916f288c 2019-07-30 stsp }
7347 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7348 916f288c 2019-07-30 stsp }
7349 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7350 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7351 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7352 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7353 e0870e44 2019-05-13 stsp if (error) {
7354 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7355 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7356 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7357 c4296144 2019-05-09 stsp goto done;
7358 e0870e44 2019-05-13 stsp }
7359 c4296144 2019-05-09 stsp
7360 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7361 c4296144 2019-05-09 stsp if (error)
7362 c4296144 2019-05-09 stsp goto done;
7363 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7364 c4296144 2019-05-09 stsp done:
7365 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7366 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7367 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7368 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7369 7266f21f 2019-10-21 stsp error == NULL)
7370 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7371 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7372 1d0f4054 2021-06-17 stsp if (repo) {
7373 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7374 1d0f4054 2021-06-17 stsp if (error == NULL)
7375 1d0f4054 2021-06-17 stsp error = close_err;
7376 1d0f4054 2021-06-17 stsp }
7377 c4296144 2019-05-09 stsp if (worktree)
7378 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7379 c4296144 2019-05-09 stsp free(cwd);
7380 c4296144 2019-05-09 stsp free(id_str);
7381 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7382 e2ba3d07 2019-05-13 stsp free(editor);
7383 aba9c984 2019-09-08 stsp free(author);
7384 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7385 f8a36e22 2021-08-26 stsp return error;
7386 f8a36e22 2021-08-26 stsp }
7387 f8a36e22 2021-08-26 stsp
7388 f8a36e22 2021-08-26 stsp __dead static void
7389 f8a36e22 2021-08-26 stsp usage_send(void)
7390 f8a36e22 2021-08-26 stsp {
7391 f8a36e22 2021-08-26 stsp fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7392 f8a36e22 2021-08-26 stsp "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7393 f8a36e22 2021-08-26 stsp "[remote-repository]\n", getprogname());
7394 f8a36e22 2021-08-26 stsp exit(1);
7395 f8a36e22 2021-08-26 stsp }
7396 f8a36e22 2021-08-26 stsp
7397 f8a36e22 2021-08-26 stsp struct got_send_progress_arg {
7398 f8a36e22 2021-08-26 stsp char last_scaled_packsize[FMT_SCALED_STRSIZE];
7399 f8a36e22 2021-08-26 stsp int verbosity;
7400 f8a36e22 2021-08-26 stsp int last_ncommits;
7401 f8a36e22 2021-08-26 stsp int last_nobj_total;
7402 f8a36e22 2021-08-26 stsp int last_p_deltify;
7403 f8a36e22 2021-08-26 stsp int last_p_written;
7404 f8a36e22 2021-08-26 stsp int last_p_sent;
7405 f8a36e22 2021-08-26 stsp int printed_something;
7406 f8a36e22 2021-08-26 stsp int sent_something;
7407 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches;
7408 f8a36e22 2021-08-26 stsp };
7409 f8a36e22 2021-08-26 stsp
7410 f8a36e22 2021-08-26 stsp static const struct got_error *
7411 f8a36e22 2021-08-26 stsp send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7412 f8a36e22 2021-08-26 stsp int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7413 f8a36e22 2021-08-26 stsp int success)
7414 f8a36e22 2021-08-26 stsp {
7415 f8a36e22 2021-08-26 stsp struct got_send_progress_arg *a = arg;
7416 f8a36e22 2021-08-26 stsp char scaled_packsize[FMT_SCALED_STRSIZE];
7417 f8a36e22 2021-08-26 stsp char scaled_sent[FMT_SCALED_STRSIZE];
7418 f8a36e22 2021-08-26 stsp int p_deltify = 0, p_written = 0, p_sent = 0;
7419 f8a36e22 2021-08-26 stsp int print_searching = 0, print_total = 0;
7420 f8a36e22 2021-08-26 stsp int print_deltify = 0, print_written = 0, print_sent = 0;
7421 f8a36e22 2021-08-26 stsp
7422 f8a36e22 2021-08-26 stsp if (a->verbosity < 0)
7423 f8a36e22 2021-08-26 stsp return NULL;
7424 f8a36e22 2021-08-26 stsp
7425 f8a36e22 2021-08-26 stsp if (refname) {
7426 f8a36e22 2021-08-26 stsp const char *status = success ? "accepted" : "rejected";
7427 f8a36e22 2021-08-26 stsp
7428 f8a36e22 2021-08-26 stsp if (success) {
7429 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7430 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, a->delete_branches, entry) {
7431 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7432 f8a36e22 2021-08-26 stsp if (got_path_cmp(branchname, refname,
7433 f8a36e22 2021-08-26 stsp strlen(branchname), strlen(refname)) == 0) {
7434 f8a36e22 2021-08-26 stsp status = "deleted";
7435 f8a36e22 2021-08-26 stsp break;
7436 f8a36e22 2021-08-26 stsp }
7437 f8a36e22 2021-08-26 stsp }
7438 f8a36e22 2021-08-26 stsp }
7439 f8a36e22 2021-08-26 stsp
7440 f8a36e22 2021-08-26 stsp printf("\nServer has %s %s", status, refname);
7441 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7442 f8a36e22 2021-08-26 stsp return NULL;
7443 f8a36e22 2021-08-26 stsp }
7444 f8a36e22 2021-08-26 stsp
7445 f8a36e22 2021-08-26 stsp if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7446 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7447 f8a36e22 2021-08-26 stsp if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7448 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7449 f8a36e22 2021-08-26 stsp
7450 f8a36e22 2021-08-26 stsp if (a->last_ncommits != ncommits) {
7451 f8a36e22 2021-08-26 stsp print_searching = 1;
7452 f8a36e22 2021-08-26 stsp a->last_ncommits = ncommits;
7453 f8a36e22 2021-08-26 stsp }
7454 f8a36e22 2021-08-26 stsp
7455 f8a36e22 2021-08-26 stsp if (a->last_nobj_total != nobj_total) {
7456 f8a36e22 2021-08-26 stsp print_searching = 1;
7457 f8a36e22 2021-08-26 stsp print_total = 1;
7458 f8a36e22 2021-08-26 stsp a->last_nobj_total = nobj_total;
7459 f8a36e22 2021-08-26 stsp }
7460 f8a36e22 2021-08-26 stsp
7461 f8a36e22 2021-08-26 stsp if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7462 f8a36e22 2021-08-26 stsp strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7463 f8a36e22 2021-08-26 stsp if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7464 f8a36e22 2021-08-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7465 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
7466 f8a36e22 2021-08-26 stsp }
7467 f8a36e22 2021-08-26 stsp
7468 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0 || nobj_written > 0) {
7469 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0) {
7470 f8a36e22 2021-08-26 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
7471 f8a36e22 2021-08-26 stsp if (p_deltify != a->last_p_deltify) {
7472 f8a36e22 2021-08-26 stsp a->last_p_deltify = p_deltify;
7473 f8a36e22 2021-08-26 stsp print_searching = 1;
7474 f8a36e22 2021-08-26 stsp print_total = 1;
7475 f8a36e22 2021-08-26 stsp print_deltify = 1;
7476 f8a36e22 2021-08-26 stsp }
7477 f8a36e22 2021-08-26 stsp }
7478 f8a36e22 2021-08-26 stsp if (nobj_written > 0) {
7479 f8a36e22 2021-08-26 stsp p_written = (nobj_written * 100) / nobj_total;
7480 f8a36e22 2021-08-26 stsp if (p_written != a->last_p_written) {
7481 f8a36e22 2021-08-26 stsp a->last_p_written = p_written;
7482 f8a36e22 2021-08-26 stsp print_searching = 1;
7483 f8a36e22 2021-08-26 stsp print_total = 1;
7484 f8a36e22 2021-08-26 stsp print_deltify = 1;
7485 f8a36e22 2021-08-26 stsp print_written = 1;
7486 f8a36e22 2021-08-26 stsp }
7487 f8a36e22 2021-08-26 stsp }
7488 f8a36e22 2021-08-26 stsp }
7489 f8a36e22 2021-08-26 stsp
7490 f8a36e22 2021-08-26 stsp if (bytes_sent > 0) {
7491 f8a36e22 2021-08-26 stsp p_sent = (bytes_sent * 100) / packfile_size;
7492 f8a36e22 2021-08-26 stsp if (p_sent != a->last_p_sent) {
7493 f8a36e22 2021-08-26 stsp a->last_p_sent = p_sent;
7494 f8a36e22 2021-08-26 stsp print_searching = 1;
7495 f8a36e22 2021-08-26 stsp print_total = 1;
7496 f8a36e22 2021-08-26 stsp print_deltify = 1;
7497 f8a36e22 2021-08-26 stsp print_written = 1;
7498 f8a36e22 2021-08-26 stsp print_sent = 1;
7499 f8a36e22 2021-08-26 stsp }
7500 f8a36e22 2021-08-26 stsp a->sent_something = 1;
7501 f8a36e22 2021-08-26 stsp }
7502 f8a36e22 2021-08-26 stsp
7503 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify || print_written ||
7504 f8a36e22 2021-08-26 stsp print_sent)
7505 f8a36e22 2021-08-26 stsp printf("\r");
7506 f8a36e22 2021-08-26 stsp if (print_searching)
7507 f8a36e22 2021-08-26 stsp printf("packing %d reference%s", ncommits,
7508 f8a36e22 2021-08-26 stsp ncommits == 1 ? "" : "s");
7509 f8a36e22 2021-08-26 stsp if (print_total)
7510 f8a36e22 2021-08-26 stsp printf("; %d object%s", nobj_total,
7511 f8a36e22 2021-08-26 stsp nobj_total == 1 ? "" : "s");
7512 f8a36e22 2021-08-26 stsp if (print_deltify)
7513 f8a36e22 2021-08-26 stsp printf("; deltify: %d%%", p_deltify);
7514 f8a36e22 2021-08-26 stsp if (print_sent)
7515 f8a36e22 2021-08-26 stsp printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7516 f8a36e22 2021-08-26 stsp scaled_packsize, p_sent);
7517 f8a36e22 2021-08-26 stsp else if (print_written)
7518 f8a36e22 2021-08-26 stsp printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7519 f8a36e22 2021-08-26 stsp scaled_packsize, p_written);
7520 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify ||
7521 f8a36e22 2021-08-26 stsp print_written || print_sent) {
7522 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7523 f8a36e22 2021-08-26 stsp fflush(stdout);
7524 f8a36e22 2021-08-26 stsp }
7525 f8a36e22 2021-08-26 stsp return NULL;
7526 f8a36e22 2021-08-26 stsp }
7527 f8a36e22 2021-08-26 stsp
7528 f8a36e22 2021-08-26 stsp static const struct got_error *
7529 f8a36e22 2021-08-26 stsp cmd_send(int argc, char *argv[])
7530 f8a36e22 2021-08-26 stsp {
7531 f8a36e22 2021-08-26 stsp const struct got_error *error = NULL;
7532 f8a36e22 2021-08-26 stsp char *cwd = NULL, *repo_path = NULL;
7533 f8a36e22 2021-08-26 stsp const char *remote_name;
7534 f8a36e22 2021-08-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
7535 f8a36e22 2021-08-26 stsp char *repo_name = NULL, *server_path = NULL;
7536 f8a36e22 2021-08-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
7537 f8a36e22 2021-08-26 stsp int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7538 f8a36e22 2021-08-26 stsp struct got_repository *repo = NULL;
7539 f8a36e22 2021-08-26 stsp struct got_worktree *worktree = NULL;
7540 f8a36e22 2021-08-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7541 f8a36e22 2021-08-26 stsp struct got_pathlist_head branches;
7542 f8a36e22 2021-08-26 stsp struct got_pathlist_head tags;
7543 f8a36e22 2021-08-26 stsp struct got_reflist_head all_branches;
7544 f8a36e22 2021-08-26 stsp struct got_reflist_head all_tags;
7545 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_args;
7546 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_branches;
7547 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
7548 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7549 f8a36e22 2021-08-26 stsp int i, ch, sendfd = -1, sendstatus;
7550 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
7551 f8a36e22 2021-08-26 stsp struct got_send_progress_arg spa;
7552 f8a36e22 2021-08-26 stsp int verbosity = 0, overwrite_refs = 0;
7553 f8a36e22 2021-08-26 stsp int send_all_branches = 0, send_all_tags = 0;
7554 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
7555 f8a36e22 2021-08-26 stsp
7556 f8a36e22 2021-08-26 stsp TAILQ_INIT(&branches);
7557 f8a36e22 2021-08-26 stsp TAILQ_INIT(&tags);
7558 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_branches);
7559 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_tags);
7560 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_args);
7561 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_branches);
7562 f8a36e22 2021-08-26 stsp
7563 f8a36e22 2021-08-26 stsp while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7564 f8a36e22 2021-08-26 stsp switch (ch) {
7565 f8a36e22 2021-08-26 stsp case 'a':
7566 f8a36e22 2021-08-26 stsp send_all_branches = 1;
7567 f8a36e22 2021-08-26 stsp break;
7568 f8a36e22 2021-08-26 stsp case 'b':
7569 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, optarg, NULL);
7570 f8a36e22 2021-08-26 stsp if (error)
7571 f8a36e22 2021-08-26 stsp return error;
7572 f8a36e22 2021-08-26 stsp nbranches++;
7573 f8a36e22 2021-08-26 stsp break;
7574 f8a36e22 2021-08-26 stsp case 'd':
7575 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&delete_args, optarg, NULL);
7576 f8a36e22 2021-08-26 stsp if (error)
7577 f8a36e22 2021-08-26 stsp return error;
7578 f8a36e22 2021-08-26 stsp break;
7579 f8a36e22 2021-08-26 stsp case 'f':
7580 f8a36e22 2021-08-26 stsp overwrite_refs = 1;
7581 f8a36e22 2021-08-26 stsp break;
7582 f8a36e22 2021-08-26 stsp case 'r':
7583 f8a36e22 2021-08-26 stsp repo_path = realpath(optarg, NULL);
7584 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7585 f8a36e22 2021-08-26 stsp return got_error_from_errno2("realpath",
7586 f8a36e22 2021-08-26 stsp optarg);
7587 f8a36e22 2021-08-26 stsp got_path_strip_trailing_slashes(repo_path);
7588 f8a36e22 2021-08-26 stsp break;
7589 f8a36e22 2021-08-26 stsp case 't':
7590 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags, optarg, NULL);
7591 f8a36e22 2021-08-26 stsp if (error)
7592 f8a36e22 2021-08-26 stsp return error;
7593 f8a36e22 2021-08-26 stsp ntags++;
7594 f8a36e22 2021-08-26 stsp break;
7595 f8a36e22 2021-08-26 stsp case 'T':
7596 f8a36e22 2021-08-26 stsp send_all_tags = 1;
7597 f8a36e22 2021-08-26 stsp break;
7598 f8a36e22 2021-08-26 stsp case 'v':
7599 f8a36e22 2021-08-26 stsp if (verbosity < 0)
7600 f8a36e22 2021-08-26 stsp verbosity = 0;
7601 f8a36e22 2021-08-26 stsp else if (verbosity < 3)
7602 f8a36e22 2021-08-26 stsp verbosity++;
7603 f8a36e22 2021-08-26 stsp break;
7604 f8a36e22 2021-08-26 stsp case 'q':
7605 f8a36e22 2021-08-26 stsp verbosity = -1;
7606 f8a36e22 2021-08-26 stsp break;
7607 f8a36e22 2021-08-26 stsp default:
7608 f8a36e22 2021-08-26 stsp usage_send();
7609 f8a36e22 2021-08-26 stsp /* NOTREACHED */
7610 f8a36e22 2021-08-26 stsp }
7611 f8a36e22 2021-08-26 stsp }
7612 f8a36e22 2021-08-26 stsp argc -= optind;
7613 f8a36e22 2021-08-26 stsp argv += optind;
7614 f8a36e22 2021-08-26 stsp
7615 f8a36e22 2021-08-26 stsp if (send_all_branches && !TAILQ_EMPTY(&branches))
7616 f8a36e22 2021-08-26 stsp option_conflict('a', 'b');
7617 f8a36e22 2021-08-26 stsp if (send_all_tags && !TAILQ_EMPTY(&tags))
7618 f8a36e22 2021-08-26 stsp option_conflict('T', 't');
7619 f8a36e22 2021-08-26 stsp
7620 f8a36e22 2021-08-26 stsp
7621 f8a36e22 2021-08-26 stsp if (argc == 0)
7622 f8a36e22 2021-08-26 stsp remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7623 f8a36e22 2021-08-26 stsp else if (argc == 1)
7624 f8a36e22 2021-08-26 stsp remote_name = argv[0];
7625 f8a36e22 2021-08-26 stsp else
7626 f8a36e22 2021-08-26 stsp usage_send();
7627 f8a36e22 2021-08-26 stsp
7628 f8a36e22 2021-08-26 stsp cwd = getcwd(NULL, 0);
7629 f8a36e22 2021-08-26 stsp if (cwd == NULL) {
7630 f8a36e22 2021-08-26 stsp error = got_error_from_errno("getcwd");
7631 f8a36e22 2021-08-26 stsp goto done;
7632 f8a36e22 2021-08-26 stsp }
7633 f8a36e22 2021-08-26 stsp
7634 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7635 f8a36e22 2021-08-26 stsp error = got_worktree_open(&worktree, cwd);
7636 f8a36e22 2021-08-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7637 f8a36e22 2021-08-26 stsp goto done;
7638 f8a36e22 2021-08-26 stsp else
7639 f8a36e22 2021-08-26 stsp error = NULL;
7640 f8a36e22 2021-08-26 stsp if (worktree) {
7641 f8a36e22 2021-08-26 stsp repo_path =
7642 f8a36e22 2021-08-26 stsp strdup(got_worktree_get_repo_path(worktree));
7643 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7644 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7645 f8a36e22 2021-08-26 stsp if (error)
7646 f8a36e22 2021-08-26 stsp goto done;
7647 f8a36e22 2021-08-26 stsp } else {
7648 f8a36e22 2021-08-26 stsp repo_path = strdup(cwd);
7649 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7650 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7651 f8a36e22 2021-08-26 stsp goto done;
7652 f8a36e22 2021-08-26 stsp }
7653 f8a36e22 2021-08-26 stsp }
7654 f8a36e22 2021-08-26 stsp }
7655 f8a36e22 2021-08-26 stsp
7656 f8a36e22 2021-08-26 stsp error = got_repo_open(&repo, repo_path, NULL);
7657 f8a36e22 2021-08-26 stsp if (error)
7658 f8a36e22 2021-08-26 stsp goto done;
7659 f8a36e22 2021-08-26 stsp
7660 f8a36e22 2021-08-26 stsp if (worktree) {
7661 f8a36e22 2021-08-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
7662 f8a36e22 2021-08-26 stsp if (worktree_conf) {
7663 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7664 f8a36e22 2021-08-26 stsp worktree_conf);
7665 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7666 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7667 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7668 f8a36e22 2021-08-26 stsp break;
7669 f8a36e22 2021-08-26 stsp }
7670 f8a36e22 2021-08-26 stsp }
7671 f8a36e22 2021-08-26 stsp }
7672 f8a36e22 2021-08-26 stsp }
7673 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7674 f8a36e22 2021-08-26 stsp repo_conf = got_repo_get_gotconfig(repo);
7675 f8a36e22 2021-08-26 stsp if (repo_conf) {
7676 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7677 f8a36e22 2021-08-26 stsp repo_conf);
7678 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7679 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7680 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7681 f8a36e22 2021-08-26 stsp break;
7682 f8a36e22 2021-08-26 stsp }
7683 f8a36e22 2021-08-26 stsp }
7684 f8a36e22 2021-08-26 stsp }
7685 f8a36e22 2021-08-26 stsp }
7686 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7687 f8a36e22 2021-08-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7688 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7689 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7690 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7691 f8a36e22 2021-08-26 stsp break;
7692 f8a36e22 2021-08-26 stsp }
7693 f8a36e22 2021-08-26 stsp }
7694 f8a36e22 2021-08-26 stsp }
7695 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7696 f8a36e22 2021-08-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7697 f8a36e22 2021-08-26 stsp goto done;
7698 f8a36e22 2021-08-26 stsp }
7699 f8a36e22 2021-08-26 stsp
7700 f8a36e22 2021-08-26 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
7701 f8a36e22 2021-08-26 stsp &repo_name, remote->url);
7702 f8a36e22 2021-08-26 stsp if (error)
7703 f8a36e22 2021-08-26 stsp goto done;
7704 f8a36e22 2021-08-26 stsp
7705 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git") == 0) {
7706 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7707 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7708 f8a36e22 2021-08-26 stsp "sendfd dns inet unveil", NULL) == -1)
7709 f8a36e22 2021-08-26 stsp err(1, "pledge");
7710 f8a36e22 2021-08-26 stsp #endif
7711 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
7712 f8a36e22 2021-08-26 stsp strcmp(proto, "ssh") == 0) {
7713 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7714 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7715 f8a36e22 2021-08-26 stsp "sendfd unveil", NULL) == -1)
7716 f8a36e22 2021-08-26 stsp err(1, "pledge");
7717 f8a36e22 2021-08-26 stsp #endif
7718 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "http") == 0 ||
7719 f8a36e22 2021-08-26 stsp strcmp(proto, "git+http") == 0) {
7720 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7721 f8a36e22 2021-08-26 stsp goto done;
7722 f8a36e22 2021-08-26 stsp } else {
7723 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7724 f8a36e22 2021-08-26 stsp goto done;
7725 f8a36e22 2021-08-26 stsp }
7726 f8a36e22 2021-08-26 stsp
7727 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
7728 f8a36e22 2021-08-26 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
7729 f8a36e22 2021-08-26 stsp error = got_error_from_errno2("unveil",
7730 f8a36e22 2021-08-26 stsp GOT_FETCH_PATH_SSH);
7731 f8a36e22 2021-08-26 stsp goto done;
7732 f8a36e22 2021-08-26 stsp }
7733 f8a36e22 2021-08-26 stsp }
7734 f8a36e22 2021-08-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7735 f8a36e22 2021-08-26 stsp if (error)
7736 f8a36e22 2021-08-26 stsp goto done;
7737 f8a36e22 2021-08-26 stsp
7738 f8a36e22 2021-08-26 stsp if (send_all_branches) {
7739 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_branches, repo, "refs/heads",
7740 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7741 f8a36e22 2021-08-26 stsp if (error)
7742 f8a36e22 2021-08-26 stsp goto done;
7743 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_branches, entry) {
7744 f8a36e22 2021-08-26 stsp const char *branchname = got_ref_get_name(re->ref);
7745 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches,
7746 f8a36e22 2021-08-26 stsp branchname, NULL);
7747 f8a36e22 2021-08-26 stsp if (error)
7748 f8a36e22 2021-08-26 stsp goto done;
7749 f8a36e22 2021-08-26 stsp nbranches++;
7750 f8a36e22 2021-08-26 stsp }
7751 f8a36e22 2021-08-26 stsp }
7752 f8a36e22 2021-08-26 stsp
7753 f8a36e22 2021-08-26 stsp if (send_all_tags) {
7754 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_tags, repo, "refs/tags",
7755 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7756 f8a36e22 2021-08-26 stsp if (error)
7757 f8a36e22 2021-08-26 stsp goto done;
7758 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_tags, entry) {
7759 f8a36e22 2021-08-26 stsp const char *tagname = got_ref_get_name(re->ref);
7760 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags,
7761 f8a36e22 2021-08-26 stsp tagname, NULL);
7762 f8a36e22 2021-08-26 stsp if (error)
7763 f8a36e22 2021-08-26 stsp goto done;
7764 f8a36e22 2021-08-26 stsp ntags++;
7765 f8a36e22 2021-08-26 stsp }
7766 f8a36e22 2021-08-26 stsp }
7767 f8a36e22 2021-08-26 stsp
7768 f8a36e22 2021-08-26 stsp /*
7769 f8a36e22 2021-08-26 stsp * To prevent accidents only branches in refs/heads/ can be deleted
7770 f8a36e22 2021-08-26 stsp * with 'got send -d'.
7771 f8a36e22 2021-08-26 stsp * Deleting anything else requires local repository access or Git.
7772 f8a36e22 2021-08-26 stsp */
7773 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_args, entry) {
7774 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7775 f8a36e22 2021-08-26 stsp char *s;
7776 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *new;
7777 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0) {
7778 f8a36e22 2021-08-26 stsp s = strdup(branchname);
7779 f8a36e22 2021-08-26 stsp if (s == NULL) {
7780 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7781 f8a36e22 2021-08-26 stsp goto done;
7782 f8a36e22 2021-08-26 stsp }
7783 f8a36e22 2021-08-26 stsp } else {
7784 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
7785 f8a36e22 2021-08-26 stsp error = got_error_from_errno("asprintf");
7786 f8a36e22 2021-08-26 stsp goto done;
7787 f8a36e22 2021-08-26 stsp }
7788 f8a36e22 2021-08-26 stsp }
7789 f8a36e22 2021-08-26 stsp error = got_pathlist_insert(&new, &delete_branches, s, NULL);
7790 f8a36e22 2021-08-26 stsp if (error || new == NULL /* duplicate */)
7791 f8a36e22 2021-08-26 stsp free(s);
7792 f8a36e22 2021-08-26 stsp if (error)
7793 f8a36e22 2021-08-26 stsp goto done;
7794 f8a36e22 2021-08-26 stsp ndelete_branches++;
7795 f8a36e22 2021-08-26 stsp }
7796 f8a36e22 2021-08-26 stsp
7797 f8a36e22 2021-08-26 stsp if (nbranches == 0 && ndelete_branches == 0) {
7798 f8a36e22 2021-08-26 stsp struct got_reference *head_ref;
7799 f8a36e22 2021-08-26 stsp if (worktree)
7800 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo,
7801 f8a36e22 2021-08-26 stsp got_worktree_get_head_ref_name(worktree), 0);
7802 f8a36e22 2021-08-26 stsp else
7803 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
7804 f8a36e22 2021-08-26 stsp if (error)
7805 f8a36e22 2021-08-26 stsp goto done;
7806 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(head_ref)) {
7807 f8a36e22 2021-08-26 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
7808 f8a36e22 2021-08-26 stsp got_ref_close(head_ref);
7809 f8a36e22 2021-08-26 stsp if (error)
7810 f8a36e22 2021-08-26 stsp goto done;
7811 f8a36e22 2021-08-26 stsp } else
7812 f8a36e22 2021-08-26 stsp ref = head_ref;
7813 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, got_ref_get_name(ref),
7814 f8a36e22 2021-08-26 stsp NULL);
7815 f8a36e22 2021-08-26 stsp if (error)
7816 f8a36e22 2021-08-26 stsp goto done;
7817 f8a36e22 2021-08-26 stsp nbranches++;
7818 f8a36e22 2021-08-26 stsp }
7819 f8a36e22 2021-08-26 stsp
7820 f8a36e22 2021-08-26 stsp if (verbosity >= 0)
7821 f8a36e22 2021-08-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
7822 f8a36e22 2021-08-26 stsp port ? ":" : "", port ? port : "");
7823 f8a36e22 2021-08-26 stsp
7824 f8a36e22 2021-08-26 stsp error = got_send_connect(&sendpid, &sendfd, proto, host, port,
7825 f8a36e22 2021-08-26 stsp server_path, verbosity);
7826 f8a36e22 2021-08-26 stsp if (error)
7827 f8a36e22 2021-08-26 stsp goto done;
7828 f8a36e22 2021-08-26 stsp
7829 f8a36e22 2021-08-26 stsp memset(&spa, 0, sizeof(spa));
7830 f8a36e22 2021-08-26 stsp spa.last_scaled_packsize[0] = '\0';
7831 f8a36e22 2021-08-26 stsp spa.last_p_deltify = -1;
7832 f8a36e22 2021-08-26 stsp spa.last_p_written = -1;
7833 f8a36e22 2021-08-26 stsp spa.verbosity = verbosity;
7834 f8a36e22 2021-08-26 stsp spa.delete_branches = &delete_branches;
7835 f8a36e22 2021-08-26 stsp error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
7836 f8a36e22 2021-08-26 stsp verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
7837 f8a36e22 2021-08-26 stsp check_cancelled, NULL);
7838 f8a36e22 2021-08-26 stsp if (spa.printed_something)
7839 f8a36e22 2021-08-26 stsp putchar('\n');
7840 f8a36e22 2021-08-26 stsp if (error)
7841 f8a36e22 2021-08-26 stsp goto done;
7842 f8a36e22 2021-08-26 stsp if (!spa.sent_something && verbosity >= 0)
7843 f8a36e22 2021-08-26 stsp printf("Already up-to-date\n");
7844 f8a36e22 2021-08-26 stsp done:
7845 f8a36e22 2021-08-26 stsp if (sendpid > 0) {
7846 f8a36e22 2021-08-26 stsp if (kill(sendpid, SIGTERM) == -1)
7847 f8a36e22 2021-08-26 stsp error = got_error_from_errno("kill");
7848 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
7849 f8a36e22 2021-08-26 stsp error = got_error_from_errno("waitpid");
7850 f8a36e22 2021-08-26 stsp }
7851 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
7852 f8a36e22 2021-08-26 stsp error = got_error_from_errno("close");
7853 f8a36e22 2021-08-26 stsp if (repo) {
7854 f8a36e22 2021-08-26 stsp const struct got_error *close_err = got_repo_close(repo);
7855 f8a36e22 2021-08-26 stsp if (error == NULL)
7856 f8a36e22 2021-08-26 stsp error = close_err;
7857 f8a36e22 2021-08-26 stsp }
7858 f8a36e22 2021-08-26 stsp if (worktree)
7859 f8a36e22 2021-08-26 stsp got_worktree_close(worktree);
7860 f8a36e22 2021-08-26 stsp if (ref)
7861 f8a36e22 2021-08-26 stsp got_ref_close(ref);
7862 f8a36e22 2021-08-26 stsp got_pathlist_free(&branches);
7863 f8a36e22 2021-08-26 stsp got_pathlist_free(&tags);
7864 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_branches);
7865 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_tags);
7866 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_args);
7867 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_branches, entry)
7868 f8a36e22 2021-08-26 stsp free((char *)pe->path);
7869 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_branches);
7870 f8a36e22 2021-08-26 stsp free(cwd);
7871 f8a36e22 2021-08-26 stsp free(repo_path);
7872 f8a36e22 2021-08-26 stsp free(proto);
7873 f8a36e22 2021-08-26 stsp free(host);
7874 f8a36e22 2021-08-26 stsp free(port);
7875 f8a36e22 2021-08-26 stsp free(server_path);
7876 f8a36e22 2021-08-26 stsp free(repo_name);
7877 234035bc 2019-06-01 stsp return error;
7878 234035bc 2019-06-01 stsp }
7879 234035bc 2019-06-01 stsp
7880 234035bc 2019-06-01 stsp __dead static void
7881 234035bc 2019-06-01 stsp usage_cherrypick(void)
7882 234035bc 2019-06-01 stsp {
7883 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7884 234035bc 2019-06-01 stsp exit(1);
7885 234035bc 2019-06-01 stsp }
7886 234035bc 2019-06-01 stsp
7887 234035bc 2019-06-01 stsp static const struct got_error *
7888 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
7889 234035bc 2019-06-01 stsp {
7890 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
7891 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
7892 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
7893 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
7894 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
7895 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
7896 234035bc 2019-06-01 stsp struct got_object_qid *pid;
7897 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
7898 9627c110 2020-04-18 stsp int ch;
7899 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7900 234035bc 2019-06-01 stsp
7901 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7902 234035bc 2019-06-01 stsp switch (ch) {
7903 234035bc 2019-06-01 stsp default:
7904 234035bc 2019-06-01 stsp usage_cherrypick();
7905 234035bc 2019-06-01 stsp /* NOTREACHED */
7906 234035bc 2019-06-01 stsp }
7907 234035bc 2019-06-01 stsp }
7908 234035bc 2019-06-01 stsp
7909 234035bc 2019-06-01 stsp argc -= optind;
7910 234035bc 2019-06-01 stsp argv += optind;
7911 234035bc 2019-06-01 stsp
7912 43012d58 2019-07-14 stsp #ifndef PROFILE
7913 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7914 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7915 43012d58 2019-07-14 stsp err(1, "pledge");
7916 43012d58 2019-07-14 stsp #endif
7917 234035bc 2019-06-01 stsp if (argc != 1)
7918 234035bc 2019-06-01 stsp usage_cherrypick();
7919 234035bc 2019-06-01 stsp
7920 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7921 234035bc 2019-06-01 stsp if (cwd == NULL) {
7922 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7923 234035bc 2019-06-01 stsp goto done;
7924 234035bc 2019-06-01 stsp }
7925 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7926 fa51e947 2020-03-27 stsp if (error) {
7927 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7928 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7929 fa51e947 2020-03-27 stsp cwd);
7930 234035bc 2019-06-01 stsp goto done;
7931 fa51e947 2020-03-27 stsp }
7932 234035bc 2019-06-01 stsp
7933 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7934 c9956ddf 2019-09-08 stsp NULL);
7935 234035bc 2019-06-01 stsp if (error != NULL)
7936 234035bc 2019-06-01 stsp goto done;
7937 234035bc 2019-06-01 stsp
7938 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7939 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7940 234035bc 2019-06-01 stsp if (error)
7941 234035bc 2019-06-01 stsp goto done;
7942 234035bc 2019-06-01 stsp
7943 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7944 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7945 234035bc 2019-06-01 stsp if (error != NULL) {
7946 234035bc 2019-06-01 stsp struct got_reference *ref;
7947 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7948 234035bc 2019-06-01 stsp goto done;
7949 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7950 234035bc 2019-06-01 stsp if (error != NULL)
7951 234035bc 2019-06-01 stsp goto done;
7952 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7953 234035bc 2019-06-01 stsp got_ref_close(ref);
7954 234035bc 2019-06-01 stsp if (error != NULL)
7955 234035bc 2019-06-01 stsp goto done;
7956 234035bc 2019-06-01 stsp }
7957 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7958 234035bc 2019-06-01 stsp if (error)
7959 234035bc 2019-06-01 stsp goto done;
7960 234035bc 2019-06-01 stsp
7961 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
7962 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
7963 234035bc 2019-06-01 stsp if (error != NULL)
7964 234035bc 2019-06-01 stsp goto done;
7965 234035bc 2019-06-01 stsp
7966 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7967 234035bc 2019-06-01 stsp if (error) {
7968 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
7969 234035bc 2019-06-01 stsp goto done;
7970 234035bc 2019-06-01 stsp error = NULL;
7971 234035bc 2019-06-01 stsp } else {
7972 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
7973 234035bc 2019-06-01 stsp goto done;
7974 234035bc 2019-06-01 stsp }
7975 234035bc 2019-06-01 stsp
7976 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7977 234035bc 2019-06-01 stsp if (error)
7978 234035bc 2019-06-01 stsp goto done;
7979 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7980 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7981 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7982 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7983 03415a1a 2019-06-02 stsp NULL);
7984 234035bc 2019-06-01 stsp if (error != NULL)
7985 234035bc 2019-06-01 stsp goto done;
7986 234035bc 2019-06-01 stsp
7987 9627c110 2020-04-18 stsp if (upa.did_something)
7988 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7989 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7990 234035bc 2019-06-01 stsp done:
7991 234035bc 2019-06-01 stsp if (commit)
7992 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7993 234035bc 2019-06-01 stsp free(commit_id_str);
7994 234035bc 2019-06-01 stsp if (head_ref)
7995 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7996 234035bc 2019-06-01 stsp if (worktree)
7997 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7998 1d0f4054 2021-06-17 stsp if (repo) {
7999 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8000 1d0f4054 2021-06-17 stsp if (error == NULL)
8001 1d0f4054 2021-06-17 stsp error = close_err;
8002 1d0f4054 2021-06-17 stsp }
8003 c4296144 2019-05-09 stsp return error;
8004 c4296144 2019-05-09 stsp }
8005 5ef14e63 2019-06-02 stsp
8006 5ef14e63 2019-06-02 stsp __dead static void
8007 5ef14e63 2019-06-02 stsp usage_backout(void)
8008 5ef14e63 2019-06-02 stsp {
8009 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8010 5ef14e63 2019-06-02 stsp exit(1);
8011 5ef14e63 2019-06-02 stsp }
8012 5ef14e63 2019-06-02 stsp
8013 5ef14e63 2019-06-02 stsp static const struct got_error *
8014 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
8015 5ef14e63 2019-06-02 stsp {
8016 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
8017 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
8018 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
8019 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
8020 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
8021 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
8022 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
8023 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
8024 9627c110 2020-04-18 stsp int ch;
8025 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8026 5ef14e63 2019-06-02 stsp
8027 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8028 5ef14e63 2019-06-02 stsp switch (ch) {
8029 5ef14e63 2019-06-02 stsp default:
8030 5ef14e63 2019-06-02 stsp usage_backout();
8031 5ef14e63 2019-06-02 stsp /* NOTREACHED */
8032 5ef14e63 2019-06-02 stsp }
8033 5ef14e63 2019-06-02 stsp }
8034 5ef14e63 2019-06-02 stsp
8035 5ef14e63 2019-06-02 stsp argc -= optind;
8036 5ef14e63 2019-06-02 stsp argv += optind;
8037 5ef14e63 2019-06-02 stsp
8038 43012d58 2019-07-14 stsp #ifndef PROFILE
8039 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8040 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8041 43012d58 2019-07-14 stsp err(1, "pledge");
8042 43012d58 2019-07-14 stsp #endif
8043 5ef14e63 2019-06-02 stsp if (argc != 1)
8044 5ef14e63 2019-06-02 stsp usage_backout();
8045 5ef14e63 2019-06-02 stsp
8046 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
8047 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
8048 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
8049 5ef14e63 2019-06-02 stsp goto done;
8050 5ef14e63 2019-06-02 stsp }
8051 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
8052 fa51e947 2020-03-27 stsp if (error) {
8053 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8054 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
8055 5ef14e63 2019-06-02 stsp goto done;
8056 fa51e947 2020-03-27 stsp }
8057 5ef14e63 2019-06-02 stsp
8058 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8059 c9956ddf 2019-09-08 stsp NULL);
8060 5ef14e63 2019-06-02 stsp if (error != NULL)
8061 5ef14e63 2019-06-02 stsp goto done;
8062 5ef14e63 2019-06-02 stsp
8063 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8064 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8065 5ef14e63 2019-06-02 stsp if (error)
8066 5ef14e63 2019-06-02 stsp goto done;
8067 5ef14e63 2019-06-02 stsp
8068 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8069 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8070 5ef14e63 2019-06-02 stsp if (error != NULL) {
8071 5ef14e63 2019-06-02 stsp struct got_reference *ref;
8072 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8073 5ef14e63 2019-06-02 stsp goto done;
8074 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8075 5ef14e63 2019-06-02 stsp if (error != NULL)
8076 5ef14e63 2019-06-02 stsp goto done;
8077 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
8078 5ef14e63 2019-06-02 stsp got_ref_close(ref);
8079 5ef14e63 2019-06-02 stsp if (error != NULL)
8080 5ef14e63 2019-06-02 stsp goto done;
8081 5ef14e63 2019-06-02 stsp }
8082 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
8083 5ef14e63 2019-06-02 stsp if (error)
8084 5ef14e63 2019-06-02 stsp goto done;
8085 5ef14e63 2019-06-02 stsp
8086 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
8087 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
8088 5ef14e63 2019-06-02 stsp if (error != NULL)
8089 5ef14e63 2019-06-02 stsp goto done;
8090 5ef14e63 2019-06-02 stsp
8091 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
8092 5ef14e63 2019-06-02 stsp if (error)
8093 5ef14e63 2019-06-02 stsp goto done;
8094 5ef14e63 2019-06-02 stsp
8095 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8096 5ef14e63 2019-06-02 stsp if (error)
8097 5ef14e63 2019-06-02 stsp goto done;
8098 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8099 5ef14e63 2019-06-02 stsp if (pid == NULL) {
8100 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
8101 5ef14e63 2019-06-02 stsp goto done;
8102 5ef14e63 2019-06-02 stsp }
8103 5ef14e63 2019-06-02 stsp
8104 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8105 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
8106 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8107 5ef14e63 2019-06-02 stsp if (error != NULL)
8108 5ef14e63 2019-06-02 stsp goto done;
8109 5ef14e63 2019-06-02 stsp
8110 9627c110 2020-04-18 stsp if (upa.did_something)
8111 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
8112 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8113 5ef14e63 2019-06-02 stsp done:
8114 5ef14e63 2019-06-02 stsp if (commit)
8115 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
8116 5ef14e63 2019-06-02 stsp free(commit_id_str);
8117 5ef14e63 2019-06-02 stsp if (head_ref)
8118 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
8119 818c7501 2019-07-11 stsp if (worktree)
8120 818c7501 2019-07-11 stsp got_worktree_close(worktree);
8121 1d0f4054 2021-06-17 stsp if (repo) {
8122 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8123 1d0f4054 2021-06-17 stsp if (error == NULL)
8124 1d0f4054 2021-06-17 stsp error = close_err;
8125 1d0f4054 2021-06-17 stsp }
8126 818c7501 2019-07-11 stsp return error;
8127 818c7501 2019-07-11 stsp }
8128 818c7501 2019-07-11 stsp
8129 818c7501 2019-07-11 stsp __dead static void
8130 818c7501 2019-07-11 stsp usage_rebase(void)
8131 818c7501 2019-07-11 stsp {
8132 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8133 af54c8f8 2019-07-11 stsp getprogname());
8134 818c7501 2019-07-11 stsp exit(1);
8135 0ebf8283 2019-07-24 stsp }
8136 0ebf8283 2019-07-24 stsp
8137 0ebf8283 2019-07-24 stsp void
8138 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
8139 0ebf8283 2019-07-24 stsp {
8140 0ebf8283 2019-07-24 stsp char *nl;
8141 0ebf8283 2019-07-24 stsp size_t len;
8142 0ebf8283 2019-07-24 stsp
8143 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
8144 0ebf8283 2019-07-24 stsp if (len > limit)
8145 0ebf8283 2019-07-24 stsp len = limit;
8146 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
8147 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
8148 0ebf8283 2019-07-24 stsp if (nl)
8149 0ebf8283 2019-07-24 stsp *nl = '\0';
8150 0ebf8283 2019-07-24 stsp }
8151 0ebf8283 2019-07-24 stsp
8152 0ebf8283 2019-07-24 stsp static const struct got_error *
8153 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8154 0ebf8283 2019-07-24 stsp {
8155 5943eee2 2019-08-13 stsp const struct got_error *err;
8156 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
8157 5943eee2 2019-08-13 stsp const char *s;
8158 0ebf8283 2019-07-24 stsp
8159 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8160 5943eee2 2019-08-13 stsp if (err)
8161 5943eee2 2019-08-13 stsp return err;
8162 0ebf8283 2019-07-24 stsp
8163 5943eee2 2019-08-13 stsp s = logmsg0;
8164 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
8165 5943eee2 2019-08-13 stsp s++;
8166 5943eee2 2019-08-13 stsp
8167 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
8168 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
8169 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
8170 5943eee2 2019-08-13 stsp goto done;
8171 5943eee2 2019-08-13 stsp }
8172 0ebf8283 2019-07-24 stsp
8173 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
8174 5943eee2 2019-08-13 stsp done:
8175 5943eee2 2019-08-13 stsp free(logmsg0);
8176 a0ea4fc0 2020-02-28 stsp return err;
8177 a0ea4fc0 2020-02-28 stsp }
8178 a0ea4fc0 2020-02-28 stsp
8179 a0ea4fc0 2020-02-28 stsp static const struct got_error *
8180 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8181 a0ea4fc0 2020-02-28 stsp {
8182 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
8183 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
8184 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
8185 a0ea4fc0 2020-02-28 stsp
8186 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
8187 a0ea4fc0 2020-02-28 stsp if (err)
8188 a0ea4fc0 2020-02-28 stsp return err;
8189 a0ea4fc0 2020-02-28 stsp
8190 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
8191 a0ea4fc0 2020-02-28 stsp if (err)
8192 a0ea4fc0 2020-02-28 stsp goto done;
8193 a0ea4fc0 2020-02-28 stsp
8194 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
8195 a0ea4fc0 2020-02-28 stsp
8196 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
8197 a0ea4fc0 2020-02-28 stsp if (err)
8198 a0ea4fc0 2020-02-28 stsp goto done;
8199 a0ea4fc0 2020-02-28 stsp
8200 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
8201 a0ea4fc0 2020-02-28 stsp done:
8202 a0ea4fc0 2020-02-28 stsp free(id_str);
8203 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
8204 a0ea4fc0 2020-02-28 stsp free(logmsg);
8205 5943eee2 2019-08-13 stsp return err;
8206 818c7501 2019-07-11 stsp }
8207 818c7501 2019-07-11 stsp
8208 818c7501 2019-07-11 stsp static const struct got_error *
8209 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
8210 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
8211 818c7501 2019-07-11 stsp {
8212 818c7501 2019-07-11 stsp const struct got_error *err;
8213 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8214 818c7501 2019-07-11 stsp
8215 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
8216 818c7501 2019-07-11 stsp if (err)
8217 818c7501 2019-07-11 stsp goto done;
8218 818c7501 2019-07-11 stsp
8219 ff0d2220 2019-07-11 stsp if (new_id) {
8220 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
8221 ff0d2220 2019-07-11 stsp if (err)
8222 ff0d2220 2019-07-11 stsp goto done;
8223 ff0d2220 2019-07-11 stsp }
8224 818c7501 2019-07-11 stsp
8225 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
8226 ff0d2220 2019-07-11 stsp if (new_id_str)
8227 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
8228 0ebf8283 2019-07-24 stsp
8229 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8230 0ebf8283 2019-07-24 stsp if (err)
8231 0ebf8283 2019-07-24 stsp goto done;
8232 0ebf8283 2019-07-24 stsp
8233 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
8234 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8235 818c7501 2019-07-11 stsp done:
8236 818c7501 2019-07-11 stsp free(old_id_str);
8237 818c7501 2019-07-11 stsp free(new_id_str);
8238 272a1371 2020-02-28 stsp free(logmsg);
8239 818c7501 2019-07-11 stsp return err;
8240 818c7501 2019-07-11 stsp }
8241 818c7501 2019-07-11 stsp
8242 1ee397ad 2019-07-12 stsp static const struct got_error *
8243 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8244 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
8245 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
8246 e600f124 2021-03-21 stsp int create_backup)
8247 818c7501 2019-07-11 stsp {
8248 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
8249 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
8250 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
8251 818c7501 2019-07-11 stsp }
8252 818c7501 2019-07-11 stsp
8253 818c7501 2019-07-11 stsp static const struct got_error *
8254 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
8255 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8256 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
8257 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8258 ff0d2220 2019-07-11 stsp {
8259 ff0d2220 2019-07-11 stsp const struct got_error *error;
8260 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
8261 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
8262 ff0d2220 2019-07-11 stsp
8263 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8264 ff0d2220 2019-07-11 stsp if (error)
8265 ff0d2220 2019-07-11 stsp return error;
8266 ff0d2220 2019-07-11 stsp
8267 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8268 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
8269 ff0d2220 2019-07-11 stsp if (error) {
8270 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8271 ff0d2220 2019-07-11 stsp goto done;
8272 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
8273 ff0d2220 2019-07-11 stsp } else {
8274 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
8275 ff0d2220 2019-07-11 stsp free(new_commit_id);
8276 ff0d2220 2019-07-11 stsp }
8277 ff0d2220 2019-07-11 stsp done:
8278 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
8279 ff0d2220 2019-07-11 stsp return error;
8280 64c6d990 2019-07-11 stsp }
8281 64c6d990 2019-07-11 stsp
8282 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
8283 64c6d990 2019-07-11 stsp const char *path_prefix;
8284 64c6d990 2019-07-11 stsp size_t len;
8285 8ca9bd68 2019-07-25 stsp int errcode;
8286 64c6d990 2019-07-11 stsp };
8287 64c6d990 2019-07-11 stsp
8288 64c6d990 2019-07-11 stsp static const struct got_error *
8289 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8290 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
8291 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
8292 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
8293 64c6d990 2019-07-11 stsp {
8294 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
8295 64c6d990 2019-07-11 stsp
8296 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8297 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8298 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
8299 64c6d990 2019-07-11 stsp
8300 64c6d990 2019-07-11 stsp return NULL;
8301 64c6d990 2019-07-11 stsp }
8302 64c6d990 2019-07-11 stsp
8303 64c6d990 2019-07-11 stsp static const struct got_error *
8304 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
8305 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
8306 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
8307 64c6d990 2019-07-11 stsp {
8308 64c6d990 2019-07-11 stsp const struct got_error *err;
8309 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8310 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
8311 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
8312 64c6d990 2019-07-11 stsp
8313 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
8314 64c6d990 2019-07-11 stsp return NULL;
8315 64c6d990 2019-07-11 stsp
8316 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8317 64c6d990 2019-07-11 stsp if (err)
8318 64c6d990 2019-07-11 stsp goto done;
8319 64c6d990 2019-07-11 stsp
8320 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8321 64c6d990 2019-07-11 stsp if (err)
8322 64c6d990 2019-07-11 stsp goto done;
8323 64c6d990 2019-07-11 stsp
8324 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
8325 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
8326 64c6d990 2019-07-11 stsp if (err)
8327 64c6d990 2019-07-11 stsp goto done;
8328 64c6d990 2019-07-11 stsp
8329 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
8330 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
8331 64c6d990 2019-07-11 stsp if (err)
8332 64c6d990 2019-07-11 stsp goto done;
8333 64c6d990 2019-07-11 stsp
8334 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
8335 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
8336 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
8337 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
8338 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
8339 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
8340 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
8341 64c6d990 2019-07-11 stsp done:
8342 64c6d990 2019-07-11 stsp if (tree1)
8343 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
8344 64c6d990 2019-07-11 stsp if (tree2)
8345 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
8346 64c6d990 2019-07-11 stsp if (commit)
8347 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
8348 64c6d990 2019-07-11 stsp if (parent_commit)
8349 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
8350 64c6d990 2019-07-11 stsp return err;
8351 ff0d2220 2019-07-11 stsp }
8352 ff0d2220 2019-07-11 stsp
8353 ff0d2220 2019-07-11 stsp static const struct got_error *
8354 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
8355 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
8356 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8357 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
8358 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
8359 af61c510 2019-07-19 stsp {
8360 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
8361 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
8362 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
8363 af61c510 2019-07-19 stsp struct got_object_qid *qid;
8364 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
8365 af61c510 2019-07-19 stsp
8366 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
8367 af61c510 2019-07-19 stsp if (err)
8368 af61c510 2019-07-19 stsp return err;
8369 af61c510 2019-07-19 stsp
8370 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8371 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8372 af61c510 2019-07-19 stsp if (err)
8373 af61c510 2019-07-19 stsp goto done;
8374 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8375 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
8376 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
8377 af61c510 2019-07-19 stsp if (err) {
8378 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
8379 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
8380 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
8381 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
8382 af61c510 2019-07-19 stsp "been reached?!?");
8383 ee780d5c 2020-01-04 stsp }
8384 ee780d5c 2020-01-04 stsp goto done;
8385 af61c510 2019-07-19 stsp } else {
8386 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
8387 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
8388 af61c510 2019-07-19 stsp if (err)
8389 af61c510 2019-07-19 stsp goto done;
8390 af61c510 2019-07-19 stsp
8391 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
8392 af61c510 2019-07-19 stsp if (err)
8393 af61c510 2019-07-19 stsp goto done;
8394 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
8395 af61c510 2019-07-19 stsp commit_id = parent_id;
8396 af61c510 2019-07-19 stsp }
8397 af61c510 2019-07-19 stsp }
8398 af61c510 2019-07-19 stsp done:
8399 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
8400 af61c510 2019-07-19 stsp return err;
8401 e600f124 2021-03-21 stsp }
8402 e600f124 2021-03-21 stsp
8403 e600f124 2021-03-21 stsp static const struct got_error *
8404 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8405 e600f124 2021-03-21 stsp {
8406 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8407 e600f124 2021-03-21 stsp time_t committer_time;
8408 e600f124 2021-03-21 stsp struct tm tm;
8409 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
8410 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
8411 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
8412 e600f124 2021-03-21 stsp
8413 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
8414 e600f124 2021-03-21 stsp if (localtime_r(&committer_time, &tm) == NULL)
8415 e600f124 2021-03-21 stsp return got_error_from_errno("localtime_r");
8416 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8417 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
8418 e600f124 2021-03-21 stsp
8419 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
8420 e600f124 2021-03-21 stsp if (author0 == NULL)
8421 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
8422 e600f124 2021-03-21 stsp author = author0;
8423 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
8424 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
8425 e600f124 2021-03-21 stsp author = smallerthan + 1;
8426 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
8427 e600f124 2021-03-21 stsp
8428 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8429 e600f124 2021-03-21 stsp if (err)
8430 e600f124 2021-03-21 stsp goto done;
8431 e600f124 2021-03-21 stsp logmsg = logmsg0;
8432 e600f124 2021-03-21 stsp while (*logmsg == '\n')
8433 e600f124 2021-03-21 stsp logmsg++;
8434 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
8435 e600f124 2021-03-21 stsp if (newline)
8436 e600f124 2021-03-21 stsp *newline = '\0';
8437 e600f124 2021-03-21 stsp
8438 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
8439 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
8440 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
8441 e600f124 2021-03-21 stsp done:
8442 e600f124 2021-03-21 stsp free(author0);
8443 e600f124 2021-03-21 stsp free(logmsg0);
8444 643b85bc 2021-07-16 stsp return err;
8445 643b85bc 2021-07-16 stsp }
8446 643b85bc 2021-07-16 stsp
8447 643b85bc 2021-07-16 stsp static const struct got_error *
8448 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8449 643b85bc 2021-07-16 stsp struct got_repository *repo)
8450 643b85bc 2021-07-16 stsp {
8451 643b85bc 2021-07-16 stsp const struct got_error *err;
8452 643b85bc 2021-07-16 stsp char *id_str;
8453 643b85bc 2021-07-16 stsp
8454 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
8455 643b85bc 2021-07-16 stsp if (err)
8456 643b85bc 2021-07-16 stsp return err;
8457 643b85bc 2021-07-16 stsp
8458 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
8459 643b85bc 2021-07-16 stsp if (err)
8460 643b85bc 2021-07-16 stsp goto done;
8461 643b85bc 2021-07-16 stsp
8462 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8463 643b85bc 2021-07-16 stsp done:
8464 643b85bc 2021-07-16 stsp free(id_str);
8465 e600f124 2021-03-21 stsp return err;
8466 e600f124 2021-03-21 stsp }
8467 e600f124 2021-03-21 stsp
8468 e600f124 2021-03-21 stsp static const struct got_error *
8469 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
8470 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8471 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
8472 e600f124 2021-03-21 stsp struct got_repository *repo)
8473 e600f124 2021-03-21 stsp {
8474 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8475 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
8476 e600f124 2021-03-21 stsp char *refs_str = NULL;
8477 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
8478 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
8479 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
8480 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
8481 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
8482 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
8483 e600f124 2021-03-21 stsp char *custom_refs_str;
8484 e600f124 2021-03-21 stsp
8485 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8486 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
8487 e600f124 2021-03-21 stsp
8488 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8489 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
8490 e600f124 2021-03-21 stsp if (err)
8491 e600f124 2021-03-21 stsp goto done;
8492 e600f124 2021-03-21 stsp
8493 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8494 e600f124 2021-03-21 stsp if (err)
8495 e600f124 2021-03-21 stsp goto done;
8496 e600f124 2021-03-21 stsp
8497 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8498 e600f124 2021-03-21 stsp if (refs) {
8499 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8500 e600f124 2021-03-21 stsp if (err)
8501 e600f124 2021-03-21 stsp goto done;
8502 e600f124 2021-03-21 stsp }
8503 e600f124 2021-03-21 stsp
8504 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8505 e600f124 2021-03-21 stsp if (err)
8506 e600f124 2021-03-21 stsp goto done;
8507 e600f124 2021-03-21 stsp
8508 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8509 e600f124 2021-03-21 stsp if (err)
8510 e600f124 2021-03-21 stsp goto done;
8511 e600f124 2021-03-21 stsp
8512 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8513 e600f124 2021-03-21 stsp old_commit_id, new_commit_id, repo, check_cancelled, NULL);
8514 e600f124 2021-03-21 stsp if (err)
8515 e600f124 2021-03-21 stsp goto done;
8516 e600f124 2021-03-21 stsp
8517 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8518 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8519 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
8520 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8521 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
8522 e600f124 2021-03-21 stsp free(refs_str);
8523 e600f124 2021-03-21 stsp refs_str = NULL;
8524 e600f124 2021-03-21 stsp
8525 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8526 e600f124 2021-03-21 stsp if (err)
8527 e600f124 2021-03-21 stsp goto done;
8528 e600f124 2021-03-21 stsp
8529 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
8530 e600f124 2021-03-21 stsp if (err)
8531 e600f124 2021-03-21 stsp goto done;
8532 e600f124 2021-03-21 stsp
8533 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
8534 e600f124 2021-03-21 stsp if (err)
8535 e600f124 2021-03-21 stsp goto done;
8536 e600f124 2021-03-21 stsp
8537 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8538 e600f124 2021-03-21 stsp if (refs) {
8539 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
8540 e600f124 2021-03-21 stsp if (err)
8541 e600f124 2021-03-21 stsp goto done;
8542 e600f124 2021-03-21 stsp }
8543 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
8544 e600f124 2021-03-21 stsp yca_id_str,
8545 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8546 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
8547 e600f124 2021-03-21 stsp }
8548 e600f124 2021-03-21 stsp done:
8549 e600f124 2021-03-21 stsp free(custom_refs_str);
8550 e600f124 2021-03-21 stsp free(new_commit_id);
8551 e600f124 2021-03-21 stsp free(refs_str);
8552 e600f124 2021-03-21 stsp free(yca_id);
8553 e600f124 2021-03-21 stsp free(yca_id_str);
8554 e600f124 2021-03-21 stsp free(yca_brief_str);
8555 e600f124 2021-03-21 stsp if (new_commit)
8556 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
8557 e600f124 2021-03-21 stsp if (yca_commit)
8558 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
8559 e600f124 2021-03-21 stsp
8560 e600f124 2021-03-21 stsp return NULL;
8561 af61c510 2019-07-19 stsp }
8562 af61c510 2019-07-19 stsp
8563 af61c510 2019-07-19 stsp static const struct got_error *
8564 643b85bc 2021-07-16 stsp process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8565 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
8566 e600f124 2021-03-21 stsp {
8567 e600f124 2021-03-21 stsp const struct got_error *err;
8568 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
8569 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
8570 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8571 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
8572 e600f124 2021-03-21 stsp char *branch_name = NULL;
8573 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
8574 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
8575 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
8576 e600f124 2021-03-21 stsp
8577 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
8578 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
8579 e600f124 2021-03-21 stsp
8580 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8581 e600f124 2021-03-21 stsp if (err)
8582 e600f124 2021-03-21 stsp return err;
8583 e600f124 2021-03-21 stsp
8584 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8585 e600f124 2021-03-21 stsp if (err)
8586 e600f124 2021-03-21 stsp goto done;
8587 e600f124 2021-03-21 stsp
8588 e600f124 2021-03-21 stsp if (wanted_branch_name) {
8589 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8590 e600f124 2021-03-21 stsp wanted_branch_name += 11;
8591 e600f124 2021-03-21 stsp }
8592 e600f124 2021-03-21 stsp
8593 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8594 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
8595 e600f124 2021-03-21 stsp if (err)
8596 e600f124 2021-03-21 stsp goto done;
8597 e600f124 2021-03-21 stsp
8598 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
8599 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
8600 e600f124 2021-03-21 stsp char *slash;
8601 e600f124 2021-03-21 stsp
8602 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
8603 643b85bc 2021-07-16 stsp if (err)
8604 643b85bc 2021-07-16 stsp break;
8605 643b85bc 2021-07-16 stsp
8606 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
8607 e600f124 2021-03-21 stsp if (err)
8608 e600f124 2021-03-21 stsp break;
8609 e600f124 2021-03-21 stsp
8610 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
8611 e600f124 2021-03-21 stsp old_commit_id);
8612 e600f124 2021-03-21 stsp if (err)
8613 e600f124 2021-03-21 stsp break;
8614 e600f124 2021-03-21 stsp
8615 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
8616 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
8617 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
8618 e600f124 2021-03-21 stsp
8619 e600f124 2021-03-21 stsp while (refname[0] == '/')
8620 e600f124 2021-03-21 stsp refname++;
8621 e600f124 2021-03-21 stsp
8622 e600f124 2021-03-21 stsp branch_name = strdup(refname);
8623 e600f124 2021-03-21 stsp if (branch_name == NULL) {
8624 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
8625 e600f124 2021-03-21 stsp break;
8626 e600f124 2021-03-21 stsp }
8627 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
8628 e600f124 2021-03-21 stsp if (slash) {
8629 e600f124 2021-03-21 stsp *slash = '\0';
8630 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
8631 e600f124 2021-03-21 stsp }
8632 e600f124 2021-03-21 stsp
8633 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
8634 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
8635 9e822917 2021-03-23 stsp wanted_branch_found = 1;
8636 643b85bc 2021-07-16 stsp if (delete) {
8637 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
8638 643b85bc 2021-07-16 stsp old_commit_id, repo);
8639 643b85bc 2021-07-16 stsp } else {
8640 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
8641 643b85bc 2021-07-16 stsp old_commit_id, old_commit, refs_idmap,
8642 643b85bc 2021-07-16 stsp repo);
8643 643b85bc 2021-07-16 stsp }
8644 e600f124 2021-03-21 stsp if (err)
8645 e600f124 2021-03-21 stsp break;
8646 e600f124 2021-03-21 stsp }
8647 e600f124 2021-03-21 stsp
8648 e600f124 2021-03-21 stsp free(old_commit_id);
8649 e600f124 2021-03-21 stsp old_commit_id = NULL;
8650 e600f124 2021-03-21 stsp free(branch_name);
8651 e600f124 2021-03-21 stsp branch_name = NULL;
8652 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8653 e600f124 2021-03-21 stsp old_commit = NULL;
8654 e600f124 2021-03-21 stsp }
8655 9e822917 2021-03-23 stsp
8656 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
8657 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
8658 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
8659 9e822917 2021-03-23 stsp }
8660 e600f124 2021-03-21 stsp done:
8661 e600f124 2021-03-21 stsp if (refs_idmap)
8662 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
8663 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
8664 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
8665 e600f124 2021-03-21 stsp free(old_commit_id);
8666 e600f124 2021-03-21 stsp free(branch_name);
8667 e600f124 2021-03-21 stsp if (old_commit)
8668 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8669 e600f124 2021-03-21 stsp return err;
8670 e600f124 2021-03-21 stsp }
8671 e600f124 2021-03-21 stsp
8672 e600f124 2021-03-21 stsp static const struct got_error *
8673 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
8674 818c7501 2019-07-11 stsp {
8675 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
8676 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
8677 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
8678 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8679 818c7501 2019-07-11 stsp char *cwd = NULL;
8680 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
8681 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8682 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
8683 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
8684 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8685 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
8686 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8687 e600f124 2021-03-21 stsp int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8688 643b85bc 2021-07-16 stsp int delete_backups = 0;
8689 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8690 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
8691 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
8692 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
8693 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
8694 818c7501 2019-07-11 stsp
8695 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
8696 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
8697 818c7501 2019-07-11 stsp
8698 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
8699 818c7501 2019-07-11 stsp switch (ch) {
8700 818c7501 2019-07-11 stsp case 'a':
8701 818c7501 2019-07-11 stsp abort_rebase = 1;
8702 818c7501 2019-07-11 stsp break;
8703 818c7501 2019-07-11 stsp case 'c':
8704 818c7501 2019-07-11 stsp continue_rebase = 1;
8705 818c7501 2019-07-11 stsp break;
8706 e600f124 2021-03-21 stsp case 'l':
8707 e600f124 2021-03-21 stsp list_backups = 1;
8708 e600f124 2021-03-21 stsp break;
8709 643b85bc 2021-07-16 stsp case 'X':
8710 643b85bc 2021-07-16 stsp delete_backups = 1;
8711 643b85bc 2021-07-16 stsp break;
8712 818c7501 2019-07-11 stsp default:
8713 818c7501 2019-07-11 stsp usage_rebase();
8714 818c7501 2019-07-11 stsp /* NOTREACHED */
8715 818c7501 2019-07-11 stsp }
8716 818c7501 2019-07-11 stsp }
8717 818c7501 2019-07-11 stsp
8718 818c7501 2019-07-11 stsp argc -= optind;
8719 818c7501 2019-07-11 stsp argv += optind;
8720 818c7501 2019-07-11 stsp
8721 43012d58 2019-07-14 stsp #ifndef PROFILE
8722 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8723 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8724 43012d58 2019-07-14 stsp err(1, "pledge");
8725 43012d58 2019-07-14 stsp #endif
8726 e600f124 2021-03-21 stsp if (list_backups) {
8727 e600f124 2021-03-21 stsp if (abort_rebase)
8728 e600f124 2021-03-21 stsp option_conflict('l', 'a');
8729 e600f124 2021-03-21 stsp if (continue_rebase)
8730 e600f124 2021-03-21 stsp option_conflict('l', 'c');
8731 643b85bc 2021-07-16 stsp if (delete_backups)
8732 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8733 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
8734 643b85bc 2021-07-16 stsp usage_rebase();
8735 643b85bc 2021-07-16 stsp } else if (delete_backups) {
8736 643b85bc 2021-07-16 stsp if (abort_rebase)
8737 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
8738 643b85bc 2021-07-16 stsp if (continue_rebase)
8739 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
8740 643b85bc 2021-07-16 stsp if (list_backups)
8741 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8742 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
8743 818c7501 2019-07-11 stsp usage_rebase();
8744 e600f124 2021-03-21 stsp } else {
8745 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
8746 e600f124 2021-03-21 stsp usage_rebase();
8747 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
8748 e600f124 2021-03-21 stsp if (argc != 0)
8749 e600f124 2021-03-21 stsp usage_rebase();
8750 e600f124 2021-03-21 stsp } else if (argc != 1)
8751 e600f124 2021-03-21 stsp usage_rebase();
8752 e600f124 2021-03-21 stsp }
8753 818c7501 2019-07-11 stsp
8754 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
8755 818c7501 2019-07-11 stsp if (cwd == NULL) {
8756 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
8757 818c7501 2019-07-11 stsp goto done;
8758 818c7501 2019-07-11 stsp }
8759 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
8760 fa51e947 2020-03-27 stsp if (error) {
8761 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8762 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
8763 e600f124 2021-03-21 stsp goto done;
8764 e600f124 2021-03-21 stsp } else {
8765 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8766 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
8767 e600f124 2021-03-21 stsp "rebase", cwd);
8768 e600f124 2021-03-21 stsp goto done;
8769 e600f124 2021-03-21 stsp }
8770 fa51e947 2020-03-27 stsp }
8771 818c7501 2019-07-11 stsp
8772 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
8773 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8774 818c7501 2019-07-11 stsp if (error != NULL)
8775 818c7501 2019-07-11 stsp goto done;
8776 818c7501 2019-07-11 stsp
8777 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8778 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
8779 7ef62c4e 2020-02-24 stsp if (error)
8780 7ef62c4e 2020-02-24 stsp goto done;
8781 7ef62c4e 2020-02-24 stsp
8782 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8783 643b85bc 2021-07-16 stsp error = process_backup_refs(
8784 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8785 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
8786 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
8787 e600f124 2021-03-21 stsp }
8788 e600f124 2021-03-21 stsp
8789 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
8790 7ef62c4e 2020-02-24 stsp worktree);
8791 818c7501 2019-07-11 stsp if (error)
8792 7ef62c4e 2020-02-24 stsp goto done;
8793 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
8794 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8795 818c7501 2019-07-11 stsp goto done;
8796 7ef62c4e 2020-02-24 stsp }
8797 818c7501 2019-07-11 stsp
8798 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8799 818c7501 2019-07-11 stsp if (error)
8800 818c7501 2019-07-11 stsp goto done;
8801 818c7501 2019-07-11 stsp
8802 f6794adc 2019-07-23 stsp if (abort_rebase) {
8803 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8804 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8805 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8806 f6794adc 2019-07-23 stsp goto done;
8807 f6794adc 2019-07-23 stsp }
8808 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8809 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8810 3e3a69f1 2019-07-25 stsp worktree, repo);
8811 818c7501 2019-07-11 stsp if (error)
8812 818c7501 2019-07-11 stsp goto done;
8813 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
8814 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
8815 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8816 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
8817 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
8818 818c7501 2019-07-11 stsp if (error)
8819 818c7501 2019-07-11 stsp goto done;
8820 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8821 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8822 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
8823 818c7501 2019-07-11 stsp }
8824 818c7501 2019-07-11 stsp
8825 818c7501 2019-07-11 stsp if (continue_rebase) {
8826 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8827 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8828 f6794adc 2019-07-23 stsp goto done;
8829 f6794adc 2019-07-23 stsp }
8830 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8831 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8832 3e3a69f1 2019-07-25 stsp worktree, repo);
8833 818c7501 2019-07-11 stsp if (error)
8834 818c7501 2019-07-11 stsp goto done;
8835 818c7501 2019-07-11 stsp
8836 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8837 01757395 2019-07-12 stsp resume_commit_id, repo);
8838 818c7501 2019-07-11 stsp if (error)
8839 818c7501 2019-07-11 stsp goto done;
8840 818c7501 2019-07-11 stsp
8841 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
8842 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
8843 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
8844 818c7501 2019-07-11 stsp goto done;
8845 818c7501 2019-07-11 stsp }
8846 818c7501 2019-07-11 stsp } else {
8847 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
8848 818c7501 2019-07-11 stsp if (error != NULL)
8849 818c7501 2019-07-11 stsp goto done;
8850 ff0d2220 2019-07-11 stsp }
8851 818c7501 2019-07-11 stsp
8852 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8853 ff0d2220 2019-07-11 stsp if (error)
8854 ff0d2220 2019-07-11 stsp goto done;
8855 ff0d2220 2019-07-11 stsp
8856 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
8857 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
8858 a51a74b3 2019-07-27 stsp
8859 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
8860 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8861 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
8862 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8863 818c7501 2019-07-11 stsp if (error)
8864 818c7501 2019-07-11 stsp goto done;
8865 818c7501 2019-07-11 stsp if (yca_id == NULL) {
8866 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
8867 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
8868 818c7501 2019-07-11 stsp "with work tree's branch");
8869 818c7501 2019-07-11 stsp goto done;
8870 818c7501 2019-07-11 stsp }
8871 818c7501 2019-07-11 stsp
8872 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
8873 a51a74b3 2019-07-27 stsp if (error) {
8874 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
8875 a51a74b3 2019-07-27 stsp goto done;
8876 a51a74b3 2019-07-27 stsp error = NULL;
8877 a51a74b3 2019-07-27 stsp } else {
8878 df3ed485 2021-01-31 stsp static char msg[128];
8879 df3ed485 2021-01-31 stsp snprintf(msg, sizeof(msg),
8880 df3ed485 2021-01-31 stsp "%s is already based on %s",
8881 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
8882 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
8883 df3ed485 2021-01-31 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8884 a51a74b3 2019-07-27 stsp goto done;
8885 a51a74b3 2019-07-27 stsp }
8886 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
8887 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
8888 818c7501 2019-07-11 stsp if (error)
8889 818c7501 2019-07-11 stsp goto done;
8890 818c7501 2019-07-11 stsp }
8891 818c7501 2019-07-11 stsp
8892 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
8893 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8894 818c7501 2019-07-11 stsp if (error)
8895 818c7501 2019-07-11 stsp goto done;
8896 818c7501 2019-07-11 stsp
8897 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8898 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
8899 fc66b545 2019-08-12 stsp if (pid == NULL) {
8900 fc66b545 2019-08-12 stsp if (!continue_rebase) {
8901 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8902 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8903 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
8904 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
8905 fc66b545 2019-08-12 stsp if (error)
8906 fc66b545 2019-08-12 stsp goto done;
8907 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
8908 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
8909 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8910 9627c110 2020-04-18 stsp
8911 fc66b545 2019-08-12 stsp }
8912 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
8913 fc66b545 2019-08-12 stsp goto done;
8914 fc66b545 2019-08-12 stsp }
8915 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
8916 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
8917 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
8918 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8919 818c7501 2019-07-11 stsp commit = NULL;
8920 818c7501 2019-07-11 stsp if (error)
8921 818c7501 2019-07-11 stsp goto done;
8922 64c6d990 2019-07-11 stsp
8923 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
8924 38b0338b 2019-11-29 stsp if (continue_rebase) {
8925 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
8926 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
8927 e600f124 2021-03-21 stsp create_backup);
8928 38b0338b 2019-11-29 stsp goto done;
8929 38b0338b 2019-11-29 stsp } else {
8930 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
8931 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
8932 38b0338b 2019-11-29 stsp char *id_str;
8933 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
8934 38b0338b 2019-11-29 stsp new_base_branch);
8935 38b0338b 2019-11-29 stsp if (error)
8936 38b0338b 2019-11-29 stsp goto done;
8937 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
8938 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
8939 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
8940 38b0338b 2019-11-29 stsp free(id_str);
8941 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
8942 38b0338b 2019-11-29 stsp new_head_commit_id);
8943 38b0338b 2019-11-29 stsp if (error)
8944 38b0338b 2019-11-29 stsp goto done;
8945 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
8946 e600f124 2021-03-21 stsp create_backup = 0;
8947 38b0338b 2019-11-29 stsp }
8948 818c7501 2019-07-11 stsp }
8949 818c7501 2019-07-11 stsp
8950 818c7501 2019-07-11 stsp pid = NULL;
8951 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
8952 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8953 9627c110 2020-04-18 stsp
8954 818c7501 2019-07-11 stsp commit_id = qid->id;
8955 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
8956 818c7501 2019-07-11 stsp pid = qid;
8957 818c7501 2019-07-11 stsp
8958 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8959 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
8960 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
8961 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8962 818c7501 2019-07-11 stsp if (error)
8963 818c7501 2019-07-11 stsp goto done;
8964 9627c110 2020-04-18 stsp
8965 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8966 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8967 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8968 818c7501 2019-07-11 stsp
8969 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8970 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
8971 a0ea4fc0 2020-02-28 stsp if (error)
8972 a0ea4fc0 2020-02-28 stsp goto done;
8973 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8974 818c7501 2019-07-11 stsp break;
8975 01757395 2019-07-12 stsp }
8976 818c7501 2019-07-11 stsp
8977 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
8978 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
8979 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8980 818c7501 2019-07-11 stsp if (error)
8981 818c7501 2019-07-11 stsp goto done;
8982 818c7501 2019-07-11 stsp }
8983 818c7501 2019-07-11 stsp
8984 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8985 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
8986 818c7501 2019-07-11 stsp if (error)
8987 818c7501 2019-07-11 stsp goto done;
8988 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8989 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
8990 818c7501 2019-07-11 stsp } else
8991 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
8992 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
8993 818c7501 2019-07-11 stsp done:
8994 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
8995 818c7501 2019-07-11 stsp free(branch_head_commit_id);
8996 818c7501 2019-07-11 stsp free(resume_commit_id);
8997 818c7501 2019-07-11 stsp free(yca_id);
8998 818c7501 2019-07-11 stsp if (commit)
8999 818c7501 2019-07-11 stsp got_object_commit_close(commit);
9000 818c7501 2019-07-11 stsp if (branch)
9001 818c7501 2019-07-11 stsp got_ref_close(branch);
9002 818c7501 2019-07-11 stsp if (new_base_branch)
9003 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
9004 818c7501 2019-07-11 stsp if (tmp_branch)
9005 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
9006 5ef14e63 2019-06-02 stsp if (worktree)
9007 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
9008 1d0f4054 2021-06-17 stsp if (repo) {
9009 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9010 1d0f4054 2021-06-17 stsp if (error == NULL)
9011 1d0f4054 2021-06-17 stsp error = close_err;
9012 1d0f4054 2021-06-17 stsp }
9013 5ef14e63 2019-06-02 stsp return error;
9014 0ebf8283 2019-07-24 stsp }
9015 0ebf8283 2019-07-24 stsp
9016 0ebf8283 2019-07-24 stsp __dead static void
9017 0ebf8283 2019-07-24 stsp usage_histedit(void)
9018 0ebf8283 2019-07-24 stsp {
9019 e600f124 2021-03-21 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
9020 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9021 643b85bc 2021-07-16 stsp getprogname());
9022 0ebf8283 2019-07-24 stsp exit(1);
9023 0ebf8283 2019-07-24 stsp }
9024 0ebf8283 2019-07-24 stsp
9025 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
9026 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
9027 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
9028 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
9029 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
9030 0ebf8283 2019-07-24 stsp
9031 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
9032 0ebf8283 2019-07-24 stsp unsigned char code;
9033 0ebf8283 2019-07-24 stsp const char *name;
9034 0ebf8283 2019-07-24 stsp const char *desc;
9035 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
9036 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
9037 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9038 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9039 82997472 2020-01-29 stsp "be used" },
9040 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9041 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
9042 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
9043 0ebf8283 2019-07-24 stsp };
9044 0ebf8283 2019-07-24 stsp
9045 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
9046 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
9047 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
9048 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9049 0ebf8283 2019-07-24 stsp char *logmsg;
9050 0ebf8283 2019-07-24 stsp };
9051 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9052 0ebf8283 2019-07-24 stsp
9053 0ebf8283 2019-07-24 stsp static const struct got_error *
9054 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9055 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9056 0ebf8283 2019-07-24 stsp {
9057 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9058 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
9059 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9060 8138f3e1 2019-08-11 stsp int n;
9061 0ebf8283 2019-07-24 stsp
9062 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
9063 0ebf8283 2019-07-24 stsp if (err)
9064 0ebf8283 2019-07-24 stsp goto done;
9065 0ebf8283 2019-07-24 stsp
9066 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
9067 0ebf8283 2019-07-24 stsp if (err)
9068 0ebf8283 2019-07-24 stsp goto done;
9069 0ebf8283 2019-07-24 stsp
9070 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
9071 0ebf8283 2019-07-24 stsp if (err)
9072 0ebf8283 2019-07-24 stsp goto done;
9073 0ebf8283 2019-07-24 stsp
9074 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9075 0ebf8283 2019-07-24 stsp if (n < 0)
9076 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9077 0ebf8283 2019-07-24 stsp done:
9078 0ebf8283 2019-07-24 stsp if (commit)
9079 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9080 0ebf8283 2019-07-24 stsp free(id_str);
9081 0ebf8283 2019-07-24 stsp free(logmsg);
9082 0ebf8283 2019-07-24 stsp return err;
9083 0ebf8283 2019-07-24 stsp }
9084 0ebf8283 2019-07-24 stsp
9085 0ebf8283 2019-07-24 stsp static const struct got_error *
9086 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
9087 466785b9 2020-12-10 jrick FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
9088 0ebf8283 2019-07-24 stsp {
9089 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9090 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
9091 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
9092 0ebf8283 2019-07-24 stsp
9093 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9094 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9095 0ebf8283 2019-07-24 stsp
9096 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9097 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
9098 dbdddfee 2021-06-23 naddy if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9099 466785b9 2020-12-10 jrick histedit_cmd = "fold";
9100 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9101 0ebf8283 2019-07-24 stsp if (err)
9102 0ebf8283 2019-07-24 stsp break;
9103 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
9104 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9105 083957f4 2020-02-24 stsp if (n < 0) {
9106 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
9107 083957f4 2020-02-24 stsp break;
9108 083957f4 2020-02-24 stsp }
9109 083957f4 2020-02-24 stsp }
9110 0ebf8283 2019-07-24 stsp }
9111 0ebf8283 2019-07-24 stsp
9112 0ebf8283 2019-07-24 stsp return err;
9113 0ebf8283 2019-07-24 stsp }
9114 0ebf8283 2019-07-24 stsp
9115 0ebf8283 2019-07-24 stsp static const struct got_error *
9116 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
9117 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
9118 0ebf8283 2019-07-24 stsp {
9119 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9120 6059809a 2020-12-17 stsp size_t i;
9121 6059809a 2020-12-17 stsp int n;
9122 514f2ffe 2020-01-29 stsp char *id_str;
9123 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
9124 514f2ffe 2020-01-29 stsp
9125 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
9126 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
9127 514f2ffe 2020-01-29 stsp if (err)
9128 514f2ffe 2020-01-29 stsp return err;
9129 514f2ffe 2020-01-29 stsp
9130 514f2ffe 2020-01-29 stsp n = fprintf(f,
9131 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
9132 514f2ffe 2020-01-29 stsp "# commit %s\n"
9133 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
9134 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
9135 514f2ffe 2020-01-29 stsp if (n < 0) {
9136 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9137 514f2ffe 2020-01-29 stsp goto done;
9138 514f2ffe 2020-01-29 stsp }
9139 0ebf8283 2019-07-24 stsp
9140 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
9141 514f2ffe 2020-01-29 stsp if (n < 0) {
9142 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9143 514f2ffe 2020-01-29 stsp goto done;
9144 514f2ffe 2020-01-29 stsp }
9145 0ebf8283 2019-07-24 stsp
9146 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9147 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9148 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9149 0ebf8283 2019-07-24 stsp cmd->desc);
9150 0ebf8283 2019-07-24 stsp if (n < 0) {
9151 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9152 0ebf8283 2019-07-24 stsp break;
9153 0ebf8283 2019-07-24 stsp }
9154 0ebf8283 2019-07-24 stsp }
9155 514f2ffe 2020-01-29 stsp done:
9156 514f2ffe 2020-01-29 stsp free(id_str);
9157 0ebf8283 2019-07-24 stsp return err;
9158 0ebf8283 2019-07-24 stsp }
9159 0ebf8283 2019-07-24 stsp
9160 0ebf8283 2019-07-24 stsp static const struct got_error *
9161 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
9162 0ebf8283 2019-07-24 stsp {
9163 0ebf8283 2019-07-24 stsp static char msg[42];
9164 0ebf8283 2019-07-24 stsp int ret;
9165 0ebf8283 2019-07-24 stsp
9166 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9167 0ebf8283 2019-07-24 stsp lineno);
9168 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
9169 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9170 0ebf8283 2019-07-24 stsp
9171 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9172 0ebf8283 2019-07-24 stsp }
9173 0ebf8283 2019-07-24 stsp
9174 0ebf8283 2019-07-24 stsp static const struct got_error *
9175 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9176 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
9177 0ebf8283 2019-07-24 stsp {
9178 0ebf8283 2019-07-24 stsp const struct got_error *err;
9179 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
9180 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
9181 0ebf8283 2019-07-24 stsp
9182 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9183 0ebf8283 2019-07-24 stsp if (err)
9184 0ebf8283 2019-07-24 stsp return err;
9185 0ebf8283 2019-07-24 stsp
9186 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9187 0ebf8283 2019-07-24 stsp if (err)
9188 0ebf8283 2019-07-24 stsp goto done;
9189 0ebf8283 2019-07-24 stsp
9190 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9191 5943eee2 2019-08-13 stsp if (err)
9192 5943eee2 2019-08-13 stsp goto done;
9193 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9194 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9195 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
9196 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9197 0ebf8283 2019-07-24 stsp }
9198 0ebf8283 2019-07-24 stsp done:
9199 0ebf8283 2019-07-24 stsp if (folded_commit)
9200 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
9201 0ebf8283 2019-07-24 stsp free(id_str);
9202 5943eee2 2019-08-13 stsp free(folded_logmsg);
9203 0ebf8283 2019-07-24 stsp return err;
9204 0ebf8283 2019-07-24 stsp }
9205 0ebf8283 2019-07-24 stsp
9206 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
9207 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
9208 0ebf8283 2019-07-24 stsp {
9209 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
9210 0ebf8283 2019-07-24 stsp
9211 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
9212 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9213 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
9214 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9215 3f9de99f 2019-07-24 stsp folded = prev;
9216 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
9217 0ebf8283 2019-07-24 stsp }
9218 0ebf8283 2019-07-24 stsp
9219 0ebf8283 2019-07-24 stsp return folded;
9220 0ebf8283 2019-07-24 stsp }
9221 0ebf8283 2019-07-24 stsp
9222 0ebf8283 2019-07-24 stsp static const struct got_error *
9223 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9224 0ebf8283 2019-07-24 stsp struct got_repository *repo)
9225 0ebf8283 2019-07-24 stsp {
9226 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9227 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9228 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9229 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9230 1601cb9f 2020-09-11 naddy int logmsg_len;
9231 0ebf8283 2019-07-24 stsp int fd;
9232 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
9233 0ebf8283 2019-07-24 stsp
9234 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9235 0ebf8283 2019-07-24 stsp if (err)
9236 0ebf8283 2019-07-24 stsp return err;
9237 0ebf8283 2019-07-24 stsp
9238 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
9239 0ebf8283 2019-07-24 stsp if (folded) {
9240 0ebf8283 2019-07-24 stsp while (folded != hle) {
9241 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9242 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9243 3f9de99f 2019-07-24 stsp continue;
9244 3f9de99f 2019-07-24 stsp }
9245 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
9246 0ebf8283 2019-07-24 stsp logmsg, repo);
9247 0ebf8283 2019-07-24 stsp if (err)
9248 0ebf8283 2019-07-24 stsp goto done;
9249 0ebf8283 2019-07-24 stsp free(logmsg);
9250 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9251 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9252 0ebf8283 2019-07-24 stsp }
9253 0ebf8283 2019-07-24 stsp }
9254 0ebf8283 2019-07-24 stsp
9255 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9256 0ebf8283 2019-07-24 stsp if (err)
9257 0ebf8283 2019-07-24 stsp goto done;
9258 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9259 5943eee2 2019-08-13 stsp if (err)
9260 5943eee2 2019-08-13 stsp goto done;
9261 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
9262 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
9263 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
9264 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
9265 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9266 0ebf8283 2019-07-24 stsp goto done;
9267 0ebf8283 2019-07-24 stsp }
9268 0ebf8283 2019-07-24 stsp free(logmsg);
9269 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9270 0ebf8283 2019-07-24 stsp
9271 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9272 0ebf8283 2019-07-24 stsp if (err)
9273 0ebf8283 2019-07-24 stsp goto done;
9274 0ebf8283 2019-07-24 stsp
9275 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
9276 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
9277 0ebf8283 2019-07-24 stsp if (err)
9278 0ebf8283 2019-07-24 stsp goto done;
9279 0ebf8283 2019-07-24 stsp
9280 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
9281 0ebf8283 2019-07-24 stsp close(fd);
9282 0ebf8283 2019-07-24 stsp
9283 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9284 0ebf8283 2019-07-24 stsp if (err)
9285 0ebf8283 2019-07-24 stsp goto done;
9286 0ebf8283 2019-07-24 stsp
9287 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9288 0d5bb276 2020-12-15 stsp logmsg_len, 0);
9289 0ebf8283 2019-07-24 stsp if (err) {
9290 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9291 0ebf8283 2019-07-24 stsp goto done;
9292 71392a05 2020-12-13 stsp err = NULL;
9293 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
9294 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
9295 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
9296 0ebf8283 2019-07-24 stsp }
9297 0ebf8283 2019-07-24 stsp done:
9298 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9299 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
9300 0ebf8283 2019-07-24 stsp free(logmsg_path);
9301 0ebf8283 2019-07-24 stsp free(logmsg);
9302 5943eee2 2019-08-13 stsp free(orig_logmsg);
9303 0ebf8283 2019-07-24 stsp free(editor);
9304 0ebf8283 2019-07-24 stsp if (commit)
9305 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9306 0ebf8283 2019-07-24 stsp return err;
9307 5ef14e63 2019-06-02 stsp }
9308 0ebf8283 2019-07-24 stsp
9309 0ebf8283 2019-07-24 stsp static const struct got_error *
9310 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
9311 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9312 0ebf8283 2019-07-24 stsp {
9313 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9314 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
9315 6059809a 2020-12-17 stsp size_t i, size;
9316 0ebf8283 2019-07-24 stsp ssize_t len;
9317 6059809a 2020-12-17 stsp int lineno = 0;
9318 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9319 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
9320 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
9321 0ebf8283 2019-07-24 stsp
9322 0ebf8283 2019-07-24 stsp for (;;) {
9323 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
9324 0ebf8283 2019-07-24 stsp if (len == -1) {
9325 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
9326 0ebf8283 2019-07-24 stsp if (feof(f))
9327 0ebf8283 2019-07-24 stsp break;
9328 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
9329 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
9330 0ebf8283 2019-07-24 stsp break;
9331 0ebf8283 2019-07-24 stsp }
9332 0ebf8283 2019-07-24 stsp lineno++;
9333 0ebf8283 2019-07-24 stsp p = line;
9334 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9335 0ebf8283 2019-07-24 stsp p++;
9336 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
9337 0ebf8283 2019-07-24 stsp free(line);
9338 0ebf8283 2019-07-24 stsp line = NULL;
9339 0ebf8283 2019-07-24 stsp continue;
9340 0ebf8283 2019-07-24 stsp }
9341 0ebf8283 2019-07-24 stsp cmd = NULL;
9342 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9343 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
9344 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9345 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
9346 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
9347 0ebf8283 2019-07-24 stsp break;
9348 0ebf8283 2019-07-24 stsp }
9349 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9350 0ebf8283 2019-07-24 stsp p++;
9351 0ebf8283 2019-07-24 stsp break;
9352 0ebf8283 2019-07-24 stsp }
9353 0ebf8283 2019-07-24 stsp }
9354 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
9355 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9356 0ebf8283 2019-07-24 stsp break;
9357 0ebf8283 2019-07-24 stsp }
9358 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9359 0ebf8283 2019-07-24 stsp p++;
9360 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
9361 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
9362 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
9363 0ebf8283 2019-07-24 stsp break;
9364 0ebf8283 2019-07-24 stsp }
9365 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
9366 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9367 0ebf8283 2019-07-24 stsp if (err)
9368 0ebf8283 2019-07-24 stsp break;
9369 0ebf8283 2019-07-24 stsp } else {
9370 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
9371 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
9372 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9373 0ebf8283 2019-07-24 stsp break;
9374 0ebf8283 2019-07-24 stsp }
9375 0ebf8283 2019-07-24 stsp }
9376 0ebf8283 2019-07-24 stsp free(line);
9377 0ebf8283 2019-07-24 stsp line = NULL;
9378 0ebf8283 2019-07-24 stsp continue;
9379 0ebf8283 2019-07-24 stsp } else {
9380 0ebf8283 2019-07-24 stsp end = p;
9381 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
9382 0ebf8283 2019-07-24 stsp end++;
9383 0ebf8283 2019-07-24 stsp *end = '\0';
9384 0ebf8283 2019-07-24 stsp
9385 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
9386 0ebf8283 2019-07-24 stsp if (err) {
9387 0ebf8283 2019-07-24 stsp /* override error code */
9388 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9389 0ebf8283 2019-07-24 stsp break;
9390 0ebf8283 2019-07-24 stsp }
9391 0ebf8283 2019-07-24 stsp }
9392 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
9393 0ebf8283 2019-07-24 stsp if (hle == NULL) {
9394 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
9395 0ebf8283 2019-07-24 stsp break;
9396 0ebf8283 2019-07-24 stsp }
9397 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
9398 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
9399 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
9400 0ebf8283 2019-07-24 stsp commit_id = NULL;
9401 0ebf8283 2019-07-24 stsp free(line);
9402 0ebf8283 2019-07-24 stsp line = NULL;
9403 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9404 0ebf8283 2019-07-24 stsp }
9405 0ebf8283 2019-07-24 stsp
9406 0ebf8283 2019-07-24 stsp free(line);
9407 0ebf8283 2019-07-24 stsp free(commit_id);
9408 0ebf8283 2019-07-24 stsp return err;
9409 0ebf8283 2019-07-24 stsp }
9410 0ebf8283 2019-07-24 stsp
9411 0ebf8283 2019-07-24 stsp static const struct got_error *
9412 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
9413 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
9414 bfce7f83 2019-07-27 stsp {
9415 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
9416 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
9417 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9418 5b87815e 2020-03-05 stsp static char msg[92];
9419 bfce7f83 2019-07-27 stsp char *id_str;
9420 bfce7f83 2019-07-27 stsp
9421 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
9422 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9423 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
9424 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9425 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9426 5b87815e 2020-03-05 stsp
9427 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9428 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
9429 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9430 5b87815e 2020-03-05 stsp if (hle == hle2)
9431 5b87815e 2020-03-05 stsp continue;
9432 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
9433 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
9434 5b87815e 2020-03-05 stsp continue;
9435 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
9436 5b87815e 2020-03-05 stsp if (err)
9437 5b87815e 2020-03-05 stsp return err;
9438 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
9439 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
9440 5b87815e 2020-03-05 stsp free(id_str);
9441 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9442 5b87815e 2020-03-05 stsp }
9443 5b87815e 2020-03-05 stsp }
9444 bfce7f83 2019-07-27 stsp
9445 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9446 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9447 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9448 bfce7f83 2019-07-27 stsp break;
9449 bfce7f83 2019-07-27 stsp }
9450 bfce7f83 2019-07-27 stsp if (hle == NULL) {
9451 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
9452 bfce7f83 2019-07-27 stsp if (err)
9453 bfce7f83 2019-07-27 stsp return err;
9454 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
9455 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
9456 bfce7f83 2019-07-27 stsp free(id_str);
9457 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9458 bfce7f83 2019-07-27 stsp }
9459 bfce7f83 2019-07-27 stsp }
9460 bfce7f83 2019-07-27 stsp
9461 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9462 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9463 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9464 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
9465 bfce7f83 2019-07-27 stsp
9466 bfce7f83 2019-07-27 stsp return NULL;
9467 bfce7f83 2019-07-27 stsp }
9468 bfce7f83 2019-07-27 stsp
9469 bfce7f83 2019-07-27 stsp static const struct got_error *
9470 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
9471 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
9472 bfce7f83 2019-07-27 stsp struct got_repository *repo)
9473 0ebf8283 2019-07-24 stsp {
9474 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9475 0ebf8283 2019-07-24 stsp char *editor;
9476 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9477 0ebf8283 2019-07-24 stsp
9478 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9479 0ebf8283 2019-07-24 stsp if (err)
9480 0ebf8283 2019-07-24 stsp return err;
9481 0ebf8283 2019-07-24 stsp
9482 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
9483 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
9484 0ebf8283 2019-07-24 stsp goto done;
9485 0ebf8283 2019-07-24 stsp }
9486 0ebf8283 2019-07-24 stsp
9487 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9488 0ebf8283 2019-07-24 stsp if (f == NULL) {
9489 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
9490 0ebf8283 2019-07-24 stsp goto done;
9491 0ebf8283 2019-07-24 stsp }
9492 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9493 bfce7f83 2019-07-27 stsp if (err)
9494 bfce7f83 2019-07-27 stsp goto done;
9495 bfce7f83 2019-07-27 stsp
9496 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
9497 0ebf8283 2019-07-24 stsp done:
9498 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9499 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9500 0ebf8283 2019-07-24 stsp free(editor);
9501 0ebf8283 2019-07-24 stsp return err;
9502 0ebf8283 2019-07-24 stsp }
9503 0ebf8283 2019-07-24 stsp
9504 0ebf8283 2019-07-24 stsp static const struct got_error *
9505 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9506 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
9507 514f2ffe 2020-01-29 stsp struct got_repository *);
9508 0ebf8283 2019-07-24 stsp
9509 0ebf8283 2019-07-24 stsp static const struct got_error *
9510 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
9511 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
9512 466785b9 2020-12-10 jrick int edit_logmsg_only, int fold_only, struct got_repository *repo)
9513 0ebf8283 2019-07-24 stsp {
9514 0ebf8283 2019-07-24 stsp const struct got_error *err;
9515 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9516 0ebf8283 2019-07-24 stsp char *path = NULL;
9517 0ebf8283 2019-07-24 stsp
9518 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
9519 0ebf8283 2019-07-24 stsp if (err)
9520 0ebf8283 2019-07-24 stsp return err;
9521 0ebf8283 2019-07-24 stsp
9522 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
9523 0ebf8283 2019-07-24 stsp if (err)
9524 0ebf8283 2019-07-24 stsp goto done;
9525 0ebf8283 2019-07-24 stsp
9526 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9527 466785b9 2020-12-10 jrick fold_only, repo);
9528 0ebf8283 2019-07-24 stsp if (err)
9529 0ebf8283 2019-07-24 stsp goto done;
9530 0ebf8283 2019-07-24 stsp
9531 466785b9 2020-12-10 jrick if (edit_logmsg_only || fold_only) {
9532 083957f4 2020-02-24 stsp rewind(f);
9533 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9534 083957f4 2020-02-24 stsp } else {
9535 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
9536 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
9537 0ebf8283 2019-07-24 stsp goto done;
9538 083957f4 2020-02-24 stsp }
9539 083957f4 2020-02-24 stsp f = NULL;
9540 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
9541 083957f4 2020-02-24 stsp if (err) {
9542 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9543 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9544 083957f4 2020-02-24 stsp goto done;
9545 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
9546 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
9547 083957f4 2020-02-24 stsp }
9548 0ebf8283 2019-07-24 stsp }
9549 0ebf8283 2019-07-24 stsp done:
9550 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9551 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9552 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
9553 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
9554 0ebf8283 2019-07-24 stsp free(path);
9555 0ebf8283 2019-07-24 stsp return err;
9556 0ebf8283 2019-07-24 stsp }
9557 0ebf8283 2019-07-24 stsp
9558 0ebf8283 2019-07-24 stsp static const struct got_error *
9559 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
9560 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9561 0ebf8283 2019-07-24 stsp {
9562 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9563 0ebf8283 2019-07-24 stsp char *path = NULL;
9564 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9565 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9566 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9567 0ebf8283 2019-07-24 stsp
9568 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
9569 0ebf8283 2019-07-24 stsp if (err)
9570 0ebf8283 2019-07-24 stsp return err;
9571 0ebf8283 2019-07-24 stsp
9572 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
9573 0ebf8283 2019-07-24 stsp if (f == NULL) {
9574 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9575 0ebf8283 2019-07-24 stsp goto done;
9576 0ebf8283 2019-07-24 stsp }
9577 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9578 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9579 0ebf8283 2019-07-24 stsp repo);
9580 0ebf8283 2019-07-24 stsp if (err)
9581 0ebf8283 2019-07-24 stsp break;
9582 0ebf8283 2019-07-24 stsp
9583 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9584 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
9585 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
9586 0ebf8283 2019-07-24 stsp if (n < 0) {
9587 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9588 0ebf8283 2019-07-24 stsp break;
9589 0ebf8283 2019-07-24 stsp }
9590 0ebf8283 2019-07-24 stsp }
9591 0ebf8283 2019-07-24 stsp }
9592 0ebf8283 2019-07-24 stsp done:
9593 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9594 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9595 0ebf8283 2019-07-24 stsp free(path);
9596 0ebf8283 2019-07-24 stsp if (commit)
9597 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9598 0ebf8283 2019-07-24 stsp return err;
9599 0ebf8283 2019-07-24 stsp }
9600 0ebf8283 2019-07-24 stsp
9601 bfce7f83 2019-07-27 stsp void
9602 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
9603 bfce7f83 2019-07-27 stsp {
9604 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9605 bfce7f83 2019-07-27 stsp
9606 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
9607 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
9608 bfce7f83 2019-07-27 stsp free(hle);
9609 bfce7f83 2019-07-27 stsp }
9610 bfce7f83 2019-07-27 stsp }
9611 bfce7f83 2019-07-27 stsp
9612 0ebf8283 2019-07-24 stsp static const struct got_error *
9613 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
9614 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
9615 0ebf8283 2019-07-24 stsp {
9616 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9617 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9618 0ebf8283 2019-07-24 stsp
9619 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9620 0ebf8283 2019-07-24 stsp if (f == NULL) {
9621 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9622 0ebf8283 2019-07-24 stsp goto done;
9623 0ebf8283 2019-07-24 stsp }
9624 0ebf8283 2019-07-24 stsp
9625 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9626 0ebf8283 2019-07-24 stsp done:
9627 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9628 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9629 0ebf8283 2019-07-24 stsp return err;
9630 0ebf8283 2019-07-24 stsp }
9631 0ebf8283 2019-07-24 stsp
9632 0ebf8283 2019-07-24 stsp static const struct got_error *
9633 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9634 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
9635 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
9636 0ebf8283 2019-07-24 stsp {
9637 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
9638 0ebf8283 2019-07-24 stsp int resp = ' ';
9639 0ebf8283 2019-07-24 stsp
9640 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
9641 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9642 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
9643 0ebf8283 2019-07-24 stsp resp = getchar();
9644 a61a4414 2019-08-07 stsp if (resp == '\n')
9645 a61a4414 2019-08-07 stsp resp = getchar();
9646 426ebf2e 2019-07-25 stsp if (resp == 'c') {
9647 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9648 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
9649 bfce7f83 2019-07-27 stsp repo);
9650 426ebf2e 2019-07-25 stsp if (err) {
9651 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9652 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9653 426ebf2e 2019-07-25 stsp break;
9654 bfce7f83 2019-07-27 stsp prev_err = err;
9655 426ebf2e 2019-07-25 stsp resp = ' ';
9656 426ebf2e 2019-07-25 stsp continue;
9657 426ebf2e 2019-07-25 stsp }
9658 bfce7f83 2019-07-27 stsp break;
9659 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
9660 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9661 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
9662 466785b9 2020-12-10 jrick commits, branch_name, 0, 0, repo);
9663 426ebf2e 2019-07-25 stsp if (err) {
9664 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9665 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9666 426ebf2e 2019-07-25 stsp break;
9667 bfce7f83 2019-07-27 stsp prev_err = err;
9668 426ebf2e 2019-07-25 stsp resp = ' ';
9669 426ebf2e 2019-07-25 stsp continue;
9670 426ebf2e 2019-07-25 stsp }
9671 bfce7f83 2019-07-27 stsp break;
9672 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
9673 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9674 0ebf8283 2019-07-24 stsp break;
9675 426ebf2e 2019-07-25 stsp } else
9676 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
9677 0ebf8283 2019-07-24 stsp }
9678 0ebf8283 2019-07-24 stsp
9679 0ebf8283 2019-07-24 stsp return err;
9680 0ebf8283 2019-07-24 stsp }
9681 0ebf8283 2019-07-24 stsp
9682 0ebf8283 2019-07-24 stsp static const struct got_error *
9683 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
9684 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9685 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
9686 0ebf8283 2019-07-24 stsp {
9687 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9688 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9689 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9690 3e3a69f1 2019-07-25 stsp branch, repo);
9691 0ebf8283 2019-07-24 stsp }
9692 0ebf8283 2019-07-24 stsp
9693 0ebf8283 2019-07-24 stsp static const struct got_error *
9694 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
9695 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9696 0ebf8283 2019-07-24 stsp {
9697 0ebf8283 2019-07-24 stsp const struct got_error *err;
9698 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9699 0ebf8283 2019-07-24 stsp
9700 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
9701 0ebf8283 2019-07-24 stsp if (err)
9702 0ebf8283 2019-07-24 stsp goto done;
9703 0ebf8283 2019-07-24 stsp
9704 0ebf8283 2019-07-24 stsp if (new_id) {
9705 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
9706 0ebf8283 2019-07-24 stsp if (err)
9707 0ebf8283 2019-07-24 stsp goto done;
9708 0ebf8283 2019-07-24 stsp }
9709 0ebf8283 2019-07-24 stsp
9710 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
9711 0ebf8283 2019-07-24 stsp if (new_id_str)
9712 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
9713 0ebf8283 2019-07-24 stsp
9714 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9715 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
9716 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
9717 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9718 0ebf8283 2019-07-24 stsp goto done;
9719 0ebf8283 2019-07-24 stsp }
9720 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
9721 0ebf8283 2019-07-24 stsp } else {
9722 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
9723 0ebf8283 2019-07-24 stsp if (err)
9724 0ebf8283 2019-07-24 stsp goto done;
9725 0ebf8283 2019-07-24 stsp }
9726 0ebf8283 2019-07-24 stsp
9727 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
9728 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
9729 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
9730 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
9731 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
9732 0ebf8283 2019-07-24 stsp break;
9733 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
9734 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
9735 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9736 0ebf8283 2019-07-24 stsp logmsg);
9737 0ebf8283 2019-07-24 stsp break;
9738 0ebf8283 2019-07-24 stsp default:
9739 0ebf8283 2019-07-24 stsp break;
9740 0ebf8283 2019-07-24 stsp }
9741 0ebf8283 2019-07-24 stsp done:
9742 0ebf8283 2019-07-24 stsp free(old_id_str);
9743 0ebf8283 2019-07-24 stsp free(new_id_str);
9744 0ebf8283 2019-07-24 stsp return err;
9745 0ebf8283 2019-07-24 stsp }
9746 0ebf8283 2019-07-24 stsp
9747 0ebf8283 2019-07-24 stsp static const struct got_error *
9748 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
9749 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
9750 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9751 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
9752 0ebf8283 2019-07-24 stsp {
9753 0ebf8283 2019-07-24 stsp const struct got_error *err;
9754 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9755 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
9756 0ebf8283 2019-07-24 stsp
9757 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9758 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
9759 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9760 0ebf8283 2019-07-24 stsp if (err)
9761 0ebf8283 2019-07-24 stsp return err;
9762 0ebf8283 2019-07-24 stsp }
9763 0ebf8283 2019-07-24 stsp
9764 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9765 0ebf8283 2019-07-24 stsp if (err)
9766 0ebf8283 2019-07-24 stsp return err;
9767 0ebf8283 2019-07-24 stsp
9768 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9769 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
9770 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
9771 0ebf8283 2019-07-24 stsp if (err) {
9772 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9773 0ebf8283 2019-07-24 stsp goto done;
9774 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
9775 0ebf8283 2019-07-24 stsp } else {
9776 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
9777 0ebf8283 2019-07-24 stsp free(new_commit_id);
9778 0ebf8283 2019-07-24 stsp }
9779 0ebf8283 2019-07-24 stsp done:
9780 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9781 0ebf8283 2019-07-24 stsp return err;
9782 0ebf8283 2019-07-24 stsp }
9783 0ebf8283 2019-07-24 stsp
9784 0ebf8283 2019-07-24 stsp static const struct got_error *
9785 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
9786 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9787 0ebf8283 2019-07-24 stsp {
9788 0ebf8283 2019-07-24 stsp const struct got_error *error;
9789 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9790 0ebf8283 2019-07-24 stsp
9791 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9792 0ebf8283 2019-07-24 stsp repo);
9793 0ebf8283 2019-07-24 stsp if (error)
9794 0ebf8283 2019-07-24 stsp return error;
9795 0ebf8283 2019-07-24 stsp
9796 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9797 0ebf8283 2019-07-24 stsp if (error)
9798 0ebf8283 2019-07-24 stsp return error;
9799 0ebf8283 2019-07-24 stsp
9800 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
9801 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9802 0ebf8283 2019-07-24 stsp return error;
9803 ab20a43a 2020-01-29 stsp }
9804 ab20a43a 2020-01-29 stsp
9805 ab20a43a 2020-01-29 stsp static const struct got_error *
9806 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
9807 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
9808 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9809 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9810 ab20a43a 2020-01-29 stsp {
9811 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
9812 ab20a43a 2020-01-29 stsp
9813 ab20a43a 2020-01-29 stsp switch (status) {
9814 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9815 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9816 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9817 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
9818 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9819 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9820 ab20a43a 2020-01-29 stsp default:
9821 ab20a43a 2020-01-29 stsp break;
9822 ab20a43a 2020-01-29 stsp }
9823 ab20a43a 2020-01-29 stsp
9824 ab20a43a 2020-01-29 stsp switch (staged_status) {
9825 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9826 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9827 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9828 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9829 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9830 ab20a43a 2020-01-29 stsp default:
9831 ab20a43a 2020-01-29 stsp break;
9832 ab20a43a 2020-01-29 stsp }
9833 ab20a43a 2020-01-29 stsp
9834 ab20a43a 2020-01-29 stsp return NULL;
9835 0ebf8283 2019-07-24 stsp }
9836 0ebf8283 2019-07-24 stsp
9837 0ebf8283 2019-07-24 stsp static const struct got_error *
9838 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
9839 0ebf8283 2019-07-24 stsp {
9840 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
9841 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
9842 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9843 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
9844 0ebf8283 2019-07-24 stsp char *cwd = NULL;
9845 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
9846 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
9847 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
9848 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
9849 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
9850 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9851 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
9852 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9853 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9854 466785b9 2020-12-10 jrick int edit_logmsg_only = 0, fold_only = 0;
9855 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
9856 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
9857 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9858 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
9859 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
9860 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
9861 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
9862 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
9863 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9864 0ebf8283 2019-07-24 stsp
9865 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
9866 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
9867 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
9868 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9869 0ebf8283 2019-07-24 stsp
9870 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
9871 0ebf8283 2019-07-24 stsp switch (ch) {
9872 0ebf8283 2019-07-24 stsp case 'a':
9873 0ebf8283 2019-07-24 stsp abort_edit = 1;
9874 0ebf8283 2019-07-24 stsp break;
9875 0ebf8283 2019-07-24 stsp case 'c':
9876 0ebf8283 2019-07-24 stsp continue_edit = 1;
9877 0ebf8283 2019-07-24 stsp break;
9878 466785b9 2020-12-10 jrick case 'f':
9879 466785b9 2020-12-10 jrick fold_only = 1;
9880 466785b9 2020-12-10 jrick break;
9881 0ebf8283 2019-07-24 stsp case 'F':
9882 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
9883 0ebf8283 2019-07-24 stsp break;
9884 083957f4 2020-02-24 stsp case 'm':
9885 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
9886 083957f4 2020-02-24 stsp break;
9887 e600f124 2021-03-21 stsp case 'l':
9888 e600f124 2021-03-21 stsp list_backups = 1;
9889 e600f124 2021-03-21 stsp break;
9890 643b85bc 2021-07-16 stsp case 'X':
9891 643b85bc 2021-07-16 stsp delete_backups = 1;
9892 643b85bc 2021-07-16 stsp break;
9893 0ebf8283 2019-07-24 stsp default:
9894 0ebf8283 2019-07-24 stsp usage_histedit();
9895 0ebf8283 2019-07-24 stsp /* NOTREACHED */
9896 0ebf8283 2019-07-24 stsp }
9897 0ebf8283 2019-07-24 stsp }
9898 0ebf8283 2019-07-24 stsp
9899 0ebf8283 2019-07-24 stsp argc -= optind;
9900 0ebf8283 2019-07-24 stsp argv += optind;
9901 0ebf8283 2019-07-24 stsp
9902 0ebf8283 2019-07-24 stsp #ifndef PROFILE
9903 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9904 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
9905 0ebf8283 2019-07-24 stsp err(1, "pledge");
9906 0ebf8283 2019-07-24 stsp #endif
9907 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
9908 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
9909 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
9910 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
9911 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
9912 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
9913 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
9914 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
9915 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
9916 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
9917 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
9918 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
9919 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
9920 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
9921 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
9922 b3805337 2020-12-13 stsp option_conflict('F', 'f');
9923 e600f124 2021-03-21 stsp if (list_backups) {
9924 e600f124 2021-03-21 stsp if (abort_edit)
9925 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9926 e600f124 2021-03-21 stsp if (continue_edit)
9927 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9928 e600f124 2021-03-21 stsp if (edit_script_path)
9929 e600f124 2021-03-21 stsp option_conflict('l', 'F');
9930 e600f124 2021-03-21 stsp if (edit_logmsg_only)
9931 e600f124 2021-03-21 stsp option_conflict('l', 'm');
9932 e600f124 2021-03-21 stsp if (fold_only)
9933 e600f124 2021-03-21 stsp option_conflict('l', 'f');
9934 643b85bc 2021-07-16 stsp if (delete_backups)
9935 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9936 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9937 e600f124 2021-03-21 stsp usage_histedit();
9938 643b85bc 2021-07-16 stsp } else if (delete_backups) {
9939 643b85bc 2021-07-16 stsp if (abort_edit)
9940 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
9941 643b85bc 2021-07-16 stsp if (continue_edit)
9942 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
9943 643b85bc 2021-07-16 stsp if (edit_script_path)
9944 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
9945 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
9946 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
9947 643b85bc 2021-07-16 stsp if (fold_only)
9948 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
9949 643b85bc 2021-07-16 stsp if (list_backups)
9950 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
9951 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
9952 643b85bc 2021-07-16 stsp usage_histedit();
9953 e600f124 2021-03-21 stsp } else if (argc != 0)
9954 0ebf8283 2019-07-24 stsp usage_histedit();
9955 0ebf8283 2019-07-24 stsp
9956 0ebf8283 2019-07-24 stsp /*
9957 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
9958 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
9959 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
9960 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
9961 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
9962 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
9963 0ebf8283 2019-07-24 stsp */
9964 0ebf8283 2019-07-24 stsp
9965 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
9966 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
9967 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
9968 0ebf8283 2019-07-24 stsp goto done;
9969 0ebf8283 2019-07-24 stsp }
9970 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
9971 fa51e947 2020-03-27 stsp if (error) {
9972 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9973 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
9974 e600f124 2021-03-21 stsp goto done;
9975 e600f124 2021-03-21 stsp } else {
9976 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9977 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
9978 e600f124 2021-03-21 stsp "histedit", cwd);
9979 e600f124 2021-03-21 stsp goto done;
9980 e600f124 2021-03-21 stsp }
9981 e600f124 2021-03-21 stsp }
9982 e600f124 2021-03-21 stsp
9983 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9984 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
9985 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
9986 e600f124 2021-03-21 stsp NULL);
9987 e600f124 2021-03-21 stsp if (error != NULL)
9988 e600f124 2021-03-21 stsp goto done;
9989 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9990 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
9991 e600f124 2021-03-21 stsp if (error)
9992 e600f124 2021-03-21 stsp goto done;
9993 643b85bc 2021-07-16 stsp error = process_backup_refs(
9994 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
9995 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
9996 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
9997 fa51e947 2020-03-27 stsp }
9998 0ebf8283 2019-07-24 stsp
9999 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10000 c9956ddf 2019-09-08 stsp NULL);
10001 0ebf8283 2019-07-24 stsp if (error != NULL)
10002 0ebf8283 2019-07-24 stsp goto done;
10003 0ebf8283 2019-07-24 stsp
10004 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10005 0ebf8283 2019-07-24 stsp if (error)
10006 0ebf8283 2019-07-24 stsp goto done;
10007 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
10008 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
10009 0ebf8283 2019-07-24 stsp goto done;
10010 0ebf8283 2019-07-24 stsp }
10011 0ebf8283 2019-07-24 stsp
10012 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10013 0ebf8283 2019-07-24 stsp if (error)
10014 0ebf8283 2019-07-24 stsp goto done;
10015 0ebf8283 2019-07-24 stsp
10016 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
10017 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10018 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
10019 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
10020 083957f4 2020-02-24 stsp "before the -m option can be used");
10021 083957f4 2020-02-24 stsp goto done;
10022 083957f4 2020-02-24 stsp }
10023 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
10024 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10025 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
10026 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
10027 466785b9 2020-12-10 jrick "before the -f option can be used");
10028 466785b9 2020-12-10 jrick goto done;
10029 466785b9 2020-12-10 jrick }
10030 083957f4 2020-02-24 stsp
10031 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
10032 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10033 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10034 3e3a69f1 2019-07-25 stsp worktree, repo);
10035 0ebf8283 2019-07-24 stsp if (error)
10036 0ebf8283 2019-07-24 stsp goto done;
10037 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10038 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10039 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
10040 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
10041 0ebf8283 2019-07-24 stsp if (error)
10042 0ebf8283 2019-07-24 stsp goto done;
10043 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
10044 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10045 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10046 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
10047 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
10048 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10049 0ebf8283 2019-07-24 stsp goto done;
10050 0ebf8283 2019-07-24 stsp }
10051 0ebf8283 2019-07-24 stsp
10052 0ebf8283 2019-07-24 stsp if (continue_edit) {
10053 0ebf8283 2019-07-24 stsp char *path;
10054 0ebf8283 2019-07-24 stsp
10055 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
10056 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10057 0ebf8283 2019-07-24 stsp goto done;
10058 0ebf8283 2019-07-24 stsp }
10059 0ebf8283 2019-07-24 stsp
10060 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
10061 0ebf8283 2019-07-24 stsp if (error)
10062 0ebf8283 2019-07-24 stsp goto done;
10063 0ebf8283 2019-07-24 stsp
10064 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
10065 0ebf8283 2019-07-24 stsp free(path);
10066 0ebf8283 2019-07-24 stsp if (error)
10067 0ebf8283 2019-07-24 stsp goto done;
10068 0ebf8283 2019-07-24 stsp
10069 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10070 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10071 3e3a69f1 2019-07-25 stsp worktree, repo);
10072 0ebf8283 2019-07-24 stsp if (error)
10073 0ebf8283 2019-07-24 stsp goto done;
10074 0ebf8283 2019-07-24 stsp
10075 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10076 0ebf8283 2019-07-24 stsp if (error)
10077 0ebf8283 2019-07-24 stsp goto done;
10078 0ebf8283 2019-07-24 stsp
10079 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10080 0ebf8283 2019-07-24 stsp head_commit_id);
10081 0ebf8283 2019-07-24 stsp if (error)
10082 0ebf8283 2019-07-24 stsp goto done;
10083 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10084 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10085 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10086 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10087 3aac7cf7 2019-07-25 stsp goto done;
10088 3aac7cf7 2019-07-25 stsp }
10089 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10090 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
10091 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10092 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10093 0ebf8283 2019-07-24 stsp commit = NULL;
10094 0ebf8283 2019-07-24 stsp if (error)
10095 0ebf8283 2019-07-24 stsp goto done;
10096 0ebf8283 2019-07-24 stsp } else {
10097 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
10098 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
10099 0ebf8283 2019-07-24 stsp goto done;
10100 0ebf8283 2019-07-24 stsp }
10101 0ebf8283 2019-07-24 stsp
10102 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
10103 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
10104 0ebf8283 2019-07-24 stsp if (error != NULL)
10105 0ebf8283 2019-07-24 stsp goto done;
10106 0ebf8283 2019-07-24 stsp
10107 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10108 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10109 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
10110 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
10111 c7d20a3f 2019-07-30 stsp goto done;
10112 c7d20a3f 2019-07-30 stsp }
10113 c7d20a3f 2019-07-30 stsp
10114 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10115 bfce7f83 2019-07-27 stsp got_ref_close(branch);
10116 bfce7f83 2019-07-27 stsp branch = NULL;
10117 0ebf8283 2019-07-24 stsp if (error)
10118 0ebf8283 2019-07-24 stsp goto done;
10119 0ebf8283 2019-07-24 stsp
10120 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10121 0ebf8283 2019-07-24 stsp head_commit_id);
10122 0ebf8283 2019-07-24 stsp if (error)
10123 0ebf8283 2019-07-24 stsp goto done;
10124 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10125 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10126 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10127 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10128 3aac7cf7 2019-07-25 stsp goto done;
10129 3aac7cf7 2019-07-25 stsp }
10130 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10131 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
10132 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
10133 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10134 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10135 0ebf8283 2019-07-24 stsp commit = NULL;
10136 0ebf8283 2019-07-24 stsp if (error)
10137 0ebf8283 2019-07-24 stsp goto done;
10138 0ebf8283 2019-07-24 stsp
10139 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
10140 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10141 c5996fff 2020-01-29 stsp goto done;
10142 c5996fff 2020-01-29 stsp }
10143 c5996fff 2020-01-29 stsp
10144 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10145 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
10146 bfce7f83 2019-07-27 stsp if (error)
10147 bfce7f83 2019-07-27 stsp goto done;
10148 bfce7f83 2019-07-27 stsp
10149 0ebf8283 2019-07-24 stsp if (edit_script_path) {
10150 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
10151 0ebf8283 2019-07-24 stsp edit_script_path, repo);
10152 6ba2bea2 2019-07-27 stsp if (error) {
10153 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10154 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10155 9627c110 2020-04-18 stsp update_progress, &upa);
10156 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10157 0ebf8283 2019-07-24 stsp goto done;
10158 6ba2bea2 2019-07-27 stsp }
10159 0ebf8283 2019-07-24 stsp } else {
10160 514f2ffe 2020-01-29 stsp const char *branch_name;
10161 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
10162 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
10163 514f2ffe 2020-01-29 stsp branch_name += 11;
10164 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
10165 466785b9 2020-12-10 jrick branch_name, edit_logmsg_only, fold_only, repo);
10166 6ba2bea2 2019-07-27 stsp if (error) {
10167 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10168 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10169 9627c110 2020-04-18 stsp update_progress, &upa);
10170 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10171 0ebf8283 2019-07-24 stsp goto done;
10172 6ba2bea2 2019-07-27 stsp }
10173 0ebf8283 2019-07-24 stsp
10174 0ebf8283 2019-07-24 stsp }
10175 0ebf8283 2019-07-24 stsp
10176 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
10177 0ebf8283 2019-07-24 stsp repo);
10178 6ba2bea2 2019-07-27 stsp if (error) {
10179 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10180 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10181 9627c110 2020-04-18 stsp update_progress, &upa);
10182 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10183 0ebf8283 2019-07-24 stsp goto done;
10184 6ba2bea2 2019-07-27 stsp }
10185 0ebf8283 2019-07-24 stsp
10186 0ebf8283 2019-07-24 stsp }
10187 0ebf8283 2019-07-24 stsp
10188 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
10189 4ec14e60 2019-08-28 hiltjo if (error)
10190 0ebf8283 2019-07-24 stsp goto done;
10191 0ebf8283 2019-07-24 stsp
10192 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10193 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
10194 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
10195 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
10196 0ebf8283 2019-07-24 stsp continue;
10197 0ebf8283 2019-07-24 stsp
10198 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
10199 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10200 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
10201 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
10202 62d463ca 2020-10-20 naddy repo);
10203 ab20a43a 2020-01-29 stsp if (error)
10204 ab20a43a 2020-01-29 stsp goto done;
10205 0ebf8283 2019-07-24 stsp } else {
10206 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
10207 ab20a43a 2020-01-29 stsp int have_changes = 0;
10208 ab20a43a 2020-01-29 stsp
10209 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
10210 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
10211 ab20a43a 2020-01-29 stsp if (error)
10212 ab20a43a 2020-01-29 stsp goto done;
10213 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
10214 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
10215 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
10216 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
10217 ab20a43a 2020-01-29 stsp if (error) {
10218 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
10219 ab20a43a 2020-01-29 stsp goto done;
10220 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
10221 ab20a43a 2020-01-29 stsp goto done;
10222 ab20a43a 2020-01-29 stsp }
10223 ab20a43a 2020-01-29 stsp if (have_changes) {
10224 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
10225 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
10226 ab20a43a 2020-01-29 stsp if (error)
10227 ab20a43a 2020-01-29 stsp goto done;
10228 ab20a43a 2020-01-29 stsp } else {
10229 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
10230 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
10231 ab20a43a 2020-01-29 stsp if (error)
10232 ab20a43a 2020-01-29 stsp goto done;
10233 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
10234 ab20a43a 2020-01-29 stsp hle, NULL);
10235 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
10236 ab20a43a 2020-01-29 stsp commit = NULL;
10237 ab20a43a 2020-01-29 stsp if (error)
10238 ab20a43a 2020-01-29 stsp goto done;
10239 ab20a43a 2020-01-29 stsp }
10240 0ebf8283 2019-07-24 stsp }
10241 0ebf8283 2019-07-24 stsp continue;
10242 0ebf8283 2019-07-24 stsp }
10243 0ebf8283 2019-07-24 stsp
10244 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10245 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10246 0ebf8283 2019-07-24 stsp if (error)
10247 0ebf8283 2019-07-24 stsp goto done;
10248 0ebf8283 2019-07-24 stsp continue;
10249 0ebf8283 2019-07-24 stsp }
10250 0ebf8283 2019-07-24 stsp
10251 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10252 0ebf8283 2019-07-24 stsp hle->commit_id);
10253 0ebf8283 2019-07-24 stsp if (error)
10254 0ebf8283 2019-07-24 stsp goto done;
10255 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10256 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10257 0ebf8283 2019-07-24 stsp
10258 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
10259 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
10260 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
10261 0ebf8283 2019-07-24 stsp if (error)
10262 0ebf8283 2019-07-24 stsp goto done;
10263 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10264 0ebf8283 2019-07-24 stsp commit = NULL;
10265 0ebf8283 2019-07-24 stsp
10266 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10267 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
10268 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
10269 9627c110 2020-04-18 stsp
10270 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
10271 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
10272 a0ea4fc0 2020-02-28 stsp repo);
10273 a0ea4fc0 2020-02-28 stsp if (error)
10274 a0ea4fc0 2020-02-28 stsp goto done;
10275 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10276 0ebf8283 2019-07-24 stsp break;
10277 0ebf8283 2019-07-24 stsp }
10278 0ebf8283 2019-07-24 stsp
10279 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10280 0ebf8283 2019-07-24 stsp char *id_str;
10281 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
10282 0ebf8283 2019-07-24 stsp if (error)
10283 0ebf8283 2019-07-24 stsp goto done;
10284 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
10285 0ebf8283 2019-07-24 stsp id_str);
10286 0ebf8283 2019-07-24 stsp free(id_str);
10287 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10288 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
10289 3e3a69f1 2019-07-25 stsp fileindex);
10290 0ebf8283 2019-07-24 stsp goto done;
10291 0ebf8283 2019-07-24 stsp }
10292 0ebf8283 2019-07-24 stsp
10293 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10294 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10295 0ebf8283 2019-07-24 stsp if (error)
10296 0ebf8283 2019-07-24 stsp goto done;
10297 0ebf8283 2019-07-24 stsp continue;
10298 0ebf8283 2019-07-24 stsp }
10299 0ebf8283 2019-07-24 stsp
10300 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
10301 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
10302 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10303 0ebf8283 2019-07-24 stsp if (error)
10304 0ebf8283 2019-07-24 stsp goto done;
10305 0ebf8283 2019-07-24 stsp }
10306 0ebf8283 2019-07-24 stsp
10307 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
10308 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
10309 0ebf8283 2019-07-24 stsp if (error)
10310 0ebf8283 2019-07-24 stsp goto done;
10311 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10312 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
10313 0ebf8283 2019-07-24 stsp } else
10314 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
10315 3e3a69f1 2019-07-25 stsp branch, repo);
10316 0ebf8283 2019-07-24 stsp done:
10317 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
10318 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
10319 0ebf8283 2019-07-24 stsp free(head_commit_id);
10320 0ebf8283 2019-07-24 stsp free(base_commit_id);
10321 0ebf8283 2019-07-24 stsp free(resume_commit_id);
10322 0ebf8283 2019-07-24 stsp if (commit)
10323 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10324 0ebf8283 2019-07-24 stsp if (branch)
10325 0ebf8283 2019-07-24 stsp got_ref_close(branch);
10326 0ebf8283 2019-07-24 stsp if (tmp_branch)
10327 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
10328 0ebf8283 2019-07-24 stsp if (worktree)
10329 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
10330 1d0f4054 2021-06-17 stsp if (repo) {
10331 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10332 1d0f4054 2021-06-17 stsp if (error == NULL)
10333 1d0f4054 2021-06-17 stsp error = close_err;
10334 1d0f4054 2021-06-17 stsp }
10335 2822a352 2019-10-15 stsp return error;
10336 2822a352 2019-10-15 stsp }
10337 2822a352 2019-10-15 stsp
10338 2822a352 2019-10-15 stsp __dead static void
10339 2822a352 2019-10-15 stsp usage_integrate(void)
10340 2822a352 2019-10-15 stsp {
10341 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10342 2822a352 2019-10-15 stsp exit(1);
10343 2822a352 2019-10-15 stsp }
10344 2822a352 2019-10-15 stsp
10345 2822a352 2019-10-15 stsp static const struct got_error *
10346 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
10347 2822a352 2019-10-15 stsp {
10348 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
10349 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
10350 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
10351 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10352 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
10353 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10354 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
10355 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10356 9627c110 2020-04-18 stsp int ch;
10357 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10358 2822a352 2019-10-15 stsp
10359 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10360 2822a352 2019-10-15 stsp switch (ch) {
10361 2822a352 2019-10-15 stsp default:
10362 2822a352 2019-10-15 stsp usage_integrate();
10363 2822a352 2019-10-15 stsp /* NOTREACHED */
10364 2822a352 2019-10-15 stsp }
10365 2822a352 2019-10-15 stsp }
10366 2822a352 2019-10-15 stsp
10367 2822a352 2019-10-15 stsp argc -= optind;
10368 2822a352 2019-10-15 stsp argv += optind;
10369 2822a352 2019-10-15 stsp
10370 2822a352 2019-10-15 stsp if (argc != 1)
10371 2822a352 2019-10-15 stsp usage_integrate();
10372 2822a352 2019-10-15 stsp branch_arg = argv[0];
10373 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
10374 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10375 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
10376 2822a352 2019-10-15 stsp err(1, "pledge");
10377 b08a0ccd 2020-09-24 stsp #endif
10378 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
10379 2822a352 2019-10-15 stsp if (cwd == NULL) {
10380 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
10381 2822a352 2019-10-15 stsp goto done;
10382 2822a352 2019-10-15 stsp }
10383 2822a352 2019-10-15 stsp
10384 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
10385 fa51e947 2020-03-27 stsp if (error) {
10386 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10387 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
10388 fa51e947 2020-03-27 stsp cwd);
10389 2822a352 2019-10-15 stsp goto done;
10390 fa51e947 2020-03-27 stsp }
10391 2822a352 2019-10-15 stsp
10392 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
10393 2822a352 2019-10-15 stsp if (error)
10394 2822a352 2019-10-15 stsp goto done;
10395 2822a352 2019-10-15 stsp
10396 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10397 2822a352 2019-10-15 stsp NULL);
10398 2822a352 2019-10-15 stsp if (error != NULL)
10399 2822a352 2019-10-15 stsp goto done;
10400 2822a352 2019-10-15 stsp
10401 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10402 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
10403 2822a352 2019-10-15 stsp if (error)
10404 2822a352 2019-10-15 stsp goto done;
10405 2822a352 2019-10-15 stsp
10406 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10407 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
10408 2822a352 2019-10-15 stsp goto done;
10409 2822a352 2019-10-15 stsp }
10410 2822a352 2019-10-15 stsp
10411 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10412 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
10413 2822a352 2019-10-15 stsp if (error)
10414 2822a352 2019-10-15 stsp goto done;
10415 2822a352 2019-10-15 stsp
10416 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
10417 2822a352 2019-10-15 stsp if (refname == NULL) {
10418 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10419 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10420 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10421 2822a352 2019-10-15 stsp goto done;
10422 2822a352 2019-10-15 stsp }
10423 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
10424 2822a352 2019-10-15 stsp if (base_refname == NULL) {
10425 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10426 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10427 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10428 2822a352 2019-10-15 stsp goto done;
10429 2822a352 2019-10-15 stsp }
10430 2822a352 2019-10-15 stsp
10431 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
10432 2822a352 2019-10-15 stsp if (error)
10433 2822a352 2019-10-15 stsp goto done;
10434 2822a352 2019-10-15 stsp
10435 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10436 2822a352 2019-10-15 stsp if (error)
10437 2822a352 2019-10-15 stsp goto done;
10438 2822a352 2019-10-15 stsp
10439 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10440 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
10441 2822a352 2019-10-15 stsp "specified branch has already been integrated");
10442 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10443 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10444 2822a352 2019-10-15 stsp goto done;
10445 2822a352 2019-10-15 stsp }
10446 2822a352 2019-10-15 stsp
10447 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10448 2822a352 2019-10-15 stsp if (error) {
10449 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
10450 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
10451 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10452 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10453 2822a352 2019-10-15 stsp goto done;
10454 2822a352 2019-10-15 stsp }
10455 2822a352 2019-10-15 stsp
10456 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10457 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
10458 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
10459 2822a352 2019-10-15 stsp check_cancelled, NULL);
10460 2822a352 2019-10-15 stsp if (error)
10461 2822a352 2019-10-15 stsp goto done;
10462 2822a352 2019-10-15 stsp
10463 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
10464 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10465 2822a352 2019-10-15 stsp done:
10466 1d0f4054 2021-06-17 stsp if (repo) {
10467 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10468 1d0f4054 2021-06-17 stsp if (error == NULL)
10469 1d0f4054 2021-06-17 stsp error = close_err;
10470 1d0f4054 2021-06-17 stsp }
10471 2822a352 2019-10-15 stsp if (worktree)
10472 2822a352 2019-10-15 stsp got_worktree_close(worktree);
10473 2822a352 2019-10-15 stsp free(cwd);
10474 2822a352 2019-10-15 stsp free(base_commit_id);
10475 2822a352 2019-10-15 stsp free(commit_id);
10476 2822a352 2019-10-15 stsp free(refname);
10477 2822a352 2019-10-15 stsp free(base_refname);
10478 715dc77e 2019-08-03 stsp return error;
10479 715dc77e 2019-08-03 stsp }
10480 715dc77e 2019-08-03 stsp
10481 715dc77e 2019-08-03 stsp __dead static void
10482 715dc77e 2019-08-03 stsp usage_stage(void)
10483 715dc77e 2019-08-03 stsp {
10484 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
10485 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
10486 715dc77e 2019-08-03 stsp getprogname());
10487 715dc77e 2019-08-03 stsp exit(1);
10488 715dc77e 2019-08-03 stsp }
10489 715dc77e 2019-08-03 stsp
10490 715dc77e 2019-08-03 stsp static const struct got_error *
10491 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
10492 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
10493 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
10494 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
10495 408b4ebc 2019-08-03 stsp {
10496 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
10497 408b4ebc 2019-08-03 stsp char *id_str = NULL;
10498 408b4ebc 2019-08-03 stsp
10499 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
10500 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
10501 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
10502 408b4ebc 2019-08-03 stsp return NULL;
10503 408b4ebc 2019-08-03 stsp
10504 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
10505 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
10506 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
10507 408b4ebc 2019-08-03 stsp else
10508 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
10509 408b4ebc 2019-08-03 stsp if (err)
10510 408b4ebc 2019-08-03 stsp return err;
10511 408b4ebc 2019-08-03 stsp
10512 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
10513 408b4ebc 2019-08-03 stsp free(id_str);
10514 dc424a06 2019-08-07 stsp return NULL;
10515 dc424a06 2019-08-07 stsp }
10516 2e1f37b0 2019-08-08 stsp
10517 dc424a06 2019-08-07 stsp static const struct got_error *
10518 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
10519 715dc77e 2019-08-03 stsp {
10520 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
10521 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
10522 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
10523 715dc77e 2019-08-03 stsp char *cwd = NULL;
10524 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
10525 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
10526 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10527 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
10528 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10529 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10530 715dc77e 2019-08-03 stsp
10531 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
10532 715dc77e 2019-08-03 stsp
10533 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
10534 715dc77e 2019-08-03 stsp switch (ch) {
10535 408b4ebc 2019-08-03 stsp case 'l':
10536 408b4ebc 2019-08-03 stsp list_stage = 1;
10537 408b4ebc 2019-08-03 stsp break;
10538 dc424a06 2019-08-07 stsp case 'p':
10539 dc424a06 2019-08-07 stsp pflag = 1;
10540 dc424a06 2019-08-07 stsp break;
10541 dc424a06 2019-08-07 stsp case 'F':
10542 dc424a06 2019-08-07 stsp patch_script_path = optarg;
10543 dc424a06 2019-08-07 stsp break;
10544 35213c7c 2020-07-23 stsp case 'S':
10545 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
10546 35213c7c 2020-07-23 stsp break;
10547 715dc77e 2019-08-03 stsp default:
10548 715dc77e 2019-08-03 stsp usage_stage();
10549 715dc77e 2019-08-03 stsp /* NOTREACHED */
10550 715dc77e 2019-08-03 stsp }
10551 715dc77e 2019-08-03 stsp }
10552 715dc77e 2019-08-03 stsp
10553 715dc77e 2019-08-03 stsp argc -= optind;
10554 715dc77e 2019-08-03 stsp argv += optind;
10555 715dc77e 2019-08-03 stsp
10556 715dc77e 2019-08-03 stsp #ifndef PROFILE
10557 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10558 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
10559 715dc77e 2019-08-03 stsp err(1, "pledge");
10560 715dc77e 2019-08-03 stsp #endif
10561 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
10562 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
10563 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
10564 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
10565 715dc77e 2019-08-03 stsp
10566 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
10567 715dc77e 2019-08-03 stsp if (cwd == NULL) {
10568 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
10569 715dc77e 2019-08-03 stsp goto done;
10570 715dc77e 2019-08-03 stsp }
10571 715dc77e 2019-08-03 stsp
10572 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10573 fa51e947 2020-03-27 stsp if (error) {
10574 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10575 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
10576 715dc77e 2019-08-03 stsp goto done;
10577 fa51e947 2020-03-27 stsp }
10578 715dc77e 2019-08-03 stsp
10579 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10580 c9956ddf 2019-09-08 stsp NULL);
10581 715dc77e 2019-08-03 stsp if (error != NULL)
10582 715dc77e 2019-08-03 stsp goto done;
10583 715dc77e 2019-08-03 stsp
10584 dc424a06 2019-08-07 stsp if (patch_script_path) {
10585 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
10586 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
10587 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
10588 dc424a06 2019-08-07 stsp patch_script_path);
10589 dc424a06 2019-08-07 stsp goto done;
10590 dc424a06 2019-08-07 stsp }
10591 dc424a06 2019-08-07 stsp }
10592 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10593 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
10594 715dc77e 2019-08-03 stsp if (error)
10595 715dc77e 2019-08-03 stsp goto done;
10596 715dc77e 2019-08-03 stsp
10597 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10598 6d022e97 2019-08-04 stsp if (error)
10599 6d022e97 2019-08-04 stsp goto done;
10600 715dc77e 2019-08-03 stsp
10601 6d022e97 2019-08-04 stsp if (list_stage)
10602 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
10603 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
10604 2e1f37b0 2019-08-08 stsp else {
10605 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10606 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
10607 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
10608 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
10609 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
10610 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
10611 2e1f37b0 2019-08-08 stsp }
10612 ad493afc 2019-08-03 stsp done:
10613 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10614 2e1f37b0 2019-08-08 stsp error == NULL)
10615 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10616 1d0f4054 2021-06-17 stsp if (repo) {
10617 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10618 1d0f4054 2021-06-17 stsp if (error == NULL)
10619 1d0f4054 2021-06-17 stsp error = close_err;
10620 1d0f4054 2021-06-17 stsp }
10621 ad493afc 2019-08-03 stsp if (worktree)
10622 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
10623 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10624 ad493afc 2019-08-03 stsp free((char *)pe->path);
10625 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
10626 ad493afc 2019-08-03 stsp free(cwd);
10627 ad493afc 2019-08-03 stsp return error;
10628 ad493afc 2019-08-03 stsp }
10629 ad493afc 2019-08-03 stsp
10630 ad493afc 2019-08-03 stsp __dead static void
10631 ad493afc 2019-08-03 stsp usage_unstage(void)
10632 ad493afc 2019-08-03 stsp {
10633 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
10634 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
10635 ad493afc 2019-08-03 stsp getprogname());
10636 ad493afc 2019-08-03 stsp exit(1);
10637 ad493afc 2019-08-03 stsp }
10638 ad493afc 2019-08-03 stsp
10639 ad493afc 2019-08-03 stsp
10640 ad493afc 2019-08-03 stsp static const struct got_error *
10641 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
10642 ad493afc 2019-08-03 stsp {
10643 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
10644 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
10645 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
10646 ad493afc 2019-08-03 stsp char *cwd = NULL;
10647 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
10648 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
10649 9627c110 2020-04-18 stsp int ch, pflag = 0;
10650 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10651 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
10652 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10653 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10654 ad493afc 2019-08-03 stsp
10655 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
10656 ad493afc 2019-08-03 stsp
10657 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
10658 ad493afc 2019-08-03 stsp switch (ch) {
10659 2e1f37b0 2019-08-08 stsp case 'p':
10660 2e1f37b0 2019-08-08 stsp pflag = 1;
10661 2e1f37b0 2019-08-08 stsp break;
10662 2e1f37b0 2019-08-08 stsp case 'F':
10663 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
10664 2e1f37b0 2019-08-08 stsp break;
10665 ad493afc 2019-08-03 stsp default:
10666 ad493afc 2019-08-03 stsp usage_unstage();
10667 ad493afc 2019-08-03 stsp /* NOTREACHED */
10668 ad493afc 2019-08-03 stsp }
10669 ad493afc 2019-08-03 stsp }
10670 ad493afc 2019-08-03 stsp
10671 ad493afc 2019-08-03 stsp argc -= optind;
10672 ad493afc 2019-08-03 stsp argv += optind;
10673 ad493afc 2019-08-03 stsp
10674 ad493afc 2019-08-03 stsp #ifndef PROFILE
10675 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10676 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
10677 ad493afc 2019-08-03 stsp err(1, "pledge");
10678 ad493afc 2019-08-03 stsp #endif
10679 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
10680 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
10681 2e1f37b0 2019-08-08 stsp
10682 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
10683 ad493afc 2019-08-03 stsp if (cwd == NULL) {
10684 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
10685 ad493afc 2019-08-03 stsp goto done;
10686 ad493afc 2019-08-03 stsp }
10687 ad493afc 2019-08-03 stsp
10688 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10689 fa51e947 2020-03-27 stsp if (error) {
10690 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10691 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
10692 ad493afc 2019-08-03 stsp goto done;
10693 fa51e947 2020-03-27 stsp }
10694 ad493afc 2019-08-03 stsp
10695 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10696 c9956ddf 2019-09-08 stsp NULL);
10697 ad493afc 2019-08-03 stsp if (error != NULL)
10698 ad493afc 2019-08-03 stsp goto done;
10699 ad493afc 2019-08-03 stsp
10700 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
10701 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
10702 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
10703 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
10704 2e1f37b0 2019-08-08 stsp patch_script_path);
10705 2e1f37b0 2019-08-08 stsp goto done;
10706 2e1f37b0 2019-08-08 stsp }
10707 2e1f37b0 2019-08-08 stsp }
10708 2e1f37b0 2019-08-08 stsp
10709 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10710 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
10711 ad493afc 2019-08-03 stsp if (error)
10712 ad493afc 2019-08-03 stsp goto done;
10713 ad493afc 2019-08-03 stsp
10714 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10715 ad493afc 2019-08-03 stsp if (error)
10716 ad493afc 2019-08-03 stsp goto done;
10717 ad493afc 2019-08-03 stsp
10718 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10719 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
10720 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10721 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
10722 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
10723 9627c110 2020-04-18 stsp if (!error)
10724 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10725 715dc77e 2019-08-03 stsp done:
10726 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10727 2e1f37b0 2019-08-08 stsp error == NULL)
10728 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10729 1d0f4054 2021-06-17 stsp if (repo) {
10730 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10731 1d0f4054 2021-06-17 stsp if (error == NULL)
10732 1d0f4054 2021-06-17 stsp error = close_err;
10733 1d0f4054 2021-06-17 stsp }
10734 715dc77e 2019-08-03 stsp if (worktree)
10735 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
10736 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10737 715dc77e 2019-08-03 stsp free((char *)pe->path);
10738 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
10739 715dc77e 2019-08-03 stsp free(cwd);
10740 01073a5d 2019-08-22 stsp return error;
10741 01073a5d 2019-08-22 stsp }
10742 01073a5d 2019-08-22 stsp
10743 01073a5d 2019-08-22 stsp __dead static void
10744 01073a5d 2019-08-22 stsp usage_cat(void)
10745 01073a5d 2019-08-22 stsp {
10746 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10747 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
10748 01073a5d 2019-08-22 stsp exit(1);
10749 01073a5d 2019-08-22 stsp }
10750 01073a5d 2019-08-22 stsp
10751 01073a5d 2019-08-22 stsp static const struct got_error *
10752 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10753 01073a5d 2019-08-22 stsp {
10754 01073a5d 2019-08-22 stsp const struct got_error *err;
10755 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
10756 01073a5d 2019-08-22 stsp
10757 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
10758 01073a5d 2019-08-22 stsp if (err)
10759 01073a5d 2019-08-22 stsp return err;
10760 01073a5d 2019-08-22 stsp
10761 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10762 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
10763 01073a5d 2019-08-22 stsp return err;
10764 01073a5d 2019-08-22 stsp }
10765 01073a5d 2019-08-22 stsp
10766 01073a5d 2019-08-22 stsp static const struct got_error *
10767 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10768 01073a5d 2019-08-22 stsp {
10769 01073a5d 2019-08-22 stsp const struct got_error *err;
10770 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
10771 56e0773d 2019-11-28 stsp int nentries, i;
10772 01073a5d 2019-08-22 stsp
10773 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
10774 01073a5d 2019-08-22 stsp if (err)
10775 01073a5d 2019-08-22 stsp return err;
10776 01073a5d 2019-08-22 stsp
10777 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
10778 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
10779 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
10780 01073a5d 2019-08-22 stsp char *id_str;
10781 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
10782 01073a5d 2019-08-22 stsp break;
10783 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
10784 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10785 01073a5d 2019-08-22 stsp if (err)
10786 01073a5d 2019-08-22 stsp break;
10787 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
10788 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
10789 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
10790 01073a5d 2019-08-22 stsp free(id_str);
10791 01073a5d 2019-08-22 stsp }
10792 01073a5d 2019-08-22 stsp
10793 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
10794 01073a5d 2019-08-22 stsp return err;
10795 01073a5d 2019-08-22 stsp }
10796 01073a5d 2019-08-22 stsp
10797 01073a5d 2019-08-22 stsp static const struct got_error *
10798 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10799 01073a5d 2019-08-22 stsp {
10800 01073a5d 2019-08-22 stsp const struct got_error *err;
10801 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
10802 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
10803 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
10804 01073a5d 2019-08-22 stsp char *id_str = NULL;
10805 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
10806 01073a5d 2019-08-22 stsp
10807 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
10808 01073a5d 2019-08-22 stsp if (err)
10809 01073a5d 2019-08-22 stsp return err;
10810 01073a5d 2019-08-22 stsp
10811 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10812 01073a5d 2019-08-22 stsp if (err)
10813 01073a5d 2019-08-22 stsp goto done;
10814 01073a5d 2019-08-22 stsp
10815 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10816 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10817 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
10818 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
10819 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
10820 01073a5d 2019-08-22 stsp char *pid_str;
10821 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
10822 01073a5d 2019-08-22 stsp if (err)
10823 01073a5d 2019-08-22 stsp goto done;
10824 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10825 01073a5d 2019-08-22 stsp free(pid_str);
10826 01073a5d 2019-08-22 stsp }
10827 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10828 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10829 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
10830 01073a5d 2019-08-22 stsp
10831 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10832 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10833 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
10834 01073a5d 2019-08-22 stsp
10835 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
10836 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10837 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
10838 01073a5d 2019-08-22 stsp done:
10839 01073a5d 2019-08-22 stsp free(id_str);
10840 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
10841 01073a5d 2019-08-22 stsp return err;
10842 01073a5d 2019-08-22 stsp }
10843 01073a5d 2019-08-22 stsp
10844 01073a5d 2019-08-22 stsp static const struct got_error *
10845 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10846 01073a5d 2019-08-22 stsp {
10847 01073a5d 2019-08-22 stsp const struct got_error *err;
10848 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
10849 01073a5d 2019-08-22 stsp char *id_str = NULL;
10850 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
10851 01073a5d 2019-08-22 stsp
10852 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
10853 01073a5d 2019-08-22 stsp if (err)
10854 01073a5d 2019-08-22 stsp return err;
10855 01073a5d 2019-08-22 stsp
10856 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10857 01073a5d 2019-08-22 stsp if (err)
10858 01073a5d 2019-08-22 stsp goto done;
10859 01073a5d 2019-08-22 stsp
10860 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10861 e15d5241 2019-08-22 stsp
10862 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
10863 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10864 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10865 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
10866 01073a5d 2019-08-22 stsp break;
10867 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10868 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10869 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
10870 01073a5d 2019-08-22 stsp break;
10871 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10872 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10873 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
10874 01073a5d 2019-08-22 stsp break;
10875 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10876 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10877 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
10878 01073a5d 2019-08-22 stsp break;
10879 01073a5d 2019-08-22 stsp default:
10880 01073a5d 2019-08-22 stsp break;
10881 01073a5d 2019-08-22 stsp }
10882 01073a5d 2019-08-22 stsp
10883 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10884 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
10885 e15d5241 2019-08-22 stsp
10886 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10887 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
10888 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
10889 01073a5d 2019-08-22 stsp
10890 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
10891 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10892 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
10893 01073a5d 2019-08-22 stsp done:
10894 01073a5d 2019-08-22 stsp free(id_str);
10895 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
10896 01073a5d 2019-08-22 stsp return err;
10897 01073a5d 2019-08-22 stsp }
10898 01073a5d 2019-08-22 stsp
10899 01073a5d 2019-08-22 stsp static const struct got_error *
10900 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
10901 01073a5d 2019-08-22 stsp {
10902 01073a5d 2019-08-22 stsp const struct got_error *error;
10903 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
10904 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
10905 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
10906 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
10907 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
10908 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
10909 84de9106 2020-12-26 stsp struct got_reflist_head refs;
10910 84de9106 2020-12-26 stsp
10911 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
10912 01073a5d 2019-08-22 stsp
10913 01073a5d 2019-08-22 stsp #ifndef PROFILE
10914 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10915 01073a5d 2019-08-22 stsp NULL) == -1)
10916 01073a5d 2019-08-22 stsp err(1, "pledge");
10917 01073a5d 2019-08-22 stsp #endif
10918 01073a5d 2019-08-22 stsp
10919 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10920 01073a5d 2019-08-22 stsp switch (ch) {
10921 896e9b6f 2019-08-26 stsp case 'c':
10922 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
10923 896e9b6f 2019-08-26 stsp break;
10924 01073a5d 2019-08-22 stsp case 'r':
10925 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
10926 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10927 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
10928 9ba1d308 2019-10-21 stsp optarg);
10929 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
10930 01073a5d 2019-08-22 stsp break;
10931 896e9b6f 2019-08-26 stsp case 'P':
10932 896e9b6f 2019-08-26 stsp force_path = 1;
10933 896e9b6f 2019-08-26 stsp break;
10934 01073a5d 2019-08-22 stsp default:
10935 01073a5d 2019-08-22 stsp usage_cat();
10936 01073a5d 2019-08-22 stsp /* NOTREACHED */
10937 01073a5d 2019-08-22 stsp }
10938 01073a5d 2019-08-22 stsp }
10939 01073a5d 2019-08-22 stsp
10940 01073a5d 2019-08-22 stsp argc -= optind;
10941 01073a5d 2019-08-22 stsp argv += optind;
10942 01073a5d 2019-08-22 stsp
10943 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
10944 01073a5d 2019-08-22 stsp if (cwd == NULL) {
10945 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
10946 01073a5d 2019-08-22 stsp goto done;
10947 01073a5d 2019-08-22 stsp }
10948 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
10949 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
10950 01073a5d 2019-08-22 stsp goto done;
10951 01073a5d 2019-08-22 stsp if (worktree) {
10952 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10953 01073a5d 2019-08-22 stsp repo_path = strdup(
10954 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
10955 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10956 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
10957 01073a5d 2019-08-22 stsp goto done;
10958 01073a5d 2019-08-22 stsp }
10959 01073a5d 2019-08-22 stsp }
10960 01073a5d 2019-08-22 stsp }
10961 01073a5d 2019-08-22 stsp
10962 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10963 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
10964 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10965 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
10966 01073a5d 2019-08-22 stsp }
10967 01073a5d 2019-08-22 stsp
10968 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
10969 01073a5d 2019-08-22 stsp free(repo_path);
10970 01073a5d 2019-08-22 stsp if (error != NULL)
10971 01073a5d 2019-08-22 stsp goto done;
10972 01073a5d 2019-08-22 stsp
10973 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
10974 896e9b6f 2019-08-26 stsp if (error)
10975 896e9b6f 2019-08-26 stsp goto done;
10976 896e9b6f 2019-08-26 stsp
10977 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10978 84de9106 2020-12-26 stsp if (error)
10979 84de9106 2020-12-26 stsp goto done;
10980 84de9106 2020-12-26 stsp
10981 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
10982 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
10983 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
10984 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
10985 01073a5d 2019-08-22 stsp if (error)
10986 01073a5d 2019-08-22 stsp goto done;
10987 01073a5d 2019-08-22 stsp
10988 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
10989 896e9b6f 2019-08-26 stsp if (force_path) {
10990 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
10991 896e9b6f 2019-08-26 stsp argv[i]);
10992 896e9b6f 2019-08-26 stsp if (error)
10993 896e9b6f 2019-08-26 stsp break;
10994 896e9b6f 2019-08-26 stsp } else {
10995 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
10996 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
10997 84de9106 2020-12-26 stsp repo);
10998 896e9b6f 2019-08-26 stsp if (error) {
10999 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11000 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
11001 896e9b6f 2019-08-26 stsp break;
11002 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
11003 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
11004 896e9b6f 2019-08-26 stsp if (error)
11005 896e9b6f 2019-08-26 stsp break;
11006 896e9b6f 2019-08-26 stsp }
11007 896e9b6f 2019-08-26 stsp }
11008 01073a5d 2019-08-22 stsp
11009 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
11010 01073a5d 2019-08-22 stsp if (error)
11011 01073a5d 2019-08-22 stsp break;
11012 01073a5d 2019-08-22 stsp
11013 01073a5d 2019-08-22 stsp switch (obj_type) {
11014 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
11015 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
11016 01073a5d 2019-08-22 stsp break;
11017 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
11018 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
11019 01073a5d 2019-08-22 stsp break;
11020 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
11021 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
11022 01073a5d 2019-08-22 stsp break;
11023 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11024 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
11025 01073a5d 2019-08-22 stsp break;
11026 01073a5d 2019-08-22 stsp default:
11027 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
11028 01073a5d 2019-08-22 stsp break;
11029 01073a5d 2019-08-22 stsp }
11030 01073a5d 2019-08-22 stsp if (error)
11031 01073a5d 2019-08-22 stsp break;
11032 01073a5d 2019-08-22 stsp free(label);
11033 01073a5d 2019-08-22 stsp label = NULL;
11034 01073a5d 2019-08-22 stsp free(id);
11035 01073a5d 2019-08-22 stsp id = NULL;
11036 01073a5d 2019-08-22 stsp }
11037 01073a5d 2019-08-22 stsp done:
11038 01073a5d 2019-08-22 stsp free(label);
11039 01073a5d 2019-08-22 stsp free(id);
11040 896e9b6f 2019-08-26 stsp free(commit_id);
11041 01073a5d 2019-08-22 stsp if (worktree)
11042 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
11043 01073a5d 2019-08-22 stsp if (repo) {
11044 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11045 01073a5d 2019-08-22 stsp if (error == NULL)
11046 1d0f4054 2021-06-17 stsp error = close_err;
11047 b2118c49 2020-07-28 stsp }
11048 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
11049 b2118c49 2020-07-28 stsp return error;
11050 b2118c49 2020-07-28 stsp }
11051 b2118c49 2020-07-28 stsp
11052 b2118c49 2020-07-28 stsp __dead static void
11053 b2118c49 2020-07-28 stsp usage_info(void)
11054 b2118c49 2020-07-28 stsp {
11055 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
11056 b2118c49 2020-07-28 stsp getprogname());
11057 b2118c49 2020-07-28 stsp exit(1);
11058 b2118c49 2020-07-28 stsp }
11059 b2118c49 2020-07-28 stsp
11060 b2118c49 2020-07-28 stsp static const struct got_error *
11061 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11062 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11063 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
11064 b2118c49 2020-07-28 stsp {
11065 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
11066 b2118c49 2020-07-28 stsp char *id_str = NULL;
11067 b2118c49 2020-07-28 stsp char datebuf[128];
11068 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
11069 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
11070 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11071 b2118c49 2020-07-28 stsp
11072 b2118c49 2020-07-28 stsp /*
11073 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
11074 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
11075 b2118c49 2020-07-28 stsp */
11076 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
11077 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
11078 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
11079 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
11080 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
11081 b2118c49 2020-07-28 stsp }
11082 b2118c49 2020-07-28 stsp
11083 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
11084 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
11085 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
11086 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
11087 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
11088 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11089 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
11090 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
11091 b2118c49 2020-07-28 stsp else
11092 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
11093 b2118c49 2020-07-28 stsp
11094 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
11095 b2118c49 2020-07-28 stsp if (tm == NULL)
11096 b2118c49 2020-07-28 stsp return NULL;
11097 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11098 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
11099 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
11100 b2118c49 2020-07-28 stsp
11101 b2118c49 2020-07-28 stsp if (blob_id) {
11102 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
11103 b2118c49 2020-07-28 stsp if (err)
11104 b2118c49 2020-07-28 stsp return err;
11105 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
11106 b2118c49 2020-07-28 stsp free(id_str);
11107 b2118c49 2020-07-28 stsp }
11108 b2118c49 2020-07-28 stsp
11109 b2118c49 2020-07-28 stsp if (staged_blob_id) {
11110 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
11111 b2118c49 2020-07-28 stsp if (err)
11112 b2118c49 2020-07-28 stsp return err;
11113 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
11114 b2118c49 2020-07-28 stsp free(id_str);
11115 b2118c49 2020-07-28 stsp }
11116 b2118c49 2020-07-28 stsp
11117 b2118c49 2020-07-28 stsp if (commit_id) {
11118 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
11119 b2118c49 2020-07-28 stsp if (err)
11120 b2118c49 2020-07-28 stsp return err;
11121 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
11122 b2118c49 2020-07-28 stsp free(id_str);
11123 b2118c49 2020-07-28 stsp }
11124 b2118c49 2020-07-28 stsp
11125 b2118c49 2020-07-28 stsp return NULL;
11126 b2118c49 2020-07-28 stsp }
11127 b2118c49 2020-07-28 stsp
11128 b2118c49 2020-07-28 stsp static const struct got_error *
11129 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
11130 b2118c49 2020-07-28 stsp {
11131 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
11132 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
11133 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
11134 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
11135 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11136 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
11137 b2118c49 2020-07-28 stsp int ch, show_files = 0;
11138 b2118c49 2020-07-28 stsp
11139 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
11140 b2118c49 2020-07-28 stsp
11141 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
11142 b2118c49 2020-07-28 stsp switch (ch) {
11143 b2118c49 2020-07-28 stsp default:
11144 b2118c49 2020-07-28 stsp usage_info();
11145 b2118c49 2020-07-28 stsp /* NOTREACHED */
11146 b2118c49 2020-07-28 stsp }
11147 01073a5d 2019-08-22 stsp }
11148 b2118c49 2020-07-28 stsp
11149 b2118c49 2020-07-28 stsp argc -= optind;
11150 b2118c49 2020-07-28 stsp argv += optind;
11151 b2118c49 2020-07-28 stsp
11152 b2118c49 2020-07-28 stsp #ifndef PROFILE
11153 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11154 b2118c49 2020-07-28 stsp NULL) == -1)
11155 b2118c49 2020-07-28 stsp err(1, "pledge");
11156 b2118c49 2020-07-28 stsp #endif
11157 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
11158 b2118c49 2020-07-28 stsp if (cwd == NULL) {
11159 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
11160 b2118c49 2020-07-28 stsp goto done;
11161 b2118c49 2020-07-28 stsp }
11162 b2118c49 2020-07-28 stsp
11163 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
11164 b2118c49 2020-07-28 stsp if (error) {
11165 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11166 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
11167 b2118c49 2020-07-28 stsp goto done;
11168 b2118c49 2020-07-28 stsp }
11169 b2118c49 2020-07-28 stsp
11170 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11171 b2118c49 2020-07-28 stsp if (error)
11172 b2118c49 2020-07-28 stsp goto done;
11173 b2118c49 2020-07-28 stsp
11174 b2118c49 2020-07-28 stsp if (argc >= 1) {
11175 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
11176 b2118c49 2020-07-28 stsp worktree);
11177 b2118c49 2020-07-28 stsp if (error)
11178 b2118c49 2020-07-28 stsp goto done;
11179 b2118c49 2020-07-28 stsp show_files = 1;
11180 b2118c49 2020-07-28 stsp }
11181 b2118c49 2020-07-28 stsp
11182 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
11183 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
11184 b2118c49 2020-07-28 stsp if (error)
11185 b2118c49 2020-07-28 stsp goto done;
11186 b2118c49 2020-07-28 stsp
11187 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
11188 b2118c49 2020-07-28 stsp if (error)
11189 b2118c49 2020-07-28 stsp goto done;
11190 b2118c49 2020-07-28 stsp
11191 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11192 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
11193 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
11194 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
11195 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
11196 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
11197 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
11198 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11199 b2118c49 2020-07-28 stsp
11200 b2118c49 2020-07-28 stsp if (show_files) {
11201 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11202 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11203 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
11204 b2118c49 2020-07-28 stsp continue;
11205 b2118c49 2020-07-28 stsp /*
11206 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
11207 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
11208 b2118c49 2020-07-28 stsp */
11209 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
11210 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
11211 b2118c49 2020-07-28 stsp }
11212 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
11213 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
11214 b2118c49 2020-07-28 stsp if (error)
11215 b2118c49 2020-07-28 stsp goto done;
11216 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11217 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
11218 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
11219 b2118c49 2020-07-28 stsp break;
11220 b2118c49 2020-07-28 stsp }
11221 b2118c49 2020-07-28 stsp }
11222 b2118c49 2020-07-28 stsp }
11223 b2118c49 2020-07-28 stsp done:
11224 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
11225 b2118c49 2020-07-28 stsp free((char *)pe->path);
11226 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
11227 b2118c49 2020-07-28 stsp free(cwd);
11228 b2118c49 2020-07-28 stsp free(id_str);
11229 b2118c49 2020-07-28 stsp free(uuidstr);
11230 0ebf8283 2019-07-24 stsp return error;
11231 0ebf8283 2019-07-24 stsp }