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 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
23 f42b1b34 2018-03-12 stsp
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12463d8b 2019-12-13 stsp #include <fcntl.h>
27 12ce7a6c 2019-08-12 stsp #include <limits.h>
28 5c860e29 2018-03-12 stsp #include <locale.h>
29 818c7501 2019-07-11 stsp #include <ctype.h>
30 99437157 2018-11-11 stsp #include <signal.h>
31 5c860e29 2018-03-12 stsp #include <stdio.h>
32 5c860e29 2018-03-12 stsp #include <stdlib.h>
33 5c860e29 2018-03-12 stsp #include <string.h>
34 5c860e29 2018-03-12 stsp #include <unistd.h>
35 c09a553d 2018-03-12 stsp #include <libgen.h>
36 c0768b0f 2018-06-10 stsp #include <time.h>
37 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 6841bf13 2019-11-29 kn #include <regex.h>
39 83cd27f8 2020-01-13 stsp #include <getopt.h>
40 d2cdc636 2020-03-18 stsp #include <util.h>
41 5c860e29 2018-03-12 stsp
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 f8a36e22 2021-08-26 stsp #include "got_send.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
58 d65a88a2 2021-09-05 stsp #include "got_dial.h"
59 e9ce266e 2022-03-07 op #include "got_patch.h"
60 5c860e29 2018-03-12 stsp
61 5c860e29 2018-03-12 stsp #ifndef nitems
62 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 5c860e29 2018-03-12 stsp #endif
64 99437157 2018-11-11 stsp
65 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
66 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
67 99437157 2018-11-11 stsp
68 99437157 2018-11-11 stsp static void
69 99437157 2018-11-11 stsp catch_sigint(int signo)
70 99437157 2018-11-11 stsp {
71 99437157 2018-11-11 stsp sigint_received = 1;
72 99437157 2018-11-11 stsp }
73 99437157 2018-11-11 stsp
74 99437157 2018-11-11 stsp static void
75 99437157 2018-11-11 stsp catch_sigpipe(int signo)
76 99437157 2018-11-11 stsp {
77 99437157 2018-11-11 stsp sigpipe_received = 1;
78 99437157 2018-11-11 stsp }
79 5c860e29 2018-03-12 stsp
80 99437157 2018-11-11 stsp
81 8cfb4057 2019-07-09 stsp struct got_cmd {
82 820059fa 2020-09-25 stsp const char *cmd_name;
83 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
84 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
85 97b3a7be 2019-07-09 stsp const char *cmd_alias;
86 5c860e29 2018-03-12 stsp };
87 5c860e29 2018-03-12 stsp
88 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
89 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
90 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
91 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
92 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
93 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
94 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
95 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
96 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
97 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
98 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
99 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
100 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
101 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
102 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
103 d00136be 2019-03-26 stsp __dead static void usage_add(void);
104 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
105 e9ce266e 2022-03-07 op __dead static void usage_patch(void);
106 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
107 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
108 f8a36e22 2021-08-26 stsp __dead static void usage_send(void);
109 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
110 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
111 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
112 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
113 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
114 f259c4c1 2021-09-24 stsp __dead static void usage_merge(void);
115 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
116 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
117 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
118 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
119 5c860e29 2018-03-12 stsp
120 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
121 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
122 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
123 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
124 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
125 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
126 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
127 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
128 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
129 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
130 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
131 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
132 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
133 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
134 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
135 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
136 e9ce266e 2022-03-07 op static const struct got_error* cmd_patch(int, char *[]);
137 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
138 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
139 f8a36e22 2021-08-26 stsp static const struct got_error* cmd_send(int, char *[]);
140 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
141 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
142 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
143 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
144 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
145 f259c4c1 2021-09-24 stsp static const struct got_error* cmd_merge(int, char *[]);
146 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
147 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
148 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
149 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
150 5c860e29 2018-03-12 stsp
151 3e166534 2022-02-16 naddy static const struct got_cmd got_commands[] = {
152 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
153 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
154 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
155 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
156 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
157 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
158 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
159 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
160 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
161 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
162 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
163 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
164 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
165 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
166 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
167 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
168 e9ce266e 2022-03-07 op { "patch", cmd_patch, usage_patch, "pa" },
169 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
170 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
171 f8a36e22 2021-08-26 stsp { "send", cmd_send, usage_send, "se" },
172 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
173 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
174 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
175 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
176 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
177 f259c4c1 2021-09-24 stsp { "merge", cmd_merge, usage_merge, "mg" },
178 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
179 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
180 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
181 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
182 5c860e29 2018-03-12 stsp };
183 ce5b7c56 2019-07-09 stsp
184 ce5b7c56 2019-07-09 stsp static void
185 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
186 ce5b7c56 2019-07-09 stsp {
187 6059809a 2020-12-17 stsp size_t i;
188 ce5b7c56 2019-07-09 stsp
189 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
190 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
191 3e166534 2022-02-16 naddy const struct got_cmd *cmd = &got_commands[i];
192 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
193 ce5b7c56 2019-07-09 stsp }
194 6879ba42 2020-10-01 naddy fputc('\n', fp);
195 ce5b7c56 2019-07-09 stsp }
196 5c860e29 2018-03-12 stsp
197 ff69268e 2020-12-13 stsp __dead static void
198 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
199 ff69268e 2020-12-13 stsp {
200 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
201 ff69268e 2020-12-13 stsp }
202 ff69268e 2020-12-13 stsp
203 5c860e29 2018-03-12 stsp int
204 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
205 5c860e29 2018-03-12 stsp {
206 3e166534 2022-02-16 naddy const struct got_cmd *cmd;
207 6059809a 2020-12-17 stsp size_t i;
208 5c860e29 2018-03-12 stsp int ch;
209 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
210 3e166534 2022-02-16 naddy static const struct option longopts[] = {
211 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
212 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
213 83cd27f8 2020-01-13 stsp };
214 5c860e29 2018-03-12 stsp
215 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
216 5c860e29 2018-03-12 stsp
217 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
218 5c860e29 2018-03-12 stsp switch (ch) {
219 1b6b95a8 2018-03-12 stsp case 'h':
220 1b6b95a8 2018-03-12 stsp hflag = 1;
221 53ccebc2 2019-07-30 stsp break;
222 53ccebc2 2019-07-30 stsp case 'V':
223 53ccebc2 2019-07-30 stsp Vflag = 1;
224 1b6b95a8 2018-03-12 stsp break;
225 5c860e29 2018-03-12 stsp default:
226 6879ba42 2020-10-01 naddy usage(hflag, 1);
227 5c860e29 2018-03-12 stsp /* NOTREACHED */
228 5c860e29 2018-03-12 stsp }
229 5c860e29 2018-03-12 stsp }
230 5c860e29 2018-03-12 stsp
231 5c860e29 2018-03-12 stsp argc -= optind;
232 5c860e29 2018-03-12 stsp argv += optind;
233 9814e6a3 2020-09-27 naddy optind = 1;
234 9814e6a3 2020-09-27 naddy optreset = 1;
235 53ccebc2 2019-07-30 stsp
236 53ccebc2 2019-07-30 stsp if (Vflag) {
237 53ccebc2 2019-07-30 stsp got_version_print_str();
238 6879ba42 2020-10-01 naddy return 0;
239 53ccebc2 2019-07-30 stsp }
240 5c860e29 2018-03-12 stsp
241 5c860e29 2018-03-12 stsp if (argc <= 0)
242 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
243 5c860e29 2018-03-12 stsp
244 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
245 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
246 99437157 2018-11-11 stsp
247 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
248 d7d4f210 2018-03-12 stsp const struct got_error *error;
249 d7d4f210 2018-03-12 stsp
250 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
251 5c860e29 2018-03-12 stsp
252 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
253 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
254 5c860e29 2018-03-12 stsp continue;
255 5c860e29 2018-03-12 stsp
256 1b6b95a8 2018-03-12 stsp if (hflag)
257 3e166534 2022-02-16 naddy cmd->cmd_usage();
258 1b6b95a8 2018-03-12 stsp
259 3e166534 2022-02-16 naddy error = cmd->cmd_main(argc, argv);
260 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
261 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
262 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
263 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
264 70015d7a 2019-11-08 stsp !(sigint_received &&
265 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
266 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
267 d7d4f210 2018-03-12 stsp return 1;
268 d7d4f210 2018-03-12 stsp }
269 d7d4f210 2018-03-12 stsp
270 d7d4f210 2018-03-12 stsp return 0;
271 5c860e29 2018-03-12 stsp }
272 5c860e29 2018-03-12 stsp
273 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
274 6879ba42 2020-10-01 naddy list_commands(stderr);
275 5c860e29 2018-03-12 stsp return 1;
276 5c860e29 2018-03-12 stsp }
277 5c860e29 2018-03-12 stsp
278 4ed7e80c 2018-05-20 stsp __dead static void
279 6879ba42 2020-10-01 naddy usage(int hflag, int status)
280 5c860e29 2018-03-12 stsp {
281 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
282 6879ba42 2020-10-01 naddy
283 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
284 53ccebc2 2019-07-30 stsp getprogname());
285 ce5b7c56 2019-07-09 stsp if (hflag)
286 6879ba42 2020-10-01 naddy list_commands(fp);
287 6879ba42 2020-10-01 naddy exit(status);
288 5c860e29 2018-03-12 stsp }
289 5c860e29 2018-03-12 stsp
290 0266afb7 2019-01-04 stsp static const struct got_error *
291 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
292 e2ba3d07 2019-05-13 stsp {
293 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
294 e2ba3d07 2019-05-13 stsp const char *editor;
295 8920fa04 2019-08-18 stsp
296 8920fa04 2019-08-18 stsp *abspath = NULL;
297 e2ba3d07 2019-05-13 stsp
298 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
299 e2ba3d07 2019-05-13 stsp if (editor == NULL)
300 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
301 e2ba3d07 2019-05-13 stsp
302 0ee7065d 2019-05-13 stsp if (editor) {
303 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
304 0ee7065d 2019-05-13 stsp if (err)
305 0ee7065d 2019-05-13 stsp return err;
306 0ee7065d 2019-05-13 stsp }
307 e2ba3d07 2019-05-13 stsp
308 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
309 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
310 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
311 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
312 0ee7065d 2019-05-13 stsp }
313 0ee7065d 2019-05-13 stsp
314 e2ba3d07 2019-05-13 stsp return NULL;
315 e2ba3d07 2019-05-13 stsp }
316 e2ba3d07 2019-05-13 stsp
317 e2ba3d07 2019-05-13 stsp static const struct got_error *
318 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
319 c530dc23 2019-07-23 stsp const char *worktree_path)
320 0266afb7 2019-01-04 stsp {
321 163ce85a 2019-05-13 stsp const struct got_error *err;
322 0266afb7 2019-01-04 stsp
323 37c06ea4 2019-07-15 stsp #ifdef PROFILE
324 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
325 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
326 37c06ea4 2019-07-15 stsp #endif
327 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
328 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
329 0266afb7 2019-01-04 stsp
330 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
331 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
332 0266afb7 2019-01-04 stsp
333 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
334 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
335 0266afb7 2019-01-04 stsp
336 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
337 163ce85a 2019-05-13 stsp if (err != NULL)
338 163ce85a 2019-05-13 stsp return err;
339 0266afb7 2019-01-04 stsp
340 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
341 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
342 0266afb7 2019-01-04 stsp
343 0266afb7 2019-01-04 stsp return NULL;
344 2c7829a4 2019-06-17 stsp }
345 2c7829a4 2019-06-17 stsp
346 2c7829a4 2019-06-17 stsp __dead static void
347 2c7829a4 2019-06-17 stsp usage_init(void)
348 2c7829a4 2019-06-17 stsp {
349 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
350 2c7829a4 2019-06-17 stsp exit(1);
351 2c7829a4 2019-06-17 stsp }
352 2c7829a4 2019-06-17 stsp
353 2c7829a4 2019-06-17 stsp static const struct got_error *
354 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
355 2c7829a4 2019-06-17 stsp {
356 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
357 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
358 2c7829a4 2019-06-17 stsp int ch;
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
361 2c7829a4 2019-06-17 stsp switch (ch) {
362 2c7829a4 2019-06-17 stsp default:
363 2c7829a4 2019-06-17 stsp usage_init();
364 2c7829a4 2019-06-17 stsp /* NOTREACHED */
365 2c7829a4 2019-06-17 stsp }
366 2c7829a4 2019-06-17 stsp }
367 2c7829a4 2019-06-17 stsp
368 2c7829a4 2019-06-17 stsp argc -= optind;
369 2c7829a4 2019-06-17 stsp argv += optind;
370 2c7829a4 2019-06-17 stsp
371 2c7829a4 2019-06-17 stsp #ifndef PROFILE
372 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
373 2c7829a4 2019-06-17 stsp err(1, "pledge");
374 2c7829a4 2019-06-17 stsp #endif
375 2c7829a4 2019-06-17 stsp if (argc != 1)
376 bc20e173 2019-06-17 stsp usage_init();
377 2c7829a4 2019-06-17 stsp
378 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
379 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
380 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
381 2c7829a4 2019-06-17 stsp
382 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
383 2c7829a4 2019-06-17 stsp
384 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
385 2c7829a4 2019-06-17 stsp if (error &&
386 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
387 2c7829a4 2019-06-17 stsp goto done;
388 2c7829a4 2019-06-17 stsp
389 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
390 2c7829a4 2019-06-17 stsp if (error)
391 2c7829a4 2019-06-17 stsp goto done;
392 2c7829a4 2019-06-17 stsp
393 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
394 3ce1b845 2019-07-15 stsp done:
395 3ce1b845 2019-07-15 stsp free(repo_path);
396 3ce1b845 2019-07-15 stsp return error;
397 3ce1b845 2019-07-15 stsp }
398 3ce1b845 2019-07-15 stsp
399 3ce1b845 2019-07-15 stsp __dead static void
400 3ce1b845 2019-07-15 stsp usage_import(void)
401 3ce1b845 2019-07-15 stsp {
402 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
403 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
404 3ce1b845 2019-07-15 stsp exit(1);
405 3ce1b845 2019-07-15 stsp }
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp int
408 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
409 3ce1b845 2019-07-15 stsp {
410 3ce1b845 2019-07-15 stsp pid_t pid;
411 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
412 3ce1b845 2019-07-15 stsp int st = -1;
413 3ce1b845 2019-07-15 stsp
414 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
415 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
416 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
417 3ce1b845 2019-07-15 stsp
418 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
419 3ce1b845 2019-07-15 stsp case -1:
420 3ce1b845 2019-07-15 stsp goto doneediting;
421 3ce1b845 2019-07-15 stsp case 0:
422 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
423 3ce1b845 2019-07-15 stsp _exit(127);
424 3ce1b845 2019-07-15 stsp }
425 3ce1b845 2019-07-15 stsp
426 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
427 3ce1b845 2019-07-15 stsp if (errno != EINTR)
428 3ce1b845 2019-07-15 stsp break;
429 3ce1b845 2019-07-15 stsp
430 3ce1b845 2019-07-15 stsp doneediting:
431 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
432 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
433 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
436 3ce1b845 2019-07-15 stsp errno = EINTR;
437 3ce1b845 2019-07-15 stsp return -1;
438 3ce1b845 2019-07-15 stsp }
439 3ce1b845 2019-07-15 stsp
440 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
441 3ce1b845 2019-07-15 stsp }
442 3ce1b845 2019-07-15 stsp
443 3ce1b845 2019-07-15 stsp static const struct got_error *
444 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
445 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
446 28cf319f 2021-01-28 stsp int require_modification)
447 3ce1b845 2019-07-15 stsp {
448 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
449 bfa12d5e 2020-09-26 stsp char *line = NULL;
450 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
451 bfa12d5e 2020-09-26 stsp ssize_t linelen;
452 3ce1b845 2019-07-15 stsp struct stat st, st2;
453 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
454 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
455 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
456 3ce1b845 2019-07-15 stsp
457 3ce1b845 2019-07-15 stsp *logmsg = NULL;
458 3ce1b845 2019-07-15 stsp
459 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
460 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
461 3ce1b845 2019-07-15 stsp
462 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
463 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
464 3ce1b845 2019-07-15 stsp
465 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
466 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
467 3ce1b845 2019-07-15 stsp
468 28cf319f 2021-01-28 stsp if (require_modification &&
469 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
470 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
471 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
472 3ce1b845 2019-07-15 stsp
473 bfa12d5e 2020-09-26 stsp /*
474 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
475 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
476 bfa12d5e 2020-09-26 stsp * has in fact been edited.
477 bfa12d5e 2020-09-26 stsp */
478 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
479 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
480 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
481 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
482 bfa12d5e 2020-09-26 stsp
483 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
484 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
485 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
486 bfa12d5e 2020-09-26 stsp goto done;
487 bfa12d5e 2020-09-26 stsp }
488 bfa12d5e 2020-09-26 stsp s = buf;
489 bfa12d5e 2020-09-26 stsp len = 0;
490 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
491 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
492 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
493 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
494 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
495 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
496 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
497 bfa12d5e 2020-09-26 stsp goto done;
498 bfa12d5e 2020-09-26 stsp }
499 bfa12d5e 2020-09-26 stsp }
500 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
501 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
502 bfa12d5e 2020-09-26 stsp len--;
503 bfa12d5e 2020-09-26 stsp }
504 bfa12d5e 2020-09-26 stsp
505 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
506 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
507 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
508 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
509 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
510 3ce1b845 2019-07-15 stsp
511 00fe21f2 2021-12-31 stsp fp = fopen(logmsg_path, "re");
512 3ce1b845 2019-07-15 stsp if (fp == NULL) {
513 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
514 3ce1b845 2019-07-15 stsp goto done;
515 3ce1b845 2019-07-15 stsp }
516 bfa12d5e 2020-09-26 stsp
517 bfa12d5e 2020-09-26 stsp len = 0;
518 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
519 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
520 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
521 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
522 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
523 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
524 bfa12d5e 2020-09-26 stsp goto done;
525 bfa12d5e 2020-09-26 stsp }
526 3ce1b845 2019-07-15 stsp }
527 bfa12d5e 2020-09-26 stsp free(line);
528 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
529 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
530 bfa12d5e 2020-09-26 stsp goto done;
531 bfa12d5e 2020-09-26 stsp }
532 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
533 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
534 3ce1b845 2019-07-15 stsp len--;
535 3ce1b845 2019-07-15 stsp }
536 3ce1b845 2019-07-15 stsp
537 bfa12d5e 2020-09-26 stsp if (len == 0) {
538 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
539 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
540 bfa12d5e 2020-09-26 stsp goto done;
541 bfa12d5e 2020-09-26 stsp }
542 28cf319f 2021-01-28 stsp if (require_modification &&
543 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
544 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
545 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
546 3ce1b845 2019-07-15 stsp done:
547 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
548 bfa12d5e 2020-09-26 stsp free(buf);
549 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
550 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
551 3ce1b845 2019-07-15 stsp if (err) {
552 3ce1b845 2019-07-15 stsp free(*logmsg);
553 3ce1b845 2019-07-15 stsp *logmsg = NULL;
554 3ce1b845 2019-07-15 stsp }
555 3ce1b845 2019-07-15 stsp return err;
556 3ce1b845 2019-07-15 stsp }
557 3ce1b845 2019-07-15 stsp
558 3ce1b845 2019-07-15 stsp static const struct got_error *
559 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
560 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
561 3ce1b845 2019-07-15 stsp {
562 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
563 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
564 1601cb9f 2020-09-11 naddy int initial_content_len;
565 97972933 2020-09-11 stsp int fd = -1;
566 3ce1b845 2019-07-15 stsp
567 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
568 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
569 1601cb9f 2020-09-11 naddy branch_name);
570 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
571 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
572 3ce1b845 2019-07-15 stsp
573 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
574 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
575 3ce1b845 2019-07-15 stsp if (err)
576 3ce1b845 2019-07-15 stsp goto done;
577 3ce1b845 2019-07-15 stsp
578 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
579 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
580 97972933 2020-09-11 stsp goto done;
581 97972933 2020-09-11 stsp }
582 3ce1b845 2019-07-15 stsp
583 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
584 0d5bb276 2020-12-15 stsp initial_content_len, 1);
585 3ce1b845 2019-07-15 stsp done:
586 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
587 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
588 3ce1b845 2019-07-15 stsp free(initial_content);
589 59f86c76 2020-09-11 stsp if (err) {
590 59f86c76 2020-09-11 stsp free(*logmsg_path);
591 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
592 59f86c76 2020-09-11 stsp }
593 3ce1b845 2019-07-15 stsp return err;
594 3ce1b845 2019-07-15 stsp }
595 3ce1b845 2019-07-15 stsp
596 3ce1b845 2019-07-15 stsp static const struct got_error *
597 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
598 3ce1b845 2019-07-15 stsp {
599 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
600 3ce1b845 2019-07-15 stsp return NULL;
601 1d918cf9 2022-02-06 op }
602 1d918cf9 2022-02-06 op
603 1d918cf9 2022-02-06 op static int
604 1d918cf9 2022-02-06 op valid_author(const char *author)
605 1d918cf9 2022-02-06 op {
606 1d918cf9 2022-02-06 op /*
607 1d918cf9 2022-02-06 op * Really dumb email address check; we're only doing this to
608 1d918cf9 2022-02-06 op * avoid git's object parser breaking on commits we create.
609 1d918cf9 2022-02-06 op */
610 1d918cf9 2022-02-06 op while (*author && *author != '<')
611 1d918cf9 2022-02-06 op author++;
612 1d918cf9 2022-02-06 op if (*author != '<')
613 1d918cf9 2022-02-06 op return 0;
614 1d918cf9 2022-02-06 op while (*author && *author != '@')
615 1d918cf9 2022-02-06 op author++;
616 1d918cf9 2022-02-06 op if (*author != '@')
617 1d918cf9 2022-02-06 op return 0;
618 1d918cf9 2022-02-06 op while (*author && *author != '>')
619 1d918cf9 2022-02-06 op author++;
620 1d918cf9 2022-02-06 op return *author == '>';
621 3ce1b845 2019-07-15 stsp }
622 3ce1b845 2019-07-15 stsp
623 3ce1b845 2019-07-15 stsp static const struct got_error *
624 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
625 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
626 84792843 2019-08-09 stsp {
627 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
628 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
629 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
630 84792843 2019-08-09 stsp
631 84792843 2019-08-09 stsp *author = NULL;
632 aba9c984 2019-09-08 stsp
633 50b0790e 2020-09-11 stsp if (worktree)
634 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
635 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
636 50b0790e 2020-09-11 stsp
637 50b0790e 2020-09-11 stsp /*
638 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
639 50b0790e 2020-09-11 stsp * significant to least significant:
640 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
641 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
642 50b0790e 2020-09-11 stsp * 3) repository's git config file
643 50b0790e 2020-09-11 stsp * 4) environment variables
644 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
645 50b0790e 2020-09-11 stsp */
646 50b0790e 2020-09-11 stsp
647 50b0790e 2020-09-11 stsp if (worktree_conf)
648 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
649 50b0790e 2020-09-11 stsp if (got_author == NULL)
650 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
651 84792843 2019-08-09 stsp if (got_author == NULL) {
652 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
653 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
654 c9956ddf 2019-09-08 stsp if (name && email) {
655 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
656 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
657 c9956ddf 2019-09-08 stsp return NULL;
658 c9956ddf 2019-09-08 stsp }
659 257add31 2020-09-09 stsp
660 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
661 257add31 2020-09-09 stsp if (got_author == NULL) {
662 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
663 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
664 257add31 2020-09-09 stsp repo);
665 257add31 2020-09-09 stsp if (name && email) {
666 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
667 257add31 2020-09-09 stsp == -1)
668 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
669 257add31 2020-09-09 stsp return NULL;
670 257add31 2020-09-09 stsp }
671 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
672 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
673 257add31 2020-09-09 stsp }
674 84792843 2019-08-09 stsp }
675 84792843 2019-08-09 stsp
676 aba9c984 2019-09-08 stsp *author = strdup(got_author);
677 aba9c984 2019-09-08 stsp if (*author == NULL)
678 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
679 84792843 2019-08-09 stsp
680 1d918cf9 2022-02-06 op if (!valid_author(*author)) {
681 1d918cf9 2022-02-06 op err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
682 aba9c984 2019-09-08 stsp free(*author);
683 aba9c984 2019-09-08 stsp *author = NULL;
684 aba9c984 2019-09-08 stsp }
685 aba9c984 2019-09-08 stsp return err;
686 c9956ddf 2019-09-08 stsp }
687 c9956ddf 2019-09-08 stsp
688 c9956ddf 2019-09-08 stsp static const struct got_error *
689 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
690 c9956ddf 2019-09-08 stsp {
691 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
692 c9956ddf 2019-09-08 stsp
693 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
694 c9956ddf 2019-09-08 stsp if (homedir) {
695 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
696 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
697 c9956ddf 2019-09-08 stsp
698 c9956ddf 2019-09-08 stsp }
699 c9956ddf 2019-09-08 stsp return NULL;
700 84792843 2019-08-09 stsp }
701 84792843 2019-08-09 stsp
702 84792843 2019-08-09 stsp static const struct got_error *
703 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
704 3ce1b845 2019-07-15 stsp {
705 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
706 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
707 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
708 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
709 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
710 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
711 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
712 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
713 3ce1b845 2019-07-15 stsp int ch;
714 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
715 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
716 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
717 3ce1b845 2019-07-15 stsp
718 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
719 3ce1b845 2019-07-15 stsp
720 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
721 3ce1b845 2019-07-15 stsp switch (ch) {
722 3ce1b845 2019-07-15 stsp case 'b':
723 3ce1b845 2019-07-15 stsp branch_name = optarg;
724 3ce1b845 2019-07-15 stsp break;
725 3ce1b845 2019-07-15 stsp case 'm':
726 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
727 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
728 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
729 3ce1b845 2019-07-15 stsp goto done;
730 3ce1b845 2019-07-15 stsp }
731 3ce1b845 2019-07-15 stsp break;
732 3ce1b845 2019-07-15 stsp case 'r':
733 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
734 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
735 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
736 9ba1d308 2019-10-21 stsp optarg);
737 3ce1b845 2019-07-15 stsp goto done;
738 3ce1b845 2019-07-15 stsp }
739 3ce1b845 2019-07-15 stsp break;
740 3ce1b845 2019-07-15 stsp case 'I':
741 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
742 3ce1b845 2019-07-15 stsp break;
743 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
744 3ce1b845 2019-07-15 stsp NULL);
745 3ce1b845 2019-07-15 stsp if (error)
746 3ce1b845 2019-07-15 stsp goto done;
747 3ce1b845 2019-07-15 stsp break;
748 3ce1b845 2019-07-15 stsp default:
749 b2b65d18 2019-08-22 stsp usage_import();
750 3ce1b845 2019-07-15 stsp /* NOTREACHED */
751 3ce1b845 2019-07-15 stsp }
752 3ce1b845 2019-07-15 stsp }
753 3ce1b845 2019-07-15 stsp
754 3ce1b845 2019-07-15 stsp argc -= optind;
755 3ce1b845 2019-07-15 stsp argv += optind;
756 3ce1b845 2019-07-15 stsp
757 3ce1b845 2019-07-15 stsp #ifndef PROFILE
758 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
759 aba9c984 2019-09-08 stsp "unveil",
760 3ce1b845 2019-07-15 stsp NULL) == -1)
761 3ce1b845 2019-07-15 stsp err(1, "pledge");
762 3ce1b845 2019-07-15 stsp #endif
763 3ce1b845 2019-07-15 stsp if (argc != 1)
764 3ce1b845 2019-07-15 stsp usage_import();
765 2c7829a4 2019-06-17 stsp
766 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
767 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
768 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
769 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
770 3ce1b845 2019-07-15 stsp }
771 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
772 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
773 c9956ddf 2019-09-08 stsp if (error)
774 c9956ddf 2019-09-08 stsp goto done;
775 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
776 3ce1b845 2019-07-15 stsp if (error)
777 3ce1b845 2019-07-15 stsp goto done;
778 aba9c984 2019-09-08 stsp
779 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
780 aba9c984 2019-09-08 stsp if (error)
781 aba9c984 2019-09-08 stsp return error;
782 e560b7e0 2019-11-28 stsp
783 e560b7e0 2019-11-28 stsp /*
784 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
785 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
786 e560b7e0 2019-11-28 stsp * an unintended typo.
787 e560b7e0 2019-11-28 stsp */
788 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
789 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
790 3ce1b845 2019-07-15 stsp
791 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
792 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
793 3ce1b845 2019-07-15 stsp goto done;
794 3ce1b845 2019-07-15 stsp }
795 3ce1b845 2019-07-15 stsp
796 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
797 3ce1b845 2019-07-15 stsp if (error) {
798 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
799 3ce1b845 2019-07-15 stsp goto done;
800 3ce1b845 2019-07-15 stsp } else {
801 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
802 3ce1b845 2019-07-15 stsp "import target branch already exists");
803 3ce1b845 2019-07-15 stsp goto done;
804 3ce1b845 2019-07-15 stsp }
805 3ce1b845 2019-07-15 stsp
806 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
807 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
808 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
809 3ce1b845 2019-07-15 stsp goto done;
810 3ce1b845 2019-07-15 stsp }
811 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
812 3ce1b845 2019-07-15 stsp
813 3ce1b845 2019-07-15 stsp /*
814 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
815 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
816 3ce1b845 2019-07-15 stsp */
817 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
818 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
819 3ce1b845 2019-07-15 stsp if (error)
820 3ce1b845 2019-07-15 stsp goto done;
821 8e158b01 2019-09-22 stsp free(logmsg);
822 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
823 ef293bdd 2019-10-21 stsp path_dir, refname);
824 ef293bdd 2019-10-21 stsp if (error) {
825 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
826 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
827 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
828 3ce1b845 2019-07-15 stsp goto done;
829 ef293bdd 2019-10-21 stsp }
830 3ce1b845 2019-07-15 stsp }
831 3ce1b845 2019-07-15 stsp
832 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
833 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
834 ef293bdd 2019-10-21 stsp if (logmsg_path)
835 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
836 3ce1b845 2019-07-15 stsp goto done;
837 ef293bdd 2019-10-21 stsp }
838 3ce1b845 2019-07-15 stsp
839 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
840 ef293bdd 2019-10-21 stsp if (error) {
841 ef293bdd 2019-10-21 stsp if (logmsg_path)
842 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
843 ef293bdd 2019-10-21 stsp goto done;
844 ef293bdd 2019-10-21 stsp }
845 ef293bdd 2019-10-21 stsp
846 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
847 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
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_alloc(&branch_ref, refname, new_commit_id);
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_ref_write(branch_ref, repo);
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_object_id_str(&id_str, new_commit_id);
869 ef293bdd 2019-10-21 stsp if (error) {
870 ef293bdd 2019-10-21 stsp if (logmsg_path)
871 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
872 3ce1b845 2019-07-15 stsp goto done;
873 ef293bdd 2019-10-21 stsp }
874 3ce1b845 2019-07-15 stsp
875 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
876 3ce1b845 2019-07-15 stsp if (error) {
877 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
878 ef293bdd 2019-10-21 stsp if (logmsg_path)
879 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
880 3ce1b845 2019-07-15 stsp goto done;
881 ef293bdd 2019-10-21 stsp }
882 3ce1b845 2019-07-15 stsp
883 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
884 3ce1b845 2019-07-15 stsp branch_ref);
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 error = got_ref_write(head_ref, repo);
892 ef293bdd 2019-10-21 stsp if (error) {
893 ef293bdd 2019-10-21 stsp if (logmsg_path)
894 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
895 3ce1b845 2019-07-15 stsp goto done;
896 ef293bdd 2019-10-21 stsp }
897 3ce1b845 2019-07-15 stsp }
898 3ce1b845 2019-07-15 stsp
899 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
900 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
901 2c7829a4 2019-06-17 stsp done:
902 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
903 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
904 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
905 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
906 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
907 8e158b01 2019-09-22 stsp free(logmsg);
908 ef293bdd 2019-10-21 stsp free(logmsg_path);
909 2c7829a4 2019-06-17 stsp free(repo_path);
910 3ce1b845 2019-07-15 stsp free(editor);
911 3ce1b845 2019-07-15 stsp free(refname);
912 3ce1b845 2019-07-15 stsp free(new_commit_id);
913 3ce1b845 2019-07-15 stsp free(id_str);
914 aba9c984 2019-09-08 stsp free(author);
915 c9956ddf 2019-09-08 stsp free(gitconfig_path);
916 3ce1b845 2019-07-15 stsp if (branch_ref)
917 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
918 3ce1b845 2019-07-15 stsp if (head_ref)
919 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
920 2c7829a4 2019-06-17 stsp return error;
921 93658fb9 2020-03-18 stsp }
922 93658fb9 2020-03-18 stsp
923 93658fb9 2020-03-18 stsp __dead static void
924 93658fb9 2020-03-18 stsp usage_clone(void)
925 93658fb9 2020-03-18 stsp {
926 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
927 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
928 93658fb9 2020-03-18 stsp exit(1);
929 93658fb9 2020-03-18 stsp }
930 892ac3b6 2020-03-18 stsp
931 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
932 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
933 892ac3b6 2020-03-18 stsp int last_p_indexed;
934 892ac3b6 2020-03-18 stsp int last_p_resolved;
935 68999b92 2020-03-18 stsp int verbosity;
936 04d9a9ec 2020-09-24 stsp
937 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
938 04d9a9ec 2020-09-24 stsp
939 04d9a9ec 2020-09-24 stsp int create_configs;
940 04d9a9ec 2020-09-24 stsp int configs_created;
941 04d9a9ec 2020-09-24 stsp struct {
942 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
943 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
944 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
945 04d9a9ec 2020-09-24 stsp const char *proto;
946 04d9a9ec 2020-09-24 stsp const char *host;
947 04d9a9ec 2020-09-24 stsp const char *port;
948 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
949 04d9a9ec 2020-09-24 stsp const char *git_url;
950 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
951 04d9a9ec 2020-09-24 stsp int mirror_references;
952 04d9a9ec 2020-09-24 stsp } config_info;
953 892ac3b6 2020-03-18 stsp };
954 93658fb9 2020-03-18 stsp
955 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
956 93658fb9 2020-03-18 stsp static const struct got_error *
957 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
958 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
959 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
960 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
961 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
962 04d9a9ec 2020-09-24 stsp
963 04d9a9ec 2020-09-24 stsp static const struct got_error *
964 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
965 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
966 531c3985 2020-03-18 stsp {
967 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
968 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
969 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
970 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
971 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
972 04d9a9ec 2020-09-24 stsp
973 04d9a9ec 2020-09-24 stsp /*
974 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
975 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
976 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
977 04d9a9ec 2020-09-24 stsp * we have all required information.
978 04d9a9ec 2020-09-24 stsp */
979 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
980 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
981 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
982 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
983 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
984 62d463ca 2020-10-20 naddy a->config_info.git_url,
985 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
986 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
987 62d463ca 2020-10-20 naddy a->config_info.symrefs,
988 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
989 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
990 04d9a9ec 2020-09-24 stsp if (err)
991 04d9a9ec 2020-09-24 stsp return err;
992 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
993 04d9a9ec 2020-09-24 stsp }
994 b2409d58 2020-03-18 stsp
995 68999b92 2020-03-18 stsp if (a->verbosity < 0)
996 68999b92 2020-03-18 stsp return NULL;
997 68999b92 2020-03-18 stsp
998 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
999 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
1000 892ac3b6 2020-03-18 stsp fflush(stdout);
1001 12d1281e 2020-03-19 stsp return NULL;
1002 b2409d58 2020-03-18 stsp }
1003 b2409d58 2020-03-18 stsp
1004 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
1005 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1006 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
1007 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
1008 892ac3b6 2020-03-18 stsp print_size = 1;
1009 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
1010 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1011 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1012 892ac3b6 2020-03-18 stsp }
1013 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1014 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1015 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1016 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1017 892ac3b6 2020-03-18 stsp print_indexed = 1;
1018 892ac3b6 2020-03-18 stsp print_size = 1;
1019 892ac3b6 2020-03-18 stsp }
1020 61cc1a7a 2020-03-18 stsp }
1021 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1022 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1023 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1024 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1025 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1026 892ac3b6 2020-03-18 stsp print_resolved = 1;
1027 892ac3b6 2020-03-18 stsp print_indexed = 1;
1028 892ac3b6 2020-03-18 stsp print_size = 1;
1029 892ac3b6 2020-03-18 stsp }
1030 61cc1a7a 2020-03-18 stsp }
1031 3168e5da 2020-09-10 stsp
1032 d2cdc636 2020-03-18 stsp }
1033 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1034 892ac3b6 2020-03-18 stsp printf("\r");
1035 892ac3b6 2020-03-18 stsp if (print_size)
1036 b5934965 2022-02-12 naddy printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1037 d715f13e 2020-03-19 stsp if (print_indexed)
1038 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1039 d715f13e 2020-03-19 stsp if (print_resolved)
1040 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1041 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1042 892ac3b6 2020-03-18 stsp fflush(stdout);
1043 892ac3b6 2020-03-18 stsp
1044 531c3985 2020-03-18 stsp return NULL;
1045 531c3985 2020-03-18 stsp }
1046 531c3985 2020-03-18 stsp
1047 531c3985 2020-03-18 stsp static const struct got_error *
1048 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1049 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1050 4ba14133 2020-03-20 stsp {
1051 4ba14133 2020-03-20 stsp const struct got_error *err;
1052 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1053 4ba14133 2020-03-20 stsp
1054 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1055 4ba14133 2020-03-20 stsp if (err)
1056 4ba14133 2020-03-20 stsp return err;
1057 4ba14133 2020-03-20 stsp
1058 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1059 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1060 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1061 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1062 6338a6a1 2020-03-21 stsp }
1063 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1064 4ba14133 2020-03-20 stsp return err;
1065 4ba14133 2020-03-20 stsp }
1066 4ba14133 2020-03-20 stsp
1067 4ba14133 2020-03-20 stsp static const struct got_error *
1068 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1069 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1070 41b0de12 2020-03-21 stsp {
1071 41b0de12 2020-03-21 stsp const struct got_error *err;
1072 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1073 41b0de12 2020-03-21 stsp
1074 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1075 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1076 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1077 41b0de12 2020-03-21 stsp
1078 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1079 41b0de12 2020-03-21 stsp }
1080 41b0de12 2020-03-21 stsp
1081 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1082 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1083 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1084 41b0de12 2020-03-21 stsp char *id_str;
1085 41b0de12 2020-03-21 stsp
1086 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1087 41b0de12 2020-03-21 stsp if (err)
1088 41b0de12 2020-03-21 stsp return err;
1089 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1090 41b0de12 2020-03-21 stsp free(id_str);
1091 41b0de12 2020-03-21 stsp }
1092 41b0de12 2020-03-21 stsp
1093 41b0de12 2020-03-21 stsp return NULL;
1094 6338a6a1 2020-03-21 stsp }
1095 6338a6a1 2020-03-21 stsp
1096 6338a6a1 2020-03-21 stsp static const struct got_error *
1097 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1098 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1099 6338a6a1 2020-03-21 stsp {
1100 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1101 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1102 6338a6a1 2020-03-21 stsp char *id_str;
1103 6338a6a1 2020-03-21 stsp
1104 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1105 6338a6a1 2020-03-21 stsp if (err)
1106 6338a6a1 2020-03-21 stsp return err;
1107 6338a6a1 2020-03-21 stsp
1108 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1109 6338a6a1 2020-03-21 stsp if (err)
1110 6338a6a1 2020-03-21 stsp goto done;
1111 6338a6a1 2020-03-21 stsp
1112 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1113 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1114 6338a6a1 2020-03-21 stsp
1115 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1116 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1117 6338a6a1 2020-03-21 stsp done:
1118 6338a6a1 2020-03-21 stsp free(id_str);
1119 6338a6a1 2020-03-21 stsp return err;
1120 0e4002ca 2020-03-21 stsp }
1121 0e4002ca 2020-03-21 stsp
1122 0e4002ca 2020-03-21 stsp static int
1123 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1124 0e4002ca 2020-03-21 stsp {
1125 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1126 0e4002ca 2020-03-21 stsp return 0;
1127 0e4002ca 2020-03-21 stsp refname += 5;
1128 0e4002ca 2020-03-21 stsp
1129 0e4002ca 2020-03-21 stsp /*
1130 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1131 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1132 0e4002ca 2020-03-21 stsp */
1133 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1134 0e4002ca 2020-03-21 stsp return 0;
1135 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1136 0e4002ca 2020-03-21 stsp return 0;
1137 0e4002ca 2020-03-21 stsp
1138 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1139 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1140 0e4002ca 2020-03-21 stsp
1141 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1142 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1143 0e4002ca 2020-03-21 stsp return 1;
1144 0e4002ca 2020-03-21 stsp
1145 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1146 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1147 41b0de12 2020-03-21 stsp }
1148 41b0de12 2020-03-21 stsp
1149 0e4002ca 2020-03-21 stsp static int
1150 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1151 0e4002ca 2020-03-21 stsp {
1152 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1153 0e4002ca 2020-03-21 stsp
1154 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1155 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1156 0e4002ca 2020-03-21 stsp return 1;
1157 0e4002ca 2020-03-21 stsp }
1158 0e4002ca 2020-03-21 stsp
1159 0e4002ca 2020-03-21 stsp return 0;
1160 0e4002ca 2020-03-21 stsp }
1161 0e4002ca 2020-03-21 stsp
1162 41b0de12 2020-03-21 stsp static const struct got_error *
1163 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1164 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1165 0e4002ca 2020-03-21 stsp {
1166 0e4002ca 2020-03-21 stsp const struct got_error *err;
1167 0e4002ca 2020-03-21 stsp char *remote_refname;
1168 0e4002ca 2020-03-21 stsp
1169 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1170 0e4002ca 2020-03-21 stsp refname += 5;
1171 0e4002ca 2020-03-21 stsp
1172 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1173 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1174 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1175 0e4002ca 2020-03-21 stsp
1176 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1177 0e4002ca 2020-03-21 stsp free(remote_refname);
1178 7c0b7f42 2020-09-24 stsp return err;
1179 7c0b7f42 2020-09-24 stsp }
1180 7c0b7f42 2020-09-24 stsp
1181 7c0b7f42 2020-09-24 stsp static const struct got_error *
1182 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1183 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1184 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1185 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1186 99495ddb 2021-01-10 stsp struct got_repository *repo)
1187 7c0b7f42 2020-09-24 stsp {
1188 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1189 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1190 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1191 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1192 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1193 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1194 7c0b7f42 2020-09-24 stsp ssize_t n;
1195 7c0b7f42 2020-09-24 stsp
1196 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1197 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1198 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1199 132af4a5 2021-01-05 stsp char *s;
1200 132af4a5 2021-01-05 stsp branchname = pe->path;
1201 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1202 132af4a5 2021-01-05 stsp branchname += 11;
1203 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1204 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1205 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1206 132af4a5 2021-01-05 stsp goto done;
1207 132af4a5 2021-01-05 stsp }
1208 132af4a5 2021-01-05 stsp free(branches);
1209 132af4a5 2021-01-05 stsp branches = s;
1210 132af4a5 2021-01-05 stsp }
1211 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1212 15d3c221 2021-01-05 stsp branchname = default_branch;
1213 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1214 15d3c221 2021-01-05 stsp branchname += 11;
1215 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1216 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1217 132af4a5 2021-01-05 stsp goto done;
1218 99495ddb 2021-01-10 stsp }
1219 99495ddb 2021-01-10 stsp }
1220 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1221 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1222 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1223 99495ddb 2021-01-10 stsp char *s;
1224 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1225 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1226 99495ddb 2021-01-10 stsp branchname += 5;
1227 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1228 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1229 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1230 99495ddb 2021-01-10 stsp goto done;
1231 99495ddb 2021-01-10 stsp }
1232 99495ddb 2021-01-10 stsp free(refs);
1233 99495ddb 2021-01-10 stsp refs = s;
1234 132af4a5 2021-01-05 stsp }
1235 15d3c221 2021-01-05 stsp }
1236 15d3c221 2021-01-05 stsp
1237 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1238 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1239 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1240 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1241 7c0b7f42 2020-09-24 stsp goto done;
1242 7c0b7f42 2020-09-24 stsp }
1243 00fe21f2 2021-12-31 stsp gotconfig_file = fopen(gotconfig_path, "ae");
1244 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1245 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1246 7c0b7f42 2020-09-24 stsp goto done;
1247 7c0b7f42 2020-09-24 stsp }
1248 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1249 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1250 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1251 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1252 7c0b7f42 2020-09-24 stsp "%s%s%s"
1253 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1254 15d3c221 2021-01-05 stsp "%s%s%s"
1255 99495ddb 2021-01-10 stsp "%s%s%s"
1256 7c0b7f42 2020-09-24 stsp "%s"
1257 0c8b29c5 2021-01-05 stsp "%s"
1258 7c0b7f42 2020-09-24 stsp "}\n",
1259 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1260 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1261 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1262 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1263 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1264 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1265 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1266 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1267 7c0b7f42 2020-09-24 stsp goto done;
1268 7c0b7f42 2020-09-24 stsp }
1269 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1270 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1271 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1272 7c0b7f42 2020-09-24 stsp goto done;
1273 7c0b7f42 2020-09-24 stsp }
1274 7c0b7f42 2020-09-24 stsp
1275 7c0b7f42 2020-09-24 stsp done:
1276 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1277 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1278 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1279 132af4a5 2021-01-05 stsp free(branches);
1280 7c0b7f42 2020-09-24 stsp return err;
1281 7c0b7f42 2020-09-24 stsp }
1282 7c0b7f42 2020-09-24 stsp
1283 7c0b7f42 2020-09-24 stsp static const struct got_error *
1284 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1285 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1286 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1287 99495ddb 2021-01-10 stsp struct got_repository *repo)
1288 7c0b7f42 2020-09-24 stsp {
1289 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1290 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1291 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1292 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1293 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1294 56d0a753 2021-01-20 stsp const char *branchname;
1295 7c0b7f42 2020-09-24 stsp ssize_t n;
1296 7c0b7f42 2020-09-24 stsp
1297 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1298 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1299 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1300 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1301 7c0b7f42 2020-09-24 stsp goto done;
1302 7c0b7f42 2020-09-24 stsp }
1303 00fe21f2 2021-12-31 stsp gitconfig_file = fopen(gitconfig_path, "ae");
1304 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1305 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1306 7c0b7f42 2020-09-24 stsp goto done;
1307 7c0b7f42 2020-09-24 stsp }
1308 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1309 56d0a753 2021-01-20 stsp if (mirror_references) {
1310 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1311 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1312 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1313 56d0a753 2021-01-20 stsp goto done;
1314 56d0a753 2021-01-20 stsp }
1315 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1316 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1317 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1318 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1319 7c0b7f42 2020-09-24 stsp goto done;
1320 132af4a5 2021-01-05 stsp }
1321 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1322 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1323 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1324 132af4a5 2021-01-05 stsp char *s;
1325 132af4a5 2021-01-05 stsp branchname = pe->path;
1326 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1327 132af4a5 2021-01-05 stsp branchname += 11;
1328 56d0a753 2021-01-20 stsp if (mirror_references) {
1329 56d0a753 2021-01-20 stsp if (asprintf(&s,
1330 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1331 56d0a753 2021-01-20 stsp branches ? branches : "",
1332 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1333 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1334 56d0a753 2021-01-20 stsp goto done;
1335 56d0a753 2021-01-20 stsp }
1336 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1337 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1338 132af4a5 2021-01-05 stsp branches ? branches : "",
1339 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1340 132af4a5 2021-01-05 stsp branchname) == -1) {
1341 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1342 132af4a5 2021-01-05 stsp goto done;
1343 132af4a5 2021-01-05 stsp }
1344 132af4a5 2021-01-05 stsp free(branches);
1345 132af4a5 2021-01-05 stsp branches = s;
1346 7c0b7f42 2020-09-24 stsp }
1347 7c0b7f42 2020-09-24 stsp } else {
1348 7c0b7f42 2020-09-24 stsp /*
1349 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1350 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1351 7c0b7f42 2020-09-24 stsp */
1352 04d9a9ec 2020-09-24 stsp if (default_branch) {
1353 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1354 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1355 7c0b7f42 2020-09-24 stsp branchname += 11;
1356 7c0b7f42 2020-09-24 stsp } else
1357 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1358 56d0a753 2021-01-20 stsp if (mirror_references) {
1359 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1360 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1361 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1362 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1363 56d0a753 2021-01-20 stsp goto done;
1364 56d0a753 2021-01-20 stsp }
1365 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1366 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1367 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1368 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1369 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1370 7c0b7f42 2020-09-24 stsp goto done;
1371 99495ddb 2021-01-10 stsp }
1372 99495ddb 2021-01-10 stsp }
1373 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1374 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1375 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1376 99495ddb 2021-01-10 stsp char *s;
1377 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1378 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1379 99495ddb 2021-01-10 stsp refname += 5;
1380 56d0a753 2021-01-20 stsp if (mirror_references) {
1381 56d0a753 2021-01-20 stsp if (asprintf(&s,
1382 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1383 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1384 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1385 56d0a753 2021-01-20 stsp goto done;
1386 56d0a753 2021-01-20 stsp }
1387 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1388 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1389 99495ddb 2021-01-10 stsp refs ? refs : "",
1390 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1391 99495ddb 2021-01-10 stsp refname) == -1) {
1392 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1393 99495ddb 2021-01-10 stsp goto done;
1394 99495ddb 2021-01-10 stsp }
1395 99495ddb 2021-01-10 stsp free(refs);
1396 99495ddb 2021-01-10 stsp refs = s;
1397 7c0b7f42 2020-09-24 stsp }
1398 132af4a5 2021-01-05 stsp }
1399 99495ddb 2021-01-10 stsp
1400 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1401 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1402 132af4a5 2021-01-05 stsp "\turl = %s\n"
1403 132af4a5 2021-01-05 stsp "%s"
1404 99495ddb 2021-01-10 stsp "%s"
1405 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1406 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1407 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1408 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1409 132af4a5 2021-01-05 stsp goto done;
1410 7c0b7f42 2020-09-24 stsp }
1411 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1412 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1413 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1414 7c0b7f42 2020-09-24 stsp goto done;
1415 7c0b7f42 2020-09-24 stsp }
1416 7c0b7f42 2020-09-24 stsp done:
1417 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1418 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1419 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1420 132af4a5 2021-01-05 stsp free(branches);
1421 0e4002ca 2020-03-21 stsp return err;
1422 0e4002ca 2020-03-21 stsp }
1423 0e4002ca 2020-03-21 stsp
1424 0e4002ca 2020-03-21 stsp static const struct got_error *
1425 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1426 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1427 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1428 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1429 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1430 04d9a9ec 2020-09-24 stsp {
1431 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1432 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1433 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1434 04d9a9ec 2020-09-24 stsp
1435 04d9a9ec 2020-09-24 stsp /*
1436 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1437 04d9a9ec 2020-09-24 stsp * one of those.
1438 04d9a9ec 2020-09-24 stsp */
1439 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1440 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1441 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1442 04d9a9ec 2020-09-24 stsp } else {
1443 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1444 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1445 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1446 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1447 04d9a9ec 2020-09-24 stsp
1448 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1449 04d9a9ec 2020-09-24 stsp continue;
1450 04d9a9ec 2020-09-24 stsp
1451 04d9a9ec 2020-09-24 stsp default_branch = target;
1452 04d9a9ec 2020-09-24 stsp break;
1453 04d9a9ec 2020-09-24 stsp }
1454 04d9a9ec 2020-09-24 stsp }
1455 04d9a9ec 2020-09-24 stsp
1456 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1457 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1458 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1459 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1460 04d9a9ec 2020-09-24 stsp if (err)
1461 04d9a9ec 2020-09-24 stsp return err;
1462 04d9a9ec 2020-09-24 stsp
1463 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1464 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1465 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1466 04d9a9ec 2020-09-24 stsp }
1467 04d9a9ec 2020-09-24 stsp
1468 04d9a9ec 2020-09-24 stsp static const struct got_error *
1469 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1470 93658fb9 2020-03-18 stsp {
1471 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1472 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1473 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1474 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1475 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1476 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1477 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1478 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1479 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1480 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1481 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1482 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1483 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1484 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1485 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1486 93658fb9 2020-03-18 stsp
1487 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1488 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1489 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1490 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1491 d9b4d0c0 2020-03-18 stsp
1492 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1493 93658fb9 2020-03-18 stsp switch (ch) {
1494 659e7fbd 2020-03-20 stsp case 'a':
1495 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1496 4ba14133 2020-03-20 stsp break;
1497 4ba14133 2020-03-20 stsp case 'b':
1498 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1499 4ba14133 2020-03-20 stsp optarg, NULL);
1500 4ba14133 2020-03-20 stsp if (error)
1501 4ba14133 2020-03-20 stsp return error;
1502 659e7fbd 2020-03-20 stsp break;
1503 41b0de12 2020-03-21 stsp case 'l':
1504 41b0de12 2020-03-21 stsp list_refs_only = 1;
1505 41b0de12 2020-03-21 stsp break;
1506 469dd726 2020-03-20 stsp case 'm':
1507 469dd726 2020-03-20 stsp mirror_references = 1;
1508 469dd726 2020-03-20 stsp break;
1509 68999b92 2020-03-18 stsp case 'v':
1510 68999b92 2020-03-18 stsp if (verbosity < 0)
1511 68999b92 2020-03-18 stsp verbosity = 0;
1512 68999b92 2020-03-18 stsp else if (verbosity < 3)
1513 68999b92 2020-03-18 stsp verbosity++;
1514 68999b92 2020-03-18 stsp break;
1515 68999b92 2020-03-18 stsp case 'q':
1516 68999b92 2020-03-18 stsp verbosity = -1;
1517 68999b92 2020-03-18 stsp break;
1518 0e4002ca 2020-03-21 stsp case 'R':
1519 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1520 0e4002ca 2020-03-21 stsp optarg, NULL);
1521 0e4002ca 2020-03-21 stsp if (error)
1522 0e4002ca 2020-03-21 stsp return error;
1523 0e4002ca 2020-03-21 stsp break;
1524 93658fb9 2020-03-18 stsp default:
1525 93658fb9 2020-03-18 stsp usage_clone();
1526 93658fb9 2020-03-18 stsp break;
1527 93658fb9 2020-03-18 stsp }
1528 93658fb9 2020-03-18 stsp }
1529 93658fb9 2020-03-18 stsp argc -= optind;
1530 93658fb9 2020-03-18 stsp argv += optind;
1531 39c64a6a 2020-03-18 stsp
1532 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1533 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1534 41b0de12 2020-03-21 stsp if (list_refs_only) {
1535 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1536 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1537 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1538 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1539 41b0de12 2020-03-21 stsp if (mirror_references)
1540 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1541 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1542 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1543 41b0de12 2020-03-21 stsp }
1544 4ba14133 2020-03-20 stsp
1545 93658fb9 2020-03-18 stsp uri = argv[0];
1546 9df6f38b 2020-03-18 stsp
1547 9df6f38b 2020-03-18 stsp if (argc == 1)
1548 93658fb9 2020-03-18 stsp dirname = NULL;
1549 9df6f38b 2020-03-18 stsp else if (argc == 2)
1550 93658fb9 2020-03-18 stsp dirname = argv[1];
1551 93658fb9 2020-03-18 stsp else
1552 93658fb9 2020-03-18 stsp usage_clone();
1553 09838ffc 2020-03-18 stsp
1554 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1555 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1556 39c64a6a 2020-03-18 stsp if (error)
1557 09f63084 2020-03-20 stsp goto done;
1558 09f63084 2020-03-20 stsp
1559 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1560 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1561 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1562 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1563 09838ffc 2020-03-18 stsp goto done;
1564 09f63084 2020-03-20 stsp }
1565 09838ffc 2020-03-18 stsp
1566 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 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 dns inet 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, "git+ssh") == 0 ||
1573 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1574 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1575 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1576 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1577 39c64a6a 2020-03-18 stsp err(1, "pledge");
1578 b46f3e71 2020-03-18 stsp #endif
1579 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1580 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1581 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1582 39c64a6a 2020-03-18 stsp goto done;
1583 39c64a6a 2020-03-18 stsp } else {
1584 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1585 39c64a6a 2020-03-18 stsp goto done;
1586 39c64a6a 2020-03-18 stsp }
1587 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1588 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1589 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1590 bb64b798 2020-03-18 stsp goto done;
1591 bb64b798 2020-03-18 stsp }
1592 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1593 bb64b798 2020-03-18 stsp } else
1594 bb64b798 2020-03-18 stsp repo_path = dirname;
1595 bb64b798 2020-03-18 stsp
1596 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1597 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1598 2751fe64 2020-09-24 stsp if (error &&
1599 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1600 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1601 41b0de12 2020-03-21 stsp goto done;
1602 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1603 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1604 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1605 41b0de12 2020-03-21 stsp goto done;
1606 2751fe64 2020-09-24 stsp }
1607 41b0de12 2020-03-21 stsp }
1608 bb64b798 2020-03-18 stsp
1609 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
1610 d65a88a2 2021-09-05 stsp if (error)
1611 d65a88a2 2021-09-05 stsp goto done;
1612 d65a88a2 2021-09-05 stsp
1613 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1614 ee448f5f 2020-03-18 stsp if (error)
1615 ee448f5f 2020-03-18 stsp goto done;
1616 f79e6490 2020-04-19 stsp
1617 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1618 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1619 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1620 ee448f5f 2020-03-18 stsp
1621 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1622 9c52365f 2020-03-21 stsp server_path, verbosity);
1623 39c64a6a 2020-03-18 stsp if (error)
1624 bb64b798 2020-03-18 stsp goto done;
1625 bb64b798 2020-03-18 stsp
1626 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1627 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1628 2751fe64 2020-09-24 stsp if (error)
1629 2751fe64 2020-09-24 stsp goto done;
1630 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1631 2751fe64 2020-09-24 stsp if (error)
1632 2751fe64 2020-09-24 stsp goto done;
1633 2751fe64 2020-09-24 stsp }
1634 2751fe64 2020-09-24 stsp
1635 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1636 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1637 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1638 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1639 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1640 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1641 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1644 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1645 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1646 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1647 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1648 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1649 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1650 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1651 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1652 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1653 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1654 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1655 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1656 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1657 39c64a6a 2020-03-18 stsp if (error)
1658 d9b4d0c0 2020-03-18 stsp goto done;
1659 d9b4d0c0 2020-03-18 stsp
1660 41b0de12 2020-03-21 stsp if (list_refs_only) {
1661 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1662 41b0de12 2020-03-21 stsp goto done;
1663 41b0de12 2020-03-21 stsp }
1664 41b0de12 2020-03-21 stsp
1665 8a4f8535 2021-12-27 stsp if (pack_hash == NULL) {
1666 8a4f8535 2021-12-27 stsp error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1667 8a4f8535 2021-12-27 stsp "server sent an empty pack file");
1668 8a4f8535 2021-12-27 stsp goto done;
1669 8a4f8535 2021-12-27 stsp }
1670 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1671 39c64a6a 2020-03-18 stsp if (error)
1672 d9b4d0c0 2020-03-18 stsp goto done;
1673 68999b92 2020-03-18 stsp if (verbosity >= 0)
1674 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1675 d9b4d0c0 2020-03-18 stsp free(id_str);
1676 d9b4d0c0 2020-03-18 stsp
1677 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1678 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1679 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1680 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1681 7ebc0570 2020-03-18 stsp char *remote_refname;
1682 0e4002ca 2020-03-21 stsp
1683 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1684 0e4002ca 2020-03-21 stsp !mirror_references) {
1685 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1686 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1687 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1688 0e4002ca 2020-03-21 stsp if (error)
1689 0e4002ca 2020-03-21 stsp goto done;
1690 0e4002ca 2020-03-21 stsp continue;
1691 0e4002ca 2020-03-21 stsp }
1692 668a20f6 2020-03-18 stsp
1693 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1694 39c64a6a 2020-03-18 stsp if (error)
1695 d9b4d0c0 2020-03-18 stsp goto done;
1696 d9b4d0c0 2020-03-18 stsp
1697 469dd726 2020-03-20 stsp if (mirror_references)
1698 469dd726 2020-03-20 stsp continue;
1699 469dd726 2020-03-20 stsp
1700 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1701 7ebc0570 2020-03-18 stsp continue;
1702 7ebc0570 2020-03-18 stsp
1703 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1704 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1705 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1706 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1707 7ebc0570 2020-03-18 stsp goto done;
1708 7ebc0570 2020-03-18 stsp }
1709 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1710 f298ae0f 2020-03-25 stsp free(remote_refname);
1711 39c64a6a 2020-03-18 stsp if (error)
1712 d9b4d0c0 2020-03-18 stsp goto done;
1713 d9b4d0c0 2020-03-18 stsp }
1714 d9b4d0c0 2020-03-18 stsp
1715 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1716 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1717 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1718 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1719 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1720 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1721 d9b4d0c0 2020-03-18 stsp
1722 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1723 d9b4d0c0 2020-03-18 stsp continue;
1724 d9b4d0c0 2020-03-18 stsp
1725 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1726 39c64a6a 2020-03-18 stsp if (error) {
1727 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1728 55330abe 2020-03-20 stsp error = NULL;
1729 d9b4d0c0 2020-03-18 stsp continue;
1730 55330abe 2020-03-20 stsp }
1731 d9b4d0c0 2020-03-18 stsp goto done;
1732 d9b4d0c0 2020-03-18 stsp }
1733 d9b4d0c0 2020-03-18 stsp
1734 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1735 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1736 f298ae0f 2020-03-25 stsp if (error)
1737 f298ae0f 2020-03-25 stsp goto done;
1738 f298ae0f 2020-03-25 stsp
1739 f298ae0f 2020-03-25 stsp if (mirror_references)
1740 f298ae0f 2020-03-25 stsp continue;
1741 f298ae0f 2020-03-25 stsp
1742 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1743 f298ae0f 2020-03-25 stsp continue;
1744 f298ae0f 2020-03-25 stsp
1745 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1746 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1747 f298ae0f 2020-03-25 stsp refname) == -1) {
1748 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1749 f298ae0f 2020-03-25 stsp goto done;
1750 f298ae0f 2020-03-25 stsp }
1751 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1752 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1753 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1754 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1755 f298ae0f 2020-03-25 stsp free(remote_refname);
1756 f298ae0f 2020-03-25 stsp goto done;
1757 f298ae0f 2020-03-25 stsp }
1758 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1759 f298ae0f 2020-03-25 stsp if (error) {
1760 f298ae0f 2020-03-25 stsp free(remote_refname);
1761 f298ae0f 2020-03-25 stsp free(remote_target);
1762 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1763 f298ae0f 2020-03-25 stsp error = NULL;
1764 f298ae0f 2020-03-25 stsp continue;
1765 f298ae0f 2020-03-25 stsp }
1766 f298ae0f 2020-03-25 stsp goto done;
1767 f298ae0f 2020-03-25 stsp }
1768 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1769 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1770 f298ae0f 2020-03-25 stsp free(remote_refname);
1771 f298ae0f 2020-03-25 stsp free(remote_target);
1772 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1773 39c64a6a 2020-03-18 stsp if (error)
1774 d9b4d0c0 2020-03-18 stsp goto done;
1775 4ba14133 2020-03-20 stsp }
1776 4ba14133 2020-03-20 stsp if (pe == NULL) {
1777 4ba14133 2020-03-20 stsp /*
1778 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1779 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1780 4ba14133 2020-03-20 stsp * which could be fetched instead.
1781 4ba14133 2020-03-20 stsp */
1782 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1783 4ba14133 2020-03-20 stsp const char *target = pe->path;
1784 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1785 d9b4d0c0 2020-03-18 stsp
1786 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1787 4ba14133 2020-03-20 stsp if (error) {
1788 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1789 4ba14133 2020-03-20 stsp error = NULL;
1790 4ba14133 2020-03-20 stsp continue;
1791 4ba14133 2020-03-20 stsp }
1792 4ba14133 2020-03-20 stsp goto done;
1793 4ba14133 2020-03-20 stsp }
1794 4ba14133 2020-03-20 stsp
1795 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1796 04d9a9ec 2020-09-24 stsp verbosity, repo);
1797 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1798 4ba14133 2020-03-20 stsp if (error)
1799 4ba14133 2020-03-20 stsp goto done;
1800 4ba14133 2020-03-20 stsp break;
1801 4ba14133 2020-03-20 stsp }
1802 659e7fbd 2020-03-20 stsp }
1803 659e7fbd 2020-03-20 stsp
1804 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1805 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1806 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1807 09838ffc 2020-03-18 stsp done:
1808 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1809 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1810 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1811 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1812 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1813 9c52365f 2020-03-21 stsp }
1814 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1815 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1816 1d0f4054 2021-06-17 stsp if (repo) {
1817 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1818 1d0f4054 2021-06-17 stsp if (error == NULL)
1819 1d0f4054 2021-06-17 stsp error = close_err;
1820 1d0f4054 2021-06-17 stsp }
1821 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1822 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1823 d9b4d0c0 2020-03-18 stsp free(pe->data);
1824 d9b4d0c0 2020-03-18 stsp }
1825 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1826 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1827 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1828 d9b4d0c0 2020-03-18 stsp free(pe->data);
1829 d9b4d0c0 2020-03-18 stsp }
1830 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1831 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1832 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1833 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1834 09838ffc 2020-03-18 stsp free(proto);
1835 09838ffc 2020-03-18 stsp free(host);
1836 09838ffc 2020-03-18 stsp free(port);
1837 09838ffc 2020-03-18 stsp free(server_path);
1838 09838ffc 2020-03-18 stsp free(repo_name);
1839 bb64b798 2020-03-18 stsp free(default_destdir);
1840 b46f3e71 2020-03-18 stsp free(git_url);
1841 39c64a6a 2020-03-18 stsp return error;
1842 7848a0e1 2020-03-19 stsp }
1843 7848a0e1 2020-03-19 stsp
1844 7848a0e1 2020-03-19 stsp static const struct got_error *
1845 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1846 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1847 7848a0e1 2020-03-19 stsp {
1848 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1849 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1850 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1851 7848a0e1 2020-03-19 stsp
1852 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1853 7848a0e1 2020-03-19 stsp if (err)
1854 7848a0e1 2020-03-19 stsp goto done;
1855 7848a0e1 2020-03-19 stsp
1856 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1857 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1858 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1859 88609724 2020-03-21 stsp if (err)
1860 88609724 2020-03-21 stsp goto done;
1861 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1862 88609724 2020-03-21 stsp goto done;
1863 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1864 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1865 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1866 db6d8ad8 2020-03-21 stsp }
1867 db6d8ad8 2020-03-21 stsp goto done;
1868 db6d8ad8 2020-03-21 stsp }
1869 db6d8ad8 2020-03-21 stsp
1870 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1871 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1872 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1873 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1874 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1875 688f11b3 2020-03-21 stsp }
1876 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1877 7848a0e1 2020-03-19 stsp if (err)
1878 7848a0e1 2020-03-19 stsp goto done;
1879 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1880 e8a967e0 2020-03-21 stsp if (err)
1881 e8a967e0 2020-03-21 stsp goto done;
1882 7848a0e1 2020-03-19 stsp } else {
1883 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1884 7848a0e1 2020-03-19 stsp if (err)
1885 7848a0e1 2020-03-19 stsp goto done;
1886 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1887 6338a6a1 2020-03-21 stsp goto done;
1888 6338a6a1 2020-03-21 stsp
1889 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1890 6338a6a1 2020-03-21 stsp if (err)
1891 6338a6a1 2020-03-21 stsp goto done;
1892 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1893 6338a6a1 2020-03-21 stsp if (err)
1894 6338a6a1 2020-03-21 stsp goto done;
1895 7848a0e1 2020-03-19 stsp }
1896 6338a6a1 2020-03-21 stsp
1897 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1898 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1899 6338a6a1 2020-03-21 stsp new_id_str);
1900 7848a0e1 2020-03-19 stsp done:
1901 7848a0e1 2020-03-19 stsp free(old_id);
1902 7848a0e1 2020-03-19 stsp free(new_id_str);
1903 7848a0e1 2020-03-19 stsp return err;
1904 2ab43947 2020-03-18 stsp }
1905 f1bcca34 2020-03-25 stsp
1906 f1bcca34 2020-03-25 stsp static const struct got_error *
1907 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1908 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1909 f1bcca34 2020-03-25 stsp {
1910 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1911 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1912 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1913 f1bcca34 2020-03-25 stsp
1914 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1915 bcf34b0e 2020-03-26 stsp if (err) {
1916 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1917 bcf34b0e 2020-03-26 stsp return err;
1918 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1919 bcf34b0e 2020-03-26 stsp if (err)
1920 bcf34b0e 2020-03-26 stsp goto done;
1921 2ab43947 2020-03-18 stsp
1922 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1923 bcf34b0e 2020-03-26 stsp if (err)
1924 bcf34b0e 2020-03-26 stsp goto done;
1925 f1bcca34 2020-03-25 stsp
1926 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1927 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1928 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1929 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1930 bcf34b0e 2020-03-26 stsp } else {
1931 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1932 f1bcca34 2020-03-25 stsp
1933 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1934 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1935 bcf34b0e 2020-03-26 stsp goto done;
1936 bcf34b0e 2020-03-26 stsp
1937 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1938 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1939 bcf34b0e 2020-03-26 stsp if (err)
1940 bcf34b0e 2020-03-26 stsp goto done;
1941 bcf34b0e 2020-03-26 stsp
1942 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1943 bcf34b0e 2020-03-26 stsp if (err)
1944 bcf34b0e 2020-03-26 stsp goto done;
1945 bcf34b0e 2020-03-26 stsp
1946 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1947 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1948 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1949 bcf34b0e 2020-03-26 stsp
1950 bcf34b0e 2020-03-26 stsp }
1951 f1bcca34 2020-03-25 stsp done:
1952 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1953 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1954 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1955 bcf34b0e 2020-03-26 stsp err = unlock_err;
1956 bcf34b0e 2020-03-26 stsp }
1957 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1958 f1bcca34 2020-03-25 stsp return err;
1959 f1bcca34 2020-03-25 stsp }
1960 f1bcca34 2020-03-25 stsp
1961 2ab43947 2020-03-18 stsp __dead static void
1962 7848a0e1 2020-03-19 stsp usage_fetch(void)
1963 7848a0e1 2020-03-19 stsp {
1964 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1965 161728eb 2021-07-24 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1966 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1967 13f12b09 2020-03-21 stsp getprogname());
1968 7848a0e1 2020-03-19 stsp exit(1);
1969 7848a0e1 2020-03-19 stsp }
1970 7848a0e1 2020-03-19 stsp
1971 7848a0e1 2020-03-19 stsp static const struct got_error *
1972 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1973 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1974 f21ec2f0 2020-03-21 stsp {
1975 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1976 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1977 3789fd73 2020-03-26 stsp char *id_str = NULL;
1978 3789fd73 2020-03-26 stsp
1979 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1980 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1981 3789fd73 2020-03-26 stsp if (err)
1982 3789fd73 2020-03-26 stsp return err;
1983 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1984 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1985 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1986 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1987 3789fd73 2020-03-26 stsp }
1988 3789fd73 2020-03-26 stsp } else {
1989 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1990 3789fd73 2020-03-26 stsp if (err)
1991 3789fd73 2020-03-26 stsp return err;
1992 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1993 3789fd73 2020-03-26 stsp if (err)
1994 3789fd73 2020-03-26 stsp goto done;
1995 3168e5da 2020-09-10 stsp
1996 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1997 3789fd73 2020-03-26 stsp if (err)
1998 3789fd73 2020-03-26 stsp goto done;
1999 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
2000 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
2001 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
2002 3789fd73 2020-03-26 stsp }
2003 3789fd73 2020-03-26 stsp }
2004 3789fd73 2020-03-26 stsp done:
2005 3789fd73 2020-03-26 stsp free(id);
2006 3789fd73 2020-03-26 stsp free(id_str);
2007 3789fd73 2020-03-26 stsp return NULL;
2008 3789fd73 2020-03-26 stsp }
2009 3789fd73 2020-03-26 stsp
2010 3789fd73 2020-03-26 stsp static const struct got_error *
2011 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
2012 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2013 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2014 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2015 3789fd73 2020-03-26 stsp {
2016 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2017 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2018 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2019 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2020 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2021 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2022 f21ec2f0 2020-03-21 stsp
2023 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2024 f21ec2f0 2020-03-21 stsp
2025 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2026 3789fd73 2020-03-26 stsp == -1)
2027 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2028 3789fd73 2020-03-26 stsp
2029 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2030 f21ec2f0 2020-03-21 stsp if (err)
2031 3789fd73 2020-03-26 stsp goto done;
2032 f21ec2f0 2020-03-21 stsp
2033 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2034 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2035 1b796c3f 2021-09-11 stsp const char *their_refname;
2036 f21ec2f0 2020-03-21 stsp
2037 1b796c3f 2021-09-11 stsp if (remote->mirror_references) {
2038 1b796c3f 2021-09-11 stsp their_refname = refname;
2039 1b796c3f 2021-09-11 stsp } else {
2040 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2041 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2042 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2043 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2044 3789fd73 2020-03-26 stsp continue;
2045 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2046 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2047 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2048 3789fd73 2020-03-26 stsp goto done;
2049 3789fd73 2020-03-26 stsp }
2050 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2051 3789fd73 2020-03-26 stsp continue;
2052 f21ec2f0 2020-03-21 stsp
2053 1b796c3f 2021-09-11 stsp their_refname = local_refname;
2054 1b796c3f 2021-09-11 stsp }
2055 1b796c3f 2021-09-11 stsp
2056 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2057 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2058 f21ec2f0 2020-03-21 stsp break;
2059 f21ec2f0 2020-03-21 stsp }
2060 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2061 f21ec2f0 2020-03-21 stsp continue;
2062 f21ec2f0 2020-03-21 stsp
2063 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2064 1b796c3f 2021-09-11 stsp if (strcmp(their_refname, pe->path) == 0)
2065 3789fd73 2020-03-26 stsp break;
2066 3789fd73 2020-03-26 stsp }
2067 3789fd73 2020-03-26 stsp if (pe != NULL)
2068 3789fd73 2020-03-26 stsp continue;
2069 f21ec2f0 2020-03-21 stsp
2070 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2071 f21ec2f0 2020-03-21 stsp if (err)
2072 f21ec2f0 2020-03-21 stsp break;
2073 3789fd73 2020-03-26 stsp
2074 3789fd73 2020-03-26 stsp if (local_refname) {
2075 3789fd73 2020-03-26 stsp struct got_reference *ref;
2076 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2077 3789fd73 2020-03-26 stsp if (err) {
2078 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2079 3789fd73 2020-03-26 stsp break;
2080 3789fd73 2020-03-26 stsp free(local_refname);
2081 3789fd73 2020-03-26 stsp local_refname = NULL;
2082 3789fd73 2020-03-26 stsp continue;
2083 3789fd73 2020-03-26 stsp }
2084 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2085 3789fd73 2020-03-26 stsp if (err)
2086 3789fd73 2020-03-26 stsp break;
2087 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2088 3789fd73 2020-03-26 stsp got_ref_close(ref);
2089 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2090 3789fd73 2020-03-26 stsp err = unlock_err;
2091 3789fd73 2020-03-26 stsp break;
2092 3789fd73 2020-03-26 stsp }
2093 3789fd73 2020-03-26 stsp
2094 3789fd73 2020-03-26 stsp free(local_refname);
2095 3789fd73 2020-03-26 stsp local_refname = NULL;
2096 6338a6a1 2020-03-21 stsp }
2097 f21ec2f0 2020-03-21 stsp }
2098 3789fd73 2020-03-26 stsp done:
2099 3789fd73 2020-03-26 stsp free(remote_namespace);
2100 3789fd73 2020-03-26 stsp free(local_refname);
2101 0e4002ca 2020-03-21 stsp return err;
2102 0e4002ca 2020-03-21 stsp }
2103 0e4002ca 2020-03-21 stsp
2104 0e4002ca 2020-03-21 stsp static const struct got_error *
2105 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2106 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2107 0e4002ca 2020-03-21 stsp {
2108 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2109 0e4002ca 2020-03-21 stsp char *remote_refname;
2110 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2111 0e4002ca 2020-03-21 stsp
2112 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2113 0e4002ca 2020-03-21 stsp refname += 5;
2114 0e4002ca 2020-03-21 stsp
2115 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2116 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2117 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2118 f21ec2f0 2020-03-21 stsp
2119 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2120 0e4002ca 2020-03-21 stsp if (err) {
2121 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2122 0e4002ca 2020-03-21 stsp goto done;
2123 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2124 0e4002ca 2020-03-21 stsp } else {
2125 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2126 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2127 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2128 9f142382 2020-03-21 stsp err = unlock_err;
2129 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2130 0e4002ca 2020-03-21 stsp }
2131 0e4002ca 2020-03-21 stsp done:
2132 0e4002ca 2020-03-21 stsp free(remote_refname);
2133 161728eb 2021-07-24 stsp return err;
2134 161728eb 2021-07-24 stsp }
2135 161728eb 2021-07-24 stsp
2136 161728eb 2021-07-24 stsp static const struct got_error *
2137 161728eb 2021-07-24 stsp delete_ref(struct got_repository *repo, struct got_reference *ref)
2138 161728eb 2021-07-24 stsp {
2139 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2140 161728eb 2021-07-24 stsp struct got_object_id *id = NULL;
2141 161728eb 2021-07-24 stsp char *id_str = NULL;
2142 161728eb 2021-07-24 stsp const char *target;
2143 161728eb 2021-07-24 stsp
2144 161728eb 2021-07-24 stsp if (got_ref_is_symbolic(ref)) {
2145 161728eb 2021-07-24 stsp target = got_ref_get_symref_target(ref);
2146 161728eb 2021-07-24 stsp } else {
2147 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2148 161728eb 2021-07-24 stsp if (err)
2149 161728eb 2021-07-24 stsp goto done;
2150 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2151 161728eb 2021-07-24 stsp if (err)
2152 161728eb 2021-07-24 stsp goto done;
2153 161728eb 2021-07-24 stsp target = id_str;
2154 161728eb 2021-07-24 stsp }
2155 161728eb 2021-07-24 stsp
2156 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2157 161728eb 2021-07-24 stsp if (err)
2158 161728eb 2021-07-24 stsp goto done;
2159 161728eb 2021-07-24 stsp
2160 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2161 161728eb 2021-07-24 stsp done:
2162 161728eb 2021-07-24 stsp free(id);
2163 161728eb 2021-07-24 stsp free(id_str);
2164 f21ec2f0 2020-03-21 stsp return err;
2165 f21ec2f0 2020-03-21 stsp }
2166 f21ec2f0 2020-03-21 stsp
2167 f21ec2f0 2020-03-21 stsp static const struct got_error *
2168 161728eb 2021-07-24 stsp delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2169 161728eb 2021-07-24 stsp {
2170 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2171 161728eb 2021-07-24 stsp struct got_reflist_head refs;
2172 161728eb 2021-07-24 stsp struct got_reflist_entry *re;
2173 161728eb 2021-07-24 stsp char *prefix;
2174 161728eb 2021-07-24 stsp
2175 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2176 161728eb 2021-07-24 stsp
2177 161728eb 2021-07-24 stsp if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2178 161728eb 2021-07-24 stsp err = got_error_from_errno("asprintf");
2179 161728eb 2021-07-24 stsp goto done;
2180 161728eb 2021-07-24 stsp }
2181 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2182 161728eb 2021-07-24 stsp if (err)
2183 161728eb 2021-07-24 stsp goto done;
2184 161728eb 2021-07-24 stsp
2185 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2186 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2187 161728eb 2021-07-24 stsp done:
2188 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2189 161728eb 2021-07-24 stsp return err;
2190 161728eb 2021-07-24 stsp }
2191 161728eb 2021-07-24 stsp
2192 161728eb 2021-07-24 stsp static const struct got_error *
2193 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2194 7848a0e1 2020-03-19 stsp {
2195 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2196 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2197 7848a0e1 2020-03-19 stsp const char *remote_name;
2198 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2199 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2200 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2201 7848a0e1 2020-03-19 stsp int nremotes;
2202 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2203 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2204 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2205 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2206 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2207 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2208 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2209 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2210 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2211 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2212 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2213 161728eb 2021-07-24 stsp int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2214 7848a0e1 2020-03-19 stsp
2215 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2216 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2217 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2218 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2219 7848a0e1 2020-03-19 stsp
2220 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2221 7848a0e1 2020-03-19 stsp switch (ch) {
2222 659e7fbd 2020-03-20 stsp case 'a':
2223 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2224 4ba14133 2020-03-20 stsp break;
2225 4ba14133 2020-03-20 stsp case 'b':
2226 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2227 4ba14133 2020-03-20 stsp optarg, NULL);
2228 4ba14133 2020-03-20 stsp if (error)
2229 4ba14133 2020-03-20 stsp return error;
2230 41b0de12 2020-03-21 stsp break;
2231 f21ec2f0 2020-03-21 stsp case 'd':
2232 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2233 f21ec2f0 2020-03-21 stsp break;
2234 41b0de12 2020-03-21 stsp case 'l':
2235 41b0de12 2020-03-21 stsp list_refs_only = 1;
2236 659e7fbd 2020-03-20 stsp break;
2237 7848a0e1 2020-03-19 stsp case 'r':
2238 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2239 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2240 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2241 7848a0e1 2020-03-19 stsp optarg);
2242 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2243 7848a0e1 2020-03-19 stsp break;
2244 db6d8ad8 2020-03-21 stsp case 't':
2245 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2246 db6d8ad8 2020-03-21 stsp break;
2247 7848a0e1 2020-03-19 stsp case 'v':
2248 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2249 7848a0e1 2020-03-19 stsp verbosity = 0;
2250 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2251 7848a0e1 2020-03-19 stsp verbosity++;
2252 7848a0e1 2020-03-19 stsp break;
2253 7848a0e1 2020-03-19 stsp case 'q':
2254 7848a0e1 2020-03-19 stsp verbosity = -1;
2255 7848a0e1 2020-03-19 stsp break;
2256 0e4002ca 2020-03-21 stsp case 'R':
2257 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2258 0e4002ca 2020-03-21 stsp optarg, NULL);
2259 0e4002ca 2020-03-21 stsp if (error)
2260 0e4002ca 2020-03-21 stsp return error;
2261 0e4002ca 2020-03-21 stsp break;
2262 161728eb 2021-07-24 stsp case 'X':
2263 161728eb 2021-07-24 stsp delete_remote = 1;
2264 161728eb 2021-07-24 stsp break;
2265 7848a0e1 2020-03-19 stsp default:
2266 7848a0e1 2020-03-19 stsp usage_fetch();
2267 7848a0e1 2020-03-19 stsp break;
2268 7848a0e1 2020-03-19 stsp }
2269 7848a0e1 2020-03-19 stsp }
2270 7848a0e1 2020-03-19 stsp argc -= optind;
2271 7848a0e1 2020-03-19 stsp argv += optind;
2272 7848a0e1 2020-03-19 stsp
2273 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2274 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2275 41b0de12 2020-03-21 stsp if (list_refs_only) {
2276 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2277 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2278 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2279 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2280 f21ec2f0 2020-03-21 stsp if (delete_refs)
2281 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2282 161728eb 2021-07-24 stsp if (delete_remote)
2283 161728eb 2021-07-24 stsp option_conflict('l', 'X');
2284 41b0de12 2020-03-21 stsp }
2285 161728eb 2021-07-24 stsp if (delete_remote) {
2286 161728eb 2021-07-24 stsp if (fetch_all_branches)
2287 161728eb 2021-07-24 stsp option_conflict('X', 'a');
2288 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_branches))
2289 161728eb 2021-07-24 stsp option_conflict('X', 'b');
2290 161728eb 2021-07-24 stsp if (delete_refs)
2291 161728eb 2021-07-24 stsp option_conflict('X', 'd');
2292 161728eb 2021-07-24 stsp if (replace_tags)
2293 161728eb 2021-07-24 stsp option_conflict('X', 't');
2294 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_refs))
2295 161728eb 2021-07-24 stsp option_conflict('X', 'R');
2296 161728eb 2021-07-24 stsp }
2297 161728eb 2021-07-24 stsp
2298 161728eb 2021-07-24 stsp if (argc == 0) {
2299 161728eb 2021-07-24 stsp if (delete_remote)
2300 161728eb 2021-07-24 stsp errx(1, "-X option requires a remote name");
2301 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2302 161728eb 2021-07-24 stsp } else if (argc == 1)
2303 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2304 7848a0e1 2020-03-19 stsp else
2305 7848a0e1 2020-03-19 stsp usage_fetch();
2306 7848a0e1 2020-03-19 stsp
2307 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2308 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2309 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2310 7848a0e1 2020-03-19 stsp goto done;
2311 7848a0e1 2020-03-19 stsp }
2312 7848a0e1 2020-03-19 stsp
2313 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2314 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2315 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2316 7848a0e1 2020-03-19 stsp goto done;
2317 7848a0e1 2020-03-19 stsp else
2318 7848a0e1 2020-03-19 stsp error = NULL;
2319 7848a0e1 2020-03-19 stsp if (worktree) {
2320 7848a0e1 2020-03-19 stsp repo_path =
2321 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2322 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2323 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2324 7848a0e1 2020-03-19 stsp if (error)
2325 7848a0e1 2020-03-19 stsp goto done;
2326 7848a0e1 2020-03-19 stsp } else {
2327 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2328 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2329 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2330 7848a0e1 2020-03-19 stsp goto done;
2331 7848a0e1 2020-03-19 stsp }
2332 7848a0e1 2020-03-19 stsp }
2333 7848a0e1 2020-03-19 stsp }
2334 7848a0e1 2020-03-19 stsp
2335 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2336 7848a0e1 2020-03-19 stsp if (error)
2337 7848a0e1 2020-03-19 stsp goto done;
2338 7848a0e1 2020-03-19 stsp
2339 161728eb 2021-07-24 stsp if (delete_remote) {
2340 161728eb 2021-07-24 stsp error = delete_refs_for_remote(repo, remote_name);
2341 161728eb 2021-07-24 stsp goto done; /* nothing else to do */
2342 161728eb 2021-07-24 stsp }
2343 161728eb 2021-07-24 stsp
2344 50b0790e 2020-09-11 stsp if (worktree) {
2345 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2346 50b0790e 2020-09-11 stsp if (worktree_conf) {
2347 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2348 50b0790e 2020-09-11 stsp worktree_conf);
2349 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2350 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2351 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2352 50b0790e 2020-09-11 stsp break;
2353 54eb00d5 2020-10-20 stsp }
2354 50b0790e 2020-09-11 stsp }
2355 50b0790e 2020-09-11 stsp }
2356 7848a0e1 2020-03-19 stsp }
2357 50b0790e 2020-09-11 stsp if (remote == NULL) {
2358 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2359 50b0790e 2020-09-11 stsp if (repo_conf) {
2360 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2361 50b0790e 2020-09-11 stsp repo_conf);
2362 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2363 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2364 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2365 50b0790e 2020-09-11 stsp break;
2366 54eb00d5 2020-10-20 stsp }
2367 50b0790e 2020-09-11 stsp }
2368 50b0790e 2020-09-11 stsp }
2369 50b0790e 2020-09-11 stsp }
2370 50b0790e 2020-09-11 stsp if (remote == NULL) {
2371 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2372 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2373 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2374 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2375 257add31 2020-09-09 stsp break;
2376 54eb00d5 2020-10-20 stsp }
2377 257add31 2020-09-09 stsp }
2378 7848a0e1 2020-03-19 stsp }
2379 50b0790e 2020-09-11 stsp if (remote == NULL) {
2380 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2381 50b0790e 2020-09-11 stsp goto done;
2382 b8adfa55 2020-09-25 stsp }
2383 b8adfa55 2020-09-25 stsp
2384 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2385 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2386 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2387 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_branches; i++) {
2388 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2389 6480c871 2021-08-30 stsp remote->fetch_branches[i], NULL);
2390 99495ddb 2021-01-10 stsp }
2391 99495ddb 2021-01-10 stsp }
2392 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2393 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_refs; i++) {
2394 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2395 6480c871 2021-08-30 stsp remote->fetch_refs[i], NULL);
2396 b8adfa55 2020-09-25 stsp }
2397 50b0790e 2020-09-11 stsp }
2398 7848a0e1 2020-03-19 stsp
2399 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2400 6480c871 2021-08-30 stsp &repo_name, remote->fetch_url);
2401 7848a0e1 2020-03-19 stsp if (error)
2402 7848a0e1 2020-03-19 stsp goto done;
2403 7848a0e1 2020-03-19 stsp
2404 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2405 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2406 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2407 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2408 7848a0e1 2020-03-19 stsp err(1, "pledge");
2409 7848a0e1 2020-03-19 stsp #endif
2410 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2411 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2412 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2413 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2414 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2415 7848a0e1 2020-03-19 stsp err(1, "pledge");
2416 7848a0e1 2020-03-19 stsp #endif
2417 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2418 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2419 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2420 7848a0e1 2020-03-19 stsp goto done;
2421 7848a0e1 2020-03-19 stsp } else {
2422 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2423 7848a0e1 2020-03-19 stsp goto done;
2424 7848a0e1 2020-03-19 stsp }
2425 7848a0e1 2020-03-19 stsp
2426 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
2427 d65a88a2 2021-09-05 stsp if (error)
2428 d65a88a2 2021-09-05 stsp goto done;
2429 d65a88a2 2021-09-05 stsp
2430 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2431 7848a0e1 2020-03-19 stsp if (error)
2432 7848a0e1 2020-03-19 stsp goto done;
2433 f79e6490 2020-04-19 stsp
2434 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2435 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2436 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2437 7848a0e1 2020-03-19 stsp
2438 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2439 9c52365f 2020-03-21 stsp server_path, verbosity);
2440 7848a0e1 2020-03-19 stsp if (error)
2441 7848a0e1 2020-03-19 stsp goto done;
2442 7848a0e1 2020-03-19 stsp
2443 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2444 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2445 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2446 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2447 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2448 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2449 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2450 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2451 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2452 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2453 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2454 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2455 7848a0e1 2020-03-19 stsp if (error)
2456 7848a0e1 2020-03-19 stsp goto done;
2457 7848a0e1 2020-03-19 stsp
2458 41b0de12 2020-03-21 stsp if (list_refs_only) {
2459 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2460 41b0de12 2020-03-21 stsp goto done;
2461 41b0de12 2020-03-21 stsp }
2462 41b0de12 2020-03-21 stsp
2463 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2464 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2465 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2466 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2467 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2468 984065c8 2020-03-19 stsp if (error)
2469 984065c8 2020-03-19 stsp goto done;
2470 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2471 984065c8 2020-03-19 stsp free(id_str);
2472 984065c8 2020-03-19 stsp id_str = NULL;
2473 984065c8 2020-03-19 stsp }
2474 7848a0e1 2020-03-19 stsp
2475 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2476 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2477 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2478 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2479 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2480 7848a0e1 2020-03-19 stsp char *remote_refname;
2481 7848a0e1 2020-03-19 stsp
2482 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2483 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2484 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2485 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2486 0e4002ca 2020-03-21 stsp if (error)
2487 0e4002ca 2020-03-21 stsp goto done;
2488 0e4002ca 2020-03-21 stsp continue;
2489 0e4002ca 2020-03-21 stsp }
2490 0e4002ca 2020-03-21 stsp
2491 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2492 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2493 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2494 7848a0e1 2020-03-19 stsp if (error) {
2495 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2496 7848a0e1 2020-03-19 stsp goto done;
2497 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2498 6338a6a1 2020-03-21 stsp repo);
2499 7848a0e1 2020-03-19 stsp if (error)
2500 7848a0e1 2020-03-19 stsp goto done;
2501 7848a0e1 2020-03-19 stsp } else {
2502 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2503 db6d8ad8 2020-03-21 stsp verbosity, repo);
2504 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2505 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2506 9f142382 2020-03-21 stsp error = unlock_err;
2507 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2508 7848a0e1 2020-03-19 stsp if (error)
2509 7848a0e1 2020-03-19 stsp goto done;
2510 7848a0e1 2020-03-19 stsp }
2511 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2512 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2513 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2514 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2515 7848a0e1 2020-03-19 stsp goto done;
2516 7848a0e1 2020-03-19 stsp }
2517 7848a0e1 2020-03-19 stsp
2518 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2519 7848a0e1 2020-03-19 stsp if (error) {
2520 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2521 7848a0e1 2020-03-19 stsp goto done;
2522 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2523 688f11b3 2020-03-21 stsp verbosity, repo);
2524 7848a0e1 2020-03-19 stsp if (error)
2525 7848a0e1 2020-03-19 stsp goto done;
2526 7848a0e1 2020-03-19 stsp } else {
2527 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2528 db6d8ad8 2020-03-21 stsp verbosity, repo);
2529 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2530 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2531 9f142382 2020-03-21 stsp error = unlock_err;
2532 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2533 7848a0e1 2020-03-19 stsp if (error)
2534 7848a0e1 2020-03-19 stsp goto done;
2535 7848a0e1 2020-03-19 stsp }
2536 2ec30c80 2020-03-20 stsp
2537 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2538 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2539 2ec30c80 2020-03-20 stsp if (error) {
2540 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2541 2ec30c80 2020-03-20 stsp goto done;
2542 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2543 6338a6a1 2020-03-21 stsp repo);
2544 2ec30c80 2020-03-20 stsp if (error)
2545 2ec30c80 2020-03-20 stsp goto done;
2546 9f142382 2020-03-21 stsp } else {
2547 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2548 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2549 9f142382 2020-03-21 stsp error = unlock_err;
2550 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2551 9f142382 2020-03-21 stsp }
2552 7848a0e1 2020-03-19 stsp }
2553 7848a0e1 2020-03-19 stsp }
2554 f1bcca34 2020-03-25 stsp if (delete_refs) {
2555 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2556 3789fd73 2020-03-26 stsp verbosity, repo);
2557 f1bcca34 2020-03-25 stsp if (error)
2558 f1bcca34 2020-03-25 stsp goto done;
2559 f1bcca34 2020-03-25 stsp }
2560 f1bcca34 2020-03-25 stsp
2561 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2562 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2563 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2564 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2565 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2566 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2567 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2568 f1bcca34 2020-03-25 stsp
2569 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2570 f1bcca34 2020-03-25 stsp continue;
2571 f1bcca34 2020-03-25 stsp
2572 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2573 f1bcca34 2020-03-25 stsp continue;
2574 f1bcca34 2020-03-25 stsp
2575 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2576 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2577 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2578 f1bcca34 2020-03-25 stsp goto done;
2579 f1bcca34 2020-03-25 stsp }
2580 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2581 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2582 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2583 f1bcca34 2020-03-25 stsp free(remote_refname);
2584 f1bcca34 2020-03-25 stsp goto done;
2585 f1bcca34 2020-03-25 stsp }
2586 f1bcca34 2020-03-25 stsp
2587 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2588 f1bcca34 2020-03-25 stsp 0);
2589 f1bcca34 2020-03-25 stsp if (error) {
2590 f1bcca34 2020-03-25 stsp free(remote_refname);
2591 f1bcca34 2020-03-25 stsp free(remote_target);
2592 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2593 f1bcca34 2020-03-25 stsp error = NULL;
2594 f1bcca34 2020-03-25 stsp continue;
2595 f1bcca34 2020-03-25 stsp }
2596 f1bcca34 2020-03-25 stsp goto done;
2597 f1bcca34 2020-03-25 stsp }
2598 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2599 f1bcca34 2020-03-25 stsp verbosity, repo);
2600 f1bcca34 2020-03-25 stsp free(remote_refname);
2601 f1bcca34 2020-03-25 stsp free(remote_target);
2602 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2603 f1bcca34 2020-03-25 stsp if (error)
2604 f1bcca34 2020-03-25 stsp goto done;
2605 f1bcca34 2020-03-25 stsp }
2606 f1bcca34 2020-03-25 stsp }
2607 7848a0e1 2020-03-19 stsp done:
2608 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2609 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2610 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2611 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2612 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2613 9c52365f 2020-03-21 stsp }
2614 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2615 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2616 1d0f4054 2021-06-17 stsp if (repo) {
2617 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2618 1d0f4054 2021-06-17 stsp if (error == NULL)
2619 1d0f4054 2021-06-17 stsp error = close_err;
2620 1d0f4054 2021-06-17 stsp }
2621 7848a0e1 2020-03-19 stsp if (worktree)
2622 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2623 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2624 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2625 7848a0e1 2020-03-19 stsp free(pe->data);
2626 7848a0e1 2020-03-19 stsp }
2627 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2628 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2629 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2630 7848a0e1 2020-03-19 stsp free(pe->data);
2631 7848a0e1 2020-03-19 stsp }
2632 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2633 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2634 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2635 7848a0e1 2020-03-19 stsp free(id_str);
2636 7848a0e1 2020-03-19 stsp free(cwd);
2637 7848a0e1 2020-03-19 stsp free(repo_path);
2638 7848a0e1 2020-03-19 stsp free(pack_hash);
2639 7848a0e1 2020-03-19 stsp free(proto);
2640 7848a0e1 2020-03-19 stsp free(host);
2641 7848a0e1 2020-03-19 stsp free(port);
2642 7848a0e1 2020-03-19 stsp free(server_path);
2643 7848a0e1 2020-03-19 stsp free(repo_name);
2644 7848a0e1 2020-03-19 stsp return error;
2645 7848a0e1 2020-03-19 stsp }
2646 7848a0e1 2020-03-19 stsp
2647 7848a0e1 2020-03-19 stsp
2648 7848a0e1 2020-03-19 stsp __dead static void
2649 2ab43947 2020-03-18 stsp usage_checkout(void)
2650 2ab43947 2020-03-18 stsp {
2651 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2652 4ad4a1ec 2021-09-13 tracey "[-p prefix] [-q] repository-path [worktree-path]\n",
2653 4ad4a1ec 2021-09-13 tracey getprogname());
2654 2ab43947 2020-03-18 stsp exit(1);
2655 2ab43947 2020-03-18 stsp }
2656 2ab43947 2020-03-18 stsp
2657 2ab43947 2020-03-18 stsp static void
2658 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2659 2ab43947 2020-03-18 stsp {
2660 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2661 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2662 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2663 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2664 2ab43947 2020-03-18 stsp getprogname());
2665 2ab43947 2020-03-18 stsp }
2666 2ab43947 2020-03-18 stsp
2667 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2668 2ab43947 2020-03-18 stsp const char *worktree_path;
2669 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2670 4ad4a1ec 2021-09-13 tracey int verbosity;
2671 2ab43947 2020-03-18 stsp };
2672 2ab43947 2020-03-18 stsp
2673 2ab43947 2020-03-18 stsp static const struct got_error *
2674 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2675 2ab43947 2020-03-18 stsp {
2676 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2677 2ab43947 2020-03-18 stsp
2678 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2679 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2680 2ab43947 2020-03-18 stsp return NULL;
2681 2ab43947 2020-03-18 stsp
2682 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2683 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2684 2ab43947 2020-03-18 stsp return NULL;
2685 2ab43947 2020-03-18 stsp }
2686 2ab43947 2020-03-18 stsp
2687 2ab43947 2020-03-18 stsp while (path[0] == '/')
2688 2ab43947 2020-03-18 stsp path++;
2689 2ab43947 2020-03-18 stsp
2690 4ad4a1ec 2021-09-13 tracey if (a->verbosity >= 0)
2691 4ad4a1ec 2021-09-13 tracey printf("%c %s/%s\n", status, a->worktree_path, path);
2692 4ad4a1ec 2021-09-13 tracey
2693 2ab43947 2020-03-18 stsp return NULL;
2694 2ab43947 2020-03-18 stsp }
2695 2ab43947 2020-03-18 stsp
2696 2ab43947 2020-03-18 stsp static const struct got_error *
2697 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2698 2ab43947 2020-03-18 stsp {
2699 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2700 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2701 2ab43947 2020-03-18 stsp return NULL;
2702 2ab43947 2020-03-18 stsp }
2703 2ab43947 2020-03-18 stsp
2704 2ab43947 2020-03-18 stsp static const struct got_error *
2705 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2706 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2707 2ab43947 2020-03-18 stsp struct got_repository *repo)
2708 2ab43947 2020-03-18 stsp {
2709 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2710 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2711 2ab43947 2020-03-18 stsp
2712 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2713 4e91ef15 2021-09-26 stsp commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2714 2ab43947 2020-03-18 stsp if (err)
2715 2ab43947 2020-03-18 stsp return err;
2716 2ab43947 2020-03-18 stsp
2717 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2718 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2719 2ab43947 2020-03-18 stsp
2720 2ab43947 2020-03-18 stsp /*
2721 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2722 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2723 2ab43947 2020-03-18 stsp *
2724 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2725 2ab43947 2020-03-18 stsp *
2726 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2727 2ab43947 2020-03-18 stsp * \ /
2728 2ab43947 2020-03-18 stsp * C E
2729 2ab43947 2020-03-18 stsp * \ /
2730 2ab43947 2020-03-18 stsp * B (yca)
2731 2ab43947 2020-03-18 stsp * |
2732 2ab43947 2020-03-18 stsp * A
2733 2ab43947 2020-03-18 stsp *
2734 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2735 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2736 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2737 2ab43947 2020-03-18 stsp */
2738 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2739 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2740 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2741 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2742 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2743 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2744 2ab43947 2020-03-18 stsp
2745 2ab43947 2020-03-18 stsp free(yca_id);
2746 2ab43947 2020-03-18 stsp return NULL;
2747 2ab43947 2020-03-18 stsp }
2748 2ab43947 2020-03-18 stsp
2749 2ab43947 2020-03-18 stsp static const struct got_error *
2750 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2751 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2752 2ab43947 2020-03-18 stsp struct got_repository *repo)
2753 2ab43947 2020-03-18 stsp {
2754 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2755 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2756 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2757 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2758 2ab43947 2020-03-18 stsp
2759 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2760 2ab43947 2020-03-18 stsp if (err)
2761 2ab43947 2020-03-18 stsp goto done;
2762 2ab43947 2020-03-18 stsp
2763 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2764 2ab43947 2020-03-18 stsp is_same_branch = 1;
2765 2ab43947 2020-03-18 stsp goto done;
2766 2ab43947 2020-03-18 stsp }
2767 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2768 2ab43947 2020-03-18 stsp is_same_branch = 1;
2769 2ab43947 2020-03-18 stsp goto done;
2770 2ab43947 2020-03-18 stsp }
2771 2ab43947 2020-03-18 stsp
2772 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2773 2ab43947 2020-03-18 stsp if (err)
2774 2ab43947 2020-03-18 stsp goto done;
2775 2ab43947 2020-03-18 stsp
2776 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2777 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2778 2ab43947 2020-03-18 stsp if (err)
2779 2ab43947 2020-03-18 stsp goto done;
2780 2ab43947 2020-03-18 stsp
2781 2ab43947 2020-03-18 stsp for (;;) {
2782 2ab43947 2020-03-18 stsp struct got_object_id *id;
2783 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2784 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2785 2ab43947 2020-03-18 stsp if (err) {
2786 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2787 2ab43947 2020-03-18 stsp err = NULL;
2788 2ab43947 2020-03-18 stsp break;
2789 2ab43947 2020-03-18 stsp }
2790 2ab43947 2020-03-18 stsp
2791 2ab43947 2020-03-18 stsp if (id) {
2792 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2793 2ab43947 2020-03-18 stsp break;
2794 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2795 2ab43947 2020-03-18 stsp is_same_branch = 1;
2796 2ab43947 2020-03-18 stsp break;
2797 2ab43947 2020-03-18 stsp }
2798 2ab43947 2020-03-18 stsp }
2799 2ab43947 2020-03-18 stsp }
2800 2ab43947 2020-03-18 stsp done:
2801 2ab43947 2020-03-18 stsp if (graph)
2802 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2803 2ab43947 2020-03-18 stsp free(head_commit_id);
2804 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2805 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2806 2ab43947 2020-03-18 stsp return err;
2807 a367ff0f 2019-05-14 stsp }
2808 8069f636 2019-01-12 stsp
2809 4ed7e80c 2018-05-20 stsp static const struct got_error *
2810 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2811 4b6c9460 2020-03-05 stsp {
2812 4b6c9460 2020-03-05 stsp static char msg[512];
2813 4b6c9460 2020-03-05 stsp const char *branch_name;
2814 4b6c9460 2020-03-05 stsp
2815 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2816 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2817 4b6c9460 2020-03-05 stsp else
2818 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2819 4b6c9460 2020-03-05 stsp
2820 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2821 4b6c9460 2020-03-05 stsp branch_name += 11;
2822 4b6c9460 2020-03-05 stsp
2823 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2824 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2825 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2826 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2827 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2828 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2829 4b6c9460 2020-03-05 stsp
2830 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2831 4b6c9460 2020-03-05 stsp }
2832 4b6c9460 2020-03-05 stsp
2833 4b6c9460 2020-03-05 stsp static const struct got_error *
2834 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2835 c09a553d 2018-03-12 stsp {
2836 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2837 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2838 08e5873e 2021-09-14 stsp struct got_reference *head_ref = NULL, *ref = NULL;
2839 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2840 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2841 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2842 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2843 08e5873e 2021-09-14 stsp const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2844 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2845 08e5873e 2021-09-14 stsp struct got_object_id *commit_id = NULL;
2846 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2847 4ad4a1ec 2021-09-13 tracey int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2848 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2849 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2850 f2ea84fa 2019-07-27 stsp
2851 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2852 c09a553d 2018-03-12 stsp
2853 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2854 0bb8a95e 2018-03-12 stsp switch (ch) {
2855 08573d5b 2019-05-14 stsp case 'b':
2856 08573d5b 2019-05-14 stsp branch_name = optarg;
2857 08573d5b 2019-05-14 stsp break;
2858 8069f636 2019-01-12 stsp case 'c':
2859 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2860 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2861 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2862 bb51a5b4 2020-01-13 stsp break;
2863 bb51a5b4 2020-01-13 stsp case 'E':
2864 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2865 8069f636 2019-01-12 stsp break;
2866 0bb8a95e 2018-03-12 stsp case 'p':
2867 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2868 0bb8a95e 2018-03-12 stsp break;
2869 4ad4a1ec 2021-09-13 tracey case 'q':
2870 4ad4a1ec 2021-09-13 tracey verbosity = -1;
2871 4ad4a1ec 2021-09-13 tracey break;
2872 0bb8a95e 2018-03-12 stsp default:
2873 2deda0b9 2019-03-07 stsp usage_checkout();
2874 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2875 0bb8a95e 2018-03-12 stsp }
2876 0bb8a95e 2018-03-12 stsp }
2877 0bb8a95e 2018-03-12 stsp
2878 0bb8a95e 2018-03-12 stsp argc -= optind;
2879 0bb8a95e 2018-03-12 stsp argv += optind;
2880 0bb8a95e 2018-03-12 stsp
2881 6715a751 2018-03-16 stsp #ifndef PROFILE
2882 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2883 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2884 c09a553d 2018-03-12 stsp err(1, "pledge");
2885 6715a751 2018-03-16 stsp #endif
2886 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2887 c47340da 2020-09-20 stsp char *base, *dotgit;
2888 f0207f6a 2020-10-19 stsp const char *path;
2889 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2890 76089277 2018-04-01 stsp if (repo_path == NULL)
2891 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2892 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2893 76089277 2018-04-01 stsp if (cwd == NULL) {
2894 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2895 76089277 2018-04-01 stsp goto done;
2896 76089277 2018-04-01 stsp }
2897 c47340da 2020-09-20 stsp if (path_prefix[0])
2898 f0207f6a 2020-10-19 stsp path = path_prefix;
2899 c47340da 2020-09-20 stsp else
2900 f0207f6a 2020-10-19 stsp path = repo_path;
2901 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2902 f0207f6a 2020-10-19 stsp if (error)
2903 c47340da 2020-09-20 stsp goto done;
2904 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2905 c09a553d 2018-03-12 stsp if (dotgit)
2906 c09a553d 2018-03-12 stsp *dotgit = '\0';
2907 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2908 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2909 f0207f6a 2020-10-19 stsp free(base);
2910 76089277 2018-04-01 stsp goto done;
2911 c09a553d 2018-03-12 stsp }
2912 f0207f6a 2020-10-19 stsp free(base);
2913 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2914 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2915 76089277 2018-04-01 stsp if (repo_path == NULL) {
2916 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2917 76089277 2018-04-01 stsp goto done;
2918 76089277 2018-04-01 stsp }
2919 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2920 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2921 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2922 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2923 b4b3a7dd 2019-07-22 stsp argv[1]);
2924 b4b3a7dd 2019-07-22 stsp goto done;
2925 b4b3a7dd 2019-07-22 stsp }
2926 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2927 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2928 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2929 b4b3a7dd 2019-07-22 stsp goto done;
2930 b4b3a7dd 2019-07-22 stsp }
2931 76089277 2018-04-01 stsp }
2932 c09a553d 2018-03-12 stsp } else
2933 c09a553d 2018-03-12 stsp usage_checkout();
2934 c09a553d 2018-03-12 stsp
2935 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2936 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2937 13bfb272 2019-05-10 jcs
2938 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2939 c09a553d 2018-03-12 stsp if (error != NULL)
2940 c02c541e 2019-03-29 stsp goto done;
2941 c02c541e 2019-03-29 stsp
2942 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2943 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2944 c530dc23 2019-07-23 stsp if (error) {
2945 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2946 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2947 c530dc23 2019-07-23 stsp goto done;
2948 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2949 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2950 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2951 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2952 c530dc23 2019-07-23 stsp goto done;
2953 c530dc23 2019-07-23 stsp }
2954 c530dc23 2019-07-23 stsp }
2955 c530dc23 2019-07-23 stsp
2956 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2957 c02c541e 2019-03-29 stsp if (error)
2958 c09a553d 2018-03-12 stsp goto done;
2959 8069f636 2019-01-12 stsp
2960 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2961 c09a553d 2018-03-12 stsp if (error != NULL)
2962 c09a553d 2018-03-12 stsp goto done;
2963 c09a553d 2018-03-12 stsp
2964 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2965 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2966 c09a553d 2018-03-12 stsp goto done;
2967 c09a553d 2018-03-12 stsp
2968 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2969 c09a553d 2018-03-12 stsp if (error != NULL)
2970 c09a553d 2018-03-12 stsp goto done;
2971 c09a553d 2018-03-12 stsp
2972 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2973 e5dc7198 2018-12-29 stsp path_prefix);
2974 e5dc7198 2018-12-29 stsp if (error != NULL)
2975 e5dc7198 2018-12-29 stsp goto done;
2976 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2977 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2978 49520a32 2018-12-29 stsp goto done;
2979 49520a32 2018-12-29 stsp }
2980 49520a32 2018-12-29 stsp
2981 8069f636 2019-01-12 stsp if (commit_id_str) {
2982 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2983 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2984 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2985 84de9106 2020-12-26 stsp NULL);
2986 84de9106 2020-12-26 stsp if (error)
2987 84de9106 2020-12-26 stsp goto done;
2988 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2989 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2990 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2991 30837e32 2019-07-25 stsp if (error)
2992 8069f636 2019-01-12 stsp goto done;
2993 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2994 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2995 8069f636 2019-01-12 stsp if (error != NULL) {
2996 8069f636 2019-01-12 stsp free(commit_id);
2997 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2998 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2999 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
3000 4b6c9460 2020-03-05 stsp }
3001 8069f636 2019-01-12 stsp goto done;
3002 8069f636 2019-01-12 stsp }
3003 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3004 4b6c9460 2020-03-05 stsp if (error) {
3005 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
3006 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
3007 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
3008 4b6c9460 2020-03-05 stsp }
3009 45d344f6 2019-05-14 stsp goto done;
3010 4b6c9460 2020-03-05 stsp }
3011 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3012 8069f636 2019-01-12 stsp commit_id);
3013 8069f636 2019-01-12 stsp if (error)
3014 8069f636 2019-01-12 stsp goto done;
3015 08e5873e 2021-09-14 stsp /* Expand potentially abbreviated commit ID string. */
3016 08e5873e 2021-09-14 stsp free(commit_id_str);
3017 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3018 08e5873e 2021-09-14 stsp if (error)
3019 08e5873e 2021-09-14 stsp goto done;
3020 08e5873e 2021-09-14 stsp } else {
3021 08e5873e 2021-09-14 stsp commit_id = got_object_id_dup(
3022 08e5873e 2021-09-14 stsp got_worktree_get_base_commit_id(worktree));
3023 08e5873e 2021-09-14 stsp if (commit_id == NULL) {
3024 08e5873e 2021-09-14 stsp error = got_error_from_errno("got_object_id_dup");
3025 08e5873e 2021-09-14 stsp goto done;
3026 08e5873e 2021-09-14 stsp }
3027 08e5873e 2021-09-14 stsp error = got_object_id_str(&commit_id_str, commit_id);
3028 08e5873e 2021-09-14 stsp if (error)
3029 08e5873e 2021-09-14 stsp goto done;
3030 8069f636 2019-01-12 stsp }
3031 8069f636 2019-01-12 stsp
3032 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
3033 f2ea84fa 2019-07-27 stsp if (error)
3034 f2ea84fa 2019-07-27 stsp goto done;
3035 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
3036 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
3037 4ad4a1ec 2021-09-13 tracey cpa.verbosity = verbosity;
3038 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3039 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
3040 c09a553d 2018-03-12 stsp if (error != NULL)
3041 c09a553d 2018-03-12 stsp goto done;
3042 c09a553d 2018-03-12 stsp
3043 08e5873e 2021-09-14 stsp if (got_ref_is_symbolic(head_ref)) {
3044 08e5873e 2021-09-14 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3045 08e5873e 2021-09-14 stsp if (error)
3046 08e5873e 2021-09-14 stsp goto done;
3047 08e5873e 2021-09-14 stsp refname = got_ref_get_name(ref);
3048 08e5873e 2021-09-14 stsp } else
3049 08e5873e 2021-09-14 stsp refname = got_ref_get_name(head_ref);
3050 08e5873e 2021-09-14 stsp printf("Checked out %s: %s\n", refname, commit_id_str);
3051 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
3052 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
3053 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
3054 c09a553d 2018-03-12 stsp done:
3055 08e5873e 2021-09-14 stsp if (head_ref)
3056 08e5873e 2021-09-14 stsp got_ref_close(head_ref);
3057 08e5873e 2021-09-14 stsp if (ref)
3058 08e5873e 2021-09-14 stsp got_ref_close(ref);
3059 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3060 8069f636 2019-01-12 stsp free(commit_id_str);
3061 08e5873e 2021-09-14 stsp free(commit_id);
3062 76089277 2018-04-01 stsp free(repo_path);
3063 507dc3bb 2018-12-29 stsp free(worktree_path);
3064 c47340da 2020-09-20 stsp free(cwd);
3065 507dc3bb 2018-12-29 stsp return error;
3066 9627c110 2020-04-18 stsp }
3067 9627c110 2020-04-18 stsp
3068 9627c110 2020-04-18 stsp struct got_update_progress_arg {
3069 9627c110 2020-04-18 stsp int did_something;
3070 9627c110 2020-04-18 stsp int conflicts;
3071 9627c110 2020-04-18 stsp int obstructed;
3072 9627c110 2020-04-18 stsp int not_updated;
3073 f259c4c1 2021-09-24 stsp int missing;
3074 35ca1db7 2021-09-28 stsp int not_deleted;
3075 35ca1db7 2021-09-28 stsp int unversioned;
3076 4ad4a1ec 2021-09-13 tracey int verbosity;
3077 9627c110 2020-04-18 stsp };
3078 9627c110 2020-04-18 stsp
3079 9627c110 2020-04-18 stsp void
3080 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3081 9627c110 2020-04-18 stsp {
3082 9627c110 2020-04-18 stsp if (!upa->did_something)
3083 9627c110 2020-04-18 stsp return;
3084 9627c110 2020-04-18 stsp
3085 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
3086 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3087 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
3088 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
3089 9627c110 2020-04-18 stsp upa->obstructed);
3090 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
3091 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
3092 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
3093 507dc3bb 2018-12-29 stsp }
3094 507dc3bb 2018-12-29 stsp
3095 35ca1db7 2021-09-28 stsp /*
3096 35ca1db7 2021-09-28 stsp * The meaning of some status codes differs between merge-style operations and
3097 35ca1db7 2021-09-28 stsp * update operations. For example, the ! status code means "file was missing"
3098 35ca1db7 2021-09-28 stsp * if changes were merged into the work tree, and "missing file was restored"
3099 35ca1db7 2021-09-28 stsp * if the work tree was updated. This function should be used by any operation
3100 35ca1db7 2021-09-28 stsp * which merges changes into the work tree without updating the work tree.
3101 35ca1db7 2021-09-28 stsp */
3102 35ca1db7 2021-09-28 stsp void
3103 35ca1db7 2021-09-28 stsp print_merge_progress_stats(struct got_update_progress_arg *upa)
3104 35ca1db7 2021-09-28 stsp {
3105 35ca1db7 2021-09-28 stsp if (!upa->did_something)
3106 35ca1db7 2021-09-28 stsp return;
3107 35ca1db7 2021-09-28 stsp
3108 35ca1db7 2021-09-28 stsp if (upa->conflicts > 0)
3109 35ca1db7 2021-09-28 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3110 35ca1db7 2021-09-28 stsp if (upa->obstructed > 0)
3111 35ca1db7 2021-09-28 stsp printf("File paths obstructed by a non-regular file: %d\n",
3112 35ca1db7 2021-09-28 stsp upa->obstructed);
3113 35ca1db7 2021-09-28 stsp if (upa->missing > 0)
3114 35ca1db7 2021-09-28 stsp printf("Files which had incoming changes but could not be "
3115 35ca1db7 2021-09-28 stsp "found in the work tree: %d\n", upa->missing);
3116 35ca1db7 2021-09-28 stsp if (upa->not_deleted > 0)
3117 35ca1db7 2021-09-28 stsp printf("Files not deleted due to differences in deleted "
3118 35ca1db7 2021-09-28 stsp "content: %d\n", upa->not_deleted);
3119 35ca1db7 2021-09-28 stsp if (upa->unversioned > 0)
3120 35ca1db7 2021-09-28 stsp printf("Files not merged because an unversioned file was "
3121 35ca1db7 2021-09-28 stsp "found in the work tree: %d\n", upa->unversioned);
3122 35ca1db7 2021-09-28 stsp }
3123 35ca1db7 2021-09-28 stsp
3124 507dc3bb 2018-12-29 stsp __dead static void
3125 507dc3bb 2018-12-29 stsp usage_update(void)
3126 507dc3bb 2018-12-29 stsp {
3127 4ad4a1ec 2021-09-13 tracey fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3128 4ad4a1ec 2021-09-13 tracey "[path ...]\n",
3129 507dc3bb 2018-12-29 stsp getprogname());
3130 507dc3bb 2018-12-29 stsp exit(1);
3131 507dc3bb 2018-12-29 stsp }
3132 507dc3bb 2018-12-29 stsp
3133 1ee397ad 2019-07-12 stsp static const struct got_error *
3134 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
3135 507dc3bb 2018-12-29 stsp {
3136 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3137 784955db 2019-01-12 stsp
3138 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
3139 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
3140 1ee397ad 2019-07-12 stsp return NULL;
3141 507dc3bb 2018-12-29 stsp
3142 9627c110 2020-04-18 stsp upa->did_something = 1;
3143 a484d721 2019-06-10 stsp
3144 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
3145 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
3146 1ee397ad 2019-07-12 stsp return NULL;
3147 a484d721 2019-06-10 stsp
3148 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
3149 9627c110 2020-04-18 stsp upa->conflicts++;
3150 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
3151 9627c110 2020-04-18 stsp upa->obstructed++;
3152 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
3153 9627c110 2020-04-18 stsp upa->not_updated++;
3154 f259c4c1 2021-09-24 stsp if (status == GOT_STATUS_MISSING)
3155 f259c4c1 2021-09-24 stsp upa->missing++;
3156 35ca1db7 2021-09-28 stsp if (status == GOT_STATUS_CANNOT_DELETE)
3157 35ca1db7 2021-09-28 stsp upa->not_deleted++;
3158 35ca1db7 2021-09-28 stsp if (status == GOT_STATUS_UNVERSIONED)
3159 35ca1db7 2021-09-28 stsp upa->unversioned++;
3160 9627c110 2020-04-18 stsp
3161 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3162 507dc3bb 2018-12-29 stsp path++;
3163 4ad4a1ec 2021-09-13 tracey if (upa->verbosity >= 0)
3164 4ad4a1ec 2021-09-13 tracey printf("%c %s\n", status, path);
3165 4ad4a1ec 2021-09-13 tracey
3166 1ee397ad 2019-07-12 stsp return NULL;
3167 be7061eb 2018-12-30 stsp }
3168 be7061eb 2018-12-30 stsp
3169 be7061eb 2018-12-30 stsp static const struct got_error *
3170 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
3171 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3172 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3173 a1fb16d8 2019-05-24 stsp {
3174 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3175 a1fb16d8 2019-05-24 stsp char *base_id_str;
3176 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3177 a1fb16d8 2019-05-24 stsp
3178 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3179 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3180 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3181 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3182 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3183 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3184 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3185 a1fb16d8 2019-05-24 stsp }
3186 a1fb16d8 2019-05-24 stsp
3187 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3188 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3189 a1fb16d8 2019-05-24 stsp if (err) {
3190 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3191 a1fb16d8 2019-05-24 stsp return err;
3192 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3193 a1fb16d8 2019-05-24 stsp }
3194 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3195 a1fb16d8 2019-05-24 stsp return NULL;
3196 a1fb16d8 2019-05-24 stsp
3197 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3198 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3199 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3200 a1fb16d8 2019-05-24 stsp if (err)
3201 a1fb16d8 2019-05-24 stsp return err;
3202 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3203 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3204 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3205 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3206 0ebf8283 2019-07-24 stsp return NULL;
3207 0ebf8283 2019-07-24 stsp }
3208 0ebf8283 2019-07-24 stsp
3209 0ebf8283 2019-07-24 stsp static const struct got_error *
3210 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3211 0ebf8283 2019-07-24 stsp {
3212 0ebf8283 2019-07-24 stsp const struct got_error *err;
3213 0ebf8283 2019-07-24 stsp int in_progress;
3214 0ebf8283 2019-07-24 stsp
3215 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3216 0ebf8283 2019-07-24 stsp if (err)
3217 0ebf8283 2019-07-24 stsp return err;
3218 0ebf8283 2019-07-24 stsp if (in_progress)
3219 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3220 0ebf8283 2019-07-24 stsp
3221 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3222 0ebf8283 2019-07-24 stsp if (err)
3223 0ebf8283 2019-07-24 stsp return err;
3224 0ebf8283 2019-07-24 stsp if (in_progress)
3225 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3226 f259c4c1 2021-09-24 stsp
3227 f259c4c1 2021-09-24 stsp return NULL;
3228 f259c4c1 2021-09-24 stsp }
3229 f259c4c1 2021-09-24 stsp
3230 f259c4c1 2021-09-24 stsp static const struct got_error *
3231 f259c4c1 2021-09-24 stsp check_merge_in_progress(struct got_worktree *worktree,
3232 f259c4c1 2021-09-24 stsp struct got_repository *repo)
3233 f259c4c1 2021-09-24 stsp {
3234 f259c4c1 2021-09-24 stsp const struct got_error *err;
3235 f259c4c1 2021-09-24 stsp int in_progress;
3236 f259c4c1 2021-09-24 stsp
3237 f259c4c1 2021-09-24 stsp err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3238 f259c4c1 2021-09-24 stsp if (err)
3239 f259c4c1 2021-09-24 stsp return err;
3240 f259c4c1 2021-09-24 stsp if (in_progress)
3241 f259c4c1 2021-09-24 stsp return got_error(GOT_ERR_MERGE_BUSY);
3242 0ebf8283 2019-07-24 stsp
3243 a1fb16d8 2019-05-24 stsp return NULL;
3244 a5edda0a 2019-07-27 stsp }
3245 a5edda0a 2019-07-27 stsp
3246 a5edda0a 2019-07-27 stsp static const struct got_error *
3247 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3248 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3249 a5edda0a 2019-07-27 stsp {
3250 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3251 a5edda0a 2019-07-27 stsp char *path;
3252 10a623df 2021-10-11 stsp struct got_pathlist_entry *new;
3253 a5edda0a 2019-07-27 stsp int i;
3254 a5edda0a 2019-07-27 stsp
3255 a5edda0a 2019-07-27 stsp if (argc == 0) {
3256 a5edda0a 2019-07-27 stsp path = strdup("");
3257 a5edda0a 2019-07-27 stsp if (path == NULL)
3258 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3259 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3260 a5edda0a 2019-07-27 stsp }
3261 a5edda0a 2019-07-27 stsp
3262 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3263 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3264 a5edda0a 2019-07-27 stsp if (err)
3265 a5edda0a 2019-07-27 stsp break;
3266 10a623df 2021-10-11 stsp err = got_pathlist_insert(&new, paths, path, NULL);
3267 10a623df 2021-10-11 stsp if (err || new == NULL /* duplicate */) {
3268 a5edda0a 2019-07-27 stsp free(path);
3269 10a623df 2021-10-11 stsp if (err)
3270 10a623df 2021-10-11 stsp break;
3271 a5edda0a 2019-07-27 stsp }
3272 a5edda0a 2019-07-27 stsp }
3273 a5edda0a 2019-07-27 stsp
3274 a5edda0a 2019-07-27 stsp return err;
3275 a1fb16d8 2019-05-24 stsp }
3276 a1fb16d8 2019-05-24 stsp
3277 a1fb16d8 2019-05-24 stsp static const struct got_error *
3278 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3279 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3280 fa51e947 2020-03-27 stsp {
3281 fa51e947 2020-03-27 stsp const struct got_error *err;
3282 fa51e947 2020-03-27 stsp struct got_repository *repo;
3283 fa51e947 2020-03-27 stsp static char msg[512];
3284 fa51e947 2020-03-27 stsp
3285 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3286 fa51e947 2020-03-27 stsp if (err)
3287 fa51e947 2020-03-27 stsp return orig_err;
3288 fa51e947 2020-03-27 stsp
3289 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3290 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3291 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3292 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3293 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3294 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3295 fa51e947 2020-03-27 stsp got_repo_close(repo);
3296 fa51e947 2020-03-27 stsp return err;
3297 fa51e947 2020-03-27 stsp }
3298 fa51e947 2020-03-27 stsp
3299 fa51e947 2020-03-27 stsp static const struct got_error *
3300 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3301 507dc3bb 2018-12-29 stsp {
3302 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3303 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3304 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3305 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3306 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3307 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3308 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3309 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3310 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3311 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3312 4ad4a1ec 2021-09-13 tracey int ch, verbosity = 0;
3313 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3314 507dc3bb 2018-12-29 stsp
3315 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3316 f2ea84fa 2019-07-27 stsp
3317 4ad4a1ec 2021-09-13 tracey while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3318 507dc3bb 2018-12-29 stsp switch (ch) {
3319 024e9686 2019-05-14 stsp case 'b':
3320 024e9686 2019-05-14 stsp branch_name = optarg;
3321 024e9686 2019-05-14 stsp break;
3322 507dc3bb 2018-12-29 stsp case 'c':
3323 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3324 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3325 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3326 4ad4a1ec 2021-09-13 tracey break;
3327 4ad4a1ec 2021-09-13 tracey case 'q':
3328 4ad4a1ec 2021-09-13 tracey verbosity = -1;
3329 507dc3bb 2018-12-29 stsp break;
3330 507dc3bb 2018-12-29 stsp default:
3331 2deda0b9 2019-03-07 stsp usage_update();
3332 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3333 507dc3bb 2018-12-29 stsp }
3334 507dc3bb 2018-12-29 stsp }
3335 507dc3bb 2018-12-29 stsp
3336 507dc3bb 2018-12-29 stsp argc -= optind;
3337 507dc3bb 2018-12-29 stsp argv += optind;
3338 507dc3bb 2018-12-29 stsp
3339 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3340 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3341 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3342 507dc3bb 2018-12-29 stsp err(1, "pledge");
3343 507dc3bb 2018-12-29 stsp #endif
3344 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3345 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3346 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3347 c4cdcb68 2019-04-03 stsp goto done;
3348 c4cdcb68 2019-04-03 stsp }
3349 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3350 fa51e947 2020-03-27 stsp if (error) {
3351 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3352 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3353 fa51e947 2020-03-27 stsp worktree_path);
3354 7d5807f4 2019-07-11 stsp goto done;
3355 fa51e947 2020-03-27 stsp }
3356 7d5807f4 2019-07-11 stsp
3357 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3358 f2ea84fa 2019-07-27 stsp if (error)
3359 f2ea84fa 2019-07-27 stsp goto done;
3360 507dc3bb 2018-12-29 stsp
3361 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3362 c9956ddf 2019-09-08 stsp NULL);
3363 507dc3bb 2018-12-29 stsp if (error != NULL)
3364 507dc3bb 2018-12-29 stsp goto done;
3365 507dc3bb 2018-12-29 stsp
3366 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3367 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3368 087fb88c 2019-08-04 stsp if (error)
3369 087fb88c 2019-08-04 stsp goto done;
3370 087fb88c 2019-08-04 stsp
3371 f259c4c1 2021-09-24 stsp error = check_merge_in_progress(worktree, repo);
3372 f259c4c1 2021-09-24 stsp if (error)
3373 f259c4c1 2021-09-24 stsp goto done;
3374 f259c4c1 2021-09-24 stsp
3375 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3376 0266afb7 2019-01-04 stsp if (error)
3377 0266afb7 2019-01-04 stsp goto done;
3378 0266afb7 2019-01-04 stsp
3379 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3380 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3381 024e9686 2019-05-14 stsp if (error != NULL)
3382 024e9686 2019-05-14 stsp goto done;
3383 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3384 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3385 9c4b8182 2019-01-02 stsp if (error != NULL)
3386 9c4b8182 2019-01-02 stsp goto done;
3387 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3388 507dc3bb 2018-12-29 stsp if (error != NULL)
3389 507dc3bb 2018-12-29 stsp goto done;
3390 507dc3bb 2018-12-29 stsp } else {
3391 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3392 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3393 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3394 84de9106 2020-12-26 stsp NULL);
3395 84de9106 2020-12-26 stsp if (error)
3396 84de9106 2020-12-26 stsp goto done;
3397 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3398 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3399 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3400 04f57cb3 2019-07-25 stsp free(commit_id_str);
3401 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3402 30837e32 2019-07-25 stsp if (error)
3403 a297e751 2019-07-11 stsp goto done;
3404 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3405 a297e751 2019-07-11 stsp if (error)
3406 507dc3bb 2018-12-29 stsp goto done;
3407 507dc3bb 2018-12-29 stsp }
3408 35c965b2 2018-12-29 stsp
3409 a1fb16d8 2019-05-24 stsp if (branch_name) {
3410 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3411 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3412 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3413 f2ea84fa 2019-07-27 stsp continue;
3414 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3415 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3416 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3417 024e9686 2019-05-14 stsp goto done;
3418 024e9686 2019-05-14 stsp }
3419 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3420 024e9686 2019-05-14 stsp if (error)
3421 024e9686 2019-05-14 stsp goto done;
3422 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3423 3aef623b 2019-10-15 stsp repo);
3424 a367ff0f 2019-05-14 stsp free(head_commit_id);
3425 024e9686 2019-05-14 stsp if (error != NULL)
3426 024e9686 2019-05-14 stsp goto done;
3427 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3428 a367ff0f 2019-05-14 stsp if (error)
3429 a367ff0f 2019-05-14 stsp goto done;
3430 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3431 024e9686 2019-05-14 stsp if (error)
3432 024e9686 2019-05-14 stsp goto done;
3433 024e9686 2019-05-14 stsp } else {
3434 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3435 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3436 a367ff0f 2019-05-14 stsp if (error != NULL) {
3437 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3438 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3439 024e9686 2019-05-14 stsp goto done;
3440 a367ff0f 2019-05-14 stsp }
3441 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3442 a367ff0f 2019-05-14 stsp if (error)
3443 a367ff0f 2019-05-14 stsp goto done;
3444 024e9686 2019-05-14 stsp }
3445 507dc3bb 2018-12-29 stsp
3446 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3447 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3448 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3449 507dc3bb 2018-12-29 stsp commit_id);
3450 507dc3bb 2018-12-29 stsp if (error)
3451 507dc3bb 2018-12-29 stsp goto done;
3452 507dc3bb 2018-12-29 stsp }
3453 507dc3bb 2018-12-29 stsp
3454 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3455 4ad4a1ec 2021-09-13 tracey upa.verbosity = verbosity;
3456 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3457 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3458 507dc3bb 2018-12-29 stsp if (error != NULL)
3459 507dc3bb 2018-12-29 stsp goto done;
3460 9c4b8182 2019-01-02 stsp
3461 4f3c844b 2021-09-14 stsp if (upa.did_something) {
3462 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
3463 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree), commit_id_str);
3464 4f3c844b 2021-09-14 stsp } else
3465 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3466 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3467 507dc3bb 2018-12-29 stsp done:
3468 c09a553d 2018-03-12 stsp free(worktree_path);
3469 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3470 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3471 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3472 507dc3bb 2018-12-29 stsp free(commit_id);
3473 9c4b8182 2019-01-02 stsp free(commit_id_str);
3474 c09a553d 2018-03-12 stsp return error;
3475 c09a553d 2018-03-12 stsp }
3476 c09a553d 2018-03-12 stsp
3477 f42b1b34 2018-03-12 stsp static const struct got_error *
3478 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3479 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3480 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3481 5c860e29 2018-03-12 stsp {
3482 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3483 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3484 79109fed 2018-03-27 stsp
3485 44392932 2019-08-25 stsp if (blob_id1) {
3486 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3487 44392932 2019-08-25 stsp if (err)
3488 44392932 2019-08-25 stsp goto done;
3489 44392932 2019-08-25 stsp }
3490 79109fed 2018-03-27 stsp
3491 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3492 44392932 2019-08-25 stsp if (err)
3493 44392932 2019-08-25 stsp goto done;
3494 79109fed 2018-03-27 stsp
3495 44392932 2019-08-25 stsp while (path[0] == '/')
3496 44392932 2019-08-25 stsp path++;
3497 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3498 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3499 44392932 2019-08-25 stsp done:
3500 44392932 2019-08-25 stsp if (blob1)
3501 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3502 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3503 44392932 2019-08-25 stsp return err;
3504 44392932 2019-08-25 stsp }
3505 44392932 2019-08-25 stsp
3506 44392932 2019-08-25 stsp static const struct got_error *
3507 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3508 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3509 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3510 44392932 2019-08-25 stsp {
3511 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3512 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3513 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3514 44392932 2019-08-25 stsp
3515 44392932 2019-08-25 stsp if (tree_id1) {
3516 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3517 79109fed 2018-03-27 stsp if (err)
3518 44392932 2019-08-25 stsp goto done;
3519 79109fed 2018-03-27 stsp }
3520 79109fed 2018-03-27 stsp
3521 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3522 0f2b3dca 2018-12-22 stsp if (err)
3523 0f2b3dca 2018-12-22 stsp goto done;
3524 0f2b3dca 2018-12-22 stsp
3525 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3526 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3527 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3528 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3529 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3530 fe621944 2020-11-10 stsp arg.nlines = 0;
3531 44392932 2019-08-25 stsp while (path[0] == '/')
3532 44392932 2019-08-25 stsp path++;
3533 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3534 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3535 0f2b3dca 2018-12-22 stsp done:
3536 79109fed 2018-03-27 stsp if (tree1)
3537 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3538 366e0a5f 2019-10-10 stsp if (tree2)
3539 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3540 44392932 2019-08-25 stsp return err;
3541 44392932 2019-08-25 stsp }
3542 44392932 2019-08-25 stsp
3543 44392932 2019-08-25 stsp static const struct got_error *
3544 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3545 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3546 0208f208 2020-05-05 stsp {
3547 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3548 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3549 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3550 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3551 0208f208 2020-05-05 stsp
3552 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3553 0208f208 2020-05-05 stsp if (qid != NULL) {
3554 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3555 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3556 0208f208 2020-05-05 stsp qid->id);
3557 0208f208 2020-05-05 stsp if (err)
3558 0208f208 2020-05-05 stsp return err;
3559 0208f208 2020-05-05 stsp
3560 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3561 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3562 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3563 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3564 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3565 aa8b5dd0 2021-08-01 stsp }
3566 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3567 0208f208 2020-05-05 stsp
3568 0208f208 2020-05-05 stsp }
3569 0208f208 2020-05-05 stsp
3570 0208f208 2020-05-05 stsp if (tree_id1) {
3571 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3572 0208f208 2020-05-05 stsp if (err)
3573 0208f208 2020-05-05 stsp goto done;
3574 0208f208 2020-05-05 stsp }
3575 0208f208 2020-05-05 stsp
3576 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3577 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3578 0208f208 2020-05-05 stsp if (err)
3579 0208f208 2020-05-05 stsp goto done;
3580 0208f208 2020-05-05 stsp
3581 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3582 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3583 0208f208 2020-05-05 stsp done:
3584 0208f208 2020-05-05 stsp if (tree1)
3585 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3586 0208f208 2020-05-05 stsp if (tree2)
3587 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3588 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3589 0208f208 2020-05-05 stsp return err;
3590 0208f208 2020-05-05 stsp }
3591 0208f208 2020-05-05 stsp
3592 0208f208 2020-05-05 stsp static const struct got_error *
3593 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3594 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3595 44392932 2019-08-25 stsp {
3596 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3597 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3598 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3599 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3600 44392932 2019-08-25 stsp struct got_object_qid *qid;
3601 44392932 2019-08-25 stsp
3602 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3603 44392932 2019-08-25 stsp if (qid != NULL) {
3604 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3605 44392932 2019-08-25 stsp qid->id);
3606 44392932 2019-08-25 stsp if (err)
3607 44392932 2019-08-25 stsp return err;
3608 44392932 2019-08-25 stsp }
3609 44392932 2019-08-25 stsp
3610 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3611 44392932 2019-08-25 stsp int obj_type;
3612 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3613 44392932 2019-08-25 stsp if (err)
3614 44392932 2019-08-25 stsp goto done;
3615 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3616 44392932 2019-08-25 stsp if (err) {
3617 44392932 2019-08-25 stsp free(obj_id2);
3618 44392932 2019-08-25 stsp goto done;
3619 44392932 2019-08-25 stsp }
3620 44392932 2019-08-25 stsp if (pcommit) {
3621 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3622 44392932 2019-08-25 stsp qid->id, path);
3623 44392932 2019-08-25 stsp if (err) {
3624 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3625 2e8c69d1 2020-05-04 stsp free(obj_id2);
3626 2e8c69d1 2020-05-04 stsp goto done;
3627 2e8c69d1 2020-05-04 stsp }
3628 2e8c69d1 2020-05-04 stsp } else {
3629 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3630 2e8c69d1 2020-05-04 stsp if (err) {
3631 2e8c69d1 2020-05-04 stsp free(obj_id2);
3632 2e8c69d1 2020-05-04 stsp goto done;
3633 2e8c69d1 2020-05-04 stsp }
3634 44392932 2019-08-25 stsp }
3635 44392932 2019-08-25 stsp }
3636 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3637 44392932 2019-08-25 stsp if (err) {
3638 44392932 2019-08-25 stsp free(obj_id2);
3639 44392932 2019-08-25 stsp goto done;
3640 44392932 2019-08-25 stsp }
3641 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3642 44392932 2019-08-25 stsp switch (obj_type) {
3643 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3644 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3645 64453f7e 2020-11-21 stsp 0, 0, repo);
3646 44392932 2019-08-25 stsp break;
3647 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3648 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3649 64453f7e 2020-11-21 stsp 0, 0, repo);
3650 44392932 2019-08-25 stsp break;
3651 44392932 2019-08-25 stsp default:
3652 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3653 44392932 2019-08-25 stsp break;
3654 44392932 2019-08-25 stsp }
3655 44392932 2019-08-25 stsp free(obj_id1);
3656 44392932 2019-08-25 stsp free(obj_id2);
3657 44392932 2019-08-25 stsp } else {
3658 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3659 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3660 44392932 2019-08-25 stsp if (err)
3661 44392932 2019-08-25 stsp goto done;
3662 579bd556 2020-10-24 stsp if (pcommit) {
3663 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3664 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3665 579bd556 2020-10-24 stsp if (err)
3666 579bd556 2020-10-24 stsp goto done;
3667 579bd556 2020-10-24 stsp }
3668 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3669 64453f7e 2020-11-21 stsp id_str2);
3670 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3671 64453f7e 2020-11-21 stsp repo);
3672 44392932 2019-08-25 stsp }
3673 44392932 2019-08-25 stsp done:
3674 0f2b3dca 2018-12-22 stsp free(id_str1);
3675 0f2b3dca 2018-12-22 stsp free(id_str2);
3676 44392932 2019-08-25 stsp if (pcommit)
3677 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3678 79109fed 2018-03-27 stsp return err;
3679 79109fed 2018-03-27 stsp }
3680 79109fed 2018-03-27 stsp
3681 4bb494d5 2018-06-16 stsp static char *
3682 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3683 6c281f94 2018-06-11 stsp {
3684 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3685 09867e48 2019-08-13 stsp char *p, *s;
3686 09867e48 2019-08-13 stsp
3687 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3688 09867e48 2019-08-13 stsp if (tm == NULL)
3689 09867e48 2019-08-13 stsp return NULL;
3690 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3691 09867e48 2019-08-13 stsp if (s == NULL)
3692 09867e48 2019-08-13 stsp return NULL;
3693 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3694 6c281f94 2018-06-11 stsp if (p)
3695 6c281f94 2018-06-11 stsp *p = '\0';
3696 6c281f94 2018-06-11 stsp return s;
3697 6c281f94 2018-06-11 stsp }
3698 dc424a06 2019-08-07 stsp
3699 6841bf13 2019-11-29 kn static const struct got_error *
3700 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3701 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3702 6841bf13 2019-11-29 kn {
3703 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3704 6841bf13 2019-11-29 kn regmatch_t regmatch;
3705 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3706 6841bf13 2019-11-29 kn
3707 6841bf13 2019-11-29 kn *have_match = 0;
3708 6841bf13 2019-11-29 kn
3709 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3710 6841bf13 2019-11-29 kn if (err)
3711 6841bf13 2019-11-29 kn return err;
3712 6841bf13 2019-11-29 kn
3713 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3714 6841bf13 2019-11-29 kn if (err)
3715 6841bf13 2019-11-29 kn goto done;
3716 6841bf13 2019-11-29 kn
3717 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3718 6841bf13 2019-11-29 kn *have_match = 1;
3719 6841bf13 2019-11-29 kn done:
3720 6841bf13 2019-11-29 kn free(id_str);
3721 6841bf13 2019-11-29 kn free(logmsg);
3722 6841bf13 2019-11-29 kn return err;
3723 6841bf13 2019-11-29 kn }
3724 6841bf13 2019-11-29 kn
3725 0208f208 2020-05-05 stsp static void
3726 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3727 0208f208 2020-05-05 stsp regex_t *regex)
3728 0208f208 2020-05-05 stsp {
3729 0208f208 2020-05-05 stsp regmatch_t regmatch;
3730 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3731 0208f208 2020-05-05 stsp
3732 0208f208 2020-05-05 stsp *have_match = 0;
3733 0208f208 2020-05-05 stsp
3734 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3735 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3736 0208f208 2020-05-05 stsp *have_match = 1;
3737 0208f208 2020-05-05 stsp break;
3738 0208f208 2020-05-05 stsp }
3739 0208f208 2020-05-05 stsp }
3740 0208f208 2020-05-05 stsp }
3741 0208f208 2020-05-05 stsp
3742 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3743 6c281f94 2018-06-11 stsp
3744 888b7d99 2020-12-26 stsp static const struct got_error*
3745 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3746 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3747 888b7d99 2020-12-26 stsp {
3748 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3749 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3750 888b7d99 2020-12-26 stsp char *s;
3751 888b7d99 2020-12-26 stsp const char *name;
3752 5c860e29 2018-03-12 stsp
3753 888b7d99 2020-12-26 stsp *refs_str = NULL;
3754 888b7d99 2020-12-26 stsp
3755 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3756 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3757 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3758 a436ad14 2019-08-13 stsp int cmp;
3759 a436ad14 2019-08-13 stsp
3760 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3761 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3762 d9498b20 2019-02-05 stsp continue;
3763 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3764 199a4027 2019-02-02 stsp name += 5;
3765 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3766 7143d404 2019-03-12 stsp continue;
3767 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3768 e34f9ed6 2019-02-02 stsp name += 6;
3769 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3770 141c2bff 2019-02-04 stsp name += 8;
3771 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3772 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3773 4343a07f 2020-04-24 stsp continue;
3774 4343a07f 2020-04-24 stsp }
3775 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3776 48cae60d 2020-09-22 stsp if (err)
3777 888b7d99 2020-12-26 stsp break;
3778 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3779 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3780 5d844a1e 2019-08-13 stsp if (err) {
3781 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3782 48cae60d 2020-09-22 stsp free(ref_id);
3783 888b7d99 2020-12-26 stsp break;
3784 48cae60d 2020-09-22 stsp }
3785 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3786 5d844a1e 2019-08-13 stsp err = NULL;
3787 5d844a1e 2019-08-13 stsp tag = NULL;
3788 5d844a1e 2019-08-13 stsp }
3789 a436ad14 2019-08-13 stsp }
3790 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3791 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3792 48cae60d 2020-09-22 stsp free(ref_id);
3793 a436ad14 2019-08-13 stsp if (tag)
3794 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3795 a436ad14 2019-08-13 stsp if (cmp != 0)
3796 a436ad14 2019-08-13 stsp continue;
3797 888b7d99 2020-12-26 stsp s = *refs_str;
3798 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3799 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3800 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3801 199a4027 2019-02-02 stsp free(s);
3802 888b7d99 2020-12-26 stsp *refs_str = NULL;
3803 888b7d99 2020-12-26 stsp break;
3804 199a4027 2019-02-02 stsp }
3805 199a4027 2019-02-02 stsp free(s);
3806 199a4027 2019-02-02 stsp }
3807 888b7d99 2020-12-26 stsp
3808 888b7d99 2020-12-26 stsp return err;
3809 888b7d99 2020-12-26 stsp }
3810 888b7d99 2020-12-26 stsp
3811 888b7d99 2020-12-26 stsp static const struct got_error *
3812 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3813 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3814 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3815 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3816 e600f124 2021-03-21 stsp const char *custom_refs_str)
3817 888b7d99 2020-12-26 stsp {
3818 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3819 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3820 888b7d99 2020-12-26 stsp char datebuf[26];
3821 888b7d99 2020-12-26 stsp time_t committer_time;
3822 888b7d99 2020-12-26 stsp const char *author, *committer;
3823 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3824 888b7d99 2020-12-26 stsp
3825 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3826 8bf5b3c9 2018-03-17 stsp if (err)
3827 8bf5b3c9 2018-03-17 stsp return err;
3828 788c352e 2018-06-16 stsp
3829 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3830 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3831 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3832 e600f124 2021-03-21 stsp if (refs) {
3833 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3834 e600f124 2021-03-21 stsp if (err)
3835 e600f124 2021-03-21 stsp goto done;
3836 e600f124 2021-03-21 stsp }
3837 888b7d99 2020-12-26 stsp }
3838 888b7d99 2020-12-26 stsp
3839 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3840 e600f124 2021-03-21 stsp if (custom_refs_str)
3841 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3842 e600f124 2021-03-21 stsp else
3843 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3844 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3845 832c249c 2018-06-10 stsp free(id_str);
3846 d3d493d7 2019-02-21 stsp id_str = NULL;
3847 d3d493d7 2019-02-21 stsp free(refs_str);
3848 d3d493d7 2019-02-21 stsp refs_str = NULL;
3849 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3850 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3851 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3852 09867e48 2019-08-13 stsp if (datestr)
3853 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3854 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3855 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3856 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3857 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3858 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3859 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3860 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3861 3fe1abad 2018-06-10 stsp int n = 1;
3862 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3863 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
3864 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3865 a0603db2 2018-06-10 stsp if (err)
3866 888b7d99 2020-12-26 stsp goto done;
3867 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3868 a0603db2 2018-06-10 stsp free(id_str);
3869 888b7d99 2020-12-26 stsp id_str = NULL;
3870 a0603db2 2018-06-10 stsp }
3871 a0603db2 2018-06-10 stsp }
3872 832c249c 2018-06-10 stsp
3873 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3874 5943eee2 2019-08-13 stsp if (err)
3875 888b7d99 2020-12-26 stsp goto done;
3876 8bf5b3c9 2018-03-17 stsp
3877 621015ac 2018-07-23 stsp logmsg = logmsg0;
3878 832c249c 2018-06-10 stsp do {
3879 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3880 832c249c 2018-06-10 stsp if (line)
3881 832c249c 2018-06-10 stsp printf(" %s\n", line);
3882 832c249c 2018-06-10 stsp } while (line);
3883 621015ac 2018-07-23 stsp free(logmsg0);
3884 832c249c 2018-06-10 stsp
3885 0208f208 2020-05-05 stsp if (changed_paths) {
3886 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3887 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3888 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3889 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3890 0208f208 2020-05-05 stsp }
3891 0208f208 2020-05-05 stsp printf("\n");
3892 0208f208 2020-05-05 stsp }
3893 971751ac 2018-03-27 stsp if (show_patch) {
3894 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3895 971751ac 2018-03-27 stsp if (err == 0)
3896 971751ac 2018-03-27 stsp printf("\n");
3897 971751ac 2018-03-27 stsp }
3898 07862c20 2018-09-15 stsp
3899 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3900 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3901 888b7d99 2020-12-26 stsp done:
3902 888b7d99 2020-12-26 stsp free(id_str);
3903 888b7d99 2020-12-26 stsp free(refs_str);
3904 07862c20 2018-09-15 stsp return err;
3905 f42b1b34 2018-03-12 stsp }
3906 5c860e29 2018-03-12 stsp
3907 f42b1b34 2018-03-12 stsp static const struct got_error *
3908 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3909 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3910 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3911 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3912 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3913 f42b1b34 2018-03-12 stsp {
3914 f42b1b34 2018-03-12 stsp const struct got_error *err;
3915 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3916 6841bf13 2019-11-29 kn regex_t regex;
3917 6841bf13 2019-11-29 kn int have_match;
3918 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3919 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3920 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3921 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3922 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3923 dbec59df 2020-04-18 stsp
3924 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3925 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3926 372ccdbb 2018-06-10 stsp
3927 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3928 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3929 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3930 6841bf13 2019-11-29 kn
3931 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3932 f42b1b34 2018-03-12 stsp if (err)
3933 f42b1b34 2018-03-12 stsp return err;
3934 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3935 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3936 372ccdbb 2018-06-10 stsp if (err)
3937 fcc85cad 2018-07-22 stsp goto done;
3938 656b1f76 2019-05-11 jcs for (;;) {
3939 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3940 8bf5b3c9 2018-03-17 stsp
3941 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3942 84453469 2018-11-11 stsp break;
3943 84453469 2018-11-11 stsp
3944 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3945 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3946 372ccdbb 2018-06-10 stsp if (err) {
3947 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3948 9ba79e04 2018-06-11 stsp err = NULL;
3949 ee780d5c 2020-01-04 stsp break;
3950 7e665116 2018-04-02 stsp }
3951 b43fbaa0 2018-06-11 stsp if (id == NULL)
3952 7e665116 2018-04-02 stsp break;
3953 b43fbaa0 2018-06-11 stsp
3954 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3955 b43fbaa0 2018-06-11 stsp if (err)
3956 fcc85cad 2018-07-22 stsp break;
3957 6841bf13 2019-11-29 kn
3958 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3959 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3960 0208f208 2020-05-05 stsp if (err)
3961 0208f208 2020-05-05 stsp break;
3962 0208f208 2020-05-05 stsp }
3963 0208f208 2020-05-05 stsp
3964 6841bf13 2019-11-29 kn if (search_pattern) {
3965 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3966 6841bf13 2019-11-29 kn if (err) {
3967 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3968 6841bf13 2019-11-29 kn break;
3969 6841bf13 2019-11-29 kn }
3970 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3971 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3972 0208f208 2020-05-05 stsp &changed_paths, &regex);
3973 6841bf13 2019-11-29 kn if (have_match == 0) {
3974 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3975 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3976 0208f208 2020-05-05 stsp free((char *)pe->path);
3977 0208f208 2020-05-05 stsp free(pe->data);
3978 0208f208 2020-05-05 stsp }
3979 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3980 6841bf13 2019-11-29 kn continue;
3981 6841bf13 2019-11-29 kn }
3982 6841bf13 2019-11-29 kn }
3983 6841bf13 2019-11-29 kn
3984 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3985 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3986 dbec59df 2020-04-18 stsp if (err)
3987 dbec59df 2020-04-18 stsp break;
3988 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3989 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3990 dbec59df 2020-04-18 stsp } else {
3991 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3992 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3993 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3994 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3995 dbec59df 2020-04-18 stsp if (err)
3996 dbec59df 2020-04-18 stsp break;
3997 dbec59df 2020-04-18 stsp }
3998 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3999 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
4000 7e665116 2018-04-02 stsp break;
4001 0208f208 2020-05-05 stsp
4002 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4003 0208f208 2020-05-05 stsp free((char *)pe->path);
4004 0208f208 2020-05-05 stsp free(pe->data);
4005 0208f208 2020-05-05 stsp }
4006 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4007 31cedeaf 2018-09-15 stsp }
4008 dbec59df 2020-04-18 stsp if (reverse_display_order) {
4009 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
4010 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
4011 dbec59df 2020-04-18 stsp if (err)
4012 dbec59df 2020-04-18 stsp break;
4013 502b9684 2020-07-31 stsp if (show_changed_paths) {
4014 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
4015 502b9684 2020-07-31 stsp commit, repo);
4016 502b9684 2020-07-31 stsp if (err)
4017 502b9684 2020-07-31 stsp break;
4018 502b9684 2020-07-31 stsp }
4019 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
4020 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
4021 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
4022 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
4023 dbec59df 2020-04-18 stsp if (err)
4024 dbec59df 2020-04-18 stsp break;
4025 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4026 502b9684 2020-07-31 stsp free((char *)pe->path);
4027 502b9684 2020-07-31 stsp free(pe->data);
4028 502b9684 2020-07-31 stsp }
4029 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
4030 dbec59df 2020-04-18 stsp }
4031 dbec59df 2020-04-18 stsp }
4032 fcc85cad 2018-07-22 stsp done:
4033 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
4034 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
4035 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4036 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
4037 dbec59df 2020-04-18 stsp }
4038 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
4039 0208f208 2020-05-05 stsp free((char *)pe->path);
4040 0208f208 2020-05-05 stsp free(pe->data);
4041 0208f208 2020-05-05 stsp }
4042 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
4043 6841bf13 2019-11-29 kn if (search_pattern)
4044 6841bf13 2019-11-29 kn regfree(&regex);
4045 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
4046 f42b1b34 2018-03-12 stsp return err;
4047 f42b1b34 2018-03-12 stsp }
4048 5c860e29 2018-03-12 stsp
4049 4ed7e80c 2018-05-20 stsp __dead static void
4050 6f3d1eb0 2018-03-12 stsp usage_log(void)
4051 6f3d1eb0 2018-03-12 stsp {
4052 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
4053 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
4054 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
4055 6f3d1eb0 2018-03-12 stsp exit(1);
4056 b1ebc001 2019-08-13 stsp }
4057 b1ebc001 2019-08-13 stsp
4058 b1ebc001 2019-08-13 stsp static int
4059 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
4060 b1ebc001 2019-08-13 stsp {
4061 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
4062 b1ebc001 2019-08-13 stsp long long n;
4063 b1ebc001 2019-08-13 stsp const char *errstr;
4064 b1ebc001 2019-08-13 stsp
4065 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4066 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
4067 b1ebc001 2019-08-13 stsp return 0;
4068 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4069 b1ebc001 2019-08-13 stsp if (errstr != NULL)
4070 b1ebc001 2019-08-13 stsp return 0;
4071 b1ebc001 2019-08-13 stsp return n;
4072 6f3d1eb0 2018-03-12 stsp }
4073 6f3d1eb0 2018-03-12 stsp
4074 4ed7e80c 2018-05-20 stsp static const struct got_error *
4075 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
4076 f42b1b34 2018-03-12 stsp {
4077 f42b1b34 2018-03-12 stsp const struct got_error *error;
4078 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
4079 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
4080 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
4081 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4082 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
4083 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
4084 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
4085 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4086 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
4087 64a96a6d 2018-04-01 stsp const char *errstr;
4088 199a4027 2019-02-02 stsp struct got_reflist_head refs;
4089 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
4090 1b3893a2 2019-03-18 stsp
4091 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4092 5c860e29 2018-03-12 stsp
4093 6715a751 2018-03-16 stsp #ifndef PROFILE
4094 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4095 6098196c 2019-01-04 stsp NULL)
4096 ad242220 2018-09-08 stsp == -1)
4097 f42b1b34 2018-03-12 stsp err(1, "pledge");
4098 6715a751 2018-03-16 stsp #endif
4099 79109fed 2018-03-27 stsp
4100 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
4101 b1ebc001 2019-08-13 stsp
4102 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
4103 79109fed 2018-03-27 stsp switch (ch) {
4104 79109fed 2018-03-27 stsp case 'p':
4105 79109fed 2018-03-27 stsp show_patch = 1;
4106 d142fc45 2018-04-01 stsp break;
4107 0208f208 2020-05-05 stsp case 'P':
4108 0208f208 2020-05-05 stsp show_changed_paths = 1;
4109 0208f208 2020-05-05 stsp break;
4110 d142fc45 2018-04-01 stsp case 'c':
4111 d142fc45 2018-04-01 stsp start_commit = optarg;
4112 64a96a6d 2018-04-01 stsp break;
4113 c0cc5c62 2018-10-18 stsp case 'C':
4114 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4115 4a8520aa 2018-10-18 stsp &errstr);
4116 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4117 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4118 5a20d08d 2022-02-09 op errstr, optarg);
4119 c0cc5c62 2018-10-18 stsp break;
4120 64a96a6d 2018-04-01 stsp case 'l':
4121 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
4122 64a96a6d 2018-04-01 stsp if (errstr != NULL)
4123 5a20d08d 2022-02-09 op errx(1, "number of commits is %s: %s",
4124 5a20d08d 2022-02-09 op errstr, optarg);
4125 cc54c501 2019-07-15 stsp break;
4126 48c8c60d 2020-01-27 stsp case 'b':
4127 48c8c60d 2020-01-27 stsp log_branches = 1;
4128 a0603db2 2018-06-10 stsp break;
4129 04ca23f4 2018-07-16 stsp case 'r':
4130 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4131 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
4132 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4133 9ba1d308 2019-10-21 stsp optarg);
4134 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4135 04ca23f4 2018-07-16 stsp break;
4136 dbec59df 2020-04-18 stsp case 'R':
4137 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4138 dbec59df 2020-04-18 stsp break;
4139 6841bf13 2019-11-29 kn case 's':
4140 6841bf13 2019-11-29 kn search_pattern = optarg;
4141 6841bf13 2019-11-29 kn break;
4142 d1fe46f9 2020-04-18 stsp case 'x':
4143 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4144 d1fe46f9 2020-04-18 stsp break;
4145 79109fed 2018-03-27 stsp default:
4146 2deda0b9 2019-03-07 stsp usage_log();
4147 79109fed 2018-03-27 stsp /* NOTREACHED */
4148 79109fed 2018-03-27 stsp }
4149 79109fed 2018-03-27 stsp }
4150 79109fed 2018-03-27 stsp
4151 79109fed 2018-03-27 stsp argc -= optind;
4152 79109fed 2018-03-27 stsp argv += optind;
4153 f42b1b34 2018-03-12 stsp
4154 dc1edbfa 2019-11-29 kn if (diff_context == -1)
4155 dc1edbfa 2019-11-29 kn diff_context = 3;
4156 dc1edbfa 2019-11-29 kn else if (!show_patch)
4157 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
4158 dc1edbfa 2019-11-29 kn
4159 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
4160 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
4161 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4162 04ca23f4 2018-07-16 stsp goto done;
4163 04ca23f4 2018-07-16 stsp }
4164 cffc0aa4 2019-02-05 stsp
4165 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
4166 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
4167 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4168 50f2fada 2020-04-24 stsp goto done;
4169 50f2fada 2020-04-24 stsp error = NULL;
4170 50f2fada 2020-04-24 stsp }
4171 cffc0aa4 2019-02-05 stsp
4172 603cdeb0 2020-10-22 stsp if (argc == 1) {
4173 e7301579 2019-03-18 stsp if (worktree) {
4174 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4175 e7301579 2019-03-18 stsp argv[0]);
4176 e7301579 2019-03-18 stsp if (error)
4177 e7301579 2019-03-18 stsp goto done;
4178 e7301579 2019-03-18 stsp } else {
4179 e7301579 2019-03-18 stsp path = strdup(argv[0]);
4180 e7301579 2019-03-18 stsp if (path == NULL) {
4181 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4182 e7301579 2019-03-18 stsp goto done;
4183 e7301579 2019-03-18 stsp }
4184 e7301579 2019-03-18 stsp }
4185 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4186 cbd1af7a 2019-03-18 stsp usage_log();
4187 cbd1af7a 2019-03-18 stsp
4188 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
4189 5486daa2 2019-05-11 stsp repo_path = worktree ?
4190 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4191 5486daa2 2019-05-11 stsp }
4192 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
4193 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4194 cffc0aa4 2019-02-05 stsp goto done;
4195 04ca23f4 2018-07-16 stsp }
4196 6098196c 2019-01-04 stsp
4197 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4198 d7d4f210 2018-03-12 stsp if (error != NULL)
4199 04ca23f4 2018-07-16 stsp goto done;
4200 f42b1b34 2018-03-12 stsp
4201 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4202 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4203 c02c541e 2019-03-29 stsp if (error)
4204 c02c541e 2019-03-29 stsp goto done;
4205 c02c541e 2019-03-29 stsp
4206 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4207 84de9106 2020-12-26 stsp if (error)
4208 84de9106 2020-12-26 stsp goto done;
4209 84de9106 2020-12-26 stsp
4210 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4211 888b7d99 2020-12-26 stsp if (error)
4212 888b7d99 2020-12-26 stsp goto done;
4213 888b7d99 2020-12-26 stsp
4214 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4215 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4216 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4217 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4218 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4219 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4220 3235492e 2018-04-01 stsp if (error != NULL)
4221 d1fe46f9 2020-04-18 stsp goto done;
4222 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4223 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4224 3235492e 2018-04-01 stsp if (error != NULL)
4225 d1fe46f9 2020-04-18 stsp goto done;
4226 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4227 d1fe46f9 2020-04-18 stsp start_id);
4228 d1fe46f9 2020-04-18 stsp if (error != NULL)
4229 d1fe46f9 2020-04-18 stsp goto done;
4230 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4231 3235492e 2018-04-01 stsp } else {
4232 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4233 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4234 d1fe46f9 2020-04-18 stsp if (error != NULL)
4235 d1fe46f9 2020-04-18 stsp goto done;
4236 3235492e 2018-04-01 stsp }
4237 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4238 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4239 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4240 d1fe46f9 2020-04-18 stsp if (error != NULL)
4241 d1fe46f9 2020-04-18 stsp goto done;
4242 d1fe46f9 2020-04-18 stsp }
4243 04ca23f4 2018-07-16 stsp
4244 deeabeae 2019-08-27 stsp if (worktree) {
4245 603cdeb0 2020-10-22 stsp /*
4246 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4247 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4248 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4249 603cdeb0 2020-10-22 stsp */
4250 603cdeb0 2020-10-22 stsp if (path) {
4251 603cdeb0 2020-10-22 stsp const char *prefix;
4252 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4253 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4254 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4255 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4256 603cdeb0 2020-10-22 stsp goto done;
4257 603cdeb0 2020-10-22 stsp }
4258 603cdeb0 2020-10-22 stsp }
4259 deeabeae 2019-08-27 stsp } else
4260 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4261 8fa913ec 2020-11-14 stsp path ? path : "");
4262 04ca23f4 2018-07-16 stsp if (error != NULL)
4263 04ca23f4 2018-07-16 stsp goto done;
4264 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4265 04ca23f4 2018-07-16 stsp free(path);
4266 04ca23f4 2018-07-16 stsp path = in_repo_path;
4267 04ca23f4 2018-07-16 stsp }
4268 199a4027 2019-02-02 stsp
4269 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4270 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4271 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4272 04ca23f4 2018-07-16 stsp done:
4273 04ca23f4 2018-07-16 stsp free(path);
4274 04ca23f4 2018-07-16 stsp free(repo_path);
4275 04ca23f4 2018-07-16 stsp free(cwd);
4276 cffc0aa4 2019-02-05 stsp if (worktree)
4277 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4278 ad242220 2018-09-08 stsp if (repo) {
4279 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4280 ad242220 2018-09-08 stsp if (error == NULL)
4281 1d0f4054 2021-06-17 stsp error = close_err;
4282 ad242220 2018-09-08 stsp }
4283 888b7d99 2020-12-26 stsp if (refs_idmap)
4284 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4285 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4286 8bf5b3c9 2018-03-17 stsp return error;
4287 5c860e29 2018-03-12 stsp }
4288 5c860e29 2018-03-12 stsp
4289 4ed7e80c 2018-05-20 stsp __dead static void
4290 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4291 3f8b7d6a 2018-04-01 stsp {
4292 51ed7429 2021-10-10 stsp fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4293 51ed7429 2021-10-10 stsp "[-r repository-path] [-s] [-w] [-P] "
4294 51ed7429 2021-10-10 stsp "[object1 object2 | path ...]\n", getprogname());
4295 3f8b7d6a 2018-04-01 stsp exit(1);
4296 b00d56cd 2018-04-01 stsp }
4297 b00d56cd 2018-04-01 stsp
4298 b72f483a 2019-02-05 stsp struct print_diff_arg {
4299 b72f483a 2019-02-05 stsp struct got_repository *repo;
4300 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4301 b72f483a 2019-02-05 stsp int diff_context;
4302 3fc0c068 2019-02-10 stsp const char *id_str;
4303 3fc0c068 2019-02-10 stsp int header_shown;
4304 98eaaa12 2019-08-03 stsp int diff_staged;
4305 63035f9f 2019-10-06 stsp int ignore_whitespace;
4306 64453f7e 2020-11-21 stsp int force_text_diff;
4307 b72f483a 2019-02-05 stsp };
4308 39449a05 2020-07-23 stsp
4309 39449a05 2020-07-23 stsp /*
4310 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4311 39449a05 2020-07-23 stsp * it as content to the diff engine.
4312 39449a05 2020-07-23 stsp */
4313 39449a05 2020-07-23 stsp static const struct got_error *
4314 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4315 39449a05 2020-07-23 stsp const char *abspath)
4316 39449a05 2020-07-23 stsp {
4317 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4318 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4319 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4320 39449a05 2020-07-23 stsp
4321 39449a05 2020-07-23 stsp *fd = -1;
4322 39449a05 2020-07-23 stsp
4323 39449a05 2020-07-23 stsp if (dirfd != -1) {
4324 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4325 39449a05 2020-07-23 stsp if (target_len == -1)
4326 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4327 39449a05 2020-07-23 stsp } else {
4328 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4329 39449a05 2020-07-23 stsp if (target_len == -1)
4330 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4331 39449a05 2020-07-23 stsp }
4332 39449a05 2020-07-23 stsp
4333 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4334 39449a05 2020-07-23 stsp if (*fd == -1)
4335 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4336 39449a05 2020-07-23 stsp
4337 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4338 39449a05 2020-07-23 stsp if (outlen == -1) {
4339 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4340 39449a05 2020-07-23 stsp goto done;
4341 39449a05 2020-07-23 stsp }
4342 39449a05 2020-07-23 stsp
4343 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4344 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4345 39449a05 2020-07-23 stsp goto done;
4346 39449a05 2020-07-23 stsp }
4347 39449a05 2020-07-23 stsp done:
4348 39449a05 2020-07-23 stsp if (err) {
4349 39449a05 2020-07-23 stsp close(*fd);
4350 39449a05 2020-07-23 stsp *fd = -1;
4351 39449a05 2020-07-23 stsp }
4352 39449a05 2020-07-23 stsp return err;
4353 39449a05 2020-07-23 stsp }
4354 b72f483a 2019-02-05 stsp
4355 4ed7e80c 2018-05-20 stsp static const struct got_error *
4356 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4357 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4358 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4359 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4360 b72f483a 2019-02-05 stsp {
4361 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4362 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4363 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4364 12463d8b 2019-12-13 stsp int fd = -1;
4365 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4366 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4367 b72f483a 2019-02-05 stsp struct stat sb;
4368 b72f483a 2019-02-05 stsp
4369 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4370 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4371 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4372 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4373 98eaaa12 2019-08-03 stsp return NULL;
4374 98eaaa12 2019-08-03 stsp } else {
4375 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4376 98eaaa12 2019-08-03 stsp return NULL;
4377 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4378 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4379 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4380 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4381 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4382 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4383 98eaaa12 2019-08-03 stsp return NULL;
4384 98eaaa12 2019-08-03 stsp }
4385 3fc0c068 2019-02-10 stsp
4386 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4387 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4388 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4389 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4390 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4391 3fc0c068 2019-02-10 stsp }
4392 b72f483a 2019-02-05 stsp
4393 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4394 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4395 98eaaa12 2019-08-03 stsp switch (staged_status) {
4396 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4397 98eaaa12 2019-08-03 stsp label1 = path;
4398 98eaaa12 2019-08-03 stsp label2 = path;
4399 98eaaa12 2019-08-03 stsp break;
4400 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4401 98eaaa12 2019-08-03 stsp label2 = path;
4402 98eaaa12 2019-08-03 stsp break;
4403 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4404 98eaaa12 2019-08-03 stsp label1 = path;
4405 98eaaa12 2019-08-03 stsp break;
4406 98eaaa12 2019-08-03 stsp default:
4407 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4408 98eaaa12 2019-08-03 stsp }
4409 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4410 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4411 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4412 98eaaa12 2019-08-03 stsp }
4413 98eaaa12 2019-08-03 stsp
4414 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4415 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4416 4ce46740 2019-08-08 stsp char *id_str;
4417 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4418 408b4ebc 2019-08-03 stsp 8192);
4419 4ce46740 2019-08-08 stsp if (err)
4420 4ce46740 2019-08-08 stsp goto done;
4421 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4422 4ce46740 2019-08-08 stsp if (err)
4423 4ce46740 2019-08-08 stsp goto done;
4424 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4425 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4426 4ce46740 2019-08-08 stsp free(id_str);
4427 4ce46740 2019-08-08 stsp goto done;
4428 4ce46740 2019-08-08 stsp }
4429 4ce46740 2019-08-08 stsp free(id_str);
4430 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4431 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4432 4ce46740 2019-08-08 stsp if (err)
4433 4ce46740 2019-08-08 stsp goto done;
4434 4ce46740 2019-08-08 stsp }
4435 d00136be 2019-03-26 stsp
4436 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4437 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4438 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4439 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4440 2ec1f75b 2019-03-26 stsp goto done;
4441 2ec1f75b 2019-03-26 stsp }
4442 b72f483a 2019-02-05 stsp
4443 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4444 e7ae0baf 2021-12-31 stsp fd = openat(dirfd, de_name,
4445 e7ae0baf 2021-12-31 stsp O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4446 12463d8b 2019-12-13 stsp if (fd == -1) {
4447 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4448 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4449 39449a05 2020-07-23 stsp abspath);
4450 39449a05 2020-07-23 stsp goto done;
4451 39449a05 2020-07-23 stsp }
4452 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4453 39449a05 2020-07-23 stsp de_name, abspath);
4454 39449a05 2020-07-23 stsp if (err)
4455 39449a05 2020-07-23 stsp goto done;
4456 12463d8b 2019-12-13 stsp }
4457 12463d8b 2019-12-13 stsp } else {
4458 8bd0cdad 2021-12-31 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4459 12463d8b 2019-12-13 stsp if (fd == -1) {
4460 5c02d2a5 2021-09-26 stsp if (!got_err_open_nofollow_on_symlink()) {
4461 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4462 39449a05 2020-07-23 stsp abspath);
4463 39449a05 2020-07-23 stsp goto done;
4464 39449a05 2020-07-23 stsp }
4465 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4466 39449a05 2020-07-23 stsp de_name, abspath);
4467 39449a05 2020-07-23 stsp if (err)
4468 39449a05 2020-07-23 stsp goto done;
4469 12463d8b 2019-12-13 stsp }
4470 12463d8b 2019-12-13 stsp }
4471 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4472 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4473 2ec1f75b 2019-03-26 stsp goto done;
4474 2ec1f75b 2019-03-26 stsp }
4475 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4476 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4477 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4478 2ec1f75b 2019-03-26 stsp goto done;
4479 2ec1f75b 2019-03-26 stsp }
4480 12463d8b 2019-12-13 stsp fd = -1;
4481 2ec1f75b 2019-03-26 stsp } else
4482 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4483 b72f483a 2019-02-05 stsp
4484 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4485 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4486 b72f483a 2019-02-05 stsp done:
4487 b72f483a 2019-02-05 stsp if (blob1)
4488 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4489 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4490 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4491 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4492 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4493 b72f483a 2019-02-05 stsp free(abspath);
4494 927df6b7 2019-02-10 stsp return err;
4495 927df6b7 2019-02-10 stsp }
4496 927df6b7 2019-02-10 stsp
4497 927df6b7 2019-02-10 stsp static const struct got_error *
4498 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4499 b00d56cd 2018-04-01 stsp {
4500 b00d56cd 2018-04-01 stsp const struct got_error *error;
4501 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4502 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4503 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4504 67b631c9 2021-10-10 stsp const char *commit_args[2] = { NULL, NULL };
4505 67b631c9 2021-10-10 stsp int ncommit_args = 0;
4506 e7ffb0b0 2021-10-07 stsp struct got_object_id *ids[2] = { NULL, NULL };
4507 e7ffb0b0 2021-10-07 stsp char *labels[2] = { NULL, NULL };
4508 67b631c9 2021-10-10 stsp int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4509 e7ffb0b0 2021-10-07 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4510 e7ffb0b0 2021-10-07 stsp int force_text_diff = 0, force_path = 0, rflag = 0;
4511 c0cc5c62 2018-10-18 stsp const char *errstr;
4512 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4513 e7ffb0b0 2021-10-07 stsp struct got_pathlist_head paths;
4514 e7ffb0b0 2021-10-07 stsp struct got_pathlist_entry *pe;
4515 b00d56cd 2018-04-01 stsp
4516 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4517 e7ffb0b0 2021-10-07 stsp TAILQ_INIT(&paths);
4518 84de9106 2020-12-26 stsp
4519 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4520 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4521 25eccc22 2019-01-04 stsp NULL) == -1)
4522 b00d56cd 2018-04-01 stsp err(1, "pledge");
4523 b00d56cd 2018-04-01 stsp #endif
4524 b00d56cd 2018-04-01 stsp
4525 67b631c9 2021-10-10 stsp while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4526 b00d56cd 2018-04-01 stsp switch (ch) {
4527 64453f7e 2020-11-21 stsp case 'a':
4528 64453f7e 2020-11-21 stsp force_text_diff = 1;
4529 64453f7e 2020-11-21 stsp break;
4530 67b631c9 2021-10-10 stsp case 'c':
4531 67b631c9 2021-10-10 stsp if (ncommit_args >= 2)
4532 67b631c9 2021-10-10 stsp errx(1, "too many -c options used");
4533 67b631c9 2021-10-10 stsp commit_args[ncommit_args++] = optarg;
4534 67b631c9 2021-10-10 stsp break;
4535 c0cc5c62 2018-10-18 stsp case 'C':
4536 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4537 dfcab68b 2019-11-29 kn &errstr);
4538 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4539 5a20d08d 2022-02-09 op errx(1, "number of context lines is %s: %s",
4540 5a20d08d 2022-02-09 op errstr, optarg);
4541 c0cc5c62 2018-10-18 stsp break;
4542 b72f483a 2019-02-05 stsp case 'r':
4543 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4544 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4545 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4546 9ba1d308 2019-10-21 stsp optarg);
4547 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4548 e7ffb0b0 2021-10-07 stsp rflag = 1;
4549 b72f483a 2019-02-05 stsp break;
4550 98eaaa12 2019-08-03 stsp case 's':
4551 98eaaa12 2019-08-03 stsp diff_staged = 1;
4552 63035f9f 2019-10-06 stsp break;
4553 63035f9f 2019-10-06 stsp case 'w':
4554 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4555 98eaaa12 2019-08-03 stsp break;
4556 e7ffb0b0 2021-10-07 stsp case 'P':
4557 e7ffb0b0 2021-10-07 stsp force_path = 1;
4558 e7ffb0b0 2021-10-07 stsp break;
4559 b00d56cd 2018-04-01 stsp default:
4560 2deda0b9 2019-03-07 stsp usage_diff();
4561 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4562 b00d56cd 2018-04-01 stsp }
4563 b00d56cd 2018-04-01 stsp }
4564 b00d56cd 2018-04-01 stsp
4565 b00d56cd 2018-04-01 stsp argc -= optind;
4566 b00d56cd 2018-04-01 stsp argv += optind;
4567 b00d56cd 2018-04-01 stsp
4568 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4569 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4570 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4571 b72f483a 2019-02-05 stsp goto done;
4572 b72f483a 2019-02-05 stsp }
4573 e7ffb0b0 2021-10-07 stsp
4574 e7ffb0b0 2021-10-07 stsp if (repo_path == NULL) {
4575 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4576 e7ffb0b0 2021-10-07 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4577 1f03b8da 2020-03-20 stsp goto done;
4578 e7ffb0b0 2021-10-07 stsp else
4579 e7ffb0b0 2021-10-07 stsp error = NULL;
4580 e7ffb0b0 2021-10-07 stsp if (worktree) {
4581 e7ffb0b0 2021-10-07 stsp repo_path =
4582 e7ffb0b0 2021-10-07 stsp strdup(got_worktree_get_repo_path(worktree));
4583 e7ffb0b0 2021-10-07 stsp if (repo_path == NULL) {
4584 e7ffb0b0 2021-10-07 stsp error = got_error_from_errno("strdup");
4585 927df6b7 2019-02-10 stsp goto done;
4586 e7ffb0b0 2021-10-07 stsp }
4587 927df6b7 2019-02-10 stsp } else {
4588 e7ffb0b0 2021-10-07 stsp repo_path = strdup(cwd);
4589 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4590 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4591 1a1242a9 2021-04-01 kn goto done;
4592 30db809c 2019-06-05 stsp }
4593 30db809c 2019-06-05 stsp }
4594 e7ffb0b0 2021-10-07 stsp }
4595 b00d56cd 2018-04-01 stsp
4596 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4597 76089277 2018-04-01 stsp free(repo_path);
4598 b00d56cd 2018-04-01 stsp if (error != NULL)
4599 b00d56cd 2018-04-01 stsp goto done;
4600 67b631c9 2021-10-10 stsp
4601 67b631c9 2021-10-10 stsp if (rflag || worktree == NULL || ncommit_args > 0) {
4602 67b631c9 2021-10-10 stsp if (force_path) {
4603 67b631c9 2021-10-10 stsp error = got_error_msg(GOT_ERR_NOT_IMPL,
4604 67b631c9 2021-10-10 stsp "-P option can only be used when diffing "
4605 67b631c9 2021-10-10 stsp "a work tree");
4606 67b631c9 2021-10-10 stsp goto done;
4607 67b631c9 2021-10-10 stsp }
4608 67b631c9 2021-10-10 stsp if (diff_staged) {
4609 67b631c9 2021-10-10 stsp error = got_error_msg(GOT_ERR_NOT_IMPL,
4610 67b631c9 2021-10-10 stsp "-s option can only be used when diffing "
4611 67b631c9 2021-10-10 stsp "a work tree");
4612 67b631c9 2021-10-10 stsp goto done;
4613 67b631c9 2021-10-10 stsp }
4614 67b631c9 2021-10-10 stsp }
4615 b00d56cd 2018-04-01 stsp
4616 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4617 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4618 c02c541e 2019-03-29 stsp if (error)
4619 c02c541e 2019-03-29 stsp goto done;
4620 c02c541e 2019-03-29 stsp
4621 67b631c9 2021-10-10 stsp if ((!force_path && argc == 2) || ncommit_args > 0) {
4622 67b631c9 2021-10-10 stsp int obj_type = (ncommit_args > 0 ?
4623 67b631c9 2021-10-10 stsp GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4624 e7ffb0b0 2021-10-07 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4625 e7ffb0b0 2021-10-07 stsp NULL);
4626 e7ffb0b0 2021-10-07 stsp if (error)
4627 e7ffb0b0 2021-10-07 stsp goto done;
4628 67b631c9 2021-10-10 stsp for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4629 67b631c9 2021-10-10 stsp const char *arg;
4630 67b631c9 2021-10-10 stsp if (ncommit_args > 0)
4631 67b631c9 2021-10-10 stsp arg = commit_args[i];
4632 67b631c9 2021-10-10 stsp else
4633 67b631c9 2021-10-10 stsp arg = argv[i];
4634 e7ffb0b0 2021-10-07 stsp error = got_repo_match_object_id(&ids[i], &labels[i],
4635 67b631c9 2021-10-10 stsp arg, obj_type, &refs, repo);
4636 e7ffb0b0 2021-10-07 stsp if (error) {
4637 e7ffb0b0 2021-10-07 stsp if (error->code != GOT_ERR_NOT_REF &&
4638 e7ffb0b0 2021-10-07 stsp error->code != GOT_ERR_NO_OBJ)
4639 e7ffb0b0 2021-10-07 stsp goto done;
4640 67b631c9 2021-10-10 stsp if (ncommit_args > 0)
4641 67b631c9 2021-10-10 stsp goto done;
4642 e7ffb0b0 2021-10-07 stsp error = NULL;
4643 e7ffb0b0 2021-10-07 stsp break;
4644 e7ffb0b0 2021-10-07 stsp }
4645 e7ffb0b0 2021-10-07 stsp }
4646 e7ffb0b0 2021-10-07 stsp }
4647 e7ffb0b0 2021-10-07 stsp
4648 67b631c9 2021-10-10 stsp if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4649 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4650 b72f483a 2019-02-05 stsp char *id_str;
4651 72ea6654 2019-07-27 stsp
4652 67b631c9 2021-10-10 stsp if (worktree == NULL) {
4653 67b631c9 2021-10-10 stsp if (argc == 2 && ids[0] == NULL) {
4654 67b631c9 2021-10-10 stsp error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4655 67b631c9 2021-10-10 stsp goto done;
4656 67b631c9 2021-10-10 stsp } else if (argc == 2 && ids[1] == NULL) {
4657 67b631c9 2021-10-10 stsp error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4658 67b631c9 2021-10-10 stsp goto done;
4659 67b631c9 2021-10-10 stsp } else if (argc > 0) {
4660 67b631c9 2021-10-10 stsp error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4661 67b631c9 2021-10-10 stsp "%s", "specified paths cannot be resolved");
4662 67b631c9 2021-10-10 stsp goto done;
4663 67b631c9 2021-10-10 stsp } else {
4664 67b631c9 2021-10-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
4665 67b631c9 2021-10-10 stsp goto done;
4666 67b631c9 2021-10-10 stsp }
4667 67b631c9 2021-10-10 stsp }
4668 67b631c9 2021-10-10 stsp
4669 67b631c9 2021-10-10 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
4670 67b631c9 2021-10-10 stsp worktree);
4671 67b631c9 2021-10-10 stsp if (error)
4672 67b631c9 2021-10-10 stsp goto done;
4673 67b631c9 2021-10-10 stsp
4674 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4675 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4676 b72f483a 2019-02-05 stsp if (error)
4677 b72f483a 2019-02-05 stsp goto done;
4678 b72f483a 2019-02-05 stsp arg.repo = repo;
4679 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4680 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4681 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4682 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4683 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4684 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4685 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4686 b72f483a 2019-02-05 stsp
4687 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4688 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4689 b72f483a 2019-02-05 stsp free(id_str);
4690 b72f483a 2019-02-05 stsp goto done;
4691 b72f483a 2019-02-05 stsp }
4692 84de9106 2020-12-26 stsp
4693 67b631c9 2021-10-10 stsp if (ncommit_args == 1) {
4694 67b631c9 2021-10-10 stsp struct got_commit_object *commit;
4695 67b631c9 2021-10-10 stsp error = got_object_open_as_commit(&commit, repo, ids[0]);
4696 67b631c9 2021-10-10 stsp if (error)
4697 e7ffb0b0 2021-10-07 stsp goto done;
4698 b00d56cd 2018-04-01 stsp
4699 67b631c9 2021-10-10 stsp labels[1] = labels[0];
4700 67b631c9 2021-10-10 stsp ids[1] = ids[0];
4701 67b631c9 2021-10-10 stsp if (got_object_commit_get_nparents(commit) > 0) {
4702 67b631c9 2021-10-10 stsp const struct got_object_id_queue *pids;
4703 67b631c9 2021-10-10 stsp struct got_object_qid *pid;
4704 67b631c9 2021-10-10 stsp pids = got_object_commit_get_parent_ids(commit);
4705 67b631c9 2021-10-10 stsp pid = STAILQ_FIRST(pids);
4706 67b631c9 2021-10-10 stsp ids[0] = got_object_id_dup(pid->id);
4707 67b631c9 2021-10-10 stsp if (ids[0] == NULL) {
4708 67b631c9 2021-10-10 stsp error = got_error_from_errno(
4709 67b631c9 2021-10-10 stsp "got_object_id_dup");
4710 67b631c9 2021-10-10 stsp got_object_commit_close(commit);
4711 67b631c9 2021-10-10 stsp goto done;
4712 67b631c9 2021-10-10 stsp }
4713 67b631c9 2021-10-10 stsp error = got_object_id_str(&labels[0], ids[0]);
4714 67b631c9 2021-10-10 stsp if (error) {
4715 67b631c9 2021-10-10 stsp got_object_commit_close(commit);
4716 67b631c9 2021-10-10 stsp goto done;
4717 67b631c9 2021-10-10 stsp }
4718 67b631c9 2021-10-10 stsp } else {
4719 67b631c9 2021-10-10 stsp ids[0] = NULL;
4720 67b631c9 2021-10-10 stsp labels[0] = strdup("/dev/null");
4721 67b631c9 2021-10-10 stsp if (labels[0] == NULL) {
4722 67b631c9 2021-10-10 stsp error = got_error_from_errno("strdup");
4723 67b631c9 2021-10-10 stsp got_object_commit_close(commit);
4724 67b631c9 2021-10-10 stsp goto done;
4725 67b631c9 2021-10-10 stsp }
4726 67b631c9 2021-10-10 stsp }
4727 67b631c9 2021-10-10 stsp
4728 67b631c9 2021-10-10 stsp got_object_commit_close(commit);
4729 67b631c9 2021-10-10 stsp }
4730 67b631c9 2021-10-10 stsp
4731 67b631c9 2021-10-10 stsp if (ncommit_args == 0 && argc > 2) {
4732 67b631c9 2021-10-10 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4733 67b631c9 2021-10-10 stsp "path arguments cannot be used when diffing two objects");
4734 0adb7196 2019-08-11 stsp goto done;
4735 67b631c9 2021-10-10 stsp }
4736 b00d56cd 2018-04-01 stsp
4737 67b631c9 2021-10-10 stsp if (ids[0]) {
4738 67b631c9 2021-10-10 stsp error = got_object_get_type(&type1, repo, ids[0]);
4739 67b631c9 2021-10-10 stsp if (error)
4740 67b631c9 2021-10-10 stsp goto done;
4741 67b631c9 2021-10-10 stsp }
4742 67b631c9 2021-10-10 stsp
4743 e7ffb0b0 2021-10-07 stsp error = got_object_get_type(&type2, repo, ids[1]);
4744 15a94983 2018-12-23 stsp if (error)
4745 15a94983 2018-12-23 stsp goto done;
4746 67b631c9 2021-10-10 stsp if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
4747 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4748 b00d56cd 2018-04-01 stsp goto done;
4749 b00d56cd 2018-04-01 stsp }
4750 67b631c9 2021-10-10 stsp if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
4751 67b631c9 2021-10-10 stsp error = got_error_msg(GOT_ERR_OBJ_TYPE,
4752 67b631c9 2021-10-10 stsp "path arguments cannot be used when diffing blobs");
4753 67b631c9 2021-10-10 stsp goto done;
4754 67b631c9 2021-10-10 stsp }
4755 b00d56cd 2018-04-01 stsp
4756 67b631c9 2021-10-10 stsp for (i = 0; ncommit_args > 0 && i < argc; i++) {
4757 67b631c9 2021-10-10 stsp char *in_repo_path;
4758 67b631c9 2021-10-10 stsp struct got_pathlist_entry *new;
4759 67b631c9 2021-10-10 stsp if (worktree) {
4760 67b631c9 2021-10-10 stsp const char *prefix;
4761 67b631c9 2021-10-10 stsp char *p;
4762 67b631c9 2021-10-10 stsp error = got_worktree_resolve_path(&p, worktree,
4763 67b631c9 2021-10-10 stsp argv[i]);
4764 67b631c9 2021-10-10 stsp if (error)
4765 67b631c9 2021-10-10 stsp goto done;
4766 67b631c9 2021-10-10 stsp prefix = got_worktree_get_path_prefix(worktree);
4767 67b631c9 2021-10-10 stsp while (prefix[0] == '/')
4768 67b631c9 2021-10-10 stsp prefix++;
4769 67b631c9 2021-10-10 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4770 67b631c9 2021-10-10 stsp (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
4771 67b631c9 2021-10-10 stsp p) == -1) {
4772 67b631c9 2021-10-10 stsp error = got_error_from_errno("asprintf");
4773 67b631c9 2021-10-10 stsp free(p);
4774 67b631c9 2021-10-10 stsp goto done;
4775 67b631c9 2021-10-10 stsp }
4776 67b631c9 2021-10-10 stsp free(p);
4777 67b631c9 2021-10-10 stsp } else {
4778 67b631c9 2021-10-10 stsp char *mapped_path, *s;
4779 67b631c9 2021-10-10 stsp error = got_repo_map_path(&mapped_path, repo, argv[i]);
4780 67b631c9 2021-10-10 stsp if (error)
4781 67b631c9 2021-10-10 stsp goto done;
4782 67b631c9 2021-10-10 stsp s = mapped_path;
4783 67b631c9 2021-10-10 stsp while (s[0] == '/')
4784 67b631c9 2021-10-10 stsp s++;
4785 67b631c9 2021-10-10 stsp in_repo_path = strdup(s);
4786 67b631c9 2021-10-10 stsp if (in_repo_path == NULL) {
4787 67b631c9 2021-10-10 stsp error = got_error_from_errno("asprintf");
4788 67b631c9 2021-10-10 stsp free(mapped_path);
4789 67b631c9 2021-10-10 stsp goto done;
4790 67b631c9 2021-10-10 stsp }
4791 67b631c9 2021-10-10 stsp free(mapped_path);
4792 67b631c9 2021-10-10 stsp
4793 67b631c9 2021-10-10 stsp }
4794 67b631c9 2021-10-10 stsp error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
4795 67b631c9 2021-10-10 stsp if (error || new == NULL /* duplicate */)
4796 67b631c9 2021-10-10 stsp free(in_repo_path);
4797 67b631c9 2021-10-10 stsp if (error)
4798 67b631c9 2021-10-10 stsp goto done;
4799 67b631c9 2021-10-10 stsp }
4800 67b631c9 2021-10-10 stsp
4801 67b631c9 2021-10-10 stsp switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
4802 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4803 e7ffb0b0 2021-10-07 stsp error = got_diff_objects_as_blobs(NULL, NULL, ids[0], ids[1],
4804 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4805 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4806 b00d56cd 2018-04-01 stsp break;
4807 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4808 e7ffb0b0 2021-10-07 stsp error = got_diff_objects_as_trees(NULL, NULL, ids[0], ids[1],
4809 67b631c9 2021-10-10 stsp &paths, "", "", diff_context, ignore_whitespace,
4810 67b631c9 2021-10-10 stsp force_text_diff, repo, stdout);
4811 b00d56cd 2018-04-01 stsp break;
4812 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4813 e7ffb0b0 2021-10-07 stsp printf("diff %s %s\n", labels[0], labels[1]);
4814 e7ffb0b0 2021-10-07 stsp error = got_diff_objects_as_commits(NULL, NULL, ids[0], ids[1],
4815 67b631c9 2021-10-10 stsp &paths, diff_context, ignore_whitespace, force_text_diff,
4816 67b631c9 2021-10-10 stsp repo, stdout);
4817 b00d56cd 2018-04-01 stsp break;
4818 b00d56cd 2018-04-01 stsp default:
4819 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4820 b00d56cd 2018-04-01 stsp }
4821 b00d56cd 2018-04-01 stsp done:
4822 e7ffb0b0 2021-10-07 stsp free(labels[0]);
4823 e7ffb0b0 2021-10-07 stsp free(labels[1]);
4824 e7ffb0b0 2021-10-07 stsp free(ids[0]);
4825 e7ffb0b0 2021-10-07 stsp free(ids[1]);
4826 b72f483a 2019-02-05 stsp if (worktree)
4827 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4828 ad242220 2018-09-08 stsp if (repo) {
4829 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4830 ad242220 2018-09-08 stsp if (error == NULL)
4831 1d0f4054 2021-06-17 stsp error = close_err;
4832 ad242220 2018-09-08 stsp }
4833 e7ffb0b0 2021-10-07 stsp TAILQ_FOREACH(pe, &paths, entry)
4834 e7ffb0b0 2021-10-07 stsp free((char *)pe->path);
4835 e7ffb0b0 2021-10-07 stsp got_pathlist_free(&paths);
4836 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4837 b00d56cd 2018-04-01 stsp return error;
4838 404c43c4 2018-06-21 stsp }
4839 404c43c4 2018-06-21 stsp
4840 404c43c4 2018-06-21 stsp __dead static void
4841 404c43c4 2018-06-21 stsp usage_blame(void)
4842 404c43c4 2018-06-21 stsp {
4843 9270e621 2019-02-05 stsp fprintf(stderr,
4844 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4845 404c43c4 2018-06-21 stsp getprogname());
4846 404c43c4 2018-06-21 stsp exit(1);
4847 b00d56cd 2018-04-01 stsp }
4848 b00d56cd 2018-04-01 stsp
4849 28315671 2019-08-14 stsp struct blame_line {
4850 28315671 2019-08-14 stsp int annotated;
4851 28315671 2019-08-14 stsp char *id_str;
4852 82f6abb8 2019-08-14 stsp char *committer;
4853 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4854 28315671 2019-08-14 stsp };
4855 28315671 2019-08-14 stsp
4856 28315671 2019-08-14 stsp struct blame_cb_args {
4857 28315671 2019-08-14 stsp struct blame_line *lines;
4858 28315671 2019-08-14 stsp int nlines;
4859 7ef28ff8 2019-08-14 stsp int nlines_prec;
4860 28315671 2019-08-14 stsp int lineno_cur;
4861 28315671 2019-08-14 stsp off_t *line_offsets;
4862 28315671 2019-08-14 stsp FILE *f;
4863 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4864 28315671 2019-08-14 stsp };
4865 28315671 2019-08-14 stsp
4866 404c43c4 2018-06-21 stsp static const struct got_error *
4867 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4868 28315671 2019-08-14 stsp {
4869 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4870 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4871 28315671 2019-08-14 stsp struct blame_line *bline;
4872 82f6abb8 2019-08-14 stsp char *line = NULL;
4873 28315671 2019-08-14 stsp size_t linesize = 0;
4874 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4875 28315671 2019-08-14 stsp off_t offset;
4876 bcb49d15 2019-08-14 stsp struct tm tm;
4877 bcb49d15 2019-08-14 stsp time_t committer_time;
4878 28315671 2019-08-14 stsp
4879 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4880 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4881 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4882 28315671 2019-08-14 stsp
4883 28315671 2019-08-14 stsp if (sigint_received)
4884 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4885 28315671 2019-08-14 stsp
4886 2839f8b9 2019-08-18 stsp if (lineno == -1)
4887 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4888 2839f8b9 2019-08-18 stsp
4889 28315671 2019-08-14 stsp /* Annotate this line. */
4890 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4891 28315671 2019-08-14 stsp if (bline->annotated)
4892 28315671 2019-08-14 stsp return NULL;
4893 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4894 28315671 2019-08-14 stsp if (err)
4895 28315671 2019-08-14 stsp return err;
4896 82f6abb8 2019-08-14 stsp
4897 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4898 82f6abb8 2019-08-14 stsp if (err)
4899 82f6abb8 2019-08-14 stsp goto done;
4900 82f6abb8 2019-08-14 stsp
4901 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4902 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4903 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4904 bcb49d15 2019-08-14 stsp goto done;
4905 bcb49d15 2019-08-14 stsp }
4906 bcb49d15 2019-08-14 stsp
4907 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4908 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
4909 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
4910 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4911 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4912 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4913 82f6abb8 2019-08-14 stsp goto done;
4914 82f6abb8 2019-08-14 stsp }
4915 28315671 2019-08-14 stsp bline->annotated = 1;
4916 28315671 2019-08-14 stsp
4917 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4918 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4919 28315671 2019-08-14 stsp if (!bline->annotated)
4920 82f6abb8 2019-08-14 stsp goto done;
4921 28315671 2019-08-14 stsp
4922 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4923 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4924 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4925 82f6abb8 2019-08-14 stsp goto done;
4926 82f6abb8 2019-08-14 stsp }
4927 28315671 2019-08-14 stsp
4928 28315671 2019-08-14 stsp while (bline->annotated) {
4929 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4930 82f6abb8 2019-08-14 stsp size_t len;
4931 82f6abb8 2019-08-14 stsp
4932 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4933 28315671 2019-08-14 stsp if (ferror(a->f))
4934 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4935 28315671 2019-08-14 stsp break;
4936 28315671 2019-08-14 stsp }
4937 82f6abb8 2019-08-14 stsp
4938 82f6abb8 2019-08-14 stsp committer = bline->committer;
4939 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4940 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4941 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4942 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4943 82f6abb8 2019-08-14 stsp if (at)
4944 82f6abb8 2019-08-14 stsp *at = '\0';
4945 82f6abb8 2019-08-14 stsp len = strlen(committer);
4946 82f6abb8 2019-08-14 stsp if (len >= 9)
4947 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4948 28315671 2019-08-14 stsp
4949 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4950 28315671 2019-08-14 stsp if (nl)
4951 28315671 2019-08-14 stsp *nl = '\0';
4952 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4953 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4954 28315671 2019-08-14 stsp
4955 28315671 2019-08-14 stsp a->lineno_cur++;
4956 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4957 28315671 2019-08-14 stsp }
4958 82f6abb8 2019-08-14 stsp done:
4959 82f6abb8 2019-08-14 stsp if (commit)
4960 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4961 28315671 2019-08-14 stsp free(line);
4962 28315671 2019-08-14 stsp return err;
4963 28315671 2019-08-14 stsp }
4964 28315671 2019-08-14 stsp
4965 28315671 2019-08-14 stsp static const struct got_error *
4966 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4967 404c43c4 2018-06-21 stsp {
4968 404c43c4 2018-06-21 stsp const struct got_error *error;
4969 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4970 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4971 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4972 0587e10c 2020-07-23 stsp char *link_target = NULL;
4973 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4974 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4975 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4976 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4977 28315671 2019-08-14 stsp struct blame_cb_args bca;
4978 28315671 2019-08-14 stsp int ch, obj_type, i;
4979 be659d10 2020-11-18 stsp off_t filesize;
4980 90f3c347 2019-08-19 stsp
4981 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4982 404c43c4 2018-06-21 stsp
4983 404c43c4 2018-06-21 stsp #ifndef PROFILE
4984 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4985 36e2fb66 2019-01-04 stsp NULL) == -1)
4986 404c43c4 2018-06-21 stsp err(1, "pledge");
4987 404c43c4 2018-06-21 stsp #endif
4988 404c43c4 2018-06-21 stsp
4989 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4990 404c43c4 2018-06-21 stsp switch (ch) {
4991 404c43c4 2018-06-21 stsp case 'c':
4992 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4993 404c43c4 2018-06-21 stsp break;
4994 66bea077 2018-08-02 stsp case 'r':
4995 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4996 66bea077 2018-08-02 stsp if (repo_path == NULL)
4997 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4998 9ba1d308 2019-10-21 stsp optarg);
4999 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5000 66bea077 2018-08-02 stsp break;
5001 404c43c4 2018-06-21 stsp default:
5002 2deda0b9 2019-03-07 stsp usage_blame();
5003 404c43c4 2018-06-21 stsp /* NOTREACHED */
5004 404c43c4 2018-06-21 stsp }
5005 404c43c4 2018-06-21 stsp }
5006 404c43c4 2018-06-21 stsp
5007 404c43c4 2018-06-21 stsp argc -= optind;
5008 404c43c4 2018-06-21 stsp argv += optind;
5009 404c43c4 2018-06-21 stsp
5010 a39318fd 2018-08-02 stsp if (argc == 1)
5011 404c43c4 2018-06-21 stsp path = argv[0];
5012 a39318fd 2018-08-02 stsp else
5013 404c43c4 2018-06-21 stsp usage_blame();
5014 404c43c4 2018-06-21 stsp
5015 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
5016 66bea077 2018-08-02 stsp if (cwd == NULL) {
5017 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5018 66bea077 2018-08-02 stsp goto done;
5019 66bea077 2018-08-02 stsp }
5020 66bea077 2018-08-02 stsp if (repo_path == NULL) {
5021 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5022 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5023 66bea077 2018-08-02 stsp goto done;
5024 0c06baac 2019-02-05 stsp else
5025 0c06baac 2019-02-05 stsp error = NULL;
5026 0c06baac 2019-02-05 stsp if (worktree) {
5027 0c06baac 2019-02-05 stsp repo_path =
5028 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5029 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
5030 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5031 1f03b8da 2020-03-20 stsp if (error)
5032 1f03b8da 2020-03-20 stsp goto done;
5033 1f03b8da 2020-03-20 stsp }
5034 0c06baac 2019-02-05 stsp } else {
5035 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
5036 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
5037 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5038 0c06baac 2019-02-05 stsp goto done;
5039 0c06baac 2019-02-05 stsp }
5040 66bea077 2018-08-02 stsp }
5041 66bea077 2018-08-02 stsp }
5042 36e2fb66 2019-01-04 stsp
5043 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5044 c02c541e 2019-03-29 stsp if (error != NULL)
5045 404c43c4 2018-06-21 stsp goto done;
5046 404c43c4 2018-06-21 stsp
5047 0c06baac 2019-02-05 stsp if (worktree) {
5048 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5049 01740607 2020-11-04 stsp char *p;
5050 01740607 2020-11-04 stsp
5051 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
5052 01740607 2020-11-04 stsp if (error)
5053 01740607 2020-11-04 stsp goto done;
5054 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5055 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5056 01740607 2020-11-04 stsp p) == -1) {
5057 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
5058 01740607 2020-11-04 stsp free(p);
5059 0c06baac 2019-02-05 stsp goto done;
5060 0c06baac 2019-02-05 stsp }
5061 0c06baac 2019-02-05 stsp free(p);
5062 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5063 0c06baac 2019-02-05 stsp } else {
5064 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5065 01740607 2020-11-04 stsp if (error)
5066 01740607 2020-11-04 stsp goto done;
5067 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5068 0c06baac 2019-02-05 stsp }
5069 0c06baac 2019-02-05 stsp if (error)
5070 66bea077 2018-08-02 stsp goto done;
5071 66bea077 2018-08-02 stsp
5072 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
5073 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
5074 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
5075 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5076 404c43c4 2018-06-21 stsp if (error != NULL)
5077 66bea077 2018-08-02 stsp goto done;
5078 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5079 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
5080 404c43c4 2018-06-21 stsp if (error != NULL)
5081 66bea077 2018-08-02 stsp goto done;
5082 404c43c4 2018-06-21 stsp } else {
5083 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5084 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5085 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5086 84de9106 2020-12-26 stsp NULL);
5087 84de9106 2020-12-26 stsp if (error)
5088 84de9106 2020-12-26 stsp goto done;
5089 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5090 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5091 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5092 30837e32 2019-07-25 stsp if (error)
5093 66bea077 2018-08-02 stsp goto done;
5094 404c43c4 2018-06-21 stsp }
5095 404c43c4 2018-06-21 stsp
5096 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
5097 0587e10c 2020-07-23 stsp commit_id, repo);
5098 0587e10c 2020-07-23 stsp if (error)
5099 0587e10c 2020-07-23 stsp goto done;
5100 0587e10c 2020-07-23 stsp
5101 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
5102 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
5103 28315671 2019-08-14 stsp if (error)
5104 28315671 2019-08-14 stsp goto done;
5105 28315671 2019-08-14 stsp
5106 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
5107 28315671 2019-08-14 stsp if (error)
5108 28315671 2019-08-14 stsp goto done;
5109 28315671 2019-08-14 stsp
5110 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
5111 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
5112 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
5113 28315671 2019-08-14 stsp goto done;
5114 28315671 2019-08-14 stsp }
5115 28315671 2019-08-14 stsp
5116 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
5117 28315671 2019-08-14 stsp if (error)
5118 28315671 2019-08-14 stsp goto done;
5119 28315671 2019-08-14 stsp bca.f = got_opentemp();
5120 28315671 2019-08-14 stsp if (bca.f == NULL) {
5121 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
5122 28315671 2019-08-14 stsp goto done;
5123 28315671 2019-08-14 stsp }
5124 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5125 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
5126 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
5127 28315671 2019-08-14 stsp goto done;
5128 28315671 2019-08-14 stsp
5129 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
5130 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
5131 b02560ec 2019-08-19 stsp bca.nlines--;
5132 b02560ec 2019-08-19 stsp
5133 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5134 28315671 2019-08-14 stsp if (bca.lines == NULL) {
5135 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
5136 28315671 2019-08-14 stsp goto done;
5137 28315671 2019-08-14 stsp }
5138 28315671 2019-08-14 stsp bca.lineno_cur = 1;
5139 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
5140 7ef28ff8 2019-08-14 stsp i = bca.nlines;
5141 7ef28ff8 2019-08-14 stsp while (i > 0) {
5142 7ef28ff8 2019-08-14 stsp i /= 10;
5143 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
5144 7ef28ff8 2019-08-14 stsp }
5145 82f6abb8 2019-08-14 stsp bca.repo = repo;
5146 7ef28ff8 2019-08-14 stsp
5147 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5148 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
5149 404c43c4 2018-06-21 stsp done:
5150 66bea077 2018-08-02 stsp free(in_repo_path);
5151 0587e10c 2020-07-23 stsp free(link_target);
5152 66bea077 2018-08-02 stsp free(repo_path);
5153 66bea077 2018-08-02 stsp free(cwd);
5154 404c43c4 2018-06-21 stsp free(commit_id);
5155 28315671 2019-08-14 stsp free(obj_id);
5156 28315671 2019-08-14 stsp if (blob)
5157 28315671 2019-08-14 stsp got_object_blob_close(blob);
5158 0c06baac 2019-02-05 stsp if (worktree)
5159 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
5160 ad242220 2018-09-08 stsp if (repo) {
5161 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5162 ad242220 2018-09-08 stsp if (error == NULL)
5163 1d0f4054 2021-06-17 stsp error = close_err;
5164 ad242220 2018-09-08 stsp }
5165 3affba96 2019-09-22 stsp if (bca.lines) {
5166 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
5167 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
5168 3affba96 2019-09-22 stsp free(bline->id_str);
5169 3affba96 2019-09-22 stsp free(bline->committer);
5170 3affba96 2019-09-22 stsp }
5171 3affba96 2019-09-22 stsp free(bca.lines);
5172 28315671 2019-08-14 stsp }
5173 28315671 2019-08-14 stsp free(bca.line_offsets);
5174 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
5175 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
5176 404c43c4 2018-06-21 stsp return error;
5177 5de5890b 2018-10-18 stsp }
5178 5de5890b 2018-10-18 stsp
5179 5de5890b 2018-10-18 stsp __dead static void
5180 5de5890b 2018-10-18 stsp usage_tree(void)
5181 5de5890b 2018-10-18 stsp {
5182 c1669e2e 2019-01-09 stsp fprintf(stderr,
5183 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5184 5de5890b 2018-10-18 stsp getprogname());
5185 5de5890b 2018-10-18 stsp exit(1);
5186 5de5890b 2018-10-18 stsp }
5187 5de5890b 2018-10-18 stsp
5188 0d6c6ee3 2020-05-20 stsp static const struct got_error *
5189 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
5190 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
5191 c1669e2e 2019-01-09 stsp {
5192 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
5193 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
5194 848d6979 2019-08-12 stsp const char *modestr = "";
5195 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
5196 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
5197 5de5890b 2018-10-18 stsp
5198 c1669e2e 2019-01-09 stsp path += strlen(root_path);
5199 c1669e2e 2019-01-09 stsp while (path[0] == '/')
5200 c1669e2e 2019-01-09 stsp path++;
5201 c1669e2e 2019-01-09 stsp
5202 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
5203 63c5ca5d 2019-08-24 stsp modestr = "$";
5204 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
5205 0d6c6ee3 2020-05-20 stsp int i;
5206 0d6c6ee3 2020-05-20 stsp
5207 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5208 0d6c6ee3 2020-05-20 stsp if (err)
5209 0d6c6ee3 2020-05-20 stsp return err;
5210 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
5211 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
5212 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
5213 0d6c6ee3 2020-05-20 stsp }
5214 0d6c6ee3 2020-05-20 stsp
5215 848d6979 2019-08-12 stsp modestr = "@";
5216 0d6c6ee3 2020-05-20 stsp }
5217 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
5218 848d6979 2019-08-12 stsp modestr = "/";
5219 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
5220 848d6979 2019-08-12 stsp modestr = "*";
5221 848d6979 2019-08-12 stsp
5222 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5223 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5224 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
5225 0d6c6ee3 2020-05-20 stsp
5226 0d6c6ee3 2020-05-20 stsp free(link_target);
5227 0d6c6ee3 2020-05-20 stsp return NULL;
5228 c1669e2e 2019-01-09 stsp }
5229 c1669e2e 2019-01-09 stsp
5230 5de5890b 2018-10-18 stsp static const struct got_error *
5231 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
5232 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
5233 c1669e2e 2019-01-09 stsp struct got_repository *repo)
5234 5de5890b 2018-10-18 stsp {
5235 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
5236 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
5237 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
5238 56e0773d 2019-11-28 stsp int nentries, i;
5239 5de5890b 2018-10-18 stsp
5240 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
5241 5de5890b 2018-10-18 stsp if (err)
5242 5de5890b 2018-10-18 stsp goto done;
5243 5de5890b 2018-10-18 stsp
5244 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
5245 5de5890b 2018-10-18 stsp if (err)
5246 5de5890b 2018-10-18 stsp goto done;
5247 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
5248 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
5249 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5250 5de5890b 2018-10-18 stsp char *id = NULL;
5251 84453469 2018-11-11 stsp
5252 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5253 84453469 2018-11-11 stsp break;
5254 84453469 2018-11-11 stsp
5255 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
5256 5de5890b 2018-10-18 stsp if (show_ids) {
5257 5de5890b 2018-10-18 stsp char *id_str;
5258 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5259 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5260 5de5890b 2018-10-18 stsp if (err)
5261 5de5890b 2018-10-18 stsp goto done;
5262 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
5263 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5264 5de5890b 2018-10-18 stsp free(id_str);
5265 5de5890b 2018-10-18 stsp goto done;
5266 5de5890b 2018-10-18 stsp }
5267 5de5890b 2018-10-18 stsp free(id_str);
5268 5de5890b 2018-10-18 stsp }
5269 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5270 5de5890b 2018-10-18 stsp free(id);
5271 0d6c6ee3 2020-05-20 stsp if (err)
5272 0d6c6ee3 2020-05-20 stsp goto done;
5273 c1669e2e 2019-01-09 stsp
5274 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5275 c1669e2e 2019-01-09 stsp char *child_path;
5276 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
5277 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
5278 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
5279 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5280 c1669e2e 2019-01-09 stsp goto done;
5281 c1669e2e 2019-01-09 stsp }
5282 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
5283 c1669e2e 2019-01-09 stsp root_path, repo);
5284 c1669e2e 2019-01-09 stsp free(child_path);
5285 c1669e2e 2019-01-09 stsp if (err)
5286 c1669e2e 2019-01-09 stsp goto done;
5287 c1669e2e 2019-01-09 stsp }
5288 5de5890b 2018-10-18 stsp }
5289 5de5890b 2018-10-18 stsp done:
5290 5de5890b 2018-10-18 stsp if (tree)
5291 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
5292 5de5890b 2018-10-18 stsp free(tree_id);
5293 5de5890b 2018-10-18 stsp return err;
5294 404c43c4 2018-06-21 stsp }
5295 404c43c4 2018-06-21 stsp
5296 5de5890b 2018-10-18 stsp static const struct got_error *
5297 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5298 5de5890b 2018-10-18 stsp {
5299 5de5890b 2018-10-18 stsp const struct got_error *error;
5300 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
5301 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
5302 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
5303 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5304 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
5305 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
5306 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
5307 5de5890b 2018-10-18 stsp int ch;
5308 5de5890b 2018-10-18 stsp
5309 5de5890b 2018-10-18 stsp #ifndef PROFILE
5310 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5311 0f8d269b 2019-01-04 stsp NULL) == -1)
5312 5de5890b 2018-10-18 stsp err(1, "pledge");
5313 5de5890b 2018-10-18 stsp #endif
5314 5de5890b 2018-10-18 stsp
5315 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5316 5de5890b 2018-10-18 stsp switch (ch) {
5317 5de5890b 2018-10-18 stsp case 'c':
5318 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5319 5de5890b 2018-10-18 stsp break;
5320 5de5890b 2018-10-18 stsp case 'r':
5321 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
5322 5de5890b 2018-10-18 stsp if (repo_path == NULL)
5323 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5324 9ba1d308 2019-10-21 stsp optarg);
5325 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5326 5de5890b 2018-10-18 stsp break;
5327 5de5890b 2018-10-18 stsp case 'i':
5328 5de5890b 2018-10-18 stsp show_ids = 1;
5329 5de5890b 2018-10-18 stsp break;
5330 c1669e2e 2019-01-09 stsp case 'R':
5331 c1669e2e 2019-01-09 stsp recurse = 1;
5332 c1669e2e 2019-01-09 stsp break;
5333 5de5890b 2018-10-18 stsp default:
5334 2deda0b9 2019-03-07 stsp usage_tree();
5335 5de5890b 2018-10-18 stsp /* NOTREACHED */
5336 5de5890b 2018-10-18 stsp }
5337 5de5890b 2018-10-18 stsp }
5338 5de5890b 2018-10-18 stsp
5339 5de5890b 2018-10-18 stsp argc -= optind;
5340 5de5890b 2018-10-18 stsp argv += optind;
5341 5de5890b 2018-10-18 stsp
5342 5de5890b 2018-10-18 stsp if (argc == 1)
5343 5de5890b 2018-10-18 stsp path = argv[0];
5344 5de5890b 2018-10-18 stsp else if (argc > 1)
5345 5de5890b 2018-10-18 stsp usage_tree();
5346 5de5890b 2018-10-18 stsp else
5347 7a2c19d6 2019-02-05 stsp path = NULL;
5348 7a2c19d6 2019-02-05 stsp
5349 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5350 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5351 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5352 9bf7a39b 2019-02-05 stsp goto done;
5353 9bf7a39b 2019-02-05 stsp }
5354 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5355 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5356 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5357 7a2c19d6 2019-02-05 stsp goto done;
5358 7a2c19d6 2019-02-05 stsp else
5359 7a2c19d6 2019-02-05 stsp error = NULL;
5360 7a2c19d6 2019-02-05 stsp if (worktree) {
5361 7a2c19d6 2019-02-05 stsp repo_path =
5362 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5363 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5364 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5365 7a2c19d6 2019-02-05 stsp if (error)
5366 7a2c19d6 2019-02-05 stsp goto done;
5367 7a2c19d6 2019-02-05 stsp } else {
5368 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5369 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5370 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5371 7a2c19d6 2019-02-05 stsp goto done;
5372 7a2c19d6 2019-02-05 stsp }
5373 5de5890b 2018-10-18 stsp }
5374 5de5890b 2018-10-18 stsp }
5375 5de5890b 2018-10-18 stsp
5376 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5377 5de5890b 2018-10-18 stsp if (error != NULL)
5378 c02c541e 2019-03-29 stsp goto done;
5379 c02c541e 2019-03-29 stsp
5380 4fedbf4c 2020-11-07 stsp if (worktree) {
5381 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5382 4fedbf4c 2020-11-07 stsp char *p;
5383 5de5890b 2018-10-18 stsp
5384 4fedbf4c 2020-11-07 stsp if (path == NULL)
5385 4fedbf4c 2020-11-07 stsp path = "";
5386 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5387 4fedbf4c 2020-11-07 stsp if (error)
5388 4fedbf4c 2020-11-07 stsp goto done;
5389 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5390 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5391 4fedbf4c 2020-11-07 stsp p) == -1) {
5392 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5393 9bf7a39b 2019-02-05 stsp free(p);
5394 4fedbf4c 2020-11-07 stsp goto done;
5395 4fedbf4c 2020-11-07 stsp }
5396 4fedbf4c 2020-11-07 stsp free(p);
5397 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5398 4fedbf4c 2020-11-07 stsp if (error)
5399 4fedbf4c 2020-11-07 stsp goto done;
5400 4fedbf4c 2020-11-07 stsp } else {
5401 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5402 4fedbf4c 2020-11-07 stsp if (error)
5403 4fedbf4c 2020-11-07 stsp goto done;
5404 4fedbf4c 2020-11-07 stsp if (path == NULL)
5405 9bf7a39b 2019-02-05 stsp path = "/";
5406 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5407 9bf7a39b 2019-02-05 stsp if (error != NULL)
5408 9bf7a39b 2019-02-05 stsp goto done;
5409 9bf7a39b 2019-02-05 stsp }
5410 5de5890b 2018-10-18 stsp
5411 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5412 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5413 4e0a20a4 2020-03-23 tracey if (worktree)
5414 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5415 4e0a20a4 2020-03-23 tracey else
5416 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5417 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5418 5de5890b 2018-10-18 stsp if (error != NULL)
5419 5de5890b 2018-10-18 stsp goto done;
5420 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5421 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5422 5de5890b 2018-10-18 stsp if (error != NULL)
5423 5de5890b 2018-10-18 stsp goto done;
5424 5de5890b 2018-10-18 stsp } else {
5425 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5426 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5427 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5428 84de9106 2020-12-26 stsp NULL);
5429 84de9106 2020-12-26 stsp if (error)
5430 84de9106 2020-12-26 stsp goto done;
5431 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5432 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5433 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5434 30837e32 2019-07-25 stsp if (error)
5435 5de5890b 2018-10-18 stsp goto done;
5436 5de5890b 2018-10-18 stsp }
5437 5de5890b 2018-10-18 stsp
5438 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5439 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5440 5de5890b 2018-10-18 stsp done:
5441 5de5890b 2018-10-18 stsp free(in_repo_path);
5442 5de5890b 2018-10-18 stsp free(repo_path);
5443 5de5890b 2018-10-18 stsp free(cwd);
5444 5de5890b 2018-10-18 stsp free(commit_id);
5445 7a2c19d6 2019-02-05 stsp if (worktree)
5446 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5447 5de5890b 2018-10-18 stsp if (repo) {
5448 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5449 5de5890b 2018-10-18 stsp if (error == NULL)
5450 1d0f4054 2021-06-17 stsp error = close_err;
5451 5de5890b 2018-10-18 stsp }
5452 5de5890b 2018-10-18 stsp return error;
5453 5de5890b 2018-10-18 stsp }
5454 5de5890b 2018-10-18 stsp
5455 6bad629b 2019-02-04 stsp __dead static void
5456 6bad629b 2019-02-04 stsp usage_status(void)
5457 6bad629b 2019-02-04 stsp {
5458 00357e4d 2021-09-14 tracey fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5459 00357e4d 2021-09-14 tracey "[-S status-codes] [path ...]\n", getprogname());
5460 6bad629b 2019-02-04 stsp exit(1);
5461 6bad629b 2019-02-04 stsp }
5462 00357e4d 2021-09-14 tracey
5463 00357e4d 2021-09-14 tracey struct got_status_arg {
5464 00357e4d 2021-09-14 tracey char *status_codes;
5465 00357e4d 2021-09-14 tracey int suppress;
5466 00357e4d 2021-09-14 tracey };
5467 5c860e29 2018-03-12 stsp
5468 b72f483a 2019-02-05 stsp static const struct got_error *
5469 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5470 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5471 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5472 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5473 6bad629b 2019-02-04 stsp {
5474 00357e4d 2021-09-14 tracey struct got_status_arg *st = arg;
5475 00357e4d 2021-09-14 tracey
5476 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5477 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5478 00357e4d 2021-09-14 tracey if (st != NULL && st->status_codes) {
5479 00357e4d 2021-09-14 tracey size_t ncodes = strlen(st->status_codes);
5480 00357e4d 2021-09-14 tracey int i, j = 0;
5481 00357e4d 2021-09-14 tracey
5482 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5483 00357e4d 2021-09-14 tracey if (st->suppress) {
5484 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5485 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i]) {
5486 00357e4d 2021-09-14 tracey j++;
5487 00357e4d 2021-09-14 tracey continue;
5488 00357e4d 2021-09-14 tracey }
5489 00357e4d 2021-09-14 tracey } else {
5490 00357e4d 2021-09-14 tracey if (status == st->status_codes[i] ||
5491 00357e4d 2021-09-14 tracey staged_status == st->status_codes[i])
5492 00357e4d 2021-09-14 tracey break;
5493 00357e4d 2021-09-14 tracey }
5494 081470ac 2020-08-13 stsp }
5495 00357e4d 2021-09-14 tracey
5496 00357e4d 2021-09-14 tracey if (st->suppress && j == 0)
5497 00357e4d 2021-09-14 tracey goto print;
5498 00357e4d 2021-09-14 tracey
5499 081470ac 2020-08-13 stsp if (i == ncodes)
5500 081470ac 2020-08-13 stsp return NULL;
5501 081470ac 2020-08-13 stsp }
5502 00357e4d 2021-09-14 tracey print:
5503 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5504 b72f483a 2019-02-05 stsp return NULL;
5505 6bad629b 2019-02-04 stsp }
5506 5c860e29 2018-03-12 stsp
5507 6bad629b 2019-02-04 stsp static const struct got_error *
5508 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5509 6bad629b 2019-02-04 stsp {
5510 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5511 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5512 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5513 00357e4d 2021-09-14 tracey struct got_status_arg st;
5514 00357e4d 2021-09-14 tracey char *cwd = NULL;
5515 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5516 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5517 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5518 5c860e29 2018-03-12 stsp
5519 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5520 72ea6654 2019-07-27 stsp
5521 00357e4d 2021-09-14 tracey memset(&st, 0, sizeof(st));
5522 00357e4d 2021-09-14 tracey st.status_codes = NULL;
5523 00357e4d 2021-09-14 tracey st.suppress = 0;
5524 00357e4d 2021-09-14 tracey
5525 00357e4d 2021-09-14 tracey while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5526 6bad629b 2019-02-04 stsp switch (ch) {
5527 f6343036 2021-06-22 stsp case 'I':
5528 f6343036 2021-06-22 stsp no_ignores = 1;
5529 f6343036 2021-06-22 stsp break;
5530 788d4a19 2021-09-14 stsp case 'S':
5531 788d4a19 2021-09-14 stsp if (st.status_codes != NULL && st.suppress == 0)
5532 788d4a19 2021-09-14 stsp option_conflict('S', 's');
5533 788d4a19 2021-09-14 stsp st.suppress = 1;
5534 788d4a19 2021-09-14 stsp /* fallthrough */
5535 081470ac 2020-08-13 stsp case 's':
5536 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5537 081470ac 2020-08-13 stsp switch (optarg[i]) {
5538 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5539 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5540 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5541 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5542 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5543 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5544 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5545 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5546 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5547 081470ac 2020-08-13 stsp break;
5548 081470ac 2020-08-13 stsp default:
5549 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5550 081470ac 2020-08-13 stsp optarg[i]);
5551 081470ac 2020-08-13 stsp }
5552 081470ac 2020-08-13 stsp }
5553 788d4a19 2021-09-14 stsp if (ch == 's' && st.suppress)
5554 b043307b 2021-09-14 stsp option_conflict('s', 'S');
5555 00357e4d 2021-09-14 tracey st.status_codes = optarg;
5556 081470ac 2020-08-13 stsp break;
5557 5c860e29 2018-03-12 stsp default:
5558 2deda0b9 2019-03-07 stsp usage_status();
5559 6bad629b 2019-02-04 stsp /* NOTREACHED */
5560 5c860e29 2018-03-12 stsp }
5561 5c860e29 2018-03-12 stsp }
5562 5c860e29 2018-03-12 stsp
5563 6bad629b 2019-02-04 stsp argc -= optind;
5564 6bad629b 2019-02-04 stsp argv += optind;
5565 5c860e29 2018-03-12 stsp
5566 6bad629b 2019-02-04 stsp #ifndef PROFILE
5567 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5568 6bad629b 2019-02-04 stsp NULL) == -1)
5569 6bad629b 2019-02-04 stsp err(1, "pledge");
5570 f42b1b34 2018-03-12 stsp #endif
5571 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5572 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5573 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5574 927df6b7 2019-02-10 stsp goto done;
5575 927df6b7 2019-02-10 stsp }
5576 927df6b7 2019-02-10 stsp
5577 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5578 fa51e947 2020-03-27 stsp if (error) {
5579 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5580 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5581 a5edda0a 2019-07-27 stsp goto done;
5582 fa51e947 2020-03-27 stsp }
5583 6bad629b 2019-02-04 stsp
5584 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5585 c9956ddf 2019-09-08 stsp NULL);
5586 6bad629b 2019-02-04 stsp if (error != NULL)
5587 6bad629b 2019-02-04 stsp goto done;
5588 6bad629b 2019-02-04 stsp
5589 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5590 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5591 087fb88c 2019-08-04 stsp if (error)
5592 087fb88c 2019-08-04 stsp goto done;
5593 087fb88c 2019-08-04 stsp
5594 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5595 6bad629b 2019-02-04 stsp if (error)
5596 6bad629b 2019-02-04 stsp goto done;
5597 6bad629b 2019-02-04 stsp
5598 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5599 00357e4d 2021-09-14 tracey print_status, &st, check_cancelled, NULL);
5600 6bad629b 2019-02-04 stsp done:
5601 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5602 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5603 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5604 927df6b7 2019-02-10 stsp free(cwd);
5605 d0eebce4 2019-03-11 stsp return error;
5606 d0eebce4 2019-03-11 stsp }
5607 d0eebce4 2019-03-11 stsp
5608 d0eebce4 2019-03-11 stsp __dead static void
5609 d0eebce4 2019-03-11 stsp usage_ref(void)
5610 d0eebce4 2019-03-11 stsp {
5611 d0eebce4 2019-03-11 stsp fprintf(stderr,
5612 0f104432 2021-11-20 stsp "usage: %s ref [-r repository] [-l] [-t] [-c object] "
5613 0f104432 2021-11-20 stsp "[-s reference] [-d] [name]\n",
5614 d0eebce4 2019-03-11 stsp getprogname());
5615 d0eebce4 2019-03-11 stsp exit(1);
5616 d0eebce4 2019-03-11 stsp }
5617 d0eebce4 2019-03-11 stsp
5618 d0eebce4 2019-03-11 stsp static const struct got_error *
5619 0f104432 2021-11-20 stsp list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
5620 d0eebce4 2019-03-11 stsp {
5621 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5622 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5623 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5624 d0eebce4 2019-03-11 stsp
5625 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5626 0f104432 2021-11-20 stsp err = got_ref_list(&refs, repo, refname, sort_by_time ?
5627 0f104432 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
5628 0f104432 2021-11-20 stsp repo);
5629 d0eebce4 2019-03-11 stsp if (err)
5630 d0eebce4 2019-03-11 stsp return err;
5631 d0eebce4 2019-03-11 stsp
5632 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5633 d0eebce4 2019-03-11 stsp char *refstr;
5634 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5635 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5636 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5637 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5638 d0eebce4 2019-03-11 stsp free(refstr);
5639 d0eebce4 2019-03-11 stsp }
5640 d0eebce4 2019-03-11 stsp
5641 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5642 d0eebce4 2019-03-11 stsp return NULL;
5643 d0eebce4 2019-03-11 stsp }
5644 d0eebce4 2019-03-11 stsp
5645 d0eebce4 2019-03-11 stsp static const struct got_error *
5646 161728eb 2021-07-24 stsp delete_ref_by_name(struct got_repository *repo, const char *refname)
5647 d0eebce4 2019-03-11 stsp {
5648 161728eb 2021-07-24 stsp const struct got_error *err;
5649 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5650 d0eebce4 2019-03-11 stsp
5651 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5652 d0eebce4 2019-03-11 stsp if (err)
5653 d0eebce4 2019-03-11 stsp return err;
5654 993f033b 2021-07-16 stsp
5655 161728eb 2021-07-24 stsp err = delete_ref(repo, ref);
5656 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5657 d0eebce4 2019-03-11 stsp return err;
5658 d0eebce4 2019-03-11 stsp }
5659 d0eebce4 2019-03-11 stsp
5660 d0eebce4 2019-03-11 stsp static const struct got_error *
5661 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5662 d0eebce4 2019-03-11 stsp {
5663 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5664 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5665 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5666 d1644381 2019-07-14 stsp
5667 d1644381 2019-07-14 stsp /*
5668 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5669 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5670 d1644381 2019-07-14 stsp * an unintended typo.
5671 d1644381 2019-07-14 stsp */
5672 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5673 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5674 d0eebce4 2019-03-11 stsp
5675 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5676 dd88155e 2019-06-29 stsp repo);
5677 d83d9d5c 2019-05-13 stsp if (err) {
5678 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5679 d0eebce4 2019-03-11 stsp
5680 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5681 d83d9d5c 2019-05-13 stsp return err;
5682 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5683 d83d9d5c 2019-05-13 stsp if (err)
5684 d83d9d5c 2019-05-13 stsp return err;
5685 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5686 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5687 d83d9d5c 2019-05-13 stsp if (err)
5688 d83d9d5c 2019-05-13 stsp return err;
5689 d83d9d5c 2019-05-13 stsp }
5690 d83d9d5c 2019-05-13 stsp
5691 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5692 d0eebce4 2019-03-11 stsp if (err)
5693 d0eebce4 2019-03-11 stsp goto done;
5694 d0eebce4 2019-03-11 stsp
5695 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5696 d0eebce4 2019-03-11 stsp done:
5697 d0eebce4 2019-03-11 stsp if (ref)
5698 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5699 d0eebce4 2019-03-11 stsp free(id);
5700 d1c1ae5f 2019-08-12 stsp return err;
5701 d1c1ae5f 2019-08-12 stsp }
5702 d1c1ae5f 2019-08-12 stsp
5703 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5704 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5705 d1c1ae5f 2019-08-12 stsp {
5706 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5707 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5708 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5709 d1c1ae5f 2019-08-12 stsp
5710 d1c1ae5f 2019-08-12 stsp /*
5711 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5712 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5713 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5714 d1c1ae5f 2019-08-12 stsp */
5715 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5716 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5717 d1c1ae5f 2019-08-12 stsp
5718 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5719 d1c1ae5f 2019-08-12 stsp if (err)
5720 d1c1ae5f 2019-08-12 stsp return err;
5721 d1c1ae5f 2019-08-12 stsp
5722 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5723 d1c1ae5f 2019-08-12 stsp if (err)
5724 d1c1ae5f 2019-08-12 stsp goto done;
5725 d1c1ae5f 2019-08-12 stsp
5726 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5727 d1c1ae5f 2019-08-12 stsp done:
5728 d1c1ae5f 2019-08-12 stsp if (target_ref)
5729 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5730 d1c1ae5f 2019-08-12 stsp if (ref)
5731 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5732 d0eebce4 2019-03-11 stsp return err;
5733 d0eebce4 2019-03-11 stsp }
5734 d0eebce4 2019-03-11 stsp
5735 d0eebce4 2019-03-11 stsp static const struct got_error *
5736 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5737 d0eebce4 2019-03-11 stsp {
5738 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5739 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5740 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5741 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5742 0f104432 2021-11-20 stsp int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
5743 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5744 b2070a3f 2020-03-22 stsp char *refname = NULL;
5745 d0eebce4 2019-03-11 stsp
5746 0f104432 2021-11-20 stsp while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
5747 d0eebce4 2019-03-11 stsp switch (ch) {
5748 e31abbf2 2020-03-22 stsp case 'c':
5749 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5750 e31abbf2 2020-03-22 stsp break;
5751 d0eebce4 2019-03-11 stsp case 'd':
5752 e31abbf2 2020-03-22 stsp do_delete = 1;
5753 d0eebce4 2019-03-11 stsp break;
5754 d0eebce4 2019-03-11 stsp case 'r':
5755 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5756 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5757 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5758 9ba1d308 2019-10-21 stsp optarg);
5759 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5760 d0eebce4 2019-03-11 stsp break;
5761 d0eebce4 2019-03-11 stsp case 'l':
5762 d0eebce4 2019-03-11 stsp do_list = 1;
5763 d1c1ae5f 2019-08-12 stsp break;
5764 d1c1ae5f 2019-08-12 stsp case 's':
5765 e31abbf2 2020-03-22 stsp symref_target = optarg;
5766 0f104432 2021-11-20 stsp break;
5767 0f104432 2021-11-20 stsp case 't':
5768 0f104432 2021-11-20 stsp sort_by_time = 1;
5769 d0eebce4 2019-03-11 stsp break;
5770 d0eebce4 2019-03-11 stsp default:
5771 d0eebce4 2019-03-11 stsp usage_ref();
5772 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5773 d0eebce4 2019-03-11 stsp }
5774 d0eebce4 2019-03-11 stsp }
5775 d0eebce4 2019-03-11 stsp
5776 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5777 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5778 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5779 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5780 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5781 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5782 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5783 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5784 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5785 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5786 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5787 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5788 0f104432 2021-11-20 stsp if (sort_by_time && !do_list)
5789 0f104432 2021-11-20 stsp errx(1, "-t option requires -l option");
5790 d0eebce4 2019-03-11 stsp
5791 d0eebce4 2019-03-11 stsp argc -= optind;
5792 d0eebce4 2019-03-11 stsp argv += optind;
5793 d0eebce4 2019-03-11 stsp
5794 e31abbf2 2020-03-22 stsp if (do_list) {
5795 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5796 d0eebce4 2019-03-11 stsp usage_ref();
5797 b2070a3f 2020-03-22 stsp if (argc == 1) {
5798 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5799 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5800 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5801 b2070a3f 2020-03-22 stsp goto done;
5802 b2070a3f 2020-03-22 stsp }
5803 b2070a3f 2020-03-22 stsp }
5804 e31abbf2 2020-03-22 stsp } else {
5805 e31abbf2 2020-03-22 stsp if (argc != 1)
5806 e31abbf2 2020-03-22 stsp usage_ref();
5807 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5808 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5809 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5810 b2070a3f 2020-03-22 stsp goto done;
5811 b2070a3f 2020-03-22 stsp }
5812 e31abbf2 2020-03-22 stsp }
5813 b2070a3f 2020-03-22 stsp
5814 b2070a3f 2020-03-22 stsp if (refname)
5815 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5816 d0eebce4 2019-03-11 stsp
5817 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5818 e0b57350 2019-03-12 stsp if (do_list) {
5819 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5820 e0b57350 2019-03-12 stsp NULL) == -1)
5821 e0b57350 2019-03-12 stsp err(1, "pledge");
5822 e0b57350 2019-03-12 stsp } else {
5823 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5824 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5825 e0b57350 2019-03-12 stsp err(1, "pledge");
5826 e0b57350 2019-03-12 stsp }
5827 d0eebce4 2019-03-11 stsp #endif
5828 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5829 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5830 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5831 d0eebce4 2019-03-11 stsp goto done;
5832 d0eebce4 2019-03-11 stsp }
5833 d0eebce4 2019-03-11 stsp
5834 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5835 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5836 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5837 d0eebce4 2019-03-11 stsp goto done;
5838 d0eebce4 2019-03-11 stsp else
5839 d0eebce4 2019-03-11 stsp error = NULL;
5840 d0eebce4 2019-03-11 stsp if (worktree) {
5841 d0eebce4 2019-03-11 stsp repo_path =
5842 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5843 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5844 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5845 d0eebce4 2019-03-11 stsp if (error)
5846 d0eebce4 2019-03-11 stsp goto done;
5847 d0eebce4 2019-03-11 stsp } else {
5848 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5849 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5850 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5851 d0eebce4 2019-03-11 stsp goto done;
5852 d0eebce4 2019-03-11 stsp }
5853 d0eebce4 2019-03-11 stsp }
5854 d0eebce4 2019-03-11 stsp }
5855 d0eebce4 2019-03-11 stsp
5856 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5857 d0eebce4 2019-03-11 stsp if (error != NULL)
5858 d0eebce4 2019-03-11 stsp goto done;
5859 d0eebce4 2019-03-11 stsp
5860 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5861 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5862 c02c541e 2019-03-29 stsp if (error)
5863 c02c541e 2019-03-29 stsp goto done;
5864 c02c541e 2019-03-29 stsp
5865 d0eebce4 2019-03-11 stsp if (do_list)
5866 0f104432 2021-11-20 stsp error = list_refs(repo, refname, sort_by_time);
5867 e31abbf2 2020-03-22 stsp else if (do_delete)
5868 161728eb 2021-07-24 stsp error = delete_ref_by_name(repo, refname);
5869 e31abbf2 2020-03-22 stsp else if (symref_target)
5870 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5871 e31abbf2 2020-03-22 stsp else {
5872 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5873 e31abbf2 2020-03-22 stsp usage_ref();
5874 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5875 e31abbf2 2020-03-22 stsp }
5876 4e759de4 2019-06-26 stsp done:
5877 b2070a3f 2020-03-22 stsp free(refname);
5878 1d0f4054 2021-06-17 stsp if (repo) {
5879 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5880 1d0f4054 2021-06-17 stsp if (error == NULL)
5881 1d0f4054 2021-06-17 stsp error = close_err;
5882 1d0f4054 2021-06-17 stsp }
5883 4e759de4 2019-06-26 stsp if (worktree)
5884 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5885 4e759de4 2019-06-26 stsp free(cwd);
5886 4e759de4 2019-06-26 stsp free(repo_path);
5887 4e759de4 2019-06-26 stsp return error;
5888 4e759de4 2019-06-26 stsp }
5889 4e759de4 2019-06-26 stsp
5890 4e759de4 2019-06-26 stsp __dead static void
5891 4e759de4 2019-06-26 stsp usage_branch(void)
5892 4e759de4 2019-06-26 stsp {
5893 4e759de4 2019-06-26 stsp fprintf(stderr,
5894 f76670f0 2021-11-20 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
5895 f76670f0 2021-11-20 stsp "[-n] [name]\n", getprogname());
5896 4e759de4 2019-06-26 stsp exit(1);
5897 4e759de4 2019-06-26 stsp }
5898 4e759de4 2019-06-26 stsp
5899 4e759de4 2019-06-26 stsp static const struct got_error *
5900 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5901 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5902 4e99b47e 2019-10-04 stsp {
5903 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5904 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5905 4e99b47e 2019-10-04 stsp char *refstr;
5906 4e99b47e 2019-10-04 stsp
5907 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5908 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5909 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5910 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5911 4e99b47e 2019-10-04 stsp
5912 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5913 4e99b47e 2019-10-04 stsp if (err)
5914 4e99b47e 2019-10-04 stsp return err;
5915 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5916 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5917 4e99b47e 2019-10-04 stsp marker = "* ";
5918 4e99b47e 2019-10-04 stsp else
5919 4e99b47e 2019-10-04 stsp marker = "~ ";
5920 4e99b47e 2019-10-04 stsp free(id);
5921 4e99b47e 2019-10-04 stsp }
5922 4e99b47e 2019-10-04 stsp
5923 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5924 4e99b47e 2019-10-04 stsp refname += 11;
5925 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5926 4e99b47e 2019-10-04 stsp refname += 18;
5927 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5928 34d4e04c 2021-02-08 stsp refname += 13;
5929 4e99b47e 2019-10-04 stsp
5930 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5931 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5932 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5933 4e99b47e 2019-10-04 stsp
5934 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5935 4e99b47e 2019-10-04 stsp free(refstr);
5936 4e99b47e 2019-10-04 stsp return NULL;
5937 4e99b47e 2019-10-04 stsp }
5938 4e99b47e 2019-10-04 stsp
5939 4e99b47e 2019-10-04 stsp static const struct got_error *
5940 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5941 ad89fa31 2019-10-04 stsp {
5942 ad89fa31 2019-10-04 stsp const char *refname;
5943 ad89fa31 2019-10-04 stsp
5944 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5945 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5946 ad89fa31 2019-10-04 stsp
5947 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5948 ad89fa31 2019-10-04 stsp
5949 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5950 ad89fa31 2019-10-04 stsp refname += 11;
5951 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5952 ad89fa31 2019-10-04 stsp refname += 18;
5953 ad89fa31 2019-10-04 stsp
5954 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5955 ad89fa31 2019-10-04 stsp
5956 ad89fa31 2019-10-04 stsp return NULL;
5957 ad89fa31 2019-10-04 stsp }
5958 ad89fa31 2019-10-04 stsp
5959 ad89fa31 2019-10-04 stsp static const struct got_error *
5960 f76670f0 2021-11-20 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree,
5961 f76670f0 2021-11-20 stsp int sort_by_time)
5962 4e759de4 2019-06-26 stsp {
5963 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5964 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5965 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5966 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5967 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5968 4e759de4 2019-06-26 stsp
5969 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5970 ba882ee3 2019-07-11 stsp
5971 4e99b47e 2019-10-04 stsp if (worktree) {
5972 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5973 4e99b47e 2019-10-04 stsp worktree);
5974 4e99b47e 2019-10-04 stsp if (err)
5975 4e99b47e 2019-10-04 stsp return err;
5976 4e99b47e 2019-10-04 stsp
5977 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5978 4e99b47e 2019-10-04 stsp worktree);
5979 4e99b47e 2019-10-04 stsp if (err)
5980 4e99b47e 2019-10-04 stsp return err;
5981 4e99b47e 2019-10-04 stsp
5982 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5983 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5984 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5985 4e99b47e 2019-10-04 stsp if (err)
5986 4e99b47e 2019-10-04 stsp return err;
5987 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5988 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5989 4e99b47e 2019-10-04 stsp }
5990 4e99b47e 2019-10-04 stsp }
5991 4e99b47e 2019-10-04 stsp
5992 f76670f0 2021-11-20 stsp err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
5993 f76670f0 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
5994 f76670f0 2021-11-20 stsp repo);
5995 4e759de4 2019-06-26 stsp if (err)
5996 4e759de4 2019-06-26 stsp return err;
5997 4e759de4 2019-06-26 stsp
5998 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5999 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
6000 4e759de4 2019-06-26 stsp
6001 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
6002 34d4e04c 2021-02-08 stsp
6003 f76670f0 2021-11-20 stsp err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6004 f76670f0 2021-11-20 stsp got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6005 f76670f0 2021-11-20 stsp repo);
6006 34d4e04c 2021-02-08 stsp if (err)
6007 34d4e04c 2021-02-08 stsp return err;
6008 34d4e04c 2021-02-08 stsp
6009 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
6010 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
6011 34d4e04c 2021-02-08 stsp
6012 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
6013 34d4e04c 2021-02-08 stsp
6014 4e759de4 2019-06-26 stsp return NULL;
6015 4e759de4 2019-06-26 stsp }
6016 4e759de4 2019-06-26 stsp
6017 4e759de4 2019-06-26 stsp static const struct got_error *
6018 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6019 45cd4e47 2019-08-25 stsp const char *branch_name)
6020 4e759de4 2019-06-26 stsp {
6021 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
6022 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
6023 2f1457c6 2021-08-27 stsp char *refname, *remote_refname = NULL;
6024 4e759de4 2019-06-26 stsp
6025 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/", 5) == 0)
6026 2f1457c6 2021-08-27 stsp branch_name += 5;
6027 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "heads/", 6) == 0)
6028 2f1457c6 2021-08-27 stsp branch_name += 6;
6029 2f1457c6 2021-08-27 stsp else if (strncmp(branch_name, "remotes/", 8) == 0)
6030 2f1457c6 2021-08-27 stsp branch_name += 8;
6031 2f1457c6 2021-08-27 stsp
6032 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6033 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
6034 4e759de4 2019-06-26 stsp
6035 2f1457c6 2021-08-27 stsp if (asprintf(&remote_refname, "refs/remotes/%s",
6036 2f1457c6 2021-08-27 stsp branch_name) == -1) {
6037 2f1457c6 2021-08-27 stsp err = got_error_from_errno("asprintf");
6038 4e759de4 2019-06-26 stsp goto done;
6039 2f1457c6 2021-08-27 stsp }
6040 4e759de4 2019-06-26 stsp
6041 2f1457c6 2021-08-27 stsp err = got_ref_open(&ref, repo, refname, 0);
6042 2f1457c6 2021-08-27 stsp if (err) {
6043 2f1457c6 2021-08-27 stsp const struct got_error *err2;
6044 2f1457c6 2021-08-27 stsp if (err->code != GOT_ERR_NOT_REF)
6045 2f1457c6 2021-08-27 stsp goto done;
6046 2f1457c6 2021-08-27 stsp /*
6047 2f1457c6 2021-08-27 stsp * Keep 'err' intact such that if neither branch exists
6048 2f1457c6 2021-08-27 stsp * we report "refs/heads" rather than "refs/remotes" in
6049 2f1457c6 2021-08-27 stsp * our error message.
6050 2f1457c6 2021-08-27 stsp */
6051 2f1457c6 2021-08-27 stsp err2 = got_ref_open(&ref, repo, remote_refname, 0);
6052 2f1457c6 2021-08-27 stsp if (err2)
6053 2f1457c6 2021-08-27 stsp goto done;
6054 2f1457c6 2021-08-27 stsp err = NULL;
6055 2f1457c6 2021-08-27 stsp }
6056 2f1457c6 2021-08-27 stsp
6057 45cd4e47 2019-08-25 stsp if (worktree &&
6058 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
6059 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
6060 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
6061 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
6062 45cd4e47 2019-08-25 stsp goto done;
6063 45cd4e47 2019-08-25 stsp }
6064 45cd4e47 2019-08-25 stsp
6065 978a28a1 2021-09-04 naddy err = delete_ref(repo, ref);
6066 4e759de4 2019-06-26 stsp done:
6067 45cd4e47 2019-08-25 stsp if (ref)
6068 45cd4e47 2019-08-25 stsp got_ref_close(ref);
6069 4e759de4 2019-06-26 stsp free(refname);
6070 2f1457c6 2021-08-27 stsp free(remote_refname);
6071 4e759de4 2019-06-26 stsp return err;
6072 4e759de4 2019-06-26 stsp }
6073 4e759de4 2019-06-26 stsp
6074 4e759de4 2019-06-26 stsp static const struct got_error *
6075 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
6076 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
6077 4e759de4 2019-06-26 stsp {
6078 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
6079 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
6080 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
6081 d3f84d51 2019-07-11 stsp
6082 d3f84d51 2019-07-11 stsp /*
6083 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
6084 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
6085 d3f84d51 2019-07-11 stsp * an unintended typo.
6086 d3f84d51 2019-07-11 stsp */
6087 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
6088 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6089 2f1457c6 2021-08-27 stsp
6090 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6091 2f1457c6 2021-08-27 stsp branch_name += 11;
6092 4e759de4 2019-06-26 stsp
6093 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6094 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6095 62d463ca 2020-10-20 naddy goto done;
6096 4e759de4 2019-06-26 stsp }
6097 4e759de4 2019-06-26 stsp
6098 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
6099 4e759de4 2019-06-26 stsp if (err == NULL) {
6100 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
6101 4e759de4 2019-06-26 stsp goto done;
6102 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
6103 4e759de4 2019-06-26 stsp goto done;
6104 4e759de4 2019-06-26 stsp
6105 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
6106 4e759de4 2019-06-26 stsp if (err)
6107 4e759de4 2019-06-26 stsp goto done;
6108 4e759de4 2019-06-26 stsp
6109 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
6110 d0eebce4 2019-03-11 stsp done:
6111 4e759de4 2019-06-26 stsp if (ref)
6112 4e759de4 2019-06-26 stsp got_ref_close(ref);
6113 4e759de4 2019-06-26 stsp free(base_refname);
6114 4e759de4 2019-06-26 stsp free(refname);
6115 4e759de4 2019-06-26 stsp return err;
6116 4e759de4 2019-06-26 stsp }
6117 4e759de4 2019-06-26 stsp
6118 4e759de4 2019-06-26 stsp static const struct got_error *
6119 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
6120 4e759de4 2019-06-26 stsp {
6121 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
6122 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
6123 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
6124 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
6125 f76670f0 2021-11-20 stsp int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6126 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
6127 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
6128 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
6129 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
6130 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
6131 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
6132 4e759de4 2019-06-26 stsp
6133 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
6134 da76fce2 2020-02-24 stsp
6135 f76670f0 2021-11-20 stsp while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6136 4e759de4 2019-06-26 stsp switch (ch) {
6137 a74f7e83 2019-11-10 stsp case 'c':
6138 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
6139 a74f7e83 2019-11-10 stsp break;
6140 4e759de4 2019-06-26 stsp case 'd':
6141 4e759de4 2019-06-26 stsp delref = optarg;
6142 4e759de4 2019-06-26 stsp break;
6143 4e759de4 2019-06-26 stsp case 'r':
6144 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
6145 4e759de4 2019-06-26 stsp if (repo_path == NULL)
6146 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6147 9ba1d308 2019-10-21 stsp optarg);
6148 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
6149 4e759de4 2019-06-26 stsp break;
6150 4e759de4 2019-06-26 stsp case 'l':
6151 4e759de4 2019-06-26 stsp do_list = 1;
6152 da76fce2 2020-02-24 stsp break;
6153 da76fce2 2020-02-24 stsp case 'n':
6154 da76fce2 2020-02-24 stsp do_update = 0;
6155 4e759de4 2019-06-26 stsp break;
6156 f76670f0 2021-11-20 stsp case 't':
6157 f76670f0 2021-11-20 stsp sort_by_time = 1;
6158 f76670f0 2021-11-20 stsp break;
6159 4e759de4 2019-06-26 stsp default:
6160 4e759de4 2019-06-26 stsp usage_branch();
6161 4e759de4 2019-06-26 stsp /* NOTREACHED */
6162 4e759de4 2019-06-26 stsp }
6163 4e759de4 2019-06-26 stsp }
6164 4e759de4 2019-06-26 stsp
6165 4e759de4 2019-06-26 stsp if (do_list && delref)
6166 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
6167 f76670f0 2021-11-20 stsp if (sort_by_time && !do_list)
6168 f76670f0 2021-11-20 stsp errx(1, "-t option requires -l option");
6169 4e759de4 2019-06-26 stsp
6170 4e759de4 2019-06-26 stsp argc -= optind;
6171 4e759de4 2019-06-26 stsp argv += optind;
6172 ad89fa31 2019-10-04 stsp
6173 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
6174 ad89fa31 2019-10-04 stsp do_show = 1;
6175 4e759de4 2019-06-26 stsp
6176 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
6177 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
6178 a74f7e83 2019-11-10 stsp
6179 4e759de4 2019-06-26 stsp if (do_list || delref) {
6180 4e759de4 2019-06-26 stsp if (argc > 0)
6181 4e759de4 2019-06-26 stsp usage_branch();
6182 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
6183 4e759de4 2019-06-26 stsp usage_branch();
6184 4e759de4 2019-06-26 stsp
6185 4e759de4 2019-06-26 stsp #ifndef PROFILE
6186 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
6187 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6188 4e759de4 2019-06-26 stsp NULL) == -1)
6189 4e759de4 2019-06-26 stsp err(1, "pledge");
6190 4e759de4 2019-06-26 stsp } else {
6191 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6192 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
6193 4e759de4 2019-06-26 stsp err(1, "pledge");
6194 4e759de4 2019-06-26 stsp }
6195 4e759de4 2019-06-26 stsp #endif
6196 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
6197 4e759de4 2019-06-26 stsp if (cwd == NULL) {
6198 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
6199 4e759de4 2019-06-26 stsp goto done;
6200 4e759de4 2019-06-26 stsp }
6201 4e759de4 2019-06-26 stsp
6202 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
6203 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
6204 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6205 4e759de4 2019-06-26 stsp goto done;
6206 4e759de4 2019-06-26 stsp else
6207 4e759de4 2019-06-26 stsp error = NULL;
6208 4e759de4 2019-06-26 stsp if (worktree) {
6209 4e759de4 2019-06-26 stsp repo_path =
6210 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
6211 4e759de4 2019-06-26 stsp if (repo_path == NULL)
6212 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
6213 4e759de4 2019-06-26 stsp if (error)
6214 4e759de4 2019-06-26 stsp goto done;
6215 4e759de4 2019-06-26 stsp } else {
6216 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
6217 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
6218 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
6219 4e759de4 2019-06-26 stsp goto done;
6220 4e759de4 2019-06-26 stsp }
6221 4e759de4 2019-06-26 stsp }
6222 4e759de4 2019-06-26 stsp }
6223 4e759de4 2019-06-26 stsp
6224 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6225 4e759de4 2019-06-26 stsp if (error != NULL)
6226 4e759de4 2019-06-26 stsp goto done;
6227 4e759de4 2019-06-26 stsp
6228 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
6229 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
6230 4e759de4 2019-06-26 stsp if (error)
6231 4e759de4 2019-06-26 stsp goto done;
6232 4e759de4 2019-06-26 stsp
6233 ad89fa31 2019-10-04 stsp if (do_show)
6234 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
6235 ad89fa31 2019-10-04 stsp else if (do_list)
6236 f76670f0 2021-11-20 stsp error = list_branches(repo, worktree, sort_by_time);
6237 4e759de4 2019-06-26 stsp else if (delref)
6238 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
6239 4e759de4 2019-06-26 stsp else {
6240 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6241 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6242 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6243 84de9106 2020-12-26 stsp NULL);
6244 84de9106 2020-12-26 stsp if (error)
6245 84de9106 2020-12-26 stsp goto done;
6246 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
6247 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
6248 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
6249 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
6250 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
6251 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6252 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6253 a74f7e83 2019-11-10 stsp if (error)
6254 a74f7e83 2019-11-10 stsp goto done;
6255 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
6256 da76fce2 2020-02-24 stsp if (error)
6257 da76fce2 2020-02-24 stsp goto done;
6258 da76fce2 2020-02-24 stsp if (worktree && do_update) {
6259 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6260 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
6261 da76fce2 2020-02-24 stsp
6262 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
6263 da76fce2 2020-02-24 stsp if (error)
6264 da76fce2 2020-02-24 stsp goto done;
6265 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
6266 da76fce2 2020-02-24 stsp worktree);
6267 da76fce2 2020-02-24 stsp if (error)
6268 da76fce2 2020-02-24 stsp goto done;
6269 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6270 da76fce2 2020-02-24 stsp == -1) {
6271 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
6272 da76fce2 2020-02-24 stsp goto done;
6273 da76fce2 2020-02-24 stsp }
6274 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
6275 da76fce2 2020-02-24 stsp free(branch_refname);
6276 da76fce2 2020-02-24 stsp if (error)
6277 da76fce2 2020-02-24 stsp goto done;
6278 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
6279 da76fce2 2020-02-24 stsp repo);
6280 da76fce2 2020-02-24 stsp if (error)
6281 da76fce2 2020-02-24 stsp goto done;
6282 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
6283 da76fce2 2020-02-24 stsp commit_id);
6284 da76fce2 2020-02-24 stsp if (error)
6285 da76fce2 2020-02-24 stsp goto done;
6286 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6287 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
6288 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
6289 9627c110 2020-04-18 stsp NULL);
6290 da76fce2 2020-02-24 stsp if (error)
6291 da76fce2 2020-02-24 stsp goto done;
6292 4f3c844b 2021-09-14 stsp if (upa.did_something) {
6293 4f3c844b 2021-09-14 stsp printf("Updated to %s: %s\n",
6294 4f3c844b 2021-09-14 stsp got_worktree_get_head_ref_name(worktree),
6295 4f3c844b 2021-09-14 stsp commit_id_str);
6296 4f3c844b 2021-09-14 stsp }
6297 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6298 da76fce2 2020-02-24 stsp }
6299 4e759de4 2019-06-26 stsp }
6300 4e759de4 2019-06-26 stsp done:
6301 da76fce2 2020-02-24 stsp if (ref)
6302 da76fce2 2020-02-24 stsp got_ref_close(ref);
6303 1d0f4054 2021-06-17 stsp if (repo) {
6304 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6305 1d0f4054 2021-06-17 stsp if (error == NULL)
6306 1d0f4054 2021-06-17 stsp error = close_err;
6307 1d0f4054 2021-06-17 stsp }
6308 d0eebce4 2019-03-11 stsp if (worktree)
6309 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
6310 d0eebce4 2019-03-11 stsp free(cwd);
6311 d0eebce4 2019-03-11 stsp free(repo_path);
6312 da76fce2 2020-02-24 stsp free(commit_id);
6313 da76fce2 2020-02-24 stsp free(commit_id_str);
6314 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
6315 da76fce2 2020-02-24 stsp free((char *)pe->path);
6316 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
6317 d00136be 2019-03-26 stsp return error;
6318 d00136be 2019-03-26 stsp }
6319 d00136be 2019-03-26 stsp
6320 8e7bd50a 2019-08-22 stsp
6321 d00136be 2019-03-26 stsp __dead static void
6322 8e7bd50a 2019-08-22 stsp usage_tag(void)
6323 8e7bd50a 2019-08-22 stsp {
6324 8e7bd50a 2019-08-22 stsp fprintf(stderr,
6325 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
6326 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
6327 8e7bd50a 2019-08-22 stsp exit(1);
6328 b8bad2ba 2019-08-23 stsp }
6329 b8bad2ba 2019-08-23 stsp
6330 b8bad2ba 2019-08-23 stsp #if 0
6331 b8bad2ba 2019-08-23 stsp static const struct got_error *
6332 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6333 b8bad2ba 2019-08-23 stsp {
6334 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
6335 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
6336 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
6337 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
6338 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
6339 b8bad2ba 2019-08-23 stsp
6340 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
6341 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
6342 b8bad2ba 2019-08-23 stsp if (se == NULL) {
6343 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6344 b8bad2ba 2019-08-23 stsp if (err)
6345 b8bad2ba 2019-08-23 stsp return err;
6346 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6347 b8bad2ba 2019-08-23 stsp continue;
6348 b8bad2ba 2019-08-23 stsp } else {
6349 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6350 b8bad2ba 2019-08-23 stsp if (err)
6351 b8bad2ba 2019-08-23 stsp break;
6352 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
6353 b8bad2ba 2019-08-23 stsp free(re_id);
6354 b8bad2ba 2019-08-23 stsp if (err)
6355 b8bad2ba 2019-08-23 stsp break;
6356 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
6357 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
6358 b8bad2ba 2019-08-23 stsp }
6359 b8bad2ba 2019-08-23 stsp
6360 b8bad2ba 2019-08-23 stsp while (se) {
6361 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6362 b8bad2ba 2019-08-23 stsp if (err)
6363 b8bad2ba 2019-08-23 stsp break;
6364 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
6365 b8bad2ba 2019-08-23 stsp free(se_id);
6366 b8bad2ba 2019-08-23 stsp if (err)
6367 b8bad2ba 2019-08-23 stsp break;
6368 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
6369 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
6370 b8bad2ba 2019-08-23 stsp
6371 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6372 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6373 b8bad2ba 2019-08-23 stsp if (err)
6374 b8bad2ba 2019-08-23 stsp return err;
6375 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6376 b8bad2ba 2019-08-23 stsp break;
6377 b8bad2ba 2019-08-23 stsp }
6378 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6379 b8bad2ba 2019-08-23 stsp continue;
6380 b8bad2ba 2019-08-23 stsp }
6381 b8bad2ba 2019-08-23 stsp }
6382 b8bad2ba 2019-08-23 stsp done:
6383 b8bad2ba 2019-08-23 stsp return err;
6384 8e7bd50a 2019-08-22 stsp }
6385 b8bad2ba 2019-08-23 stsp #endif
6386 b8bad2ba 2019-08-23 stsp
6387 b8bad2ba 2019-08-23 stsp static const struct got_error *
6388 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
6389 8e7bd50a 2019-08-22 stsp {
6390 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
6391 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
6392 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6393 8e7bd50a 2019-08-22 stsp
6394 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6395 8e7bd50a 2019-08-22 stsp
6396 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6397 8e7bd50a 2019-08-22 stsp if (err)
6398 8e7bd50a 2019-08-22 stsp return err;
6399 8e7bd50a 2019-08-22 stsp
6400 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6401 8e7bd50a 2019-08-22 stsp const char *refname;
6402 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6403 8e7bd50a 2019-08-22 stsp char datebuf[26];
6404 d4efa91b 2020-01-14 stsp const char *tagger;
6405 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6406 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6407 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6408 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6409 8e7bd50a 2019-08-22 stsp
6410 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6411 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6412 8e7bd50a 2019-08-22 stsp continue;
6413 8e7bd50a 2019-08-22 stsp refname += 10;
6414 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6415 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6416 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6417 8e7bd50a 2019-08-22 stsp break;
6418 8e7bd50a 2019-08-22 stsp }
6419 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6420 8e7bd50a 2019-08-22 stsp free(refstr);
6421 8e7bd50a 2019-08-22 stsp
6422 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6423 8e7bd50a 2019-08-22 stsp if (err)
6424 8e7bd50a 2019-08-22 stsp break;
6425 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6426 d4efa91b 2020-01-14 stsp if (err) {
6427 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6428 d4efa91b 2020-01-14 stsp free(id);
6429 d4efa91b 2020-01-14 stsp break;
6430 d4efa91b 2020-01-14 stsp }
6431 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6432 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6433 d4efa91b 2020-01-14 stsp if (err) {
6434 d4efa91b 2020-01-14 stsp free(id);
6435 d4efa91b 2020-01-14 stsp break;
6436 d4efa91b 2020-01-14 stsp }
6437 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6438 d4efa91b 2020-01-14 stsp tagger_time =
6439 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6440 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6441 d4efa91b 2020-01-14 stsp free(id);
6442 d4efa91b 2020-01-14 stsp if (err)
6443 d4efa91b 2020-01-14 stsp break;
6444 d4efa91b 2020-01-14 stsp } else {
6445 d4efa91b 2020-01-14 stsp free(id);
6446 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6447 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6448 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6449 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6450 d4efa91b 2020-01-14 stsp if (err)
6451 d4efa91b 2020-01-14 stsp break;
6452 d4efa91b 2020-01-14 stsp }
6453 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6454 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6455 2417344c 2019-08-23 stsp if (datestr)
6456 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6457 d4efa91b 2020-01-14 stsp if (commit)
6458 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6459 d4efa91b 2020-01-14 stsp else {
6460 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6461 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6462 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6463 d4efa91b 2020-01-14 stsp id_str);
6464 d4efa91b 2020-01-14 stsp break;
6465 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6466 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6467 d4efa91b 2020-01-14 stsp id_str);
6468 d4efa91b 2020-01-14 stsp break;
6469 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6470 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6471 d4efa91b 2020-01-14 stsp id_str);
6472 d4efa91b 2020-01-14 stsp break;
6473 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6474 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6475 d4efa91b 2020-01-14 stsp id_str);
6476 d4efa91b 2020-01-14 stsp break;
6477 d4efa91b 2020-01-14 stsp default:
6478 d4efa91b 2020-01-14 stsp break;
6479 d4efa91b 2020-01-14 stsp }
6480 8e7bd50a 2019-08-22 stsp }
6481 8e7bd50a 2019-08-22 stsp free(id_str);
6482 d4efa91b 2020-01-14 stsp if (commit) {
6483 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6484 d4efa91b 2020-01-14 stsp if (err)
6485 d4efa91b 2020-01-14 stsp break;
6486 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6487 d4efa91b 2020-01-14 stsp } else {
6488 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6489 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6490 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6491 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6492 d4efa91b 2020-01-14 stsp break;
6493 d4efa91b 2020-01-14 stsp }
6494 8e7bd50a 2019-08-22 stsp }
6495 8e7bd50a 2019-08-22 stsp
6496 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6497 8e7bd50a 2019-08-22 stsp do {
6498 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6499 8e7bd50a 2019-08-22 stsp if (line)
6500 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6501 8e7bd50a 2019-08-22 stsp } while (line);
6502 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6503 8e7bd50a 2019-08-22 stsp }
6504 8e7bd50a 2019-08-22 stsp
6505 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6506 8e7bd50a 2019-08-22 stsp return NULL;
6507 8e7bd50a 2019-08-22 stsp }
6508 8e7bd50a 2019-08-22 stsp
6509 8e7bd50a 2019-08-22 stsp static const struct got_error *
6510 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6511 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6512 8e7bd50a 2019-08-22 stsp {
6513 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6514 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6515 f372d5cd 2019-10-21 stsp char *editor = NULL;
6516 1601cb9f 2020-09-11 naddy int initial_content_len;
6517 8e7bd50a 2019-08-22 stsp int fd = -1;
6518 8e7bd50a 2019-08-22 stsp
6519 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6520 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6521 8e7bd50a 2019-08-22 stsp goto done;
6522 8e7bd50a 2019-08-22 stsp }
6523 8e7bd50a 2019-08-22 stsp
6524 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6525 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6526 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6527 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6528 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6529 8e7bd50a 2019-08-22 stsp goto done;
6530 8e7bd50a 2019-08-22 stsp }
6531 8e7bd50a 2019-08-22 stsp
6532 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6533 8e7bd50a 2019-08-22 stsp if (err)
6534 8e7bd50a 2019-08-22 stsp goto done;
6535 8e7bd50a 2019-08-22 stsp
6536 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6537 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6538 97972933 2020-09-11 stsp goto done;
6539 97972933 2020-09-11 stsp }
6540 8e7bd50a 2019-08-22 stsp
6541 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6542 8e7bd50a 2019-08-22 stsp if (err)
6543 8e7bd50a 2019-08-22 stsp goto done;
6544 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6545 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6546 8e7bd50a 2019-08-22 stsp done:
6547 8e7bd50a 2019-08-22 stsp free(initial_content);
6548 8e7bd50a 2019-08-22 stsp free(template);
6549 8e7bd50a 2019-08-22 stsp free(editor);
6550 8e7bd50a 2019-08-22 stsp
6551 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6552 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6553 97972933 2020-09-11 stsp
6554 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6555 59f86c76 2020-09-11 stsp if (err == NULL)
6556 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6557 59f86c76 2020-09-11 stsp if (err) {
6558 59f86c76 2020-09-11 stsp free(*tagmsg);
6559 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6560 8e7bd50a 2019-08-22 stsp }
6561 8e7bd50a 2019-08-22 stsp return err;
6562 8e7bd50a 2019-08-22 stsp }
6563 8e7bd50a 2019-08-22 stsp
6564 8e7bd50a 2019-08-22 stsp static const struct got_error *
6565 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6566 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6567 8e7bd50a 2019-08-22 stsp {
6568 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6569 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6570 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6571 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6572 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6573 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6574 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6575 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6576 8e7bd50a 2019-08-22 stsp
6577 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6578 84de9106 2020-12-26 stsp
6579 8e7bd50a 2019-08-22 stsp /*
6580 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6581 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6582 8e7bd50a 2019-08-22 stsp * an unintended typo.
6583 8e7bd50a 2019-08-22 stsp */
6584 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6585 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6586 8e7bd50a 2019-08-22 stsp
6587 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6588 8e7bd50a 2019-08-22 stsp if (err)
6589 8e7bd50a 2019-08-22 stsp return err;
6590 8e7bd50a 2019-08-22 stsp
6591 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6592 84de9106 2020-12-26 stsp if (err)
6593 84de9106 2020-12-26 stsp goto done;
6594 84de9106 2020-12-26 stsp
6595 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6596 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6597 8e7bd50a 2019-08-22 stsp if (err)
6598 8e7bd50a 2019-08-22 stsp goto done;
6599 8e7bd50a 2019-08-22 stsp
6600 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6601 8e7bd50a 2019-08-22 stsp if (err)
6602 8e7bd50a 2019-08-22 stsp goto done;
6603 8e7bd50a 2019-08-22 stsp
6604 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6605 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6606 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6607 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6608 62d463ca 2020-10-20 naddy goto done;
6609 8e7bd50a 2019-08-22 stsp }
6610 8e7bd50a 2019-08-22 stsp tag_name += 10;
6611 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6612 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6613 62d463ca 2020-10-20 naddy goto done;
6614 8e7bd50a 2019-08-22 stsp }
6615 8e7bd50a 2019-08-22 stsp
6616 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6617 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6618 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6619 8e7bd50a 2019-08-22 stsp goto done;
6620 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6621 8e7bd50a 2019-08-22 stsp goto done;
6622 8e7bd50a 2019-08-22 stsp
6623 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6624 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6625 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6626 f372d5cd 2019-10-21 stsp if (err) {
6627 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6628 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6629 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6630 8e7bd50a 2019-08-22 stsp goto done;
6631 f372d5cd 2019-10-21 stsp }
6632 8e7bd50a 2019-08-22 stsp }
6633 8e7bd50a 2019-08-22 stsp
6634 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6635 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6636 f372d5cd 2019-10-21 stsp if (err) {
6637 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6638 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6639 8e7bd50a 2019-08-22 stsp goto done;
6640 f372d5cd 2019-10-21 stsp }
6641 8e7bd50a 2019-08-22 stsp
6642 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6643 f372d5cd 2019-10-21 stsp if (err) {
6644 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6645 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6646 8e7bd50a 2019-08-22 stsp goto done;
6647 f372d5cd 2019-10-21 stsp }
6648 8e7bd50a 2019-08-22 stsp
6649 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6650 f372d5cd 2019-10-21 stsp if (err) {
6651 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6652 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6653 f372d5cd 2019-10-21 stsp goto done;
6654 f372d5cd 2019-10-21 stsp }
6655 8e7bd50a 2019-08-22 stsp
6656 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6657 f372d5cd 2019-10-21 stsp if (err) {
6658 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6659 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6660 f372d5cd 2019-10-21 stsp goto done;
6661 8e7bd50a 2019-08-22 stsp }
6662 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6663 8e7bd50a 2019-08-22 stsp done:
6664 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6665 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6666 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6667 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6668 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6669 f372d5cd 2019-10-21 stsp free(tag_id_str);
6670 8e7bd50a 2019-08-22 stsp if (ref)
6671 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6672 8e7bd50a 2019-08-22 stsp free(commit_id);
6673 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6674 8e7bd50a 2019-08-22 stsp free(refname);
6675 8e7bd50a 2019-08-22 stsp free(tagmsg);
6676 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6677 aba9c984 2019-09-08 stsp free(tagger);
6678 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6679 8e7bd50a 2019-08-22 stsp return err;
6680 8e7bd50a 2019-08-22 stsp }
6681 8e7bd50a 2019-08-22 stsp
6682 8e7bd50a 2019-08-22 stsp static const struct got_error *
6683 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6684 8e7bd50a 2019-08-22 stsp {
6685 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6686 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6687 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6688 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6689 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6690 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6691 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6692 8e7bd50a 2019-08-22 stsp
6693 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6694 8e7bd50a 2019-08-22 stsp switch (ch) {
6695 80106605 2020-02-24 stsp case 'c':
6696 80106605 2020-02-24 stsp commit_id_arg = optarg;
6697 80106605 2020-02-24 stsp break;
6698 8e7bd50a 2019-08-22 stsp case 'm':
6699 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6700 8e7bd50a 2019-08-22 stsp break;
6701 8e7bd50a 2019-08-22 stsp case 'r':
6702 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6703 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6704 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6705 9ba1d308 2019-10-21 stsp optarg);
6706 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6707 8e7bd50a 2019-08-22 stsp break;
6708 8e7bd50a 2019-08-22 stsp case 'l':
6709 8e7bd50a 2019-08-22 stsp do_list = 1;
6710 8e7bd50a 2019-08-22 stsp break;
6711 8e7bd50a 2019-08-22 stsp default:
6712 8e7bd50a 2019-08-22 stsp usage_tag();
6713 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6714 8e7bd50a 2019-08-22 stsp }
6715 8e7bd50a 2019-08-22 stsp }
6716 8e7bd50a 2019-08-22 stsp
6717 8e7bd50a 2019-08-22 stsp argc -= optind;
6718 8e7bd50a 2019-08-22 stsp argv += optind;
6719 8e7bd50a 2019-08-22 stsp
6720 8e7bd50a 2019-08-22 stsp if (do_list) {
6721 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6722 775ce909 2020-03-22 stsp errx(1,
6723 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6724 8e7bd50a 2019-08-22 stsp if (tagmsg)
6725 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6726 8e7bd50a 2019-08-22 stsp if (argc > 0)
6727 8e7bd50a 2019-08-22 stsp usage_tag();
6728 80106605 2020-02-24 stsp } else if (argc != 1)
6729 8e7bd50a 2019-08-22 stsp usage_tag();
6730 80106605 2020-02-24 stsp
6731 a2887370 2019-08-23 stsp tag_name = argv[0];
6732 8e7bd50a 2019-08-22 stsp
6733 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6734 8e7bd50a 2019-08-22 stsp if (do_list) {
6735 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6736 8e7bd50a 2019-08-22 stsp NULL) == -1)
6737 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6738 8e7bd50a 2019-08-22 stsp } else {
6739 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6740 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6741 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6742 8e7bd50a 2019-08-22 stsp }
6743 8e7bd50a 2019-08-22 stsp #endif
6744 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6745 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6746 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6747 8e7bd50a 2019-08-22 stsp goto done;
6748 8e7bd50a 2019-08-22 stsp }
6749 8e7bd50a 2019-08-22 stsp
6750 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6751 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6752 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6753 8e7bd50a 2019-08-22 stsp goto done;
6754 8e7bd50a 2019-08-22 stsp else
6755 8e7bd50a 2019-08-22 stsp error = NULL;
6756 8e7bd50a 2019-08-22 stsp if (worktree) {
6757 8e7bd50a 2019-08-22 stsp repo_path =
6758 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6759 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6760 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6761 8e7bd50a 2019-08-22 stsp if (error)
6762 8e7bd50a 2019-08-22 stsp goto done;
6763 8e7bd50a 2019-08-22 stsp } else {
6764 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6765 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6766 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6767 8e7bd50a 2019-08-22 stsp goto done;
6768 8e7bd50a 2019-08-22 stsp }
6769 8e7bd50a 2019-08-22 stsp }
6770 8e7bd50a 2019-08-22 stsp }
6771 8e7bd50a 2019-08-22 stsp
6772 8e7bd50a 2019-08-22 stsp if (do_list) {
6773 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6774 c9956ddf 2019-09-08 stsp if (error != NULL)
6775 c9956ddf 2019-09-08 stsp goto done;
6776 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6777 8e7bd50a 2019-08-22 stsp if (error)
6778 8e7bd50a 2019-08-22 stsp goto done;
6779 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6780 8e7bd50a 2019-08-22 stsp } else {
6781 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6782 c9956ddf 2019-09-08 stsp if (error)
6783 c9956ddf 2019-09-08 stsp goto done;
6784 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6785 c9956ddf 2019-09-08 stsp if (error != NULL)
6786 c9956ddf 2019-09-08 stsp goto done;
6787 c9956ddf 2019-09-08 stsp
6788 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6789 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6790 8e7bd50a 2019-08-22 stsp if (error)
6791 8e7bd50a 2019-08-22 stsp goto done;
6792 8e7bd50a 2019-08-22 stsp }
6793 8e7bd50a 2019-08-22 stsp
6794 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6795 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6796 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6797 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6798 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6799 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6800 8e7bd50a 2019-08-22 stsp if (error)
6801 8e7bd50a 2019-08-22 stsp goto done;
6802 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6803 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6804 8e7bd50a 2019-08-22 stsp if (error)
6805 8e7bd50a 2019-08-22 stsp goto done;
6806 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6807 8e7bd50a 2019-08-22 stsp free(commit_id);
6808 8e7bd50a 2019-08-22 stsp if (error)
6809 8e7bd50a 2019-08-22 stsp goto done;
6810 8e7bd50a 2019-08-22 stsp }
6811 8e7bd50a 2019-08-22 stsp
6812 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6813 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6814 8e7bd50a 2019-08-22 stsp }
6815 8e7bd50a 2019-08-22 stsp done:
6816 1d0f4054 2021-06-17 stsp if (repo) {
6817 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6818 1d0f4054 2021-06-17 stsp if (error == NULL)
6819 1d0f4054 2021-06-17 stsp error = close_err;
6820 1d0f4054 2021-06-17 stsp }
6821 8e7bd50a 2019-08-22 stsp if (worktree)
6822 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6823 8e7bd50a 2019-08-22 stsp free(cwd);
6824 8e7bd50a 2019-08-22 stsp free(repo_path);
6825 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6826 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6827 8e7bd50a 2019-08-22 stsp return error;
6828 8e7bd50a 2019-08-22 stsp }
6829 8e7bd50a 2019-08-22 stsp
6830 8e7bd50a 2019-08-22 stsp __dead static void
6831 d00136be 2019-03-26 stsp usage_add(void)
6832 d00136be 2019-03-26 stsp {
6833 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6834 022fae89 2019-12-06 tracey getprogname());
6835 d00136be 2019-03-26 stsp exit(1);
6836 d00136be 2019-03-26 stsp }
6837 d00136be 2019-03-26 stsp
6838 d00136be 2019-03-26 stsp static const struct got_error *
6839 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6840 4e68cba3 2019-11-23 stsp {
6841 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6842 4e68cba3 2019-11-23 stsp path++;
6843 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6844 4e68cba3 2019-11-23 stsp return NULL;
6845 4e68cba3 2019-11-23 stsp }
6846 4e68cba3 2019-11-23 stsp
6847 4e68cba3 2019-11-23 stsp static const struct got_error *
6848 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6849 d00136be 2019-03-26 stsp {
6850 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6851 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6852 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6853 1dd54920 2019-05-11 stsp char *cwd = NULL;
6854 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6855 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6856 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6857 1dd54920 2019-05-11 stsp
6858 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6859 d00136be 2019-03-26 stsp
6860 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6861 d00136be 2019-03-26 stsp switch (ch) {
6862 022fae89 2019-12-06 tracey case 'I':
6863 022fae89 2019-12-06 tracey no_ignores = 1;
6864 022fae89 2019-12-06 tracey break;
6865 4e68cba3 2019-11-23 stsp case 'R':
6866 4e68cba3 2019-11-23 stsp can_recurse = 1;
6867 4e68cba3 2019-11-23 stsp break;
6868 d00136be 2019-03-26 stsp default:
6869 d00136be 2019-03-26 stsp usage_add();
6870 d00136be 2019-03-26 stsp /* NOTREACHED */
6871 d00136be 2019-03-26 stsp }
6872 d00136be 2019-03-26 stsp }
6873 d00136be 2019-03-26 stsp
6874 d00136be 2019-03-26 stsp argc -= optind;
6875 d00136be 2019-03-26 stsp argv += optind;
6876 d00136be 2019-03-26 stsp
6877 43012d58 2019-07-14 stsp #ifndef PROFILE
6878 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6879 43012d58 2019-07-14 stsp NULL) == -1)
6880 43012d58 2019-07-14 stsp err(1, "pledge");
6881 43012d58 2019-07-14 stsp #endif
6882 723c305c 2019-05-11 jcs if (argc < 1)
6883 d00136be 2019-03-26 stsp usage_add();
6884 d00136be 2019-03-26 stsp
6885 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6886 d00136be 2019-03-26 stsp if (cwd == NULL) {
6887 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6888 d00136be 2019-03-26 stsp goto done;
6889 d00136be 2019-03-26 stsp }
6890 723c305c 2019-05-11 jcs
6891 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6892 fa51e947 2020-03-27 stsp if (error) {
6893 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6894 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6895 d00136be 2019-03-26 stsp goto done;
6896 fa51e947 2020-03-27 stsp }
6897 d00136be 2019-03-26 stsp
6898 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6899 c9956ddf 2019-09-08 stsp NULL);
6900 031a5338 2019-03-26 stsp if (error != NULL)
6901 031a5338 2019-03-26 stsp goto done;
6902 031a5338 2019-03-26 stsp
6903 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6904 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6905 d00136be 2019-03-26 stsp if (error)
6906 d00136be 2019-03-26 stsp goto done;
6907 d00136be 2019-03-26 stsp
6908 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6909 6d022e97 2019-08-04 stsp if (error)
6910 022fae89 2019-12-06 tracey goto done;
6911 022fae89 2019-12-06 tracey
6912 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6913 4e68cba3 2019-11-23 stsp char *ondisk_path;
6914 4e68cba3 2019-11-23 stsp struct stat sb;
6915 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6916 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6917 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6918 62d463ca 2020-10-20 naddy pe->path) == -1) {
6919 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6920 4e68cba3 2019-11-23 stsp goto done;
6921 4e68cba3 2019-11-23 stsp }
6922 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6923 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6924 4e68cba3 2019-11-23 stsp free(ondisk_path);
6925 4e68cba3 2019-11-23 stsp continue;
6926 4e68cba3 2019-11-23 stsp }
6927 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6928 4e68cba3 2019-11-23 stsp ondisk_path);
6929 4e68cba3 2019-11-23 stsp free(ondisk_path);
6930 4e68cba3 2019-11-23 stsp goto done;
6931 4e68cba3 2019-11-23 stsp }
6932 4e68cba3 2019-11-23 stsp free(ondisk_path);
6933 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6934 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6935 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6936 4e68cba3 2019-11-23 stsp goto done;
6937 4e68cba3 2019-11-23 stsp }
6938 4e68cba3 2019-11-23 stsp }
6939 4e68cba3 2019-11-23 stsp }
6940 022fae89 2019-12-06 tracey
6941 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6942 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6943 d00136be 2019-03-26 stsp done:
6944 1d0f4054 2021-06-17 stsp if (repo) {
6945 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6946 1d0f4054 2021-06-17 stsp if (error == NULL)
6947 1d0f4054 2021-06-17 stsp error = close_err;
6948 1d0f4054 2021-06-17 stsp }
6949 d00136be 2019-03-26 stsp if (worktree)
6950 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6951 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6952 1dd54920 2019-05-11 stsp free((char *)pe->path);
6953 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6954 2ec1f75b 2019-03-26 stsp free(cwd);
6955 2ec1f75b 2019-03-26 stsp return error;
6956 2ec1f75b 2019-03-26 stsp }
6957 2ec1f75b 2019-03-26 stsp
6958 2ec1f75b 2019-03-26 stsp __dead static void
6959 648e4ef7 2019-07-09 stsp usage_remove(void)
6960 2ec1f75b 2019-03-26 stsp {
6961 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6962 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6963 2ec1f75b 2019-03-26 stsp exit(1);
6964 2a06fe5f 2019-08-24 stsp }
6965 2a06fe5f 2019-08-24 stsp
6966 2a06fe5f 2019-08-24 stsp static const struct got_error *
6967 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6968 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6969 2a06fe5f 2019-08-24 stsp {
6970 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6971 f2a9dc41 2019-12-13 tracey path++;
6972 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6973 2a06fe5f 2019-08-24 stsp return NULL;
6974 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6975 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6976 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6977 2a06fe5f 2019-08-24 stsp return NULL;
6978 2ec1f75b 2019-03-26 stsp }
6979 2ec1f75b 2019-03-26 stsp
6980 2ec1f75b 2019-03-26 stsp static const struct got_error *
6981 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6982 2ec1f75b 2019-03-26 stsp {
6983 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6984 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6985 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6986 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6987 17ed4618 2019-06-02 stsp char *cwd = NULL;
6988 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6989 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6990 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6991 4e12cd97 2022-01-25 stsp int ignore_missing_paths = 0;
6992 2ec1f75b 2019-03-26 stsp
6993 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6994 17ed4618 2019-06-02 stsp
6995 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6996 2ec1f75b 2019-03-26 stsp switch (ch) {
6997 2ec1f75b 2019-03-26 stsp case 'f':
6998 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6999 4e12cd97 2022-01-25 stsp ignore_missing_paths = 1;
7000 2ec1f75b 2019-03-26 stsp break;
7001 70e3e7f5 2019-12-13 tracey case 'k':
7002 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
7003 70e3e7f5 2019-12-13 tracey break;
7004 f2a9dc41 2019-12-13 tracey case 'R':
7005 f2a9dc41 2019-12-13 tracey can_recurse = 1;
7006 f2a9dc41 2019-12-13 tracey break;
7007 766841c2 2020-08-13 stsp case 's':
7008 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
7009 766841c2 2020-08-13 stsp switch (optarg[i]) {
7010 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
7011 766841c2 2020-08-13 stsp delete_local_mods = 1;
7012 766841c2 2020-08-13 stsp break;
7013 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
7014 4e12cd97 2022-01-25 stsp ignore_missing_paths = 1;
7015 766841c2 2020-08-13 stsp break;
7016 766841c2 2020-08-13 stsp default:
7017 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
7018 766841c2 2020-08-13 stsp optarg[i]);
7019 766841c2 2020-08-13 stsp }
7020 766841c2 2020-08-13 stsp }
7021 766841c2 2020-08-13 stsp status_codes = optarg;
7022 766841c2 2020-08-13 stsp break;
7023 2ec1f75b 2019-03-26 stsp default:
7024 f2a9dc41 2019-12-13 tracey usage_remove();
7025 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
7026 2ec1f75b 2019-03-26 stsp }
7027 2ec1f75b 2019-03-26 stsp }
7028 2ec1f75b 2019-03-26 stsp
7029 2ec1f75b 2019-03-26 stsp argc -= optind;
7030 2ec1f75b 2019-03-26 stsp argv += optind;
7031 2ec1f75b 2019-03-26 stsp
7032 43012d58 2019-07-14 stsp #ifndef PROFILE
7033 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7034 43012d58 2019-07-14 stsp NULL) == -1)
7035 43012d58 2019-07-14 stsp err(1, "pledge");
7036 43012d58 2019-07-14 stsp #endif
7037 17ed4618 2019-06-02 stsp if (argc < 1)
7038 648e4ef7 2019-07-09 stsp usage_remove();
7039 2ec1f75b 2019-03-26 stsp
7040 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
7041 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
7042 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7043 2ec1f75b 2019-03-26 stsp goto done;
7044 2ec1f75b 2019-03-26 stsp }
7045 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
7046 fa51e947 2020-03-27 stsp if (error) {
7047 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7048 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
7049 2ec1f75b 2019-03-26 stsp goto done;
7050 fa51e947 2020-03-27 stsp }
7051 2ec1f75b 2019-03-26 stsp
7052 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7053 c9956ddf 2019-09-08 stsp NULL);
7054 2af4a041 2019-05-11 jcs if (error)
7055 2ec1f75b 2019-03-26 stsp goto done;
7056 2ec1f75b 2019-03-26 stsp
7057 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7058 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7059 2ec1f75b 2019-03-26 stsp if (error)
7060 2ec1f75b 2019-03-26 stsp goto done;
7061 2ec1f75b 2019-03-26 stsp
7062 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7063 6d022e97 2019-08-04 stsp if (error)
7064 6d022e97 2019-08-04 stsp goto done;
7065 17ed4618 2019-06-02 stsp
7066 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
7067 f2a9dc41 2019-12-13 tracey char *ondisk_path;
7068 f2a9dc41 2019-12-13 tracey struct stat sb;
7069 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
7070 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
7071 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
7072 62d463ca 2020-10-20 naddy pe->path) == -1) {
7073 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
7074 f2a9dc41 2019-12-13 tracey goto done;
7075 f2a9dc41 2019-12-13 tracey }
7076 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
7077 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
7078 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7079 f2a9dc41 2019-12-13 tracey continue;
7080 f2a9dc41 2019-12-13 tracey }
7081 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
7082 f2a9dc41 2019-12-13 tracey ondisk_path);
7083 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7084 f2a9dc41 2019-12-13 tracey goto done;
7085 f2a9dc41 2019-12-13 tracey }
7086 f2a9dc41 2019-12-13 tracey free(ondisk_path);
7087 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
7088 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
7089 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
7090 f2a9dc41 2019-12-13 tracey goto done;
7091 f2a9dc41 2019-12-13 tracey }
7092 f2a9dc41 2019-12-13 tracey }
7093 f2a9dc41 2019-12-13 tracey }
7094 f2a9dc41 2019-12-13 tracey
7095 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
7096 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
7097 4e12cd97 2022-01-25 stsp repo, keep_on_disk, ignore_missing_paths);
7098 a129376b 2019-03-28 stsp done:
7099 1d0f4054 2021-06-17 stsp if (repo) {
7100 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7101 1d0f4054 2021-06-17 stsp if (error == NULL)
7102 1d0f4054 2021-06-17 stsp error = close_err;
7103 1d0f4054 2021-06-17 stsp }
7104 a129376b 2019-03-28 stsp if (worktree)
7105 a129376b 2019-03-28 stsp got_worktree_close(worktree);
7106 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
7107 17ed4618 2019-06-02 stsp free((char *)pe->path);
7108 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
7109 e9ce266e 2022-03-07 op free(cwd);
7110 e9ce266e 2022-03-07 op return error;
7111 e9ce266e 2022-03-07 op }
7112 e9ce266e 2022-03-07 op
7113 e9ce266e 2022-03-07 op __dead static void
7114 e9ce266e 2022-03-07 op usage_patch(void)
7115 e9ce266e 2022-03-07 op {
7116 e9ce266e 2022-03-07 op fprintf(stderr, "usage: %s patch [patchfile]\n",
7117 e9ce266e 2022-03-07 op getprogname());
7118 e9ce266e 2022-03-07 op exit(1);
7119 e9ce266e 2022-03-07 op }
7120 e9ce266e 2022-03-07 op
7121 e9ce266e 2022-03-07 op static const struct got_error *
7122 e9ce266e 2022-03-07 op patch_from_stdin(int *patchfd)
7123 e9ce266e 2022-03-07 op {
7124 e9ce266e 2022-03-07 op const struct got_error *err = NULL;
7125 e9ce266e 2022-03-07 op ssize_t r;
7126 e9ce266e 2022-03-07 op char *path, buf[BUFSIZ];
7127 e9ce266e 2022-03-07 op sig_t sighup, sigint, sigquit;
7128 e9ce266e 2022-03-07 op
7129 e9ce266e 2022-03-07 op err = got_opentemp_named_fd(&path, patchfd,
7130 e9ce266e 2022-03-07 op GOT_TMPDIR_STR "/got-patch");
7131 e9ce266e 2022-03-07 op if (err)
7132 e9ce266e 2022-03-07 op return err;
7133 e9ce266e 2022-03-07 op unlink(path);
7134 e9ce266e 2022-03-07 op free(path);
7135 e9ce266e 2022-03-07 op
7136 e9ce266e 2022-03-07 op sighup = signal(SIGHUP, SIG_DFL);
7137 e9ce266e 2022-03-07 op sigint = signal(SIGINT, SIG_DFL);
7138 e9ce266e 2022-03-07 op sigquit = signal(SIGQUIT, SIG_DFL);
7139 e9ce266e 2022-03-07 op
7140 e9ce266e 2022-03-07 op for (;;) {
7141 e9ce266e 2022-03-07 op r = read(0, buf, sizeof(buf));
7142 e9ce266e 2022-03-07 op if (r == -1) {
7143 e9ce266e 2022-03-07 op err = got_error_from_errno("read");
7144 e9ce266e 2022-03-07 op break;
7145 e9ce266e 2022-03-07 op }
7146 e9ce266e 2022-03-07 op if (r == 0)
7147 e9ce266e 2022-03-07 op break;
7148 e9ce266e 2022-03-07 op if (write(*patchfd, buf, r) == -1) {
7149 e9ce266e 2022-03-07 op err = got_error_from_errno("write");
7150 e9ce266e 2022-03-07 op break;
7151 e9ce266e 2022-03-07 op }
7152 e9ce266e 2022-03-07 op }
7153 e9ce266e 2022-03-07 op
7154 e9ce266e 2022-03-07 op signal(SIGHUP, sighup);
7155 e9ce266e 2022-03-07 op signal(SIGINT, sigint);
7156 e9ce266e 2022-03-07 op signal(SIGQUIT, sigquit);
7157 e9ce266e 2022-03-07 op
7158 e9ce266e 2022-03-07 op if (err != NULL)
7159 e9ce266e 2022-03-07 op close(*patchfd);
7160 e9ce266e 2022-03-07 op return NULL;
7161 e9ce266e 2022-03-07 op }
7162 e9ce266e 2022-03-07 op
7163 e9ce266e 2022-03-07 op static const struct got_error *
7164 e9ce266e 2022-03-07 op cmd_patch(int argc, char *argv[])
7165 e9ce266e 2022-03-07 op {
7166 e9ce266e 2022-03-07 op const struct got_error *error = NULL, *close_error = NULL;
7167 e9ce266e 2022-03-07 op struct got_worktree *worktree = NULL;
7168 e9ce266e 2022-03-07 op struct got_repository *repo = NULL;
7169 e9ce266e 2022-03-07 op char *cwd = NULL;
7170 e9ce266e 2022-03-07 op int ch;
7171 e9ce266e 2022-03-07 op int patchfd;
7172 e9ce266e 2022-03-07 op
7173 e9ce266e 2022-03-07 op while ((ch = getopt(argc, argv, "")) != -1) {
7174 e9ce266e 2022-03-07 op switch (ch) {
7175 e9ce266e 2022-03-07 op default:
7176 e9ce266e 2022-03-07 op usage_patch();
7177 e9ce266e 2022-03-07 op /* NOTREACHED */
7178 e9ce266e 2022-03-07 op }
7179 e9ce266e 2022-03-07 op }
7180 e9ce266e 2022-03-07 op
7181 e9ce266e 2022-03-07 op argc -= optind;
7182 e9ce266e 2022-03-07 op argv += optind;
7183 e9ce266e 2022-03-07 op
7184 e9ce266e 2022-03-07 op if (argc == 0) {
7185 e9ce266e 2022-03-07 op error = patch_from_stdin(&patchfd);
7186 e9ce266e 2022-03-07 op if (error)
7187 e9ce266e 2022-03-07 op return error;
7188 e9ce266e 2022-03-07 op } else if (argc == 1) {
7189 e9ce266e 2022-03-07 op patchfd = open(argv[0], O_RDONLY);
7190 e9ce266e 2022-03-07 op if (patchfd == -1) {
7191 e9ce266e 2022-03-07 op error = got_error_from_errno2("open", argv[0]);
7192 e9ce266e 2022-03-07 op return error;
7193 e9ce266e 2022-03-07 op }
7194 e9ce266e 2022-03-07 op } else
7195 e9ce266e 2022-03-07 op usage_patch();
7196 e9ce266e 2022-03-07 op
7197 e9ce266e 2022-03-07 op if ((cwd = getcwd(NULL, 0)) == NULL) {
7198 e9ce266e 2022-03-07 op error = got_error_from_errno("getcwd");
7199 e9ce266e 2022-03-07 op goto done;
7200 e9ce266e 2022-03-07 op }
7201 e9ce266e 2022-03-07 op
7202 e9ce266e 2022-03-07 op error = got_worktree_open(&worktree, cwd);
7203 e9ce266e 2022-03-07 op if (error != NULL)
7204 e9ce266e 2022-03-07 op goto done;
7205 e9ce266e 2022-03-07 op
7206 e9ce266e 2022-03-07 op const char *repo_path = got_worktree_get_repo_path(worktree);
7207 e9ce266e 2022-03-07 op error = got_repo_open(&repo, repo_path, NULL);
7208 e9ce266e 2022-03-07 op if (error != NULL)
7209 e9ce266e 2022-03-07 op goto done;
7210 e9ce266e 2022-03-07 op
7211 e9ce266e 2022-03-07 op error = apply_unveil(got_repo_get_path(repo), 0,
7212 e9ce266e 2022-03-07 op worktree ? got_worktree_get_root_path(worktree) : NULL);
7213 e9ce266e 2022-03-07 op if (error != NULL)
7214 e9ce266e 2022-03-07 op goto done;
7215 e9ce266e 2022-03-07 op
7216 e9ce266e 2022-03-07 op #ifndef PROFILE
7217 e9ce266e 2022-03-07 op if (pledge("stdio rpath wpath cpath proc exec sendfd flock",
7218 e9ce266e 2022-03-07 op NULL) == -1)
7219 e9ce266e 2022-03-07 op err(1, "pledge");
7220 e9ce266e 2022-03-07 op #endif
7221 e9ce266e 2022-03-07 op
7222 e9ce266e 2022-03-07 op error = got_patch(patchfd, worktree, repo, &print_remove_status,
7223 d955343d 2022-03-08 op NULL, &add_progress, NULL);
7224 e9ce266e 2022-03-07 op
7225 e9ce266e 2022-03-07 op done:
7226 e9ce266e 2022-03-07 op if (repo) {
7227 e9ce266e 2022-03-07 op close_error = got_repo_close(repo);
7228 e9ce266e 2022-03-07 op if (error == NULL)
7229 e9ce266e 2022-03-07 op error = close_error;
7230 e9ce266e 2022-03-07 op }
7231 e9ce266e 2022-03-07 op if (worktree != NULL) {
7232 e9ce266e 2022-03-07 op close_error = got_worktree_close(worktree);
7233 e9ce266e 2022-03-07 op if (error == NULL)
7234 e9ce266e 2022-03-07 op error = close_error;
7235 e9ce266e 2022-03-07 op }
7236 a129376b 2019-03-28 stsp free(cwd);
7237 a129376b 2019-03-28 stsp return error;
7238 a129376b 2019-03-28 stsp }
7239 a129376b 2019-03-28 stsp
7240 a129376b 2019-03-28 stsp __dead static void
7241 a129376b 2019-03-28 stsp usage_revert(void)
7242 a129376b 2019-03-28 stsp {
7243 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7244 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
7245 a129376b 2019-03-28 stsp exit(1);
7246 a129376b 2019-03-28 stsp }
7247 a129376b 2019-03-28 stsp
7248 1ee397ad 2019-07-12 stsp static const struct got_error *
7249 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
7250 a129376b 2019-03-28 stsp {
7251 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
7252 fb9704af 2020-01-27 stsp return NULL;
7253 fb9704af 2020-01-27 stsp
7254 a129376b 2019-03-28 stsp while (path[0] == '/')
7255 a129376b 2019-03-28 stsp path++;
7256 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
7257 33aa809d 2019-08-08 stsp return NULL;
7258 33aa809d 2019-08-08 stsp }
7259 33aa809d 2019-08-08 stsp
7260 33aa809d 2019-08-08 stsp struct choose_patch_arg {
7261 33aa809d 2019-08-08 stsp FILE *patch_script_file;
7262 33aa809d 2019-08-08 stsp const char *action;
7263 33aa809d 2019-08-08 stsp };
7264 33aa809d 2019-08-08 stsp
7265 33aa809d 2019-08-08 stsp static const struct got_error *
7266 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7267 33aa809d 2019-08-08 stsp int nchanges, const char *action)
7268 33aa809d 2019-08-08 stsp {
7269 33aa809d 2019-08-08 stsp char *line = NULL;
7270 33aa809d 2019-08-08 stsp size_t linesize = 0;
7271 33aa809d 2019-08-08 stsp ssize_t linelen;
7272 33aa809d 2019-08-08 stsp
7273 33aa809d 2019-08-08 stsp switch (status) {
7274 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
7275 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
7276 33aa809d 2019-08-08 stsp break;
7277 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
7278 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
7279 33aa809d 2019-08-08 stsp break;
7280 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
7281 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
7282 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
7283 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
7284 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7285 33aa809d 2019-08-08 stsp printf("%s", line);
7286 33aa809d 2019-08-08 stsp if (ferror(patch_file))
7287 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
7288 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
7289 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7290 33aa809d 2019-08-08 stsp path, n, nchanges, action);
7291 33aa809d 2019-08-08 stsp break;
7292 33aa809d 2019-08-08 stsp default:
7293 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
7294 33aa809d 2019-08-08 stsp }
7295 33aa809d 2019-08-08 stsp
7296 33aa809d 2019-08-08 stsp return NULL;
7297 33aa809d 2019-08-08 stsp }
7298 33aa809d 2019-08-08 stsp
7299 33aa809d 2019-08-08 stsp static const struct got_error *
7300 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7301 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
7302 33aa809d 2019-08-08 stsp {
7303 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
7304 33aa809d 2019-08-08 stsp char *line = NULL;
7305 33aa809d 2019-08-08 stsp size_t linesize = 0;
7306 33aa809d 2019-08-08 stsp ssize_t linelen;
7307 33aa809d 2019-08-08 stsp int resp = ' ';
7308 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
7309 33aa809d 2019-08-08 stsp
7310 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
7311 33aa809d 2019-08-08 stsp
7312 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
7313 33aa809d 2019-08-08 stsp char *nl;
7314 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
7315 33aa809d 2019-08-08 stsp a->action);
7316 33aa809d 2019-08-08 stsp if (err)
7317 33aa809d 2019-08-08 stsp return err;
7318 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
7319 33aa809d 2019-08-08 stsp if (linelen == -1) {
7320 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
7321 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
7322 33aa809d 2019-08-08 stsp return NULL;
7323 33aa809d 2019-08-08 stsp }
7324 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
7325 33aa809d 2019-08-08 stsp if (nl)
7326 33aa809d 2019-08-08 stsp *nl = '\0';
7327 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
7328 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
7329 33aa809d 2019-08-08 stsp printf("y\n");
7330 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
7331 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
7332 33aa809d 2019-08-08 stsp printf("n\n");
7333 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
7334 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
7335 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
7336 33aa809d 2019-08-08 stsp printf("q\n");
7337 33aa809d 2019-08-08 stsp } else
7338 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
7339 33aa809d 2019-08-08 stsp free(line);
7340 33aa809d 2019-08-08 stsp return NULL;
7341 33aa809d 2019-08-08 stsp }
7342 33aa809d 2019-08-08 stsp
7343 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
7344 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
7345 33aa809d 2019-08-08 stsp a->action);
7346 33aa809d 2019-08-08 stsp if (err)
7347 33aa809d 2019-08-08 stsp return err;
7348 33aa809d 2019-08-08 stsp resp = getchar();
7349 33aa809d 2019-08-08 stsp if (resp == '\n')
7350 33aa809d 2019-08-08 stsp resp = getchar();
7351 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
7352 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
7353 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7354 33aa809d 2019-08-08 stsp resp = ' ';
7355 33aa809d 2019-08-08 stsp }
7356 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
7357 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
7358 33aa809d 2019-08-08 stsp resp = ' ';
7359 33aa809d 2019-08-08 stsp }
7360 33aa809d 2019-08-08 stsp }
7361 33aa809d 2019-08-08 stsp
7362 33aa809d 2019-08-08 stsp if (resp == 'y')
7363 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
7364 33aa809d 2019-08-08 stsp else if (resp == 'n')
7365 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
7366 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7367 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
7368 33aa809d 2019-08-08 stsp
7369 1ee397ad 2019-07-12 stsp return NULL;
7370 a129376b 2019-03-28 stsp }
7371 a129376b 2019-03-28 stsp
7372 a129376b 2019-03-28 stsp static const struct got_error *
7373 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
7374 a129376b 2019-03-28 stsp {
7375 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
7376 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
7377 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
7378 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
7379 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
7380 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
7381 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
7382 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
7383 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
7384 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
7385 a129376b 2019-03-28 stsp
7386 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
7387 e20a8b6f 2019-06-04 stsp
7388 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7389 a129376b 2019-03-28 stsp switch (ch) {
7390 33aa809d 2019-08-08 stsp case 'p':
7391 33aa809d 2019-08-08 stsp pflag = 1;
7392 33aa809d 2019-08-08 stsp break;
7393 33aa809d 2019-08-08 stsp case 'F':
7394 33aa809d 2019-08-08 stsp patch_script_path = optarg;
7395 33aa809d 2019-08-08 stsp break;
7396 0f6d7415 2019-08-08 stsp case 'R':
7397 0f6d7415 2019-08-08 stsp can_recurse = 1;
7398 0f6d7415 2019-08-08 stsp break;
7399 a129376b 2019-03-28 stsp default:
7400 a129376b 2019-03-28 stsp usage_revert();
7401 a129376b 2019-03-28 stsp /* NOTREACHED */
7402 a129376b 2019-03-28 stsp }
7403 a129376b 2019-03-28 stsp }
7404 a129376b 2019-03-28 stsp
7405 a129376b 2019-03-28 stsp argc -= optind;
7406 a129376b 2019-03-28 stsp argv += optind;
7407 a129376b 2019-03-28 stsp
7408 43012d58 2019-07-14 stsp #ifndef PROFILE
7409 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7410 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7411 43012d58 2019-07-14 stsp err(1, "pledge");
7412 43012d58 2019-07-14 stsp #endif
7413 e20a8b6f 2019-06-04 stsp if (argc < 1)
7414 a129376b 2019-03-28 stsp usage_revert();
7415 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
7416 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7417 a129376b 2019-03-28 stsp
7418 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
7419 a129376b 2019-03-28 stsp if (cwd == NULL) {
7420 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7421 a129376b 2019-03-28 stsp goto done;
7422 a129376b 2019-03-28 stsp }
7423 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
7424 fa51e947 2020-03-27 stsp if (error) {
7425 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7426 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
7427 2ec1f75b 2019-03-26 stsp goto done;
7428 fa51e947 2020-03-27 stsp }
7429 a129376b 2019-03-28 stsp
7430 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7431 c9956ddf 2019-09-08 stsp NULL);
7432 a129376b 2019-03-28 stsp if (error != NULL)
7433 a129376b 2019-03-28 stsp goto done;
7434 a129376b 2019-03-28 stsp
7435 33aa809d 2019-08-08 stsp if (patch_script_path) {
7436 00fe21f2 2021-12-31 stsp patch_script_file = fopen(patch_script_path, "re");
7437 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
7438 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
7439 33aa809d 2019-08-08 stsp patch_script_path);
7440 33aa809d 2019-08-08 stsp goto done;
7441 33aa809d 2019-08-08 stsp }
7442 33aa809d 2019-08-08 stsp }
7443 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7444 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7445 a129376b 2019-03-28 stsp if (error)
7446 a129376b 2019-03-28 stsp goto done;
7447 a129376b 2019-03-28 stsp
7448 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7449 6d022e97 2019-08-04 stsp if (error)
7450 6d022e97 2019-08-04 stsp goto done;
7451 00db391e 2019-08-03 stsp
7452 0f6d7415 2019-08-08 stsp if (!can_recurse) {
7453 0f6d7415 2019-08-08 stsp char *ondisk_path;
7454 0f6d7415 2019-08-08 stsp struct stat sb;
7455 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
7456 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
7457 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
7458 62d463ca 2020-10-20 naddy pe->path) == -1) {
7459 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
7460 0f6d7415 2019-08-08 stsp goto done;
7461 0f6d7415 2019-08-08 stsp }
7462 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
7463 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
7464 0f6d7415 2019-08-08 stsp free(ondisk_path);
7465 0f6d7415 2019-08-08 stsp continue;
7466 0f6d7415 2019-08-08 stsp }
7467 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
7468 0f6d7415 2019-08-08 stsp ondisk_path);
7469 0f6d7415 2019-08-08 stsp free(ondisk_path);
7470 0f6d7415 2019-08-08 stsp goto done;
7471 0f6d7415 2019-08-08 stsp }
7472 0f6d7415 2019-08-08 stsp free(ondisk_path);
7473 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
7474 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7475 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
7476 0f6d7415 2019-08-08 stsp goto done;
7477 0f6d7415 2019-08-08 stsp }
7478 0f6d7415 2019-08-08 stsp }
7479 0f6d7415 2019-08-08 stsp }
7480 0f6d7415 2019-08-08 stsp
7481 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7482 33aa809d 2019-08-08 stsp cpa.action = "revert";
7483 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7484 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7485 2ec1f75b 2019-03-26 stsp done:
7486 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7487 33aa809d 2019-08-08 stsp error == NULL)
7488 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7489 1d0f4054 2021-06-17 stsp if (repo) {
7490 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7491 1d0f4054 2021-06-17 stsp if (error == NULL)
7492 1d0f4054 2021-06-17 stsp error = close_err;
7493 1d0f4054 2021-06-17 stsp }
7494 2ec1f75b 2019-03-26 stsp if (worktree)
7495 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
7496 2ec1f75b 2019-03-26 stsp free(path);
7497 d00136be 2019-03-26 stsp free(cwd);
7498 6bad629b 2019-02-04 stsp return error;
7499 c4296144 2019-05-09 stsp }
7500 c4296144 2019-05-09 stsp
7501 c4296144 2019-05-09 stsp __dead static void
7502 c4296144 2019-05-09 stsp usage_commit(void)
7503 c4296144 2019-05-09 stsp {
7504 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7505 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
7506 c4296144 2019-05-09 stsp exit(1);
7507 33ad4cbe 2019-05-12 jcs }
7508 33ad4cbe 2019-05-12 jcs
7509 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
7510 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
7511 28cf319f 2021-01-28 stsp const char *prepared_log;
7512 28cf319f 2021-01-28 stsp int non_interactive;
7513 e2ba3d07 2019-05-13 stsp const char *editor;
7514 e0870e44 2019-05-13 stsp const char *worktree_path;
7515 76d98825 2019-06-03 stsp const char *branch_name;
7516 314a6357 2019-05-13 stsp const char *repo_path;
7517 e0870e44 2019-05-13 stsp char *logmsg_path;
7518 e2ba3d07 2019-05-13 stsp
7519 e2ba3d07 2019-05-13 stsp };
7520 28cf319f 2021-01-28 stsp
7521 28cf319f 2021-01-28 stsp static const struct got_error *
7522 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
7523 28cf319f 2021-01-28 stsp {
7524 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
7525 28cf319f 2021-01-28 stsp FILE *f = NULL;
7526 28cf319f 2021-01-28 stsp struct stat sb;
7527 28cf319f 2021-01-28 stsp size_t r;
7528 28cf319f 2021-01-28 stsp
7529 28cf319f 2021-01-28 stsp *logmsg = NULL;
7530 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7531 28cf319f 2021-01-28 stsp
7532 00fe21f2 2021-12-31 stsp f = fopen(path, "re");
7533 28cf319f 2021-01-28 stsp if (f == NULL)
7534 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
7535 28cf319f 2021-01-28 stsp
7536 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
7537 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
7538 28cf319f 2021-01-28 stsp goto done;
7539 28cf319f 2021-01-28 stsp }
7540 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
7541 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7542 28cf319f 2021-01-28 stsp goto done;
7543 28cf319f 2021-01-28 stsp }
7544 28cf319f 2021-01-28 stsp
7545 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
7546 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
7547 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
7548 28cf319f 2021-01-28 stsp goto done;
7549 28cf319f 2021-01-28 stsp }
7550 28cf319f 2021-01-28 stsp
7551 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7552 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7553 28cf319f 2021-01-28 stsp if (ferror(f))
7554 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7555 28cf319f 2021-01-28 stsp else
7556 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7557 28cf319f 2021-01-28 stsp goto done;
7558 28cf319f 2021-01-28 stsp }
7559 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7560 28cf319f 2021-01-28 stsp done:
7561 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7562 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7563 28cf319f 2021-01-28 stsp if (err) {
7564 28cf319f 2021-01-28 stsp free(*logmsg);
7565 28cf319f 2021-01-28 stsp *logmsg = NULL;
7566 28cf319f 2021-01-28 stsp }
7567 28cf319f 2021-01-28 stsp return err;
7568 28cf319f 2021-01-28 stsp
7569 28cf319f 2021-01-28 stsp }
7570 e0870e44 2019-05-13 stsp
7571 33ad4cbe 2019-05-12 jcs static const struct got_error *
7572 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7573 33ad4cbe 2019-05-12 jcs void *arg)
7574 33ad4cbe 2019-05-12 jcs {
7575 76d98825 2019-06-03 stsp char *initial_content = NULL;
7576 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7577 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7578 e0870e44 2019-05-13 stsp char *template = NULL;
7579 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7580 1601cb9f 2020-09-11 naddy int initial_content_len;
7581 97972933 2020-09-11 stsp int fd = -1;
7582 33ad4cbe 2019-05-12 jcs size_t len;
7583 33ad4cbe 2019-05-12 jcs
7584 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7585 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7586 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7587 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7588 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7589 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7590 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7591 33ad4cbe 2019-05-12 jcs return NULL;
7592 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7593 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7594 33ad4cbe 2019-05-12 jcs
7595 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7596 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7597 76d98825 2019-06-03 stsp
7598 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7599 28cf319f 2021-01-28 stsp if (err)
7600 28cf319f 2021-01-28 stsp goto done;
7601 28cf319f 2021-01-28 stsp
7602 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7603 28cf319f 2021-01-28 stsp char *msg;
7604 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7605 28cf319f 2021-01-28 stsp if (err)
7606 28cf319f 2021-01-28 stsp goto done;
7607 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7608 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7609 28cf319f 2021-01-28 stsp free(msg);
7610 28cf319f 2021-01-28 stsp goto done;
7611 28cf319f 2021-01-28 stsp }
7612 28cf319f 2021-01-28 stsp free(msg);
7613 28cf319f 2021-01-28 stsp }
7614 28cf319f 2021-01-28 stsp
7615 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7616 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7617 1601cb9f 2020-09-11 naddy a->branch_name);
7618 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7619 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7620 e0870e44 2019-05-13 stsp goto done;
7621 28cf319f 2021-01-28 stsp }
7622 33ad4cbe 2019-05-12 jcs
7623 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7624 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7625 97972933 2020-09-11 stsp goto done;
7626 97972933 2020-09-11 stsp }
7627 33ad4cbe 2019-05-12 jcs
7628 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7629 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7630 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7631 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7632 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7633 33ad4cbe 2019-05-12 jcs }
7634 33ad4cbe 2019-05-12 jcs
7635 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7636 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7637 33ad4cbe 2019-05-12 jcs done:
7638 76d98825 2019-06-03 stsp free(initial_content);
7639 e0870e44 2019-05-13 stsp free(template);
7640 314a6357 2019-05-13 stsp
7641 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7642 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7643 97972933 2020-09-11 stsp
7644 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7645 59f86c76 2020-09-11 stsp if (err == NULL)
7646 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7647 59f86c76 2020-09-11 stsp if (err) {
7648 59f86c76 2020-09-11 stsp free(*logmsg);
7649 59f86c76 2020-09-11 stsp *logmsg = NULL;
7650 3ce1b845 2019-07-15 stsp }
7651 33ad4cbe 2019-05-12 jcs return err;
7652 6bad629b 2019-02-04 stsp }
7653 c4296144 2019-05-09 stsp
7654 c4296144 2019-05-09 stsp static const struct got_error *
7655 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7656 c4296144 2019-05-09 stsp {
7657 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7658 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7659 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7660 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7661 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7662 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7663 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7664 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7665 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7666 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7667 f259c4c1 2021-09-24 stsp int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
7668 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7669 5c1e53bc 2019-07-28 stsp
7670 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7671 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7672 c4296144 2019-05-09 stsp
7673 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7674 c4296144 2019-05-09 stsp switch (ch) {
7675 28cf319f 2021-01-28 stsp case 'F':
7676 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7677 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7678 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7679 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7680 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7681 28cf319f 2021-01-28 stsp optarg);
7682 28cf319f 2021-01-28 stsp break;
7683 c4296144 2019-05-09 stsp case 'm':
7684 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7685 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7686 c4296144 2019-05-09 stsp logmsg = optarg;
7687 28cf319f 2021-01-28 stsp break;
7688 28cf319f 2021-01-28 stsp case 'N':
7689 28cf319f 2021-01-28 stsp non_interactive = 1;
7690 c4296144 2019-05-09 stsp break;
7691 35213c7c 2020-07-23 stsp case 'S':
7692 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7693 35213c7c 2020-07-23 stsp break;
7694 c4296144 2019-05-09 stsp default:
7695 c4296144 2019-05-09 stsp usage_commit();
7696 c4296144 2019-05-09 stsp /* NOTREACHED */
7697 c4296144 2019-05-09 stsp }
7698 c4296144 2019-05-09 stsp }
7699 c4296144 2019-05-09 stsp
7700 c4296144 2019-05-09 stsp argc -= optind;
7701 c4296144 2019-05-09 stsp argv += optind;
7702 c4296144 2019-05-09 stsp
7703 43012d58 2019-07-14 stsp #ifndef PROFILE
7704 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7705 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7706 43012d58 2019-07-14 stsp err(1, "pledge");
7707 43012d58 2019-07-14 stsp #endif
7708 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7709 c4296144 2019-05-09 stsp if (cwd == NULL) {
7710 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7711 c4296144 2019-05-09 stsp goto done;
7712 c4296144 2019-05-09 stsp }
7713 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7714 fa51e947 2020-03-27 stsp if (error) {
7715 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7716 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7717 7d5807f4 2019-07-11 stsp goto done;
7718 fa51e947 2020-03-27 stsp }
7719 7d5807f4 2019-07-11 stsp
7720 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7721 c4296144 2019-05-09 stsp if (error)
7722 a698f62e 2019-07-25 stsp goto done;
7723 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7724 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7725 c4296144 2019-05-09 stsp goto done;
7726 a698f62e 2019-07-25 stsp }
7727 5c1e53bc 2019-07-28 stsp
7728 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7729 916f288c 2019-07-30 stsp worktree);
7730 5c1e53bc 2019-07-28 stsp if (error)
7731 5c1e53bc 2019-07-28 stsp goto done;
7732 c4296144 2019-05-09 stsp
7733 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7734 c9956ddf 2019-09-08 stsp if (error)
7735 c9956ddf 2019-09-08 stsp goto done;
7736 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7737 c9956ddf 2019-09-08 stsp gitconfig_path);
7738 c4296144 2019-05-09 stsp if (error != NULL)
7739 c4296144 2019-05-09 stsp goto done;
7740 c4296144 2019-05-09 stsp
7741 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
7742 f259c4c1 2021-09-24 stsp if (error)
7743 f259c4c1 2021-09-24 stsp goto done;
7744 f259c4c1 2021-09-24 stsp if (merge_in_progress) {
7745 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_MERGE_BUSY);
7746 f259c4c1 2021-09-24 stsp goto done;
7747 f259c4c1 2021-09-24 stsp }
7748 f259c4c1 2021-09-24 stsp
7749 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7750 aba9c984 2019-09-08 stsp if (error)
7751 aba9c984 2019-09-08 stsp return error;
7752 aba9c984 2019-09-08 stsp
7753 314a6357 2019-05-13 stsp /*
7754 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7755 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7756 314a6357 2019-05-13 stsp */
7757 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7758 314a6357 2019-05-13 stsp error = get_editor(&editor);
7759 314a6357 2019-05-13 stsp else
7760 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7761 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7762 c4296144 2019-05-09 stsp if (error)
7763 c4296144 2019-05-09 stsp goto done;
7764 c4296144 2019-05-09 stsp
7765 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7766 087fb88c 2019-08-04 stsp if (error)
7767 087fb88c 2019-08-04 stsp goto done;
7768 087fb88c 2019-08-04 stsp
7769 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7770 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7771 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7772 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7773 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7774 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7775 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7776 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7777 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7778 916f288c 2019-07-30 stsp goto done;
7779 916f288c 2019-07-30 stsp }
7780 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7781 916f288c 2019-07-30 stsp }
7782 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7783 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7784 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7785 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7786 e0870e44 2019-05-13 stsp if (error) {
7787 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7788 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7789 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7790 c4296144 2019-05-09 stsp goto done;
7791 e0870e44 2019-05-13 stsp }
7792 c4296144 2019-05-09 stsp
7793 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7794 c4296144 2019-05-09 stsp if (error)
7795 c4296144 2019-05-09 stsp goto done;
7796 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7797 c4296144 2019-05-09 stsp done:
7798 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7799 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7800 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7801 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7802 7266f21f 2019-10-21 stsp error == NULL)
7803 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7804 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7805 1d0f4054 2021-06-17 stsp if (repo) {
7806 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7807 1d0f4054 2021-06-17 stsp if (error == NULL)
7808 1d0f4054 2021-06-17 stsp error = close_err;
7809 1d0f4054 2021-06-17 stsp }
7810 c4296144 2019-05-09 stsp if (worktree)
7811 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7812 c4296144 2019-05-09 stsp free(cwd);
7813 c4296144 2019-05-09 stsp free(id_str);
7814 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7815 e2ba3d07 2019-05-13 stsp free(editor);
7816 aba9c984 2019-09-08 stsp free(author);
7817 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7818 f8a36e22 2021-08-26 stsp return error;
7819 f8a36e22 2021-08-26 stsp }
7820 f8a36e22 2021-08-26 stsp
7821 f8a36e22 2021-08-26 stsp __dead static void
7822 f8a36e22 2021-08-26 stsp usage_send(void)
7823 f8a36e22 2021-08-26 stsp {
7824 f8a36e22 2021-08-26 stsp fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7825 f8a36e22 2021-08-26 stsp "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7826 f8a36e22 2021-08-26 stsp "[remote-repository]\n", getprogname());
7827 f8a36e22 2021-08-26 stsp exit(1);
7828 f8a36e22 2021-08-26 stsp }
7829 f8a36e22 2021-08-26 stsp
7830 f8a36e22 2021-08-26 stsp struct got_send_progress_arg {
7831 f8a36e22 2021-08-26 stsp char last_scaled_packsize[FMT_SCALED_STRSIZE];
7832 f8a36e22 2021-08-26 stsp int verbosity;
7833 f8a36e22 2021-08-26 stsp int last_ncommits;
7834 f8a36e22 2021-08-26 stsp int last_nobj_total;
7835 f8a36e22 2021-08-26 stsp int last_p_deltify;
7836 f8a36e22 2021-08-26 stsp int last_p_written;
7837 f8a36e22 2021-08-26 stsp int last_p_sent;
7838 f8a36e22 2021-08-26 stsp int printed_something;
7839 f8a36e22 2021-08-26 stsp int sent_something;
7840 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches;
7841 f8a36e22 2021-08-26 stsp };
7842 f8a36e22 2021-08-26 stsp
7843 f8a36e22 2021-08-26 stsp static const struct got_error *
7844 f8a36e22 2021-08-26 stsp send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7845 f8a36e22 2021-08-26 stsp int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7846 f8a36e22 2021-08-26 stsp int success)
7847 f8a36e22 2021-08-26 stsp {
7848 f8a36e22 2021-08-26 stsp struct got_send_progress_arg *a = arg;
7849 f8a36e22 2021-08-26 stsp char scaled_packsize[FMT_SCALED_STRSIZE];
7850 f8a36e22 2021-08-26 stsp char scaled_sent[FMT_SCALED_STRSIZE];
7851 f8a36e22 2021-08-26 stsp int p_deltify = 0, p_written = 0, p_sent = 0;
7852 f8a36e22 2021-08-26 stsp int print_searching = 0, print_total = 0;
7853 f8a36e22 2021-08-26 stsp int print_deltify = 0, print_written = 0, print_sent = 0;
7854 f8a36e22 2021-08-26 stsp
7855 f8a36e22 2021-08-26 stsp if (a->verbosity < 0)
7856 f8a36e22 2021-08-26 stsp return NULL;
7857 f8a36e22 2021-08-26 stsp
7858 f8a36e22 2021-08-26 stsp if (refname) {
7859 f8a36e22 2021-08-26 stsp const char *status = success ? "accepted" : "rejected";
7860 f8a36e22 2021-08-26 stsp
7861 f8a36e22 2021-08-26 stsp if (success) {
7862 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7863 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, a->delete_branches, entry) {
7864 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7865 f8a36e22 2021-08-26 stsp if (got_path_cmp(branchname, refname,
7866 f8a36e22 2021-08-26 stsp strlen(branchname), strlen(refname)) == 0) {
7867 f8a36e22 2021-08-26 stsp status = "deleted";
7868 27b75514 2021-08-28 stsp a->sent_something = 1;
7869 f8a36e22 2021-08-26 stsp break;
7870 f8a36e22 2021-08-26 stsp }
7871 f8a36e22 2021-08-26 stsp }
7872 f8a36e22 2021-08-26 stsp }
7873 f8a36e22 2021-08-26 stsp
7874 27b75514 2021-08-28 stsp if (a->printed_something)
7875 27b75514 2021-08-28 stsp putchar('\n');
7876 27b75514 2021-08-28 stsp printf("Server has %s %s", status, refname);
7877 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7878 f8a36e22 2021-08-26 stsp return NULL;
7879 f8a36e22 2021-08-26 stsp }
7880 f8a36e22 2021-08-26 stsp
7881 f8a36e22 2021-08-26 stsp if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7882 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7883 f8a36e22 2021-08-26 stsp if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7884 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7885 f8a36e22 2021-08-26 stsp
7886 f8a36e22 2021-08-26 stsp if (a->last_ncommits != ncommits) {
7887 f8a36e22 2021-08-26 stsp print_searching = 1;
7888 f8a36e22 2021-08-26 stsp a->last_ncommits = ncommits;
7889 f8a36e22 2021-08-26 stsp }
7890 f8a36e22 2021-08-26 stsp
7891 f8a36e22 2021-08-26 stsp if (a->last_nobj_total != nobj_total) {
7892 f8a36e22 2021-08-26 stsp print_searching = 1;
7893 f8a36e22 2021-08-26 stsp print_total = 1;
7894 f8a36e22 2021-08-26 stsp a->last_nobj_total = nobj_total;
7895 f8a36e22 2021-08-26 stsp }
7896 f8a36e22 2021-08-26 stsp
7897 f8a36e22 2021-08-26 stsp if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7898 f8a36e22 2021-08-26 stsp strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7899 f8a36e22 2021-08-26 stsp if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7900 f8a36e22 2021-08-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7901 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
7902 f8a36e22 2021-08-26 stsp }
7903 f8a36e22 2021-08-26 stsp
7904 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0 || nobj_written > 0) {
7905 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0) {
7906 f8a36e22 2021-08-26 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
7907 f8a36e22 2021-08-26 stsp if (p_deltify != a->last_p_deltify) {
7908 f8a36e22 2021-08-26 stsp a->last_p_deltify = p_deltify;
7909 f8a36e22 2021-08-26 stsp print_searching = 1;
7910 f8a36e22 2021-08-26 stsp print_total = 1;
7911 f8a36e22 2021-08-26 stsp print_deltify = 1;
7912 f8a36e22 2021-08-26 stsp }
7913 f8a36e22 2021-08-26 stsp }
7914 f8a36e22 2021-08-26 stsp if (nobj_written > 0) {
7915 f8a36e22 2021-08-26 stsp p_written = (nobj_written * 100) / nobj_total;
7916 f8a36e22 2021-08-26 stsp if (p_written != a->last_p_written) {
7917 f8a36e22 2021-08-26 stsp a->last_p_written = p_written;
7918 f8a36e22 2021-08-26 stsp print_searching = 1;
7919 f8a36e22 2021-08-26 stsp print_total = 1;
7920 f8a36e22 2021-08-26 stsp print_deltify = 1;
7921 f8a36e22 2021-08-26 stsp print_written = 1;
7922 f8a36e22 2021-08-26 stsp }
7923 f8a36e22 2021-08-26 stsp }
7924 f8a36e22 2021-08-26 stsp }
7925 f8a36e22 2021-08-26 stsp
7926 f8a36e22 2021-08-26 stsp if (bytes_sent > 0) {
7927 f8a36e22 2021-08-26 stsp p_sent = (bytes_sent * 100) / packfile_size;
7928 f8a36e22 2021-08-26 stsp if (p_sent != a->last_p_sent) {
7929 f8a36e22 2021-08-26 stsp a->last_p_sent = p_sent;
7930 f8a36e22 2021-08-26 stsp print_searching = 1;
7931 f8a36e22 2021-08-26 stsp print_total = 1;
7932 f8a36e22 2021-08-26 stsp print_deltify = 1;
7933 f8a36e22 2021-08-26 stsp print_written = 1;
7934 f8a36e22 2021-08-26 stsp print_sent = 1;
7935 f8a36e22 2021-08-26 stsp }
7936 f8a36e22 2021-08-26 stsp a->sent_something = 1;
7937 f8a36e22 2021-08-26 stsp }
7938 f8a36e22 2021-08-26 stsp
7939 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify || print_written ||
7940 f8a36e22 2021-08-26 stsp print_sent)
7941 f8a36e22 2021-08-26 stsp printf("\r");
7942 f8a36e22 2021-08-26 stsp if (print_searching)
7943 f8a36e22 2021-08-26 stsp printf("packing %d reference%s", ncommits,
7944 f8a36e22 2021-08-26 stsp ncommits == 1 ? "" : "s");
7945 f8a36e22 2021-08-26 stsp if (print_total)
7946 f8a36e22 2021-08-26 stsp printf("; %d object%s", nobj_total,
7947 f8a36e22 2021-08-26 stsp nobj_total == 1 ? "" : "s");
7948 f8a36e22 2021-08-26 stsp if (print_deltify)
7949 f8a36e22 2021-08-26 stsp printf("; deltify: %d%%", p_deltify);
7950 f8a36e22 2021-08-26 stsp if (print_sent)
7951 b5934965 2022-02-12 naddy printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
7952 f8a36e22 2021-08-26 stsp scaled_packsize, p_sent);
7953 f8a36e22 2021-08-26 stsp else if (print_written)
7954 b5934965 2022-02-12 naddy printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
7955 f8a36e22 2021-08-26 stsp scaled_packsize, p_written);
7956 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify ||
7957 f8a36e22 2021-08-26 stsp print_written || print_sent) {
7958 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7959 f8a36e22 2021-08-26 stsp fflush(stdout);
7960 f8a36e22 2021-08-26 stsp }
7961 f8a36e22 2021-08-26 stsp return NULL;
7962 f8a36e22 2021-08-26 stsp }
7963 f8a36e22 2021-08-26 stsp
7964 f8a36e22 2021-08-26 stsp static const struct got_error *
7965 f8a36e22 2021-08-26 stsp cmd_send(int argc, char *argv[])
7966 f8a36e22 2021-08-26 stsp {
7967 f8a36e22 2021-08-26 stsp const struct got_error *error = NULL;
7968 f8a36e22 2021-08-26 stsp char *cwd = NULL, *repo_path = NULL;
7969 f8a36e22 2021-08-26 stsp const char *remote_name;
7970 f8a36e22 2021-08-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
7971 f8a36e22 2021-08-26 stsp char *repo_name = NULL, *server_path = NULL;
7972 f8a36e22 2021-08-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
7973 f8a36e22 2021-08-26 stsp int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7974 f8a36e22 2021-08-26 stsp struct got_repository *repo = NULL;
7975 f8a36e22 2021-08-26 stsp struct got_worktree *worktree = NULL;
7976 f8a36e22 2021-08-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7977 f8a36e22 2021-08-26 stsp struct got_pathlist_head branches;
7978 f8a36e22 2021-08-26 stsp struct got_pathlist_head tags;
7979 f8a36e22 2021-08-26 stsp struct got_reflist_head all_branches;
7980 f8a36e22 2021-08-26 stsp struct got_reflist_head all_tags;
7981 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_args;
7982 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_branches;
7983 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
7984 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7985 f8a36e22 2021-08-26 stsp int i, ch, sendfd = -1, sendstatus;
7986 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
7987 f8a36e22 2021-08-26 stsp struct got_send_progress_arg spa;
7988 f8a36e22 2021-08-26 stsp int verbosity = 0, overwrite_refs = 0;
7989 f8a36e22 2021-08-26 stsp int send_all_branches = 0, send_all_tags = 0;
7990 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
7991 f8a36e22 2021-08-26 stsp
7992 f8a36e22 2021-08-26 stsp TAILQ_INIT(&branches);
7993 f8a36e22 2021-08-26 stsp TAILQ_INIT(&tags);
7994 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_branches);
7995 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_tags);
7996 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_args);
7997 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_branches);
7998 f8a36e22 2021-08-26 stsp
7999 f8a36e22 2021-08-26 stsp while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8000 f8a36e22 2021-08-26 stsp switch (ch) {
8001 f8a36e22 2021-08-26 stsp case 'a':
8002 f8a36e22 2021-08-26 stsp send_all_branches = 1;
8003 f8a36e22 2021-08-26 stsp break;
8004 f8a36e22 2021-08-26 stsp case 'b':
8005 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, optarg, NULL);
8006 f8a36e22 2021-08-26 stsp if (error)
8007 f8a36e22 2021-08-26 stsp return error;
8008 f8a36e22 2021-08-26 stsp nbranches++;
8009 f8a36e22 2021-08-26 stsp break;
8010 f8a36e22 2021-08-26 stsp case 'd':
8011 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&delete_args, optarg, NULL);
8012 f8a36e22 2021-08-26 stsp if (error)
8013 f8a36e22 2021-08-26 stsp return error;
8014 f8a36e22 2021-08-26 stsp break;
8015 f8a36e22 2021-08-26 stsp case 'f':
8016 f8a36e22 2021-08-26 stsp overwrite_refs = 1;
8017 f8a36e22 2021-08-26 stsp break;
8018 f8a36e22 2021-08-26 stsp case 'r':
8019 f8a36e22 2021-08-26 stsp repo_path = realpath(optarg, NULL);
8020 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
8021 f8a36e22 2021-08-26 stsp return got_error_from_errno2("realpath",
8022 f8a36e22 2021-08-26 stsp optarg);
8023 f8a36e22 2021-08-26 stsp got_path_strip_trailing_slashes(repo_path);
8024 f8a36e22 2021-08-26 stsp break;
8025 f8a36e22 2021-08-26 stsp case 't':
8026 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags, optarg, NULL);
8027 f8a36e22 2021-08-26 stsp if (error)
8028 f8a36e22 2021-08-26 stsp return error;
8029 f8a36e22 2021-08-26 stsp ntags++;
8030 f8a36e22 2021-08-26 stsp break;
8031 f8a36e22 2021-08-26 stsp case 'T':
8032 f8a36e22 2021-08-26 stsp send_all_tags = 1;
8033 f8a36e22 2021-08-26 stsp break;
8034 f8a36e22 2021-08-26 stsp case 'v':
8035 f8a36e22 2021-08-26 stsp if (verbosity < 0)
8036 f8a36e22 2021-08-26 stsp verbosity = 0;
8037 f8a36e22 2021-08-26 stsp else if (verbosity < 3)
8038 f8a36e22 2021-08-26 stsp verbosity++;
8039 f8a36e22 2021-08-26 stsp break;
8040 f8a36e22 2021-08-26 stsp case 'q':
8041 f8a36e22 2021-08-26 stsp verbosity = -1;
8042 f8a36e22 2021-08-26 stsp break;
8043 f8a36e22 2021-08-26 stsp default:
8044 f8a36e22 2021-08-26 stsp usage_send();
8045 f8a36e22 2021-08-26 stsp /* NOTREACHED */
8046 f8a36e22 2021-08-26 stsp }
8047 f8a36e22 2021-08-26 stsp }
8048 f8a36e22 2021-08-26 stsp argc -= optind;
8049 f8a36e22 2021-08-26 stsp argv += optind;
8050 f8a36e22 2021-08-26 stsp
8051 f8a36e22 2021-08-26 stsp if (send_all_branches && !TAILQ_EMPTY(&branches))
8052 f8a36e22 2021-08-26 stsp option_conflict('a', 'b');
8053 f8a36e22 2021-08-26 stsp if (send_all_tags && !TAILQ_EMPTY(&tags))
8054 f8a36e22 2021-08-26 stsp option_conflict('T', 't');
8055 f8a36e22 2021-08-26 stsp
8056 f8a36e22 2021-08-26 stsp
8057 f8a36e22 2021-08-26 stsp if (argc == 0)
8058 f8a36e22 2021-08-26 stsp remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8059 f8a36e22 2021-08-26 stsp else if (argc == 1)
8060 f8a36e22 2021-08-26 stsp remote_name = argv[0];
8061 f8a36e22 2021-08-26 stsp else
8062 f8a36e22 2021-08-26 stsp usage_send();
8063 f8a36e22 2021-08-26 stsp
8064 f8a36e22 2021-08-26 stsp cwd = getcwd(NULL, 0);
8065 f8a36e22 2021-08-26 stsp if (cwd == NULL) {
8066 f8a36e22 2021-08-26 stsp error = got_error_from_errno("getcwd");
8067 f8a36e22 2021-08-26 stsp goto done;
8068 f8a36e22 2021-08-26 stsp }
8069 f8a36e22 2021-08-26 stsp
8070 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
8071 f8a36e22 2021-08-26 stsp error = got_worktree_open(&worktree, cwd);
8072 f8a36e22 2021-08-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8073 f8a36e22 2021-08-26 stsp goto done;
8074 f8a36e22 2021-08-26 stsp else
8075 f8a36e22 2021-08-26 stsp error = NULL;
8076 f8a36e22 2021-08-26 stsp if (worktree) {
8077 f8a36e22 2021-08-26 stsp repo_path =
8078 f8a36e22 2021-08-26 stsp strdup(got_worktree_get_repo_path(worktree));
8079 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
8080 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
8081 f8a36e22 2021-08-26 stsp if (error)
8082 f8a36e22 2021-08-26 stsp goto done;
8083 f8a36e22 2021-08-26 stsp } else {
8084 f8a36e22 2021-08-26 stsp repo_path = strdup(cwd);
8085 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
8086 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
8087 f8a36e22 2021-08-26 stsp goto done;
8088 f8a36e22 2021-08-26 stsp }
8089 f8a36e22 2021-08-26 stsp }
8090 f8a36e22 2021-08-26 stsp }
8091 f8a36e22 2021-08-26 stsp
8092 f8a36e22 2021-08-26 stsp error = got_repo_open(&repo, repo_path, NULL);
8093 f8a36e22 2021-08-26 stsp if (error)
8094 f8a36e22 2021-08-26 stsp goto done;
8095 f8a36e22 2021-08-26 stsp
8096 f8a36e22 2021-08-26 stsp if (worktree) {
8097 f8a36e22 2021-08-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
8098 f8a36e22 2021-08-26 stsp if (worktree_conf) {
8099 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
8100 f8a36e22 2021-08-26 stsp worktree_conf);
8101 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
8102 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
8103 f8a36e22 2021-08-26 stsp remote = &remotes[i];
8104 f8a36e22 2021-08-26 stsp break;
8105 f8a36e22 2021-08-26 stsp }
8106 f8a36e22 2021-08-26 stsp }
8107 f8a36e22 2021-08-26 stsp }
8108 f8a36e22 2021-08-26 stsp }
8109 f8a36e22 2021-08-26 stsp if (remote == NULL) {
8110 f8a36e22 2021-08-26 stsp repo_conf = got_repo_get_gotconfig(repo);
8111 f8a36e22 2021-08-26 stsp if (repo_conf) {
8112 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
8113 f8a36e22 2021-08-26 stsp repo_conf);
8114 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
8115 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
8116 f8a36e22 2021-08-26 stsp remote = &remotes[i];
8117 f8a36e22 2021-08-26 stsp break;
8118 f8a36e22 2021-08-26 stsp }
8119 f8a36e22 2021-08-26 stsp }
8120 f8a36e22 2021-08-26 stsp }
8121 f8a36e22 2021-08-26 stsp }
8122 f8a36e22 2021-08-26 stsp if (remote == NULL) {
8123 f8a36e22 2021-08-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
8124 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
8125 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
8126 f8a36e22 2021-08-26 stsp remote = &remotes[i];
8127 f8a36e22 2021-08-26 stsp break;
8128 f8a36e22 2021-08-26 stsp }
8129 f8a36e22 2021-08-26 stsp }
8130 f8a36e22 2021-08-26 stsp }
8131 f8a36e22 2021-08-26 stsp if (remote == NULL) {
8132 f8a36e22 2021-08-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
8133 f8a36e22 2021-08-26 stsp goto done;
8134 f8a36e22 2021-08-26 stsp }
8135 f8a36e22 2021-08-26 stsp
8136 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8137 6480c871 2021-08-30 stsp &repo_name, remote->send_url);
8138 f8a36e22 2021-08-26 stsp if (error)
8139 f8a36e22 2021-08-26 stsp goto done;
8140 f8a36e22 2021-08-26 stsp
8141 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git") == 0) {
8142 f8a36e22 2021-08-26 stsp #ifndef PROFILE
8143 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8144 f8a36e22 2021-08-26 stsp "sendfd dns inet unveil", NULL) == -1)
8145 f8a36e22 2021-08-26 stsp err(1, "pledge");
8146 f8a36e22 2021-08-26 stsp #endif
8147 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
8148 f8a36e22 2021-08-26 stsp strcmp(proto, "ssh") == 0) {
8149 f8a36e22 2021-08-26 stsp #ifndef PROFILE
8150 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8151 f8a36e22 2021-08-26 stsp "sendfd unveil", NULL) == -1)
8152 f8a36e22 2021-08-26 stsp err(1, "pledge");
8153 f8a36e22 2021-08-26 stsp #endif
8154 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "http") == 0 ||
8155 f8a36e22 2021-08-26 stsp strcmp(proto, "git+http") == 0) {
8156 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8157 f8a36e22 2021-08-26 stsp goto done;
8158 f8a36e22 2021-08-26 stsp } else {
8159 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8160 f8a36e22 2021-08-26 stsp goto done;
8161 f8a36e22 2021-08-26 stsp }
8162 f8a36e22 2021-08-26 stsp
8163 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
8164 d65a88a2 2021-09-05 stsp if (error)
8165 d65a88a2 2021-09-05 stsp goto done;
8166 d65a88a2 2021-09-05 stsp
8167 f8a36e22 2021-08-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8168 f8a36e22 2021-08-26 stsp if (error)
8169 f8a36e22 2021-08-26 stsp goto done;
8170 f8a36e22 2021-08-26 stsp
8171 f8a36e22 2021-08-26 stsp if (send_all_branches) {
8172 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_branches, repo, "refs/heads",
8173 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
8174 f8a36e22 2021-08-26 stsp if (error)
8175 f8a36e22 2021-08-26 stsp goto done;
8176 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_branches, entry) {
8177 f8a36e22 2021-08-26 stsp const char *branchname = got_ref_get_name(re->ref);
8178 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches,
8179 f8a36e22 2021-08-26 stsp branchname, NULL);
8180 f8a36e22 2021-08-26 stsp if (error)
8181 f8a36e22 2021-08-26 stsp goto done;
8182 f8a36e22 2021-08-26 stsp nbranches++;
8183 f8a36e22 2021-08-26 stsp }
8184 eac1df47 2021-09-01 stsp } else if (nbranches == 0) {
8185 eac1df47 2021-09-01 stsp for (i = 0; i < remote->nsend_branches; i++) {
8186 eac1df47 2021-09-01 stsp got_pathlist_append(&branches,
8187 eac1df47 2021-09-01 stsp remote->send_branches[i], NULL);
8188 eac1df47 2021-09-01 stsp }
8189 f8a36e22 2021-08-26 stsp }
8190 f8a36e22 2021-08-26 stsp
8191 f8a36e22 2021-08-26 stsp if (send_all_tags) {
8192 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_tags, repo, "refs/tags",
8193 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
8194 f8a36e22 2021-08-26 stsp if (error)
8195 f8a36e22 2021-08-26 stsp goto done;
8196 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_tags, entry) {
8197 f8a36e22 2021-08-26 stsp const char *tagname = got_ref_get_name(re->ref);
8198 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags,
8199 f8a36e22 2021-08-26 stsp tagname, NULL);
8200 f8a36e22 2021-08-26 stsp if (error)
8201 f8a36e22 2021-08-26 stsp goto done;
8202 f8a36e22 2021-08-26 stsp ntags++;
8203 f8a36e22 2021-08-26 stsp }
8204 f8a36e22 2021-08-26 stsp }
8205 f8a36e22 2021-08-26 stsp
8206 f8a36e22 2021-08-26 stsp /*
8207 f8a36e22 2021-08-26 stsp * To prevent accidents only branches in refs/heads/ can be deleted
8208 f8a36e22 2021-08-26 stsp * with 'got send -d'.
8209 f8a36e22 2021-08-26 stsp * Deleting anything else requires local repository access or Git.
8210 f8a36e22 2021-08-26 stsp */
8211 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_args, entry) {
8212 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
8213 f8a36e22 2021-08-26 stsp char *s;
8214 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *new;
8215 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0) {
8216 f8a36e22 2021-08-26 stsp s = strdup(branchname);
8217 f8a36e22 2021-08-26 stsp if (s == NULL) {
8218 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
8219 f8a36e22 2021-08-26 stsp goto done;
8220 f8a36e22 2021-08-26 stsp }
8221 f8a36e22 2021-08-26 stsp } else {
8222 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8223 f8a36e22 2021-08-26 stsp error = got_error_from_errno("asprintf");
8224 f8a36e22 2021-08-26 stsp goto done;
8225 f8a36e22 2021-08-26 stsp }
8226 f8a36e22 2021-08-26 stsp }
8227 f8a36e22 2021-08-26 stsp error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8228 f8a36e22 2021-08-26 stsp if (error || new == NULL /* duplicate */)
8229 f8a36e22 2021-08-26 stsp free(s);
8230 f8a36e22 2021-08-26 stsp if (error)
8231 f8a36e22 2021-08-26 stsp goto done;
8232 f8a36e22 2021-08-26 stsp ndelete_branches++;
8233 f8a36e22 2021-08-26 stsp }
8234 f8a36e22 2021-08-26 stsp
8235 f8a36e22 2021-08-26 stsp if (nbranches == 0 && ndelete_branches == 0) {
8236 f8a36e22 2021-08-26 stsp struct got_reference *head_ref;
8237 f8a36e22 2021-08-26 stsp if (worktree)
8238 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo,
8239 f8a36e22 2021-08-26 stsp got_worktree_get_head_ref_name(worktree), 0);
8240 f8a36e22 2021-08-26 stsp else
8241 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8242 f8a36e22 2021-08-26 stsp if (error)
8243 f8a36e22 2021-08-26 stsp goto done;
8244 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(head_ref)) {
8245 f8a36e22 2021-08-26 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8246 f8a36e22 2021-08-26 stsp got_ref_close(head_ref);
8247 f8a36e22 2021-08-26 stsp if (error)
8248 f8a36e22 2021-08-26 stsp goto done;
8249 f8a36e22 2021-08-26 stsp } else
8250 f8a36e22 2021-08-26 stsp ref = head_ref;
8251 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, got_ref_get_name(ref),
8252 abc59930 2021-09-05 naddy NULL);
8253 f8a36e22 2021-08-26 stsp if (error)
8254 f8a36e22 2021-08-26 stsp goto done;
8255 f8a36e22 2021-08-26 stsp nbranches++;
8256 f8a36e22 2021-08-26 stsp }
8257 f8a36e22 2021-08-26 stsp
8258 f8a36e22 2021-08-26 stsp if (verbosity >= 0)
8259 f8a36e22 2021-08-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8260 f8a36e22 2021-08-26 stsp port ? ":" : "", port ? port : "");
8261 f8a36e22 2021-08-26 stsp
8262 f8a36e22 2021-08-26 stsp error = got_send_connect(&sendpid, &sendfd, proto, host, port,
8263 f8a36e22 2021-08-26 stsp server_path, verbosity);
8264 f8a36e22 2021-08-26 stsp if (error)
8265 f8a36e22 2021-08-26 stsp goto done;
8266 f8a36e22 2021-08-26 stsp
8267 f8a36e22 2021-08-26 stsp memset(&spa, 0, sizeof(spa));
8268 f8a36e22 2021-08-26 stsp spa.last_scaled_packsize[0] = '\0';
8269 f8a36e22 2021-08-26 stsp spa.last_p_deltify = -1;
8270 f8a36e22 2021-08-26 stsp spa.last_p_written = -1;
8271 f8a36e22 2021-08-26 stsp spa.verbosity = verbosity;
8272 f8a36e22 2021-08-26 stsp spa.delete_branches = &delete_branches;
8273 f8a36e22 2021-08-26 stsp error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
8274 f8a36e22 2021-08-26 stsp verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
8275 f8a36e22 2021-08-26 stsp check_cancelled, NULL);
8276 f8a36e22 2021-08-26 stsp if (spa.printed_something)
8277 f8a36e22 2021-08-26 stsp putchar('\n');
8278 f8a36e22 2021-08-26 stsp if (error)
8279 f8a36e22 2021-08-26 stsp goto done;
8280 f8a36e22 2021-08-26 stsp if (!spa.sent_something && verbosity >= 0)
8281 f8a36e22 2021-08-26 stsp printf("Already up-to-date\n");
8282 f8a36e22 2021-08-26 stsp done:
8283 f8a36e22 2021-08-26 stsp if (sendpid > 0) {
8284 f8a36e22 2021-08-26 stsp if (kill(sendpid, SIGTERM) == -1)
8285 f8a36e22 2021-08-26 stsp error = got_error_from_errno("kill");
8286 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8287 f8a36e22 2021-08-26 stsp error = got_error_from_errno("waitpid");
8288 f8a36e22 2021-08-26 stsp }
8289 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8290 f8a36e22 2021-08-26 stsp error = got_error_from_errno("close");
8291 f8a36e22 2021-08-26 stsp if (repo) {
8292 f8a36e22 2021-08-26 stsp const struct got_error *close_err = got_repo_close(repo);
8293 f8a36e22 2021-08-26 stsp if (error == NULL)
8294 f8a36e22 2021-08-26 stsp error = close_err;
8295 f8a36e22 2021-08-26 stsp }
8296 f8a36e22 2021-08-26 stsp if (worktree)
8297 f8a36e22 2021-08-26 stsp got_worktree_close(worktree);
8298 f8a36e22 2021-08-26 stsp if (ref)
8299 f8a36e22 2021-08-26 stsp got_ref_close(ref);
8300 f8a36e22 2021-08-26 stsp got_pathlist_free(&branches);
8301 f8a36e22 2021-08-26 stsp got_pathlist_free(&tags);
8302 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_branches);
8303 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_tags);
8304 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_args);
8305 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_branches, entry)
8306 f8a36e22 2021-08-26 stsp free((char *)pe->path);
8307 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_branches);
8308 f8a36e22 2021-08-26 stsp free(cwd);
8309 f8a36e22 2021-08-26 stsp free(repo_path);
8310 f8a36e22 2021-08-26 stsp free(proto);
8311 f8a36e22 2021-08-26 stsp free(host);
8312 f8a36e22 2021-08-26 stsp free(port);
8313 f8a36e22 2021-08-26 stsp free(server_path);
8314 f8a36e22 2021-08-26 stsp free(repo_name);
8315 234035bc 2019-06-01 stsp return error;
8316 234035bc 2019-06-01 stsp }
8317 234035bc 2019-06-01 stsp
8318 234035bc 2019-06-01 stsp __dead static void
8319 234035bc 2019-06-01 stsp usage_cherrypick(void)
8320 234035bc 2019-06-01 stsp {
8321 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8322 234035bc 2019-06-01 stsp exit(1);
8323 234035bc 2019-06-01 stsp }
8324 234035bc 2019-06-01 stsp
8325 234035bc 2019-06-01 stsp static const struct got_error *
8326 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
8327 234035bc 2019-06-01 stsp {
8328 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
8329 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
8330 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
8331 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
8332 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
8333 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
8334 234035bc 2019-06-01 stsp struct got_object_qid *pid;
8335 9627c110 2020-04-18 stsp int ch;
8336 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8337 234035bc 2019-06-01 stsp
8338 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8339 234035bc 2019-06-01 stsp switch (ch) {
8340 234035bc 2019-06-01 stsp default:
8341 234035bc 2019-06-01 stsp usage_cherrypick();
8342 234035bc 2019-06-01 stsp /* NOTREACHED */
8343 234035bc 2019-06-01 stsp }
8344 234035bc 2019-06-01 stsp }
8345 234035bc 2019-06-01 stsp
8346 234035bc 2019-06-01 stsp argc -= optind;
8347 234035bc 2019-06-01 stsp argv += optind;
8348 234035bc 2019-06-01 stsp
8349 43012d58 2019-07-14 stsp #ifndef PROFILE
8350 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8351 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8352 43012d58 2019-07-14 stsp err(1, "pledge");
8353 43012d58 2019-07-14 stsp #endif
8354 234035bc 2019-06-01 stsp if (argc != 1)
8355 234035bc 2019-06-01 stsp usage_cherrypick();
8356 234035bc 2019-06-01 stsp
8357 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
8358 234035bc 2019-06-01 stsp if (cwd == NULL) {
8359 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
8360 234035bc 2019-06-01 stsp goto done;
8361 234035bc 2019-06-01 stsp }
8362 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
8363 fa51e947 2020-03-27 stsp if (error) {
8364 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8365 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
8366 fa51e947 2020-03-27 stsp cwd);
8367 234035bc 2019-06-01 stsp goto done;
8368 fa51e947 2020-03-27 stsp }
8369 234035bc 2019-06-01 stsp
8370 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8371 c9956ddf 2019-09-08 stsp NULL);
8372 234035bc 2019-06-01 stsp if (error != NULL)
8373 234035bc 2019-06-01 stsp goto done;
8374 234035bc 2019-06-01 stsp
8375 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8376 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8377 234035bc 2019-06-01 stsp if (error)
8378 234035bc 2019-06-01 stsp goto done;
8379 234035bc 2019-06-01 stsp
8380 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8381 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8382 234035bc 2019-06-01 stsp if (error != NULL) {
8383 234035bc 2019-06-01 stsp struct got_reference *ref;
8384 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8385 234035bc 2019-06-01 stsp goto done;
8386 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8387 234035bc 2019-06-01 stsp if (error != NULL)
8388 234035bc 2019-06-01 stsp goto done;
8389 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
8390 234035bc 2019-06-01 stsp got_ref_close(ref);
8391 234035bc 2019-06-01 stsp if (error != NULL)
8392 234035bc 2019-06-01 stsp goto done;
8393 234035bc 2019-06-01 stsp }
8394 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
8395 234035bc 2019-06-01 stsp if (error)
8396 234035bc 2019-06-01 stsp goto done;
8397 234035bc 2019-06-01 stsp
8398 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8399 234035bc 2019-06-01 stsp if (error)
8400 234035bc 2019-06-01 stsp goto done;
8401 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8402 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8403 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
8404 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
8405 03415a1a 2019-06-02 stsp NULL);
8406 234035bc 2019-06-01 stsp if (error != NULL)
8407 234035bc 2019-06-01 stsp goto done;
8408 234035bc 2019-06-01 stsp
8409 9627c110 2020-04-18 stsp if (upa.did_something)
8410 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
8411 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
8412 234035bc 2019-06-01 stsp done:
8413 234035bc 2019-06-01 stsp if (commit)
8414 234035bc 2019-06-01 stsp got_object_commit_close(commit);
8415 234035bc 2019-06-01 stsp free(commit_id_str);
8416 234035bc 2019-06-01 stsp if (worktree)
8417 234035bc 2019-06-01 stsp got_worktree_close(worktree);
8418 1d0f4054 2021-06-17 stsp if (repo) {
8419 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8420 1d0f4054 2021-06-17 stsp if (error == NULL)
8421 1d0f4054 2021-06-17 stsp error = close_err;
8422 1d0f4054 2021-06-17 stsp }
8423 c4296144 2019-05-09 stsp return error;
8424 c4296144 2019-05-09 stsp }
8425 5ef14e63 2019-06-02 stsp
8426 5ef14e63 2019-06-02 stsp __dead static void
8427 5ef14e63 2019-06-02 stsp usage_backout(void)
8428 5ef14e63 2019-06-02 stsp {
8429 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
8430 5ef14e63 2019-06-02 stsp exit(1);
8431 5ef14e63 2019-06-02 stsp }
8432 5ef14e63 2019-06-02 stsp
8433 5ef14e63 2019-06-02 stsp static const struct got_error *
8434 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
8435 5ef14e63 2019-06-02 stsp {
8436 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
8437 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
8438 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
8439 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
8440 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
8441 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
8442 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
8443 9627c110 2020-04-18 stsp int ch;
8444 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8445 5ef14e63 2019-06-02 stsp
8446 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8447 5ef14e63 2019-06-02 stsp switch (ch) {
8448 5ef14e63 2019-06-02 stsp default:
8449 5ef14e63 2019-06-02 stsp usage_backout();
8450 5ef14e63 2019-06-02 stsp /* NOTREACHED */
8451 5ef14e63 2019-06-02 stsp }
8452 5ef14e63 2019-06-02 stsp }
8453 5ef14e63 2019-06-02 stsp
8454 5ef14e63 2019-06-02 stsp argc -= optind;
8455 5ef14e63 2019-06-02 stsp argv += optind;
8456 5ef14e63 2019-06-02 stsp
8457 43012d58 2019-07-14 stsp #ifndef PROFILE
8458 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8459 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8460 43012d58 2019-07-14 stsp err(1, "pledge");
8461 43012d58 2019-07-14 stsp #endif
8462 5ef14e63 2019-06-02 stsp if (argc != 1)
8463 5ef14e63 2019-06-02 stsp usage_backout();
8464 5ef14e63 2019-06-02 stsp
8465 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
8466 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
8467 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
8468 5ef14e63 2019-06-02 stsp goto done;
8469 5ef14e63 2019-06-02 stsp }
8470 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
8471 fa51e947 2020-03-27 stsp if (error) {
8472 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8473 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
8474 5ef14e63 2019-06-02 stsp goto done;
8475 fa51e947 2020-03-27 stsp }
8476 5ef14e63 2019-06-02 stsp
8477 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8478 c9956ddf 2019-09-08 stsp NULL);
8479 5ef14e63 2019-06-02 stsp if (error != NULL)
8480 5ef14e63 2019-06-02 stsp goto done;
8481 5ef14e63 2019-06-02 stsp
8482 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8483 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8484 5ef14e63 2019-06-02 stsp if (error)
8485 5ef14e63 2019-06-02 stsp goto done;
8486 5ef14e63 2019-06-02 stsp
8487 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8488 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8489 5ef14e63 2019-06-02 stsp if (error != NULL) {
8490 5ef14e63 2019-06-02 stsp struct got_reference *ref;
8491 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8492 5ef14e63 2019-06-02 stsp goto done;
8493 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8494 5ef14e63 2019-06-02 stsp if (error != NULL)
8495 5ef14e63 2019-06-02 stsp goto done;
8496 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
8497 5ef14e63 2019-06-02 stsp got_ref_close(ref);
8498 5ef14e63 2019-06-02 stsp if (error != NULL)
8499 5ef14e63 2019-06-02 stsp goto done;
8500 5ef14e63 2019-06-02 stsp }
8501 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
8502 5ef14e63 2019-06-02 stsp if (error)
8503 5ef14e63 2019-06-02 stsp goto done;
8504 5ef14e63 2019-06-02 stsp
8505 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8506 5ef14e63 2019-06-02 stsp if (error)
8507 5ef14e63 2019-06-02 stsp goto done;
8508 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8509 5ef14e63 2019-06-02 stsp if (pid == NULL) {
8510 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
8511 5ef14e63 2019-06-02 stsp goto done;
8512 5ef14e63 2019-06-02 stsp }
8513 5ef14e63 2019-06-02 stsp
8514 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8515 f259c4c1 2021-09-24 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id,
8516 f259c4c1 2021-09-24 stsp repo, update_progress, &upa, check_cancelled, NULL);
8517 5ef14e63 2019-06-02 stsp if (error != NULL)
8518 5ef14e63 2019-06-02 stsp goto done;
8519 5ef14e63 2019-06-02 stsp
8520 9627c110 2020-04-18 stsp if (upa.did_something)
8521 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
8522 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
8523 5ef14e63 2019-06-02 stsp done:
8524 5ef14e63 2019-06-02 stsp if (commit)
8525 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
8526 5ef14e63 2019-06-02 stsp free(commit_id_str);
8527 818c7501 2019-07-11 stsp if (worktree)
8528 818c7501 2019-07-11 stsp got_worktree_close(worktree);
8529 1d0f4054 2021-06-17 stsp if (repo) {
8530 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8531 1d0f4054 2021-06-17 stsp if (error == NULL)
8532 1d0f4054 2021-06-17 stsp error = close_err;
8533 1d0f4054 2021-06-17 stsp }
8534 818c7501 2019-07-11 stsp return error;
8535 818c7501 2019-07-11 stsp }
8536 818c7501 2019-07-11 stsp
8537 818c7501 2019-07-11 stsp __dead static void
8538 818c7501 2019-07-11 stsp usage_rebase(void)
8539 818c7501 2019-07-11 stsp {
8540 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8541 af54c8f8 2019-07-11 stsp getprogname());
8542 818c7501 2019-07-11 stsp exit(1);
8543 0ebf8283 2019-07-24 stsp }
8544 0ebf8283 2019-07-24 stsp
8545 0ebf8283 2019-07-24 stsp void
8546 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
8547 0ebf8283 2019-07-24 stsp {
8548 0ebf8283 2019-07-24 stsp char *nl;
8549 0ebf8283 2019-07-24 stsp size_t len;
8550 0ebf8283 2019-07-24 stsp
8551 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
8552 0ebf8283 2019-07-24 stsp if (len > limit)
8553 0ebf8283 2019-07-24 stsp len = limit;
8554 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
8555 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
8556 0ebf8283 2019-07-24 stsp if (nl)
8557 0ebf8283 2019-07-24 stsp *nl = '\0';
8558 0ebf8283 2019-07-24 stsp }
8559 0ebf8283 2019-07-24 stsp
8560 0ebf8283 2019-07-24 stsp static const struct got_error *
8561 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8562 0ebf8283 2019-07-24 stsp {
8563 5943eee2 2019-08-13 stsp const struct got_error *err;
8564 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
8565 5943eee2 2019-08-13 stsp const char *s;
8566 0ebf8283 2019-07-24 stsp
8567 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8568 5943eee2 2019-08-13 stsp if (err)
8569 5943eee2 2019-08-13 stsp return err;
8570 0ebf8283 2019-07-24 stsp
8571 5943eee2 2019-08-13 stsp s = logmsg0;
8572 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
8573 5943eee2 2019-08-13 stsp s++;
8574 5943eee2 2019-08-13 stsp
8575 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
8576 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
8577 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
8578 5943eee2 2019-08-13 stsp goto done;
8579 5943eee2 2019-08-13 stsp }
8580 0ebf8283 2019-07-24 stsp
8581 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
8582 5943eee2 2019-08-13 stsp done:
8583 5943eee2 2019-08-13 stsp free(logmsg0);
8584 a0ea4fc0 2020-02-28 stsp return err;
8585 a0ea4fc0 2020-02-28 stsp }
8586 a0ea4fc0 2020-02-28 stsp
8587 a0ea4fc0 2020-02-28 stsp static const struct got_error *
8588 c0df5966 2021-12-31 stsp show_rebase_merge_conflict(struct got_object_id *id,
8589 c0df5966 2021-12-31 stsp struct got_repository *repo)
8590 a0ea4fc0 2020-02-28 stsp {
8591 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
8592 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
8593 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
8594 a0ea4fc0 2020-02-28 stsp
8595 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
8596 a0ea4fc0 2020-02-28 stsp if (err)
8597 a0ea4fc0 2020-02-28 stsp return err;
8598 a0ea4fc0 2020-02-28 stsp
8599 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
8600 a0ea4fc0 2020-02-28 stsp if (err)
8601 a0ea4fc0 2020-02-28 stsp goto done;
8602 a0ea4fc0 2020-02-28 stsp
8603 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
8604 a0ea4fc0 2020-02-28 stsp
8605 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
8606 a0ea4fc0 2020-02-28 stsp if (err)
8607 a0ea4fc0 2020-02-28 stsp goto done;
8608 a0ea4fc0 2020-02-28 stsp
8609 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
8610 a0ea4fc0 2020-02-28 stsp done:
8611 a0ea4fc0 2020-02-28 stsp free(id_str);
8612 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
8613 a0ea4fc0 2020-02-28 stsp free(logmsg);
8614 5943eee2 2019-08-13 stsp return err;
8615 818c7501 2019-07-11 stsp }
8616 818c7501 2019-07-11 stsp
8617 818c7501 2019-07-11 stsp static const struct got_error *
8618 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
8619 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
8620 818c7501 2019-07-11 stsp {
8621 818c7501 2019-07-11 stsp const struct got_error *err;
8622 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8623 818c7501 2019-07-11 stsp
8624 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
8625 818c7501 2019-07-11 stsp if (err)
8626 818c7501 2019-07-11 stsp goto done;
8627 818c7501 2019-07-11 stsp
8628 ff0d2220 2019-07-11 stsp if (new_id) {
8629 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
8630 ff0d2220 2019-07-11 stsp if (err)
8631 ff0d2220 2019-07-11 stsp goto done;
8632 ff0d2220 2019-07-11 stsp }
8633 818c7501 2019-07-11 stsp
8634 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
8635 ff0d2220 2019-07-11 stsp if (new_id_str)
8636 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
8637 0ebf8283 2019-07-24 stsp
8638 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8639 0ebf8283 2019-07-24 stsp if (err)
8640 0ebf8283 2019-07-24 stsp goto done;
8641 0ebf8283 2019-07-24 stsp
8642 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
8643 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8644 818c7501 2019-07-11 stsp done:
8645 818c7501 2019-07-11 stsp free(old_id_str);
8646 818c7501 2019-07-11 stsp free(new_id_str);
8647 272a1371 2020-02-28 stsp free(logmsg);
8648 818c7501 2019-07-11 stsp return err;
8649 818c7501 2019-07-11 stsp }
8650 818c7501 2019-07-11 stsp
8651 1ee397ad 2019-07-12 stsp static const struct got_error *
8652 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8653 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
8654 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
8655 e600f124 2021-03-21 stsp int create_backup)
8656 818c7501 2019-07-11 stsp {
8657 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
8658 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
8659 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
8660 818c7501 2019-07-11 stsp }
8661 818c7501 2019-07-11 stsp
8662 818c7501 2019-07-11 stsp static const struct got_error *
8663 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
8664 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8665 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
8666 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8667 ff0d2220 2019-07-11 stsp {
8668 ff0d2220 2019-07-11 stsp const struct got_error *error;
8669 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
8670 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
8671 ff0d2220 2019-07-11 stsp
8672 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8673 ff0d2220 2019-07-11 stsp if (error)
8674 ff0d2220 2019-07-11 stsp return error;
8675 ff0d2220 2019-07-11 stsp
8676 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8677 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
8678 ff0d2220 2019-07-11 stsp if (error) {
8679 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8680 ff0d2220 2019-07-11 stsp goto done;
8681 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
8682 ff0d2220 2019-07-11 stsp } else {
8683 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
8684 ff0d2220 2019-07-11 stsp free(new_commit_id);
8685 ff0d2220 2019-07-11 stsp }
8686 ff0d2220 2019-07-11 stsp done:
8687 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
8688 ff0d2220 2019-07-11 stsp return error;
8689 64c6d990 2019-07-11 stsp }
8690 64c6d990 2019-07-11 stsp
8691 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
8692 64c6d990 2019-07-11 stsp const char *path_prefix;
8693 64c6d990 2019-07-11 stsp size_t len;
8694 8ca9bd68 2019-07-25 stsp int errcode;
8695 64c6d990 2019-07-11 stsp };
8696 64c6d990 2019-07-11 stsp
8697 64c6d990 2019-07-11 stsp static const struct got_error *
8698 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8699 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
8700 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
8701 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
8702 64c6d990 2019-07-11 stsp {
8703 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
8704 64c6d990 2019-07-11 stsp
8705 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8706 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8707 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
8708 64c6d990 2019-07-11 stsp
8709 64c6d990 2019-07-11 stsp return NULL;
8710 64c6d990 2019-07-11 stsp }
8711 64c6d990 2019-07-11 stsp
8712 64c6d990 2019-07-11 stsp static const struct got_error *
8713 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
8714 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
8715 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
8716 64c6d990 2019-07-11 stsp {
8717 64c6d990 2019-07-11 stsp const struct got_error *err;
8718 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8719 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
8720 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
8721 64c6d990 2019-07-11 stsp
8722 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
8723 64c6d990 2019-07-11 stsp return NULL;
8724 64c6d990 2019-07-11 stsp
8725 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8726 64c6d990 2019-07-11 stsp if (err)
8727 64c6d990 2019-07-11 stsp goto done;
8728 64c6d990 2019-07-11 stsp
8729 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8730 64c6d990 2019-07-11 stsp if (err)
8731 64c6d990 2019-07-11 stsp goto done;
8732 64c6d990 2019-07-11 stsp
8733 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
8734 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
8735 64c6d990 2019-07-11 stsp if (err)
8736 64c6d990 2019-07-11 stsp goto done;
8737 64c6d990 2019-07-11 stsp
8738 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
8739 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
8740 64c6d990 2019-07-11 stsp if (err)
8741 64c6d990 2019-07-11 stsp goto done;
8742 64c6d990 2019-07-11 stsp
8743 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
8744 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
8745 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
8746 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
8747 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
8748 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
8749 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
8750 64c6d990 2019-07-11 stsp done:
8751 64c6d990 2019-07-11 stsp if (tree1)
8752 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
8753 64c6d990 2019-07-11 stsp if (tree2)
8754 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
8755 64c6d990 2019-07-11 stsp if (commit)
8756 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
8757 64c6d990 2019-07-11 stsp if (parent_commit)
8758 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
8759 64c6d990 2019-07-11 stsp return err;
8760 ff0d2220 2019-07-11 stsp }
8761 ff0d2220 2019-07-11 stsp
8762 ff0d2220 2019-07-11 stsp static const struct got_error *
8763 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
8764 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
8765 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8766 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
8767 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
8768 af61c510 2019-07-19 stsp {
8769 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
8770 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
8771 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
8772 af61c510 2019-07-19 stsp struct got_object_qid *qid;
8773 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
8774 af61c510 2019-07-19 stsp
8775 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
8776 af61c510 2019-07-19 stsp if (err)
8777 af61c510 2019-07-19 stsp return err;
8778 af61c510 2019-07-19 stsp
8779 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8780 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8781 af61c510 2019-07-19 stsp if (err)
8782 af61c510 2019-07-19 stsp goto done;
8783 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8784 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
8785 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
8786 af61c510 2019-07-19 stsp if (err) {
8787 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
8788 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
8789 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
8790 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
8791 af61c510 2019-07-19 stsp "been reached?!?");
8792 ee780d5c 2020-01-04 stsp }
8793 ee780d5c 2020-01-04 stsp goto done;
8794 af61c510 2019-07-19 stsp } else {
8795 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
8796 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
8797 af61c510 2019-07-19 stsp if (err)
8798 af61c510 2019-07-19 stsp goto done;
8799 af61c510 2019-07-19 stsp
8800 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
8801 af61c510 2019-07-19 stsp if (err)
8802 af61c510 2019-07-19 stsp goto done;
8803 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
8804 af61c510 2019-07-19 stsp commit_id = parent_id;
8805 af61c510 2019-07-19 stsp }
8806 af61c510 2019-07-19 stsp }
8807 af61c510 2019-07-19 stsp done:
8808 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
8809 af61c510 2019-07-19 stsp return err;
8810 e600f124 2021-03-21 stsp }
8811 e600f124 2021-03-21 stsp
8812 e600f124 2021-03-21 stsp static const struct got_error *
8813 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8814 e600f124 2021-03-21 stsp {
8815 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8816 e600f124 2021-03-21 stsp time_t committer_time;
8817 e600f124 2021-03-21 stsp struct tm tm;
8818 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
8819 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
8820 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
8821 e600f124 2021-03-21 stsp
8822 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
8823 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
8824 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
8825 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8826 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
8827 e600f124 2021-03-21 stsp
8828 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
8829 e600f124 2021-03-21 stsp if (author0 == NULL)
8830 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
8831 e600f124 2021-03-21 stsp author = author0;
8832 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
8833 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
8834 e600f124 2021-03-21 stsp author = smallerthan + 1;
8835 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
8836 e600f124 2021-03-21 stsp
8837 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8838 e600f124 2021-03-21 stsp if (err)
8839 e600f124 2021-03-21 stsp goto done;
8840 e600f124 2021-03-21 stsp logmsg = logmsg0;
8841 e600f124 2021-03-21 stsp while (*logmsg == '\n')
8842 e600f124 2021-03-21 stsp logmsg++;
8843 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
8844 e600f124 2021-03-21 stsp if (newline)
8845 e600f124 2021-03-21 stsp *newline = '\0';
8846 e600f124 2021-03-21 stsp
8847 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
8848 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
8849 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
8850 e600f124 2021-03-21 stsp done:
8851 e600f124 2021-03-21 stsp free(author0);
8852 e600f124 2021-03-21 stsp free(logmsg0);
8853 643b85bc 2021-07-16 stsp return err;
8854 643b85bc 2021-07-16 stsp }
8855 643b85bc 2021-07-16 stsp
8856 643b85bc 2021-07-16 stsp static const struct got_error *
8857 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8858 643b85bc 2021-07-16 stsp struct got_repository *repo)
8859 643b85bc 2021-07-16 stsp {
8860 643b85bc 2021-07-16 stsp const struct got_error *err;
8861 643b85bc 2021-07-16 stsp char *id_str;
8862 643b85bc 2021-07-16 stsp
8863 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
8864 643b85bc 2021-07-16 stsp if (err)
8865 643b85bc 2021-07-16 stsp return err;
8866 643b85bc 2021-07-16 stsp
8867 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
8868 643b85bc 2021-07-16 stsp if (err)
8869 643b85bc 2021-07-16 stsp goto done;
8870 643b85bc 2021-07-16 stsp
8871 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8872 643b85bc 2021-07-16 stsp done:
8873 643b85bc 2021-07-16 stsp free(id_str);
8874 e600f124 2021-03-21 stsp return err;
8875 e600f124 2021-03-21 stsp }
8876 e600f124 2021-03-21 stsp
8877 e600f124 2021-03-21 stsp static const struct got_error *
8878 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
8879 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8880 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
8881 e600f124 2021-03-21 stsp struct got_repository *repo)
8882 e600f124 2021-03-21 stsp {
8883 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8884 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
8885 e600f124 2021-03-21 stsp char *refs_str = NULL;
8886 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
8887 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
8888 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
8889 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
8890 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
8891 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
8892 e600f124 2021-03-21 stsp char *custom_refs_str;
8893 e600f124 2021-03-21 stsp
8894 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8895 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
8896 e600f124 2021-03-21 stsp
8897 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8898 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
8899 e600f124 2021-03-21 stsp if (err)
8900 e600f124 2021-03-21 stsp goto done;
8901 e600f124 2021-03-21 stsp
8902 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8903 e600f124 2021-03-21 stsp if (err)
8904 e600f124 2021-03-21 stsp goto done;
8905 e600f124 2021-03-21 stsp
8906 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8907 e600f124 2021-03-21 stsp if (refs) {
8908 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8909 e600f124 2021-03-21 stsp if (err)
8910 e600f124 2021-03-21 stsp goto done;
8911 e600f124 2021-03-21 stsp }
8912 e600f124 2021-03-21 stsp
8913 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8914 e600f124 2021-03-21 stsp if (err)
8915 e600f124 2021-03-21 stsp goto done;
8916 e600f124 2021-03-21 stsp
8917 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8918 e600f124 2021-03-21 stsp if (err)
8919 e600f124 2021-03-21 stsp goto done;
8920 e600f124 2021-03-21 stsp
8921 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8922 4e91ef15 2021-09-26 stsp old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
8923 e600f124 2021-03-21 stsp if (err)
8924 e600f124 2021-03-21 stsp goto done;
8925 e600f124 2021-03-21 stsp
8926 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8927 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8928 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
8929 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8930 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
8931 e600f124 2021-03-21 stsp free(refs_str);
8932 e600f124 2021-03-21 stsp refs_str = NULL;
8933 e600f124 2021-03-21 stsp
8934 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8935 e600f124 2021-03-21 stsp if (err)
8936 e600f124 2021-03-21 stsp goto done;
8937 e600f124 2021-03-21 stsp
8938 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
8939 e600f124 2021-03-21 stsp if (err)
8940 e600f124 2021-03-21 stsp goto done;
8941 e600f124 2021-03-21 stsp
8942 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
8943 e600f124 2021-03-21 stsp if (err)
8944 e600f124 2021-03-21 stsp goto done;
8945 e600f124 2021-03-21 stsp
8946 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8947 e600f124 2021-03-21 stsp if (refs) {
8948 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
8949 e600f124 2021-03-21 stsp if (err)
8950 e600f124 2021-03-21 stsp goto done;
8951 e600f124 2021-03-21 stsp }
8952 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
8953 e600f124 2021-03-21 stsp yca_id_str,
8954 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8955 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
8956 e600f124 2021-03-21 stsp }
8957 e600f124 2021-03-21 stsp done:
8958 e600f124 2021-03-21 stsp free(custom_refs_str);
8959 e600f124 2021-03-21 stsp free(new_commit_id);
8960 e600f124 2021-03-21 stsp free(refs_str);
8961 e600f124 2021-03-21 stsp free(yca_id);
8962 e600f124 2021-03-21 stsp free(yca_id_str);
8963 e600f124 2021-03-21 stsp free(yca_brief_str);
8964 e600f124 2021-03-21 stsp if (new_commit)
8965 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
8966 e600f124 2021-03-21 stsp if (yca_commit)
8967 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
8968 e600f124 2021-03-21 stsp
8969 e600f124 2021-03-21 stsp return NULL;
8970 af61c510 2019-07-19 stsp }
8971 af61c510 2019-07-19 stsp
8972 af61c510 2019-07-19 stsp static const struct got_error *
8973 c0df5966 2021-12-31 stsp process_backup_refs(const char *backup_ref_prefix,
8974 c0df5966 2021-12-31 stsp const char *wanted_branch_name,
8975 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
8976 e600f124 2021-03-21 stsp {
8977 e600f124 2021-03-21 stsp const struct got_error *err;
8978 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
8979 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
8980 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8981 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
8982 e600f124 2021-03-21 stsp char *branch_name = NULL;
8983 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
8984 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
8985 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
8986 e600f124 2021-03-21 stsp
8987 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
8988 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
8989 e600f124 2021-03-21 stsp
8990 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8991 e600f124 2021-03-21 stsp if (err)
8992 e600f124 2021-03-21 stsp return err;
8993 e600f124 2021-03-21 stsp
8994 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8995 e600f124 2021-03-21 stsp if (err)
8996 e600f124 2021-03-21 stsp goto done;
8997 e600f124 2021-03-21 stsp
8998 e600f124 2021-03-21 stsp if (wanted_branch_name) {
8999 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9000 e600f124 2021-03-21 stsp wanted_branch_name += 11;
9001 e600f124 2021-03-21 stsp }
9002 e600f124 2021-03-21 stsp
9003 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9004 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
9005 e600f124 2021-03-21 stsp if (err)
9006 e600f124 2021-03-21 stsp goto done;
9007 e600f124 2021-03-21 stsp
9008 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
9009 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
9010 e600f124 2021-03-21 stsp char *slash;
9011 e600f124 2021-03-21 stsp
9012 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
9013 643b85bc 2021-07-16 stsp if (err)
9014 643b85bc 2021-07-16 stsp break;
9015 643b85bc 2021-07-16 stsp
9016 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
9017 e600f124 2021-03-21 stsp if (err)
9018 e600f124 2021-03-21 stsp break;
9019 e600f124 2021-03-21 stsp
9020 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
9021 e600f124 2021-03-21 stsp old_commit_id);
9022 e600f124 2021-03-21 stsp if (err)
9023 e600f124 2021-03-21 stsp break;
9024 e600f124 2021-03-21 stsp
9025 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
9026 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
9027 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
9028 e600f124 2021-03-21 stsp
9029 e600f124 2021-03-21 stsp while (refname[0] == '/')
9030 e600f124 2021-03-21 stsp refname++;
9031 e600f124 2021-03-21 stsp
9032 e600f124 2021-03-21 stsp branch_name = strdup(refname);
9033 e600f124 2021-03-21 stsp if (branch_name == NULL) {
9034 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
9035 e600f124 2021-03-21 stsp break;
9036 e600f124 2021-03-21 stsp }
9037 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
9038 e600f124 2021-03-21 stsp if (slash) {
9039 e600f124 2021-03-21 stsp *slash = '\0';
9040 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
9041 e600f124 2021-03-21 stsp }
9042 e600f124 2021-03-21 stsp
9043 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
9044 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
9045 9e822917 2021-03-23 stsp wanted_branch_found = 1;
9046 643b85bc 2021-07-16 stsp if (delete) {
9047 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
9048 643b85bc 2021-07-16 stsp old_commit_id, repo);
9049 643b85bc 2021-07-16 stsp } else {
9050 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
9051 abc59930 2021-09-05 naddy old_commit_id, old_commit, refs_idmap,
9052 abc59930 2021-09-05 naddy repo);
9053 643b85bc 2021-07-16 stsp }
9054 e600f124 2021-03-21 stsp if (err)
9055 e600f124 2021-03-21 stsp break;
9056 e600f124 2021-03-21 stsp }
9057 e600f124 2021-03-21 stsp
9058 e600f124 2021-03-21 stsp free(old_commit_id);
9059 e600f124 2021-03-21 stsp old_commit_id = NULL;
9060 e600f124 2021-03-21 stsp free(branch_name);
9061 e600f124 2021-03-21 stsp branch_name = NULL;
9062 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
9063 e600f124 2021-03-21 stsp old_commit = NULL;
9064 e600f124 2021-03-21 stsp }
9065 9e822917 2021-03-23 stsp
9066 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
9067 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
9068 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
9069 9e822917 2021-03-23 stsp }
9070 e600f124 2021-03-21 stsp done:
9071 e600f124 2021-03-21 stsp if (refs_idmap)
9072 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
9073 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
9074 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
9075 e600f124 2021-03-21 stsp free(old_commit_id);
9076 e600f124 2021-03-21 stsp free(branch_name);
9077 e600f124 2021-03-21 stsp if (old_commit)
9078 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
9079 e600f124 2021-03-21 stsp return err;
9080 e600f124 2021-03-21 stsp }
9081 e600f124 2021-03-21 stsp
9082 e600f124 2021-03-21 stsp static const struct got_error *
9083 41f061b2 2021-10-05 stsp abort_progress(void *arg, unsigned char status, const char *path)
9084 41f061b2 2021-10-05 stsp {
9085 41f061b2 2021-10-05 stsp /*
9086 41f061b2 2021-10-05 stsp * Unversioned files should not clutter progress output when
9087 41f061b2 2021-10-05 stsp * an operation is aborted.
9088 41f061b2 2021-10-05 stsp */
9089 41f061b2 2021-10-05 stsp if (status == GOT_STATUS_UNVERSIONED)
9090 41f061b2 2021-10-05 stsp return NULL;
9091 41f061b2 2021-10-05 stsp
9092 41f061b2 2021-10-05 stsp return update_progress(arg, status, path);
9093 41f061b2 2021-10-05 stsp }
9094 41f061b2 2021-10-05 stsp
9095 41f061b2 2021-10-05 stsp static const struct got_error *
9096 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
9097 818c7501 2019-07-11 stsp {
9098 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
9099 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
9100 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
9101 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9102 818c7501 2019-07-11 stsp char *cwd = NULL;
9103 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
9104 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
9105 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
9106 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
9107 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
9108 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
9109 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
9110 f259c4c1 2021-09-24 stsp int histedit_in_progress = 0, merge_in_progress = 0;
9111 f259c4c1 2021-09-24 stsp int create_backup = 1, list_backups = 0, delete_backups = 0;
9112 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
9113 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
9114 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
9115 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
9116 1fa49072 2021-09-28 stsp struct got_update_progress_arg upa;
9117 818c7501 2019-07-11 stsp
9118 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
9119 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
9120 1fa49072 2021-09-28 stsp memset(&upa, 0, sizeof(upa));
9121 818c7501 2019-07-11 stsp
9122 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
9123 818c7501 2019-07-11 stsp switch (ch) {
9124 818c7501 2019-07-11 stsp case 'a':
9125 818c7501 2019-07-11 stsp abort_rebase = 1;
9126 818c7501 2019-07-11 stsp break;
9127 818c7501 2019-07-11 stsp case 'c':
9128 818c7501 2019-07-11 stsp continue_rebase = 1;
9129 818c7501 2019-07-11 stsp break;
9130 e600f124 2021-03-21 stsp case 'l':
9131 e600f124 2021-03-21 stsp list_backups = 1;
9132 e600f124 2021-03-21 stsp break;
9133 643b85bc 2021-07-16 stsp case 'X':
9134 643b85bc 2021-07-16 stsp delete_backups = 1;
9135 643b85bc 2021-07-16 stsp break;
9136 818c7501 2019-07-11 stsp default:
9137 818c7501 2019-07-11 stsp usage_rebase();
9138 818c7501 2019-07-11 stsp /* NOTREACHED */
9139 818c7501 2019-07-11 stsp }
9140 818c7501 2019-07-11 stsp }
9141 818c7501 2019-07-11 stsp
9142 818c7501 2019-07-11 stsp argc -= optind;
9143 818c7501 2019-07-11 stsp argv += optind;
9144 818c7501 2019-07-11 stsp
9145 43012d58 2019-07-14 stsp #ifndef PROFILE
9146 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9147 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
9148 43012d58 2019-07-14 stsp err(1, "pledge");
9149 43012d58 2019-07-14 stsp #endif
9150 e600f124 2021-03-21 stsp if (list_backups) {
9151 e600f124 2021-03-21 stsp if (abort_rebase)
9152 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9153 e600f124 2021-03-21 stsp if (continue_rebase)
9154 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9155 643b85bc 2021-07-16 stsp if (delete_backups)
9156 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9157 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
9158 643b85bc 2021-07-16 stsp usage_rebase();
9159 643b85bc 2021-07-16 stsp } else if (delete_backups) {
9160 643b85bc 2021-07-16 stsp if (abort_rebase)
9161 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
9162 643b85bc 2021-07-16 stsp if (continue_rebase)
9163 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
9164 643b85bc 2021-07-16 stsp if (list_backups)
9165 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9166 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9167 818c7501 2019-07-11 stsp usage_rebase();
9168 e600f124 2021-03-21 stsp } else {
9169 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
9170 e600f124 2021-03-21 stsp usage_rebase();
9171 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
9172 e600f124 2021-03-21 stsp if (argc != 0)
9173 e600f124 2021-03-21 stsp usage_rebase();
9174 e600f124 2021-03-21 stsp } else if (argc != 1)
9175 e600f124 2021-03-21 stsp usage_rebase();
9176 e600f124 2021-03-21 stsp }
9177 818c7501 2019-07-11 stsp
9178 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
9179 818c7501 2019-07-11 stsp if (cwd == NULL) {
9180 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
9181 818c7501 2019-07-11 stsp goto done;
9182 818c7501 2019-07-11 stsp }
9183 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
9184 fa51e947 2020-03-27 stsp if (error) {
9185 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9186 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
9187 e600f124 2021-03-21 stsp goto done;
9188 e600f124 2021-03-21 stsp } else {
9189 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9190 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
9191 e600f124 2021-03-21 stsp "rebase", cwd);
9192 e600f124 2021-03-21 stsp goto done;
9193 e600f124 2021-03-21 stsp }
9194 fa51e947 2020-03-27 stsp }
9195 818c7501 2019-07-11 stsp
9196 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
9197 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
9198 818c7501 2019-07-11 stsp if (error != NULL)
9199 818c7501 2019-07-11 stsp goto done;
9200 818c7501 2019-07-11 stsp
9201 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9202 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
9203 7ef62c4e 2020-02-24 stsp if (error)
9204 7ef62c4e 2020-02-24 stsp goto done;
9205 7ef62c4e 2020-02-24 stsp
9206 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9207 643b85bc 2021-07-16 stsp error = process_backup_refs(
9208 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9209 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
9210 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
9211 e600f124 2021-03-21 stsp }
9212 e600f124 2021-03-21 stsp
9213 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
9214 7ef62c4e 2020-02-24 stsp worktree);
9215 818c7501 2019-07-11 stsp if (error)
9216 7ef62c4e 2020-02-24 stsp goto done;
9217 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
9218 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
9219 f259c4c1 2021-09-24 stsp goto done;
9220 f259c4c1 2021-09-24 stsp }
9221 f259c4c1 2021-09-24 stsp
9222 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress,
9223 f259c4c1 2021-09-24 stsp worktree, repo);
9224 f259c4c1 2021-09-24 stsp if (error)
9225 f259c4c1 2021-09-24 stsp goto done;
9226 f259c4c1 2021-09-24 stsp if (merge_in_progress) {
9227 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_MERGE_BUSY);
9228 818c7501 2019-07-11 stsp goto done;
9229 7ef62c4e 2020-02-24 stsp }
9230 818c7501 2019-07-11 stsp
9231 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9232 818c7501 2019-07-11 stsp if (error)
9233 818c7501 2019-07-11 stsp goto done;
9234 818c7501 2019-07-11 stsp
9235 f6794adc 2019-07-23 stsp if (abort_rebase) {
9236 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
9237 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
9238 f6794adc 2019-07-23 stsp goto done;
9239 f6794adc 2019-07-23 stsp }
9240 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
9241 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
9242 3e3a69f1 2019-07-25 stsp worktree, repo);
9243 818c7501 2019-07-11 stsp if (error)
9244 818c7501 2019-07-11 stsp goto done;
9245 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
9246 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
9247 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
9248 41f061b2 2021-10-05 stsp new_base_branch, abort_progress, &upa);
9249 818c7501 2019-07-11 stsp if (error)
9250 818c7501 2019-07-11 stsp goto done;
9251 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
9252 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
9253 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
9254 818c7501 2019-07-11 stsp }
9255 818c7501 2019-07-11 stsp
9256 818c7501 2019-07-11 stsp if (continue_rebase) {
9257 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
9258 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
9259 f6794adc 2019-07-23 stsp goto done;
9260 f6794adc 2019-07-23 stsp }
9261 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
9262 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
9263 3e3a69f1 2019-07-25 stsp worktree, repo);
9264 818c7501 2019-07-11 stsp if (error)
9265 818c7501 2019-07-11 stsp goto done;
9266 818c7501 2019-07-11 stsp
9267 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
9268 01757395 2019-07-12 stsp resume_commit_id, repo);
9269 818c7501 2019-07-11 stsp if (error)
9270 818c7501 2019-07-11 stsp goto done;
9271 818c7501 2019-07-11 stsp
9272 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
9273 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
9274 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
9275 818c7501 2019-07-11 stsp goto done;
9276 818c7501 2019-07-11 stsp }
9277 818c7501 2019-07-11 stsp } else {
9278 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
9279 818c7501 2019-07-11 stsp if (error != NULL)
9280 818c7501 2019-07-11 stsp goto done;
9281 ff0d2220 2019-07-11 stsp }
9282 818c7501 2019-07-11 stsp
9283 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
9284 ff0d2220 2019-07-11 stsp if (error)
9285 ff0d2220 2019-07-11 stsp goto done;
9286 ff0d2220 2019-07-11 stsp
9287 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
9288 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
9289 a51a74b3 2019-07-27 stsp
9290 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
9291 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9292 4e91ef15 2021-09-26 stsp base_commit_id, branch_head_commit_id, 1, repo,
9293 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
9294 818c7501 2019-07-11 stsp if (error)
9295 818c7501 2019-07-11 stsp goto done;
9296 818c7501 2019-07-11 stsp if (yca_id == NULL) {
9297 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
9298 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
9299 818c7501 2019-07-11 stsp "with work tree's branch");
9300 818c7501 2019-07-11 stsp goto done;
9301 818c7501 2019-07-11 stsp }
9302 818c7501 2019-07-11 stsp
9303 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
9304 a51a74b3 2019-07-27 stsp if (error) {
9305 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
9306 a51a74b3 2019-07-27 stsp goto done;
9307 a51a74b3 2019-07-27 stsp error = NULL;
9308 a51a74b3 2019-07-27 stsp } else {
9309 3d42b266 2021-11-03 jrick struct got_pathlist_head paths;
9310 3d42b266 2021-11-03 jrick printf("%s is already based on %s\n",
9311 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
9312 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
9313 3d42b266 2021-11-03 jrick error = switch_head_ref(branch, branch_head_commit_id,
9314 3d42b266 2021-11-03 jrick worktree, repo);
9315 3d42b266 2021-11-03 jrick if (error)
9316 3d42b266 2021-11-03 jrick goto done;
9317 3d42b266 2021-11-03 jrick error = got_worktree_set_base_commit_id(worktree, repo,
9318 3d42b266 2021-11-03 jrick branch_head_commit_id);
9319 3d42b266 2021-11-03 jrick if (error)
9320 3d42b266 2021-11-03 jrick goto done;
9321 3d42b266 2021-11-03 jrick TAILQ_INIT(&paths);
9322 3d42b266 2021-11-03 jrick error = got_pathlist_append(&paths, "", NULL);
9323 3d42b266 2021-11-03 jrick if (error)
9324 3d42b266 2021-11-03 jrick goto done;
9325 3d42b266 2021-11-03 jrick error = got_worktree_checkout_files(worktree,
9326 3d42b266 2021-11-03 jrick &paths, repo, update_progress, &upa,
9327 3d42b266 2021-11-03 jrick check_cancelled, NULL);
9328 3d42b266 2021-11-03 jrick got_pathlist_free(&paths);
9329 3d42b266 2021-11-03 jrick if (error)
9330 3d42b266 2021-11-03 jrick goto done;
9331 3d42b266 2021-11-03 jrick if (upa.did_something) {
9332 3d42b266 2021-11-03 jrick char *id_str;
9333 3d42b266 2021-11-03 jrick error = got_object_id_str(&id_str,
9334 3d42b266 2021-11-03 jrick branch_head_commit_id);
9335 3d42b266 2021-11-03 jrick if (error)
9336 3d42b266 2021-11-03 jrick goto done;
9337 3d42b266 2021-11-03 jrick printf("Updated to %s: %s\n",
9338 3d42b266 2021-11-03 jrick got_worktree_get_head_ref_name(worktree),
9339 3d42b266 2021-11-03 jrick id_str);
9340 3d42b266 2021-11-03 jrick free(id_str);
9341 3d42b266 2021-11-03 jrick } else
9342 3d42b266 2021-11-03 jrick printf("Already up-to-date\n");
9343 3d42b266 2021-11-03 jrick print_update_progress_stats(&upa);
9344 a51a74b3 2019-07-27 stsp goto done;
9345 a51a74b3 2019-07-27 stsp }
9346 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
9347 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
9348 818c7501 2019-07-11 stsp if (error)
9349 818c7501 2019-07-11 stsp goto done;
9350 818c7501 2019-07-11 stsp }
9351 818c7501 2019-07-11 stsp
9352 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
9353 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
9354 818c7501 2019-07-11 stsp if (error)
9355 818c7501 2019-07-11 stsp goto done;
9356 818c7501 2019-07-11 stsp
9357 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9358 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
9359 fc66b545 2019-08-12 stsp if (pid == NULL) {
9360 fc66b545 2019-08-12 stsp if (!continue_rebase) {
9361 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
9362 41f061b2 2021-10-05 stsp repo, new_base_branch, abort_progress, &upa);
9363 fc66b545 2019-08-12 stsp if (error)
9364 fc66b545 2019-08-12 stsp goto done;
9365 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
9366 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
9367 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
9368 9627c110 2020-04-18 stsp
9369 fc66b545 2019-08-12 stsp }
9370 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
9371 fc66b545 2019-08-12 stsp goto done;
9372 fc66b545 2019-08-12 stsp }
9373 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
9374 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
9375 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
9376 818c7501 2019-07-11 stsp got_object_commit_close(commit);
9377 818c7501 2019-07-11 stsp commit = NULL;
9378 818c7501 2019-07-11 stsp if (error)
9379 818c7501 2019-07-11 stsp goto done;
9380 64c6d990 2019-07-11 stsp
9381 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
9382 38b0338b 2019-11-29 stsp if (continue_rebase) {
9383 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
9384 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
9385 e600f124 2021-03-21 stsp create_backup);
9386 38b0338b 2019-11-29 stsp goto done;
9387 38b0338b 2019-11-29 stsp } else {
9388 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
9389 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
9390 38b0338b 2019-11-29 stsp char *id_str;
9391 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
9392 38b0338b 2019-11-29 stsp new_base_branch);
9393 38b0338b 2019-11-29 stsp if (error)
9394 38b0338b 2019-11-29 stsp goto done;
9395 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
9396 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
9397 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
9398 38b0338b 2019-11-29 stsp free(id_str);
9399 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
9400 38b0338b 2019-11-29 stsp new_head_commit_id);
9401 38b0338b 2019-11-29 stsp if (error)
9402 38b0338b 2019-11-29 stsp goto done;
9403 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
9404 e600f124 2021-03-21 stsp create_backup = 0;
9405 38b0338b 2019-11-29 stsp }
9406 818c7501 2019-07-11 stsp }
9407 818c7501 2019-07-11 stsp
9408 818c7501 2019-07-11 stsp pid = NULL;
9409 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
9410 9627c110 2020-04-18 stsp
9411 818c7501 2019-07-11 stsp commit_id = qid->id;
9412 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
9413 818c7501 2019-07-11 stsp pid = qid;
9414 818c7501 2019-07-11 stsp
9415 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9416 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
9417 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
9418 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
9419 818c7501 2019-07-11 stsp if (error)
9420 818c7501 2019-07-11 stsp goto done;
9421 9627c110 2020-04-18 stsp
9422 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
9423 1fa49072 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
9424 1fa49072 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
9425 1fa49072 2021-09-28 stsp if (upa.conflicts > 0) {
9426 1fa49072 2021-09-28 stsp error = show_rebase_merge_conflict(qid->id,
9427 1fa49072 2021-09-28 stsp repo);
9428 1fa49072 2021-09-28 stsp if (error)
9429 1fa49072 2021-09-28 stsp goto done;
9430 1fa49072 2021-09-28 stsp }
9431 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9432 818c7501 2019-07-11 stsp break;
9433 01757395 2019-07-12 stsp }
9434 818c7501 2019-07-11 stsp
9435 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
9436 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
9437 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
9438 818c7501 2019-07-11 stsp if (error)
9439 818c7501 2019-07-11 stsp goto done;
9440 818c7501 2019-07-11 stsp }
9441 818c7501 2019-07-11 stsp
9442 1fa49072 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
9443 1fa49072 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
9444 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
9445 818c7501 2019-07-11 stsp if (error)
9446 818c7501 2019-07-11 stsp goto done;
9447 1fa49072 2021-09-28 stsp if (upa.conflicts > 0 && upa.missing == 0 &&
9448 1fa49072 2021-09-28 stsp upa.not_deleted == 0 && upa.unversioned == 0) {
9449 1fa49072 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9450 1fa49072 2021-09-28 stsp "conflicts must be resolved before rebasing "
9451 1fa49072 2021-09-28 stsp "can continue");
9452 1fa49072 2021-09-28 stsp } else if (upa.conflicts > 0) {
9453 1fa49072 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9454 1fa49072 2021-09-28 stsp "conflicts must be resolved before rebasing "
9455 1fa49072 2021-09-28 stsp "can continue; changes destined for some "
9456 1fa49072 2021-09-28 stsp "files were not yet merged and should be "
9457 1fa49072 2021-09-28 stsp "merged manually if required before the "
9458 1fa49072 2021-09-28 stsp "rebase operation is continued");
9459 1fa49072 2021-09-28 stsp } else {
9460 1fa49072 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
9461 1fa49072 2021-09-28 stsp "changes destined for some files were not "
9462 1fa49072 2021-09-28 stsp "yet merged and should be merged manually "
9463 1fa49072 2021-09-28 stsp "if required before the rebase operation "
9464 1fa49072 2021-09-28 stsp "is continued");
9465 1fa49072 2021-09-28 stsp }
9466 818c7501 2019-07-11 stsp } else
9467 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
9468 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
9469 818c7501 2019-07-11 stsp done:
9470 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
9471 818c7501 2019-07-11 stsp free(branch_head_commit_id);
9472 818c7501 2019-07-11 stsp free(resume_commit_id);
9473 818c7501 2019-07-11 stsp free(yca_id);
9474 818c7501 2019-07-11 stsp if (commit)
9475 818c7501 2019-07-11 stsp got_object_commit_close(commit);
9476 818c7501 2019-07-11 stsp if (branch)
9477 818c7501 2019-07-11 stsp got_ref_close(branch);
9478 818c7501 2019-07-11 stsp if (new_base_branch)
9479 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
9480 818c7501 2019-07-11 stsp if (tmp_branch)
9481 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
9482 5ef14e63 2019-06-02 stsp if (worktree)
9483 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
9484 1d0f4054 2021-06-17 stsp if (repo) {
9485 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
9486 1d0f4054 2021-06-17 stsp if (error == NULL)
9487 1d0f4054 2021-06-17 stsp error = close_err;
9488 1d0f4054 2021-06-17 stsp }
9489 5ef14e63 2019-06-02 stsp return error;
9490 0ebf8283 2019-07-24 stsp }
9491 0ebf8283 2019-07-24 stsp
9492 0ebf8283 2019-07-24 stsp __dead static void
9493 0ebf8283 2019-07-24 stsp usage_histedit(void)
9494 0ebf8283 2019-07-24 stsp {
9495 b93c7142 2021-10-01 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
9496 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
9497 643b85bc 2021-07-16 stsp getprogname());
9498 0ebf8283 2019-07-24 stsp exit(1);
9499 0ebf8283 2019-07-24 stsp }
9500 0ebf8283 2019-07-24 stsp
9501 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
9502 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
9503 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
9504 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
9505 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
9506 0ebf8283 2019-07-24 stsp
9507 3e166534 2022-02-16 naddy static const struct got_histedit_cmd {
9508 0ebf8283 2019-07-24 stsp unsigned char code;
9509 0ebf8283 2019-07-24 stsp const char *name;
9510 0ebf8283 2019-07-24 stsp const char *desc;
9511 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
9512 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
9513 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9514 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9515 82997472 2020-01-29 stsp "be used" },
9516 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9517 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
9518 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
9519 0ebf8283 2019-07-24 stsp };
9520 0ebf8283 2019-07-24 stsp
9521 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
9522 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
9523 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
9524 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9525 0ebf8283 2019-07-24 stsp char *logmsg;
9526 0ebf8283 2019-07-24 stsp };
9527 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9528 0ebf8283 2019-07-24 stsp
9529 0ebf8283 2019-07-24 stsp static const struct got_error *
9530 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9531 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9532 0ebf8283 2019-07-24 stsp {
9533 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9534 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
9535 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9536 8138f3e1 2019-08-11 stsp int n;
9537 0ebf8283 2019-07-24 stsp
9538 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
9539 0ebf8283 2019-07-24 stsp if (err)
9540 0ebf8283 2019-07-24 stsp goto done;
9541 0ebf8283 2019-07-24 stsp
9542 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
9543 0ebf8283 2019-07-24 stsp if (err)
9544 0ebf8283 2019-07-24 stsp goto done;
9545 0ebf8283 2019-07-24 stsp
9546 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
9547 0ebf8283 2019-07-24 stsp if (err)
9548 0ebf8283 2019-07-24 stsp goto done;
9549 0ebf8283 2019-07-24 stsp
9550 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9551 0ebf8283 2019-07-24 stsp if (n < 0)
9552 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9553 0ebf8283 2019-07-24 stsp done:
9554 0ebf8283 2019-07-24 stsp if (commit)
9555 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9556 0ebf8283 2019-07-24 stsp free(id_str);
9557 0ebf8283 2019-07-24 stsp free(logmsg);
9558 0ebf8283 2019-07-24 stsp return err;
9559 0ebf8283 2019-07-24 stsp }
9560 0ebf8283 2019-07-24 stsp
9561 0ebf8283 2019-07-24 stsp static const struct got_error *
9562 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
9563 b93c7142 2021-10-01 stsp FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
9564 b93c7142 2021-10-01 stsp struct got_repository *repo)
9565 0ebf8283 2019-07-24 stsp {
9566 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9567 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
9568 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
9569 0ebf8283 2019-07-24 stsp
9570 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9571 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9572 0ebf8283 2019-07-24 stsp
9573 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9574 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
9575 b93c7142 2021-10-01 stsp if (edit_only)
9576 b93c7142 2021-10-01 stsp histedit_cmd = "edit";
9577 b93c7142 2021-10-01 stsp else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9578 466785b9 2020-12-10 jrick histedit_cmd = "fold";
9579 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9580 0ebf8283 2019-07-24 stsp if (err)
9581 0ebf8283 2019-07-24 stsp break;
9582 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
9583 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9584 083957f4 2020-02-24 stsp if (n < 0) {
9585 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
9586 083957f4 2020-02-24 stsp break;
9587 083957f4 2020-02-24 stsp }
9588 083957f4 2020-02-24 stsp }
9589 0ebf8283 2019-07-24 stsp }
9590 0ebf8283 2019-07-24 stsp
9591 0ebf8283 2019-07-24 stsp return err;
9592 0ebf8283 2019-07-24 stsp }
9593 0ebf8283 2019-07-24 stsp
9594 0ebf8283 2019-07-24 stsp static const struct got_error *
9595 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
9596 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
9597 0ebf8283 2019-07-24 stsp {
9598 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9599 6059809a 2020-12-17 stsp size_t i;
9600 6059809a 2020-12-17 stsp int n;
9601 514f2ffe 2020-01-29 stsp char *id_str;
9602 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
9603 514f2ffe 2020-01-29 stsp
9604 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
9605 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
9606 514f2ffe 2020-01-29 stsp if (err)
9607 514f2ffe 2020-01-29 stsp return err;
9608 514f2ffe 2020-01-29 stsp
9609 514f2ffe 2020-01-29 stsp n = fprintf(f,
9610 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
9611 514f2ffe 2020-01-29 stsp "# commit %s\n"
9612 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
9613 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
9614 514f2ffe 2020-01-29 stsp if (n < 0) {
9615 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9616 514f2ffe 2020-01-29 stsp goto done;
9617 514f2ffe 2020-01-29 stsp }
9618 0ebf8283 2019-07-24 stsp
9619 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
9620 514f2ffe 2020-01-29 stsp if (n < 0) {
9621 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9622 514f2ffe 2020-01-29 stsp goto done;
9623 514f2ffe 2020-01-29 stsp }
9624 0ebf8283 2019-07-24 stsp
9625 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9626 3e166534 2022-02-16 naddy const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9627 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9628 0ebf8283 2019-07-24 stsp cmd->desc);
9629 0ebf8283 2019-07-24 stsp if (n < 0) {
9630 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9631 0ebf8283 2019-07-24 stsp break;
9632 0ebf8283 2019-07-24 stsp }
9633 0ebf8283 2019-07-24 stsp }
9634 514f2ffe 2020-01-29 stsp done:
9635 514f2ffe 2020-01-29 stsp free(id_str);
9636 0ebf8283 2019-07-24 stsp return err;
9637 0ebf8283 2019-07-24 stsp }
9638 0ebf8283 2019-07-24 stsp
9639 0ebf8283 2019-07-24 stsp static const struct got_error *
9640 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
9641 0ebf8283 2019-07-24 stsp {
9642 0ebf8283 2019-07-24 stsp static char msg[42];
9643 0ebf8283 2019-07-24 stsp int ret;
9644 0ebf8283 2019-07-24 stsp
9645 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9646 0ebf8283 2019-07-24 stsp lineno);
9647 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
9648 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9649 0ebf8283 2019-07-24 stsp
9650 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9651 0ebf8283 2019-07-24 stsp }
9652 0ebf8283 2019-07-24 stsp
9653 0ebf8283 2019-07-24 stsp static const struct got_error *
9654 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9655 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
9656 0ebf8283 2019-07-24 stsp {
9657 0ebf8283 2019-07-24 stsp const struct got_error *err;
9658 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
9659 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
9660 0ebf8283 2019-07-24 stsp
9661 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9662 0ebf8283 2019-07-24 stsp if (err)
9663 0ebf8283 2019-07-24 stsp return err;
9664 0ebf8283 2019-07-24 stsp
9665 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9666 0ebf8283 2019-07-24 stsp if (err)
9667 0ebf8283 2019-07-24 stsp goto done;
9668 0ebf8283 2019-07-24 stsp
9669 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9670 5943eee2 2019-08-13 stsp if (err)
9671 5943eee2 2019-08-13 stsp goto done;
9672 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9673 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9674 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
9675 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9676 0ebf8283 2019-07-24 stsp }
9677 0ebf8283 2019-07-24 stsp done:
9678 0ebf8283 2019-07-24 stsp if (folded_commit)
9679 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
9680 0ebf8283 2019-07-24 stsp free(id_str);
9681 5943eee2 2019-08-13 stsp free(folded_logmsg);
9682 0ebf8283 2019-07-24 stsp return err;
9683 0ebf8283 2019-07-24 stsp }
9684 0ebf8283 2019-07-24 stsp
9685 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
9686 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
9687 0ebf8283 2019-07-24 stsp {
9688 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
9689 0ebf8283 2019-07-24 stsp
9690 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
9691 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9692 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
9693 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9694 3f9de99f 2019-07-24 stsp folded = prev;
9695 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
9696 0ebf8283 2019-07-24 stsp }
9697 0ebf8283 2019-07-24 stsp
9698 0ebf8283 2019-07-24 stsp return folded;
9699 0ebf8283 2019-07-24 stsp }
9700 0ebf8283 2019-07-24 stsp
9701 0ebf8283 2019-07-24 stsp static const struct got_error *
9702 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9703 0ebf8283 2019-07-24 stsp struct got_repository *repo)
9704 0ebf8283 2019-07-24 stsp {
9705 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9706 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9707 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9708 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9709 1601cb9f 2020-09-11 naddy int logmsg_len;
9710 0ebf8283 2019-07-24 stsp int fd;
9711 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
9712 0ebf8283 2019-07-24 stsp
9713 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9714 0ebf8283 2019-07-24 stsp if (err)
9715 0ebf8283 2019-07-24 stsp return err;
9716 0ebf8283 2019-07-24 stsp
9717 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
9718 0ebf8283 2019-07-24 stsp if (folded) {
9719 0ebf8283 2019-07-24 stsp while (folded != hle) {
9720 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9721 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9722 3f9de99f 2019-07-24 stsp continue;
9723 3f9de99f 2019-07-24 stsp }
9724 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
9725 0ebf8283 2019-07-24 stsp logmsg, repo);
9726 0ebf8283 2019-07-24 stsp if (err)
9727 0ebf8283 2019-07-24 stsp goto done;
9728 0ebf8283 2019-07-24 stsp free(logmsg);
9729 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9730 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9731 0ebf8283 2019-07-24 stsp }
9732 0ebf8283 2019-07-24 stsp }
9733 0ebf8283 2019-07-24 stsp
9734 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9735 0ebf8283 2019-07-24 stsp if (err)
9736 0ebf8283 2019-07-24 stsp goto done;
9737 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9738 5943eee2 2019-08-13 stsp if (err)
9739 5943eee2 2019-08-13 stsp goto done;
9740 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
9741 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
9742 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
9743 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
9744 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9745 0ebf8283 2019-07-24 stsp goto done;
9746 0ebf8283 2019-07-24 stsp }
9747 0ebf8283 2019-07-24 stsp free(logmsg);
9748 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9749 0ebf8283 2019-07-24 stsp
9750 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9751 0ebf8283 2019-07-24 stsp if (err)
9752 0ebf8283 2019-07-24 stsp goto done;
9753 0ebf8283 2019-07-24 stsp
9754 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
9755 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
9756 0ebf8283 2019-07-24 stsp if (err)
9757 0ebf8283 2019-07-24 stsp goto done;
9758 0ebf8283 2019-07-24 stsp
9759 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
9760 0ebf8283 2019-07-24 stsp close(fd);
9761 0ebf8283 2019-07-24 stsp
9762 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9763 0ebf8283 2019-07-24 stsp if (err)
9764 0ebf8283 2019-07-24 stsp goto done;
9765 0ebf8283 2019-07-24 stsp
9766 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9767 0d5bb276 2020-12-15 stsp logmsg_len, 0);
9768 0ebf8283 2019-07-24 stsp if (err) {
9769 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9770 0ebf8283 2019-07-24 stsp goto done;
9771 71392a05 2020-12-13 stsp err = NULL;
9772 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
9773 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
9774 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
9775 0ebf8283 2019-07-24 stsp }
9776 0ebf8283 2019-07-24 stsp done:
9777 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9778 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
9779 0ebf8283 2019-07-24 stsp free(logmsg_path);
9780 0ebf8283 2019-07-24 stsp free(logmsg);
9781 5943eee2 2019-08-13 stsp free(orig_logmsg);
9782 0ebf8283 2019-07-24 stsp free(editor);
9783 0ebf8283 2019-07-24 stsp if (commit)
9784 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9785 0ebf8283 2019-07-24 stsp return err;
9786 5ef14e63 2019-06-02 stsp }
9787 0ebf8283 2019-07-24 stsp
9788 0ebf8283 2019-07-24 stsp static const struct got_error *
9789 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
9790 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9791 0ebf8283 2019-07-24 stsp {
9792 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9793 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
9794 6059809a 2020-12-17 stsp size_t i, size;
9795 0ebf8283 2019-07-24 stsp ssize_t len;
9796 6059809a 2020-12-17 stsp int lineno = 0;
9797 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9798 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
9799 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
9800 0ebf8283 2019-07-24 stsp
9801 0ebf8283 2019-07-24 stsp for (;;) {
9802 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
9803 0ebf8283 2019-07-24 stsp if (len == -1) {
9804 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
9805 0ebf8283 2019-07-24 stsp if (feof(f))
9806 0ebf8283 2019-07-24 stsp break;
9807 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
9808 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
9809 0ebf8283 2019-07-24 stsp break;
9810 0ebf8283 2019-07-24 stsp }
9811 0ebf8283 2019-07-24 stsp lineno++;
9812 0ebf8283 2019-07-24 stsp p = line;
9813 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9814 0ebf8283 2019-07-24 stsp p++;
9815 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
9816 0ebf8283 2019-07-24 stsp free(line);
9817 0ebf8283 2019-07-24 stsp line = NULL;
9818 0ebf8283 2019-07-24 stsp continue;
9819 0ebf8283 2019-07-24 stsp }
9820 0ebf8283 2019-07-24 stsp cmd = NULL;
9821 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9822 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
9823 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9824 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
9825 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
9826 0ebf8283 2019-07-24 stsp break;
9827 0ebf8283 2019-07-24 stsp }
9828 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9829 0ebf8283 2019-07-24 stsp p++;
9830 0ebf8283 2019-07-24 stsp break;
9831 0ebf8283 2019-07-24 stsp }
9832 0ebf8283 2019-07-24 stsp }
9833 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
9834 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9835 0ebf8283 2019-07-24 stsp break;
9836 0ebf8283 2019-07-24 stsp }
9837 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9838 0ebf8283 2019-07-24 stsp p++;
9839 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
9840 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
9841 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
9842 0ebf8283 2019-07-24 stsp break;
9843 0ebf8283 2019-07-24 stsp }
9844 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
9845 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9846 0ebf8283 2019-07-24 stsp if (err)
9847 0ebf8283 2019-07-24 stsp break;
9848 0ebf8283 2019-07-24 stsp } else {
9849 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
9850 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
9851 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9852 0ebf8283 2019-07-24 stsp break;
9853 0ebf8283 2019-07-24 stsp }
9854 0ebf8283 2019-07-24 stsp }
9855 0ebf8283 2019-07-24 stsp free(line);
9856 0ebf8283 2019-07-24 stsp line = NULL;
9857 0ebf8283 2019-07-24 stsp continue;
9858 0ebf8283 2019-07-24 stsp } else {
9859 0ebf8283 2019-07-24 stsp end = p;
9860 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
9861 0ebf8283 2019-07-24 stsp end++;
9862 0ebf8283 2019-07-24 stsp *end = '\0';
9863 0ebf8283 2019-07-24 stsp
9864 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
9865 0ebf8283 2019-07-24 stsp if (err) {
9866 0ebf8283 2019-07-24 stsp /* override error code */
9867 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9868 0ebf8283 2019-07-24 stsp break;
9869 0ebf8283 2019-07-24 stsp }
9870 0ebf8283 2019-07-24 stsp }
9871 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
9872 0ebf8283 2019-07-24 stsp if (hle == NULL) {
9873 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
9874 0ebf8283 2019-07-24 stsp break;
9875 0ebf8283 2019-07-24 stsp }
9876 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
9877 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
9878 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
9879 0ebf8283 2019-07-24 stsp commit_id = NULL;
9880 0ebf8283 2019-07-24 stsp free(line);
9881 0ebf8283 2019-07-24 stsp line = NULL;
9882 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9883 0ebf8283 2019-07-24 stsp }
9884 0ebf8283 2019-07-24 stsp
9885 0ebf8283 2019-07-24 stsp free(line);
9886 0ebf8283 2019-07-24 stsp free(commit_id);
9887 0ebf8283 2019-07-24 stsp return err;
9888 0ebf8283 2019-07-24 stsp }
9889 0ebf8283 2019-07-24 stsp
9890 0ebf8283 2019-07-24 stsp static const struct got_error *
9891 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
9892 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
9893 bfce7f83 2019-07-27 stsp {
9894 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
9895 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
9896 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9897 5b87815e 2020-03-05 stsp static char msg[92];
9898 bfce7f83 2019-07-27 stsp char *id_str;
9899 bfce7f83 2019-07-27 stsp
9900 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
9901 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9902 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
9903 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9904 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9905 5b87815e 2020-03-05 stsp
9906 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9907 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
9908 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9909 5b87815e 2020-03-05 stsp if (hle == hle2)
9910 5b87815e 2020-03-05 stsp continue;
9911 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
9912 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
9913 5b87815e 2020-03-05 stsp continue;
9914 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
9915 5b87815e 2020-03-05 stsp if (err)
9916 5b87815e 2020-03-05 stsp return err;
9917 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
9918 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
9919 5b87815e 2020-03-05 stsp free(id_str);
9920 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9921 5b87815e 2020-03-05 stsp }
9922 5b87815e 2020-03-05 stsp }
9923 bfce7f83 2019-07-27 stsp
9924 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9925 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9926 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9927 bfce7f83 2019-07-27 stsp break;
9928 bfce7f83 2019-07-27 stsp }
9929 bfce7f83 2019-07-27 stsp if (hle == NULL) {
9930 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
9931 bfce7f83 2019-07-27 stsp if (err)
9932 bfce7f83 2019-07-27 stsp return err;
9933 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
9934 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
9935 bfce7f83 2019-07-27 stsp free(id_str);
9936 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9937 bfce7f83 2019-07-27 stsp }
9938 bfce7f83 2019-07-27 stsp }
9939 bfce7f83 2019-07-27 stsp
9940 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9941 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9942 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9943 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
9944 bfce7f83 2019-07-27 stsp
9945 bfce7f83 2019-07-27 stsp return NULL;
9946 bfce7f83 2019-07-27 stsp }
9947 bfce7f83 2019-07-27 stsp
9948 bfce7f83 2019-07-27 stsp static const struct got_error *
9949 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
9950 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
9951 bfce7f83 2019-07-27 stsp struct got_repository *repo)
9952 0ebf8283 2019-07-24 stsp {
9953 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9954 0ebf8283 2019-07-24 stsp char *editor;
9955 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9956 0ebf8283 2019-07-24 stsp
9957 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9958 0ebf8283 2019-07-24 stsp if (err)
9959 0ebf8283 2019-07-24 stsp return err;
9960 0ebf8283 2019-07-24 stsp
9961 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
9962 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
9963 0ebf8283 2019-07-24 stsp goto done;
9964 0ebf8283 2019-07-24 stsp }
9965 0ebf8283 2019-07-24 stsp
9966 00fe21f2 2021-12-31 stsp f = fopen(path, "re");
9967 0ebf8283 2019-07-24 stsp if (f == NULL) {
9968 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
9969 0ebf8283 2019-07-24 stsp goto done;
9970 0ebf8283 2019-07-24 stsp }
9971 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9972 bfce7f83 2019-07-27 stsp if (err)
9973 bfce7f83 2019-07-27 stsp goto done;
9974 bfce7f83 2019-07-27 stsp
9975 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
9976 0ebf8283 2019-07-24 stsp done:
9977 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9978 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9979 0ebf8283 2019-07-24 stsp free(editor);
9980 0ebf8283 2019-07-24 stsp return err;
9981 0ebf8283 2019-07-24 stsp }
9982 0ebf8283 2019-07-24 stsp
9983 0ebf8283 2019-07-24 stsp static const struct got_error *
9984 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9985 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
9986 514f2ffe 2020-01-29 stsp struct got_repository *);
9987 0ebf8283 2019-07-24 stsp
9988 0ebf8283 2019-07-24 stsp static const struct got_error *
9989 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
9990 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
9991 b93c7142 2021-10-01 stsp int edit_logmsg_only, int fold_only, int edit_only,
9992 b93c7142 2021-10-01 stsp struct got_repository *repo)
9993 0ebf8283 2019-07-24 stsp {
9994 0ebf8283 2019-07-24 stsp const struct got_error *err;
9995 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9996 0ebf8283 2019-07-24 stsp char *path = NULL;
9997 0ebf8283 2019-07-24 stsp
9998 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
9999 0ebf8283 2019-07-24 stsp if (err)
10000 0ebf8283 2019-07-24 stsp return err;
10001 0ebf8283 2019-07-24 stsp
10002 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
10003 0ebf8283 2019-07-24 stsp if (err)
10004 0ebf8283 2019-07-24 stsp goto done;
10005 0ebf8283 2019-07-24 stsp
10006 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10007 b93c7142 2021-10-01 stsp fold_only, edit_only, repo);
10008 0ebf8283 2019-07-24 stsp if (err)
10009 0ebf8283 2019-07-24 stsp goto done;
10010 0ebf8283 2019-07-24 stsp
10011 b93c7142 2021-10-01 stsp if (edit_logmsg_only || fold_only || edit_only) {
10012 083957f4 2020-02-24 stsp rewind(f);
10013 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
10014 083957f4 2020-02-24 stsp } else {
10015 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
10016 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
10017 0ebf8283 2019-07-24 stsp goto done;
10018 083957f4 2020-02-24 stsp }
10019 083957f4 2020-02-24 stsp f = NULL;
10020 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
10021 083957f4 2020-02-24 stsp if (err) {
10022 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10023 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
10024 083957f4 2020-02-24 stsp goto done;
10025 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
10026 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
10027 083957f4 2020-02-24 stsp }
10028 0ebf8283 2019-07-24 stsp }
10029 0ebf8283 2019-07-24 stsp done:
10030 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
10031 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
10032 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
10033 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
10034 0ebf8283 2019-07-24 stsp free(path);
10035 0ebf8283 2019-07-24 stsp return err;
10036 0ebf8283 2019-07-24 stsp }
10037 0ebf8283 2019-07-24 stsp
10038 0ebf8283 2019-07-24 stsp static const struct got_error *
10039 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
10040 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
10041 0ebf8283 2019-07-24 stsp {
10042 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10043 0ebf8283 2019-07-24 stsp char *path = NULL;
10044 0ebf8283 2019-07-24 stsp FILE *f = NULL;
10045 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
10046 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
10047 0ebf8283 2019-07-24 stsp
10048 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
10049 0ebf8283 2019-07-24 stsp if (err)
10050 0ebf8283 2019-07-24 stsp return err;
10051 0ebf8283 2019-07-24 stsp
10052 00fe21f2 2021-12-31 stsp f = fopen(path, "we");
10053 0ebf8283 2019-07-24 stsp if (f == NULL) {
10054 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
10055 0ebf8283 2019-07-24 stsp goto done;
10056 0ebf8283 2019-07-24 stsp }
10057 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
10058 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10059 0ebf8283 2019-07-24 stsp repo);
10060 0ebf8283 2019-07-24 stsp if (err)
10061 0ebf8283 2019-07-24 stsp break;
10062 0ebf8283 2019-07-24 stsp
10063 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
10064 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
10065 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
10066 0ebf8283 2019-07-24 stsp if (n < 0) {
10067 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
10068 0ebf8283 2019-07-24 stsp break;
10069 0ebf8283 2019-07-24 stsp }
10070 0ebf8283 2019-07-24 stsp }
10071 0ebf8283 2019-07-24 stsp }
10072 0ebf8283 2019-07-24 stsp done:
10073 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
10074 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
10075 0ebf8283 2019-07-24 stsp free(path);
10076 0ebf8283 2019-07-24 stsp if (commit)
10077 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10078 0ebf8283 2019-07-24 stsp return err;
10079 0ebf8283 2019-07-24 stsp }
10080 0ebf8283 2019-07-24 stsp
10081 bfce7f83 2019-07-27 stsp void
10082 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
10083 bfce7f83 2019-07-27 stsp {
10084 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
10085 bfce7f83 2019-07-27 stsp
10086 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
10087 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
10088 bfce7f83 2019-07-27 stsp free(hle);
10089 bfce7f83 2019-07-27 stsp }
10090 bfce7f83 2019-07-27 stsp }
10091 bfce7f83 2019-07-27 stsp
10092 0ebf8283 2019-07-24 stsp static const struct got_error *
10093 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
10094 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
10095 0ebf8283 2019-07-24 stsp {
10096 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
10097 0ebf8283 2019-07-24 stsp FILE *f = NULL;
10098 0ebf8283 2019-07-24 stsp
10099 00fe21f2 2021-12-31 stsp f = fopen(path, "re");
10100 0ebf8283 2019-07-24 stsp if (f == NULL) {
10101 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
10102 0ebf8283 2019-07-24 stsp goto done;
10103 0ebf8283 2019-07-24 stsp }
10104 0ebf8283 2019-07-24 stsp
10105 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
10106 0ebf8283 2019-07-24 stsp done:
10107 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
10108 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
10109 0ebf8283 2019-07-24 stsp return err;
10110 0ebf8283 2019-07-24 stsp }
10111 0ebf8283 2019-07-24 stsp
10112 0ebf8283 2019-07-24 stsp static const struct got_error *
10113 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
10114 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
10115 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
10116 0ebf8283 2019-07-24 stsp {
10117 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
10118 0ebf8283 2019-07-24 stsp int resp = ' ';
10119 0ebf8283 2019-07-24 stsp
10120 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
10121 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
10122 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
10123 0ebf8283 2019-07-24 stsp resp = getchar();
10124 a61a4414 2019-08-07 stsp if (resp == '\n')
10125 a61a4414 2019-08-07 stsp resp = getchar();
10126 426ebf2e 2019-07-25 stsp if (resp == 'c') {
10127 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
10128 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
10129 bfce7f83 2019-07-27 stsp repo);
10130 426ebf2e 2019-07-25 stsp if (err) {
10131 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10132 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
10133 426ebf2e 2019-07-25 stsp break;
10134 bfce7f83 2019-07-27 stsp prev_err = err;
10135 426ebf2e 2019-07-25 stsp resp = ' ';
10136 426ebf2e 2019-07-25 stsp continue;
10137 426ebf2e 2019-07-25 stsp }
10138 bfce7f83 2019-07-27 stsp break;
10139 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
10140 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
10141 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
10142 b93c7142 2021-10-01 stsp commits, branch_name, 0, 0, 0, repo);
10143 426ebf2e 2019-07-25 stsp if (err) {
10144 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10145 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
10146 426ebf2e 2019-07-25 stsp break;
10147 bfce7f83 2019-07-27 stsp prev_err = err;
10148 426ebf2e 2019-07-25 stsp resp = ' ';
10149 426ebf2e 2019-07-25 stsp continue;
10150 426ebf2e 2019-07-25 stsp }
10151 bfce7f83 2019-07-27 stsp break;
10152 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
10153 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10154 0ebf8283 2019-07-24 stsp break;
10155 426ebf2e 2019-07-25 stsp } else
10156 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
10157 0ebf8283 2019-07-24 stsp }
10158 0ebf8283 2019-07-24 stsp
10159 0ebf8283 2019-07-24 stsp return err;
10160 0ebf8283 2019-07-24 stsp }
10161 0ebf8283 2019-07-24 stsp
10162 0ebf8283 2019-07-24 stsp static const struct got_error *
10163 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
10164 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10165 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
10166 0ebf8283 2019-07-24 stsp {
10167 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10168 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10169 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10170 3e3a69f1 2019-07-25 stsp branch, repo);
10171 0ebf8283 2019-07-24 stsp }
10172 0ebf8283 2019-07-24 stsp
10173 0ebf8283 2019-07-24 stsp static const struct got_error *
10174 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
10175 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10176 0ebf8283 2019-07-24 stsp {
10177 0ebf8283 2019-07-24 stsp const struct got_error *err;
10178 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10179 0ebf8283 2019-07-24 stsp
10180 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
10181 0ebf8283 2019-07-24 stsp if (err)
10182 0ebf8283 2019-07-24 stsp goto done;
10183 0ebf8283 2019-07-24 stsp
10184 0ebf8283 2019-07-24 stsp if (new_id) {
10185 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
10186 0ebf8283 2019-07-24 stsp if (err)
10187 0ebf8283 2019-07-24 stsp goto done;
10188 0ebf8283 2019-07-24 stsp }
10189 0ebf8283 2019-07-24 stsp
10190 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
10191 0ebf8283 2019-07-24 stsp if (new_id_str)
10192 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
10193 0ebf8283 2019-07-24 stsp
10194 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
10195 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
10196 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
10197 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
10198 0ebf8283 2019-07-24 stsp goto done;
10199 0ebf8283 2019-07-24 stsp }
10200 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
10201 0ebf8283 2019-07-24 stsp } else {
10202 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
10203 0ebf8283 2019-07-24 stsp if (err)
10204 0ebf8283 2019-07-24 stsp goto done;
10205 0ebf8283 2019-07-24 stsp }
10206 0ebf8283 2019-07-24 stsp
10207 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
10208 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
10209 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
10210 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
10211 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
10212 0ebf8283 2019-07-24 stsp break;
10213 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
10214 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
10215 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10216 0ebf8283 2019-07-24 stsp logmsg);
10217 0ebf8283 2019-07-24 stsp break;
10218 0ebf8283 2019-07-24 stsp default:
10219 0ebf8283 2019-07-24 stsp break;
10220 0ebf8283 2019-07-24 stsp }
10221 0ebf8283 2019-07-24 stsp done:
10222 0ebf8283 2019-07-24 stsp free(old_id_str);
10223 0ebf8283 2019-07-24 stsp free(new_id_str);
10224 0ebf8283 2019-07-24 stsp return err;
10225 0ebf8283 2019-07-24 stsp }
10226 0ebf8283 2019-07-24 stsp
10227 0ebf8283 2019-07-24 stsp static const struct got_error *
10228 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
10229 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
10230 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10231 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
10232 0ebf8283 2019-07-24 stsp {
10233 0ebf8283 2019-07-24 stsp const struct got_error *err;
10234 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
10235 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
10236 0ebf8283 2019-07-24 stsp
10237 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10238 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
10239 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
10240 0ebf8283 2019-07-24 stsp if (err)
10241 0ebf8283 2019-07-24 stsp return err;
10242 0ebf8283 2019-07-24 stsp }
10243 0ebf8283 2019-07-24 stsp
10244 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10245 0ebf8283 2019-07-24 stsp if (err)
10246 0ebf8283 2019-07-24 stsp return err;
10247 0ebf8283 2019-07-24 stsp
10248 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
10249 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
10250 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
10251 0ebf8283 2019-07-24 stsp if (err) {
10252 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
10253 0ebf8283 2019-07-24 stsp goto done;
10254 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
10255 0ebf8283 2019-07-24 stsp } else {
10256 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
10257 0ebf8283 2019-07-24 stsp free(new_commit_id);
10258 0ebf8283 2019-07-24 stsp }
10259 0ebf8283 2019-07-24 stsp done:
10260 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10261 0ebf8283 2019-07-24 stsp return err;
10262 0ebf8283 2019-07-24 stsp }
10263 0ebf8283 2019-07-24 stsp
10264 0ebf8283 2019-07-24 stsp static const struct got_error *
10265 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
10266 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
10267 0ebf8283 2019-07-24 stsp {
10268 0ebf8283 2019-07-24 stsp const struct got_error *error;
10269 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
10270 0ebf8283 2019-07-24 stsp
10271 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
10272 0ebf8283 2019-07-24 stsp repo);
10273 0ebf8283 2019-07-24 stsp if (error)
10274 0ebf8283 2019-07-24 stsp return error;
10275 0ebf8283 2019-07-24 stsp
10276 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
10277 0ebf8283 2019-07-24 stsp if (error)
10278 0ebf8283 2019-07-24 stsp return error;
10279 0ebf8283 2019-07-24 stsp
10280 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
10281 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10282 0ebf8283 2019-07-24 stsp return error;
10283 ab20a43a 2020-01-29 stsp }
10284 ab20a43a 2020-01-29 stsp
10285 ab20a43a 2020-01-29 stsp static const struct got_error *
10286 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
10287 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
10288 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10289 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
10290 ab20a43a 2020-01-29 stsp {
10291 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
10292 ab20a43a 2020-01-29 stsp
10293 ab20a43a 2020-01-29 stsp switch (status) {
10294 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
10295 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
10296 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
10297 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
10298 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
10299 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
10300 ab20a43a 2020-01-29 stsp default:
10301 ab20a43a 2020-01-29 stsp break;
10302 ab20a43a 2020-01-29 stsp }
10303 ab20a43a 2020-01-29 stsp
10304 ab20a43a 2020-01-29 stsp switch (staged_status) {
10305 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
10306 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
10307 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
10308 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
10309 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
10310 ab20a43a 2020-01-29 stsp default:
10311 ab20a43a 2020-01-29 stsp break;
10312 ab20a43a 2020-01-29 stsp }
10313 ab20a43a 2020-01-29 stsp
10314 ab20a43a 2020-01-29 stsp return NULL;
10315 0ebf8283 2019-07-24 stsp }
10316 0ebf8283 2019-07-24 stsp
10317 0ebf8283 2019-07-24 stsp static const struct got_error *
10318 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
10319 0ebf8283 2019-07-24 stsp {
10320 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
10321 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
10322 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
10323 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
10324 0ebf8283 2019-07-24 stsp char *cwd = NULL;
10325 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
10326 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
10327 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
10328 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
10329 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
10330 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
10331 f259c4c1 2021-09-24 stsp int ch, rebase_in_progress = 0, merge_in_progress = 0;
10332 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10333 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10334 b93c7142 2021-10-01 stsp int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
10335 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
10336 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
10337 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
10338 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
10339 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
10340 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
10341 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
10342 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
10343 0ebf8283 2019-07-24 stsp
10344 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
10345 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
10346 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
10347 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10348 0ebf8283 2019-07-24 stsp
10349 b93c7142 2021-10-01 stsp while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
10350 0ebf8283 2019-07-24 stsp switch (ch) {
10351 0ebf8283 2019-07-24 stsp case 'a':
10352 0ebf8283 2019-07-24 stsp abort_edit = 1;
10353 0ebf8283 2019-07-24 stsp break;
10354 0ebf8283 2019-07-24 stsp case 'c':
10355 0ebf8283 2019-07-24 stsp continue_edit = 1;
10356 0ebf8283 2019-07-24 stsp break;
10357 b93c7142 2021-10-01 stsp case 'e':
10358 b93c7142 2021-10-01 stsp edit_only = 1;
10359 b93c7142 2021-10-01 stsp break;
10360 466785b9 2020-12-10 jrick case 'f':
10361 466785b9 2020-12-10 jrick fold_only = 1;
10362 466785b9 2020-12-10 jrick break;
10363 0ebf8283 2019-07-24 stsp case 'F':
10364 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
10365 0ebf8283 2019-07-24 stsp break;
10366 083957f4 2020-02-24 stsp case 'm':
10367 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
10368 083957f4 2020-02-24 stsp break;
10369 e600f124 2021-03-21 stsp case 'l':
10370 e600f124 2021-03-21 stsp list_backups = 1;
10371 e600f124 2021-03-21 stsp break;
10372 643b85bc 2021-07-16 stsp case 'X':
10373 643b85bc 2021-07-16 stsp delete_backups = 1;
10374 643b85bc 2021-07-16 stsp break;
10375 0ebf8283 2019-07-24 stsp default:
10376 0ebf8283 2019-07-24 stsp usage_histedit();
10377 0ebf8283 2019-07-24 stsp /* NOTREACHED */
10378 0ebf8283 2019-07-24 stsp }
10379 0ebf8283 2019-07-24 stsp }
10380 0ebf8283 2019-07-24 stsp
10381 0ebf8283 2019-07-24 stsp argc -= optind;
10382 0ebf8283 2019-07-24 stsp argv += optind;
10383 0ebf8283 2019-07-24 stsp
10384 0ebf8283 2019-07-24 stsp #ifndef PROFILE
10385 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10386 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
10387 0ebf8283 2019-07-24 stsp err(1, "pledge");
10388 0ebf8283 2019-07-24 stsp #endif
10389 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
10390 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
10391 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
10392 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
10393 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
10394 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
10395 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
10396 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
10397 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
10398 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
10399 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
10400 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
10401 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
10402 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
10403 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
10404 b3805337 2020-12-13 stsp option_conflict('F', 'f');
10405 b93c7142 2021-10-01 stsp if (abort_edit && edit_only)
10406 b93c7142 2021-10-01 stsp option_conflict('a', 'e');
10407 b93c7142 2021-10-01 stsp if (continue_edit && edit_only)
10408 b93c7142 2021-10-01 stsp option_conflict('c', 'e');
10409 b93c7142 2021-10-01 stsp if (edit_only && edit_logmsg_only)
10410 b93c7142 2021-10-01 stsp option_conflict('e', 'm');
10411 b93c7142 2021-10-01 stsp if (edit_script_path && edit_only)
10412 b93c7142 2021-10-01 stsp option_conflict('F', 'e');
10413 e600f124 2021-03-21 stsp if (list_backups) {
10414 e600f124 2021-03-21 stsp if (abort_edit)
10415 e600f124 2021-03-21 stsp option_conflict('l', 'a');
10416 e600f124 2021-03-21 stsp if (continue_edit)
10417 e600f124 2021-03-21 stsp option_conflict('l', 'c');
10418 e600f124 2021-03-21 stsp if (edit_script_path)
10419 e600f124 2021-03-21 stsp option_conflict('l', 'F');
10420 e600f124 2021-03-21 stsp if (edit_logmsg_only)
10421 e600f124 2021-03-21 stsp option_conflict('l', 'm');
10422 e600f124 2021-03-21 stsp if (fold_only)
10423 e600f124 2021-03-21 stsp option_conflict('l', 'f');
10424 b93c7142 2021-10-01 stsp if (edit_only)
10425 b93c7142 2021-10-01 stsp option_conflict('l', 'e');
10426 643b85bc 2021-07-16 stsp if (delete_backups)
10427 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
10428 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
10429 e600f124 2021-03-21 stsp usage_histedit();
10430 643b85bc 2021-07-16 stsp } else if (delete_backups) {
10431 643b85bc 2021-07-16 stsp if (abort_edit)
10432 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
10433 643b85bc 2021-07-16 stsp if (continue_edit)
10434 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
10435 643b85bc 2021-07-16 stsp if (edit_script_path)
10436 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
10437 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
10438 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
10439 643b85bc 2021-07-16 stsp if (fold_only)
10440 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
10441 b93c7142 2021-10-01 stsp if (edit_only)
10442 b93c7142 2021-10-01 stsp option_conflict('X', 'e');
10443 643b85bc 2021-07-16 stsp if (list_backups)
10444 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
10445 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
10446 643b85bc 2021-07-16 stsp usage_histedit();
10447 e600f124 2021-03-21 stsp } else if (argc != 0)
10448 0ebf8283 2019-07-24 stsp usage_histedit();
10449 0ebf8283 2019-07-24 stsp
10450 0ebf8283 2019-07-24 stsp /*
10451 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
10452 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
10453 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
10454 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
10455 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
10456 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
10457 0ebf8283 2019-07-24 stsp */
10458 0ebf8283 2019-07-24 stsp
10459 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
10460 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
10461 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
10462 0ebf8283 2019-07-24 stsp goto done;
10463 0ebf8283 2019-07-24 stsp }
10464 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
10465 fa51e947 2020-03-27 stsp if (error) {
10466 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
10467 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
10468 e600f124 2021-03-21 stsp goto done;
10469 e600f124 2021-03-21 stsp } else {
10470 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10471 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
10472 e600f124 2021-03-21 stsp "histedit", cwd);
10473 e600f124 2021-03-21 stsp goto done;
10474 e600f124 2021-03-21 stsp }
10475 e600f124 2021-03-21 stsp }
10476 e600f124 2021-03-21 stsp
10477 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
10478 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
10479 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
10480 e600f124 2021-03-21 stsp NULL);
10481 e600f124 2021-03-21 stsp if (error != NULL)
10482 e600f124 2021-03-21 stsp goto done;
10483 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10484 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
10485 e600f124 2021-03-21 stsp if (error)
10486 e600f124 2021-03-21 stsp goto done;
10487 643b85bc 2021-07-16 stsp error = process_backup_refs(
10488 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
10489 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
10490 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
10491 fa51e947 2020-03-27 stsp }
10492 0ebf8283 2019-07-24 stsp
10493 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10494 c9956ddf 2019-09-08 stsp NULL);
10495 0ebf8283 2019-07-24 stsp if (error != NULL)
10496 0ebf8283 2019-07-24 stsp goto done;
10497 0ebf8283 2019-07-24 stsp
10498 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10499 0ebf8283 2019-07-24 stsp if (error)
10500 0ebf8283 2019-07-24 stsp goto done;
10501 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
10502 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
10503 f259c4c1 2021-09-24 stsp goto done;
10504 f259c4c1 2021-09-24 stsp }
10505 f259c4c1 2021-09-24 stsp
10506 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10507 f259c4c1 2021-09-24 stsp repo);
10508 f259c4c1 2021-09-24 stsp if (error)
10509 f259c4c1 2021-09-24 stsp goto done;
10510 f259c4c1 2021-09-24 stsp if (merge_in_progress) {
10511 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_MERGE_BUSY);
10512 0ebf8283 2019-07-24 stsp goto done;
10513 0ebf8283 2019-07-24 stsp }
10514 0ebf8283 2019-07-24 stsp
10515 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
10516 0ebf8283 2019-07-24 stsp if (error)
10517 0ebf8283 2019-07-24 stsp goto done;
10518 0ebf8283 2019-07-24 stsp
10519 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
10520 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10521 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
10522 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
10523 083957f4 2020-02-24 stsp "before the -m option can be used");
10524 083957f4 2020-02-24 stsp goto done;
10525 083957f4 2020-02-24 stsp }
10526 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
10527 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10528 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
10529 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
10530 466785b9 2020-12-10 jrick "before the -f option can be used");
10531 466785b9 2020-12-10 jrick goto done;
10532 466785b9 2020-12-10 jrick }
10533 b93c7142 2021-10-01 stsp if (edit_in_progress && edit_only) {
10534 b93c7142 2021-10-01 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10535 b93c7142 2021-10-01 stsp "histedit operation is in progress in this "
10536 b93c7142 2021-10-01 stsp "work tree and must be continued or aborted "
10537 b93c7142 2021-10-01 stsp "before the -e option can be used");
10538 b93c7142 2021-10-01 stsp goto done;
10539 b93c7142 2021-10-01 stsp }
10540 083957f4 2020-02-24 stsp
10541 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
10542 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10543 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10544 3e3a69f1 2019-07-25 stsp worktree, repo);
10545 0ebf8283 2019-07-24 stsp if (error)
10546 0ebf8283 2019-07-24 stsp goto done;
10547 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10548 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10549 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
10550 41f061b2 2021-10-05 stsp branch, base_commit_id, abort_progress, &upa);
10551 0ebf8283 2019-07-24 stsp if (error)
10552 0ebf8283 2019-07-24 stsp goto done;
10553 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
10554 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10555 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10556 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
10557 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
10558 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10559 0ebf8283 2019-07-24 stsp goto done;
10560 0ebf8283 2019-07-24 stsp }
10561 0ebf8283 2019-07-24 stsp
10562 0ebf8283 2019-07-24 stsp if (continue_edit) {
10563 0ebf8283 2019-07-24 stsp char *path;
10564 0ebf8283 2019-07-24 stsp
10565 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
10566 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10567 0ebf8283 2019-07-24 stsp goto done;
10568 0ebf8283 2019-07-24 stsp }
10569 0ebf8283 2019-07-24 stsp
10570 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
10571 0ebf8283 2019-07-24 stsp if (error)
10572 0ebf8283 2019-07-24 stsp goto done;
10573 0ebf8283 2019-07-24 stsp
10574 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
10575 0ebf8283 2019-07-24 stsp free(path);
10576 0ebf8283 2019-07-24 stsp if (error)
10577 0ebf8283 2019-07-24 stsp goto done;
10578 0ebf8283 2019-07-24 stsp
10579 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10580 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10581 3e3a69f1 2019-07-25 stsp worktree, repo);
10582 0ebf8283 2019-07-24 stsp if (error)
10583 0ebf8283 2019-07-24 stsp goto done;
10584 0ebf8283 2019-07-24 stsp
10585 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10586 0ebf8283 2019-07-24 stsp if (error)
10587 0ebf8283 2019-07-24 stsp goto done;
10588 0ebf8283 2019-07-24 stsp
10589 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10590 0ebf8283 2019-07-24 stsp head_commit_id);
10591 0ebf8283 2019-07-24 stsp if (error)
10592 0ebf8283 2019-07-24 stsp goto done;
10593 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10594 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10595 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10596 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10597 3aac7cf7 2019-07-25 stsp goto done;
10598 3aac7cf7 2019-07-25 stsp }
10599 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10600 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
10601 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10602 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10603 0ebf8283 2019-07-24 stsp commit = NULL;
10604 0ebf8283 2019-07-24 stsp if (error)
10605 0ebf8283 2019-07-24 stsp goto done;
10606 0ebf8283 2019-07-24 stsp } else {
10607 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
10608 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
10609 0ebf8283 2019-07-24 stsp goto done;
10610 0ebf8283 2019-07-24 stsp }
10611 0ebf8283 2019-07-24 stsp
10612 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
10613 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
10614 0ebf8283 2019-07-24 stsp if (error != NULL)
10615 0ebf8283 2019-07-24 stsp goto done;
10616 0ebf8283 2019-07-24 stsp
10617 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10618 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10619 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
10620 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
10621 c7d20a3f 2019-07-30 stsp goto done;
10622 c7d20a3f 2019-07-30 stsp }
10623 c7d20a3f 2019-07-30 stsp
10624 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10625 bfce7f83 2019-07-27 stsp got_ref_close(branch);
10626 bfce7f83 2019-07-27 stsp branch = NULL;
10627 0ebf8283 2019-07-24 stsp if (error)
10628 0ebf8283 2019-07-24 stsp goto done;
10629 0ebf8283 2019-07-24 stsp
10630 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10631 0ebf8283 2019-07-24 stsp head_commit_id);
10632 0ebf8283 2019-07-24 stsp if (error)
10633 0ebf8283 2019-07-24 stsp goto done;
10634 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10635 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10636 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10637 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10638 3aac7cf7 2019-07-25 stsp goto done;
10639 3aac7cf7 2019-07-25 stsp }
10640 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10641 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
10642 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
10643 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10644 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10645 0ebf8283 2019-07-24 stsp commit = NULL;
10646 0ebf8283 2019-07-24 stsp if (error)
10647 0ebf8283 2019-07-24 stsp goto done;
10648 0ebf8283 2019-07-24 stsp
10649 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
10650 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10651 c5996fff 2020-01-29 stsp goto done;
10652 c5996fff 2020-01-29 stsp }
10653 c5996fff 2020-01-29 stsp
10654 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10655 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
10656 bfce7f83 2019-07-27 stsp if (error)
10657 bfce7f83 2019-07-27 stsp goto done;
10658 bfce7f83 2019-07-27 stsp
10659 0ebf8283 2019-07-24 stsp if (edit_script_path) {
10660 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
10661 0ebf8283 2019-07-24 stsp edit_script_path, repo);
10662 6ba2bea2 2019-07-27 stsp if (error) {
10663 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10664 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10665 41f061b2 2021-10-05 stsp abort_progress, &upa);
10666 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10667 0ebf8283 2019-07-24 stsp goto done;
10668 6ba2bea2 2019-07-27 stsp }
10669 0ebf8283 2019-07-24 stsp } else {
10670 514f2ffe 2020-01-29 stsp const char *branch_name;
10671 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
10672 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
10673 514f2ffe 2020-01-29 stsp branch_name += 11;
10674 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
10675 b93c7142 2021-10-01 stsp branch_name, edit_logmsg_only, fold_only,
10676 b93c7142 2021-10-01 stsp edit_only, repo);
10677 6ba2bea2 2019-07-27 stsp if (error) {
10678 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10679 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10680 41f061b2 2021-10-05 stsp abort_progress, &upa);
10681 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10682 0ebf8283 2019-07-24 stsp goto done;
10683 6ba2bea2 2019-07-27 stsp }
10684 0ebf8283 2019-07-24 stsp
10685 0ebf8283 2019-07-24 stsp }
10686 0ebf8283 2019-07-24 stsp
10687 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
10688 0ebf8283 2019-07-24 stsp repo);
10689 6ba2bea2 2019-07-27 stsp if (error) {
10690 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10691 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10692 41f061b2 2021-10-05 stsp abort_progress, &upa);
10693 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10694 0ebf8283 2019-07-24 stsp goto done;
10695 6ba2bea2 2019-07-27 stsp }
10696 0ebf8283 2019-07-24 stsp
10697 0ebf8283 2019-07-24 stsp }
10698 0ebf8283 2019-07-24 stsp
10699 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
10700 4ec14e60 2019-08-28 hiltjo if (error)
10701 0ebf8283 2019-07-24 stsp goto done;
10702 0ebf8283 2019-07-24 stsp
10703 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10704 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
10705 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
10706 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
10707 0ebf8283 2019-07-24 stsp continue;
10708 0ebf8283 2019-07-24 stsp
10709 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
10710 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10711 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
10712 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
10713 62d463ca 2020-10-20 naddy repo);
10714 ab20a43a 2020-01-29 stsp if (error)
10715 ab20a43a 2020-01-29 stsp goto done;
10716 0ebf8283 2019-07-24 stsp } else {
10717 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
10718 ab20a43a 2020-01-29 stsp int have_changes = 0;
10719 ab20a43a 2020-01-29 stsp
10720 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
10721 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
10722 ab20a43a 2020-01-29 stsp if (error)
10723 ab20a43a 2020-01-29 stsp goto done;
10724 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
10725 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
10726 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
10727 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
10728 ab20a43a 2020-01-29 stsp if (error) {
10729 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
10730 ab20a43a 2020-01-29 stsp goto done;
10731 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
10732 ab20a43a 2020-01-29 stsp goto done;
10733 ab20a43a 2020-01-29 stsp }
10734 ab20a43a 2020-01-29 stsp if (have_changes) {
10735 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
10736 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
10737 ab20a43a 2020-01-29 stsp if (error)
10738 ab20a43a 2020-01-29 stsp goto done;
10739 ab20a43a 2020-01-29 stsp } else {
10740 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
10741 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
10742 ab20a43a 2020-01-29 stsp if (error)
10743 ab20a43a 2020-01-29 stsp goto done;
10744 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
10745 ab20a43a 2020-01-29 stsp hle, NULL);
10746 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
10747 ab20a43a 2020-01-29 stsp commit = NULL;
10748 ab20a43a 2020-01-29 stsp if (error)
10749 ab20a43a 2020-01-29 stsp goto done;
10750 ab20a43a 2020-01-29 stsp }
10751 0ebf8283 2019-07-24 stsp }
10752 0ebf8283 2019-07-24 stsp continue;
10753 0ebf8283 2019-07-24 stsp }
10754 0ebf8283 2019-07-24 stsp
10755 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10756 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10757 0ebf8283 2019-07-24 stsp if (error)
10758 0ebf8283 2019-07-24 stsp goto done;
10759 0ebf8283 2019-07-24 stsp continue;
10760 0ebf8283 2019-07-24 stsp }
10761 0ebf8283 2019-07-24 stsp
10762 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10763 0ebf8283 2019-07-24 stsp hle->commit_id);
10764 0ebf8283 2019-07-24 stsp if (error)
10765 0ebf8283 2019-07-24 stsp goto done;
10766 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10767 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10768 0ebf8283 2019-07-24 stsp
10769 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
10770 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
10771 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
10772 0ebf8283 2019-07-24 stsp if (error)
10773 0ebf8283 2019-07-24 stsp goto done;
10774 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10775 0ebf8283 2019-07-24 stsp commit = NULL;
10776 0ebf8283 2019-07-24 stsp
10777 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
10778 cd33da48 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
10779 cd33da48 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
10780 cd33da48 2021-09-28 stsp if (upa.conflicts > 0) {
10781 cd33da48 2021-09-28 stsp error = show_rebase_merge_conflict(
10782 cd33da48 2021-09-28 stsp hle->commit_id, repo);
10783 cd33da48 2021-09-28 stsp if (error)
10784 cd33da48 2021-09-28 stsp goto done;
10785 cd33da48 2021-09-28 stsp }
10786 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10787 0ebf8283 2019-07-24 stsp break;
10788 0ebf8283 2019-07-24 stsp }
10789 0ebf8283 2019-07-24 stsp
10790 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10791 0ebf8283 2019-07-24 stsp char *id_str;
10792 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
10793 0ebf8283 2019-07-24 stsp if (error)
10794 0ebf8283 2019-07-24 stsp goto done;
10795 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
10796 0ebf8283 2019-07-24 stsp id_str);
10797 0ebf8283 2019-07-24 stsp free(id_str);
10798 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10799 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
10800 3e3a69f1 2019-07-25 stsp fileindex);
10801 0ebf8283 2019-07-24 stsp goto done;
10802 0ebf8283 2019-07-24 stsp }
10803 0ebf8283 2019-07-24 stsp
10804 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10805 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10806 0ebf8283 2019-07-24 stsp if (error)
10807 0ebf8283 2019-07-24 stsp goto done;
10808 0ebf8283 2019-07-24 stsp continue;
10809 0ebf8283 2019-07-24 stsp }
10810 0ebf8283 2019-07-24 stsp
10811 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
10812 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
10813 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10814 0ebf8283 2019-07-24 stsp if (error)
10815 0ebf8283 2019-07-24 stsp goto done;
10816 0ebf8283 2019-07-24 stsp }
10817 0ebf8283 2019-07-24 stsp
10818 cd33da48 2021-09-28 stsp if (upa.conflicts > 0 || upa.missing > 0 ||
10819 cd33da48 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
10820 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
10821 0ebf8283 2019-07-24 stsp if (error)
10822 0ebf8283 2019-07-24 stsp goto done;
10823 cd33da48 2021-09-28 stsp if (upa.conflicts > 0 && upa.missing == 0 &&
10824 cd33da48 2021-09-28 stsp upa.not_deleted == 0 && upa.unversioned == 0) {
10825 cd33da48 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10826 cd33da48 2021-09-28 stsp "conflicts must be resolved before histedit "
10827 cd33da48 2021-09-28 stsp "can continue");
10828 cd33da48 2021-09-28 stsp } else if (upa.conflicts > 0) {
10829 cd33da48 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10830 cd33da48 2021-09-28 stsp "conflicts must be resolved before histedit "
10831 cd33da48 2021-09-28 stsp "can continue; changes destined for some "
10832 cd33da48 2021-09-28 stsp "files were not yet merged and should be "
10833 cd33da48 2021-09-28 stsp "merged manually if required before the "
10834 cd33da48 2021-09-28 stsp "histedit operation is continued");
10835 cd33da48 2021-09-28 stsp } else {
10836 cd33da48 2021-09-28 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10837 cd33da48 2021-09-28 stsp "changes destined for some files were not "
10838 cd33da48 2021-09-28 stsp "yet merged and should be merged manually "
10839 cd33da48 2021-09-28 stsp "if required before the histedit operation "
10840 cd33da48 2021-09-28 stsp "is continued");
10841 cd33da48 2021-09-28 stsp }
10842 0ebf8283 2019-07-24 stsp } else
10843 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
10844 3e3a69f1 2019-07-25 stsp branch, repo);
10845 0ebf8283 2019-07-24 stsp done:
10846 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
10847 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
10848 0ebf8283 2019-07-24 stsp free(head_commit_id);
10849 0ebf8283 2019-07-24 stsp free(base_commit_id);
10850 0ebf8283 2019-07-24 stsp free(resume_commit_id);
10851 0ebf8283 2019-07-24 stsp if (commit)
10852 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10853 0ebf8283 2019-07-24 stsp if (branch)
10854 0ebf8283 2019-07-24 stsp got_ref_close(branch);
10855 0ebf8283 2019-07-24 stsp if (tmp_branch)
10856 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
10857 0ebf8283 2019-07-24 stsp if (worktree)
10858 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
10859 1d0f4054 2021-06-17 stsp if (repo) {
10860 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10861 1d0f4054 2021-06-17 stsp if (error == NULL)
10862 1d0f4054 2021-06-17 stsp error = close_err;
10863 1d0f4054 2021-06-17 stsp }
10864 2822a352 2019-10-15 stsp return error;
10865 2822a352 2019-10-15 stsp }
10866 2822a352 2019-10-15 stsp
10867 2822a352 2019-10-15 stsp __dead static void
10868 2822a352 2019-10-15 stsp usage_integrate(void)
10869 2822a352 2019-10-15 stsp {
10870 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10871 2822a352 2019-10-15 stsp exit(1);
10872 2822a352 2019-10-15 stsp }
10873 2822a352 2019-10-15 stsp
10874 2822a352 2019-10-15 stsp static const struct got_error *
10875 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
10876 2822a352 2019-10-15 stsp {
10877 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
10878 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
10879 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
10880 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10881 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
10882 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10883 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
10884 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10885 9627c110 2020-04-18 stsp int ch;
10886 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10887 2822a352 2019-10-15 stsp
10888 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10889 2822a352 2019-10-15 stsp switch (ch) {
10890 2822a352 2019-10-15 stsp default:
10891 2822a352 2019-10-15 stsp usage_integrate();
10892 2822a352 2019-10-15 stsp /* NOTREACHED */
10893 2822a352 2019-10-15 stsp }
10894 2822a352 2019-10-15 stsp }
10895 2822a352 2019-10-15 stsp
10896 2822a352 2019-10-15 stsp argc -= optind;
10897 2822a352 2019-10-15 stsp argv += optind;
10898 2822a352 2019-10-15 stsp
10899 2822a352 2019-10-15 stsp if (argc != 1)
10900 2822a352 2019-10-15 stsp usage_integrate();
10901 2822a352 2019-10-15 stsp branch_arg = argv[0];
10902 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
10903 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10904 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
10905 2822a352 2019-10-15 stsp err(1, "pledge");
10906 b08a0ccd 2020-09-24 stsp #endif
10907 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
10908 2822a352 2019-10-15 stsp if (cwd == NULL) {
10909 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
10910 2822a352 2019-10-15 stsp goto done;
10911 2822a352 2019-10-15 stsp }
10912 2822a352 2019-10-15 stsp
10913 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
10914 fa51e947 2020-03-27 stsp if (error) {
10915 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10916 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
10917 fa51e947 2020-03-27 stsp cwd);
10918 2822a352 2019-10-15 stsp goto done;
10919 fa51e947 2020-03-27 stsp }
10920 2822a352 2019-10-15 stsp
10921 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
10922 2822a352 2019-10-15 stsp if (error)
10923 2822a352 2019-10-15 stsp goto done;
10924 2822a352 2019-10-15 stsp
10925 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10926 2822a352 2019-10-15 stsp NULL);
10927 2822a352 2019-10-15 stsp if (error != NULL)
10928 2822a352 2019-10-15 stsp goto done;
10929 2822a352 2019-10-15 stsp
10930 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10931 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
10932 2822a352 2019-10-15 stsp if (error)
10933 2822a352 2019-10-15 stsp goto done;
10934 2822a352 2019-10-15 stsp
10935 f259c4c1 2021-09-24 stsp error = check_merge_in_progress(worktree, repo);
10936 f259c4c1 2021-09-24 stsp if (error)
10937 f259c4c1 2021-09-24 stsp goto done;
10938 f259c4c1 2021-09-24 stsp
10939 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10940 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
10941 2822a352 2019-10-15 stsp goto done;
10942 2822a352 2019-10-15 stsp }
10943 2822a352 2019-10-15 stsp
10944 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10945 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
10946 2822a352 2019-10-15 stsp if (error)
10947 2822a352 2019-10-15 stsp goto done;
10948 2822a352 2019-10-15 stsp
10949 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
10950 2822a352 2019-10-15 stsp if (refname == NULL) {
10951 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10952 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10953 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10954 2822a352 2019-10-15 stsp goto done;
10955 2822a352 2019-10-15 stsp }
10956 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
10957 2822a352 2019-10-15 stsp if (base_refname == NULL) {
10958 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10959 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10960 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10961 2822a352 2019-10-15 stsp goto done;
10962 2822a352 2019-10-15 stsp }
10963 2822a352 2019-10-15 stsp
10964 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
10965 2822a352 2019-10-15 stsp if (error)
10966 2822a352 2019-10-15 stsp goto done;
10967 2822a352 2019-10-15 stsp
10968 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10969 2822a352 2019-10-15 stsp if (error)
10970 2822a352 2019-10-15 stsp goto done;
10971 2822a352 2019-10-15 stsp
10972 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10973 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
10974 2822a352 2019-10-15 stsp "specified branch has already been integrated");
10975 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10976 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10977 2822a352 2019-10-15 stsp goto done;
10978 2822a352 2019-10-15 stsp }
10979 2822a352 2019-10-15 stsp
10980 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10981 2822a352 2019-10-15 stsp if (error) {
10982 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
10983 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
10984 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10985 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10986 2822a352 2019-10-15 stsp goto done;
10987 2822a352 2019-10-15 stsp }
10988 2822a352 2019-10-15 stsp
10989 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10990 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
10991 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
10992 2822a352 2019-10-15 stsp check_cancelled, NULL);
10993 2822a352 2019-10-15 stsp if (error)
10994 2822a352 2019-10-15 stsp goto done;
10995 2822a352 2019-10-15 stsp
10996 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
10997 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10998 2822a352 2019-10-15 stsp done:
10999 1d0f4054 2021-06-17 stsp if (repo) {
11000 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11001 1d0f4054 2021-06-17 stsp if (error == NULL)
11002 1d0f4054 2021-06-17 stsp error = close_err;
11003 1d0f4054 2021-06-17 stsp }
11004 2822a352 2019-10-15 stsp if (worktree)
11005 2822a352 2019-10-15 stsp got_worktree_close(worktree);
11006 2822a352 2019-10-15 stsp free(cwd);
11007 2822a352 2019-10-15 stsp free(base_commit_id);
11008 2822a352 2019-10-15 stsp free(commit_id);
11009 2822a352 2019-10-15 stsp free(refname);
11010 2822a352 2019-10-15 stsp free(base_refname);
11011 f259c4c1 2021-09-24 stsp return error;
11012 f259c4c1 2021-09-24 stsp }
11013 f259c4c1 2021-09-24 stsp
11014 f259c4c1 2021-09-24 stsp __dead static void
11015 f259c4c1 2021-09-24 stsp usage_merge(void)
11016 f259c4c1 2021-09-24 stsp {
11017 088449d3 2021-09-26 stsp fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11018 f259c4c1 2021-09-24 stsp getprogname());
11019 f259c4c1 2021-09-24 stsp exit(1);
11020 f259c4c1 2021-09-24 stsp }
11021 f259c4c1 2021-09-24 stsp
11022 f259c4c1 2021-09-24 stsp static const struct got_error *
11023 f259c4c1 2021-09-24 stsp cmd_merge(int argc, char *argv[])
11024 f259c4c1 2021-09-24 stsp {
11025 f259c4c1 2021-09-24 stsp const struct got_error *error = NULL;
11026 f259c4c1 2021-09-24 stsp struct got_worktree *worktree = NULL;
11027 f259c4c1 2021-09-24 stsp struct got_repository *repo = NULL;
11028 f259c4c1 2021-09-24 stsp struct got_fileindex *fileindex = NULL;
11029 f259c4c1 2021-09-24 stsp char *cwd = NULL, *id_str = NULL, *author = NULL;
11030 f259c4c1 2021-09-24 stsp struct got_reference *branch = NULL, *wt_branch = NULL;
11031 f259c4c1 2021-09-24 stsp struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11032 f259c4c1 2021-09-24 stsp struct got_object_id *wt_branch_tip = NULL;
11033 f259c4c1 2021-09-24 stsp int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11034 088449d3 2021-09-26 stsp int interrupt_merge = 0;
11035 f259c4c1 2021-09-24 stsp struct got_update_progress_arg upa;
11036 f259c4c1 2021-09-24 stsp struct got_object_id *merge_commit_id = NULL;
11037 f259c4c1 2021-09-24 stsp char *branch_name = NULL;
11038 f259c4c1 2021-09-24 stsp
11039 f259c4c1 2021-09-24 stsp memset(&upa, 0, sizeof(upa));
11040 f259c4c1 2021-09-24 stsp
11041 088449d3 2021-09-26 stsp while ((ch = getopt(argc, argv, "acn")) != -1) {
11042 f259c4c1 2021-09-24 stsp switch (ch) {
11043 f259c4c1 2021-09-24 stsp case 'a':
11044 f259c4c1 2021-09-24 stsp abort_merge = 1;
11045 f259c4c1 2021-09-24 stsp break;
11046 f259c4c1 2021-09-24 stsp case 'c':
11047 f259c4c1 2021-09-24 stsp continue_merge = 1;
11048 f259c4c1 2021-09-24 stsp break;
11049 088449d3 2021-09-26 stsp case 'n':
11050 088449d3 2021-09-26 stsp interrupt_merge = 1;
11051 088449d3 2021-09-26 stsp break;
11052 f259c4c1 2021-09-24 stsp default:
11053 f259c4c1 2021-09-24 stsp usage_rebase();
11054 f259c4c1 2021-09-24 stsp /* NOTREACHED */
11055 f259c4c1 2021-09-24 stsp }
11056 f259c4c1 2021-09-24 stsp }
11057 f259c4c1 2021-09-24 stsp
11058 f259c4c1 2021-09-24 stsp argc -= optind;
11059 f259c4c1 2021-09-24 stsp argv += optind;
11060 f259c4c1 2021-09-24 stsp
11061 f259c4c1 2021-09-24 stsp #ifndef PROFILE
11062 f259c4c1 2021-09-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11063 f259c4c1 2021-09-24 stsp "unveil", NULL) == -1)
11064 f259c4c1 2021-09-24 stsp err(1, "pledge");
11065 f259c4c1 2021-09-24 stsp #endif
11066 f259c4c1 2021-09-24 stsp
11067 f259c4c1 2021-09-24 stsp if (abort_merge && continue_merge)
11068 f259c4c1 2021-09-24 stsp option_conflict('a', 'c');
11069 f259c4c1 2021-09-24 stsp if (abort_merge || continue_merge) {
11070 f259c4c1 2021-09-24 stsp if (argc != 0)
11071 f259c4c1 2021-09-24 stsp usage_merge();
11072 f259c4c1 2021-09-24 stsp } else if (argc != 1)
11073 f259c4c1 2021-09-24 stsp usage_merge();
11074 f259c4c1 2021-09-24 stsp
11075 f259c4c1 2021-09-24 stsp cwd = getcwd(NULL, 0);
11076 f259c4c1 2021-09-24 stsp if (cwd == NULL) {
11077 f259c4c1 2021-09-24 stsp error = got_error_from_errno("getcwd");
11078 f259c4c1 2021-09-24 stsp goto done;
11079 f259c4c1 2021-09-24 stsp }
11080 f259c4c1 2021-09-24 stsp
11081 f259c4c1 2021-09-24 stsp error = got_worktree_open(&worktree, cwd);
11082 f259c4c1 2021-09-24 stsp if (error) {
11083 f259c4c1 2021-09-24 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11084 f259c4c1 2021-09-24 stsp error = wrap_not_worktree_error(error,
11085 f259c4c1 2021-09-24 stsp "merge", cwd);
11086 f259c4c1 2021-09-24 stsp goto done;
11087 f259c4c1 2021-09-24 stsp }
11088 f259c4c1 2021-09-24 stsp
11089 f259c4c1 2021-09-24 stsp error = got_repo_open(&repo,
11090 f259c4c1 2021-09-24 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
11091 f259c4c1 2021-09-24 stsp if (error != NULL)
11092 f259c4c1 2021-09-24 stsp goto done;
11093 f259c4c1 2021-09-24 stsp
11094 f259c4c1 2021-09-24 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11095 f259c4c1 2021-09-24 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
11096 f259c4c1 2021-09-24 stsp if (error)
11097 f259c4c1 2021-09-24 stsp goto done;
11098 f259c4c1 2021-09-24 stsp
11099 f259c4c1 2021-09-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
11100 f259c4c1 2021-09-24 stsp if (error)
11101 f259c4c1 2021-09-24 stsp goto done;
11102 f259c4c1 2021-09-24 stsp
11103 f259c4c1 2021-09-24 stsp error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11104 f259c4c1 2021-09-24 stsp repo);
11105 f259c4c1 2021-09-24 stsp if (error)
11106 f259c4c1 2021-09-24 stsp goto done;
11107 f259c4c1 2021-09-24 stsp
11108 f259c4c1 2021-09-24 stsp if (abort_merge) {
11109 f259c4c1 2021-09-24 stsp if (!merge_in_progress) {
11110 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_NOT_MERGING);
11111 f259c4c1 2021-09-24 stsp goto done;
11112 f259c4c1 2021-09-24 stsp }
11113 f259c4c1 2021-09-24 stsp error = got_worktree_merge_continue(&branch_name,
11114 f259c4c1 2021-09-24 stsp &branch_tip, &fileindex, worktree, repo);
11115 f259c4c1 2021-09-24 stsp if (error)
11116 f259c4c1 2021-09-24 stsp goto done;
11117 f259c4c1 2021-09-24 stsp error = got_worktree_merge_abort(worktree, fileindex, repo,
11118 41f061b2 2021-10-05 stsp abort_progress, &upa);
11119 f259c4c1 2021-09-24 stsp if (error)
11120 f259c4c1 2021-09-24 stsp goto done;
11121 f259c4c1 2021-09-24 stsp printf("Merge of %s aborted\n", branch_name);
11122 f259c4c1 2021-09-24 stsp goto done; /* nothing else to do */
11123 f259c4c1 2021-09-24 stsp }
11124 f259c4c1 2021-09-24 stsp
11125 f259c4c1 2021-09-24 stsp error = get_author(&author, repo, worktree);
11126 f259c4c1 2021-09-24 stsp if (error)
11127 f259c4c1 2021-09-24 stsp goto done;
11128 f259c4c1 2021-09-24 stsp
11129 f259c4c1 2021-09-24 stsp if (continue_merge) {
11130 f259c4c1 2021-09-24 stsp if (!merge_in_progress) {
11131 f259c4c1 2021-09-24 stsp error = got_error(GOT_ERR_NOT_MERGING);
11132 f259c4c1 2021-09-24 stsp goto done;
11133 f259c4c1 2021-09-24 stsp }
11134 f259c4c1 2021-09-24 stsp error = got_worktree_merge_continue(&branch_name,
11135 f259c4c1 2021-09-24 stsp &branch_tip, &fileindex, worktree, repo);
11136 f259c4c1 2021-09-24 stsp if (error)
11137 f259c4c1 2021-09-24 stsp goto done;
11138 f259c4c1 2021-09-24 stsp } else {
11139 f259c4c1 2021-09-24 stsp error = got_ref_open(&branch, repo, argv[0], 0);
11140 f259c4c1 2021-09-24 stsp if (error != NULL)
11141 f259c4c1 2021-09-24 stsp goto done;
11142 f259c4c1 2021-09-24 stsp branch_name = strdup(got_ref_get_name(branch));
11143 f259c4c1 2021-09-24 stsp if (branch_name == NULL) {
11144 f259c4c1 2021-09-24 stsp error = got_error_from_errno("strdup");
11145 f259c4c1 2021-09-24 stsp goto done;
11146 f259c4c1 2021-09-24 stsp }
11147 f259c4c1 2021-09-24 stsp error = got_ref_resolve(&branch_tip, repo, branch);
11148 f259c4c1 2021-09-24 stsp if (error)
11149 f259c4c1 2021-09-24 stsp goto done;
11150 f259c4c1 2021-09-24 stsp }
11151 f259c4c1 2021-09-24 stsp
11152 f259c4c1 2021-09-24 stsp error = got_ref_open(&wt_branch, repo,
11153 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(worktree), 0);
11154 f259c4c1 2021-09-24 stsp if (error)
11155 f259c4c1 2021-09-24 stsp goto done;
11156 f259c4c1 2021-09-24 stsp error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11157 f259c4c1 2021-09-24 stsp if (error)
11158 f259c4c1 2021-09-24 stsp goto done;
11159 f259c4c1 2021-09-24 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11160 4e91ef15 2021-09-26 stsp wt_branch_tip, branch_tip, 0, repo,
11161 f259c4c1 2021-09-24 stsp check_cancelled, NULL);
11162 4e91ef15 2021-09-26 stsp if (error && error->code != GOT_ERR_ANCESTRY)
11163 f259c4c1 2021-09-24 stsp goto done;
11164 f259c4c1 2021-09-24 stsp
11165 f259c4c1 2021-09-24 stsp if (!continue_merge) {
11166 f259c4c1 2021-09-24 stsp error = check_path_prefix(wt_branch_tip, branch_tip,
11167 f259c4c1 2021-09-24 stsp got_worktree_get_path_prefix(worktree),
11168 f259c4c1 2021-09-24 stsp GOT_ERR_MERGE_PATH, repo);
11169 f259c4c1 2021-09-24 stsp if (error)
11170 f259c4c1 2021-09-24 stsp goto done;
11171 4e91ef15 2021-09-26 stsp if (yca_id) {
11172 4e91ef15 2021-09-26 stsp error = check_same_branch(wt_branch_tip, branch,
11173 4e91ef15 2021-09-26 stsp yca_id, repo);
11174 4e91ef15 2021-09-26 stsp if (error) {
11175 4e91ef15 2021-09-26 stsp if (error->code != GOT_ERR_ANCESTRY)
11176 4e91ef15 2021-09-26 stsp goto done;
11177 4e91ef15 2021-09-26 stsp error = NULL;
11178 4e91ef15 2021-09-26 stsp } else {
11179 4e91ef15 2021-09-26 stsp static char msg[512];
11180 4e91ef15 2021-09-26 stsp snprintf(msg, sizeof(msg),
11181 4e91ef15 2021-09-26 stsp "cannot create a merge commit because "
11182 4e91ef15 2021-09-26 stsp "%s is based on %s; %s can be integrated "
11183 4e91ef15 2021-09-26 stsp "with 'got integrate' instead", branch_name,
11184 4e91ef15 2021-09-26 stsp got_worktree_get_head_ref_name(worktree),
11185 4e91ef15 2021-09-26 stsp branch_name);
11186 4e91ef15 2021-09-26 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11187 f259c4c1 2021-09-24 stsp goto done;
11188 4e91ef15 2021-09-26 stsp }
11189 f259c4c1 2021-09-24 stsp }
11190 f259c4c1 2021-09-24 stsp error = got_worktree_merge_prepare(&fileindex, worktree,
11191 f259c4c1 2021-09-24 stsp branch, repo);
11192 f259c4c1 2021-09-24 stsp if (error)
11193 f259c4c1 2021-09-24 stsp goto done;
11194 f259c4c1 2021-09-24 stsp
11195 f259c4c1 2021-09-24 stsp error = got_worktree_merge_branch(worktree, fileindex,
11196 f259c4c1 2021-09-24 stsp yca_id, branch_tip, repo, update_progress, &upa,
11197 f259c4c1 2021-09-24 stsp check_cancelled, NULL);
11198 f259c4c1 2021-09-24 stsp if (error)
11199 f259c4c1 2021-09-24 stsp goto done;
11200 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
11201 4e91ef15 2021-09-26 stsp if (!upa.did_something) {
11202 4e91ef15 2021-09-26 stsp error = got_worktree_merge_abort(worktree, fileindex,
11203 41f061b2 2021-10-05 stsp repo, abort_progress, &upa);
11204 4e91ef15 2021-09-26 stsp if (error)
11205 4e91ef15 2021-09-26 stsp goto done;
11206 4e91ef15 2021-09-26 stsp printf("Already up-to-date\n");
11207 4e91ef15 2021-09-26 stsp goto done;
11208 4e91ef15 2021-09-26 stsp }
11209 f259c4c1 2021-09-24 stsp }
11210 f259c4c1 2021-09-24 stsp
11211 088449d3 2021-09-26 stsp if (interrupt_merge) {
11212 f259c4c1 2021-09-24 stsp error = got_worktree_merge_postpone(worktree, fileindex);
11213 f259c4c1 2021-09-24 stsp if (error)
11214 f259c4c1 2021-09-24 stsp goto done;
11215 088449d3 2021-09-26 stsp printf("Merge of %s interrupted on request\n", branch_name);
11216 c1b05723 2021-09-28 stsp } else if (upa.conflicts > 0 || upa.missing > 0 ||
11217 c1b05723 2021-09-28 stsp upa.not_deleted > 0 || upa.unversioned > 0) {
11218 088449d3 2021-09-26 stsp error = got_worktree_merge_postpone(worktree, fileindex);
11219 088449d3 2021-09-26 stsp if (error)
11220 088449d3 2021-09-26 stsp goto done;
11221 c1b05723 2021-09-28 stsp if (upa.conflicts > 0 && upa.missing == 0 &&
11222 c1b05723 2021-09-28 stsp upa.not_deleted == 0 && upa.unversioned == 0) {
11223 f259c4c1 2021-09-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
11224 f259c4c1 2021-09-24 stsp "conflicts must be resolved before merging "
11225 f259c4c1 2021-09-24 stsp "can continue");
11226 f259c4c1 2021-09-24 stsp } else if (upa.conflicts > 0) {
11227 f259c4c1 2021-09-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
11228 f259c4c1 2021-09-24 stsp "conflicts must be resolved before merging "
11229 c1b05723 2021-09-28 stsp "can continue; changes destined for some "
11230 1acd48bc 2021-09-24 stsp "files were not yet merged and "
11231 f259c4c1 2021-09-24 stsp "should be merged manually if required before the "
11232 f259c4c1 2021-09-24 stsp "merge operation is continued");
11233 f259c4c1 2021-09-24 stsp } else {
11234 f259c4c1 2021-09-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
11235 c1b05723 2021-09-28 stsp "changes destined for some "
11236 f259c4c1 2021-09-24 stsp "files were not yet merged and should be "
11237 f259c4c1 2021-09-24 stsp "merged manually if required before the "
11238 f259c4c1 2021-09-24 stsp "merge operation is continued");
11239 f259c4c1 2021-09-24 stsp }
11240 f259c4c1 2021-09-24 stsp goto done;
11241 f259c4c1 2021-09-24 stsp } else {
11242 f259c4c1 2021-09-24 stsp error = got_worktree_merge_commit(&merge_commit_id, worktree,
11243 0ff8d236 2021-09-28 stsp fileindex, author, NULL, 1, branch_tip, branch_name,
11244 0ff8d236 2021-09-28 stsp repo, continue_merge ? print_status : NULL, NULL);
11245 f259c4c1 2021-09-24 stsp if (error)
11246 f259c4c1 2021-09-24 stsp goto done;
11247 f259c4c1 2021-09-24 stsp error = got_worktree_merge_complete(worktree, fileindex, repo);
11248 f259c4c1 2021-09-24 stsp if (error)
11249 f259c4c1 2021-09-24 stsp goto done;
11250 f259c4c1 2021-09-24 stsp error = got_object_id_str(&id_str, merge_commit_id);
11251 f259c4c1 2021-09-24 stsp if (error)
11252 f259c4c1 2021-09-24 stsp goto done;
11253 f259c4c1 2021-09-24 stsp printf("Merged %s into %s: %s\n", branch_name,
11254 f259c4c1 2021-09-24 stsp got_worktree_get_head_ref_name(worktree),
11255 f259c4c1 2021-09-24 stsp id_str);
11256 f259c4c1 2021-09-24 stsp
11257 f259c4c1 2021-09-24 stsp }
11258 f259c4c1 2021-09-24 stsp done:
11259 f259c4c1 2021-09-24 stsp free(id_str);
11260 f259c4c1 2021-09-24 stsp free(merge_commit_id);
11261 f259c4c1 2021-09-24 stsp free(author);
11262 f259c4c1 2021-09-24 stsp free(branch_tip);
11263 f259c4c1 2021-09-24 stsp free(branch_name);
11264 f259c4c1 2021-09-24 stsp free(yca_id);
11265 f259c4c1 2021-09-24 stsp if (branch)
11266 f259c4c1 2021-09-24 stsp got_ref_close(branch);
11267 f259c4c1 2021-09-24 stsp if (wt_branch)
11268 f259c4c1 2021-09-24 stsp got_ref_close(wt_branch);
11269 f259c4c1 2021-09-24 stsp if (worktree)
11270 f259c4c1 2021-09-24 stsp got_worktree_close(worktree);
11271 f259c4c1 2021-09-24 stsp if (repo) {
11272 f259c4c1 2021-09-24 stsp const struct got_error *close_err = got_repo_close(repo);
11273 f259c4c1 2021-09-24 stsp if (error == NULL)
11274 f259c4c1 2021-09-24 stsp error = close_err;
11275 f259c4c1 2021-09-24 stsp }
11276 715dc77e 2019-08-03 stsp return error;
11277 715dc77e 2019-08-03 stsp }
11278 715dc77e 2019-08-03 stsp
11279 715dc77e 2019-08-03 stsp __dead static void
11280 715dc77e 2019-08-03 stsp usage_stage(void)
11281 715dc77e 2019-08-03 stsp {
11282 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
11283 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
11284 715dc77e 2019-08-03 stsp getprogname());
11285 715dc77e 2019-08-03 stsp exit(1);
11286 715dc77e 2019-08-03 stsp }
11287 715dc77e 2019-08-03 stsp
11288 715dc77e 2019-08-03 stsp static const struct got_error *
11289 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
11290 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
11291 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
11292 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
11293 408b4ebc 2019-08-03 stsp {
11294 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
11295 408b4ebc 2019-08-03 stsp char *id_str = NULL;
11296 408b4ebc 2019-08-03 stsp
11297 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
11298 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
11299 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
11300 408b4ebc 2019-08-03 stsp return NULL;
11301 408b4ebc 2019-08-03 stsp
11302 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
11303 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
11304 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
11305 408b4ebc 2019-08-03 stsp else
11306 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
11307 408b4ebc 2019-08-03 stsp if (err)
11308 408b4ebc 2019-08-03 stsp return err;
11309 408b4ebc 2019-08-03 stsp
11310 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
11311 408b4ebc 2019-08-03 stsp free(id_str);
11312 dc424a06 2019-08-07 stsp return NULL;
11313 dc424a06 2019-08-07 stsp }
11314 2e1f37b0 2019-08-08 stsp
11315 dc424a06 2019-08-07 stsp static const struct got_error *
11316 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
11317 715dc77e 2019-08-03 stsp {
11318 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
11319 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
11320 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
11321 715dc77e 2019-08-03 stsp char *cwd = NULL;
11322 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
11323 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
11324 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
11325 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
11326 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
11327 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
11328 715dc77e 2019-08-03 stsp
11329 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
11330 715dc77e 2019-08-03 stsp
11331 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
11332 715dc77e 2019-08-03 stsp switch (ch) {
11333 408b4ebc 2019-08-03 stsp case 'l':
11334 408b4ebc 2019-08-03 stsp list_stage = 1;
11335 408b4ebc 2019-08-03 stsp break;
11336 dc424a06 2019-08-07 stsp case 'p':
11337 dc424a06 2019-08-07 stsp pflag = 1;
11338 dc424a06 2019-08-07 stsp break;
11339 dc424a06 2019-08-07 stsp case 'F':
11340 dc424a06 2019-08-07 stsp patch_script_path = optarg;
11341 dc424a06 2019-08-07 stsp break;
11342 35213c7c 2020-07-23 stsp case 'S':
11343 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
11344 35213c7c 2020-07-23 stsp break;
11345 715dc77e 2019-08-03 stsp default:
11346 715dc77e 2019-08-03 stsp usage_stage();
11347 715dc77e 2019-08-03 stsp /* NOTREACHED */
11348 715dc77e 2019-08-03 stsp }
11349 715dc77e 2019-08-03 stsp }
11350 715dc77e 2019-08-03 stsp
11351 715dc77e 2019-08-03 stsp argc -= optind;
11352 715dc77e 2019-08-03 stsp argv += optind;
11353 715dc77e 2019-08-03 stsp
11354 715dc77e 2019-08-03 stsp #ifndef PROFILE
11355 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11356 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
11357 715dc77e 2019-08-03 stsp err(1, "pledge");
11358 715dc77e 2019-08-03 stsp #endif
11359 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
11360 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
11361 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
11362 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
11363 715dc77e 2019-08-03 stsp
11364 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
11365 715dc77e 2019-08-03 stsp if (cwd == NULL) {
11366 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
11367 715dc77e 2019-08-03 stsp goto done;
11368 715dc77e 2019-08-03 stsp }
11369 715dc77e 2019-08-03 stsp
11370 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
11371 fa51e947 2020-03-27 stsp if (error) {
11372 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11373 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
11374 715dc77e 2019-08-03 stsp goto done;
11375 fa51e947 2020-03-27 stsp }
11376 715dc77e 2019-08-03 stsp
11377 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11378 c9956ddf 2019-09-08 stsp NULL);
11379 715dc77e 2019-08-03 stsp if (error != NULL)
11380 715dc77e 2019-08-03 stsp goto done;
11381 715dc77e 2019-08-03 stsp
11382 dc424a06 2019-08-07 stsp if (patch_script_path) {
11383 00fe21f2 2021-12-31 stsp patch_script_file = fopen(patch_script_path, "re");
11384 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
11385 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
11386 dc424a06 2019-08-07 stsp patch_script_path);
11387 dc424a06 2019-08-07 stsp goto done;
11388 dc424a06 2019-08-07 stsp }
11389 dc424a06 2019-08-07 stsp }
11390 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11391 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
11392 715dc77e 2019-08-03 stsp if (error)
11393 715dc77e 2019-08-03 stsp goto done;
11394 715dc77e 2019-08-03 stsp
11395 f259c4c1 2021-09-24 stsp error = check_merge_in_progress(worktree, repo);
11396 f259c4c1 2021-09-24 stsp if (error)
11397 f259c4c1 2021-09-24 stsp goto done;
11398 f259c4c1 2021-09-24 stsp
11399 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11400 6d022e97 2019-08-04 stsp if (error)
11401 6d022e97 2019-08-04 stsp goto done;
11402 715dc77e 2019-08-03 stsp
11403 6d022e97 2019-08-04 stsp if (list_stage)
11404 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
11405 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
11406 2e1f37b0 2019-08-08 stsp else {
11407 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
11408 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
11409 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
11410 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
11411 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
11412 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
11413 2e1f37b0 2019-08-08 stsp }
11414 ad493afc 2019-08-03 stsp done:
11415 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
11416 2e1f37b0 2019-08-08 stsp error == NULL)
11417 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
11418 1d0f4054 2021-06-17 stsp if (repo) {
11419 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11420 1d0f4054 2021-06-17 stsp if (error == NULL)
11421 1d0f4054 2021-06-17 stsp error = close_err;
11422 1d0f4054 2021-06-17 stsp }
11423 ad493afc 2019-08-03 stsp if (worktree)
11424 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
11425 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
11426 ad493afc 2019-08-03 stsp free((char *)pe->path);
11427 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
11428 ad493afc 2019-08-03 stsp free(cwd);
11429 ad493afc 2019-08-03 stsp return error;
11430 ad493afc 2019-08-03 stsp }
11431 ad493afc 2019-08-03 stsp
11432 ad493afc 2019-08-03 stsp __dead static void
11433 ad493afc 2019-08-03 stsp usage_unstage(void)
11434 ad493afc 2019-08-03 stsp {
11435 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
11436 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
11437 ad493afc 2019-08-03 stsp getprogname());
11438 ad493afc 2019-08-03 stsp exit(1);
11439 ad493afc 2019-08-03 stsp }
11440 ad493afc 2019-08-03 stsp
11441 ad493afc 2019-08-03 stsp
11442 ad493afc 2019-08-03 stsp static const struct got_error *
11443 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
11444 ad493afc 2019-08-03 stsp {
11445 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
11446 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
11447 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
11448 ad493afc 2019-08-03 stsp char *cwd = NULL;
11449 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
11450 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
11451 9627c110 2020-04-18 stsp int ch, pflag = 0;
11452 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
11453 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
11454 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
11455 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
11456 ad493afc 2019-08-03 stsp
11457 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
11458 ad493afc 2019-08-03 stsp
11459 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
11460 ad493afc 2019-08-03 stsp switch (ch) {
11461 2e1f37b0 2019-08-08 stsp case 'p':
11462 2e1f37b0 2019-08-08 stsp pflag = 1;
11463 2e1f37b0 2019-08-08 stsp break;
11464 2e1f37b0 2019-08-08 stsp case 'F':
11465 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
11466 2e1f37b0 2019-08-08 stsp break;
11467 ad493afc 2019-08-03 stsp default:
11468 ad493afc 2019-08-03 stsp usage_unstage();
11469 ad493afc 2019-08-03 stsp /* NOTREACHED */
11470 ad493afc 2019-08-03 stsp }
11471 ad493afc 2019-08-03 stsp }
11472 ad493afc 2019-08-03 stsp
11473 ad493afc 2019-08-03 stsp argc -= optind;
11474 ad493afc 2019-08-03 stsp argv += optind;
11475 ad493afc 2019-08-03 stsp
11476 ad493afc 2019-08-03 stsp #ifndef PROFILE
11477 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11478 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
11479 ad493afc 2019-08-03 stsp err(1, "pledge");
11480 ad493afc 2019-08-03 stsp #endif
11481 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
11482 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
11483 2e1f37b0 2019-08-08 stsp
11484 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
11485 ad493afc 2019-08-03 stsp if (cwd == NULL) {
11486 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
11487 ad493afc 2019-08-03 stsp goto done;
11488 ad493afc 2019-08-03 stsp }
11489 ad493afc 2019-08-03 stsp
11490 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
11491 fa51e947 2020-03-27 stsp if (error) {
11492 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11493 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
11494 ad493afc 2019-08-03 stsp goto done;
11495 fa51e947 2020-03-27 stsp }
11496 ad493afc 2019-08-03 stsp
11497 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11498 c9956ddf 2019-09-08 stsp NULL);
11499 ad493afc 2019-08-03 stsp if (error != NULL)
11500 ad493afc 2019-08-03 stsp goto done;
11501 ad493afc 2019-08-03 stsp
11502 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
11503 00fe21f2 2021-12-31 stsp patch_script_file = fopen(patch_script_path, "re");
11504 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
11505 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
11506 2e1f37b0 2019-08-08 stsp patch_script_path);
11507 2e1f37b0 2019-08-08 stsp goto done;
11508 2e1f37b0 2019-08-08 stsp }
11509 2e1f37b0 2019-08-08 stsp }
11510 2e1f37b0 2019-08-08 stsp
11511 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
11512 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
11513 ad493afc 2019-08-03 stsp if (error)
11514 ad493afc 2019-08-03 stsp goto done;
11515 ad493afc 2019-08-03 stsp
11516 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
11517 ad493afc 2019-08-03 stsp if (error)
11518 ad493afc 2019-08-03 stsp goto done;
11519 ad493afc 2019-08-03 stsp
11520 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
11521 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
11522 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
11523 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
11524 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
11525 9627c110 2020-04-18 stsp if (!error)
11526 35ca1db7 2021-09-28 stsp print_merge_progress_stats(&upa);
11527 715dc77e 2019-08-03 stsp done:
11528 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
11529 2e1f37b0 2019-08-08 stsp error == NULL)
11530 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
11531 1d0f4054 2021-06-17 stsp if (repo) {
11532 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11533 1d0f4054 2021-06-17 stsp if (error == NULL)
11534 1d0f4054 2021-06-17 stsp error = close_err;
11535 1d0f4054 2021-06-17 stsp }
11536 715dc77e 2019-08-03 stsp if (worktree)
11537 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
11538 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
11539 715dc77e 2019-08-03 stsp free((char *)pe->path);
11540 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
11541 715dc77e 2019-08-03 stsp free(cwd);
11542 01073a5d 2019-08-22 stsp return error;
11543 01073a5d 2019-08-22 stsp }
11544 01073a5d 2019-08-22 stsp
11545 01073a5d 2019-08-22 stsp __dead static void
11546 01073a5d 2019-08-22 stsp usage_cat(void)
11547 01073a5d 2019-08-22 stsp {
11548 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
11549 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
11550 01073a5d 2019-08-22 stsp exit(1);
11551 01073a5d 2019-08-22 stsp }
11552 01073a5d 2019-08-22 stsp
11553 01073a5d 2019-08-22 stsp static const struct got_error *
11554 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11555 01073a5d 2019-08-22 stsp {
11556 01073a5d 2019-08-22 stsp const struct got_error *err;
11557 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
11558 01073a5d 2019-08-22 stsp
11559 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
11560 01073a5d 2019-08-22 stsp if (err)
11561 01073a5d 2019-08-22 stsp return err;
11562 01073a5d 2019-08-22 stsp
11563 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
11564 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
11565 01073a5d 2019-08-22 stsp return err;
11566 01073a5d 2019-08-22 stsp }
11567 01073a5d 2019-08-22 stsp
11568 01073a5d 2019-08-22 stsp static const struct got_error *
11569 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11570 01073a5d 2019-08-22 stsp {
11571 01073a5d 2019-08-22 stsp const struct got_error *err;
11572 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
11573 56e0773d 2019-11-28 stsp int nentries, i;
11574 01073a5d 2019-08-22 stsp
11575 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
11576 01073a5d 2019-08-22 stsp if (err)
11577 01073a5d 2019-08-22 stsp return err;
11578 01073a5d 2019-08-22 stsp
11579 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
11580 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
11581 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
11582 01073a5d 2019-08-22 stsp char *id_str;
11583 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
11584 01073a5d 2019-08-22 stsp break;
11585 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
11586 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
11587 01073a5d 2019-08-22 stsp if (err)
11588 01073a5d 2019-08-22 stsp break;
11589 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
11590 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
11591 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
11592 01073a5d 2019-08-22 stsp free(id_str);
11593 01073a5d 2019-08-22 stsp }
11594 01073a5d 2019-08-22 stsp
11595 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
11596 01073a5d 2019-08-22 stsp return err;
11597 01073a5d 2019-08-22 stsp }
11598 01073a5d 2019-08-22 stsp
11599 bdc78ba6 2022-02-16 jrick static void
11600 bdc78ba6 2022-02-16 jrick format_gmtoff(char *buf, size_t sz, time_t gmtoff)
11601 bdc78ba6 2022-02-16 jrick {
11602 bdc78ba6 2022-02-16 jrick long long h, m;
11603 bdc78ba6 2022-02-16 jrick char sign = '+';
11604 e546352c 2022-02-16 jrick
11605 bdc78ba6 2022-02-16 jrick if (gmtoff < 0) {
11606 bdc78ba6 2022-02-16 jrick sign = '-';
11607 bdc78ba6 2022-02-16 jrick gmtoff = -gmtoff;
11608 bdc78ba6 2022-02-16 jrick }
11609 bdc78ba6 2022-02-16 jrick
11610 bdc78ba6 2022-02-16 jrick h = (long long)gmtoff / 3600;
11611 bdc78ba6 2022-02-16 jrick m = ((long long)gmtoff - h*3600) / 60;
11612 bdc78ba6 2022-02-16 jrick snprintf(buf, sz, "%c%02lld%02lld", sign, h, m);
11613 bdc78ba6 2022-02-16 jrick }
11614 bdc78ba6 2022-02-16 jrick
11615 01073a5d 2019-08-22 stsp static const struct got_error *
11616 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11617 01073a5d 2019-08-22 stsp {
11618 01073a5d 2019-08-22 stsp const struct got_error *err;
11619 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
11620 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
11621 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
11622 01073a5d 2019-08-22 stsp char *id_str = NULL;
11623 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
11624 bdc78ba6 2022-02-16 jrick char gmtoff[6];
11625 01073a5d 2019-08-22 stsp
11626 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
11627 01073a5d 2019-08-22 stsp if (err)
11628 01073a5d 2019-08-22 stsp return err;
11629 01073a5d 2019-08-22 stsp
11630 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
11631 01073a5d 2019-08-22 stsp if (err)
11632 01073a5d 2019-08-22 stsp goto done;
11633 01073a5d 2019-08-22 stsp
11634 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
11635 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
11636 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
11637 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
11638 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
11639 01073a5d 2019-08-22 stsp char *pid_str;
11640 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
11641 01073a5d 2019-08-22 stsp if (err)
11642 01073a5d 2019-08-22 stsp goto done;
11643 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
11644 01073a5d 2019-08-22 stsp free(pid_str);
11645 01073a5d 2019-08-22 stsp }
11646 bdc78ba6 2022-02-16 jrick format_gmtoff(gmtoff, sizeof(gmtoff),
11647 bdc78ba6 2022-02-16 jrick got_object_commit_get_author_gmtoff(commit));
11648 bdc78ba6 2022-02-16 jrick fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
11649 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
11650 bdc78ba6 2022-02-16 jrick (long long)got_object_commit_get_author_time(commit),
11651 bdc78ba6 2022-02-16 jrick gmtoff);
11652 01073a5d 2019-08-22 stsp
11653 bdc78ba6 2022-02-16 jrick format_gmtoff(gmtoff, sizeof(gmtoff),
11654 bdc78ba6 2022-02-16 jrick got_object_commit_get_committer_gmtoff(commit));
11655 bdc78ba6 2022-02-16 jrick fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
11656 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
11657 bdc78ba6 2022-02-16 jrick (long long)got_object_commit_get_committer_time(commit),
11658 bdc78ba6 2022-02-16 jrick gmtoff);
11659 01073a5d 2019-08-22 stsp
11660 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
11661 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
11662 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
11663 01073a5d 2019-08-22 stsp done:
11664 01073a5d 2019-08-22 stsp free(id_str);
11665 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
11666 01073a5d 2019-08-22 stsp return err;
11667 01073a5d 2019-08-22 stsp }
11668 01073a5d 2019-08-22 stsp
11669 01073a5d 2019-08-22 stsp static const struct got_error *
11670 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
11671 01073a5d 2019-08-22 stsp {
11672 01073a5d 2019-08-22 stsp const struct got_error *err;
11673 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
11674 01073a5d 2019-08-22 stsp char *id_str = NULL;
11675 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
11676 bdc78ba6 2022-02-16 jrick char gmtoff[6];
11677 01073a5d 2019-08-22 stsp
11678 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
11679 01073a5d 2019-08-22 stsp if (err)
11680 01073a5d 2019-08-22 stsp return err;
11681 01073a5d 2019-08-22 stsp
11682 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
11683 01073a5d 2019-08-22 stsp if (err)
11684 01073a5d 2019-08-22 stsp goto done;
11685 01073a5d 2019-08-22 stsp
11686 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
11687 e15d5241 2019-08-22 stsp
11688 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
11689 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
11690 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11691 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
11692 01073a5d 2019-08-22 stsp break;
11693 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
11694 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11695 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
11696 01073a5d 2019-08-22 stsp break;
11697 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
11698 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11699 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
11700 01073a5d 2019-08-22 stsp break;
11701 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11702 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
11703 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
11704 01073a5d 2019-08-22 stsp break;
11705 01073a5d 2019-08-22 stsp default:
11706 01073a5d 2019-08-22 stsp break;
11707 01073a5d 2019-08-22 stsp }
11708 01073a5d 2019-08-22 stsp
11709 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
11710 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
11711 e15d5241 2019-08-22 stsp
11712 bdc78ba6 2022-02-16 jrick format_gmtoff(gmtoff, sizeof(gmtoff),
11713 bdc78ba6 2022-02-16 jrick got_object_tag_get_tagger_gmtoff(tag));
11714 bdc78ba6 2022-02-16 jrick fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
11715 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
11716 bdc78ba6 2022-02-16 jrick (long long)got_object_tag_get_tagger_time(tag),
11717 bdc78ba6 2022-02-16 jrick gmtoff);
11718 01073a5d 2019-08-22 stsp
11719 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
11720 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
11721 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
11722 01073a5d 2019-08-22 stsp done:
11723 01073a5d 2019-08-22 stsp free(id_str);
11724 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
11725 01073a5d 2019-08-22 stsp return err;
11726 01073a5d 2019-08-22 stsp }
11727 01073a5d 2019-08-22 stsp
11728 01073a5d 2019-08-22 stsp static const struct got_error *
11729 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
11730 01073a5d 2019-08-22 stsp {
11731 01073a5d 2019-08-22 stsp const struct got_error *error;
11732 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
11733 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
11734 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
11735 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
11736 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
11737 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
11738 84de9106 2020-12-26 stsp struct got_reflist_head refs;
11739 84de9106 2020-12-26 stsp
11740 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
11741 01073a5d 2019-08-22 stsp
11742 01073a5d 2019-08-22 stsp #ifndef PROFILE
11743 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
11744 01073a5d 2019-08-22 stsp NULL) == -1)
11745 01073a5d 2019-08-22 stsp err(1, "pledge");
11746 01073a5d 2019-08-22 stsp #endif
11747 01073a5d 2019-08-22 stsp
11748 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
11749 01073a5d 2019-08-22 stsp switch (ch) {
11750 896e9b6f 2019-08-26 stsp case 'c':
11751 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
11752 896e9b6f 2019-08-26 stsp break;
11753 01073a5d 2019-08-22 stsp case 'r':
11754 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
11755 01073a5d 2019-08-22 stsp if (repo_path == NULL)
11756 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
11757 9ba1d308 2019-10-21 stsp optarg);
11758 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
11759 01073a5d 2019-08-22 stsp break;
11760 896e9b6f 2019-08-26 stsp case 'P':
11761 896e9b6f 2019-08-26 stsp force_path = 1;
11762 896e9b6f 2019-08-26 stsp break;
11763 01073a5d 2019-08-22 stsp default:
11764 01073a5d 2019-08-22 stsp usage_cat();
11765 01073a5d 2019-08-22 stsp /* NOTREACHED */
11766 01073a5d 2019-08-22 stsp }
11767 01073a5d 2019-08-22 stsp }
11768 01073a5d 2019-08-22 stsp
11769 01073a5d 2019-08-22 stsp argc -= optind;
11770 01073a5d 2019-08-22 stsp argv += optind;
11771 01073a5d 2019-08-22 stsp
11772 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
11773 01073a5d 2019-08-22 stsp if (cwd == NULL) {
11774 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
11775 01073a5d 2019-08-22 stsp goto done;
11776 01073a5d 2019-08-22 stsp }
11777 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
11778 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
11779 01073a5d 2019-08-22 stsp goto done;
11780 01073a5d 2019-08-22 stsp if (worktree) {
11781 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11782 01073a5d 2019-08-22 stsp repo_path = strdup(
11783 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
11784 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11785 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
11786 01073a5d 2019-08-22 stsp goto done;
11787 01073a5d 2019-08-22 stsp }
11788 01073a5d 2019-08-22 stsp }
11789 01073a5d 2019-08-22 stsp }
11790 01073a5d 2019-08-22 stsp
11791 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
11792 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
11793 01073a5d 2019-08-22 stsp if (repo_path == NULL)
11794 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
11795 01073a5d 2019-08-22 stsp }
11796 01073a5d 2019-08-22 stsp
11797 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
11798 01073a5d 2019-08-22 stsp free(repo_path);
11799 01073a5d 2019-08-22 stsp if (error != NULL)
11800 01073a5d 2019-08-22 stsp goto done;
11801 01073a5d 2019-08-22 stsp
11802 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
11803 896e9b6f 2019-08-26 stsp if (error)
11804 896e9b6f 2019-08-26 stsp goto done;
11805 896e9b6f 2019-08-26 stsp
11806 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11807 84de9106 2020-12-26 stsp if (error)
11808 84de9106 2020-12-26 stsp goto done;
11809 84de9106 2020-12-26 stsp
11810 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
11811 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
11812 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
11813 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
11814 01073a5d 2019-08-22 stsp if (error)
11815 01073a5d 2019-08-22 stsp goto done;
11816 01073a5d 2019-08-22 stsp
11817 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
11818 896e9b6f 2019-08-26 stsp if (force_path) {
11819 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
11820 896e9b6f 2019-08-26 stsp argv[i]);
11821 896e9b6f 2019-08-26 stsp if (error)
11822 896e9b6f 2019-08-26 stsp break;
11823 896e9b6f 2019-08-26 stsp } else {
11824 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
11825 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
11826 84de9106 2020-12-26 stsp repo);
11827 896e9b6f 2019-08-26 stsp if (error) {
11828 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
11829 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
11830 896e9b6f 2019-08-26 stsp break;
11831 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
11832 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
11833 896e9b6f 2019-08-26 stsp if (error)
11834 896e9b6f 2019-08-26 stsp break;
11835 896e9b6f 2019-08-26 stsp }
11836 896e9b6f 2019-08-26 stsp }
11837 01073a5d 2019-08-22 stsp
11838 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
11839 01073a5d 2019-08-22 stsp if (error)
11840 01073a5d 2019-08-22 stsp break;
11841 01073a5d 2019-08-22 stsp
11842 01073a5d 2019-08-22 stsp switch (obj_type) {
11843 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
11844 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
11845 01073a5d 2019-08-22 stsp break;
11846 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
11847 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
11848 01073a5d 2019-08-22 stsp break;
11849 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
11850 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
11851 01073a5d 2019-08-22 stsp break;
11852 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11853 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
11854 01073a5d 2019-08-22 stsp break;
11855 01073a5d 2019-08-22 stsp default:
11856 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
11857 01073a5d 2019-08-22 stsp break;
11858 01073a5d 2019-08-22 stsp }
11859 01073a5d 2019-08-22 stsp if (error)
11860 01073a5d 2019-08-22 stsp break;
11861 01073a5d 2019-08-22 stsp free(label);
11862 01073a5d 2019-08-22 stsp label = NULL;
11863 01073a5d 2019-08-22 stsp free(id);
11864 01073a5d 2019-08-22 stsp id = NULL;
11865 01073a5d 2019-08-22 stsp }
11866 01073a5d 2019-08-22 stsp done:
11867 01073a5d 2019-08-22 stsp free(label);
11868 01073a5d 2019-08-22 stsp free(id);
11869 896e9b6f 2019-08-26 stsp free(commit_id);
11870 01073a5d 2019-08-22 stsp if (worktree)
11871 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
11872 01073a5d 2019-08-22 stsp if (repo) {
11873 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11874 01073a5d 2019-08-22 stsp if (error == NULL)
11875 1d0f4054 2021-06-17 stsp error = close_err;
11876 b2118c49 2020-07-28 stsp }
11877 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
11878 b2118c49 2020-07-28 stsp return error;
11879 b2118c49 2020-07-28 stsp }
11880 b2118c49 2020-07-28 stsp
11881 b2118c49 2020-07-28 stsp __dead static void
11882 b2118c49 2020-07-28 stsp usage_info(void)
11883 b2118c49 2020-07-28 stsp {
11884 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
11885 b2118c49 2020-07-28 stsp getprogname());
11886 b2118c49 2020-07-28 stsp exit(1);
11887 b2118c49 2020-07-28 stsp }
11888 b2118c49 2020-07-28 stsp
11889 b2118c49 2020-07-28 stsp static const struct got_error *
11890 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11891 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11892 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
11893 b2118c49 2020-07-28 stsp {
11894 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
11895 b2118c49 2020-07-28 stsp char *id_str = NULL;
11896 b2118c49 2020-07-28 stsp char datebuf[128];
11897 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
11898 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
11899 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11900 b2118c49 2020-07-28 stsp
11901 b2118c49 2020-07-28 stsp /*
11902 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
11903 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
11904 b2118c49 2020-07-28 stsp */
11905 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
11906 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
11907 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
11908 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
11909 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
11910 b2118c49 2020-07-28 stsp }
11911 b2118c49 2020-07-28 stsp
11912 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
11913 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
11914 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
11915 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
11916 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
11917 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11918 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
11919 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
11920 b2118c49 2020-07-28 stsp else
11921 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
11922 b2118c49 2020-07-28 stsp
11923 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
11924 b2118c49 2020-07-28 stsp if (tm == NULL)
11925 b2118c49 2020-07-28 stsp return NULL;
11926 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11927 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
11928 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
11929 b2118c49 2020-07-28 stsp
11930 b2118c49 2020-07-28 stsp if (blob_id) {
11931 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
11932 b2118c49 2020-07-28 stsp if (err)
11933 b2118c49 2020-07-28 stsp return err;
11934 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
11935 b2118c49 2020-07-28 stsp free(id_str);
11936 b2118c49 2020-07-28 stsp }
11937 b2118c49 2020-07-28 stsp
11938 b2118c49 2020-07-28 stsp if (staged_blob_id) {
11939 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
11940 b2118c49 2020-07-28 stsp if (err)
11941 b2118c49 2020-07-28 stsp return err;
11942 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
11943 b2118c49 2020-07-28 stsp free(id_str);
11944 b2118c49 2020-07-28 stsp }
11945 b2118c49 2020-07-28 stsp
11946 b2118c49 2020-07-28 stsp if (commit_id) {
11947 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
11948 b2118c49 2020-07-28 stsp if (err)
11949 b2118c49 2020-07-28 stsp return err;
11950 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
11951 b2118c49 2020-07-28 stsp free(id_str);
11952 b2118c49 2020-07-28 stsp }
11953 b2118c49 2020-07-28 stsp
11954 b2118c49 2020-07-28 stsp return NULL;
11955 b2118c49 2020-07-28 stsp }
11956 b2118c49 2020-07-28 stsp
11957 b2118c49 2020-07-28 stsp static const struct got_error *
11958 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
11959 b2118c49 2020-07-28 stsp {
11960 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
11961 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
11962 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
11963 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
11964 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11965 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
11966 b2118c49 2020-07-28 stsp int ch, show_files = 0;
11967 b2118c49 2020-07-28 stsp
11968 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
11969 b2118c49 2020-07-28 stsp
11970 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
11971 b2118c49 2020-07-28 stsp switch (ch) {
11972 b2118c49 2020-07-28 stsp default:
11973 b2118c49 2020-07-28 stsp usage_info();
11974 b2118c49 2020-07-28 stsp /* NOTREACHED */
11975 b2118c49 2020-07-28 stsp }
11976 01073a5d 2019-08-22 stsp }
11977 b2118c49 2020-07-28 stsp
11978 b2118c49 2020-07-28 stsp argc -= optind;
11979 b2118c49 2020-07-28 stsp argv += optind;
11980 b2118c49 2020-07-28 stsp
11981 b2118c49 2020-07-28 stsp #ifndef PROFILE
11982 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11983 b2118c49 2020-07-28 stsp NULL) == -1)
11984 b2118c49 2020-07-28 stsp err(1, "pledge");
11985 b2118c49 2020-07-28 stsp #endif
11986 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
11987 b2118c49 2020-07-28 stsp if (cwd == NULL) {
11988 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
11989 b2118c49 2020-07-28 stsp goto done;
11990 b2118c49 2020-07-28 stsp }
11991 b2118c49 2020-07-28 stsp
11992 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
11993 b2118c49 2020-07-28 stsp if (error) {
11994 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11995 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
11996 b2118c49 2020-07-28 stsp goto done;
11997 b2118c49 2020-07-28 stsp }
11998 b2118c49 2020-07-28 stsp
11999 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12000 b2118c49 2020-07-28 stsp if (error)
12001 b2118c49 2020-07-28 stsp goto done;
12002 b2118c49 2020-07-28 stsp
12003 b2118c49 2020-07-28 stsp if (argc >= 1) {
12004 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
12005 b2118c49 2020-07-28 stsp worktree);
12006 b2118c49 2020-07-28 stsp if (error)
12007 b2118c49 2020-07-28 stsp goto done;
12008 b2118c49 2020-07-28 stsp show_files = 1;
12009 b2118c49 2020-07-28 stsp }
12010 b2118c49 2020-07-28 stsp
12011 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
12012 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
12013 b2118c49 2020-07-28 stsp if (error)
12014 b2118c49 2020-07-28 stsp goto done;
12015 b2118c49 2020-07-28 stsp
12016 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
12017 b2118c49 2020-07-28 stsp if (error)
12018 b2118c49 2020-07-28 stsp goto done;
12019 b2118c49 2020-07-28 stsp
12020 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
12021 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
12022 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
12023 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
12024 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
12025 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
12026 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
12027 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
12028 b2118c49 2020-07-28 stsp
12029 b2118c49 2020-07-28 stsp if (show_files) {
12030 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
12031 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
12032 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
12033 b2118c49 2020-07-28 stsp continue;
12034 b2118c49 2020-07-28 stsp /*
12035 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
12036 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
12037 b2118c49 2020-07-28 stsp */
12038 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
12039 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
12040 b2118c49 2020-07-28 stsp }
12041 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
12042 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
12043 b2118c49 2020-07-28 stsp if (error)
12044 b2118c49 2020-07-28 stsp goto done;
12045 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
12046 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
12047 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
12048 b2118c49 2020-07-28 stsp break;
12049 b2118c49 2020-07-28 stsp }
12050 b2118c49 2020-07-28 stsp }
12051 b2118c49 2020-07-28 stsp }
12052 b2118c49 2020-07-28 stsp done:
12053 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
12054 b2118c49 2020-07-28 stsp free((char *)pe->path);
12055 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
12056 b2118c49 2020-07-28 stsp free(cwd);
12057 b2118c49 2020-07-28 stsp free(id_str);
12058 b2118c49 2020-07-28 stsp free(uuidstr);
12059 0ebf8283 2019-07-24 stsp return error;
12060 0ebf8283 2019-07-24 stsp }