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 5c860e29 2018-03-12 stsp
59 5c860e29 2018-03-12 stsp #ifndef nitems
60 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 5c860e29 2018-03-12 stsp #endif
62 99437157 2018-11-11 stsp
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
65 99437157 2018-11-11 stsp
66 99437157 2018-11-11 stsp static void
67 99437157 2018-11-11 stsp catch_sigint(int signo)
68 99437157 2018-11-11 stsp {
69 99437157 2018-11-11 stsp sigint_received = 1;
70 99437157 2018-11-11 stsp }
71 99437157 2018-11-11 stsp
72 99437157 2018-11-11 stsp static void
73 99437157 2018-11-11 stsp catch_sigpipe(int signo)
74 99437157 2018-11-11 stsp {
75 99437157 2018-11-11 stsp sigpipe_received = 1;
76 99437157 2018-11-11 stsp }
77 5c860e29 2018-03-12 stsp
78 99437157 2018-11-11 stsp
79 8cfb4057 2019-07-09 stsp struct got_cmd {
80 820059fa 2020-09-25 stsp const char *cmd_name;
81 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
83 97b3a7be 2019-07-09 stsp const char *cmd_alias;
84 5c860e29 2018-03-12 stsp };
85 5c860e29 2018-03-12 stsp
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
88 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
89 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
90 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
91 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
92 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
95 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
96 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
97 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
98 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
99 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
100 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
101 d00136be 2019-03-26 stsp __dead static void usage_add(void);
102 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
103 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
104 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
105 f8a36e22 2021-08-26 stsp __dead static void usage_send(void);
106 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
107 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
108 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
109 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
110 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
111 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
112 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
113 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
114 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
115 5c860e29 2018-03-12 stsp
116 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
117 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
118 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
119 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
121 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
122 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
124 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
125 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
126 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
127 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
128 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
129 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
130 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
131 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
132 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
133 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
134 f8a36e22 2021-08-26 stsp static const struct got_error* cmd_send(int, char *[]);
135 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
136 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
137 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
138 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
139 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
140 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
141 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
142 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
143 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
144 5c860e29 2018-03-12 stsp
145 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
146 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
147 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
148 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
149 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
150 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
151 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
152 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
153 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
154 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
155 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
156 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
157 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
158 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
159 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
160 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
161 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
162 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
163 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
164 f8a36e22 2021-08-26 stsp { "send", cmd_send, usage_send, "se" },
165 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
166 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
167 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
168 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
169 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
170 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
171 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
172 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
173 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
174 5c860e29 2018-03-12 stsp };
175 ce5b7c56 2019-07-09 stsp
176 ce5b7c56 2019-07-09 stsp static void
177 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
178 ce5b7c56 2019-07-09 stsp {
179 6059809a 2020-12-17 stsp size_t i;
180 ce5b7c56 2019-07-09 stsp
181 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
182 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
183 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
184 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
185 ce5b7c56 2019-07-09 stsp }
186 6879ba42 2020-10-01 naddy fputc('\n', fp);
187 ce5b7c56 2019-07-09 stsp }
188 5c860e29 2018-03-12 stsp
189 ff69268e 2020-12-13 stsp __dead static void
190 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
191 ff69268e 2020-12-13 stsp {
192 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
193 ff69268e 2020-12-13 stsp }
194 ff69268e 2020-12-13 stsp
195 5c860e29 2018-03-12 stsp int
196 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
197 5c860e29 2018-03-12 stsp {
198 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
199 6059809a 2020-12-17 stsp size_t i;
200 5c860e29 2018-03-12 stsp int ch;
201 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
202 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
203 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
204 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
205 83cd27f8 2020-01-13 stsp };
206 5c860e29 2018-03-12 stsp
207 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
208 5c860e29 2018-03-12 stsp
209 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
210 5c860e29 2018-03-12 stsp switch (ch) {
211 1b6b95a8 2018-03-12 stsp case 'h':
212 1b6b95a8 2018-03-12 stsp hflag = 1;
213 53ccebc2 2019-07-30 stsp break;
214 53ccebc2 2019-07-30 stsp case 'V':
215 53ccebc2 2019-07-30 stsp Vflag = 1;
216 1b6b95a8 2018-03-12 stsp break;
217 5c860e29 2018-03-12 stsp default:
218 6879ba42 2020-10-01 naddy usage(hflag, 1);
219 5c860e29 2018-03-12 stsp /* NOTREACHED */
220 5c860e29 2018-03-12 stsp }
221 5c860e29 2018-03-12 stsp }
222 5c860e29 2018-03-12 stsp
223 5c860e29 2018-03-12 stsp argc -= optind;
224 5c860e29 2018-03-12 stsp argv += optind;
225 9814e6a3 2020-09-27 naddy optind = 1;
226 9814e6a3 2020-09-27 naddy optreset = 1;
227 53ccebc2 2019-07-30 stsp
228 53ccebc2 2019-07-30 stsp if (Vflag) {
229 53ccebc2 2019-07-30 stsp got_version_print_str();
230 6879ba42 2020-10-01 naddy return 0;
231 53ccebc2 2019-07-30 stsp }
232 5c860e29 2018-03-12 stsp
233 5c860e29 2018-03-12 stsp if (argc <= 0)
234 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
235 5c860e29 2018-03-12 stsp
236 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
237 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
238 99437157 2018-11-11 stsp
239 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
240 d7d4f210 2018-03-12 stsp const struct got_error *error;
241 d7d4f210 2018-03-12 stsp
242 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
243 5c860e29 2018-03-12 stsp
244 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
245 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
246 5c860e29 2018-03-12 stsp continue;
247 5c860e29 2018-03-12 stsp
248 1b6b95a8 2018-03-12 stsp if (hflag)
249 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
250 1b6b95a8 2018-03-12 stsp
251 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
252 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
253 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
254 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
255 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
256 70015d7a 2019-11-08 stsp !(sigint_received &&
257 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
258 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
259 d7d4f210 2018-03-12 stsp return 1;
260 d7d4f210 2018-03-12 stsp }
261 d7d4f210 2018-03-12 stsp
262 d7d4f210 2018-03-12 stsp return 0;
263 5c860e29 2018-03-12 stsp }
264 5c860e29 2018-03-12 stsp
265 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
266 6879ba42 2020-10-01 naddy list_commands(stderr);
267 5c860e29 2018-03-12 stsp return 1;
268 5c860e29 2018-03-12 stsp }
269 5c860e29 2018-03-12 stsp
270 4ed7e80c 2018-05-20 stsp __dead static void
271 6879ba42 2020-10-01 naddy usage(int hflag, int status)
272 5c860e29 2018-03-12 stsp {
273 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
274 6879ba42 2020-10-01 naddy
275 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
276 53ccebc2 2019-07-30 stsp getprogname());
277 ce5b7c56 2019-07-09 stsp if (hflag)
278 6879ba42 2020-10-01 naddy list_commands(fp);
279 6879ba42 2020-10-01 naddy exit(status);
280 5c860e29 2018-03-12 stsp }
281 5c860e29 2018-03-12 stsp
282 0266afb7 2019-01-04 stsp static const struct got_error *
283 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
284 e2ba3d07 2019-05-13 stsp {
285 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
286 e2ba3d07 2019-05-13 stsp const char *editor;
287 8920fa04 2019-08-18 stsp
288 8920fa04 2019-08-18 stsp *abspath = NULL;
289 e2ba3d07 2019-05-13 stsp
290 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
291 e2ba3d07 2019-05-13 stsp if (editor == NULL)
292 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
293 e2ba3d07 2019-05-13 stsp
294 0ee7065d 2019-05-13 stsp if (editor) {
295 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
296 0ee7065d 2019-05-13 stsp if (err)
297 0ee7065d 2019-05-13 stsp return err;
298 0ee7065d 2019-05-13 stsp }
299 e2ba3d07 2019-05-13 stsp
300 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
301 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
302 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
303 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
304 0ee7065d 2019-05-13 stsp }
305 0ee7065d 2019-05-13 stsp
306 e2ba3d07 2019-05-13 stsp return NULL;
307 e2ba3d07 2019-05-13 stsp }
308 e2ba3d07 2019-05-13 stsp
309 e2ba3d07 2019-05-13 stsp static const struct got_error *
310 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
311 c530dc23 2019-07-23 stsp const char *worktree_path)
312 0266afb7 2019-01-04 stsp {
313 163ce85a 2019-05-13 stsp const struct got_error *err;
314 0266afb7 2019-01-04 stsp
315 37c06ea4 2019-07-15 stsp #ifdef PROFILE
316 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
317 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
318 37c06ea4 2019-07-15 stsp #endif
319 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
320 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
321 0266afb7 2019-01-04 stsp
322 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
323 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
324 0266afb7 2019-01-04 stsp
325 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
326 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
327 0266afb7 2019-01-04 stsp
328 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
329 163ce85a 2019-05-13 stsp if (err != NULL)
330 163ce85a 2019-05-13 stsp return err;
331 0266afb7 2019-01-04 stsp
332 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
333 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
334 0266afb7 2019-01-04 stsp
335 0266afb7 2019-01-04 stsp return NULL;
336 2c7829a4 2019-06-17 stsp }
337 2c7829a4 2019-06-17 stsp
338 2c7829a4 2019-06-17 stsp __dead static void
339 2c7829a4 2019-06-17 stsp usage_init(void)
340 2c7829a4 2019-06-17 stsp {
341 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
342 2c7829a4 2019-06-17 stsp exit(1);
343 2c7829a4 2019-06-17 stsp }
344 2c7829a4 2019-06-17 stsp
345 2c7829a4 2019-06-17 stsp static const struct got_error *
346 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
347 2c7829a4 2019-06-17 stsp {
348 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
349 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
350 2c7829a4 2019-06-17 stsp int ch;
351 2c7829a4 2019-06-17 stsp
352 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
353 2c7829a4 2019-06-17 stsp switch (ch) {
354 2c7829a4 2019-06-17 stsp default:
355 2c7829a4 2019-06-17 stsp usage_init();
356 2c7829a4 2019-06-17 stsp /* NOTREACHED */
357 2c7829a4 2019-06-17 stsp }
358 2c7829a4 2019-06-17 stsp }
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp argc -= optind;
361 2c7829a4 2019-06-17 stsp argv += optind;
362 2c7829a4 2019-06-17 stsp
363 2c7829a4 2019-06-17 stsp #ifndef PROFILE
364 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
365 2c7829a4 2019-06-17 stsp err(1, "pledge");
366 2c7829a4 2019-06-17 stsp #endif
367 2c7829a4 2019-06-17 stsp if (argc != 1)
368 bc20e173 2019-06-17 stsp usage_init();
369 2c7829a4 2019-06-17 stsp
370 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
371 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
372 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
373 2c7829a4 2019-06-17 stsp
374 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
375 2c7829a4 2019-06-17 stsp
376 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
377 2c7829a4 2019-06-17 stsp if (error &&
378 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
379 2c7829a4 2019-06-17 stsp goto done;
380 2c7829a4 2019-06-17 stsp
381 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
382 2c7829a4 2019-06-17 stsp if (error)
383 2c7829a4 2019-06-17 stsp goto done;
384 2c7829a4 2019-06-17 stsp
385 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
386 3ce1b845 2019-07-15 stsp done:
387 3ce1b845 2019-07-15 stsp free(repo_path);
388 3ce1b845 2019-07-15 stsp return error;
389 3ce1b845 2019-07-15 stsp }
390 3ce1b845 2019-07-15 stsp
391 3ce1b845 2019-07-15 stsp __dead static void
392 3ce1b845 2019-07-15 stsp usage_import(void)
393 3ce1b845 2019-07-15 stsp {
394 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
395 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
396 3ce1b845 2019-07-15 stsp exit(1);
397 3ce1b845 2019-07-15 stsp }
398 3ce1b845 2019-07-15 stsp
399 3ce1b845 2019-07-15 stsp int
400 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
401 3ce1b845 2019-07-15 stsp {
402 3ce1b845 2019-07-15 stsp pid_t pid;
403 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
404 3ce1b845 2019-07-15 stsp int st = -1;
405 3ce1b845 2019-07-15 stsp
406 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
407 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
408 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
409 3ce1b845 2019-07-15 stsp
410 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
411 3ce1b845 2019-07-15 stsp case -1:
412 3ce1b845 2019-07-15 stsp goto doneediting;
413 3ce1b845 2019-07-15 stsp case 0:
414 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
415 3ce1b845 2019-07-15 stsp _exit(127);
416 3ce1b845 2019-07-15 stsp }
417 3ce1b845 2019-07-15 stsp
418 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
419 3ce1b845 2019-07-15 stsp if (errno != EINTR)
420 3ce1b845 2019-07-15 stsp break;
421 3ce1b845 2019-07-15 stsp
422 3ce1b845 2019-07-15 stsp doneediting:
423 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
424 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
425 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
426 3ce1b845 2019-07-15 stsp
427 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
428 3ce1b845 2019-07-15 stsp errno = EINTR;
429 3ce1b845 2019-07-15 stsp return -1;
430 3ce1b845 2019-07-15 stsp }
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
433 3ce1b845 2019-07-15 stsp }
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp static const struct got_error *
436 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
437 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
438 28cf319f 2021-01-28 stsp int require_modification)
439 3ce1b845 2019-07-15 stsp {
440 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
441 bfa12d5e 2020-09-26 stsp char *line = NULL;
442 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
443 bfa12d5e 2020-09-26 stsp ssize_t linelen;
444 3ce1b845 2019-07-15 stsp struct stat st, st2;
445 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
446 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
447 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
448 3ce1b845 2019-07-15 stsp
449 3ce1b845 2019-07-15 stsp *logmsg = NULL;
450 3ce1b845 2019-07-15 stsp
451 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
452 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
453 3ce1b845 2019-07-15 stsp
454 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
455 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
456 3ce1b845 2019-07-15 stsp
457 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
458 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
459 3ce1b845 2019-07-15 stsp
460 28cf319f 2021-01-28 stsp if (require_modification &&
461 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
462 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
464 3ce1b845 2019-07-15 stsp
465 bfa12d5e 2020-09-26 stsp /*
466 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
467 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
468 bfa12d5e 2020-09-26 stsp * has in fact been edited.
469 bfa12d5e 2020-09-26 stsp */
470 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
471 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
472 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
473 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
474 bfa12d5e 2020-09-26 stsp
475 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
476 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
477 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
478 bfa12d5e 2020-09-26 stsp goto done;
479 bfa12d5e 2020-09-26 stsp }
480 bfa12d5e 2020-09-26 stsp s = buf;
481 bfa12d5e 2020-09-26 stsp len = 0;
482 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
483 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
484 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
485 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
486 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
487 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
488 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
489 bfa12d5e 2020-09-26 stsp goto done;
490 bfa12d5e 2020-09-26 stsp }
491 bfa12d5e 2020-09-26 stsp }
492 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
493 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
494 bfa12d5e 2020-09-26 stsp len--;
495 bfa12d5e 2020-09-26 stsp }
496 bfa12d5e 2020-09-26 stsp
497 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
498 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
499 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
500 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
501 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
502 3ce1b845 2019-07-15 stsp
503 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
504 3ce1b845 2019-07-15 stsp if (fp == NULL) {
505 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
506 3ce1b845 2019-07-15 stsp goto done;
507 3ce1b845 2019-07-15 stsp }
508 bfa12d5e 2020-09-26 stsp
509 bfa12d5e 2020-09-26 stsp len = 0;
510 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
511 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
512 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
513 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
514 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
515 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
516 bfa12d5e 2020-09-26 stsp goto done;
517 bfa12d5e 2020-09-26 stsp }
518 3ce1b845 2019-07-15 stsp }
519 bfa12d5e 2020-09-26 stsp free(line);
520 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
521 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
522 bfa12d5e 2020-09-26 stsp goto done;
523 bfa12d5e 2020-09-26 stsp }
524 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
525 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
526 3ce1b845 2019-07-15 stsp len--;
527 3ce1b845 2019-07-15 stsp }
528 3ce1b845 2019-07-15 stsp
529 bfa12d5e 2020-09-26 stsp if (len == 0) {
530 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
531 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
532 bfa12d5e 2020-09-26 stsp goto done;
533 bfa12d5e 2020-09-26 stsp }
534 28cf319f 2021-01-28 stsp if (require_modification &&
535 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
536 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
537 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
538 3ce1b845 2019-07-15 stsp done:
539 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
540 bfa12d5e 2020-09-26 stsp free(buf);
541 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
542 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
543 3ce1b845 2019-07-15 stsp if (err) {
544 3ce1b845 2019-07-15 stsp free(*logmsg);
545 3ce1b845 2019-07-15 stsp *logmsg = NULL;
546 3ce1b845 2019-07-15 stsp }
547 3ce1b845 2019-07-15 stsp return err;
548 3ce1b845 2019-07-15 stsp }
549 3ce1b845 2019-07-15 stsp
550 3ce1b845 2019-07-15 stsp static const struct got_error *
551 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
552 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
553 3ce1b845 2019-07-15 stsp {
554 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
555 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
556 1601cb9f 2020-09-11 naddy int initial_content_len;
557 97972933 2020-09-11 stsp int fd = -1;
558 3ce1b845 2019-07-15 stsp
559 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
560 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
561 1601cb9f 2020-09-11 naddy branch_name);
562 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
563 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
564 3ce1b845 2019-07-15 stsp
565 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
566 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
567 3ce1b845 2019-07-15 stsp if (err)
568 3ce1b845 2019-07-15 stsp goto done;
569 3ce1b845 2019-07-15 stsp
570 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
571 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
572 97972933 2020-09-11 stsp goto done;
573 97972933 2020-09-11 stsp }
574 3ce1b845 2019-07-15 stsp
575 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
576 0d5bb276 2020-12-15 stsp initial_content_len, 1);
577 3ce1b845 2019-07-15 stsp done:
578 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
579 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
580 3ce1b845 2019-07-15 stsp free(initial_content);
581 59f86c76 2020-09-11 stsp if (err) {
582 59f86c76 2020-09-11 stsp free(*logmsg_path);
583 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
584 59f86c76 2020-09-11 stsp }
585 3ce1b845 2019-07-15 stsp return err;
586 3ce1b845 2019-07-15 stsp }
587 3ce1b845 2019-07-15 stsp
588 3ce1b845 2019-07-15 stsp static const struct got_error *
589 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
590 3ce1b845 2019-07-15 stsp {
591 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
592 3ce1b845 2019-07-15 stsp return NULL;
593 3ce1b845 2019-07-15 stsp }
594 3ce1b845 2019-07-15 stsp
595 3ce1b845 2019-07-15 stsp static const struct got_error *
596 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
597 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
598 84792843 2019-08-09 stsp {
599 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
600 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
601 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
602 84792843 2019-08-09 stsp
603 84792843 2019-08-09 stsp *author = NULL;
604 aba9c984 2019-09-08 stsp
605 50b0790e 2020-09-11 stsp if (worktree)
606 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
607 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
608 50b0790e 2020-09-11 stsp
609 50b0790e 2020-09-11 stsp /*
610 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
611 50b0790e 2020-09-11 stsp * significant to least significant:
612 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
613 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
614 50b0790e 2020-09-11 stsp * 3) repository's git config file
615 50b0790e 2020-09-11 stsp * 4) environment variables
616 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
617 50b0790e 2020-09-11 stsp */
618 50b0790e 2020-09-11 stsp
619 50b0790e 2020-09-11 stsp if (worktree_conf)
620 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
621 50b0790e 2020-09-11 stsp if (got_author == NULL)
622 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
623 84792843 2019-08-09 stsp if (got_author == NULL) {
624 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
625 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
626 c9956ddf 2019-09-08 stsp if (name && email) {
627 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
628 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
629 c9956ddf 2019-09-08 stsp return NULL;
630 c9956ddf 2019-09-08 stsp }
631 257add31 2020-09-09 stsp
632 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
633 257add31 2020-09-09 stsp if (got_author == NULL) {
634 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
635 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
636 257add31 2020-09-09 stsp repo);
637 257add31 2020-09-09 stsp if (name && email) {
638 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
639 257add31 2020-09-09 stsp == -1)
640 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
641 257add31 2020-09-09 stsp return NULL;
642 257add31 2020-09-09 stsp }
643 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
644 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
645 257add31 2020-09-09 stsp }
646 84792843 2019-08-09 stsp }
647 84792843 2019-08-09 stsp
648 aba9c984 2019-09-08 stsp *author = strdup(got_author);
649 aba9c984 2019-09-08 stsp if (*author == NULL)
650 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
651 84792843 2019-08-09 stsp
652 84792843 2019-08-09 stsp /*
653 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
654 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
655 84792843 2019-08-09 stsp */
656 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
657 84792843 2019-08-09 stsp got_author++;
658 aba9c984 2019-09-08 stsp if (*got_author != '<') {
659 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
660 aba9c984 2019-09-08 stsp goto done;
661 aba9c984 2019-09-08 stsp }
662 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
663 84792843 2019-08-09 stsp got_author++;
664 aba9c984 2019-09-08 stsp if (*got_author != '@') {
665 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
666 aba9c984 2019-09-08 stsp goto done;
667 aba9c984 2019-09-08 stsp }
668 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
669 84792843 2019-08-09 stsp got_author++;
670 84792843 2019-08-09 stsp if (*got_author != '>')
671 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
672 aba9c984 2019-09-08 stsp done:
673 aba9c984 2019-09-08 stsp if (err) {
674 aba9c984 2019-09-08 stsp free(*author);
675 aba9c984 2019-09-08 stsp *author = NULL;
676 aba9c984 2019-09-08 stsp }
677 aba9c984 2019-09-08 stsp return err;
678 c9956ddf 2019-09-08 stsp }
679 c9956ddf 2019-09-08 stsp
680 c9956ddf 2019-09-08 stsp static const struct got_error *
681 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
682 c9956ddf 2019-09-08 stsp {
683 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
684 c9956ddf 2019-09-08 stsp
685 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
686 c9956ddf 2019-09-08 stsp if (homedir) {
687 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
688 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
689 c9956ddf 2019-09-08 stsp
690 c9956ddf 2019-09-08 stsp }
691 c9956ddf 2019-09-08 stsp return NULL;
692 84792843 2019-08-09 stsp }
693 84792843 2019-08-09 stsp
694 84792843 2019-08-09 stsp static const struct got_error *
695 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
696 3ce1b845 2019-07-15 stsp {
697 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
698 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
699 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
700 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
701 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
702 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
703 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
704 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
705 3ce1b845 2019-07-15 stsp int ch;
706 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
707 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
708 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
709 3ce1b845 2019-07-15 stsp
710 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
711 3ce1b845 2019-07-15 stsp
712 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
713 3ce1b845 2019-07-15 stsp switch (ch) {
714 3ce1b845 2019-07-15 stsp case 'b':
715 3ce1b845 2019-07-15 stsp branch_name = optarg;
716 3ce1b845 2019-07-15 stsp break;
717 3ce1b845 2019-07-15 stsp case 'm':
718 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
719 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
720 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
721 3ce1b845 2019-07-15 stsp goto done;
722 3ce1b845 2019-07-15 stsp }
723 3ce1b845 2019-07-15 stsp break;
724 3ce1b845 2019-07-15 stsp case 'r':
725 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
726 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
727 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
728 9ba1d308 2019-10-21 stsp optarg);
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 'I':
733 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
734 3ce1b845 2019-07-15 stsp break;
735 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
736 3ce1b845 2019-07-15 stsp NULL);
737 3ce1b845 2019-07-15 stsp if (error)
738 3ce1b845 2019-07-15 stsp goto done;
739 3ce1b845 2019-07-15 stsp break;
740 3ce1b845 2019-07-15 stsp default:
741 b2b65d18 2019-08-22 stsp usage_import();
742 3ce1b845 2019-07-15 stsp /* NOTREACHED */
743 3ce1b845 2019-07-15 stsp }
744 3ce1b845 2019-07-15 stsp }
745 3ce1b845 2019-07-15 stsp
746 3ce1b845 2019-07-15 stsp argc -= optind;
747 3ce1b845 2019-07-15 stsp argv += optind;
748 3ce1b845 2019-07-15 stsp
749 3ce1b845 2019-07-15 stsp #ifndef PROFILE
750 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
751 aba9c984 2019-09-08 stsp "unveil",
752 3ce1b845 2019-07-15 stsp NULL) == -1)
753 3ce1b845 2019-07-15 stsp err(1, "pledge");
754 3ce1b845 2019-07-15 stsp #endif
755 3ce1b845 2019-07-15 stsp if (argc != 1)
756 3ce1b845 2019-07-15 stsp usage_import();
757 2c7829a4 2019-06-17 stsp
758 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
759 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
760 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
761 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
762 3ce1b845 2019-07-15 stsp }
763 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
764 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
765 c9956ddf 2019-09-08 stsp if (error)
766 c9956ddf 2019-09-08 stsp goto done;
767 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
768 3ce1b845 2019-07-15 stsp if (error)
769 3ce1b845 2019-07-15 stsp goto done;
770 aba9c984 2019-09-08 stsp
771 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
772 aba9c984 2019-09-08 stsp if (error)
773 aba9c984 2019-09-08 stsp return error;
774 e560b7e0 2019-11-28 stsp
775 e560b7e0 2019-11-28 stsp /*
776 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
777 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
778 e560b7e0 2019-11-28 stsp * an unintended typo.
779 e560b7e0 2019-11-28 stsp */
780 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
781 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
782 3ce1b845 2019-07-15 stsp
783 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
784 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
785 3ce1b845 2019-07-15 stsp goto done;
786 3ce1b845 2019-07-15 stsp }
787 3ce1b845 2019-07-15 stsp
788 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
789 3ce1b845 2019-07-15 stsp if (error) {
790 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
791 3ce1b845 2019-07-15 stsp goto done;
792 3ce1b845 2019-07-15 stsp } else {
793 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
794 3ce1b845 2019-07-15 stsp "import target branch already exists");
795 3ce1b845 2019-07-15 stsp goto done;
796 3ce1b845 2019-07-15 stsp }
797 3ce1b845 2019-07-15 stsp
798 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
799 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
800 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
801 3ce1b845 2019-07-15 stsp goto done;
802 3ce1b845 2019-07-15 stsp }
803 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
804 3ce1b845 2019-07-15 stsp
805 3ce1b845 2019-07-15 stsp /*
806 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
807 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
808 3ce1b845 2019-07-15 stsp */
809 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
810 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
811 3ce1b845 2019-07-15 stsp if (error)
812 3ce1b845 2019-07-15 stsp goto done;
813 8e158b01 2019-09-22 stsp free(logmsg);
814 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
815 ef293bdd 2019-10-21 stsp path_dir, refname);
816 ef293bdd 2019-10-21 stsp if (error) {
817 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
818 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
819 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
820 3ce1b845 2019-07-15 stsp goto done;
821 ef293bdd 2019-10-21 stsp }
822 3ce1b845 2019-07-15 stsp }
823 3ce1b845 2019-07-15 stsp
824 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
825 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
826 ef293bdd 2019-10-21 stsp if (logmsg_path)
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 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
832 ef293bdd 2019-10-21 stsp if (error) {
833 ef293bdd 2019-10-21 stsp if (logmsg_path)
834 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
835 ef293bdd 2019-10-21 stsp goto done;
836 ef293bdd 2019-10-21 stsp }
837 ef293bdd 2019-10-21 stsp
838 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
839 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, 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 3ce1b845 2019-07-15 stsp goto done;
844 ef293bdd 2019-10-21 stsp }
845 3ce1b845 2019-07-15 stsp
846 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
847 ef293bdd 2019-10-21 stsp if (error) {
848 ef293bdd 2019-10-21 stsp if (logmsg_path)
849 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
850 3ce1b845 2019-07-15 stsp goto done;
851 ef293bdd 2019-10-21 stsp }
852 3ce1b845 2019-07-15 stsp
853 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
854 ef293bdd 2019-10-21 stsp if (error) {
855 ef293bdd 2019-10-21 stsp if (logmsg_path)
856 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
857 3ce1b845 2019-07-15 stsp goto done;
858 ef293bdd 2019-10-21 stsp }
859 3ce1b845 2019-07-15 stsp
860 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
861 ef293bdd 2019-10-21 stsp if (error) {
862 ef293bdd 2019-10-21 stsp if (logmsg_path)
863 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
864 3ce1b845 2019-07-15 stsp goto done;
865 ef293bdd 2019-10-21 stsp }
866 3ce1b845 2019-07-15 stsp
867 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
868 3ce1b845 2019-07-15 stsp if (error) {
869 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
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_alloc_symref(&head_ref, GOT_REF_HEAD,
876 3ce1b845 2019-07-15 stsp branch_ref);
877 ef293bdd 2019-10-21 stsp if (error) {
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_write(head_ref, repo);
884 ef293bdd 2019-10-21 stsp if (error) {
885 ef293bdd 2019-10-21 stsp if (logmsg_path)
886 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
887 3ce1b845 2019-07-15 stsp goto done;
888 ef293bdd 2019-10-21 stsp }
889 3ce1b845 2019-07-15 stsp }
890 3ce1b845 2019-07-15 stsp
891 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
892 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
893 2c7829a4 2019-06-17 stsp done:
894 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
895 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
896 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
897 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
898 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
899 8e158b01 2019-09-22 stsp free(logmsg);
900 ef293bdd 2019-10-21 stsp free(logmsg_path);
901 2c7829a4 2019-06-17 stsp free(repo_path);
902 3ce1b845 2019-07-15 stsp free(editor);
903 3ce1b845 2019-07-15 stsp free(refname);
904 3ce1b845 2019-07-15 stsp free(new_commit_id);
905 3ce1b845 2019-07-15 stsp free(id_str);
906 aba9c984 2019-09-08 stsp free(author);
907 c9956ddf 2019-09-08 stsp free(gitconfig_path);
908 3ce1b845 2019-07-15 stsp if (branch_ref)
909 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
910 3ce1b845 2019-07-15 stsp if (head_ref)
911 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
912 2c7829a4 2019-06-17 stsp return error;
913 93658fb9 2020-03-18 stsp }
914 93658fb9 2020-03-18 stsp
915 93658fb9 2020-03-18 stsp __dead static void
916 93658fb9 2020-03-18 stsp usage_clone(void)
917 93658fb9 2020-03-18 stsp {
918 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
919 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
920 93658fb9 2020-03-18 stsp exit(1);
921 93658fb9 2020-03-18 stsp }
922 892ac3b6 2020-03-18 stsp
923 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
924 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
925 892ac3b6 2020-03-18 stsp int last_p_indexed;
926 892ac3b6 2020-03-18 stsp int last_p_resolved;
927 68999b92 2020-03-18 stsp int verbosity;
928 04d9a9ec 2020-09-24 stsp
929 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
930 04d9a9ec 2020-09-24 stsp
931 04d9a9ec 2020-09-24 stsp int create_configs;
932 04d9a9ec 2020-09-24 stsp int configs_created;
933 04d9a9ec 2020-09-24 stsp struct {
934 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
935 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
936 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
937 04d9a9ec 2020-09-24 stsp const char *proto;
938 04d9a9ec 2020-09-24 stsp const char *host;
939 04d9a9ec 2020-09-24 stsp const char *port;
940 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
941 04d9a9ec 2020-09-24 stsp const char *git_url;
942 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
943 04d9a9ec 2020-09-24 stsp int mirror_references;
944 04d9a9ec 2020-09-24 stsp } config_info;
945 892ac3b6 2020-03-18 stsp };
946 93658fb9 2020-03-18 stsp
947 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
948 93658fb9 2020-03-18 stsp static const struct got_error *
949 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
950 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
951 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
952 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
953 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
954 04d9a9ec 2020-09-24 stsp
955 04d9a9ec 2020-09-24 stsp static const struct got_error *
956 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
957 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
958 531c3985 2020-03-18 stsp {
959 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
960 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
961 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
962 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
963 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
964 04d9a9ec 2020-09-24 stsp
965 04d9a9ec 2020-09-24 stsp /*
966 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
967 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
968 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
969 04d9a9ec 2020-09-24 stsp * we have all required information.
970 04d9a9ec 2020-09-24 stsp */
971 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
972 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
973 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
974 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
975 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
976 62d463ca 2020-10-20 naddy a->config_info.git_url,
977 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
978 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
979 62d463ca 2020-10-20 naddy a->config_info.symrefs,
980 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
981 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
982 04d9a9ec 2020-09-24 stsp if (err)
983 04d9a9ec 2020-09-24 stsp return err;
984 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
985 04d9a9ec 2020-09-24 stsp }
986 b2409d58 2020-03-18 stsp
987 68999b92 2020-03-18 stsp if (a->verbosity < 0)
988 68999b92 2020-03-18 stsp return NULL;
989 68999b92 2020-03-18 stsp
990 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
991 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
992 892ac3b6 2020-03-18 stsp fflush(stdout);
993 12d1281e 2020-03-19 stsp return NULL;
994 b2409d58 2020-03-18 stsp }
995 b2409d58 2020-03-18 stsp
996 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
997 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
998 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
999 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
1000 892ac3b6 2020-03-18 stsp print_size = 1;
1001 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
1002 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1003 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1004 892ac3b6 2020-03-18 stsp }
1005 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1006 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1007 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1008 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1009 892ac3b6 2020-03-18 stsp print_indexed = 1;
1010 892ac3b6 2020-03-18 stsp print_size = 1;
1011 892ac3b6 2020-03-18 stsp }
1012 61cc1a7a 2020-03-18 stsp }
1013 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1014 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1015 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1016 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1017 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1018 892ac3b6 2020-03-18 stsp print_resolved = 1;
1019 892ac3b6 2020-03-18 stsp print_indexed = 1;
1020 892ac3b6 2020-03-18 stsp print_size = 1;
1021 892ac3b6 2020-03-18 stsp }
1022 61cc1a7a 2020-03-18 stsp }
1023 3168e5da 2020-09-10 stsp
1024 d2cdc636 2020-03-18 stsp }
1025 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1026 892ac3b6 2020-03-18 stsp printf("\r");
1027 892ac3b6 2020-03-18 stsp if (print_size)
1028 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1029 d715f13e 2020-03-19 stsp if (print_indexed)
1030 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1031 d715f13e 2020-03-19 stsp if (print_resolved)
1032 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1033 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1034 892ac3b6 2020-03-18 stsp fflush(stdout);
1035 892ac3b6 2020-03-18 stsp
1036 531c3985 2020-03-18 stsp return NULL;
1037 531c3985 2020-03-18 stsp }
1038 531c3985 2020-03-18 stsp
1039 531c3985 2020-03-18 stsp static const struct got_error *
1040 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1041 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1042 4ba14133 2020-03-20 stsp {
1043 4ba14133 2020-03-20 stsp const struct got_error *err;
1044 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1045 4ba14133 2020-03-20 stsp
1046 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1047 4ba14133 2020-03-20 stsp if (err)
1048 4ba14133 2020-03-20 stsp return err;
1049 4ba14133 2020-03-20 stsp
1050 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1051 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1052 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1053 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1054 6338a6a1 2020-03-21 stsp }
1055 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1056 4ba14133 2020-03-20 stsp return err;
1057 4ba14133 2020-03-20 stsp }
1058 4ba14133 2020-03-20 stsp
1059 4ba14133 2020-03-20 stsp static const struct got_error *
1060 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1061 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1062 41b0de12 2020-03-21 stsp {
1063 41b0de12 2020-03-21 stsp const struct got_error *err;
1064 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1065 41b0de12 2020-03-21 stsp
1066 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1067 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1068 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1069 41b0de12 2020-03-21 stsp
1070 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1071 41b0de12 2020-03-21 stsp }
1072 41b0de12 2020-03-21 stsp
1073 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1074 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1075 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1076 41b0de12 2020-03-21 stsp char *id_str;
1077 41b0de12 2020-03-21 stsp
1078 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1079 41b0de12 2020-03-21 stsp if (err)
1080 41b0de12 2020-03-21 stsp return err;
1081 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1082 41b0de12 2020-03-21 stsp free(id_str);
1083 41b0de12 2020-03-21 stsp }
1084 41b0de12 2020-03-21 stsp
1085 41b0de12 2020-03-21 stsp return NULL;
1086 6338a6a1 2020-03-21 stsp }
1087 6338a6a1 2020-03-21 stsp
1088 6338a6a1 2020-03-21 stsp static const struct got_error *
1089 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1090 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1091 6338a6a1 2020-03-21 stsp {
1092 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1093 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1094 6338a6a1 2020-03-21 stsp char *id_str;
1095 6338a6a1 2020-03-21 stsp
1096 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1097 6338a6a1 2020-03-21 stsp if (err)
1098 6338a6a1 2020-03-21 stsp return err;
1099 6338a6a1 2020-03-21 stsp
1100 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1101 6338a6a1 2020-03-21 stsp if (err)
1102 6338a6a1 2020-03-21 stsp goto done;
1103 6338a6a1 2020-03-21 stsp
1104 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1105 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1106 6338a6a1 2020-03-21 stsp
1107 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1108 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1109 6338a6a1 2020-03-21 stsp done:
1110 6338a6a1 2020-03-21 stsp free(id_str);
1111 6338a6a1 2020-03-21 stsp return err;
1112 0e4002ca 2020-03-21 stsp }
1113 0e4002ca 2020-03-21 stsp
1114 0e4002ca 2020-03-21 stsp static int
1115 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1116 0e4002ca 2020-03-21 stsp {
1117 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1118 0e4002ca 2020-03-21 stsp return 0;
1119 0e4002ca 2020-03-21 stsp refname += 5;
1120 0e4002ca 2020-03-21 stsp
1121 0e4002ca 2020-03-21 stsp /*
1122 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1123 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1124 0e4002ca 2020-03-21 stsp */
1125 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1126 0e4002ca 2020-03-21 stsp return 0;
1127 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1128 0e4002ca 2020-03-21 stsp return 0;
1129 0e4002ca 2020-03-21 stsp
1130 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1131 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1132 0e4002ca 2020-03-21 stsp
1133 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1134 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1135 0e4002ca 2020-03-21 stsp return 1;
1136 0e4002ca 2020-03-21 stsp
1137 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1138 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1139 41b0de12 2020-03-21 stsp }
1140 41b0de12 2020-03-21 stsp
1141 0e4002ca 2020-03-21 stsp static int
1142 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1143 0e4002ca 2020-03-21 stsp {
1144 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1145 0e4002ca 2020-03-21 stsp
1146 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1147 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1148 0e4002ca 2020-03-21 stsp return 1;
1149 0e4002ca 2020-03-21 stsp }
1150 0e4002ca 2020-03-21 stsp
1151 0e4002ca 2020-03-21 stsp return 0;
1152 0e4002ca 2020-03-21 stsp }
1153 0e4002ca 2020-03-21 stsp
1154 41b0de12 2020-03-21 stsp static const struct got_error *
1155 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1156 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1157 0e4002ca 2020-03-21 stsp {
1158 0e4002ca 2020-03-21 stsp const struct got_error *err;
1159 0e4002ca 2020-03-21 stsp char *remote_refname;
1160 0e4002ca 2020-03-21 stsp
1161 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1162 0e4002ca 2020-03-21 stsp refname += 5;
1163 0e4002ca 2020-03-21 stsp
1164 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1165 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1166 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1167 0e4002ca 2020-03-21 stsp
1168 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1169 0e4002ca 2020-03-21 stsp free(remote_refname);
1170 7c0b7f42 2020-09-24 stsp return err;
1171 7c0b7f42 2020-09-24 stsp }
1172 7c0b7f42 2020-09-24 stsp
1173 7c0b7f42 2020-09-24 stsp static const struct got_error *
1174 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1175 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1176 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1177 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1178 99495ddb 2021-01-10 stsp struct got_repository *repo)
1179 7c0b7f42 2020-09-24 stsp {
1180 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1181 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1182 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1183 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1184 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1185 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1186 7c0b7f42 2020-09-24 stsp ssize_t n;
1187 7c0b7f42 2020-09-24 stsp
1188 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1189 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1190 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1191 132af4a5 2021-01-05 stsp char *s;
1192 132af4a5 2021-01-05 stsp branchname = pe->path;
1193 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1194 132af4a5 2021-01-05 stsp branchname += 11;
1195 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1196 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1197 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1198 132af4a5 2021-01-05 stsp goto done;
1199 132af4a5 2021-01-05 stsp }
1200 132af4a5 2021-01-05 stsp free(branches);
1201 132af4a5 2021-01-05 stsp branches = s;
1202 132af4a5 2021-01-05 stsp }
1203 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1204 15d3c221 2021-01-05 stsp branchname = default_branch;
1205 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1206 15d3c221 2021-01-05 stsp branchname += 11;
1207 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1208 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1209 132af4a5 2021-01-05 stsp goto done;
1210 99495ddb 2021-01-10 stsp }
1211 99495ddb 2021-01-10 stsp }
1212 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1213 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1214 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1215 99495ddb 2021-01-10 stsp char *s;
1216 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1217 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1218 99495ddb 2021-01-10 stsp branchname += 5;
1219 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1220 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1221 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1222 99495ddb 2021-01-10 stsp goto done;
1223 99495ddb 2021-01-10 stsp }
1224 99495ddb 2021-01-10 stsp free(refs);
1225 99495ddb 2021-01-10 stsp refs = s;
1226 132af4a5 2021-01-05 stsp }
1227 15d3c221 2021-01-05 stsp }
1228 15d3c221 2021-01-05 stsp
1229 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1230 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1231 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1232 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1233 7c0b7f42 2020-09-24 stsp goto done;
1234 7c0b7f42 2020-09-24 stsp }
1235 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1236 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1237 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1238 7c0b7f42 2020-09-24 stsp goto done;
1239 7c0b7f42 2020-09-24 stsp }
1240 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1241 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1242 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1243 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1244 7c0b7f42 2020-09-24 stsp "%s%s%s"
1245 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1246 15d3c221 2021-01-05 stsp "%s%s%s"
1247 99495ddb 2021-01-10 stsp "%s%s%s"
1248 7c0b7f42 2020-09-24 stsp "%s"
1249 0c8b29c5 2021-01-05 stsp "%s"
1250 7c0b7f42 2020-09-24 stsp "}\n",
1251 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1252 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1253 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1254 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1255 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1256 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1257 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1258 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1259 7c0b7f42 2020-09-24 stsp goto done;
1260 7c0b7f42 2020-09-24 stsp }
1261 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1262 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1263 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1264 7c0b7f42 2020-09-24 stsp goto done;
1265 7c0b7f42 2020-09-24 stsp }
1266 7c0b7f42 2020-09-24 stsp
1267 7c0b7f42 2020-09-24 stsp done:
1268 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1269 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1270 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1271 132af4a5 2021-01-05 stsp free(branches);
1272 7c0b7f42 2020-09-24 stsp return err;
1273 7c0b7f42 2020-09-24 stsp }
1274 7c0b7f42 2020-09-24 stsp
1275 7c0b7f42 2020-09-24 stsp static const struct got_error *
1276 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1277 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1278 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1279 99495ddb 2021-01-10 stsp struct got_repository *repo)
1280 7c0b7f42 2020-09-24 stsp {
1281 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1282 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1283 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1284 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1285 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1286 56d0a753 2021-01-20 stsp const char *branchname;
1287 7c0b7f42 2020-09-24 stsp ssize_t n;
1288 7c0b7f42 2020-09-24 stsp
1289 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1290 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1291 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1292 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1293 7c0b7f42 2020-09-24 stsp goto done;
1294 7c0b7f42 2020-09-24 stsp }
1295 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1296 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1297 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1298 7c0b7f42 2020-09-24 stsp goto done;
1299 7c0b7f42 2020-09-24 stsp }
1300 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1301 56d0a753 2021-01-20 stsp if (mirror_references) {
1302 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1303 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1304 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1305 56d0a753 2021-01-20 stsp goto done;
1306 56d0a753 2021-01-20 stsp }
1307 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1308 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1309 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1310 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1311 7c0b7f42 2020-09-24 stsp goto done;
1312 132af4a5 2021-01-05 stsp }
1313 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1314 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1315 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1316 132af4a5 2021-01-05 stsp char *s;
1317 132af4a5 2021-01-05 stsp branchname = pe->path;
1318 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1319 132af4a5 2021-01-05 stsp branchname += 11;
1320 56d0a753 2021-01-20 stsp if (mirror_references) {
1321 56d0a753 2021-01-20 stsp if (asprintf(&s,
1322 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1323 56d0a753 2021-01-20 stsp branches ? branches : "",
1324 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1325 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1326 56d0a753 2021-01-20 stsp goto done;
1327 56d0a753 2021-01-20 stsp }
1328 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1329 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1330 132af4a5 2021-01-05 stsp branches ? branches : "",
1331 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1332 132af4a5 2021-01-05 stsp branchname) == -1) {
1333 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1334 132af4a5 2021-01-05 stsp goto done;
1335 132af4a5 2021-01-05 stsp }
1336 132af4a5 2021-01-05 stsp free(branches);
1337 132af4a5 2021-01-05 stsp branches = s;
1338 7c0b7f42 2020-09-24 stsp }
1339 7c0b7f42 2020-09-24 stsp } else {
1340 7c0b7f42 2020-09-24 stsp /*
1341 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1342 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1343 7c0b7f42 2020-09-24 stsp */
1344 04d9a9ec 2020-09-24 stsp if (default_branch) {
1345 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1346 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1347 7c0b7f42 2020-09-24 stsp branchname += 11;
1348 7c0b7f42 2020-09-24 stsp } else
1349 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1350 56d0a753 2021-01-20 stsp if (mirror_references) {
1351 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1352 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1353 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1354 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1355 56d0a753 2021-01-20 stsp goto done;
1356 56d0a753 2021-01-20 stsp }
1357 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1358 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1359 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1360 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1361 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1362 7c0b7f42 2020-09-24 stsp goto done;
1363 99495ddb 2021-01-10 stsp }
1364 99495ddb 2021-01-10 stsp }
1365 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1366 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1367 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1368 99495ddb 2021-01-10 stsp char *s;
1369 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1370 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1371 99495ddb 2021-01-10 stsp refname += 5;
1372 56d0a753 2021-01-20 stsp if (mirror_references) {
1373 56d0a753 2021-01-20 stsp if (asprintf(&s,
1374 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1375 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1376 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1377 56d0a753 2021-01-20 stsp goto done;
1378 56d0a753 2021-01-20 stsp }
1379 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1380 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1381 99495ddb 2021-01-10 stsp refs ? refs : "",
1382 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1383 99495ddb 2021-01-10 stsp refname) == -1) {
1384 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1385 99495ddb 2021-01-10 stsp goto done;
1386 99495ddb 2021-01-10 stsp }
1387 99495ddb 2021-01-10 stsp free(refs);
1388 99495ddb 2021-01-10 stsp refs = s;
1389 7c0b7f42 2020-09-24 stsp }
1390 132af4a5 2021-01-05 stsp }
1391 99495ddb 2021-01-10 stsp
1392 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1393 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1394 132af4a5 2021-01-05 stsp "\turl = %s\n"
1395 132af4a5 2021-01-05 stsp "%s"
1396 99495ddb 2021-01-10 stsp "%s"
1397 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1398 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1399 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1400 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1401 132af4a5 2021-01-05 stsp goto done;
1402 7c0b7f42 2020-09-24 stsp }
1403 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1404 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1405 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1406 7c0b7f42 2020-09-24 stsp goto done;
1407 7c0b7f42 2020-09-24 stsp }
1408 7c0b7f42 2020-09-24 stsp done:
1409 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1410 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1411 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1412 132af4a5 2021-01-05 stsp free(branches);
1413 0e4002ca 2020-03-21 stsp return err;
1414 0e4002ca 2020-03-21 stsp }
1415 0e4002ca 2020-03-21 stsp
1416 0e4002ca 2020-03-21 stsp static const struct got_error *
1417 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1418 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1419 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1420 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1421 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1422 04d9a9ec 2020-09-24 stsp {
1423 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1424 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1425 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1426 04d9a9ec 2020-09-24 stsp
1427 04d9a9ec 2020-09-24 stsp /*
1428 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1429 04d9a9ec 2020-09-24 stsp * one of those.
1430 04d9a9ec 2020-09-24 stsp */
1431 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1432 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1433 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1434 04d9a9ec 2020-09-24 stsp } else {
1435 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1436 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1437 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1438 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1439 04d9a9ec 2020-09-24 stsp
1440 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1441 04d9a9ec 2020-09-24 stsp continue;
1442 04d9a9ec 2020-09-24 stsp
1443 04d9a9ec 2020-09-24 stsp default_branch = target;
1444 04d9a9ec 2020-09-24 stsp break;
1445 04d9a9ec 2020-09-24 stsp }
1446 04d9a9ec 2020-09-24 stsp }
1447 04d9a9ec 2020-09-24 stsp
1448 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1449 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1450 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1451 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1452 04d9a9ec 2020-09-24 stsp if (err)
1453 04d9a9ec 2020-09-24 stsp return err;
1454 04d9a9ec 2020-09-24 stsp
1455 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1456 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1457 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1458 04d9a9ec 2020-09-24 stsp }
1459 04d9a9ec 2020-09-24 stsp
1460 04d9a9ec 2020-09-24 stsp static const struct got_error *
1461 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1462 93658fb9 2020-03-18 stsp {
1463 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1464 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1465 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1466 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1467 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1468 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1469 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1470 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1471 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1472 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1473 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1474 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1475 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1476 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1477 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1478 93658fb9 2020-03-18 stsp
1479 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1480 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1481 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1482 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1483 d9b4d0c0 2020-03-18 stsp
1484 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1485 93658fb9 2020-03-18 stsp switch (ch) {
1486 659e7fbd 2020-03-20 stsp case 'a':
1487 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1488 4ba14133 2020-03-20 stsp break;
1489 4ba14133 2020-03-20 stsp case 'b':
1490 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1491 4ba14133 2020-03-20 stsp optarg, NULL);
1492 4ba14133 2020-03-20 stsp if (error)
1493 4ba14133 2020-03-20 stsp return error;
1494 659e7fbd 2020-03-20 stsp break;
1495 41b0de12 2020-03-21 stsp case 'l':
1496 41b0de12 2020-03-21 stsp list_refs_only = 1;
1497 41b0de12 2020-03-21 stsp break;
1498 469dd726 2020-03-20 stsp case 'm':
1499 469dd726 2020-03-20 stsp mirror_references = 1;
1500 469dd726 2020-03-20 stsp break;
1501 68999b92 2020-03-18 stsp case 'v':
1502 68999b92 2020-03-18 stsp if (verbosity < 0)
1503 68999b92 2020-03-18 stsp verbosity = 0;
1504 68999b92 2020-03-18 stsp else if (verbosity < 3)
1505 68999b92 2020-03-18 stsp verbosity++;
1506 68999b92 2020-03-18 stsp break;
1507 68999b92 2020-03-18 stsp case 'q':
1508 68999b92 2020-03-18 stsp verbosity = -1;
1509 68999b92 2020-03-18 stsp break;
1510 0e4002ca 2020-03-21 stsp case 'R':
1511 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1512 0e4002ca 2020-03-21 stsp optarg, NULL);
1513 0e4002ca 2020-03-21 stsp if (error)
1514 0e4002ca 2020-03-21 stsp return error;
1515 0e4002ca 2020-03-21 stsp break;
1516 93658fb9 2020-03-18 stsp default:
1517 93658fb9 2020-03-18 stsp usage_clone();
1518 93658fb9 2020-03-18 stsp break;
1519 93658fb9 2020-03-18 stsp }
1520 93658fb9 2020-03-18 stsp }
1521 93658fb9 2020-03-18 stsp argc -= optind;
1522 93658fb9 2020-03-18 stsp argv += optind;
1523 39c64a6a 2020-03-18 stsp
1524 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1525 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1526 41b0de12 2020-03-21 stsp if (list_refs_only) {
1527 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1528 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1529 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1530 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1531 41b0de12 2020-03-21 stsp if (mirror_references)
1532 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1533 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1534 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1535 41b0de12 2020-03-21 stsp }
1536 4ba14133 2020-03-20 stsp
1537 93658fb9 2020-03-18 stsp uri = argv[0];
1538 9df6f38b 2020-03-18 stsp
1539 9df6f38b 2020-03-18 stsp if (argc == 1)
1540 93658fb9 2020-03-18 stsp dirname = NULL;
1541 9df6f38b 2020-03-18 stsp else if (argc == 2)
1542 93658fb9 2020-03-18 stsp dirname = argv[1];
1543 93658fb9 2020-03-18 stsp else
1544 93658fb9 2020-03-18 stsp usage_clone();
1545 09838ffc 2020-03-18 stsp
1546 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1547 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1548 39c64a6a 2020-03-18 stsp if (error)
1549 09f63084 2020-03-20 stsp goto done;
1550 09f63084 2020-03-20 stsp
1551 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1552 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1553 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1554 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1555 09838ffc 2020-03-18 stsp goto done;
1556 09f63084 2020-03-20 stsp }
1557 09838ffc 2020-03-18 stsp
1558 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1559 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1560 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1561 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1562 39c64a6a 2020-03-18 stsp err(1, "pledge");
1563 b46f3e71 2020-03-18 stsp #endif
1564 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1565 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1566 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1567 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1568 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1569 39c64a6a 2020-03-18 stsp err(1, "pledge");
1570 b46f3e71 2020-03-18 stsp #endif
1571 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1572 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1573 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1574 39c64a6a 2020-03-18 stsp goto done;
1575 39c64a6a 2020-03-18 stsp } else {
1576 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1577 39c64a6a 2020-03-18 stsp goto done;
1578 39c64a6a 2020-03-18 stsp }
1579 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1580 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1581 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1582 bb64b798 2020-03-18 stsp goto done;
1583 bb64b798 2020-03-18 stsp }
1584 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1585 bb64b798 2020-03-18 stsp } else
1586 bb64b798 2020-03-18 stsp repo_path = dirname;
1587 bb64b798 2020-03-18 stsp
1588 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1589 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1590 2751fe64 2020-09-24 stsp if (error &&
1591 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1592 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1593 41b0de12 2020-03-21 stsp goto done;
1594 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1595 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1596 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1597 41b0de12 2020-03-21 stsp goto done;
1598 2751fe64 2020-09-24 stsp }
1599 41b0de12 2020-03-21 stsp }
1600 bb64b798 2020-03-18 stsp
1601 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1602 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1603 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1604 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1605 ee448f5f 2020-03-18 stsp goto done;
1606 ee448f5f 2020-03-18 stsp }
1607 ee448f5f 2020-03-18 stsp }
1608 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1609 ee448f5f 2020-03-18 stsp if (error)
1610 ee448f5f 2020-03-18 stsp goto done;
1611 f79e6490 2020-04-19 stsp
1612 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1613 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1614 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1615 ee448f5f 2020-03-18 stsp
1616 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1617 9c52365f 2020-03-21 stsp server_path, verbosity);
1618 39c64a6a 2020-03-18 stsp if (error)
1619 bb64b798 2020-03-18 stsp goto done;
1620 bb64b798 2020-03-18 stsp
1621 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1622 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1623 2751fe64 2020-09-24 stsp if (error)
1624 2751fe64 2020-09-24 stsp goto done;
1625 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1626 2751fe64 2020-09-24 stsp if (error)
1627 2751fe64 2020-09-24 stsp goto done;
1628 2751fe64 2020-09-24 stsp }
1629 2751fe64 2020-09-24 stsp
1630 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1631 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1632 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1633 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1634 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1635 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1636 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1637 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1639 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1640 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1641 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1644 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1645 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1646 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1647 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1648 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1649 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1650 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1651 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1652 39c64a6a 2020-03-18 stsp if (error)
1653 d9b4d0c0 2020-03-18 stsp goto done;
1654 d9b4d0c0 2020-03-18 stsp
1655 41b0de12 2020-03-21 stsp if (list_refs_only) {
1656 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1657 41b0de12 2020-03-21 stsp goto done;
1658 41b0de12 2020-03-21 stsp }
1659 41b0de12 2020-03-21 stsp
1660 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1661 39c64a6a 2020-03-18 stsp if (error)
1662 d9b4d0c0 2020-03-18 stsp goto done;
1663 68999b92 2020-03-18 stsp if (verbosity >= 0)
1664 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1665 d9b4d0c0 2020-03-18 stsp free(id_str);
1666 d9b4d0c0 2020-03-18 stsp
1667 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1668 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1669 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1670 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1671 7ebc0570 2020-03-18 stsp char *remote_refname;
1672 0e4002ca 2020-03-21 stsp
1673 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1674 0e4002ca 2020-03-21 stsp !mirror_references) {
1675 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1676 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1677 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1678 0e4002ca 2020-03-21 stsp if (error)
1679 0e4002ca 2020-03-21 stsp goto done;
1680 0e4002ca 2020-03-21 stsp continue;
1681 0e4002ca 2020-03-21 stsp }
1682 668a20f6 2020-03-18 stsp
1683 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1684 39c64a6a 2020-03-18 stsp if (error)
1685 d9b4d0c0 2020-03-18 stsp goto done;
1686 d9b4d0c0 2020-03-18 stsp
1687 469dd726 2020-03-20 stsp if (mirror_references)
1688 469dd726 2020-03-20 stsp continue;
1689 469dd726 2020-03-20 stsp
1690 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1691 7ebc0570 2020-03-18 stsp continue;
1692 7ebc0570 2020-03-18 stsp
1693 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1694 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1695 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1696 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1697 7ebc0570 2020-03-18 stsp goto done;
1698 7ebc0570 2020-03-18 stsp }
1699 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1700 f298ae0f 2020-03-25 stsp free(remote_refname);
1701 39c64a6a 2020-03-18 stsp if (error)
1702 d9b4d0c0 2020-03-18 stsp goto done;
1703 d9b4d0c0 2020-03-18 stsp }
1704 d9b4d0c0 2020-03-18 stsp
1705 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1706 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1707 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1708 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1709 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1710 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1711 d9b4d0c0 2020-03-18 stsp
1712 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1713 d9b4d0c0 2020-03-18 stsp continue;
1714 d9b4d0c0 2020-03-18 stsp
1715 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1716 39c64a6a 2020-03-18 stsp if (error) {
1717 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1718 55330abe 2020-03-20 stsp error = NULL;
1719 d9b4d0c0 2020-03-18 stsp continue;
1720 55330abe 2020-03-20 stsp }
1721 d9b4d0c0 2020-03-18 stsp goto done;
1722 d9b4d0c0 2020-03-18 stsp }
1723 d9b4d0c0 2020-03-18 stsp
1724 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1725 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1726 f298ae0f 2020-03-25 stsp if (error)
1727 f298ae0f 2020-03-25 stsp goto done;
1728 f298ae0f 2020-03-25 stsp
1729 f298ae0f 2020-03-25 stsp if (mirror_references)
1730 f298ae0f 2020-03-25 stsp continue;
1731 f298ae0f 2020-03-25 stsp
1732 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1733 f298ae0f 2020-03-25 stsp continue;
1734 f298ae0f 2020-03-25 stsp
1735 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1736 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1737 f298ae0f 2020-03-25 stsp refname) == -1) {
1738 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1739 f298ae0f 2020-03-25 stsp goto done;
1740 f298ae0f 2020-03-25 stsp }
1741 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1742 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1743 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1744 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1745 f298ae0f 2020-03-25 stsp free(remote_refname);
1746 f298ae0f 2020-03-25 stsp goto done;
1747 f298ae0f 2020-03-25 stsp }
1748 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1749 f298ae0f 2020-03-25 stsp if (error) {
1750 f298ae0f 2020-03-25 stsp free(remote_refname);
1751 f298ae0f 2020-03-25 stsp free(remote_target);
1752 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1753 f298ae0f 2020-03-25 stsp error = NULL;
1754 f298ae0f 2020-03-25 stsp continue;
1755 f298ae0f 2020-03-25 stsp }
1756 f298ae0f 2020-03-25 stsp goto done;
1757 f298ae0f 2020-03-25 stsp }
1758 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1759 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1760 f298ae0f 2020-03-25 stsp free(remote_refname);
1761 f298ae0f 2020-03-25 stsp free(remote_target);
1762 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1763 39c64a6a 2020-03-18 stsp if (error)
1764 d9b4d0c0 2020-03-18 stsp goto done;
1765 4ba14133 2020-03-20 stsp }
1766 4ba14133 2020-03-20 stsp if (pe == NULL) {
1767 4ba14133 2020-03-20 stsp /*
1768 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1769 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1770 4ba14133 2020-03-20 stsp * which could be fetched instead.
1771 4ba14133 2020-03-20 stsp */
1772 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1773 4ba14133 2020-03-20 stsp const char *target = pe->path;
1774 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1775 d9b4d0c0 2020-03-18 stsp
1776 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1777 4ba14133 2020-03-20 stsp if (error) {
1778 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1779 4ba14133 2020-03-20 stsp error = NULL;
1780 4ba14133 2020-03-20 stsp continue;
1781 4ba14133 2020-03-20 stsp }
1782 4ba14133 2020-03-20 stsp goto done;
1783 4ba14133 2020-03-20 stsp }
1784 4ba14133 2020-03-20 stsp
1785 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1786 04d9a9ec 2020-09-24 stsp verbosity, repo);
1787 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1788 4ba14133 2020-03-20 stsp if (error)
1789 4ba14133 2020-03-20 stsp goto done;
1790 4ba14133 2020-03-20 stsp break;
1791 4ba14133 2020-03-20 stsp }
1792 659e7fbd 2020-03-20 stsp }
1793 659e7fbd 2020-03-20 stsp
1794 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1795 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1796 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1797 09838ffc 2020-03-18 stsp done:
1798 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1799 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1800 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1801 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1802 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1803 9c52365f 2020-03-21 stsp }
1804 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1805 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1806 1d0f4054 2021-06-17 stsp if (repo) {
1807 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1808 1d0f4054 2021-06-17 stsp if (error == NULL)
1809 1d0f4054 2021-06-17 stsp error = close_err;
1810 1d0f4054 2021-06-17 stsp }
1811 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1812 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1813 d9b4d0c0 2020-03-18 stsp free(pe->data);
1814 d9b4d0c0 2020-03-18 stsp }
1815 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1816 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1817 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1818 d9b4d0c0 2020-03-18 stsp free(pe->data);
1819 d9b4d0c0 2020-03-18 stsp }
1820 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1821 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1822 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1823 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1824 09838ffc 2020-03-18 stsp free(proto);
1825 09838ffc 2020-03-18 stsp free(host);
1826 09838ffc 2020-03-18 stsp free(port);
1827 09838ffc 2020-03-18 stsp free(server_path);
1828 09838ffc 2020-03-18 stsp free(repo_name);
1829 bb64b798 2020-03-18 stsp free(default_destdir);
1830 b46f3e71 2020-03-18 stsp free(git_url);
1831 39c64a6a 2020-03-18 stsp return error;
1832 7848a0e1 2020-03-19 stsp }
1833 7848a0e1 2020-03-19 stsp
1834 7848a0e1 2020-03-19 stsp static const struct got_error *
1835 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1836 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1837 7848a0e1 2020-03-19 stsp {
1838 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1839 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1840 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1841 7848a0e1 2020-03-19 stsp
1842 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1843 7848a0e1 2020-03-19 stsp if (err)
1844 7848a0e1 2020-03-19 stsp goto done;
1845 7848a0e1 2020-03-19 stsp
1846 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1847 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1848 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1849 88609724 2020-03-21 stsp if (err)
1850 88609724 2020-03-21 stsp goto done;
1851 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1852 88609724 2020-03-21 stsp goto done;
1853 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1854 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1855 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1856 db6d8ad8 2020-03-21 stsp }
1857 db6d8ad8 2020-03-21 stsp goto done;
1858 db6d8ad8 2020-03-21 stsp }
1859 db6d8ad8 2020-03-21 stsp
1860 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1861 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1862 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1863 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1864 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1865 688f11b3 2020-03-21 stsp }
1866 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1867 7848a0e1 2020-03-19 stsp if (err)
1868 7848a0e1 2020-03-19 stsp goto done;
1869 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1870 e8a967e0 2020-03-21 stsp if (err)
1871 e8a967e0 2020-03-21 stsp goto done;
1872 7848a0e1 2020-03-19 stsp } else {
1873 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1874 7848a0e1 2020-03-19 stsp if (err)
1875 7848a0e1 2020-03-19 stsp goto done;
1876 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1877 6338a6a1 2020-03-21 stsp goto done;
1878 6338a6a1 2020-03-21 stsp
1879 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1880 6338a6a1 2020-03-21 stsp if (err)
1881 6338a6a1 2020-03-21 stsp goto done;
1882 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1883 6338a6a1 2020-03-21 stsp if (err)
1884 6338a6a1 2020-03-21 stsp goto done;
1885 7848a0e1 2020-03-19 stsp }
1886 6338a6a1 2020-03-21 stsp
1887 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1888 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1889 6338a6a1 2020-03-21 stsp new_id_str);
1890 7848a0e1 2020-03-19 stsp done:
1891 7848a0e1 2020-03-19 stsp free(old_id);
1892 7848a0e1 2020-03-19 stsp free(new_id_str);
1893 7848a0e1 2020-03-19 stsp return err;
1894 2ab43947 2020-03-18 stsp }
1895 f1bcca34 2020-03-25 stsp
1896 f1bcca34 2020-03-25 stsp static const struct got_error *
1897 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1898 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1899 f1bcca34 2020-03-25 stsp {
1900 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1901 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1902 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1903 f1bcca34 2020-03-25 stsp
1904 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1905 bcf34b0e 2020-03-26 stsp if (err) {
1906 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1907 bcf34b0e 2020-03-26 stsp return err;
1908 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1909 bcf34b0e 2020-03-26 stsp if (err)
1910 bcf34b0e 2020-03-26 stsp goto done;
1911 2ab43947 2020-03-18 stsp
1912 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1913 bcf34b0e 2020-03-26 stsp if (err)
1914 bcf34b0e 2020-03-26 stsp goto done;
1915 f1bcca34 2020-03-25 stsp
1916 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1917 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1918 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1919 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1920 bcf34b0e 2020-03-26 stsp } else {
1921 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1922 f1bcca34 2020-03-25 stsp
1923 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1924 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1925 bcf34b0e 2020-03-26 stsp goto done;
1926 bcf34b0e 2020-03-26 stsp
1927 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1928 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1929 bcf34b0e 2020-03-26 stsp if (err)
1930 bcf34b0e 2020-03-26 stsp goto done;
1931 bcf34b0e 2020-03-26 stsp
1932 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1933 bcf34b0e 2020-03-26 stsp if (err)
1934 bcf34b0e 2020-03-26 stsp goto done;
1935 bcf34b0e 2020-03-26 stsp
1936 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1937 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1938 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1939 bcf34b0e 2020-03-26 stsp
1940 bcf34b0e 2020-03-26 stsp }
1941 f1bcca34 2020-03-25 stsp done:
1942 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1943 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1944 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1945 bcf34b0e 2020-03-26 stsp err = unlock_err;
1946 bcf34b0e 2020-03-26 stsp }
1947 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1948 f1bcca34 2020-03-25 stsp return err;
1949 f1bcca34 2020-03-25 stsp }
1950 f1bcca34 2020-03-25 stsp
1951 2ab43947 2020-03-18 stsp __dead static void
1952 7848a0e1 2020-03-19 stsp usage_fetch(void)
1953 7848a0e1 2020-03-19 stsp {
1954 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1955 161728eb 2021-07-24 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1956 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1957 13f12b09 2020-03-21 stsp getprogname());
1958 7848a0e1 2020-03-19 stsp exit(1);
1959 7848a0e1 2020-03-19 stsp }
1960 7848a0e1 2020-03-19 stsp
1961 7848a0e1 2020-03-19 stsp static const struct got_error *
1962 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1963 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1964 f21ec2f0 2020-03-21 stsp {
1965 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1966 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1967 3789fd73 2020-03-26 stsp char *id_str = NULL;
1968 3789fd73 2020-03-26 stsp
1969 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1970 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1971 3789fd73 2020-03-26 stsp if (err)
1972 3789fd73 2020-03-26 stsp return err;
1973 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1974 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1975 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1976 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1977 3789fd73 2020-03-26 stsp }
1978 3789fd73 2020-03-26 stsp } else {
1979 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1980 3789fd73 2020-03-26 stsp if (err)
1981 3789fd73 2020-03-26 stsp return err;
1982 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1983 3789fd73 2020-03-26 stsp if (err)
1984 3789fd73 2020-03-26 stsp goto done;
1985 3168e5da 2020-09-10 stsp
1986 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1987 3789fd73 2020-03-26 stsp if (err)
1988 3789fd73 2020-03-26 stsp goto done;
1989 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1990 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1991 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1992 3789fd73 2020-03-26 stsp }
1993 3789fd73 2020-03-26 stsp }
1994 3789fd73 2020-03-26 stsp done:
1995 3789fd73 2020-03-26 stsp free(id);
1996 3789fd73 2020-03-26 stsp free(id_str);
1997 3789fd73 2020-03-26 stsp return NULL;
1998 3789fd73 2020-03-26 stsp }
1999 3789fd73 2020-03-26 stsp
2000 3789fd73 2020-03-26 stsp static const struct got_error *
2001 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
2002 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2003 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2004 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2005 3789fd73 2020-03-26 stsp {
2006 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2007 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2008 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2009 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2010 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2011 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2012 f21ec2f0 2020-03-21 stsp
2013 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2014 f21ec2f0 2020-03-21 stsp
2015 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2016 3789fd73 2020-03-26 stsp == -1)
2017 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2018 3789fd73 2020-03-26 stsp
2019 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2020 f21ec2f0 2020-03-21 stsp if (err)
2021 3789fd73 2020-03-26 stsp goto done;
2022 f21ec2f0 2020-03-21 stsp
2023 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2024 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2025 f21ec2f0 2020-03-21 stsp
2026 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
2027 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2028 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2029 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2030 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2031 3789fd73 2020-03-26 stsp continue;
2032 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2033 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2034 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2035 3789fd73 2020-03-26 stsp goto done;
2036 3789fd73 2020-03-26 stsp }
2037 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2038 3789fd73 2020-03-26 stsp continue;
2039 3789fd73 2020-03-26 stsp }
2040 f21ec2f0 2020-03-21 stsp
2041 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2042 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2043 f21ec2f0 2020-03-21 stsp break;
2044 f21ec2f0 2020-03-21 stsp }
2045 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2046 f21ec2f0 2020-03-21 stsp continue;
2047 f21ec2f0 2020-03-21 stsp
2048 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2049 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2050 3789fd73 2020-03-26 stsp break;
2051 3789fd73 2020-03-26 stsp }
2052 3789fd73 2020-03-26 stsp if (pe != NULL)
2053 3789fd73 2020-03-26 stsp continue;
2054 f21ec2f0 2020-03-21 stsp
2055 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2056 f21ec2f0 2020-03-21 stsp if (err)
2057 f21ec2f0 2020-03-21 stsp break;
2058 3789fd73 2020-03-26 stsp
2059 3789fd73 2020-03-26 stsp if (local_refname) {
2060 3789fd73 2020-03-26 stsp struct got_reference *ref;
2061 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2062 3789fd73 2020-03-26 stsp if (err) {
2063 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2064 3789fd73 2020-03-26 stsp break;
2065 3789fd73 2020-03-26 stsp free(local_refname);
2066 3789fd73 2020-03-26 stsp local_refname = NULL;
2067 3789fd73 2020-03-26 stsp continue;
2068 3789fd73 2020-03-26 stsp }
2069 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2070 3789fd73 2020-03-26 stsp if (err)
2071 3789fd73 2020-03-26 stsp break;
2072 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2073 3789fd73 2020-03-26 stsp got_ref_close(ref);
2074 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2075 3789fd73 2020-03-26 stsp err = unlock_err;
2076 3789fd73 2020-03-26 stsp break;
2077 3789fd73 2020-03-26 stsp }
2078 3789fd73 2020-03-26 stsp
2079 3789fd73 2020-03-26 stsp free(local_refname);
2080 3789fd73 2020-03-26 stsp local_refname = NULL;
2081 6338a6a1 2020-03-21 stsp }
2082 f21ec2f0 2020-03-21 stsp }
2083 3789fd73 2020-03-26 stsp done:
2084 3789fd73 2020-03-26 stsp free(remote_namespace);
2085 3789fd73 2020-03-26 stsp free(local_refname);
2086 0e4002ca 2020-03-21 stsp return err;
2087 0e4002ca 2020-03-21 stsp }
2088 0e4002ca 2020-03-21 stsp
2089 0e4002ca 2020-03-21 stsp static const struct got_error *
2090 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2091 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2092 0e4002ca 2020-03-21 stsp {
2093 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2094 0e4002ca 2020-03-21 stsp char *remote_refname;
2095 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2096 0e4002ca 2020-03-21 stsp
2097 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2098 0e4002ca 2020-03-21 stsp refname += 5;
2099 0e4002ca 2020-03-21 stsp
2100 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2101 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2102 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2103 f21ec2f0 2020-03-21 stsp
2104 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2105 0e4002ca 2020-03-21 stsp if (err) {
2106 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2107 0e4002ca 2020-03-21 stsp goto done;
2108 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2109 0e4002ca 2020-03-21 stsp } else {
2110 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2111 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2112 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2113 9f142382 2020-03-21 stsp err = unlock_err;
2114 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2115 0e4002ca 2020-03-21 stsp }
2116 0e4002ca 2020-03-21 stsp done:
2117 0e4002ca 2020-03-21 stsp free(remote_refname);
2118 161728eb 2021-07-24 stsp return err;
2119 161728eb 2021-07-24 stsp }
2120 161728eb 2021-07-24 stsp
2121 161728eb 2021-07-24 stsp static const struct got_error *
2122 161728eb 2021-07-24 stsp delete_ref(struct got_repository *repo, struct got_reference *ref)
2123 161728eb 2021-07-24 stsp {
2124 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2125 161728eb 2021-07-24 stsp struct got_object_id *id = NULL;
2126 161728eb 2021-07-24 stsp char *id_str = NULL;
2127 161728eb 2021-07-24 stsp const char *target;
2128 161728eb 2021-07-24 stsp
2129 161728eb 2021-07-24 stsp if (got_ref_is_symbolic(ref)) {
2130 161728eb 2021-07-24 stsp target = got_ref_get_symref_target(ref);
2131 161728eb 2021-07-24 stsp } else {
2132 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2133 161728eb 2021-07-24 stsp if (err)
2134 161728eb 2021-07-24 stsp goto done;
2135 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2136 161728eb 2021-07-24 stsp if (err)
2137 161728eb 2021-07-24 stsp goto done;
2138 161728eb 2021-07-24 stsp target = id_str;
2139 161728eb 2021-07-24 stsp }
2140 161728eb 2021-07-24 stsp
2141 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2142 161728eb 2021-07-24 stsp if (err)
2143 161728eb 2021-07-24 stsp goto done;
2144 161728eb 2021-07-24 stsp
2145 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2146 161728eb 2021-07-24 stsp done:
2147 161728eb 2021-07-24 stsp free(id);
2148 161728eb 2021-07-24 stsp free(id_str);
2149 f21ec2f0 2020-03-21 stsp return err;
2150 f21ec2f0 2020-03-21 stsp }
2151 f21ec2f0 2020-03-21 stsp
2152 f21ec2f0 2020-03-21 stsp static const struct got_error *
2153 161728eb 2021-07-24 stsp delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2154 161728eb 2021-07-24 stsp {
2155 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2156 161728eb 2021-07-24 stsp struct got_reflist_head refs;
2157 161728eb 2021-07-24 stsp struct got_reflist_entry *re;
2158 161728eb 2021-07-24 stsp char *prefix;
2159 161728eb 2021-07-24 stsp
2160 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2161 161728eb 2021-07-24 stsp
2162 161728eb 2021-07-24 stsp if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2163 161728eb 2021-07-24 stsp err = got_error_from_errno("asprintf");
2164 161728eb 2021-07-24 stsp goto done;
2165 161728eb 2021-07-24 stsp }
2166 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2167 161728eb 2021-07-24 stsp if (err)
2168 161728eb 2021-07-24 stsp goto done;
2169 161728eb 2021-07-24 stsp
2170 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2171 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2172 161728eb 2021-07-24 stsp done:
2173 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2174 161728eb 2021-07-24 stsp return err;
2175 161728eb 2021-07-24 stsp }
2176 161728eb 2021-07-24 stsp
2177 161728eb 2021-07-24 stsp static const struct got_error *
2178 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2179 7848a0e1 2020-03-19 stsp {
2180 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2181 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2182 7848a0e1 2020-03-19 stsp const char *remote_name;
2183 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2184 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2185 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2186 7848a0e1 2020-03-19 stsp int nremotes;
2187 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2188 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2189 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2190 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2191 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2192 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2193 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2194 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2195 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2196 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2197 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2198 161728eb 2021-07-24 stsp int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2199 7848a0e1 2020-03-19 stsp
2200 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2201 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2202 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2203 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2204 7848a0e1 2020-03-19 stsp
2205 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2206 7848a0e1 2020-03-19 stsp switch (ch) {
2207 659e7fbd 2020-03-20 stsp case 'a':
2208 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2209 4ba14133 2020-03-20 stsp break;
2210 4ba14133 2020-03-20 stsp case 'b':
2211 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2212 4ba14133 2020-03-20 stsp optarg, NULL);
2213 4ba14133 2020-03-20 stsp if (error)
2214 4ba14133 2020-03-20 stsp return error;
2215 41b0de12 2020-03-21 stsp break;
2216 f21ec2f0 2020-03-21 stsp case 'd':
2217 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2218 f21ec2f0 2020-03-21 stsp break;
2219 41b0de12 2020-03-21 stsp case 'l':
2220 41b0de12 2020-03-21 stsp list_refs_only = 1;
2221 659e7fbd 2020-03-20 stsp break;
2222 7848a0e1 2020-03-19 stsp case 'r':
2223 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2224 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2225 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2226 7848a0e1 2020-03-19 stsp optarg);
2227 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2228 7848a0e1 2020-03-19 stsp break;
2229 db6d8ad8 2020-03-21 stsp case 't':
2230 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2231 db6d8ad8 2020-03-21 stsp break;
2232 7848a0e1 2020-03-19 stsp case 'v':
2233 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2234 7848a0e1 2020-03-19 stsp verbosity = 0;
2235 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2236 7848a0e1 2020-03-19 stsp verbosity++;
2237 7848a0e1 2020-03-19 stsp break;
2238 7848a0e1 2020-03-19 stsp case 'q':
2239 7848a0e1 2020-03-19 stsp verbosity = -1;
2240 7848a0e1 2020-03-19 stsp break;
2241 0e4002ca 2020-03-21 stsp case 'R':
2242 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2243 0e4002ca 2020-03-21 stsp optarg, NULL);
2244 0e4002ca 2020-03-21 stsp if (error)
2245 0e4002ca 2020-03-21 stsp return error;
2246 0e4002ca 2020-03-21 stsp break;
2247 161728eb 2021-07-24 stsp case 'X':
2248 161728eb 2021-07-24 stsp delete_remote = 1;
2249 161728eb 2021-07-24 stsp break;
2250 7848a0e1 2020-03-19 stsp default:
2251 7848a0e1 2020-03-19 stsp usage_fetch();
2252 7848a0e1 2020-03-19 stsp break;
2253 7848a0e1 2020-03-19 stsp }
2254 7848a0e1 2020-03-19 stsp }
2255 7848a0e1 2020-03-19 stsp argc -= optind;
2256 7848a0e1 2020-03-19 stsp argv += optind;
2257 7848a0e1 2020-03-19 stsp
2258 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2259 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2260 41b0de12 2020-03-21 stsp if (list_refs_only) {
2261 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2262 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2263 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2264 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2265 f21ec2f0 2020-03-21 stsp if (delete_refs)
2266 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2267 161728eb 2021-07-24 stsp if (delete_remote)
2268 161728eb 2021-07-24 stsp option_conflict('l', 'X');
2269 41b0de12 2020-03-21 stsp }
2270 161728eb 2021-07-24 stsp if (delete_remote) {
2271 161728eb 2021-07-24 stsp if (fetch_all_branches)
2272 161728eb 2021-07-24 stsp option_conflict('X', 'a');
2273 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_branches))
2274 161728eb 2021-07-24 stsp option_conflict('X', 'b');
2275 161728eb 2021-07-24 stsp if (delete_refs)
2276 161728eb 2021-07-24 stsp option_conflict('X', 'd');
2277 161728eb 2021-07-24 stsp if (replace_tags)
2278 161728eb 2021-07-24 stsp option_conflict('X', 't');
2279 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_refs))
2280 161728eb 2021-07-24 stsp option_conflict('X', 'R');
2281 161728eb 2021-07-24 stsp }
2282 161728eb 2021-07-24 stsp
2283 161728eb 2021-07-24 stsp if (argc == 0) {
2284 161728eb 2021-07-24 stsp if (delete_remote)
2285 161728eb 2021-07-24 stsp errx(1, "-X option requires a remote name");
2286 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2287 161728eb 2021-07-24 stsp } else if (argc == 1)
2288 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2289 7848a0e1 2020-03-19 stsp else
2290 7848a0e1 2020-03-19 stsp usage_fetch();
2291 7848a0e1 2020-03-19 stsp
2292 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2293 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2294 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2295 7848a0e1 2020-03-19 stsp goto done;
2296 7848a0e1 2020-03-19 stsp }
2297 7848a0e1 2020-03-19 stsp
2298 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2299 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2300 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2301 7848a0e1 2020-03-19 stsp goto done;
2302 7848a0e1 2020-03-19 stsp else
2303 7848a0e1 2020-03-19 stsp error = NULL;
2304 7848a0e1 2020-03-19 stsp if (worktree) {
2305 7848a0e1 2020-03-19 stsp repo_path =
2306 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2307 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2308 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2309 7848a0e1 2020-03-19 stsp if (error)
2310 7848a0e1 2020-03-19 stsp goto done;
2311 7848a0e1 2020-03-19 stsp } else {
2312 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2313 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2314 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2315 7848a0e1 2020-03-19 stsp goto done;
2316 7848a0e1 2020-03-19 stsp }
2317 7848a0e1 2020-03-19 stsp }
2318 7848a0e1 2020-03-19 stsp }
2319 7848a0e1 2020-03-19 stsp
2320 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2321 7848a0e1 2020-03-19 stsp if (error)
2322 7848a0e1 2020-03-19 stsp goto done;
2323 7848a0e1 2020-03-19 stsp
2324 161728eb 2021-07-24 stsp if (delete_remote) {
2325 161728eb 2021-07-24 stsp error = delete_refs_for_remote(repo, remote_name);
2326 161728eb 2021-07-24 stsp goto done; /* nothing else to do */
2327 161728eb 2021-07-24 stsp }
2328 161728eb 2021-07-24 stsp
2329 50b0790e 2020-09-11 stsp if (worktree) {
2330 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2331 50b0790e 2020-09-11 stsp if (worktree_conf) {
2332 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2333 50b0790e 2020-09-11 stsp worktree_conf);
2334 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2335 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2336 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2337 50b0790e 2020-09-11 stsp break;
2338 54eb00d5 2020-10-20 stsp }
2339 50b0790e 2020-09-11 stsp }
2340 50b0790e 2020-09-11 stsp }
2341 7848a0e1 2020-03-19 stsp }
2342 50b0790e 2020-09-11 stsp if (remote == NULL) {
2343 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2344 50b0790e 2020-09-11 stsp if (repo_conf) {
2345 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2346 50b0790e 2020-09-11 stsp repo_conf);
2347 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2348 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2349 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2350 50b0790e 2020-09-11 stsp break;
2351 54eb00d5 2020-10-20 stsp }
2352 50b0790e 2020-09-11 stsp }
2353 50b0790e 2020-09-11 stsp }
2354 50b0790e 2020-09-11 stsp }
2355 50b0790e 2020-09-11 stsp if (remote == NULL) {
2356 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2357 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2358 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2359 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2360 257add31 2020-09-09 stsp break;
2361 54eb00d5 2020-10-20 stsp }
2362 257add31 2020-09-09 stsp }
2363 7848a0e1 2020-03-19 stsp }
2364 50b0790e 2020-09-11 stsp if (remote == NULL) {
2365 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2366 50b0790e 2020-09-11 stsp goto done;
2367 b8adfa55 2020-09-25 stsp }
2368 b8adfa55 2020-09-25 stsp
2369 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2370 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2371 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2372 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_branches; i++) {
2373 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2374 6480c871 2021-08-30 stsp remote->fetch_branches[i], NULL);
2375 99495ddb 2021-01-10 stsp }
2376 99495ddb 2021-01-10 stsp }
2377 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2378 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_refs; i++) {
2379 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2380 6480c871 2021-08-30 stsp remote->fetch_refs[i], NULL);
2381 b8adfa55 2020-09-25 stsp }
2382 50b0790e 2020-09-11 stsp }
2383 7848a0e1 2020-03-19 stsp
2384 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2385 6480c871 2021-08-30 stsp &repo_name, remote->fetch_url);
2386 7848a0e1 2020-03-19 stsp if (error)
2387 7848a0e1 2020-03-19 stsp goto done;
2388 7848a0e1 2020-03-19 stsp
2389 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2390 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2391 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2392 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2393 7848a0e1 2020-03-19 stsp err(1, "pledge");
2394 7848a0e1 2020-03-19 stsp #endif
2395 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2396 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2397 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2398 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2399 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2400 7848a0e1 2020-03-19 stsp err(1, "pledge");
2401 7848a0e1 2020-03-19 stsp #endif
2402 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2403 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2404 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2405 7848a0e1 2020-03-19 stsp goto done;
2406 7848a0e1 2020-03-19 stsp } else {
2407 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2408 7848a0e1 2020-03-19 stsp goto done;
2409 7848a0e1 2020-03-19 stsp }
2410 7848a0e1 2020-03-19 stsp
2411 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2412 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2413 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2414 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2415 7848a0e1 2020-03-19 stsp goto done;
2416 7848a0e1 2020-03-19 stsp }
2417 7848a0e1 2020-03-19 stsp }
2418 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2419 7848a0e1 2020-03-19 stsp if (error)
2420 7848a0e1 2020-03-19 stsp goto done;
2421 f79e6490 2020-04-19 stsp
2422 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2423 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2424 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2425 7848a0e1 2020-03-19 stsp
2426 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2427 9c52365f 2020-03-21 stsp server_path, verbosity);
2428 7848a0e1 2020-03-19 stsp if (error)
2429 7848a0e1 2020-03-19 stsp goto done;
2430 7848a0e1 2020-03-19 stsp
2431 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2432 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2433 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2434 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2435 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2436 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2437 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2438 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2439 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2440 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2441 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2442 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2443 7848a0e1 2020-03-19 stsp if (error)
2444 7848a0e1 2020-03-19 stsp goto done;
2445 7848a0e1 2020-03-19 stsp
2446 41b0de12 2020-03-21 stsp if (list_refs_only) {
2447 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2448 41b0de12 2020-03-21 stsp goto done;
2449 41b0de12 2020-03-21 stsp }
2450 41b0de12 2020-03-21 stsp
2451 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2452 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2453 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2454 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2455 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2456 984065c8 2020-03-19 stsp if (error)
2457 984065c8 2020-03-19 stsp goto done;
2458 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2459 984065c8 2020-03-19 stsp free(id_str);
2460 984065c8 2020-03-19 stsp id_str = NULL;
2461 984065c8 2020-03-19 stsp }
2462 7848a0e1 2020-03-19 stsp
2463 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2464 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2465 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2466 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2467 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2468 7848a0e1 2020-03-19 stsp char *remote_refname;
2469 7848a0e1 2020-03-19 stsp
2470 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2471 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2472 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2473 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2474 0e4002ca 2020-03-21 stsp if (error)
2475 0e4002ca 2020-03-21 stsp goto done;
2476 0e4002ca 2020-03-21 stsp continue;
2477 0e4002ca 2020-03-21 stsp }
2478 0e4002ca 2020-03-21 stsp
2479 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2480 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2481 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2482 7848a0e1 2020-03-19 stsp if (error) {
2483 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2484 7848a0e1 2020-03-19 stsp goto done;
2485 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2486 6338a6a1 2020-03-21 stsp repo);
2487 7848a0e1 2020-03-19 stsp if (error)
2488 7848a0e1 2020-03-19 stsp goto done;
2489 7848a0e1 2020-03-19 stsp } else {
2490 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2491 db6d8ad8 2020-03-21 stsp verbosity, repo);
2492 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2493 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2494 9f142382 2020-03-21 stsp error = unlock_err;
2495 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2496 7848a0e1 2020-03-19 stsp if (error)
2497 7848a0e1 2020-03-19 stsp goto done;
2498 7848a0e1 2020-03-19 stsp }
2499 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2500 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2501 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2502 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2503 7848a0e1 2020-03-19 stsp goto done;
2504 7848a0e1 2020-03-19 stsp }
2505 7848a0e1 2020-03-19 stsp
2506 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2507 7848a0e1 2020-03-19 stsp if (error) {
2508 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2509 7848a0e1 2020-03-19 stsp goto done;
2510 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2511 688f11b3 2020-03-21 stsp verbosity, repo);
2512 7848a0e1 2020-03-19 stsp if (error)
2513 7848a0e1 2020-03-19 stsp goto done;
2514 7848a0e1 2020-03-19 stsp } else {
2515 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2516 db6d8ad8 2020-03-21 stsp verbosity, repo);
2517 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2518 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2519 9f142382 2020-03-21 stsp error = unlock_err;
2520 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2521 7848a0e1 2020-03-19 stsp if (error)
2522 7848a0e1 2020-03-19 stsp goto done;
2523 7848a0e1 2020-03-19 stsp }
2524 2ec30c80 2020-03-20 stsp
2525 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2526 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2527 2ec30c80 2020-03-20 stsp if (error) {
2528 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2529 2ec30c80 2020-03-20 stsp goto done;
2530 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2531 6338a6a1 2020-03-21 stsp repo);
2532 2ec30c80 2020-03-20 stsp if (error)
2533 2ec30c80 2020-03-20 stsp goto done;
2534 9f142382 2020-03-21 stsp } else {
2535 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2536 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2537 9f142382 2020-03-21 stsp error = unlock_err;
2538 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2539 9f142382 2020-03-21 stsp }
2540 7848a0e1 2020-03-19 stsp }
2541 7848a0e1 2020-03-19 stsp }
2542 f1bcca34 2020-03-25 stsp if (delete_refs) {
2543 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2544 3789fd73 2020-03-26 stsp verbosity, repo);
2545 f1bcca34 2020-03-25 stsp if (error)
2546 f1bcca34 2020-03-25 stsp goto done;
2547 f1bcca34 2020-03-25 stsp }
2548 f1bcca34 2020-03-25 stsp
2549 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2550 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2551 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2552 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2553 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2554 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2555 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2556 f1bcca34 2020-03-25 stsp
2557 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2558 f1bcca34 2020-03-25 stsp continue;
2559 f1bcca34 2020-03-25 stsp
2560 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2561 f1bcca34 2020-03-25 stsp continue;
2562 f1bcca34 2020-03-25 stsp
2563 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2564 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2565 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2566 f1bcca34 2020-03-25 stsp goto done;
2567 f1bcca34 2020-03-25 stsp }
2568 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2569 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2570 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2571 f1bcca34 2020-03-25 stsp free(remote_refname);
2572 f1bcca34 2020-03-25 stsp goto done;
2573 f1bcca34 2020-03-25 stsp }
2574 f1bcca34 2020-03-25 stsp
2575 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2576 f1bcca34 2020-03-25 stsp 0);
2577 f1bcca34 2020-03-25 stsp if (error) {
2578 f1bcca34 2020-03-25 stsp free(remote_refname);
2579 f1bcca34 2020-03-25 stsp free(remote_target);
2580 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2581 f1bcca34 2020-03-25 stsp error = NULL;
2582 f1bcca34 2020-03-25 stsp continue;
2583 f1bcca34 2020-03-25 stsp }
2584 f1bcca34 2020-03-25 stsp goto done;
2585 f1bcca34 2020-03-25 stsp }
2586 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2587 f1bcca34 2020-03-25 stsp verbosity, repo);
2588 f1bcca34 2020-03-25 stsp free(remote_refname);
2589 f1bcca34 2020-03-25 stsp free(remote_target);
2590 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2591 f1bcca34 2020-03-25 stsp if (error)
2592 f1bcca34 2020-03-25 stsp goto done;
2593 f1bcca34 2020-03-25 stsp }
2594 f1bcca34 2020-03-25 stsp }
2595 7848a0e1 2020-03-19 stsp done:
2596 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2597 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2598 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2599 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2600 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2601 9c52365f 2020-03-21 stsp }
2602 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2603 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2604 1d0f4054 2021-06-17 stsp if (repo) {
2605 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2606 1d0f4054 2021-06-17 stsp if (error == NULL)
2607 1d0f4054 2021-06-17 stsp error = close_err;
2608 1d0f4054 2021-06-17 stsp }
2609 7848a0e1 2020-03-19 stsp if (worktree)
2610 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2611 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2612 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2613 7848a0e1 2020-03-19 stsp free(pe->data);
2614 7848a0e1 2020-03-19 stsp }
2615 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2616 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2617 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2618 7848a0e1 2020-03-19 stsp free(pe->data);
2619 7848a0e1 2020-03-19 stsp }
2620 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2621 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2622 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2623 7848a0e1 2020-03-19 stsp free(id_str);
2624 7848a0e1 2020-03-19 stsp free(cwd);
2625 7848a0e1 2020-03-19 stsp free(repo_path);
2626 7848a0e1 2020-03-19 stsp free(pack_hash);
2627 7848a0e1 2020-03-19 stsp free(proto);
2628 7848a0e1 2020-03-19 stsp free(host);
2629 7848a0e1 2020-03-19 stsp free(port);
2630 7848a0e1 2020-03-19 stsp free(server_path);
2631 7848a0e1 2020-03-19 stsp free(repo_name);
2632 7848a0e1 2020-03-19 stsp return error;
2633 7848a0e1 2020-03-19 stsp }
2634 7848a0e1 2020-03-19 stsp
2635 7848a0e1 2020-03-19 stsp
2636 7848a0e1 2020-03-19 stsp __dead static void
2637 2ab43947 2020-03-18 stsp usage_checkout(void)
2638 2ab43947 2020-03-18 stsp {
2639 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2640 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2641 2ab43947 2020-03-18 stsp exit(1);
2642 2ab43947 2020-03-18 stsp }
2643 2ab43947 2020-03-18 stsp
2644 2ab43947 2020-03-18 stsp static void
2645 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2646 2ab43947 2020-03-18 stsp {
2647 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2648 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2649 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2650 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2651 2ab43947 2020-03-18 stsp getprogname());
2652 2ab43947 2020-03-18 stsp }
2653 2ab43947 2020-03-18 stsp
2654 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2655 2ab43947 2020-03-18 stsp const char *worktree_path;
2656 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2657 2ab43947 2020-03-18 stsp };
2658 2ab43947 2020-03-18 stsp
2659 2ab43947 2020-03-18 stsp static const struct got_error *
2660 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2661 2ab43947 2020-03-18 stsp {
2662 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2663 2ab43947 2020-03-18 stsp
2664 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2665 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2666 2ab43947 2020-03-18 stsp return NULL;
2667 2ab43947 2020-03-18 stsp
2668 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2669 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2670 2ab43947 2020-03-18 stsp return NULL;
2671 2ab43947 2020-03-18 stsp }
2672 2ab43947 2020-03-18 stsp
2673 2ab43947 2020-03-18 stsp while (path[0] == '/')
2674 2ab43947 2020-03-18 stsp path++;
2675 2ab43947 2020-03-18 stsp
2676 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2677 2ab43947 2020-03-18 stsp return NULL;
2678 2ab43947 2020-03-18 stsp }
2679 2ab43947 2020-03-18 stsp
2680 2ab43947 2020-03-18 stsp static const struct got_error *
2681 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2682 2ab43947 2020-03-18 stsp {
2683 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2684 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2685 2ab43947 2020-03-18 stsp return NULL;
2686 2ab43947 2020-03-18 stsp }
2687 2ab43947 2020-03-18 stsp
2688 2ab43947 2020-03-18 stsp static const struct got_error *
2689 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2690 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2691 2ab43947 2020-03-18 stsp struct got_repository *repo)
2692 2ab43947 2020-03-18 stsp {
2693 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2694 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2695 2ab43947 2020-03-18 stsp
2696 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2697 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2698 2ab43947 2020-03-18 stsp if (err)
2699 2ab43947 2020-03-18 stsp return err;
2700 2ab43947 2020-03-18 stsp
2701 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2702 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2703 2ab43947 2020-03-18 stsp
2704 2ab43947 2020-03-18 stsp /*
2705 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2706 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2707 2ab43947 2020-03-18 stsp *
2708 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2709 2ab43947 2020-03-18 stsp *
2710 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2711 2ab43947 2020-03-18 stsp * \ /
2712 2ab43947 2020-03-18 stsp * C E
2713 2ab43947 2020-03-18 stsp * \ /
2714 2ab43947 2020-03-18 stsp * B (yca)
2715 2ab43947 2020-03-18 stsp * |
2716 2ab43947 2020-03-18 stsp * A
2717 2ab43947 2020-03-18 stsp *
2718 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2719 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2720 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2721 2ab43947 2020-03-18 stsp */
2722 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2723 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2724 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2725 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2726 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2727 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2728 2ab43947 2020-03-18 stsp
2729 2ab43947 2020-03-18 stsp free(yca_id);
2730 2ab43947 2020-03-18 stsp return NULL;
2731 2ab43947 2020-03-18 stsp }
2732 2ab43947 2020-03-18 stsp
2733 2ab43947 2020-03-18 stsp static const struct got_error *
2734 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2735 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2736 2ab43947 2020-03-18 stsp struct got_repository *repo)
2737 2ab43947 2020-03-18 stsp {
2738 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2739 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2740 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2741 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2742 2ab43947 2020-03-18 stsp
2743 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2744 2ab43947 2020-03-18 stsp if (err)
2745 2ab43947 2020-03-18 stsp goto done;
2746 2ab43947 2020-03-18 stsp
2747 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2748 2ab43947 2020-03-18 stsp is_same_branch = 1;
2749 2ab43947 2020-03-18 stsp goto done;
2750 2ab43947 2020-03-18 stsp }
2751 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2752 2ab43947 2020-03-18 stsp is_same_branch = 1;
2753 2ab43947 2020-03-18 stsp goto done;
2754 2ab43947 2020-03-18 stsp }
2755 2ab43947 2020-03-18 stsp
2756 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2757 2ab43947 2020-03-18 stsp if (err)
2758 2ab43947 2020-03-18 stsp goto done;
2759 2ab43947 2020-03-18 stsp
2760 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2761 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2762 2ab43947 2020-03-18 stsp if (err)
2763 2ab43947 2020-03-18 stsp goto done;
2764 2ab43947 2020-03-18 stsp
2765 2ab43947 2020-03-18 stsp for (;;) {
2766 2ab43947 2020-03-18 stsp struct got_object_id *id;
2767 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2768 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2769 2ab43947 2020-03-18 stsp if (err) {
2770 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2771 2ab43947 2020-03-18 stsp err = NULL;
2772 2ab43947 2020-03-18 stsp break;
2773 2ab43947 2020-03-18 stsp }
2774 2ab43947 2020-03-18 stsp
2775 2ab43947 2020-03-18 stsp if (id) {
2776 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2777 2ab43947 2020-03-18 stsp break;
2778 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2779 2ab43947 2020-03-18 stsp is_same_branch = 1;
2780 2ab43947 2020-03-18 stsp break;
2781 2ab43947 2020-03-18 stsp }
2782 2ab43947 2020-03-18 stsp }
2783 2ab43947 2020-03-18 stsp }
2784 2ab43947 2020-03-18 stsp done:
2785 2ab43947 2020-03-18 stsp if (graph)
2786 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2787 2ab43947 2020-03-18 stsp free(head_commit_id);
2788 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2789 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2790 2ab43947 2020-03-18 stsp return err;
2791 a367ff0f 2019-05-14 stsp }
2792 8069f636 2019-01-12 stsp
2793 4ed7e80c 2018-05-20 stsp static const struct got_error *
2794 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2795 4b6c9460 2020-03-05 stsp {
2796 4b6c9460 2020-03-05 stsp static char msg[512];
2797 4b6c9460 2020-03-05 stsp const char *branch_name;
2798 4b6c9460 2020-03-05 stsp
2799 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2800 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2801 4b6c9460 2020-03-05 stsp else
2802 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2803 4b6c9460 2020-03-05 stsp
2804 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2805 4b6c9460 2020-03-05 stsp branch_name += 11;
2806 4b6c9460 2020-03-05 stsp
2807 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2808 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2809 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2810 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2811 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2812 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2813 4b6c9460 2020-03-05 stsp
2814 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2815 4b6c9460 2020-03-05 stsp }
2816 4b6c9460 2020-03-05 stsp
2817 4b6c9460 2020-03-05 stsp static const struct got_error *
2818 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2819 c09a553d 2018-03-12 stsp {
2820 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2821 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2822 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2823 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2824 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2825 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2826 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2827 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2828 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2829 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2830 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2831 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2832 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2833 f2ea84fa 2019-07-27 stsp
2834 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2835 c09a553d 2018-03-12 stsp
2836 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2837 0bb8a95e 2018-03-12 stsp switch (ch) {
2838 08573d5b 2019-05-14 stsp case 'b':
2839 08573d5b 2019-05-14 stsp branch_name = optarg;
2840 08573d5b 2019-05-14 stsp break;
2841 8069f636 2019-01-12 stsp case 'c':
2842 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2843 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2844 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2845 bb51a5b4 2020-01-13 stsp break;
2846 bb51a5b4 2020-01-13 stsp case 'E':
2847 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2848 8069f636 2019-01-12 stsp break;
2849 0bb8a95e 2018-03-12 stsp case 'p':
2850 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2851 0bb8a95e 2018-03-12 stsp break;
2852 0bb8a95e 2018-03-12 stsp default:
2853 2deda0b9 2019-03-07 stsp usage_checkout();
2854 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2855 0bb8a95e 2018-03-12 stsp }
2856 0bb8a95e 2018-03-12 stsp }
2857 0bb8a95e 2018-03-12 stsp
2858 0bb8a95e 2018-03-12 stsp argc -= optind;
2859 0bb8a95e 2018-03-12 stsp argv += optind;
2860 0bb8a95e 2018-03-12 stsp
2861 6715a751 2018-03-16 stsp #ifndef PROFILE
2862 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2863 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2864 c09a553d 2018-03-12 stsp err(1, "pledge");
2865 6715a751 2018-03-16 stsp #endif
2866 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2867 c47340da 2020-09-20 stsp char *base, *dotgit;
2868 f0207f6a 2020-10-19 stsp const char *path;
2869 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2870 76089277 2018-04-01 stsp if (repo_path == NULL)
2871 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2872 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2873 76089277 2018-04-01 stsp if (cwd == NULL) {
2874 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2875 76089277 2018-04-01 stsp goto done;
2876 76089277 2018-04-01 stsp }
2877 c47340da 2020-09-20 stsp if (path_prefix[0])
2878 f0207f6a 2020-10-19 stsp path = path_prefix;
2879 c47340da 2020-09-20 stsp else
2880 f0207f6a 2020-10-19 stsp path = repo_path;
2881 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2882 f0207f6a 2020-10-19 stsp if (error)
2883 c47340da 2020-09-20 stsp goto done;
2884 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2885 c09a553d 2018-03-12 stsp if (dotgit)
2886 c09a553d 2018-03-12 stsp *dotgit = '\0';
2887 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2888 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2889 f0207f6a 2020-10-19 stsp free(base);
2890 76089277 2018-04-01 stsp goto done;
2891 c09a553d 2018-03-12 stsp }
2892 f0207f6a 2020-10-19 stsp free(base);
2893 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2894 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2895 76089277 2018-04-01 stsp if (repo_path == NULL) {
2896 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2897 76089277 2018-04-01 stsp goto done;
2898 76089277 2018-04-01 stsp }
2899 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2900 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2901 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2902 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2903 b4b3a7dd 2019-07-22 stsp argv[1]);
2904 b4b3a7dd 2019-07-22 stsp goto done;
2905 b4b3a7dd 2019-07-22 stsp }
2906 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2907 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2908 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2909 b4b3a7dd 2019-07-22 stsp goto done;
2910 b4b3a7dd 2019-07-22 stsp }
2911 76089277 2018-04-01 stsp }
2912 c09a553d 2018-03-12 stsp } else
2913 c09a553d 2018-03-12 stsp usage_checkout();
2914 c09a553d 2018-03-12 stsp
2915 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2916 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2917 13bfb272 2019-05-10 jcs
2918 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2919 c09a553d 2018-03-12 stsp if (error != NULL)
2920 c02c541e 2019-03-29 stsp goto done;
2921 c02c541e 2019-03-29 stsp
2922 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2923 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2924 c530dc23 2019-07-23 stsp if (error) {
2925 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2926 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2927 c530dc23 2019-07-23 stsp goto done;
2928 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2929 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2930 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2931 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2932 c530dc23 2019-07-23 stsp goto done;
2933 c530dc23 2019-07-23 stsp }
2934 c530dc23 2019-07-23 stsp }
2935 c530dc23 2019-07-23 stsp
2936 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2937 c02c541e 2019-03-29 stsp if (error)
2938 c09a553d 2018-03-12 stsp goto done;
2939 8069f636 2019-01-12 stsp
2940 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2941 c09a553d 2018-03-12 stsp if (error != NULL)
2942 c09a553d 2018-03-12 stsp goto done;
2943 c09a553d 2018-03-12 stsp
2944 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2945 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2946 c09a553d 2018-03-12 stsp goto done;
2947 c09a553d 2018-03-12 stsp
2948 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2949 c09a553d 2018-03-12 stsp if (error != NULL)
2950 c09a553d 2018-03-12 stsp goto done;
2951 c09a553d 2018-03-12 stsp
2952 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2953 e5dc7198 2018-12-29 stsp path_prefix);
2954 e5dc7198 2018-12-29 stsp if (error != NULL)
2955 e5dc7198 2018-12-29 stsp goto done;
2956 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2957 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2958 49520a32 2018-12-29 stsp goto done;
2959 49520a32 2018-12-29 stsp }
2960 49520a32 2018-12-29 stsp
2961 8069f636 2019-01-12 stsp if (commit_id_str) {
2962 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2963 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2964 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2965 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2966 84de9106 2020-12-26 stsp NULL);
2967 84de9106 2020-12-26 stsp if (error)
2968 84de9106 2020-12-26 stsp goto done;
2969 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2970 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2971 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2972 30837e32 2019-07-25 stsp if (error)
2973 8069f636 2019-01-12 stsp goto done;
2974 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2975 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2976 8069f636 2019-01-12 stsp if (error != NULL) {
2977 8069f636 2019-01-12 stsp free(commit_id);
2978 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2979 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2980 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2981 4b6c9460 2020-03-05 stsp }
2982 8069f636 2019-01-12 stsp goto done;
2983 8069f636 2019-01-12 stsp }
2984 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2985 4b6c9460 2020-03-05 stsp if (error) {
2986 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2987 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2988 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2989 4b6c9460 2020-03-05 stsp }
2990 45d344f6 2019-05-14 stsp goto done;
2991 4b6c9460 2020-03-05 stsp }
2992 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2993 8069f636 2019-01-12 stsp commit_id);
2994 8069f636 2019-01-12 stsp free(commit_id);
2995 8069f636 2019-01-12 stsp if (error)
2996 8069f636 2019-01-12 stsp goto done;
2997 8069f636 2019-01-12 stsp }
2998 8069f636 2019-01-12 stsp
2999 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
3000 f2ea84fa 2019-07-27 stsp if (error)
3001 f2ea84fa 2019-07-27 stsp goto done;
3002 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
3003 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
3004 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3005 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
3006 c09a553d 2018-03-12 stsp if (error != NULL)
3007 c09a553d 2018-03-12 stsp goto done;
3008 c09a553d 2018-03-12 stsp
3009 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
3010 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
3011 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
3012 c09a553d 2018-03-12 stsp done:
3013 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3014 8069f636 2019-01-12 stsp free(commit_id_str);
3015 76089277 2018-04-01 stsp free(repo_path);
3016 507dc3bb 2018-12-29 stsp free(worktree_path);
3017 c47340da 2020-09-20 stsp free(cwd);
3018 507dc3bb 2018-12-29 stsp return error;
3019 9627c110 2020-04-18 stsp }
3020 9627c110 2020-04-18 stsp
3021 9627c110 2020-04-18 stsp struct got_update_progress_arg {
3022 9627c110 2020-04-18 stsp int did_something;
3023 9627c110 2020-04-18 stsp int conflicts;
3024 9627c110 2020-04-18 stsp int obstructed;
3025 9627c110 2020-04-18 stsp int not_updated;
3026 9627c110 2020-04-18 stsp };
3027 9627c110 2020-04-18 stsp
3028 9627c110 2020-04-18 stsp void
3029 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3030 9627c110 2020-04-18 stsp {
3031 9627c110 2020-04-18 stsp if (!upa->did_something)
3032 9627c110 2020-04-18 stsp return;
3033 9627c110 2020-04-18 stsp
3034 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
3035 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3036 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
3037 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
3038 9627c110 2020-04-18 stsp upa->obstructed);
3039 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
3040 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
3041 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
3042 507dc3bb 2018-12-29 stsp }
3043 507dc3bb 2018-12-29 stsp
3044 507dc3bb 2018-12-29 stsp __dead static void
3045 507dc3bb 2018-12-29 stsp usage_update(void)
3046 507dc3bb 2018-12-29 stsp {
3047 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
3048 507dc3bb 2018-12-29 stsp getprogname());
3049 507dc3bb 2018-12-29 stsp exit(1);
3050 507dc3bb 2018-12-29 stsp }
3051 507dc3bb 2018-12-29 stsp
3052 1ee397ad 2019-07-12 stsp static const struct got_error *
3053 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
3054 507dc3bb 2018-12-29 stsp {
3055 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3056 784955db 2019-01-12 stsp
3057 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
3058 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
3059 1ee397ad 2019-07-12 stsp return NULL;
3060 507dc3bb 2018-12-29 stsp
3061 9627c110 2020-04-18 stsp upa->did_something = 1;
3062 a484d721 2019-06-10 stsp
3063 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
3064 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
3065 1ee397ad 2019-07-12 stsp return NULL;
3066 a484d721 2019-06-10 stsp
3067 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
3068 9627c110 2020-04-18 stsp upa->conflicts++;
3069 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
3070 9627c110 2020-04-18 stsp upa->obstructed++;
3071 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
3072 9627c110 2020-04-18 stsp upa->not_updated++;
3073 9627c110 2020-04-18 stsp
3074 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3075 507dc3bb 2018-12-29 stsp path++;
3076 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
3077 1ee397ad 2019-07-12 stsp return NULL;
3078 be7061eb 2018-12-30 stsp }
3079 be7061eb 2018-12-30 stsp
3080 be7061eb 2018-12-30 stsp static const struct got_error *
3081 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
3082 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3083 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3084 a1fb16d8 2019-05-24 stsp {
3085 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3086 a1fb16d8 2019-05-24 stsp char *base_id_str;
3087 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3088 a1fb16d8 2019-05-24 stsp
3089 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3090 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3091 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3092 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3093 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3094 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3095 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3096 a1fb16d8 2019-05-24 stsp }
3097 a1fb16d8 2019-05-24 stsp
3098 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3099 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3100 a1fb16d8 2019-05-24 stsp if (err) {
3101 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3102 a1fb16d8 2019-05-24 stsp return err;
3103 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3104 a1fb16d8 2019-05-24 stsp }
3105 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3106 a1fb16d8 2019-05-24 stsp return NULL;
3107 a1fb16d8 2019-05-24 stsp
3108 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3109 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3110 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3111 a1fb16d8 2019-05-24 stsp if (err)
3112 a1fb16d8 2019-05-24 stsp return err;
3113 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3114 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3115 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3116 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3117 0ebf8283 2019-07-24 stsp return NULL;
3118 0ebf8283 2019-07-24 stsp }
3119 0ebf8283 2019-07-24 stsp
3120 0ebf8283 2019-07-24 stsp static const struct got_error *
3121 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3122 0ebf8283 2019-07-24 stsp {
3123 0ebf8283 2019-07-24 stsp const struct got_error *err;
3124 0ebf8283 2019-07-24 stsp int in_progress;
3125 0ebf8283 2019-07-24 stsp
3126 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3127 0ebf8283 2019-07-24 stsp if (err)
3128 0ebf8283 2019-07-24 stsp return err;
3129 0ebf8283 2019-07-24 stsp if (in_progress)
3130 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3131 0ebf8283 2019-07-24 stsp
3132 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3133 0ebf8283 2019-07-24 stsp if (err)
3134 0ebf8283 2019-07-24 stsp return err;
3135 0ebf8283 2019-07-24 stsp if (in_progress)
3136 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3137 0ebf8283 2019-07-24 stsp
3138 a1fb16d8 2019-05-24 stsp return NULL;
3139 a5edda0a 2019-07-27 stsp }
3140 a5edda0a 2019-07-27 stsp
3141 a5edda0a 2019-07-27 stsp static const struct got_error *
3142 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3143 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3144 a5edda0a 2019-07-27 stsp {
3145 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3146 a5edda0a 2019-07-27 stsp char *path;
3147 a5edda0a 2019-07-27 stsp int i;
3148 a5edda0a 2019-07-27 stsp
3149 a5edda0a 2019-07-27 stsp if (argc == 0) {
3150 a5edda0a 2019-07-27 stsp path = strdup("");
3151 a5edda0a 2019-07-27 stsp if (path == NULL)
3152 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3153 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3154 a5edda0a 2019-07-27 stsp }
3155 a5edda0a 2019-07-27 stsp
3156 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3157 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3158 a5edda0a 2019-07-27 stsp if (err)
3159 a5edda0a 2019-07-27 stsp break;
3160 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3161 a5edda0a 2019-07-27 stsp if (err) {
3162 a5edda0a 2019-07-27 stsp free(path);
3163 a5edda0a 2019-07-27 stsp break;
3164 a5edda0a 2019-07-27 stsp }
3165 a5edda0a 2019-07-27 stsp }
3166 a5edda0a 2019-07-27 stsp
3167 a5edda0a 2019-07-27 stsp return err;
3168 a1fb16d8 2019-05-24 stsp }
3169 a1fb16d8 2019-05-24 stsp
3170 a1fb16d8 2019-05-24 stsp static const struct got_error *
3171 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3172 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3173 fa51e947 2020-03-27 stsp {
3174 fa51e947 2020-03-27 stsp const struct got_error *err;
3175 fa51e947 2020-03-27 stsp struct got_repository *repo;
3176 fa51e947 2020-03-27 stsp static char msg[512];
3177 fa51e947 2020-03-27 stsp
3178 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3179 fa51e947 2020-03-27 stsp if (err)
3180 fa51e947 2020-03-27 stsp return orig_err;
3181 fa51e947 2020-03-27 stsp
3182 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3183 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3184 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3185 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3186 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3187 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3188 fa51e947 2020-03-27 stsp got_repo_close(repo);
3189 fa51e947 2020-03-27 stsp return err;
3190 fa51e947 2020-03-27 stsp }
3191 fa51e947 2020-03-27 stsp
3192 fa51e947 2020-03-27 stsp static const struct got_error *
3193 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3194 507dc3bb 2018-12-29 stsp {
3195 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3196 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3197 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3198 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3199 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3200 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3201 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3202 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3203 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3204 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3205 9627c110 2020-04-18 stsp int ch;
3206 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3207 507dc3bb 2018-12-29 stsp
3208 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3209 f2ea84fa 2019-07-27 stsp
3210 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3211 507dc3bb 2018-12-29 stsp switch (ch) {
3212 024e9686 2019-05-14 stsp case 'b':
3213 024e9686 2019-05-14 stsp branch_name = optarg;
3214 024e9686 2019-05-14 stsp break;
3215 507dc3bb 2018-12-29 stsp case 'c':
3216 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3217 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3218 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3219 507dc3bb 2018-12-29 stsp break;
3220 507dc3bb 2018-12-29 stsp default:
3221 2deda0b9 2019-03-07 stsp usage_update();
3222 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3223 507dc3bb 2018-12-29 stsp }
3224 507dc3bb 2018-12-29 stsp }
3225 507dc3bb 2018-12-29 stsp
3226 507dc3bb 2018-12-29 stsp argc -= optind;
3227 507dc3bb 2018-12-29 stsp argv += optind;
3228 507dc3bb 2018-12-29 stsp
3229 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3230 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3231 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3232 507dc3bb 2018-12-29 stsp err(1, "pledge");
3233 507dc3bb 2018-12-29 stsp #endif
3234 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3235 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3236 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3237 c4cdcb68 2019-04-03 stsp goto done;
3238 c4cdcb68 2019-04-03 stsp }
3239 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3240 fa51e947 2020-03-27 stsp if (error) {
3241 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3242 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3243 fa51e947 2020-03-27 stsp worktree_path);
3244 7d5807f4 2019-07-11 stsp goto done;
3245 fa51e947 2020-03-27 stsp }
3246 7d5807f4 2019-07-11 stsp
3247 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3248 f2ea84fa 2019-07-27 stsp if (error)
3249 f2ea84fa 2019-07-27 stsp goto done;
3250 507dc3bb 2018-12-29 stsp
3251 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3252 c9956ddf 2019-09-08 stsp NULL);
3253 507dc3bb 2018-12-29 stsp if (error != NULL)
3254 507dc3bb 2018-12-29 stsp goto done;
3255 507dc3bb 2018-12-29 stsp
3256 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3257 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3258 087fb88c 2019-08-04 stsp if (error)
3259 087fb88c 2019-08-04 stsp goto done;
3260 087fb88c 2019-08-04 stsp
3261 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3262 0266afb7 2019-01-04 stsp if (error)
3263 0266afb7 2019-01-04 stsp goto done;
3264 0266afb7 2019-01-04 stsp
3265 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3266 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3267 024e9686 2019-05-14 stsp if (error != NULL)
3268 024e9686 2019-05-14 stsp goto done;
3269 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3270 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3271 9c4b8182 2019-01-02 stsp if (error != NULL)
3272 9c4b8182 2019-01-02 stsp goto done;
3273 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3274 507dc3bb 2018-12-29 stsp if (error != NULL)
3275 507dc3bb 2018-12-29 stsp goto done;
3276 507dc3bb 2018-12-29 stsp } else {
3277 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3278 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3279 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3280 84de9106 2020-12-26 stsp NULL);
3281 84de9106 2020-12-26 stsp if (error)
3282 84de9106 2020-12-26 stsp goto done;
3283 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3284 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3285 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3286 04f57cb3 2019-07-25 stsp free(commit_id_str);
3287 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3288 30837e32 2019-07-25 stsp if (error)
3289 a297e751 2019-07-11 stsp goto done;
3290 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3291 a297e751 2019-07-11 stsp if (error)
3292 507dc3bb 2018-12-29 stsp goto done;
3293 507dc3bb 2018-12-29 stsp }
3294 35c965b2 2018-12-29 stsp
3295 a1fb16d8 2019-05-24 stsp if (branch_name) {
3296 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3297 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3298 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3299 f2ea84fa 2019-07-27 stsp continue;
3300 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3301 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3302 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3303 024e9686 2019-05-14 stsp goto done;
3304 024e9686 2019-05-14 stsp }
3305 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3306 024e9686 2019-05-14 stsp if (error)
3307 024e9686 2019-05-14 stsp goto done;
3308 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3309 3aef623b 2019-10-15 stsp repo);
3310 a367ff0f 2019-05-14 stsp free(head_commit_id);
3311 024e9686 2019-05-14 stsp if (error != NULL)
3312 024e9686 2019-05-14 stsp goto done;
3313 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3314 a367ff0f 2019-05-14 stsp if (error)
3315 a367ff0f 2019-05-14 stsp goto done;
3316 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3317 024e9686 2019-05-14 stsp if (error)
3318 024e9686 2019-05-14 stsp goto done;
3319 024e9686 2019-05-14 stsp } else {
3320 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3321 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3322 a367ff0f 2019-05-14 stsp if (error != NULL) {
3323 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3324 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3325 024e9686 2019-05-14 stsp goto done;
3326 a367ff0f 2019-05-14 stsp }
3327 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3328 a367ff0f 2019-05-14 stsp if (error)
3329 a367ff0f 2019-05-14 stsp goto done;
3330 024e9686 2019-05-14 stsp }
3331 507dc3bb 2018-12-29 stsp
3332 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3333 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3334 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3335 507dc3bb 2018-12-29 stsp commit_id);
3336 507dc3bb 2018-12-29 stsp if (error)
3337 507dc3bb 2018-12-29 stsp goto done;
3338 507dc3bb 2018-12-29 stsp }
3339 507dc3bb 2018-12-29 stsp
3340 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3341 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3342 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3343 507dc3bb 2018-12-29 stsp if (error != NULL)
3344 507dc3bb 2018-12-29 stsp goto done;
3345 9c4b8182 2019-01-02 stsp
3346 9627c110 2020-04-18 stsp if (upa.did_something)
3347 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3348 784955db 2019-01-12 stsp else
3349 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3350 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3351 507dc3bb 2018-12-29 stsp done:
3352 c09a553d 2018-03-12 stsp free(worktree_path);
3353 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3354 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3355 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3356 507dc3bb 2018-12-29 stsp free(commit_id);
3357 9c4b8182 2019-01-02 stsp free(commit_id_str);
3358 c09a553d 2018-03-12 stsp return error;
3359 c09a553d 2018-03-12 stsp }
3360 c09a553d 2018-03-12 stsp
3361 f42b1b34 2018-03-12 stsp static const struct got_error *
3362 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3363 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3364 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3365 5c860e29 2018-03-12 stsp {
3366 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3367 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3368 79109fed 2018-03-27 stsp
3369 44392932 2019-08-25 stsp if (blob_id1) {
3370 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3371 44392932 2019-08-25 stsp if (err)
3372 44392932 2019-08-25 stsp goto done;
3373 44392932 2019-08-25 stsp }
3374 79109fed 2018-03-27 stsp
3375 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3376 44392932 2019-08-25 stsp if (err)
3377 44392932 2019-08-25 stsp goto done;
3378 79109fed 2018-03-27 stsp
3379 44392932 2019-08-25 stsp while (path[0] == '/')
3380 44392932 2019-08-25 stsp path++;
3381 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3382 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3383 44392932 2019-08-25 stsp done:
3384 44392932 2019-08-25 stsp if (blob1)
3385 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3386 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3387 44392932 2019-08-25 stsp return err;
3388 44392932 2019-08-25 stsp }
3389 44392932 2019-08-25 stsp
3390 44392932 2019-08-25 stsp static const struct got_error *
3391 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3392 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3393 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3394 44392932 2019-08-25 stsp {
3395 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3396 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3397 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3398 44392932 2019-08-25 stsp
3399 44392932 2019-08-25 stsp if (tree_id1) {
3400 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3401 79109fed 2018-03-27 stsp if (err)
3402 44392932 2019-08-25 stsp goto done;
3403 79109fed 2018-03-27 stsp }
3404 79109fed 2018-03-27 stsp
3405 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3406 0f2b3dca 2018-12-22 stsp if (err)
3407 0f2b3dca 2018-12-22 stsp goto done;
3408 0f2b3dca 2018-12-22 stsp
3409 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3410 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3411 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3412 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3413 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3414 fe621944 2020-11-10 stsp arg.nlines = 0;
3415 44392932 2019-08-25 stsp while (path[0] == '/')
3416 44392932 2019-08-25 stsp path++;
3417 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3418 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3419 0f2b3dca 2018-12-22 stsp done:
3420 79109fed 2018-03-27 stsp if (tree1)
3421 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3422 366e0a5f 2019-10-10 stsp if (tree2)
3423 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3424 44392932 2019-08-25 stsp return err;
3425 44392932 2019-08-25 stsp }
3426 44392932 2019-08-25 stsp
3427 44392932 2019-08-25 stsp static const struct got_error *
3428 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3429 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3430 0208f208 2020-05-05 stsp {
3431 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3432 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3433 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3434 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3435 0208f208 2020-05-05 stsp
3436 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3437 0208f208 2020-05-05 stsp if (qid != NULL) {
3438 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3439 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3440 0208f208 2020-05-05 stsp qid->id);
3441 0208f208 2020-05-05 stsp if (err)
3442 0208f208 2020-05-05 stsp return err;
3443 0208f208 2020-05-05 stsp
3444 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3445 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3446 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3447 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3448 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3449 aa8b5dd0 2021-08-01 stsp }
3450 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3451 0208f208 2020-05-05 stsp
3452 0208f208 2020-05-05 stsp }
3453 0208f208 2020-05-05 stsp
3454 0208f208 2020-05-05 stsp if (tree_id1) {
3455 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3456 0208f208 2020-05-05 stsp if (err)
3457 0208f208 2020-05-05 stsp goto done;
3458 0208f208 2020-05-05 stsp }
3459 0208f208 2020-05-05 stsp
3460 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3461 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3462 0208f208 2020-05-05 stsp if (err)
3463 0208f208 2020-05-05 stsp goto done;
3464 0208f208 2020-05-05 stsp
3465 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3466 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3467 0208f208 2020-05-05 stsp done:
3468 0208f208 2020-05-05 stsp if (tree1)
3469 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3470 0208f208 2020-05-05 stsp if (tree2)
3471 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3472 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3473 0208f208 2020-05-05 stsp return err;
3474 0208f208 2020-05-05 stsp }
3475 0208f208 2020-05-05 stsp
3476 0208f208 2020-05-05 stsp static const struct got_error *
3477 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3478 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3479 44392932 2019-08-25 stsp {
3480 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3481 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3482 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3483 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3484 44392932 2019-08-25 stsp struct got_object_qid *qid;
3485 44392932 2019-08-25 stsp
3486 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3487 44392932 2019-08-25 stsp if (qid != NULL) {
3488 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3489 44392932 2019-08-25 stsp qid->id);
3490 44392932 2019-08-25 stsp if (err)
3491 44392932 2019-08-25 stsp return err;
3492 44392932 2019-08-25 stsp }
3493 44392932 2019-08-25 stsp
3494 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3495 44392932 2019-08-25 stsp int obj_type;
3496 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3497 44392932 2019-08-25 stsp if (err)
3498 44392932 2019-08-25 stsp goto done;
3499 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3500 44392932 2019-08-25 stsp if (err) {
3501 44392932 2019-08-25 stsp free(obj_id2);
3502 44392932 2019-08-25 stsp goto done;
3503 44392932 2019-08-25 stsp }
3504 44392932 2019-08-25 stsp if (pcommit) {
3505 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3506 44392932 2019-08-25 stsp qid->id, path);
3507 44392932 2019-08-25 stsp if (err) {
3508 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3509 2e8c69d1 2020-05-04 stsp free(obj_id2);
3510 2e8c69d1 2020-05-04 stsp goto done;
3511 2e8c69d1 2020-05-04 stsp }
3512 2e8c69d1 2020-05-04 stsp } else {
3513 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3514 2e8c69d1 2020-05-04 stsp if (err) {
3515 2e8c69d1 2020-05-04 stsp free(obj_id2);
3516 2e8c69d1 2020-05-04 stsp goto done;
3517 2e8c69d1 2020-05-04 stsp }
3518 44392932 2019-08-25 stsp }
3519 44392932 2019-08-25 stsp }
3520 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3521 44392932 2019-08-25 stsp if (err) {
3522 44392932 2019-08-25 stsp free(obj_id2);
3523 44392932 2019-08-25 stsp goto done;
3524 44392932 2019-08-25 stsp }
3525 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3526 44392932 2019-08-25 stsp switch (obj_type) {
3527 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3528 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3529 64453f7e 2020-11-21 stsp 0, 0, repo);
3530 44392932 2019-08-25 stsp break;
3531 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3532 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3533 64453f7e 2020-11-21 stsp 0, 0, repo);
3534 44392932 2019-08-25 stsp break;
3535 44392932 2019-08-25 stsp default:
3536 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3537 44392932 2019-08-25 stsp break;
3538 44392932 2019-08-25 stsp }
3539 44392932 2019-08-25 stsp free(obj_id1);
3540 44392932 2019-08-25 stsp free(obj_id2);
3541 44392932 2019-08-25 stsp } else {
3542 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3543 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3544 44392932 2019-08-25 stsp if (err)
3545 44392932 2019-08-25 stsp goto done;
3546 579bd556 2020-10-24 stsp if (pcommit) {
3547 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3548 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3549 579bd556 2020-10-24 stsp if (err)
3550 579bd556 2020-10-24 stsp goto done;
3551 579bd556 2020-10-24 stsp }
3552 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3553 64453f7e 2020-11-21 stsp id_str2);
3554 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3555 64453f7e 2020-11-21 stsp repo);
3556 44392932 2019-08-25 stsp }
3557 44392932 2019-08-25 stsp done:
3558 0f2b3dca 2018-12-22 stsp free(id_str1);
3559 0f2b3dca 2018-12-22 stsp free(id_str2);
3560 44392932 2019-08-25 stsp if (pcommit)
3561 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3562 79109fed 2018-03-27 stsp return err;
3563 79109fed 2018-03-27 stsp }
3564 79109fed 2018-03-27 stsp
3565 4bb494d5 2018-06-16 stsp static char *
3566 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3567 6c281f94 2018-06-11 stsp {
3568 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3569 09867e48 2019-08-13 stsp char *p, *s;
3570 09867e48 2019-08-13 stsp
3571 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3572 09867e48 2019-08-13 stsp if (tm == NULL)
3573 09867e48 2019-08-13 stsp return NULL;
3574 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3575 09867e48 2019-08-13 stsp if (s == NULL)
3576 09867e48 2019-08-13 stsp return NULL;
3577 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3578 6c281f94 2018-06-11 stsp if (p)
3579 6c281f94 2018-06-11 stsp *p = '\0';
3580 6c281f94 2018-06-11 stsp return s;
3581 6c281f94 2018-06-11 stsp }
3582 dc424a06 2019-08-07 stsp
3583 6841bf13 2019-11-29 kn static const struct got_error *
3584 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3585 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3586 6841bf13 2019-11-29 kn {
3587 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3588 6841bf13 2019-11-29 kn regmatch_t regmatch;
3589 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3590 6841bf13 2019-11-29 kn
3591 6841bf13 2019-11-29 kn *have_match = 0;
3592 6841bf13 2019-11-29 kn
3593 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3594 6841bf13 2019-11-29 kn if (err)
3595 6841bf13 2019-11-29 kn return err;
3596 6841bf13 2019-11-29 kn
3597 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3598 6841bf13 2019-11-29 kn if (err)
3599 6841bf13 2019-11-29 kn goto done;
3600 6841bf13 2019-11-29 kn
3601 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3602 6841bf13 2019-11-29 kn *have_match = 1;
3603 6841bf13 2019-11-29 kn done:
3604 6841bf13 2019-11-29 kn free(id_str);
3605 6841bf13 2019-11-29 kn free(logmsg);
3606 6841bf13 2019-11-29 kn return err;
3607 6841bf13 2019-11-29 kn }
3608 6841bf13 2019-11-29 kn
3609 0208f208 2020-05-05 stsp static void
3610 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3611 0208f208 2020-05-05 stsp regex_t *regex)
3612 0208f208 2020-05-05 stsp {
3613 0208f208 2020-05-05 stsp regmatch_t regmatch;
3614 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3615 0208f208 2020-05-05 stsp
3616 0208f208 2020-05-05 stsp *have_match = 0;
3617 0208f208 2020-05-05 stsp
3618 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3619 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3620 0208f208 2020-05-05 stsp *have_match = 1;
3621 0208f208 2020-05-05 stsp break;
3622 0208f208 2020-05-05 stsp }
3623 0208f208 2020-05-05 stsp }
3624 0208f208 2020-05-05 stsp }
3625 0208f208 2020-05-05 stsp
3626 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3627 6c281f94 2018-06-11 stsp
3628 888b7d99 2020-12-26 stsp static const struct got_error*
3629 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3630 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3631 888b7d99 2020-12-26 stsp {
3632 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3633 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3634 888b7d99 2020-12-26 stsp char *s;
3635 888b7d99 2020-12-26 stsp const char *name;
3636 5c860e29 2018-03-12 stsp
3637 888b7d99 2020-12-26 stsp *refs_str = NULL;
3638 888b7d99 2020-12-26 stsp
3639 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3640 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3641 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3642 a436ad14 2019-08-13 stsp int cmp;
3643 a436ad14 2019-08-13 stsp
3644 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3645 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3646 d9498b20 2019-02-05 stsp continue;
3647 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3648 199a4027 2019-02-02 stsp name += 5;
3649 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3650 7143d404 2019-03-12 stsp continue;
3651 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3652 e34f9ed6 2019-02-02 stsp name += 6;
3653 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3654 141c2bff 2019-02-04 stsp name += 8;
3655 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3656 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3657 4343a07f 2020-04-24 stsp continue;
3658 4343a07f 2020-04-24 stsp }
3659 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3660 48cae60d 2020-09-22 stsp if (err)
3661 888b7d99 2020-12-26 stsp break;
3662 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3663 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3664 5d844a1e 2019-08-13 stsp if (err) {
3665 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3666 48cae60d 2020-09-22 stsp free(ref_id);
3667 888b7d99 2020-12-26 stsp break;
3668 48cae60d 2020-09-22 stsp }
3669 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3670 5d844a1e 2019-08-13 stsp err = NULL;
3671 5d844a1e 2019-08-13 stsp tag = NULL;
3672 5d844a1e 2019-08-13 stsp }
3673 a436ad14 2019-08-13 stsp }
3674 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3675 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3676 48cae60d 2020-09-22 stsp free(ref_id);
3677 a436ad14 2019-08-13 stsp if (tag)
3678 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3679 a436ad14 2019-08-13 stsp if (cmp != 0)
3680 a436ad14 2019-08-13 stsp continue;
3681 888b7d99 2020-12-26 stsp s = *refs_str;
3682 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3683 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3684 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3685 199a4027 2019-02-02 stsp free(s);
3686 888b7d99 2020-12-26 stsp *refs_str = NULL;
3687 888b7d99 2020-12-26 stsp break;
3688 199a4027 2019-02-02 stsp }
3689 199a4027 2019-02-02 stsp free(s);
3690 199a4027 2019-02-02 stsp }
3691 888b7d99 2020-12-26 stsp
3692 888b7d99 2020-12-26 stsp return err;
3693 888b7d99 2020-12-26 stsp }
3694 888b7d99 2020-12-26 stsp
3695 888b7d99 2020-12-26 stsp static const struct got_error *
3696 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3697 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3698 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3699 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3700 e600f124 2021-03-21 stsp const char *custom_refs_str)
3701 888b7d99 2020-12-26 stsp {
3702 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3703 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3704 888b7d99 2020-12-26 stsp char datebuf[26];
3705 888b7d99 2020-12-26 stsp time_t committer_time;
3706 888b7d99 2020-12-26 stsp const char *author, *committer;
3707 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3708 888b7d99 2020-12-26 stsp
3709 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3710 8bf5b3c9 2018-03-17 stsp if (err)
3711 8bf5b3c9 2018-03-17 stsp return err;
3712 788c352e 2018-06-16 stsp
3713 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3714 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3715 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3716 e600f124 2021-03-21 stsp if (refs) {
3717 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3718 e600f124 2021-03-21 stsp if (err)
3719 e600f124 2021-03-21 stsp goto done;
3720 e600f124 2021-03-21 stsp }
3721 888b7d99 2020-12-26 stsp }
3722 888b7d99 2020-12-26 stsp
3723 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3724 e600f124 2021-03-21 stsp if (custom_refs_str)
3725 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3726 e600f124 2021-03-21 stsp else
3727 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3728 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3729 832c249c 2018-06-10 stsp free(id_str);
3730 d3d493d7 2019-02-21 stsp id_str = NULL;
3731 d3d493d7 2019-02-21 stsp free(refs_str);
3732 d3d493d7 2019-02-21 stsp refs_str = NULL;
3733 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3734 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3735 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3736 09867e48 2019-08-13 stsp if (datestr)
3737 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3738 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3739 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3740 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3741 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3742 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3743 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3744 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3745 3fe1abad 2018-06-10 stsp int n = 1;
3746 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3747 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
3748 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3749 a0603db2 2018-06-10 stsp if (err)
3750 888b7d99 2020-12-26 stsp goto done;
3751 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3752 a0603db2 2018-06-10 stsp free(id_str);
3753 888b7d99 2020-12-26 stsp id_str = NULL;
3754 a0603db2 2018-06-10 stsp }
3755 a0603db2 2018-06-10 stsp }
3756 832c249c 2018-06-10 stsp
3757 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3758 5943eee2 2019-08-13 stsp if (err)
3759 888b7d99 2020-12-26 stsp goto done;
3760 8bf5b3c9 2018-03-17 stsp
3761 621015ac 2018-07-23 stsp logmsg = logmsg0;
3762 832c249c 2018-06-10 stsp do {
3763 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3764 832c249c 2018-06-10 stsp if (line)
3765 832c249c 2018-06-10 stsp printf(" %s\n", line);
3766 832c249c 2018-06-10 stsp } while (line);
3767 621015ac 2018-07-23 stsp free(logmsg0);
3768 832c249c 2018-06-10 stsp
3769 0208f208 2020-05-05 stsp if (changed_paths) {
3770 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3771 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3772 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3773 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3774 0208f208 2020-05-05 stsp }
3775 0208f208 2020-05-05 stsp printf("\n");
3776 0208f208 2020-05-05 stsp }
3777 971751ac 2018-03-27 stsp if (show_patch) {
3778 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3779 971751ac 2018-03-27 stsp if (err == 0)
3780 971751ac 2018-03-27 stsp printf("\n");
3781 971751ac 2018-03-27 stsp }
3782 07862c20 2018-09-15 stsp
3783 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3784 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3785 888b7d99 2020-12-26 stsp done:
3786 888b7d99 2020-12-26 stsp free(id_str);
3787 888b7d99 2020-12-26 stsp free(refs_str);
3788 07862c20 2018-09-15 stsp return err;
3789 f42b1b34 2018-03-12 stsp }
3790 5c860e29 2018-03-12 stsp
3791 f42b1b34 2018-03-12 stsp static const struct got_error *
3792 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3793 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3794 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3795 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3796 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3797 f42b1b34 2018-03-12 stsp {
3798 f42b1b34 2018-03-12 stsp const struct got_error *err;
3799 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3800 6841bf13 2019-11-29 kn regex_t regex;
3801 6841bf13 2019-11-29 kn int have_match;
3802 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3803 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3804 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3805 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3806 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3807 dbec59df 2020-04-18 stsp
3808 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3809 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3810 372ccdbb 2018-06-10 stsp
3811 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3812 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3813 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3814 6841bf13 2019-11-29 kn
3815 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3816 f42b1b34 2018-03-12 stsp if (err)
3817 f42b1b34 2018-03-12 stsp return err;
3818 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3819 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3820 372ccdbb 2018-06-10 stsp if (err)
3821 fcc85cad 2018-07-22 stsp goto done;
3822 656b1f76 2019-05-11 jcs for (;;) {
3823 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3824 8bf5b3c9 2018-03-17 stsp
3825 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3826 84453469 2018-11-11 stsp break;
3827 84453469 2018-11-11 stsp
3828 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3829 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3830 372ccdbb 2018-06-10 stsp if (err) {
3831 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3832 9ba79e04 2018-06-11 stsp err = NULL;
3833 ee780d5c 2020-01-04 stsp break;
3834 7e665116 2018-04-02 stsp }
3835 b43fbaa0 2018-06-11 stsp if (id == NULL)
3836 7e665116 2018-04-02 stsp break;
3837 b43fbaa0 2018-06-11 stsp
3838 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3839 b43fbaa0 2018-06-11 stsp if (err)
3840 fcc85cad 2018-07-22 stsp break;
3841 6841bf13 2019-11-29 kn
3842 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3843 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3844 0208f208 2020-05-05 stsp if (err)
3845 0208f208 2020-05-05 stsp break;
3846 0208f208 2020-05-05 stsp }
3847 0208f208 2020-05-05 stsp
3848 6841bf13 2019-11-29 kn if (search_pattern) {
3849 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3850 6841bf13 2019-11-29 kn if (err) {
3851 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3852 6841bf13 2019-11-29 kn break;
3853 6841bf13 2019-11-29 kn }
3854 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3855 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3856 0208f208 2020-05-05 stsp &changed_paths, &regex);
3857 6841bf13 2019-11-29 kn if (have_match == 0) {
3858 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3859 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3860 0208f208 2020-05-05 stsp free((char *)pe->path);
3861 0208f208 2020-05-05 stsp free(pe->data);
3862 0208f208 2020-05-05 stsp }
3863 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3864 6841bf13 2019-11-29 kn continue;
3865 6841bf13 2019-11-29 kn }
3866 6841bf13 2019-11-29 kn }
3867 6841bf13 2019-11-29 kn
3868 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3869 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3870 dbec59df 2020-04-18 stsp if (err)
3871 dbec59df 2020-04-18 stsp break;
3872 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3873 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3874 dbec59df 2020-04-18 stsp } else {
3875 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3876 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3877 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3878 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3879 dbec59df 2020-04-18 stsp if (err)
3880 dbec59df 2020-04-18 stsp break;
3881 dbec59df 2020-04-18 stsp }
3882 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3883 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3884 7e665116 2018-04-02 stsp break;
3885 0208f208 2020-05-05 stsp
3886 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3887 0208f208 2020-05-05 stsp free((char *)pe->path);
3888 0208f208 2020-05-05 stsp free(pe->data);
3889 0208f208 2020-05-05 stsp }
3890 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3891 31cedeaf 2018-09-15 stsp }
3892 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3893 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
3894 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3895 dbec59df 2020-04-18 stsp if (err)
3896 dbec59df 2020-04-18 stsp break;
3897 502b9684 2020-07-31 stsp if (show_changed_paths) {
3898 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3899 502b9684 2020-07-31 stsp commit, repo);
3900 502b9684 2020-07-31 stsp if (err)
3901 502b9684 2020-07-31 stsp break;
3902 502b9684 2020-07-31 stsp }
3903 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3904 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3905 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3906 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3907 dbec59df 2020-04-18 stsp if (err)
3908 dbec59df 2020-04-18 stsp break;
3909 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3910 502b9684 2020-07-31 stsp free((char *)pe->path);
3911 502b9684 2020-07-31 stsp free(pe->data);
3912 502b9684 2020-07-31 stsp }
3913 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3914 dbec59df 2020-04-18 stsp }
3915 dbec59df 2020-04-18 stsp }
3916 fcc85cad 2018-07-22 stsp done:
3917 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
3918 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
3919 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3920 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3921 dbec59df 2020-04-18 stsp }
3922 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3923 0208f208 2020-05-05 stsp free((char *)pe->path);
3924 0208f208 2020-05-05 stsp free(pe->data);
3925 0208f208 2020-05-05 stsp }
3926 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3927 6841bf13 2019-11-29 kn if (search_pattern)
3928 6841bf13 2019-11-29 kn regfree(&regex);
3929 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3930 f42b1b34 2018-03-12 stsp return err;
3931 f42b1b34 2018-03-12 stsp }
3932 5c860e29 2018-03-12 stsp
3933 4ed7e80c 2018-05-20 stsp __dead static void
3934 6f3d1eb0 2018-03-12 stsp usage_log(void)
3935 6f3d1eb0 2018-03-12 stsp {
3936 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3937 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3938 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3939 6f3d1eb0 2018-03-12 stsp exit(1);
3940 b1ebc001 2019-08-13 stsp }
3941 b1ebc001 2019-08-13 stsp
3942 b1ebc001 2019-08-13 stsp static int
3943 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3944 b1ebc001 2019-08-13 stsp {
3945 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3946 b1ebc001 2019-08-13 stsp long long n;
3947 b1ebc001 2019-08-13 stsp const char *errstr;
3948 b1ebc001 2019-08-13 stsp
3949 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3950 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3951 b1ebc001 2019-08-13 stsp return 0;
3952 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3953 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3954 b1ebc001 2019-08-13 stsp return 0;
3955 b1ebc001 2019-08-13 stsp return n;
3956 6f3d1eb0 2018-03-12 stsp }
3957 6f3d1eb0 2018-03-12 stsp
3958 4ed7e80c 2018-05-20 stsp static const struct got_error *
3959 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3960 f42b1b34 2018-03-12 stsp {
3961 f42b1b34 2018-03-12 stsp const struct got_error *error;
3962 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3963 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3964 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3965 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3966 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3967 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3968 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3969 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3970 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3971 64a96a6d 2018-04-01 stsp const char *errstr;
3972 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3973 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
3974 1b3893a2 2019-03-18 stsp
3975 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3976 5c860e29 2018-03-12 stsp
3977 6715a751 2018-03-16 stsp #ifndef PROFILE
3978 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3979 6098196c 2019-01-04 stsp NULL)
3980 ad242220 2018-09-08 stsp == -1)
3981 f42b1b34 2018-03-12 stsp err(1, "pledge");
3982 6715a751 2018-03-16 stsp #endif
3983 79109fed 2018-03-27 stsp
3984 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3985 b1ebc001 2019-08-13 stsp
3986 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3987 79109fed 2018-03-27 stsp switch (ch) {
3988 79109fed 2018-03-27 stsp case 'p':
3989 79109fed 2018-03-27 stsp show_patch = 1;
3990 d142fc45 2018-04-01 stsp break;
3991 0208f208 2020-05-05 stsp case 'P':
3992 0208f208 2020-05-05 stsp show_changed_paths = 1;
3993 0208f208 2020-05-05 stsp break;
3994 d142fc45 2018-04-01 stsp case 'c':
3995 d142fc45 2018-04-01 stsp start_commit = optarg;
3996 64a96a6d 2018-04-01 stsp break;
3997 c0cc5c62 2018-10-18 stsp case 'C':
3998 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3999 4a8520aa 2018-10-18 stsp &errstr);
4000 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4001 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4002 c0cc5c62 2018-10-18 stsp break;
4003 64a96a6d 2018-04-01 stsp case 'l':
4004 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
4005 64a96a6d 2018-04-01 stsp if (errstr != NULL)
4006 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
4007 cc54c501 2019-07-15 stsp break;
4008 48c8c60d 2020-01-27 stsp case 'b':
4009 48c8c60d 2020-01-27 stsp log_branches = 1;
4010 a0603db2 2018-06-10 stsp break;
4011 04ca23f4 2018-07-16 stsp case 'r':
4012 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4013 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
4014 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4015 9ba1d308 2019-10-21 stsp optarg);
4016 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4017 04ca23f4 2018-07-16 stsp break;
4018 dbec59df 2020-04-18 stsp case 'R':
4019 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4020 dbec59df 2020-04-18 stsp break;
4021 6841bf13 2019-11-29 kn case 's':
4022 6841bf13 2019-11-29 kn search_pattern = optarg;
4023 6841bf13 2019-11-29 kn break;
4024 d1fe46f9 2020-04-18 stsp case 'x':
4025 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4026 d1fe46f9 2020-04-18 stsp break;
4027 79109fed 2018-03-27 stsp default:
4028 2deda0b9 2019-03-07 stsp usage_log();
4029 79109fed 2018-03-27 stsp /* NOTREACHED */
4030 79109fed 2018-03-27 stsp }
4031 79109fed 2018-03-27 stsp }
4032 79109fed 2018-03-27 stsp
4033 79109fed 2018-03-27 stsp argc -= optind;
4034 79109fed 2018-03-27 stsp argv += optind;
4035 f42b1b34 2018-03-12 stsp
4036 dc1edbfa 2019-11-29 kn if (diff_context == -1)
4037 dc1edbfa 2019-11-29 kn diff_context = 3;
4038 dc1edbfa 2019-11-29 kn else if (!show_patch)
4039 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
4040 dc1edbfa 2019-11-29 kn
4041 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
4042 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
4043 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4044 04ca23f4 2018-07-16 stsp goto done;
4045 04ca23f4 2018-07-16 stsp }
4046 cffc0aa4 2019-02-05 stsp
4047 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
4048 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
4049 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4050 50f2fada 2020-04-24 stsp goto done;
4051 50f2fada 2020-04-24 stsp error = NULL;
4052 50f2fada 2020-04-24 stsp }
4053 cffc0aa4 2019-02-05 stsp
4054 603cdeb0 2020-10-22 stsp if (argc == 1) {
4055 e7301579 2019-03-18 stsp if (worktree) {
4056 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4057 e7301579 2019-03-18 stsp argv[0]);
4058 e7301579 2019-03-18 stsp if (error)
4059 e7301579 2019-03-18 stsp goto done;
4060 e7301579 2019-03-18 stsp } else {
4061 e7301579 2019-03-18 stsp path = strdup(argv[0]);
4062 e7301579 2019-03-18 stsp if (path == NULL) {
4063 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4064 e7301579 2019-03-18 stsp goto done;
4065 e7301579 2019-03-18 stsp }
4066 e7301579 2019-03-18 stsp }
4067 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4068 cbd1af7a 2019-03-18 stsp usage_log();
4069 cbd1af7a 2019-03-18 stsp
4070 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
4071 5486daa2 2019-05-11 stsp repo_path = worktree ?
4072 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4073 5486daa2 2019-05-11 stsp }
4074 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
4075 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4076 cffc0aa4 2019-02-05 stsp goto done;
4077 04ca23f4 2018-07-16 stsp }
4078 6098196c 2019-01-04 stsp
4079 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4080 d7d4f210 2018-03-12 stsp if (error != NULL)
4081 04ca23f4 2018-07-16 stsp goto done;
4082 f42b1b34 2018-03-12 stsp
4083 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4084 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4085 c02c541e 2019-03-29 stsp if (error)
4086 c02c541e 2019-03-29 stsp goto done;
4087 c02c541e 2019-03-29 stsp
4088 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4089 84de9106 2020-12-26 stsp if (error)
4090 84de9106 2020-12-26 stsp goto done;
4091 84de9106 2020-12-26 stsp
4092 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4093 888b7d99 2020-12-26 stsp if (error)
4094 888b7d99 2020-12-26 stsp goto done;
4095 888b7d99 2020-12-26 stsp
4096 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4097 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4098 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4099 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4100 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4101 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4102 3235492e 2018-04-01 stsp if (error != NULL)
4103 d1fe46f9 2020-04-18 stsp goto done;
4104 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4105 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4106 3235492e 2018-04-01 stsp if (error != NULL)
4107 d1fe46f9 2020-04-18 stsp goto done;
4108 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4109 d1fe46f9 2020-04-18 stsp start_id);
4110 d1fe46f9 2020-04-18 stsp if (error != NULL)
4111 d1fe46f9 2020-04-18 stsp goto done;
4112 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4113 3235492e 2018-04-01 stsp } else {
4114 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4115 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4116 d1fe46f9 2020-04-18 stsp if (error != NULL)
4117 d1fe46f9 2020-04-18 stsp goto done;
4118 3235492e 2018-04-01 stsp }
4119 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4120 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4121 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4122 d1fe46f9 2020-04-18 stsp if (error != NULL)
4123 d1fe46f9 2020-04-18 stsp goto done;
4124 d1fe46f9 2020-04-18 stsp }
4125 04ca23f4 2018-07-16 stsp
4126 deeabeae 2019-08-27 stsp if (worktree) {
4127 603cdeb0 2020-10-22 stsp /*
4128 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4129 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4130 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4131 603cdeb0 2020-10-22 stsp */
4132 603cdeb0 2020-10-22 stsp if (path) {
4133 603cdeb0 2020-10-22 stsp const char *prefix;
4134 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4135 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4136 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4137 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4138 603cdeb0 2020-10-22 stsp goto done;
4139 603cdeb0 2020-10-22 stsp }
4140 603cdeb0 2020-10-22 stsp }
4141 deeabeae 2019-08-27 stsp } else
4142 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4143 8fa913ec 2020-11-14 stsp path ? path : "");
4144 04ca23f4 2018-07-16 stsp if (error != NULL)
4145 04ca23f4 2018-07-16 stsp goto done;
4146 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4147 04ca23f4 2018-07-16 stsp free(path);
4148 04ca23f4 2018-07-16 stsp path = in_repo_path;
4149 04ca23f4 2018-07-16 stsp }
4150 199a4027 2019-02-02 stsp
4151 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4152 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4153 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4154 04ca23f4 2018-07-16 stsp done:
4155 04ca23f4 2018-07-16 stsp free(path);
4156 04ca23f4 2018-07-16 stsp free(repo_path);
4157 04ca23f4 2018-07-16 stsp free(cwd);
4158 cffc0aa4 2019-02-05 stsp if (worktree)
4159 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4160 ad242220 2018-09-08 stsp if (repo) {
4161 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4162 ad242220 2018-09-08 stsp if (error == NULL)
4163 1d0f4054 2021-06-17 stsp error = close_err;
4164 ad242220 2018-09-08 stsp }
4165 888b7d99 2020-12-26 stsp if (refs_idmap)
4166 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4167 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4168 8bf5b3c9 2018-03-17 stsp return error;
4169 5c860e29 2018-03-12 stsp }
4170 5c860e29 2018-03-12 stsp
4171 4ed7e80c 2018-05-20 stsp __dead static void
4172 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4173 3f8b7d6a 2018-04-01 stsp {
4174 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4175 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
4176 3f8b7d6a 2018-04-01 stsp exit(1);
4177 b00d56cd 2018-04-01 stsp }
4178 b00d56cd 2018-04-01 stsp
4179 b72f483a 2019-02-05 stsp struct print_diff_arg {
4180 b72f483a 2019-02-05 stsp struct got_repository *repo;
4181 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4182 b72f483a 2019-02-05 stsp int diff_context;
4183 3fc0c068 2019-02-10 stsp const char *id_str;
4184 3fc0c068 2019-02-10 stsp int header_shown;
4185 98eaaa12 2019-08-03 stsp int diff_staged;
4186 63035f9f 2019-10-06 stsp int ignore_whitespace;
4187 64453f7e 2020-11-21 stsp int force_text_diff;
4188 b72f483a 2019-02-05 stsp };
4189 39449a05 2020-07-23 stsp
4190 39449a05 2020-07-23 stsp /*
4191 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4192 39449a05 2020-07-23 stsp * it as content to the diff engine.
4193 39449a05 2020-07-23 stsp */
4194 39449a05 2020-07-23 stsp static const struct got_error *
4195 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4196 39449a05 2020-07-23 stsp const char *abspath)
4197 39449a05 2020-07-23 stsp {
4198 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4199 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4200 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4201 39449a05 2020-07-23 stsp
4202 39449a05 2020-07-23 stsp *fd = -1;
4203 39449a05 2020-07-23 stsp
4204 39449a05 2020-07-23 stsp if (dirfd != -1) {
4205 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4206 39449a05 2020-07-23 stsp if (target_len == -1)
4207 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4208 39449a05 2020-07-23 stsp } else {
4209 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4210 39449a05 2020-07-23 stsp if (target_len == -1)
4211 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4212 39449a05 2020-07-23 stsp }
4213 39449a05 2020-07-23 stsp
4214 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4215 39449a05 2020-07-23 stsp if (*fd == -1)
4216 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4217 39449a05 2020-07-23 stsp
4218 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4219 39449a05 2020-07-23 stsp if (outlen == -1) {
4220 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4221 39449a05 2020-07-23 stsp goto done;
4222 39449a05 2020-07-23 stsp }
4223 39449a05 2020-07-23 stsp
4224 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4225 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4226 39449a05 2020-07-23 stsp goto done;
4227 39449a05 2020-07-23 stsp }
4228 39449a05 2020-07-23 stsp done:
4229 39449a05 2020-07-23 stsp if (err) {
4230 39449a05 2020-07-23 stsp close(*fd);
4231 39449a05 2020-07-23 stsp *fd = -1;
4232 39449a05 2020-07-23 stsp }
4233 39449a05 2020-07-23 stsp return err;
4234 39449a05 2020-07-23 stsp }
4235 b72f483a 2019-02-05 stsp
4236 4ed7e80c 2018-05-20 stsp static const struct got_error *
4237 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4238 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4239 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4240 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4241 b72f483a 2019-02-05 stsp {
4242 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4243 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4244 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4245 12463d8b 2019-12-13 stsp int fd = -1;
4246 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4247 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4248 b72f483a 2019-02-05 stsp struct stat sb;
4249 b72f483a 2019-02-05 stsp
4250 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4251 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4252 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4253 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4254 98eaaa12 2019-08-03 stsp return NULL;
4255 98eaaa12 2019-08-03 stsp } else {
4256 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4257 98eaaa12 2019-08-03 stsp return NULL;
4258 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4259 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4260 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4261 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4262 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4263 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4264 98eaaa12 2019-08-03 stsp return NULL;
4265 98eaaa12 2019-08-03 stsp }
4266 3fc0c068 2019-02-10 stsp
4267 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4268 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4269 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4270 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4271 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4272 3fc0c068 2019-02-10 stsp }
4273 b72f483a 2019-02-05 stsp
4274 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4275 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4276 98eaaa12 2019-08-03 stsp switch (staged_status) {
4277 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4278 98eaaa12 2019-08-03 stsp label1 = path;
4279 98eaaa12 2019-08-03 stsp label2 = path;
4280 98eaaa12 2019-08-03 stsp break;
4281 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4282 98eaaa12 2019-08-03 stsp label2 = path;
4283 98eaaa12 2019-08-03 stsp break;
4284 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4285 98eaaa12 2019-08-03 stsp label1 = path;
4286 98eaaa12 2019-08-03 stsp break;
4287 98eaaa12 2019-08-03 stsp default:
4288 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4289 98eaaa12 2019-08-03 stsp }
4290 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4291 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4292 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4293 98eaaa12 2019-08-03 stsp }
4294 98eaaa12 2019-08-03 stsp
4295 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4296 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4297 4ce46740 2019-08-08 stsp char *id_str;
4298 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4299 408b4ebc 2019-08-03 stsp 8192);
4300 4ce46740 2019-08-08 stsp if (err)
4301 4ce46740 2019-08-08 stsp goto done;
4302 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4303 4ce46740 2019-08-08 stsp if (err)
4304 4ce46740 2019-08-08 stsp goto done;
4305 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4306 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4307 4ce46740 2019-08-08 stsp free(id_str);
4308 4ce46740 2019-08-08 stsp goto done;
4309 4ce46740 2019-08-08 stsp }
4310 4ce46740 2019-08-08 stsp free(id_str);
4311 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4312 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4313 4ce46740 2019-08-08 stsp if (err)
4314 4ce46740 2019-08-08 stsp goto done;
4315 4ce46740 2019-08-08 stsp }
4316 d00136be 2019-03-26 stsp
4317 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4318 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4319 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4320 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4321 2ec1f75b 2019-03-26 stsp goto done;
4322 2ec1f75b 2019-03-26 stsp }
4323 b72f483a 2019-02-05 stsp
4324 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4325 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4326 12463d8b 2019-12-13 stsp if (fd == -1) {
4327 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4328 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4329 39449a05 2020-07-23 stsp abspath);
4330 39449a05 2020-07-23 stsp goto done;
4331 39449a05 2020-07-23 stsp }
4332 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4333 39449a05 2020-07-23 stsp de_name, abspath);
4334 39449a05 2020-07-23 stsp if (err)
4335 39449a05 2020-07-23 stsp goto done;
4336 12463d8b 2019-12-13 stsp }
4337 12463d8b 2019-12-13 stsp } else {
4338 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4339 12463d8b 2019-12-13 stsp if (fd == -1) {
4340 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4341 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4342 39449a05 2020-07-23 stsp abspath);
4343 39449a05 2020-07-23 stsp goto done;
4344 39449a05 2020-07-23 stsp }
4345 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4346 39449a05 2020-07-23 stsp de_name, abspath);
4347 39449a05 2020-07-23 stsp if (err)
4348 39449a05 2020-07-23 stsp goto done;
4349 12463d8b 2019-12-13 stsp }
4350 12463d8b 2019-12-13 stsp }
4351 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4352 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4353 2ec1f75b 2019-03-26 stsp goto done;
4354 2ec1f75b 2019-03-26 stsp }
4355 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4356 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4357 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4358 2ec1f75b 2019-03-26 stsp goto done;
4359 2ec1f75b 2019-03-26 stsp }
4360 12463d8b 2019-12-13 stsp fd = -1;
4361 2ec1f75b 2019-03-26 stsp } else
4362 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4363 b72f483a 2019-02-05 stsp
4364 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4365 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4366 b72f483a 2019-02-05 stsp done:
4367 b72f483a 2019-02-05 stsp if (blob1)
4368 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4369 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4370 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4371 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4372 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4373 b72f483a 2019-02-05 stsp free(abspath);
4374 927df6b7 2019-02-10 stsp return err;
4375 927df6b7 2019-02-10 stsp }
4376 927df6b7 2019-02-10 stsp
4377 927df6b7 2019-02-10 stsp static const struct got_error *
4378 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4379 b00d56cd 2018-04-01 stsp {
4380 b00d56cd 2018-04-01 stsp const struct got_error *error;
4381 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4382 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4383 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4384 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4385 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4386 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4387 15a94983 2018-12-23 stsp int type1, type2;
4388 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4389 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4390 c0cc5c62 2018-10-18 stsp const char *errstr;
4391 927df6b7 2019-02-10 stsp char *path = NULL;
4392 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4393 b00d56cd 2018-04-01 stsp
4394 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4395 84de9106 2020-12-26 stsp
4396 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4397 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4398 25eccc22 2019-01-04 stsp NULL) == -1)
4399 b00d56cd 2018-04-01 stsp err(1, "pledge");
4400 b00d56cd 2018-04-01 stsp #endif
4401 b00d56cd 2018-04-01 stsp
4402 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4403 b00d56cd 2018-04-01 stsp switch (ch) {
4404 64453f7e 2020-11-21 stsp case 'a':
4405 64453f7e 2020-11-21 stsp force_text_diff = 1;
4406 64453f7e 2020-11-21 stsp break;
4407 c0cc5c62 2018-10-18 stsp case 'C':
4408 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4409 dfcab68b 2019-11-29 kn &errstr);
4410 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4411 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4412 c0cc5c62 2018-10-18 stsp break;
4413 b72f483a 2019-02-05 stsp case 'r':
4414 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4415 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4416 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4417 9ba1d308 2019-10-21 stsp optarg);
4418 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4419 b72f483a 2019-02-05 stsp break;
4420 98eaaa12 2019-08-03 stsp case 's':
4421 98eaaa12 2019-08-03 stsp diff_staged = 1;
4422 63035f9f 2019-10-06 stsp break;
4423 63035f9f 2019-10-06 stsp case 'w':
4424 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4425 98eaaa12 2019-08-03 stsp break;
4426 b00d56cd 2018-04-01 stsp default:
4427 2deda0b9 2019-03-07 stsp usage_diff();
4428 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4429 b00d56cd 2018-04-01 stsp }
4430 b00d56cd 2018-04-01 stsp }
4431 b00d56cd 2018-04-01 stsp
4432 b00d56cd 2018-04-01 stsp argc -= optind;
4433 b00d56cd 2018-04-01 stsp argv += optind;
4434 b00d56cd 2018-04-01 stsp
4435 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4436 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4437 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4438 b72f483a 2019-02-05 stsp goto done;
4439 b72f483a 2019-02-05 stsp }
4440 927df6b7 2019-02-10 stsp if (argc <= 1) {
4441 b72f483a 2019-02-05 stsp if (repo_path)
4442 b72f483a 2019-02-05 stsp errx(1,
4443 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4444 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4445 fa51e947 2020-03-27 stsp if (error) {
4446 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4447 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4448 fa51e947 2020-03-27 stsp cwd);
4449 1f03b8da 2020-03-20 stsp goto done;
4450 fa51e947 2020-03-27 stsp }
4451 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4452 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4453 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4454 927df6b7 2019-02-10 stsp goto done;
4455 927df6b7 2019-02-10 stsp }
4456 927df6b7 2019-02-10 stsp if (argc == 1) {
4457 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4458 cbd1af7a 2019-03-18 stsp argv[0]);
4459 927df6b7 2019-02-10 stsp if (error)
4460 927df6b7 2019-02-10 stsp goto done;
4461 927df6b7 2019-02-10 stsp } else {
4462 927df6b7 2019-02-10 stsp path = strdup("");
4463 927df6b7 2019-02-10 stsp if (path == NULL) {
4464 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4465 927df6b7 2019-02-10 stsp goto done;
4466 927df6b7 2019-02-10 stsp }
4467 927df6b7 2019-02-10 stsp }
4468 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4469 98eaaa12 2019-08-03 stsp if (diff_staged)
4470 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4471 98eaaa12 2019-08-03 stsp "objects in repository");
4472 15a94983 2018-12-23 stsp id_str1 = argv[0];
4473 15a94983 2018-12-23 stsp id_str2 = argv[1];
4474 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4475 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4476 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4477 30db809c 2019-06-05 stsp goto done;
4478 1a1242a9 2021-04-01 kn repo_path = strdup(worktree ?
4479 1a1242a9 2021-04-01 kn got_worktree_get_repo_path(worktree) : cwd);
4480 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4481 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4482 1a1242a9 2021-04-01 kn goto done;
4483 30db809c 2019-06-05 stsp }
4484 30db809c 2019-06-05 stsp }
4485 b00d56cd 2018-04-01 stsp } else
4486 b00d56cd 2018-04-01 stsp usage_diff();
4487 b00d56cd 2018-04-01 stsp
4488 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4489 76089277 2018-04-01 stsp free(repo_path);
4490 b00d56cd 2018-04-01 stsp if (error != NULL)
4491 b00d56cd 2018-04-01 stsp goto done;
4492 b00d56cd 2018-04-01 stsp
4493 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4494 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4495 c02c541e 2019-03-29 stsp if (error)
4496 c02c541e 2019-03-29 stsp goto done;
4497 c02c541e 2019-03-29 stsp
4498 30db809c 2019-06-05 stsp if (argc <= 1) {
4499 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4500 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4501 b72f483a 2019-02-05 stsp char *id_str;
4502 72ea6654 2019-07-27 stsp
4503 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4504 72ea6654 2019-07-27 stsp
4505 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4506 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4507 b72f483a 2019-02-05 stsp if (error)
4508 b72f483a 2019-02-05 stsp goto done;
4509 b72f483a 2019-02-05 stsp arg.repo = repo;
4510 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4511 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4512 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4513 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4514 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4515 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4516 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4517 b72f483a 2019-02-05 stsp
4518 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4519 72ea6654 2019-07-27 stsp if (error)
4520 72ea6654 2019-07-27 stsp goto done;
4521 72ea6654 2019-07-27 stsp
4522 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4523 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4524 b72f483a 2019-02-05 stsp free(id_str);
4525 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4526 b72f483a 2019-02-05 stsp goto done;
4527 b72f483a 2019-02-05 stsp }
4528 84de9106 2020-12-26 stsp
4529 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4530 84de9106 2020-12-26 stsp if (error)
4531 84de9106 2020-12-26 stsp return error;
4532 b72f483a 2019-02-05 stsp
4533 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4534 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4535 0adb7196 2019-08-11 stsp if (error)
4536 0adb7196 2019-08-11 stsp goto done;
4537 b00d56cd 2018-04-01 stsp
4538 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4539 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4540 0adb7196 2019-08-11 stsp if (error)
4541 0adb7196 2019-08-11 stsp goto done;
4542 b00d56cd 2018-04-01 stsp
4543 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4544 15a94983 2018-12-23 stsp if (error)
4545 15a94983 2018-12-23 stsp goto done;
4546 15a94983 2018-12-23 stsp
4547 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4548 15a94983 2018-12-23 stsp if (error)
4549 15a94983 2018-12-23 stsp goto done;
4550 15a94983 2018-12-23 stsp
4551 15a94983 2018-12-23 stsp if (type1 != type2) {
4552 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4553 b00d56cd 2018-04-01 stsp goto done;
4554 b00d56cd 2018-04-01 stsp }
4555 b00d56cd 2018-04-01 stsp
4556 15a94983 2018-12-23 stsp switch (type1) {
4557 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4558 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4559 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4560 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4561 b00d56cd 2018-04-01 stsp break;
4562 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4563 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4564 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4565 64453f7e 2020-11-21 stsp repo, stdout);
4566 b00d56cd 2018-04-01 stsp break;
4567 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4568 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4569 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4570 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4571 64453f7e 2020-11-21 stsp stdout);
4572 b00d56cd 2018-04-01 stsp break;
4573 b00d56cd 2018-04-01 stsp default:
4574 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4575 b00d56cd 2018-04-01 stsp }
4576 b00d56cd 2018-04-01 stsp done:
4577 5e70831e 2019-05-28 stsp free(label1);
4578 5e70831e 2019-05-28 stsp free(label2);
4579 15a94983 2018-12-23 stsp free(id1);
4580 15a94983 2018-12-23 stsp free(id2);
4581 927df6b7 2019-02-10 stsp free(path);
4582 b72f483a 2019-02-05 stsp if (worktree)
4583 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4584 ad242220 2018-09-08 stsp if (repo) {
4585 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4586 ad242220 2018-09-08 stsp if (error == NULL)
4587 1d0f4054 2021-06-17 stsp error = close_err;
4588 ad242220 2018-09-08 stsp }
4589 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4590 b00d56cd 2018-04-01 stsp return error;
4591 404c43c4 2018-06-21 stsp }
4592 404c43c4 2018-06-21 stsp
4593 404c43c4 2018-06-21 stsp __dead static void
4594 404c43c4 2018-06-21 stsp usage_blame(void)
4595 404c43c4 2018-06-21 stsp {
4596 9270e621 2019-02-05 stsp fprintf(stderr,
4597 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4598 404c43c4 2018-06-21 stsp getprogname());
4599 404c43c4 2018-06-21 stsp exit(1);
4600 b00d56cd 2018-04-01 stsp }
4601 b00d56cd 2018-04-01 stsp
4602 28315671 2019-08-14 stsp struct blame_line {
4603 28315671 2019-08-14 stsp int annotated;
4604 28315671 2019-08-14 stsp char *id_str;
4605 82f6abb8 2019-08-14 stsp char *committer;
4606 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4607 28315671 2019-08-14 stsp };
4608 28315671 2019-08-14 stsp
4609 28315671 2019-08-14 stsp struct blame_cb_args {
4610 28315671 2019-08-14 stsp struct blame_line *lines;
4611 28315671 2019-08-14 stsp int nlines;
4612 7ef28ff8 2019-08-14 stsp int nlines_prec;
4613 28315671 2019-08-14 stsp int lineno_cur;
4614 28315671 2019-08-14 stsp off_t *line_offsets;
4615 28315671 2019-08-14 stsp FILE *f;
4616 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4617 28315671 2019-08-14 stsp };
4618 28315671 2019-08-14 stsp
4619 404c43c4 2018-06-21 stsp static const struct got_error *
4620 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4621 28315671 2019-08-14 stsp {
4622 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4623 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4624 28315671 2019-08-14 stsp struct blame_line *bline;
4625 82f6abb8 2019-08-14 stsp char *line = NULL;
4626 28315671 2019-08-14 stsp size_t linesize = 0;
4627 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4628 28315671 2019-08-14 stsp off_t offset;
4629 bcb49d15 2019-08-14 stsp struct tm tm;
4630 bcb49d15 2019-08-14 stsp time_t committer_time;
4631 28315671 2019-08-14 stsp
4632 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4633 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4634 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4635 28315671 2019-08-14 stsp
4636 28315671 2019-08-14 stsp if (sigint_received)
4637 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4638 28315671 2019-08-14 stsp
4639 2839f8b9 2019-08-18 stsp if (lineno == -1)
4640 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4641 2839f8b9 2019-08-18 stsp
4642 28315671 2019-08-14 stsp /* Annotate this line. */
4643 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4644 28315671 2019-08-14 stsp if (bline->annotated)
4645 28315671 2019-08-14 stsp return NULL;
4646 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4647 28315671 2019-08-14 stsp if (err)
4648 28315671 2019-08-14 stsp return err;
4649 82f6abb8 2019-08-14 stsp
4650 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4651 82f6abb8 2019-08-14 stsp if (err)
4652 82f6abb8 2019-08-14 stsp goto done;
4653 82f6abb8 2019-08-14 stsp
4654 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4655 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4656 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4657 bcb49d15 2019-08-14 stsp goto done;
4658 bcb49d15 2019-08-14 stsp }
4659 bcb49d15 2019-08-14 stsp
4660 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4661 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
4662 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
4663 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4664 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4665 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4666 82f6abb8 2019-08-14 stsp goto done;
4667 82f6abb8 2019-08-14 stsp }
4668 28315671 2019-08-14 stsp bline->annotated = 1;
4669 28315671 2019-08-14 stsp
4670 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4671 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4672 28315671 2019-08-14 stsp if (!bline->annotated)
4673 82f6abb8 2019-08-14 stsp goto done;
4674 28315671 2019-08-14 stsp
4675 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4676 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4677 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4678 82f6abb8 2019-08-14 stsp goto done;
4679 82f6abb8 2019-08-14 stsp }
4680 28315671 2019-08-14 stsp
4681 28315671 2019-08-14 stsp while (bline->annotated) {
4682 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4683 82f6abb8 2019-08-14 stsp size_t len;
4684 82f6abb8 2019-08-14 stsp
4685 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4686 28315671 2019-08-14 stsp if (ferror(a->f))
4687 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4688 28315671 2019-08-14 stsp break;
4689 28315671 2019-08-14 stsp }
4690 82f6abb8 2019-08-14 stsp
4691 82f6abb8 2019-08-14 stsp committer = bline->committer;
4692 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4693 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4694 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4695 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4696 82f6abb8 2019-08-14 stsp if (at)
4697 82f6abb8 2019-08-14 stsp *at = '\0';
4698 82f6abb8 2019-08-14 stsp len = strlen(committer);
4699 82f6abb8 2019-08-14 stsp if (len >= 9)
4700 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4701 28315671 2019-08-14 stsp
4702 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4703 28315671 2019-08-14 stsp if (nl)
4704 28315671 2019-08-14 stsp *nl = '\0';
4705 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4706 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4707 28315671 2019-08-14 stsp
4708 28315671 2019-08-14 stsp a->lineno_cur++;
4709 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4710 28315671 2019-08-14 stsp }
4711 82f6abb8 2019-08-14 stsp done:
4712 82f6abb8 2019-08-14 stsp if (commit)
4713 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4714 28315671 2019-08-14 stsp free(line);
4715 28315671 2019-08-14 stsp return err;
4716 28315671 2019-08-14 stsp }
4717 28315671 2019-08-14 stsp
4718 28315671 2019-08-14 stsp static const struct got_error *
4719 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4720 404c43c4 2018-06-21 stsp {
4721 404c43c4 2018-06-21 stsp const struct got_error *error;
4722 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4723 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4724 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4725 0587e10c 2020-07-23 stsp char *link_target = NULL;
4726 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4727 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4728 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4729 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4730 28315671 2019-08-14 stsp struct blame_cb_args bca;
4731 28315671 2019-08-14 stsp int ch, obj_type, i;
4732 be659d10 2020-11-18 stsp off_t filesize;
4733 90f3c347 2019-08-19 stsp
4734 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4735 404c43c4 2018-06-21 stsp
4736 404c43c4 2018-06-21 stsp #ifndef PROFILE
4737 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4738 36e2fb66 2019-01-04 stsp NULL) == -1)
4739 404c43c4 2018-06-21 stsp err(1, "pledge");
4740 404c43c4 2018-06-21 stsp #endif
4741 404c43c4 2018-06-21 stsp
4742 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4743 404c43c4 2018-06-21 stsp switch (ch) {
4744 404c43c4 2018-06-21 stsp case 'c':
4745 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4746 404c43c4 2018-06-21 stsp break;
4747 66bea077 2018-08-02 stsp case 'r':
4748 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4749 66bea077 2018-08-02 stsp if (repo_path == NULL)
4750 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4751 9ba1d308 2019-10-21 stsp optarg);
4752 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4753 66bea077 2018-08-02 stsp break;
4754 404c43c4 2018-06-21 stsp default:
4755 2deda0b9 2019-03-07 stsp usage_blame();
4756 404c43c4 2018-06-21 stsp /* NOTREACHED */
4757 404c43c4 2018-06-21 stsp }
4758 404c43c4 2018-06-21 stsp }
4759 404c43c4 2018-06-21 stsp
4760 404c43c4 2018-06-21 stsp argc -= optind;
4761 404c43c4 2018-06-21 stsp argv += optind;
4762 404c43c4 2018-06-21 stsp
4763 a39318fd 2018-08-02 stsp if (argc == 1)
4764 404c43c4 2018-06-21 stsp path = argv[0];
4765 a39318fd 2018-08-02 stsp else
4766 404c43c4 2018-06-21 stsp usage_blame();
4767 404c43c4 2018-06-21 stsp
4768 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4769 66bea077 2018-08-02 stsp if (cwd == NULL) {
4770 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4771 66bea077 2018-08-02 stsp goto done;
4772 66bea077 2018-08-02 stsp }
4773 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4774 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4775 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4776 66bea077 2018-08-02 stsp goto done;
4777 0c06baac 2019-02-05 stsp else
4778 0c06baac 2019-02-05 stsp error = NULL;
4779 0c06baac 2019-02-05 stsp if (worktree) {
4780 0c06baac 2019-02-05 stsp repo_path =
4781 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4782 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4783 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4784 1f03b8da 2020-03-20 stsp if (error)
4785 1f03b8da 2020-03-20 stsp goto done;
4786 1f03b8da 2020-03-20 stsp }
4787 0c06baac 2019-02-05 stsp } else {
4788 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4789 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4790 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4791 0c06baac 2019-02-05 stsp goto done;
4792 0c06baac 2019-02-05 stsp }
4793 66bea077 2018-08-02 stsp }
4794 66bea077 2018-08-02 stsp }
4795 36e2fb66 2019-01-04 stsp
4796 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4797 c02c541e 2019-03-29 stsp if (error != NULL)
4798 404c43c4 2018-06-21 stsp goto done;
4799 404c43c4 2018-06-21 stsp
4800 0c06baac 2019-02-05 stsp if (worktree) {
4801 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4802 01740607 2020-11-04 stsp char *p;
4803 01740607 2020-11-04 stsp
4804 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4805 01740607 2020-11-04 stsp if (error)
4806 01740607 2020-11-04 stsp goto done;
4807 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4808 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4809 01740607 2020-11-04 stsp p) == -1) {
4810 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4811 01740607 2020-11-04 stsp free(p);
4812 0c06baac 2019-02-05 stsp goto done;
4813 0c06baac 2019-02-05 stsp }
4814 0c06baac 2019-02-05 stsp free(p);
4815 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4816 0c06baac 2019-02-05 stsp } else {
4817 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4818 01740607 2020-11-04 stsp if (error)
4819 01740607 2020-11-04 stsp goto done;
4820 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4821 0c06baac 2019-02-05 stsp }
4822 0c06baac 2019-02-05 stsp if (error)
4823 66bea077 2018-08-02 stsp goto done;
4824 66bea077 2018-08-02 stsp
4825 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4826 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4827 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4828 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4829 404c43c4 2018-06-21 stsp if (error != NULL)
4830 66bea077 2018-08-02 stsp goto done;
4831 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4832 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4833 404c43c4 2018-06-21 stsp if (error != NULL)
4834 66bea077 2018-08-02 stsp goto done;
4835 404c43c4 2018-06-21 stsp } else {
4836 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4837 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4838 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4839 84de9106 2020-12-26 stsp NULL);
4840 84de9106 2020-12-26 stsp if (error)
4841 84de9106 2020-12-26 stsp goto done;
4842 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4843 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4844 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4845 30837e32 2019-07-25 stsp if (error)
4846 66bea077 2018-08-02 stsp goto done;
4847 404c43c4 2018-06-21 stsp }
4848 404c43c4 2018-06-21 stsp
4849 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4850 0587e10c 2020-07-23 stsp commit_id, repo);
4851 0587e10c 2020-07-23 stsp if (error)
4852 0587e10c 2020-07-23 stsp goto done;
4853 0587e10c 2020-07-23 stsp
4854 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4855 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4856 28315671 2019-08-14 stsp if (error)
4857 28315671 2019-08-14 stsp goto done;
4858 28315671 2019-08-14 stsp
4859 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4860 28315671 2019-08-14 stsp if (error)
4861 28315671 2019-08-14 stsp goto done;
4862 28315671 2019-08-14 stsp
4863 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4864 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4865 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4866 28315671 2019-08-14 stsp goto done;
4867 28315671 2019-08-14 stsp }
4868 28315671 2019-08-14 stsp
4869 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4870 28315671 2019-08-14 stsp if (error)
4871 28315671 2019-08-14 stsp goto done;
4872 28315671 2019-08-14 stsp bca.f = got_opentemp();
4873 28315671 2019-08-14 stsp if (bca.f == NULL) {
4874 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4875 28315671 2019-08-14 stsp goto done;
4876 28315671 2019-08-14 stsp }
4877 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4878 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4879 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4880 28315671 2019-08-14 stsp goto done;
4881 28315671 2019-08-14 stsp
4882 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4883 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4884 b02560ec 2019-08-19 stsp bca.nlines--;
4885 b02560ec 2019-08-19 stsp
4886 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4887 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4888 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4889 28315671 2019-08-14 stsp goto done;
4890 28315671 2019-08-14 stsp }
4891 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4892 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4893 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4894 7ef28ff8 2019-08-14 stsp while (i > 0) {
4895 7ef28ff8 2019-08-14 stsp i /= 10;
4896 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4897 7ef28ff8 2019-08-14 stsp }
4898 82f6abb8 2019-08-14 stsp bca.repo = repo;
4899 7ef28ff8 2019-08-14 stsp
4900 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4901 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4902 404c43c4 2018-06-21 stsp done:
4903 66bea077 2018-08-02 stsp free(in_repo_path);
4904 0587e10c 2020-07-23 stsp free(link_target);
4905 66bea077 2018-08-02 stsp free(repo_path);
4906 66bea077 2018-08-02 stsp free(cwd);
4907 404c43c4 2018-06-21 stsp free(commit_id);
4908 28315671 2019-08-14 stsp free(obj_id);
4909 28315671 2019-08-14 stsp if (blob)
4910 28315671 2019-08-14 stsp got_object_blob_close(blob);
4911 0c06baac 2019-02-05 stsp if (worktree)
4912 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4913 ad242220 2018-09-08 stsp if (repo) {
4914 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4915 ad242220 2018-09-08 stsp if (error == NULL)
4916 1d0f4054 2021-06-17 stsp error = close_err;
4917 ad242220 2018-09-08 stsp }
4918 3affba96 2019-09-22 stsp if (bca.lines) {
4919 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4920 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4921 3affba96 2019-09-22 stsp free(bline->id_str);
4922 3affba96 2019-09-22 stsp free(bline->committer);
4923 3affba96 2019-09-22 stsp }
4924 3affba96 2019-09-22 stsp free(bca.lines);
4925 28315671 2019-08-14 stsp }
4926 28315671 2019-08-14 stsp free(bca.line_offsets);
4927 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4928 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4929 404c43c4 2018-06-21 stsp return error;
4930 5de5890b 2018-10-18 stsp }
4931 5de5890b 2018-10-18 stsp
4932 5de5890b 2018-10-18 stsp __dead static void
4933 5de5890b 2018-10-18 stsp usage_tree(void)
4934 5de5890b 2018-10-18 stsp {
4935 c1669e2e 2019-01-09 stsp fprintf(stderr,
4936 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4937 5de5890b 2018-10-18 stsp getprogname());
4938 5de5890b 2018-10-18 stsp exit(1);
4939 5de5890b 2018-10-18 stsp }
4940 5de5890b 2018-10-18 stsp
4941 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4942 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4943 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4944 c1669e2e 2019-01-09 stsp {
4945 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4946 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4947 848d6979 2019-08-12 stsp const char *modestr = "";
4948 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4949 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4950 5de5890b 2018-10-18 stsp
4951 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4952 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4953 c1669e2e 2019-01-09 stsp path++;
4954 c1669e2e 2019-01-09 stsp
4955 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4956 63c5ca5d 2019-08-24 stsp modestr = "$";
4957 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4958 0d6c6ee3 2020-05-20 stsp int i;
4959 0d6c6ee3 2020-05-20 stsp
4960 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4961 0d6c6ee3 2020-05-20 stsp if (err)
4962 0d6c6ee3 2020-05-20 stsp return err;
4963 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4964 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4965 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4966 0d6c6ee3 2020-05-20 stsp }
4967 0d6c6ee3 2020-05-20 stsp
4968 848d6979 2019-08-12 stsp modestr = "@";
4969 0d6c6ee3 2020-05-20 stsp }
4970 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4971 848d6979 2019-08-12 stsp modestr = "/";
4972 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4973 848d6979 2019-08-12 stsp modestr = "*";
4974 848d6979 2019-08-12 stsp
4975 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4976 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4977 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4978 0d6c6ee3 2020-05-20 stsp
4979 0d6c6ee3 2020-05-20 stsp free(link_target);
4980 0d6c6ee3 2020-05-20 stsp return NULL;
4981 c1669e2e 2019-01-09 stsp }
4982 c1669e2e 2019-01-09 stsp
4983 5de5890b 2018-10-18 stsp static const struct got_error *
4984 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4985 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4986 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4987 5de5890b 2018-10-18 stsp {
4988 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4989 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4990 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4991 56e0773d 2019-11-28 stsp int nentries, i;
4992 5de5890b 2018-10-18 stsp
4993 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4994 5de5890b 2018-10-18 stsp if (err)
4995 5de5890b 2018-10-18 stsp goto done;
4996 5de5890b 2018-10-18 stsp
4997 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4998 5de5890b 2018-10-18 stsp if (err)
4999 5de5890b 2018-10-18 stsp goto done;
5000 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
5001 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
5002 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
5003 5de5890b 2018-10-18 stsp char *id = NULL;
5004 84453469 2018-11-11 stsp
5005 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5006 84453469 2018-11-11 stsp break;
5007 84453469 2018-11-11 stsp
5008 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
5009 5de5890b 2018-10-18 stsp if (show_ids) {
5010 5de5890b 2018-10-18 stsp char *id_str;
5011 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5012 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5013 5de5890b 2018-10-18 stsp if (err)
5014 5de5890b 2018-10-18 stsp goto done;
5015 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
5016 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5017 5de5890b 2018-10-18 stsp free(id_str);
5018 5de5890b 2018-10-18 stsp goto done;
5019 5de5890b 2018-10-18 stsp }
5020 5de5890b 2018-10-18 stsp free(id_str);
5021 5de5890b 2018-10-18 stsp }
5022 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5023 5de5890b 2018-10-18 stsp free(id);
5024 0d6c6ee3 2020-05-20 stsp if (err)
5025 0d6c6ee3 2020-05-20 stsp goto done;
5026 c1669e2e 2019-01-09 stsp
5027 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5028 c1669e2e 2019-01-09 stsp char *child_path;
5029 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
5030 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
5031 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
5032 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5033 c1669e2e 2019-01-09 stsp goto done;
5034 c1669e2e 2019-01-09 stsp }
5035 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
5036 c1669e2e 2019-01-09 stsp root_path, repo);
5037 c1669e2e 2019-01-09 stsp free(child_path);
5038 c1669e2e 2019-01-09 stsp if (err)
5039 c1669e2e 2019-01-09 stsp goto done;
5040 c1669e2e 2019-01-09 stsp }
5041 5de5890b 2018-10-18 stsp }
5042 5de5890b 2018-10-18 stsp done:
5043 5de5890b 2018-10-18 stsp if (tree)
5044 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
5045 5de5890b 2018-10-18 stsp free(tree_id);
5046 5de5890b 2018-10-18 stsp return err;
5047 404c43c4 2018-06-21 stsp }
5048 404c43c4 2018-06-21 stsp
5049 5de5890b 2018-10-18 stsp static const struct got_error *
5050 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5051 5de5890b 2018-10-18 stsp {
5052 5de5890b 2018-10-18 stsp const struct got_error *error;
5053 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
5054 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
5055 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
5056 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5057 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
5058 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
5059 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
5060 5de5890b 2018-10-18 stsp int ch;
5061 5de5890b 2018-10-18 stsp
5062 5de5890b 2018-10-18 stsp #ifndef PROFILE
5063 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5064 0f8d269b 2019-01-04 stsp NULL) == -1)
5065 5de5890b 2018-10-18 stsp err(1, "pledge");
5066 5de5890b 2018-10-18 stsp #endif
5067 5de5890b 2018-10-18 stsp
5068 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5069 5de5890b 2018-10-18 stsp switch (ch) {
5070 5de5890b 2018-10-18 stsp case 'c':
5071 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5072 5de5890b 2018-10-18 stsp break;
5073 5de5890b 2018-10-18 stsp case 'r':
5074 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
5075 5de5890b 2018-10-18 stsp if (repo_path == NULL)
5076 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5077 9ba1d308 2019-10-21 stsp optarg);
5078 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5079 5de5890b 2018-10-18 stsp break;
5080 5de5890b 2018-10-18 stsp case 'i':
5081 5de5890b 2018-10-18 stsp show_ids = 1;
5082 5de5890b 2018-10-18 stsp break;
5083 c1669e2e 2019-01-09 stsp case 'R':
5084 c1669e2e 2019-01-09 stsp recurse = 1;
5085 c1669e2e 2019-01-09 stsp break;
5086 5de5890b 2018-10-18 stsp default:
5087 2deda0b9 2019-03-07 stsp usage_tree();
5088 5de5890b 2018-10-18 stsp /* NOTREACHED */
5089 5de5890b 2018-10-18 stsp }
5090 5de5890b 2018-10-18 stsp }
5091 5de5890b 2018-10-18 stsp
5092 5de5890b 2018-10-18 stsp argc -= optind;
5093 5de5890b 2018-10-18 stsp argv += optind;
5094 5de5890b 2018-10-18 stsp
5095 5de5890b 2018-10-18 stsp if (argc == 1)
5096 5de5890b 2018-10-18 stsp path = argv[0];
5097 5de5890b 2018-10-18 stsp else if (argc > 1)
5098 5de5890b 2018-10-18 stsp usage_tree();
5099 5de5890b 2018-10-18 stsp else
5100 7a2c19d6 2019-02-05 stsp path = NULL;
5101 7a2c19d6 2019-02-05 stsp
5102 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5103 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5104 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5105 9bf7a39b 2019-02-05 stsp goto done;
5106 9bf7a39b 2019-02-05 stsp }
5107 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5108 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5109 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5110 7a2c19d6 2019-02-05 stsp goto done;
5111 7a2c19d6 2019-02-05 stsp else
5112 7a2c19d6 2019-02-05 stsp error = NULL;
5113 7a2c19d6 2019-02-05 stsp if (worktree) {
5114 7a2c19d6 2019-02-05 stsp repo_path =
5115 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5116 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5117 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5118 7a2c19d6 2019-02-05 stsp if (error)
5119 7a2c19d6 2019-02-05 stsp goto done;
5120 7a2c19d6 2019-02-05 stsp } else {
5121 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5122 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5123 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5124 7a2c19d6 2019-02-05 stsp goto done;
5125 7a2c19d6 2019-02-05 stsp }
5126 5de5890b 2018-10-18 stsp }
5127 5de5890b 2018-10-18 stsp }
5128 5de5890b 2018-10-18 stsp
5129 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5130 5de5890b 2018-10-18 stsp if (error != NULL)
5131 c02c541e 2019-03-29 stsp goto done;
5132 c02c541e 2019-03-29 stsp
5133 4fedbf4c 2020-11-07 stsp if (worktree) {
5134 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5135 4fedbf4c 2020-11-07 stsp char *p;
5136 5de5890b 2018-10-18 stsp
5137 4fedbf4c 2020-11-07 stsp if (path == NULL)
5138 4fedbf4c 2020-11-07 stsp path = "";
5139 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5140 4fedbf4c 2020-11-07 stsp if (error)
5141 4fedbf4c 2020-11-07 stsp goto done;
5142 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5143 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5144 4fedbf4c 2020-11-07 stsp p) == -1) {
5145 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5146 9bf7a39b 2019-02-05 stsp free(p);
5147 4fedbf4c 2020-11-07 stsp goto done;
5148 4fedbf4c 2020-11-07 stsp }
5149 4fedbf4c 2020-11-07 stsp free(p);
5150 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5151 4fedbf4c 2020-11-07 stsp if (error)
5152 4fedbf4c 2020-11-07 stsp goto done;
5153 4fedbf4c 2020-11-07 stsp } else {
5154 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5155 4fedbf4c 2020-11-07 stsp if (error)
5156 4fedbf4c 2020-11-07 stsp goto done;
5157 4fedbf4c 2020-11-07 stsp if (path == NULL)
5158 9bf7a39b 2019-02-05 stsp path = "/";
5159 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5160 9bf7a39b 2019-02-05 stsp if (error != NULL)
5161 9bf7a39b 2019-02-05 stsp goto done;
5162 9bf7a39b 2019-02-05 stsp }
5163 5de5890b 2018-10-18 stsp
5164 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5165 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5166 4e0a20a4 2020-03-23 tracey if (worktree)
5167 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5168 4e0a20a4 2020-03-23 tracey else
5169 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5170 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5171 5de5890b 2018-10-18 stsp if (error != NULL)
5172 5de5890b 2018-10-18 stsp goto done;
5173 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5174 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5175 5de5890b 2018-10-18 stsp if (error != NULL)
5176 5de5890b 2018-10-18 stsp goto done;
5177 5de5890b 2018-10-18 stsp } else {
5178 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5179 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5180 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5181 84de9106 2020-12-26 stsp NULL);
5182 84de9106 2020-12-26 stsp if (error)
5183 84de9106 2020-12-26 stsp goto done;
5184 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5185 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5186 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5187 30837e32 2019-07-25 stsp if (error)
5188 5de5890b 2018-10-18 stsp goto done;
5189 5de5890b 2018-10-18 stsp }
5190 5de5890b 2018-10-18 stsp
5191 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5192 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5193 5de5890b 2018-10-18 stsp done:
5194 5de5890b 2018-10-18 stsp free(in_repo_path);
5195 5de5890b 2018-10-18 stsp free(repo_path);
5196 5de5890b 2018-10-18 stsp free(cwd);
5197 5de5890b 2018-10-18 stsp free(commit_id);
5198 7a2c19d6 2019-02-05 stsp if (worktree)
5199 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5200 5de5890b 2018-10-18 stsp if (repo) {
5201 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5202 5de5890b 2018-10-18 stsp if (error == NULL)
5203 1d0f4054 2021-06-17 stsp error = close_err;
5204 5de5890b 2018-10-18 stsp }
5205 5de5890b 2018-10-18 stsp return error;
5206 5de5890b 2018-10-18 stsp }
5207 5de5890b 2018-10-18 stsp
5208 6bad629b 2019-02-04 stsp __dead static void
5209 6bad629b 2019-02-04 stsp usage_status(void)
5210 6bad629b 2019-02-04 stsp {
5211 f6343036 2021-06-22 stsp fprintf(stderr, "usage: %s status [-I] [-s status-codes ] [path ...]\n",
5212 081470ac 2020-08-13 stsp getprogname());
5213 6bad629b 2019-02-04 stsp exit(1);
5214 6bad629b 2019-02-04 stsp }
5215 5c860e29 2018-03-12 stsp
5216 b72f483a 2019-02-05 stsp static const struct got_error *
5217 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5218 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5219 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5220 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5221 6bad629b 2019-02-04 stsp {
5222 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5223 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5224 081470ac 2020-08-13 stsp if (arg) {
5225 081470ac 2020-08-13 stsp char *status_codes = arg;
5226 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
5227 081470ac 2020-08-13 stsp int i;
5228 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5229 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
5230 081470ac 2020-08-13 stsp staged_status == status_codes[i])
5231 081470ac 2020-08-13 stsp break;
5232 081470ac 2020-08-13 stsp }
5233 081470ac 2020-08-13 stsp if (i == ncodes)
5234 081470ac 2020-08-13 stsp return NULL;
5235 081470ac 2020-08-13 stsp }
5236 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5237 b72f483a 2019-02-05 stsp return NULL;
5238 6bad629b 2019-02-04 stsp }
5239 5c860e29 2018-03-12 stsp
5240 6bad629b 2019-02-04 stsp static const struct got_error *
5241 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5242 6bad629b 2019-02-04 stsp {
5243 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5244 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5245 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5246 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
5247 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5248 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5249 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5250 5c860e29 2018-03-12 stsp
5251 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5252 72ea6654 2019-07-27 stsp
5253 f6343036 2021-06-22 stsp while ((ch = getopt(argc, argv, "Is:")) != -1) {
5254 6bad629b 2019-02-04 stsp switch (ch) {
5255 f6343036 2021-06-22 stsp case 'I':
5256 f6343036 2021-06-22 stsp no_ignores = 1;
5257 f6343036 2021-06-22 stsp break;
5258 081470ac 2020-08-13 stsp case 's':
5259 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5260 081470ac 2020-08-13 stsp switch (optarg[i]) {
5261 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5262 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5263 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5264 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5265 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5266 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5267 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5268 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5269 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5270 081470ac 2020-08-13 stsp break;
5271 081470ac 2020-08-13 stsp default:
5272 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5273 081470ac 2020-08-13 stsp optarg[i]);
5274 081470ac 2020-08-13 stsp }
5275 081470ac 2020-08-13 stsp }
5276 081470ac 2020-08-13 stsp status_codes = optarg;
5277 081470ac 2020-08-13 stsp break;
5278 5c860e29 2018-03-12 stsp default:
5279 2deda0b9 2019-03-07 stsp usage_status();
5280 6bad629b 2019-02-04 stsp /* NOTREACHED */
5281 5c860e29 2018-03-12 stsp }
5282 5c860e29 2018-03-12 stsp }
5283 5c860e29 2018-03-12 stsp
5284 6bad629b 2019-02-04 stsp argc -= optind;
5285 6bad629b 2019-02-04 stsp argv += optind;
5286 5c860e29 2018-03-12 stsp
5287 6bad629b 2019-02-04 stsp #ifndef PROFILE
5288 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5289 6bad629b 2019-02-04 stsp NULL) == -1)
5290 6bad629b 2019-02-04 stsp err(1, "pledge");
5291 f42b1b34 2018-03-12 stsp #endif
5292 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5293 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5294 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5295 927df6b7 2019-02-10 stsp goto done;
5296 927df6b7 2019-02-10 stsp }
5297 927df6b7 2019-02-10 stsp
5298 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5299 fa51e947 2020-03-27 stsp if (error) {
5300 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5301 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5302 a5edda0a 2019-07-27 stsp goto done;
5303 fa51e947 2020-03-27 stsp }
5304 6bad629b 2019-02-04 stsp
5305 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5306 c9956ddf 2019-09-08 stsp NULL);
5307 6bad629b 2019-02-04 stsp if (error != NULL)
5308 6bad629b 2019-02-04 stsp goto done;
5309 6bad629b 2019-02-04 stsp
5310 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5311 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5312 087fb88c 2019-08-04 stsp if (error)
5313 087fb88c 2019-08-04 stsp goto done;
5314 087fb88c 2019-08-04 stsp
5315 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5316 6bad629b 2019-02-04 stsp if (error)
5317 6bad629b 2019-02-04 stsp goto done;
5318 6bad629b 2019-02-04 stsp
5319 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5320 f6343036 2021-06-22 stsp print_status, status_codes, check_cancelled, NULL);
5321 6bad629b 2019-02-04 stsp done:
5322 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5323 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5324 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5325 927df6b7 2019-02-10 stsp free(cwd);
5326 d0eebce4 2019-03-11 stsp return error;
5327 d0eebce4 2019-03-11 stsp }
5328 d0eebce4 2019-03-11 stsp
5329 d0eebce4 2019-03-11 stsp __dead static void
5330 d0eebce4 2019-03-11 stsp usage_ref(void)
5331 d0eebce4 2019-03-11 stsp {
5332 d0eebce4 2019-03-11 stsp fprintf(stderr,
5333 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5334 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5335 d0eebce4 2019-03-11 stsp getprogname());
5336 d0eebce4 2019-03-11 stsp exit(1);
5337 d0eebce4 2019-03-11 stsp }
5338 d0eebce4 2019-03-11 stsp
5339 d0eebce4 2019-03-11 stsp static const struct got_error *
5340 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5341 d0eebce4 2019-03-11 stsp {
5342 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5343 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5344 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5345 d0eebce4 2019-03-11 stsp
5346 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5347 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5348 d0eebce4 2019-03-11 stsp if (err)
5349 d0eebce4 2019-03-11 stsp return err;
5350 d0eebce4 2019-03-11 stsp
5351 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5352 d0eebce4 2019-03-11 stsp char *refstr;
5353 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5354 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5355 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5356 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5357 d0eebce4 2019-03-11 stsp free(refstr);
5358 d0eebce4 2019-03-11 stsp }
5359 d0eebce4 2019-03-11 stsp
5360 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5361 d0eebce4 2019-03-11 stsp return NULL;
5362 d0eebce4 2019-03-11 stsp }
5363 d0eebce4 2019-03-11 stsp
5364 d0eebce4 2019-03-11 stsp static const struct got_error *
5365 161728eb 2021-07-24 stsp delete_ref_by_name(struct got_repository *repo, const char *refname)
5366 d0eebce4 2019-03-11 stsp {
5367 161728eb 2021-07-24 stsp const struct got_error *err;
5368 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5369 d0eebce4 2019-03-11 stsp
5370 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5371 d0eebce4 2019-03-11 stsp if (err)
5372 d0eebce4 2019-03-11 stsp return err;
5373 993f033b 2021-07-16 stsp
5374 161728eb 2021-07-24 stsp err = delete_ref(repo, ref);
5375 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5376 d0eebce4 2019-03-11 stsp return err;
5377 d0eebce4 2019-03-11 stsp }
5378 d0eebce4 2019-03-11 stsp
5379 d0eebce4 2019-03-11 stsp static const struct got_error *
5380 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5381 d0eebce4 2019-03-11 stsp {
5382 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5383 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5384 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5385 d1644381 2019-07-14 stsp
5386 d1644381 2019-07-14 stsp /*
5387 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5388 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5389 d1644381 2019-07-14 stsp * an unintended typo.
5390 d1644381 2019-07-14 stsp */
5391 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5392 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5393 d0eebce4 2019-03-11 stsp
5394 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5395 dd88155e 2019-06-29 stsp repo);
5396 d83d9d5c 2019-05-13 stsp if (err) {
5397 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5398 d0eebce4 2019-03-11 stsp
5399 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5400 d83d9d5c 2019-05-13 stsp return err;
5401 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5402 d83d9d5c 2019-05-13 stsp if (err)
5403 d83d9d5c 2019-05-13 stsp return err;
5404 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5405 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5406 d83d9d5c 2019-05-13 stsp if (err)
5407 d83d9d5c 2019-05-13 stsp return err;
5408 d83d9d5c 2019-05-13 stsp }
5409 d83d9d5c 2019-05-13 stsp
5410 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5411 d0eebce4 2019-03-11 stsp if (err)
5412 d0eebce4 2019-03-11 stsp goto done;
5413 d0eebce4 2019-03-11 stsp
5414 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5415 d0eebce4 2019-03-11 stsp done:
5416 d0eebce4 2019-03-11 stsp if (ref)
5417 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5418 d0eebce4 2019-03-11 stsp free(id);
5419 d1c1ae5f 2019-08-12 stsp return err;
5420 d1c1ae5f 2019-08-12 stsp }
5421 d1c1ae5f 2019-08-12 stsp
5422 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5423 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5424 d1c1ae5f 2019-08-12 stsp {
5425 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5426 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5427 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5428 d1c1ae5f 2019-08-12 stsp
5429 d1c1ae5f 2019-08-12 stsp /*
5430 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5431 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5432 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5433 d1c1ae5f 2019-08-12 stsp */
5434 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5435 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5436 d1c1ae5f 2019-08-12 stsp
5437 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5438 d1c1ae5f 2019-08-12 stsp if (err)
5439 d1c1ae5f 2019-08-12 stsp return err;
5440 d1c1ae5f 2019-08-12 stsp
5441 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5442 d1c1ae5f 2019-08-12 stsp if (err)
5443 d1c1ae5f 2019-08-12 stsp goto done;
5444 d1c1ae5f 2019-08-12 stsp
5445 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5446 d1c1ae5f 2019-08-12 stsp done:
5447 d1c1ae5f 2019-08-12 stsp if (target_ref)
5448 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5449 d1c1ae5f 2019-08-12 stsp if (ref)
5450 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5451 d0eebce4 2019-03-11 stsp return err;
5452 d0eebce4 2019-03-11 stsp }
5453 d0eebce4 2019-03-11 stsp
5454 d0eebce4 2019-03-11 stsp static const struct got_error *
5455 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5456 d0eebce4 2019-03-11 stsp {
5457 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5458 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5459 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5460 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5461 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5462 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5463 b2070a3f 2020-03-22 stsp char *refname = NULL;
5464 d0eebce4 2019-03-11 stsp
5465 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5466 d0eebce4 2019-03-11 stsp switch (ch) {
5467 e31abbf2 2020-03-22 stsp case 'c':
5468 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5469 e31abbf2 2020-03-22 stsp break;
5470 d0eebce4 2019-03-11 stsp case 'd':
5471 e31abbf2 2020-03-22 stsp do_delete = 1;
5472 d0eebce4 2019-03-11 stsp break;
5473 d0eebce4 2019-03-11 stsp case 'r':
5474 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5475 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5476 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5477 9ba1d308 2019-10-21 stsp optarg);
5478 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5479 d0eebce4 2019-03-11 stsp break;
5480 d0eebce4 2019-03-11 stsp case 'l':
5481 d0eebce4 2019-03-11 stsp do_list = 1;
5482 d1c1ae5f 2019-08-12 stsp break;
5483 d1c1ae5f 2019-08-12 stsp case 's':
5484 e31abbf2 2020-03-22 stsp symref_target = optarg;
5485 d0eebce4 2019-03-11 stsp break;
5486 d0eebce4 2019-03-11 stsp default:
5487 d0eebce4 2019-03-11 stsp usage_ref();
5488 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5489 d0eebce4 2019-03-11 stsp }
5490 d0eebce4 2019-03-11 stsp }
5491 d0eebce4 2019-03-11 stsp
5492 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5493 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5494 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5495 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5496 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5497 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5498 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5499 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5500 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5501 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5502 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5503 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5504 d0eebce4 2019-03-11 stsp
5505 d0eebce4 2019-03-11 stsp argc -= optind;
5506 d0eebce4 2019-03-11 stsp argv += optind;
5507 d0eebce4 2019-03-11 stsp
5508 e31abbf2 2020-03-22 stsp if (do_list) {
5509 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5510 d0eebce4 2019-03-11 stsp usage_ref();
5511 b2070a3f 2020-03-22 stsp if (argc == 1) {
5512 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5513 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5514 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5515 b2070a3f 2020-03-22 stsp goto done;
5516 b2070a3f 2020-03-22 stsp }
5517 b2070a3f 2020-03-22 stsp }
5518 e31abbf2 2020-03-22 stsp } else {
5519 e31abbf2 2020-03-22 stsp if (argc != 1)
5520 e31abbf2 2020-03-22 stsp usage_ref();
5521 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5522 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5523 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5524 b2070a3f 2020-03-22 stsp goto done;
5525 b2070a3f 2020-03-22 stsp }
5526 e31abbf2 2020-03-22 stsp }
5527 b2070a3f 2020-03-22 stsp
5528 b2070a3f 2020-03-22 stsp if (refname)
5529 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5530 d0eebce4 2019-03-11 stsp
5531 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5532 e0b57350 2019-03-12 stsp if (do_list) {
5533 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5534 e0b57350 2019-03-12 stsp NULL) == -1)
5535 e0b57350 2019-03-12 stsp err(1, "pledge");
5536 e0b57350 2019-03-12 stsp } else {
5537 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5538 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5539 e0b57350 2019-03-12 stsp err(1, "pledge");
5540 e0b57350 2019-03-12 stsp }
5541 d0eebce4 2019-03-11 stsp #endif
5542 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5543 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5544 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5545 d0eebce4 2019-03-11 stsp goto done;
5546 d0eebce4 2019-03-11 stsp }
5547 d0eebce4 2019-03-11 stsp
5548 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5549 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5550 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5551 d0eebce4 2019-03-11 stsp goto done;
5552 d0eebce4 2019-03-11 stsp else
5553 d0eebce4 2019-03-11 stsp error = NULL;
5554 d0eebce4 2019-03-11 stsp if (worktree) {
5555 d0eebce4 2019-03-11 stsp repo_path =
5556 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5557 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5558 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5559 d0eebce4 2019-03-11 stsp if (error)
5560 d0eebce4 2019-03-11 stsp goto done;
5561 d0eebce4 2019-03-11 stsp } else {
5562 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5563 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5564 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5565 d0eebce4 2019-03-11 stsp goto done;
5566 d0eebce4 2019-03-11 stsp }
5567 d0eebce4 2019-03-11 stsp }
5568 d0eebce4 2019-03-11 stsp }
5569 d0eebce4 2019-03-11 stsp
5570 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5571 d0eebce4 2019-03-11 stsp if (error != NULL)
5572 d0eebce4 2019-03-11 stsp goto done;
5573 d0eebce4 2019-03-11 stsp
5574 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5575 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5576 c02c541e 2019-03-29 stsp if (error)
5577 c02c541e 2019-03-29 stsp goto done;
5578 c02c541e 2019-03-29 stsp
5579 d0eebce4 2019-03-11 stsp if (do_list)
5580 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5581 e31abbf2 2020-03-22 stsp else if (do_delete)
5582 161728eb 2021-07-24 stsp error = delete_ref_by_name(repo, refname);
5583 e31abbf2 2020-03-22 stsp else if (symref_target)
5584 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5585 e31abbf2 2020-03-22 stsp else {
5586 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5587 e31abbf2 2020-03-22 stsp usage_ref();
5588 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5589 e31abbf2 2020-03-22 stsp }
5590 4e759de4 2019-06-26 stsp done:
5591 b2070a3f 2020-03-22 stsp free(refname);
5592 1d0f4054 2021-06-17 stsp if (repo) {
5593 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5594 1d0f4054 2021-06-17 stsp if (error == NULL)
5595 1d0f4054 2021-06-17 stsp error = close_err;
5596 1d0f4054 2021-06-17 stsp }
5597 4e759de4 2019-06-26 stsp if (worktree)
5598 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5599 4e759de4 2019-06-26 stsp free(cwd);
5600 4e759de4 2019-06-26 stsp free(repo_path);
5601 4e759de4 2019-06-26 stsp return error;
5602 4e759de4 2019-06-26 stsp }
5603 4e759de4 2019-06-26 stsp
5604 4e759de4 2019-06-26 stsp __dead static void
5605 4e759de4 2019-06-26 stsp usage_branch(void)
5606 4e759de4 2019-06-26 stsp {
5607 4e759de4 2019-06-26 stsp fprintf(stderr,
5608 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5609 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5610 4e759de4 2019-06-26 stsp exit(1);
5611 4e759de4 2019-06-26 stsp }
5612 4e759de4 2019-06-26 stsp
5613 4e759de4 2019-06-26 stsp static const struct got_error *
5614 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5615 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5616 4e99b47e 2019-10-04 stsp {
5617 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5618 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5619 4e99b47e 2019-10-04 stsp char *refstr;
5620 4e99b47e 2019-10-04 stsp
5621 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5622 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5623 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5624 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5625 4e99b47e 2019-10-04 stsp
5626 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5627 4e99b47e 2019-10-04 stsp if (err)
5628 4e99b47e 2019-10-04 stsp return err;
5629 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5630 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5631 4e99b47e 2019-10-04 stsp marker = "* ";
5632 4e99b47e 2019-10-04 stsp else
5633 4e99b47e 2019-10-04 stsp marker = "~ ";
5634 4e99b47e 2019-10-04 stsp free(id);
5635 4e99b47e 2019-10-04 stsp }
5636 4e99b47e 2019-10-04 stsp
5637 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5638 4e99b47e 2019-10-04 stsp refname += 11;
5639 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5640 4e99b47e 2019-10-04 stsp refname += 18;
5641 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5642 34d4e04c 2021-02-08 stsp refname += 13;
5643 4e99b47e 2019-10-04 stsp
5644 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5645 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5646 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5647 4e99b47e 2019-10-04 stsp
5648 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5649 4e99b47e 2019-10-04 stsp free(refstr);
5650 4e99b47e 2019-10-04 stsp return NULL;
5651 4e99b47e 2019-10-04 stsp }
5652 4e99b47e 2019-10-04 stsp
5653 4e99b47e 2019-10-04 stsp static const struct got_error *
5654 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5655 ad89fa31 2019-10-04 stsp {
5656 ad89fa31 2019-10-04 stsp const char *refname;
5657 ad89fa31 2019-10-04 stsp
5658 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5659 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5660 ad89fa31 2019-10-04 stsp
5661 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5662 ad89fa31 2019-10-04 stsp
5663 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5664 ad89fa31 2019-10-04 stsp refname += 11;
5665 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5666 ad89fa31 2019-10-04 stsp refname += 18;
5667 ad89fa31 2019-10-04 stsp
5668 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5669 ad89fa31 2019-10-04 stsp
5670 ad89fa31 2019-10-04 stsp return NULL;
5671 ad89fa31 2019-10-04 stsp }
5672 ad89fa31 2019-10-04 stsp
5673 ad89fa31 2019-10-04 stsp static const struct got_error *
5674 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5675 4e759de4 2019-06-26 stsp {
5676 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5677 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5678 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5679 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5680 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5681 4e759de4 2019-06-26 stsp
5682 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5683 ba882ee3 2019-07-11 stsp
5684 4e99b47e 2019-10-04 stsp if (worktree) {
5685 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5686 4e99b47e 2019-10-04 stsp worktree);
5687 4e99b47e 2019-10-04 stsp if (err)
5688 4e99b47e 2019-10-04 stsp return err;
5689 4e99b47e 2019-10-04 stsp
5690 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5691 4e99b47e 2019-10-04 stsp worktree);
5692 4e99b47e 2019-10-04 stsp if (err)
5693 4e99b47e 2019-10-04 stsp return err;
5694 4e99b47e 2019-10-04 stsp
5695 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5696 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5697 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5698 4e99b47e 2019-10-04 stsp if (err)
5699 4e99b47e 2019-10-04 stsp return err;
5700 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5701 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5702 4e99b47e 2019-10-04 stsp }
5703 4e99b47e 2019-10-04 stsp }
5704 4e99b47e 2019-10-04 stsp
5705 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5706 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5707 4e759de4 2019-06-26 stsp if (err)
5708 4e759de4 2019-06-26 stsp return err;
5709 4e759de4 2019-06-26 stsp
5710 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5711 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5712 4e759de4 2019-06-26 stsp
5713 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5714 34d4e04c 2021-02-08 stsp
5715 34d4e04c 2021-02-08 stsp err = got_ref_list(&refs, repo, "refs/remotes",
5716 34d4e04c 2021-02-08 stsp got_ref_cmp_by_name, NULL);
5717 34d4e04c 2021-02-08 stsp if (err)
5718 34d4e04c 2021-02-08 stsp return err;
5719 34d4e04c 2021-02-08 stsp
5720 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
5721 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
5722 34d4e04c 2021-02-08 stsp
5723 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
5724 34d4e04c 2021-02-08 stsp
5725 4e759de4 2019-06-26 stsp return NULL;
5726 4e759de4 2019-06-26 stsp }
5727 4e759de4 2019-06-26 stsp
5728 4e759de4 2019-06-26 stsp static const struct got_error *
5729 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5730 45cd4e47 2019-08-25 stsp const char *branch_name)
5731 4e759de4 2019-06-26 stsp {
5732 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5733 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5734 2f1457c6 2021-08-27 stsp char *refname, *remote_refname = NULL;
5735 4e759de4 2019-06-26 stsp
5736 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/", 5) == 0)
5737 2f1457c6 2021-08-27 stsp branch_name += 5;
5738 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "heads/", 6) == 0)
5739 2f1457c6 2021-08-27 stsp branch_name += 6;
5740 2f1457c6 2021-08-27 stsp else if (strncmp(branch_name, "remotes/", 8) == 0)
5741 2f1457c6 2021-08-27 stsp branch_name += 8;
5742 2f1457c6 2021-08-27 stsp
5743 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5744 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5745 4e759de4 2019-06-26 stsp
5746 2f1457c6 2021-08-27 stsp if (asprintf(&remote_refname, "refs/remotes/%s",
5747 2f1457c6 2021-08-27 stsp branch_name) == -1) {
5748 2f1457c6 2021-08-27 stsp err = got_error_from_errno("asprintf");
5749 4e759de4 2019-06-26 stsp goto done;
5750 2f1457c6 2021-08-27 stsp }
5751 4e759de4 2019-06-26 stsp
5752 2f1457c6 2021-08-27 stsp err = got_ref_open(&ref, repo, refname, 0);
5753 2f1457c6 2021-08-27 stsp if (err) {
5754 2f1457c6 2021-08-27 stsp const struct got_error *err2;
5755 2f1457c6 2021-08-27 stsp if (err->code != GOT_ERR_NOT_REF)
5756 2f1457c6 2021-08-27 stsp goto done;
5757 2f1457c6 2021-08-27 stsp /*
5758 2f1457c6 2021-08-27 stsp * Keep 'err' intact such that if neither branch exists
5759 2f1457c6 2021-08-27 stsp * we report "refs/heads" rather than "refs/remotes" in
5760 2f1457c6 2021-08-27 stsp * our error message.
5761 2f1457c6 2021-08-27 stsp */
5762 2f1457c6 2021-08-27 stsp err2 = got_ref_open(&ref, repo, remote_refname, 0);
5763 2f1457c6 2021-08-27 stsp if (err2)
5764 2f1457c6 2021-08-27 stsp goto done;
5765 2f1457c6 2021-08-27 stsp err = NULL;
5766 2f1457c6 2021-08-27 stsp }
5767 2f1457c6 2021-08-27 stsp
5768 45cd4e47 2019-08-25 stsp if (worktree &&
5769 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5770 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5771 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5772 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5773 45cd4e47 2019-08-25 stsp goto done;
5774 45cd4e47 2019-08-25 stsp }
5775 45cd4e47 2019-08-25 stsp
5776 978a28a1 2021-09-04 naddy err = delete_ref(repo, ref);
5777 4e759de4 2019-06-26 stsp done:
5778 45cd4e47 2019-08-25 stsp if (ref)
5779 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5780 4e759de4 2019-06-26 stsp free(refname);
5781 2f1457c6 2021-08-27 stsp free(remote_refname);
5782 4e759de4 2019-06-26 stsp return err;
5783 4e759de4 2019-06-26 stsp }
5784 4e759de4 2019-06-26 stsp
5785 4e759de4 2019-06-26 stsp static const struct got_error *
5786 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5787 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5788 4e759de4 2019-06-26 stsp {
5789 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5790 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5791 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5792 d3f84d51 2019-07-11 stsp
5793 d3f84d51 2019-07-11 stsp /*
5794 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5795 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5796 d3f84d51 2019-07-11 stsp * an unintended typo.
5797 d3f84d51 2019-07-11 stsp */
5798 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5799 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5800 2f1457c6 2021-08-27 stsp
5801 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
5802 2f1457c6 2021-08-27 stsp branch_name += 11;
5803 4e759de4 2019-06-26 stsp
5804 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5805 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5806 62d463ca 2020-10-20 naddy goto done;
5807 4e759de4 2019-06-26 stsp }
5808 4e759de4 2019-06-26 stsp
5809 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5810 4e759de4 2019-06-26 stsp if (err == NULL) {
5811 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5812 4e759de4 2019-06-26 stsp goto done;
5813 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5814 4e759de4 2019-06-26 stsp goto done;
5815 4e759de4 2019-06-26 stsp
5816 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5817 4e759de4 2019-06-26 stsp if (err)
5818 4e759de4 2019-06-26 stsp goto done;
5819 4e759de4 2019-06-26 stsp
5820 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5821 d0eebce4 2019-03-11 stsp done:
5822 4e759de4 2019-06-26 stsp if (ref)
5823 4e759de4 2019-06-26 stsp got_ref_close(ref);
5824 4e759de4 2019-06-26 stsp free(base_refname);
5825 4e759de4 2019-06-26 stsp free(refname);
5826 4e759de4 2019-06-26 stsp return err;
5827 4e759de4 2019-06-26 stsp }
5828 4e759de4 2019-06-26 stsp
5829 4e759de4 2019-06-26 stsp static const struct got_error *
5830 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5831 4e759de4 2019-06-26 stsp {
5832 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5833 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5834 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5835 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5836 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5837 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5838 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5839 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5840 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5841 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5842 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5843 4e759de4 2019-06-26 stsp
5844 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5845 da76fce2 2020-02-24 stsp
5846 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5847 4e759de4 2019-06-26 stsp switch (ch) {
5848 a74f7e83 2019-11-10 stsp case 'c':
5849 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5850 a74f7e83 2019-11-10 stsp break;
5851 4e759de4 2019-06-26 stsp case 'd':
5852 4e759de4 2019-06-26 stsp delref = optarg;
5853 4e759de4 2019-06-26 stsp break;
5854 4e759de4 2019-06-26 stsp case 'r':
5855 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5856 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5857 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5858 9ba1d308 2019-10-21 stsp optarg);
5859 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5860 4e759de4 2019-06-26 stsp break;
5861 4e759de4 2019-06-26 stsp case 'l':
5862 4e759de4 2019-06-26 stsp do_list = 1;
5863 da76fce2 2020-02-24 stsp break;
5864 da76fce2 2020-02-24 stsp case 'n':
5865 da76fce2 2020-02-24 stsp do_update = 0;
5866 4e759de4 2019-06-26 stsp break;
5867 4e759de4 2019-06-26 stsp default:
5868 4e759de4 2019-06-26 stsp usage_branch();
5869 4e759de4 2019-06-26 stsp /* NOTREACHED */
5870 4e759de4 2019-06-26 stsp }
5871 4e759de4 2019-06-26 stsp }
5872 4e759de4 2019-06-26 stsp
5873 4e759de4 2019-06-26 stsp if (do_list && delref)
5874 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
5875 4e759de4 2019-06-26 stsp
5876 4e759de4 2019-06-26 stsp argc -= optind;
5877 4e759de4 2019-06-26 stsp argv += optind;
5878 ad89fa31 2019-10-04 stsp
5879 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5880 ad89fa31 2019-10-04 stsp do_show = 1;
5881 4e759de4 2019-06-26 stsp
5882 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5883 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5884 a74f7e83 2019-11-10 stsp
5885 4e759de4 2019-06-26 stsp if (do_list || delref) {
5886 4e759de4 2019-06-26 stsp if (argc > 0)
5887 4e759de4 2019-06-26 stsp usage_branch();
5888 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5889 4e759de4 2019-06-26 stsp usage_branch();
5890 4e759de4 2019-06-26 stsp
5891 4e759de4 2019-06-26 stsp #ifndef PROFILE
5892 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5893 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5894 4e759de4 2019-06-26 stsp NULL) == -1)
5895 4e759de4 2019-06-26 stsp err(1, "pledge");
5896 4e759de4 2019-06-26 stsp } else {
5897 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5898 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5899 4e759de4 2019-06-26 stsp err(1, "pledge");
5900 4e759de4 2019-06-26 stsp }
5901 4e759de4 2019-06-26 stsp #endif
5902 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5903 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5904 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5905 4e759de4 2019-06-26 stsp goto done;
5906 4e759de4 2019-06-26 stsp }
5907 4e759de4 2019-06-26 stsp
5908 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5909 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5910 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5911 4e759de4 2019-06-26 stsp goto done;
5912 4e759de4 2019-06-26 stsp else
5913 4e759de4 2019-06-26 stsp error = NULL;
5914 4e759de4 2019-06-26 stsp if (worktree) {
5915 4e759de4 2019-06-26 stsp repo_path =
5916 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5917 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5918 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5919 4e759de4 2019-06-26 stsp if (error)
5920 4e759de4 2019-06-26 stsp goto done;
5921 4e759de4 2019-06-26 stsp } else {
5922 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5923 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5924 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5925 4e759de4 2019-06-26 stsp goto done;
5926 4e759de4 2019-06-26 stsp }
5927 4e759de4 2019-06-26 stsp }
5928 4e759de4 2019-06-26 stsp }
5929 4e759de4 2019-06-26 stsp
5930 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5931 4e759de4 2019-06-26 stsp if (error != NULL)
5932 4e759de4 2019-06-26 stsp goto done;
5933 4e759de4 2019-06-26 stsp
5934 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5935 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5936 4e759de4 2019-06-26 stsp if (error)
5937 4e759de4 2019-06-26 stsp goto done;
5938 4e759de4 2019-06-26 stsp
5939 ad89fa31 2019-10-04 stsp if (do_show)
5940 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5941 ad89fa31 2019-10-04 stsp else if (do_list)
5942 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5943 4e759de4 2019-06-26 stsp else if (delref)
5944 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5945 4e759de4 2019-06-26 stsp else {
5946 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5947 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5948 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5949 84de9106 2020-12-26 stsp NULL);
5950 84de9106 2020-12-26 stsp if (error)
5951 84de9106 2020-12-26 stsp goto done;
5952 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5953 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5954 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5955 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5956 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5957 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5958 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5959 a74f7e83 2019-11-10 stsp if (error)
5960 a74f7e83 2019-11-10 stsp goto done;
5961 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5962 da76fce2 2020-02-24 stsp if (error)
5963 da76fce2 2020-02-24 stsp goto done;
5964 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5965 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5966 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5967 da76fce2 2020-02-24 stsp
5968 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5969 da76fce2 2020-02-24 stsp if (error)
5970 da76fce2 2020-02-24 stsp goto done;
5971 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5972 da76fce2 2020-02-24 stsp worktree);
5973 da76fce2 2020-02-24 stsp if (error)
5974 da76fce2 2020-02-24 stsp goto done;
5975 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5976 da76fce2 2020-02-24 stsp == -1) {
5977 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5978 da76fce2 2020-02-24 stsp goto done;
5979 da76fce2 2020-02-24 stsp }
5980 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5981 da76fce2 2020-02-24 stsp free(branch_refname);
5982 da76fce2 2020-02-24 stsp if (error)
5983 da76fce2 2020-02-24 stsp goto done;
5984 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5985 da76fce2 2020-02-24 stsp repo);
5986 da76fce2 2020-02-24 stsp if (error)
5987 da76fce2 2020-02-24 stsp goto done;
5988 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5989 da76fce2 2020-02-24 stsp commit_id);
5990 da76fce2 2020-02-24 stsp if (error)
5991 da76fce2 2020-02-24 stsp goto done;
5992 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5993 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5994 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5995 9627c110 2020-04-18 stsp NULL);
5996 da76fce2 2020-02-24 stsp if (error)
5997 da76fce2 2020-02-24 stsp goto done;
5998 9627c110 2020-04-18 stsp if (upa.did_something)
5999 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
6000 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6001 da76fce2 2020-02-24 stsp }
6002 4e759de4 2019-06-26 stsp }
6003 4e759de4 2019-06-26 stsp done:
6004 da76fce2 2020-02-24 stsp if (ref)
6005 da76fce2 2020-02-24 stsp got_ref_close(ref);
6006 1d0f4054 2021-06-17 stsp if (repo) {
6007 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6008 1d0f4054 2021-06-17 stsp if (error == NULL)
6009 1d0f4054 2021-06-17 stsp error = close_err;
6010 1d0f4054 2021-06-17 stsp }
6011 d0eebce4 2019-03-11 stsp if (worktree)
6012 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
6013 d0eebce4 2019-03-11 stsp free(cwd);
6014 d0eebce4 2019-03-11 stsp free(repo_path);
6015 da76fce2 2020-02-24 stsp free(commit_id);
6016 da76fce2 2020-02-24 stsp free(commit_id_str);
6017 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
6018 da76fce2 2020-02-24 stsp free((char *)pe->path);
6019 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
6020 d00136be 2019-03-26 stsp return error;
6021 d00136be 2019-03-26 stsp }
6022 d00136be 2019-03-26 stsp
6023 8e7bd50a 2019-08-22 stsp
6024 d00136be 2019-03-26 stsp __dead static void
6025 8e7bd50a 2019-08-22 stsp usage_tag(void)
6026 8e7bd50a 2019-08-22 stsp {
6027 8e7bd50a 2019-08-22 stsp fprintf(stderr,
6028 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
6029 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
6030 8e7bd50a 2019-08-22 stsp exit(1);
6031 b8bad2ba 2019-08-23 stsp }
6032 b8bad2ba 2019-08-23 stsp
6033 b8bad2ba 2019-08-23 stsp #if 0
6034 b8bad2ba 2019-08-23 stsp static const struct got_error *
6035 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6036 b8bad2ba 2019-08-23 stsp {
6037 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
6038 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
6039 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
6040 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
6041 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
6042 b8bad2ba 2019-08-23 stsp
6043 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
6044 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
6045 b8bad2ba 2019-08-23 stsp if (se == NULL) {
6046 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6047 b8bad2ba 2019-08-23 stsp if (err)
6048 b8bad2ba 2019-08-23 stsp return err;
6049 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6050 b8bad2ba 2019-08-23 stsp continue;
6051 b8bad2ba 2019-08-23 stsp } else {
6052 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6053 b8bad2ba 2019-08-23 stsp if (err)
6054 b8bad2ba 2019-08-23 stsp break;
6055 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
6056 b8bad2ba 2019-08-23 stsp free(re_id);
6057 b8bad2ba 2019-08-23 stsp if (err)
6058 b8bad2ba 2019-08-23 stsp break;
6059 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
6060 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
6061 b8bad2ba 2019-08-23 stsp }
6062 b8bad2ba 2019-08-23 stsp
6063 b8bad2ba 2019-08-23 stsp while (se) {
6064 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6065 b8bad2ba 2019-08-23 stsp if (err)
6066 b8bad2ba 2019-08-23 stsp break;
6067 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
6068 b8bad2ba 2019-08-23 stsp free(se_id);
6069 b8bad2ba 2019-08-23 stsp if (err)
6070 b8bad2ba 2019-08-23 stsp break;
6071 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
6072 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
6073 b8bad2ba 2019-08-23 stsp
6074 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6075 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6076 b8bad2ba 2019-08-23 stsp if (err)
6077 b8bad2ba 2019-08-23 stsp return err;
6078 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6079 b8bad2ba 2019-08-23 stsp break;
6080 b8bad2ba 2019-08-23 stsp }
6081 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6082 b8bad2ba 2019-08-23 stsp continue;
6083 b8bad2ba 2019-08-23 stsp }
6084 b8bad2ba 2019-08-23 stsp }
6085 b8bad2ba 2019-08-23 stsp done:
6086 b8bad2ba 2019-08-23 stsp return err;
6087 8e7bd50a 2019-08-22 stsp }
6088 b8bad2ba 2019-08-23 stsp #endif
6089 b8bad2ba 2019-08-23 stsp
6090 b8bad2ba 2019-08-23 stsp static const struct got_error *
6091 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
6092 8e7bd50a 2019-08-22 stsp {
6093 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
6094 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
6095 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6096 8e7bd50a 2019-08-22 stsp
6097 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6098 8e7bd50a 2019-08-22 stsp
6099 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6100 8e7bd50a 2019-08-22 stsp if (err)
6101 8e7bd50a 2019-08-22 stsp return err;
6102 8e7bd50a 2019-08-22 stsp
6103 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6104 8e7bd50a 2019-08-22 stsp const char *refname;
6105 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6106 8e7bd50a 2019-08-22 stsp char datebuf[26];
6107 d4efa91b 2020-01-14 stsp const char *tagger;
6108 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6109 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6110 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6111 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6112 8e7bd50a 2019-08-22 stsp
6113 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6114 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6115 8e7bd50a 2019-08-22 stsp continue;
6116 8e7bd50a 2019-08-22 stsp refname += 10;
6117 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6118 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6119 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6120 8e7bd50a 2019-08-22 stsp break;
6121 8e7bd50a 2019-08-22 stsp }
6122 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6123 8e7bd50a 2019-08-22 stsp free(refstr);
6124 8e7bd50a 2019-08-22 stsp
6125 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6126 8e7bd50a 2019-08-22 stsp if (err)
6127 8e7bd50a 2019-08-22 stsp break;
6128 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6129 d4efa91b 2020-01-14 stsp if (err) {
6130 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6131 d4efa91b 2020-01-14 stsp free(id);
6132 d4efa91b 2020-01-14 stsp break;
6133 d4efa91b 2020-01-14 stsp }
6134 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6135 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6136 d4efa91b 2020-01-14 stsp if (err) {
6137 d4efa91b 2020-01-14 stsp free(id);
6138 d4efa91b 2020-01-14 stsp break;
6139 d4efa91b 2020-01-14 stsp }
6140 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6141 d4efa91b 2020-01-14 stsp tagger_time =
6142 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6143 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6144 d4efa91b 2020-01-14 stsp free(id);
6145 d4efa91b 2020-01-14 stsp if (err)
6146 d4efa91b 2020-01-14 stsp break;
6147 d4efa91b 2020-01-14 stsp } else {
6148 d4efa91b 2020-01-14 stsp free(id);
6149 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6150 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6151 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6152 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6153 d4efa91b 2020-01-14 stsp if (err)
6154 d4efa91b 2020-01-14 stsp break;
6155 d4efa91b 2020-01-14 stsp }
6156 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6157 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6158 2417344c 2019-08-23 stsp if (datestr)
6159 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6160 d4efa91b 2020-01-14 stsp if (commit)
6161 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6162 d4efa91b 2020-01-14 stsp else {
6163 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6164 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6165 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6166 d4efa91b 2020-01-14 stsp id_str);
6167 d4efa91b 2020-01-14 stsp break;
6168 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6169 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6170 d4efa91b 2020-01-14 stsp id_str);
6171 d4efa91b 2020-01-14 stsp break;
6172 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6173 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6174 d4efa91b 2020-01-14 stsp id_str);
6175 d4efa91b 2020-01-14 stsp break;
6176 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6177 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6178 d4efa91b 2020-01-14 stsp id_str);
6179 d4efa91b 2020-01-14 stsp break;
6180 d4efa91b 2020-01-14 stsp default:
6181 d4efa91b 2020-01-14 stsp break;
6182 d4efa91b 2020-01-14 stsp }
6183 8e7bd50a 2019-08-22 stsp }
6184 8e7bd50a 2019-08-22 stsp free(id_str);
6185 d4efa91b 2020-01-14 stsp if (commit) {
6186 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6187 d4efa91b 2020-01-14 stsp if (err)
6188 d4efa91b 2020-01-14 stsp break;
6189 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6190 d4efa91b 2020-01-14 stsp } else {
6191 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6192 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6193 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6194 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6195 d4efa91b 2020-01-14 stsp break;
6196 d4efa91b 2020-01-14 stsp }
6197 8e7bd50a 2019-08-22 stsp }
6198 8e7bd50a 2019-08-22 stsp
6199 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6200 8e7bd50a 2019-08-22 stsp do {
6201 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6202 8e7bd50a 2019-08-22 stsp if (line)
6203 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6204 8e7bd50a 2019-08-22 stsp } while (line);
6205 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6206 8e7bd50a 2019-08-22 stsp }
6207 8e7bd50a 2019-08-22 stsp
6208 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6209 8e7bd50a 2019-08-22 stsp return NULL;
6210 8e7bd50a 2019-08-22 stsp }
6211 8e7bd50a 2019-08-22 stsp
6212 8e7bd50a 2019-08-22 stsp static const struct got_error *
6213 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6214 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6215 8e7bd50a 2019-08-22 stsp {
6216 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6217 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6218 f372d5cd 2019-10-21 stsp char *editor = NULL;
6219 1601cb9f 2020-09-11 naddy int initial_content_len;
6220 8e7bd50a 2019-08-22 stsp int fd = -1;
6221 8e7bd50a 2019-08-22 stsp
6222 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6223 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6224 8e7bd50a 2019-08-22 stsp goto done;
6225 8e7bd50a 2019-08-22 stsp }
6226 8e7bd50a 2019-08-22 stsp
6227 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6228 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6229 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6230 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6231 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6232 8e7bd50a 2019-08-22 stsp goto done;
6233 8e7bd50a 2019-08-22 stsp }
6234 8e7bd50a 2019-08-22 stsp
6235 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6236 8e7bd50a 2019-08-22 stsp if (err)
6237 8e7bd50a 2019-08-22 stsp goto done;
6238 8e7bd50a 2019-08-22 stsp
6239 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6240 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6241 97972933 2020-09-11 stsp goto done;
6242 97972933 2020-09-11 stsp }
6243 8e7bd50a 2019-08-22 stsp
6244 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6245 8e7bd50a 2019-08-22 stsp if (err)
6246 8e7bd50a 2019-08-22 stsp goto done;
6247 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6248 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6249 8e7bd50a 2019-08-22 stsp done:
6250 8e7bd50a 2019-08-22 stsp free(initial_content);
6251 8e7bd50a 2019-08-22 stsp free(template);
6252 8e7bd50a 2019-08-22 stsp free(editor);
6253 8e7bd50a 2019-08-22 stsp
6254 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6255 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6256 97972933 2020-09-11 stsp
6257 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6258 59f86c76 2020-09-11 stsp if (err == NULL)
6259 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6260 59f86c76 2020-09-11 stsp if (err) {
6261 59f86c76 2020-09-11 stsp free(*tagmsg);
6262 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6263 8e7bd50a 2019-08-22 stsp }
6264 8e7bd50a 2019-08-22 stsp return err;
6265 8e7bd50a 2019-08-22 stsp }
6266 8e7bd50a 2019-08-22 stsp
6267 8e7bd50a 2019-08-22 stsp static const struct got_error *
6268 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6269 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6270 8e7bd50a 2019-08-22 stsp {
6271 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6272 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6273 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6274 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6275 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6276 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6277 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6278 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6279 8e7bd50a 2019-08-22 stsp
6280 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6281 84de9106 2020-12-26 stsp
6282 8e7bd50a 2019-08-22 stsp /*
6283 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6284 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6285 8e7bd50a 2019-08-22 stsp * an unintended typo.
6286 8e7bd50a 2019-08-22 stsp */
6287 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6288 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6289 8e7bd50a 2019-08-22 stsp
6290 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6291 8e7bd50a 2019-08-22 stsp if (err)
6292 8e7bd50a 2019-08-22 stsp return err;
6293 8e7bd50a 2019-08-22 stsp
6294 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6295 84de9106 2020-12-26 stsp if (err)
6296 84de9106 2020-12-26 stsp goto done;
6297 84de9106 2020-12-26 stsp
6298 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6299 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6300 8e7bd50a 2019-08-22 stsp if (err)
6301 8e7bd50a 2019-08-22 stsp goto done;
6302 8e7bd50a 2019-08-22 stsp
6303 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6304 8e7bd50a 2019-08-22 stsp if (err)
6305 8e7bd50a 2019-08-22 stsp goto done;
6306 8e7bd50a 2019-08-22 stsp
6307 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6308 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6309 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6310 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6311 62d463ca 2020-10-20 naddy goto done;
6312 8e7bd50a 2019-08-22 stsp }
6313 8e7bd50a 2019-08-22 stsp tag_name += 10;
6314 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6315 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6316 62d463ca 2020-10-20 naddy goto done;
6317 8e7bd50a 2019-08-22 stsp }
6318 8e7bd50a 2019-08-22 stsp
6319 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6320 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6321 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6322 8e7bd50a 2019-08-22 stsp goto done;
6323 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6324 8e7bd50a 2019-08-22 stsp goto done;
6325 8e7bd50a 2019-08-22 stsp
6326 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6327 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6328 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6329 f372d5cd 2019-10-21 stsp if (err) {
6330 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6331 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6332 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6333 8e7bd50a 2019-08-22 stsp goto done;
6334 f372d5cd 2019-10-21 stsp }
6335 8e7bd50a 2019-08-22 stsp }
6336 8e7bd50a 2019-08-22 stsp
6337 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6338 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6339 f372d5cd 2019-10-21 stsp if (err) {
6340 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6341 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6342 8e7bd50a 2019-08-22 stsp goto done;
6343 f372d5cd 2019-10-21 stsp }
6344 8e7bd50a 2019-08-22 stsp
6345 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6346 f372d5cd 2019-10-21 stsp if (err) {
6347 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6348 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6349 8e7bd50a 2019-08-22 stsp goto done;
6350 f372d5cd 2019-10-21 stsp }
6351 8e7bd50a 2019-08-22 stsp
6352 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6353 f372d5cd 2019-10-21 stsp if (err) {
6354 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6355 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6356 f372d5cd 2019-10-21 stsp goto done;
6357 f372d5cd 2019-10-21 stsp }
6358 8e7bd50a 2019-08-22 stsp
6359 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6360 f372d5cd 2019-10-21 stsp if (err) {
6361 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6362 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6363 f372d5cd 2019-10-21 stsp goto done;
6364 8e7bd50a 2019-08-22 stsp }
6365 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6366 8e7bd50a 2019-08-22 stsp done:
6367 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6368 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6369 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6370 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6371 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6372 f372d5cd 2019-10-21 stsp free(tag_id_str);
6373 8e7bd50a 2019-08-22 stsp if (ref)
6374 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6375 8e7bd50a 2019-08-22 stsp free(commit_id);
6376 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6377 8e7bd50a 2019-08-22 stsp free(refname);
6378 8e7bd50a 2019-08-22 stsp free(tagmsg);
6379 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6380 aba9c984 2019-09-08 stsp free(tagger);
6381 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6382 8e7bd50a 2019-08-22 stsp return err;
6383 8e7bd50a 2019-08-22 stsp }
6384 8e7bd50a 2019-08-22 stsp
6385 8e7bd50a 2019-08-22 stsp static const struct got_error *
6386 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6387 8e7bd50a 2019-08-22 stsp {
6388 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6389 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6390 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6391 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6392 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6393 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6394 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6395 8e7bd50a 2019-08-22 stsp
6396 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6397 8e7bd50a 2019-08-22 stsp switch (ch) {
6398 80106605 2020-02-24 stsp case 'c':
6399 80106605 2020-02-24 stsp commit_id_arg = optarg;
6400 80106605 2020-02-24 stsp break;
6401 8e7bd50a 2019-08-22 stsp case 'm':
6402 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6403 8e7bd50a 2019-08-22 stsp break;
6404 8e7bd50a 2019-08-22 stsp case 'r':
6405 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6406 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6407 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6408 9ba1d308 2019-10-21 stsp optarg);
6409 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6410 8e7bd50a 2019-08-22 stsp break;
6411 8e7bd50a 2019-08-22 stsp case 'l':
6412 8e7bd50a 2019-08-22 stsp do_list = 1;
6413 8e7bd50a 2019-08-22 stsp break;
6414 8e7bd50a 2019-08-22 stsp default:
6415 8e7bd50a 2019-08-22 stsp usage_tag();
6416 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6417 8e7bd50a 2019-08-22 stsp }
6418 8e7bd50a 2019-08-22 stsp }
6419 8e7bd50a 2019-08-22 stsp
6420 8e7bd50a 2019-08-22 stsp argc -= optind;
6421 8e7bd50a 2019-08-22 stsp argv += optind;
6422 8e7bd50a 2019-08-22 stsp
6423 8e7bd50a 2019-08-22 stsp if (do_list) {
6424 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6425 775ce909 2020-03-22 stsp errx(1,
6426 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6427 8e7bd50a 2019-08-22 stsp if (tagmsg)
6428 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6429 8e7bd50a 2019-08-22 stsp if (argc > 0)
6430 8e7bd50a 2019-08-22 stsp usage_tag();
6431 80106605 2020-02-24 stsp } else if (argc != 1)
6432 8e7bd50a 2019-08-22 stsp usage_tag();
6433 80106605 2020-02-24 stsp
6434 a2887370 2019-08-23 stsp tag_name = argv[0];
6435 8e7bd50a 2019-08-22 stsp
6436 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6437 8e7bd50a 2019-08-22 stsp if (do_list) {
6438 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6439 8e7bd50a 2019-08-22 stsp NULL) == -1)
6440 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6441 8e7bd50a 2019-08-22 stsp } else {
6442 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6443 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6444 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6445 8e7bd50a 2019-08-22 stsp }
6446 8e7bd50a 2019-08-22 stsp #endif
6447 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6448 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6449 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6450 8e7bd50a 2019-08-22 stsp goto done;
6451 8e7bd50a 2019-08-22 stsp }
6452 8e7bd50a 2019-08-22 stsp
6453 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6454 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6455 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6456 8e7bd50a 2019-08-22 stsp goto done;
6457 8e7bd50a 2019-08-22 stsp else
6458 8e7bd50a 2019-08-22 stsp error = NULL;
6459 8e7bd50a 2019-08-22 stsp if (worktree) {
6460 8e7bd50a 2019-08-22 stsp repo_path =
6461 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6462 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6463 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6464 8e7bd50a 2019-08-22 stsp if (error)
6465 8e7bd50a 2019-08-22 stsp goto done;
6466 8e7bd50a 2019-08-22 stsp } else {
6467 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6468 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6469 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6470 8e7bd50a 2019-08-22 stsp goto done;
6471 8e7bd50a 2019-08-22 stsp }
6472 8e7bd50a 2019-08-22 stsp }
6473 8e7bd50a 2019-08-22 stsp }
6474 8e7bd50a 2019-08-22 stsp
6475 8e7bd50a 2019-08-22 stsp if (do_list) {
6476 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6477 c9956ddf 2019-09-08 stsp if (error != NULL)
6478 c9956ddf 2019-09-08 stsp goto done;
6479 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6480 8e7bd50a 2019-08-22 stsp if (error)
6481 8e7bd50a 2019-08-22 stsp goto done;
6482 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6483 8e7bd50a 2019-08-22 stsp } else {
6484 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6485 c9956ddf 2019-09-08 stsp if (error)
6486 c9956ddf 2019-09-08 stsp goto done;
6487 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6488 c9956ddf 2019-09-08 stsp if (error != NULL)
6489 c9956ddf 2019-09-08 stsp goto done;
6490 c9956ddf 2019-09-08 stsp
6491 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6492 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6493 8e7bd50a 2019-08-22 stsp if (error)
6494 8e7bd50a 2019-08-22 stsp goto done;
6495 8e7bd50a 2019-08-22 stsp }
6496 8e7bd50a 2019-08-22 stsp
6497 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6498 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6499 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6500 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6501 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6502 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6503 8e7bd50a 2019-08-22 stsp if (error)
6504 8e7bd50a 2019-08-22 stsp goto done;
6505 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6506 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6507 8e7bd50a 2019-08-22 stsp if (error)
6508 8e7bd50a 2019-08-22 stsp goto done;
6509 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6510 8e7bd50a 2019-08-22 stsp free(commit_id);
6511 8e7bd50a 2019-08-22 stsp if (error)
6512 8e7bd50a 2019-08-22 stsp goto done;
6513 8e7bd50a 2019-08-22 stsp }
6514 8e7bd50a 2019-08-22 stsp
6515 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6516 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6517 8e7bd50a 2019-08-22 stsp }
6518 8e7bd50a 2019-08-22 stsp done:
6519 1d0f4054 2021-06-17 stsp if (repo) {
6520 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6521 1d0f4054 2021-06-17 stsp if (error == NULL)
6522 1d0f4054 2021-06-17 stsp error = close_err;
6523 1d0f4054 2021-06-17 stsp }
6524 8e7bd50a 2019-08-22 stsp if (worktree)
6525 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6526 8e7bd50a 2019-08-22 stsp free(cwd);
6527 8e7bd50a 2019-08-22 stsp free(repo_path);
6528 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6529 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6530 8e7bd50a 2019-08-22 stsp return error;
6531 8e7bd50a 2019-08-22 stsp }
6532 8e7bd50a 2019-08-22 stsp
6533 8e7bd50a 2019-08-22 stsp __dead static void
6534 d00136be 2019-03-26 stsp usage_add(void)
6535 d00136be 2019-03-26 stsp {
6536 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6537 022fae89 2019-12-06 tracey getprogname());
6538 d00136be 2019-03-26 stsp exit(1);
6539 d00136be 2019-03-26 stsp }
6540 d00136be 2019-03-26 stsp
6541 d00136be 2019-03-26 stsp static const struct got_error *
6542 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6543 4e68cba3 2019-11-23 stsp {
6544 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6545 4e68cba3 2019-11-23 stsp path++;
6546 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6547 4e68cba3 2019-11-23 stsp return NULL;
6548 4e68cba3 2019-11-23 stsp }
6549 4e68cba3 2019-11-23 stsp
6550 4e68cba3 2019-11-23 stsp static const struct got_error *
6551 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6552 d00136be 2019-03-26 stsp {
6553 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6554 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6555 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6556 1dd54920 2019-05-11 stsp char *cwd = NULL;
6557 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6558 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6559 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6560 1dd54920 2019-05-11 stsp
6561 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6562 d00136be 2019-03-26 stsp
6563 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6564 d00136be 2019-03-26 stsp switch (ch) {
6565 022fae89 2019-12-06 tracey case 'I':
6566 022fae89 2019-12-06 tracey no_ignores = 1;
6567 022fae89 2019-12-06 tracey break;
6568 4e68cba3 2019-11-23 stsp case 'R':
6569 4e68cba3 2019-11-23 stsp can_recurse = 1;
6570 4e68cba3 2019-11-23 stsp break;
6571 d00136be 2019-03-26 stsp default:
6572 d00136be 2019-03-26 stsp usage_add();
6573 d00136be 2019-03-26 stsp /* NOTREACHED */
6574 d00136be 2019-03-26 stsp }
6575 d00136be 2019-03-26 stsp }
6576 d00136be 2019-03-26 stsp
6577 d00136be 2019-03-26 stsp argc -= optind;
6578 d00136be 2019-03-26 stsp argv += optind;
6579 d00136be 2019-03-26 stsp
6580 43012d58 2019-07-14 stsp #ifndef PROFILE
6581 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6582 43012d58 2019-07-14 stsp NULL) == -1)
6583 43012d58 2019-07-14 stsp err(1, "pledge");
6584 43012d58 2019-07-14 stsp #endif
6585 723c305c 2019-05-11 jcs if (argc < 1)
6586 d00136be 2019-03-26 stsp usage_add();
6587 d00136be 2019-03-26 stsp
6588 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6589 d00136be 2019-03-26 stsp if (cwd == NULL) {
6590 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6591 d00136be 2019-03-26 stsp goto done;
6592 d00136be 2019-03-26 stsp }
6593 723c305c 2019-05-11 jcs
6594 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6595 fa51e947 2020-03-27 stsp if (error) {
6596 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6597 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6598 d00136be 2019-03-26 stsp goto done;
6599 fa51e947 2020-03-27 stsp }
6600 d00136be 2019-03-26 stsp
6601 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6602 c9956ddf 2019-09-08 stsp NULL);
6603 031a5338 2019-03-26 stsp if (error != NULL)
6604 031a5338 2019-03-26 stsp goto done;
6605 031a5338 2019-03-26 stsp
6606 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6607 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6608 d00136be 2019-03-26 stsp if (error)
6609 d00136be 2019-03-26 stsp goto done;
6610 d00136be 2019-03-26 stsp
6611 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6612 6d022e97 2019-08-04 stsp if (error)
6613 022fae89 2019-12-06 tracey goto done;
6614 022fae89 2019-12-06 tracey
6615 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6616 4e68cba3 2019-11-23 stsp char *ondisk_path;
6617 4e68cba3 2019-11-23 stsp struct stat sb;
6618 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6619 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6620 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6621 62d463ca 2020-10-20 naddy pe->path) == -1) {
6622 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6623 4e68cba3 2019-11-23 stsp goto done;
6624 4e68cba3 2019-11-23 stsp }
6625 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6626 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6627 4e68cba3 2019-11-23 stsp free(ondisk_path);
6628 4e68cba3 2019-11-23 stsp continue;
6629 4e68cba3 2019-11-23 stsp }
6630 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6631 4e68cba3 2019-11-23 stsp ondisk_path);
6632 4e68cba3 2019-11-23 stsp free(ondisk_path);
6633 4e68cba3 2019-11-23 stsp goto done;
6634 4e68cba3 2019-11-23 stsp }
6635 4e68cba3 2019-11-23 stsp free(ondisk_path);
6636 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6637 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6638 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6639 4e68cba3 2019-11-23 stsp goto done;
6640 4e68cba3 2019-11-23 stsp }
6641 4e68cba3 2019-11-23 stsp }
6642 4e68cba3 2019-11-23 stsp }
6643 022fae89 2019-12-06 tracey
6644 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6645 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6646 d00136be 2019-03-26 stsp done:
6647 1d0f4054 2021-06-17 stsp if (repo) {
6648 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6649 1d0f4054 2021-06-17 stsp if (error == NULL)
6650 1d0f4054 2021-06-17 stsp error = close_err;
6651 1d0f4054 2021-06-17 stsp }
6652 d00136be 2019-03-26 stsp if (worktree)
6653 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6654 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6655 1dd54920 2019-05-11 stsp free((char *)pe->path);
6656 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6657 2ec1f75b 2019-03-26 stsp free(cwd);
6658 2ec1f75b 2019-03-26 stsp return error;
6659 2ec1f75b 2019-03-26 stsp }
6660 2ec1f75b 2019-03-26 stsp
6661 2ec1f75b 2019-03-26 stsp __dead static void
6662 648e4ef7 2019-07-09 stsp usage_remove(void)
6663 2ec1f75b 2019-03-26 stsp {
6664 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6665 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6666 2ec1f75b 2019-03-26 stsp exit(1);
6667 2a06fe5f 2019-08-24 stsp }
6668 2a06fe5f 2019-08-24 stsp
6669 2a06fe5f 2019-08-24 stsp static const struct got_error *
6670 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6671 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6672 2a06fe5f 2019-08-24 stsp {
6673 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6674 f2a9dc41 2019-12-13 tracey path++;
6675 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6676 2a06fe5f 2019-08-24 stsp return NULL;
6677 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6678 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6679 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6680 2a06fe5f 2019-08-24 stsp return NULL;
6681 2ec1f75b 2019-03-26 stsp }
6682 2ec1f75b 2019-03-26 stsp
6683 2ec1f75b 2019-03-26 stsp static const struct got_error *
6684 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6685 2ec1f75b 2019-03-26 stsp {
6686 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6687 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6688 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6689 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6690 17ed4618 2019-06-02 stsp char *cwd = NULL;
6691 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6692 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6693 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6694 2ec1f75b 2019-03-26 stsp
6695 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6696 17ed4618 2019-06-02 stsp
6697 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6698 2ec1f75b 2019-03-26 stsp switch (ch) {
6699 2ec1f75b 2019-03-26 stsp case 'f':
6700 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6701 2ec1f75b 2019-03-26 stsp break;
6702 70e3e7f5 2019-12-13 tracey case 'k':
6703 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6704 70e3e7f5 2019-12-13 tracey break;
6705 f2a9dc41 2019-12-13 tracey case 'R':
6706 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6707 f2a9dc41 2019-12-13 tracey break;
6708 766841c2 2020-08-13 stsp case 's':
6709 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6710 766841c2 2020-08-13 stsp switch (optarg[i]) {
6711 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6712 766841c2 2020-08-13 stsp delete_local_mods = 1;
6713 766841c2 2020-08-13 stsp break;
6714 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6715 766841c2 2020-08-13 stsp break;
6716 766841c2 2020-08-13 stsp default:
6717 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6718 766841c2 2020-08-13 stsp optarg[i]);
6719 766841c2 2020-08-13 stsp }
6720 766841c2 2020-08-13 stsp }
6721 766841c2 2020-08-13 stsp status_codes = optarg;
6722 766841c2 2020-08-13 stsp break;
6723 2ec1f75b 2019-03-26 stsp default:
6724 f2a9dc41 2019-12-13 tracey usage_remove();
6725 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6726 2ec1f75b 2019-03-26 stsp }
6727 2ec1f75b 2019-03-26 stsp }
6728 2ec1f75b 2019-03-26 stsp
6729 2ec1f75b 2019-03-26 stsp argc -= optind;
6730 2ec1f75b 2019-03-26 stsp argv += optind;
6731 2ec1f75b 2019-03-26 stsp
6732 43012d58 2019-07-14 stsp #ifndef PROFILE
6733 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6734 43012d58 2019-07-14 stsp NULL) == -1)
6735 43012d58 2019-07-14 stsp err(1, "pledge");
6736 43012d58 2019-07-14 stsp #endif
6737 17ed4618 2019-06-02 stsp if (argc < 1)
6738 648e4ef7 2019-07-09 stsp usage_remove();
6739 2ec1f75b 2019-03-26 stsp
6740 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6741 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6742 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6743 2ec1f75b 2019-03-26 stsp goto done;
6744 2ec1f75b 2019-03-26 stsp }
6745 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6746 fa51e947 2020-03-27 stsp if (error) {
6747 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6748 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6749 2ec1f75b 2019-03-26 stsp goto done;
6750 fa51e947 2020-03-27 stsp }
6751 2ec1f75b 2019-03-26 stsp
6752 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6753 c9956ddf 2019-09-08 stsp NULL);
6754 2af4a041 2019-05-11 jcs if (error)
6755 2ec1f75b 2019-03-26 stsp goto done;
6756 2ec1f75b 2019-03-26 stsp
6757 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6758 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6759 2ec1f75b 2019-03-26 stsp if (error)
6760 2ec1f75b 2019-03-26 stsp goto done;
6761 2ec1f75b 2019-03-26 stsp
6762 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6763 6d022e97 2019-08-04 stsp if (error)
6764 6d022e97 2019-08-04 stsp goto done;
6765 17ed4618 2019-06-02 stsp
6766 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6767 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6768 f2a9dc41 2019-12-13 tracey struct stat sb;
6769 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6770 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6771 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6772 62d463ca 2020-10-20 naddy pe->path) == -1) {
6773 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6774 f2a9dc41 2019-12-13 tracey goto done;
6775 f2a9dc41 2019-12-13 tracey }
6776 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6777 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6778 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6779 f2a9dc41 2019-12-13 tracey continue;
6780 f2a9dc41 2019-12-13 tracey }
6781 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6782 f2a9dc41 2019-12-13 tracey ondisk_path);
6783 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6784 f2a9dc41 2019-12-13 tracey goto done;
6785 f2a9dc41 2019-12-13 tracey }
6786 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6787 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6788 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6789 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6790 f2a9dc41 2019-12-13 tracey goto done;
6791 f2a9dc41 2019-12-13 tracey }
6792 f2a9dc41 2019-12-13 tracey }
6793 f2a9dc41 2019-12-13 tracey }
6794 f2a9dc41 2019-12-13 tracey
6795 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6796 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6797 766841c2 2020-08-13 stsp repo, keep_on_disk);
6798 a129376b 2019-03-28 stsp done:
6799 1d0f4054 2021-06-17 stsp if (repo) {
6800 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6801 1d0f4054 2021-06-17 stsp if (error == NULL)
6802 1d0f4054 2021-06-17 stsp error = close_err;
6803 1d0f4054 2021-06-17 stsp }
6804 a129376b 2019-03-28 stsp if (worktree)
6805 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6806 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6807 17ed4618 2019-06-02 stsp free((char *)pe->path);
6808 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6809 a129376b 2019-03-28 stsp free(cwd);
6810 a129376b 2019-03-28 stsp return error;
6811 a129376b 2019-03-28 stsp }
6812 a129376b 2019-03-28 stsp
6813 a129376b 2019-03-28 stsp __dead static void
6814 a129376b 2019-03-28 stsp usage_revert(void)
6815 a129376b 2019-03-28 stsp {
6816 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6817 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6818 a129376b 2019-03-28 stsp exit(1);
6819 a129376b 2019-03-28 stsp }
6820 a129376b 2019-03-28 stsp
6821 1ee397ad 2019-07-12 stsp static const struct got_error *
6822 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6823 a129376b 2019-03-28 stsp {
6824 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6825 fb9704af 2020-01-27 stsp return NULL;
6826 fb9704af 2020-01-27 stsp
6827 a129376b 2019-03-28 stsp while (path[0] == '/')
6828 a129376b 2019-03-28 stsp path++;
6829 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6830 33aa809d 2019-08-08 stsp return NULL;
6831 33aa809d 2019-08-08 stsp }
6832 33aa809d 2019-08-08 stsp
6833 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6834 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6835 33aa809d 2019-08-08 stsp const char *action;
6836 33aa809d 2019-08-08 stsp };
6837 33aa809d 2019-08-08 stsp
6838 33aa809d 2019-08-08 stsp static const struct got_error *
6839 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6840 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6841 33aa809d 2019-08-08 stsp {
6842 33aa809d 2019-08-08 stsp char *line = NULL;
6843 33aa809d 2019-08-08 stsp size_t linesize = 0;
6844 33aa809d 2019-08-08 stsp ssize_t linelen;
6845 33aa809d 2019-08-08 stsp
6846 33aa809d 2019-08-08 stsp switch (status) {
6847 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6848 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6849 33aa809d 2019-08-08 stsp break;
6850 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6851 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6852 33aa809d 2019-08-08 stsp break;
6853 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6854 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6855 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6856 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6857 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6858 33aa809d 2019-08-08 stsp printf("%s", line);
6859 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6860 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6861 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6862 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6863 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6864 33aa809d 2019-08-08 stsp break;
6865 33aa809d 2019-08-08 stsp default:
6866 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6867 33aa809d 2019-08-08 stsp }
6868 33aa809d 2019-08-08 stsp
6869 33aa809d 2019-08-08 stsp return NULL;
6870 33aa809d 2019-08-08 stsp }
6871 33aa809d 2019-08-08 stsp
6872 33aa809d 2019-08-08 stsp static const struct got_error *
6873 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6874 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6875 33aa809d 2019-08-08 stsp {
6876 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6877 33aa809d 2019-08-08 stsp char *line = NULL;
6878 33aa809d 2019-08-08 stsp size_t linesize = 0;
6879 33aa809d 2019-08-08 stsp ssize_t linelen;
6880 33aa809d 2019-08-08 stsp int resp = ' ';
6881 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6882 33aa809d 2019-08-08 stsp
6883 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6884 33aa809d 2019-08-08 stsp
6885 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6886 33aa809d 2019-08-08 stsp char *nl;
6887 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6888 33aa809d 2019-08-08 stsp a->action);
6889 33aa809d 2019-08-08 stsp if (err)
6890 33aa809d 2019-08-08 stsp return err;
6891 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6892 33aa809d 2019-08-08 stsp if (linelen == -1) {
6893 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6894 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6895 33aa809d 2019-08-08 stsp return NULL;
6896 33aa809d 2019-08-08 stsp }
6897 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6898 33aa809d 2019-08-08 stsp if (nl)
6899 33aa809d 2019-08-08 stsp *nl = '\0';
6900 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6901 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6902 33aa809d 2019-08-08 stsp printf("y\n");
6903 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6904 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6905 33aa809d 2019-08-08 stsp printf("n\n");
6906 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6907 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6908 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6909 33aa809d 2019-08-08 stsp printf("q\n");
6910 33aa809d 2019-08-08 stsp } else
6911 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6912 33aa809d 2019-08-08 stsp free(line);
6913 33aa809d 2019-08-08 stsp return NULL;
6914 33aa809d 2019-08-08 stsp }
6915 33aa809d 2019-08-08 stsp
6916 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6917 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6918 33aa809d 2019-08-08 stsp a->action);
6919 33aa809d 2019-08-08 stsp if (err)
6920 33aa809d 2019-08-08 stsp return err;
6921 33aa809d 2019-08-08 stsp resp = getchar();
6922 33aa809d 2019-08-08 stsp if (resp == '\n')
6923 33aa809d 2019-08-08 stsp resp = getchar();
6924 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6925 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6926 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6927 33aa809d 2019-08-08 stsp resp = ' ';
6928 33aa809d 2019-08-08 stsp }
6929 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6930 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6931 33aa809d 2019-08-08 stsp resp = ' ';
6932 33aa809d 2019-08-08 stsp }
6933 33aa809d 2019-08-08 stsp }
6934 33aa809d 2019-08-08 stsp
6935 33aa809d 2019-08-08 stsp if (resp == 'y')
6936 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6937 33aa809d 2019-08-08 stsp else if (resp == 'n')
6938 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6939 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6940 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6941 33aa809d 2019-08-08 stsp
6942 1ee397ad 2019-07-12 stsp return NULL;
6943 a129376b 2019-03-28 stsp }
6944 a129376b 2019-03-28 stsp
6945 33aa809d 2019-08-08 stsp
6946 a129376b 2019-03-28 stsp static const struct got_error *
6947 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6948 a129376b 2019-03-28 stsp {
6949 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6950 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6951 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6952 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6953 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6954 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6955 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6956 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6957 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6958 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6959 a129376b 2019-03-28 stsp
6960 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6961 e20a8b6f 2019-06-04 stsp
6962 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6963 a129376b 2019-03-28 stsp switch (ch) {
6964 33aa809d 2019-08-08 stsp case 'p':
6965 33aa809d 2019-08-08 stsp pflag = 1;
6966 33aa809d 2019-08-08 stsp break;
6967 33aa809d 2019-08-08 stsp case 'F':
6968 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6969 33aa809d 2019-08-08 stsp break;
6970 0f6d7415 2019-08-08 stsp case 'R':
6971 0f6d7415 2019-08-08 stsp can_recurse = 1;
6972 0f6d7415 2019-08-08 stsp break;
6973 a129376b 2019-03-28 stsp default:
6974 a129376b 2019-03-28 stsp usage_revert();
6975 a129376b 2019-03-28 stsp /* NOTREACHED */
6976 a129376b 2019-03-28 stsp }
6977 a129376b 2019-03-28 stsp }
6978 a129376b 2019-03-28 stsp
6979 a129376b 2019-03-28 stsp argc -= optind;
6980 a129376b 2019-03-28 stsp argv += optind;
6981 a129376b 2019-03-28 stsp
6982 43012d58 2019-07-14 stsp #ifndef PROFILE
6983 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6984 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6985 43012d58 2019-07-14 stsp err(1, "pledge");
6986 43012d58 2019-07-14 stsp #endif
6987 e20a8b6f 2019-06-04 stsp if (argc < 1)
6988 a129376b 2019-03-28 stsp usage_revert();
6989 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6990 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6991 a129376b 2019-03-28 stsp
6992 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6993 a129376b 2019-03-28 stsp if (cwd == NULL) {
6994 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6995 a129376b 2019-03-28 stsp goto done;
6996 a129376b 2019-03-28 stsp }
6997 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6998 fa51e947 2020-03-27 stsp if (error) {
6999 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7000 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
7001 2ec1f75b 2019-03-26 stsp goto done;
7002 fa51e947 2020-03-27 stsp }
7003 a129376b 2019-03-28 stsp
7004 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7005 c9956ddf 2019-09-08 stsp NULL);
7006 a129376b 2019-03-28 stsp if (error != NULL)
7007 a129376b 2019-03-28 stsp goto done;
7008 a129376b 2019-03-28 stsp
7009 33aa809d 2019-08-08 stsp if (patch_script_path) {
7010 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7011 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
7012 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
7013 33aa809d 2019-08-08 stsp patch_script_path);
7014 33aa809d 2019-08-08 stsp goto done;
7015 33aa809d 2019-08-08 stsp }
7016 33aa809d 2019-08-08 stsp }
7017 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7018 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7019 a129376b 2019-03-28 stsp if (error)
7020 a129376b 2019-03-28 stsp goto done;
7021 a129376b 2019-03-28 stsp
7022 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7023 6d022e97 2019-08-04 stsp if (error)
7024 6d022e97 2019-08-04 stsp goto done;
7025 00db391e 2019-08-03 stsp
7026 0f6d7415 2019-08-08 stsp if (!can_recurse) {
7027 0f6d7415 2019-08-08 stsp char *ondisk_path;
7028 0f6d7415 2019-08-08 stsp struct stat sb;
7029 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
7030 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
7031 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
7032 62d463ca 2020-10-20 naddy pe->path) == -1) {
7033 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
7034 0f6d7415 2019-08-08 stsp goto done;
7035 0f6d7415 2019-08-08 stsp }
7036 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
7037 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
7038 0f6d7415 2019-08-08 stsp free(ondisk_path);
7039 0f6d7415 2019-08-08 stsp continue;
7040 0f6d7415 2019-08-08 stsp }
7041 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
7042 0f6d7415 2019-08-08 stsp ondisk_path);
7043 0f6d7415 2019-08-08 stsp free(ondisk_path);
7044 0f6d7415 2019-08-08 stsp goto done;
7045 0f6d7415 2019-08-08 stsp }
7046 0f6d7415 2019-08-08 stsp free(ondisk_path);
7047 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
7048 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7049 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
7050 0f6d7415 2019-08-08 stsp goto done;
7051 0f6d7415 2019-08-08 stsp }
7052 0f6d7415 2019-08-08 stsp }
7053 0f6d7415 2019-08-08 stsp }
7054 0f6d7415 2019-08-08 stsp
7055 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7056 33aa809d 2019-08-08 stsp cpa.action = "revert";
7057 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7058 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7059 2ec1f75b 2019-03-26 stsp done:
7060 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7061 33aa809d 2019-08-08 stsp error == NULL)
7062 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7063 1d0f4054 2021-06-17 stsp if (repo) {
7064 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7065 1d0f4054 2021-06-17 stsp if (error == NULL)
7066 1d0f4054 2021-06-17 stsp error = close_err;
7067 1d0f4054 2021-06-17 stsp }
7068 2ec1f75b 2019-03-26 stsp if (worktree)
7069 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
7070 2ec1f75b 2019-03-26 stsp free(path);
7071 d00136be 2019-03-26 stsp free(cwd);
7072 6bad629b 2019-02-04 stsp return error;
7073 c4296144 2019-05-09 stsp }
7074 c4296144 2019-05-09 stsp
7075 c4296144 2019-05-09 stsp __dead static void
7076 c4296144 2019-05-09 stsp usage_commit(void)
7077 c4296144 2019-05-09 stsp {
7078 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7079 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
7080 c4296144 2019-05-09 stsp exit(1);
7081 33ad4cbe 2019-05-12 jcs }
7082 33ad4cbe 2019-05-12 jcs
7083 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
7084 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
7085 28cf319f 2021-01-28 stsp const char *prepared_log;
7086 28cf319f 2021-01-28 stsp int non_interactive;
7087 e2ba3d07 2019-05-13 stsp const char *editor;
7088 e0870e44 2019-05-13 stsp const char *worktree_path;
7089 76d98825 2019-06-03 stsp const char *branch_name;
7090 314a6357 2019-05-13 stsp const char *repo_path;
7091 e0870e44 2019-05-13 stsp char *logmsg_path;
7092 e2ba3d07 2019-05-13 stsp
7093 e2ba3d07 2019-05-13 stsp };
7094 28cf319f 2021-01-28 stsp
7095 28cf319f 2021-01-28 stsp static const struct got_error *
7096 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
7097 28cf319f 2021-01-28 stsp {
7098 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
7099 28cf319f 2021-01-28 stsp FILE *f = NULL;
7100 28cf319f 2021-01-28 stsp struct stat sb;
7101 28cf319f 2021-01-28 stsp size_t r;
7102 28cf319f 2021-01-28 stsp
7103 28cf319f 2021-01-28 stsp *logmsg = NULL;
7104 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7105 28cf319f 2021-01-28 stsp
7106 28cf319f 2021-01-28 stsp f = fopen(path, "r");
7107 28cf319f 2021-01-28 stsp if (f == NULL)
7108 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
7109 28cf319f 2021-01-28 stsp
7110 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
7111 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
7112 28cf319f 2021-01-28 stsp goto done;
7113 28cf319f 2021-01-28 stsp }
7114 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
7115 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7116 28cf319f 2021-01-28 stsp goto done;
7117 28cf319f 2021-01-28 stsp }
7118 28cf319f 2021-01-28 stsp
7119 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
7120 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
7121 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
7122 28cf319f 2021-01-28 stsp goto done;
7123 28cf319f 2021-01-28 stsp }
7124 28cf319f 2021-01-28 stsp
7125 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7126 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7127 28cf319f 2021-01-28 stsp if (ferror(f))
7128 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7129 28cf319f 2021-01-28 stsp else
7130 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7131 28cf319f 2021-01-28 stsp goto done;
7132 28cf319f 2021-01-28 stsp }
7133 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7134 28cf319f 2021-01-28 stsp done:
7135 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7136 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7137 28cf319f 2021-01-28 stsp if (err) {
7138 28cf319f 2021-01-28 stsp free(*logmsg);
7139 28cf319f 2021-01-28 stsp *logmsg = NULL;
7140 28cf319f 2021-01-28 stsp }
7141 28cf319f 2021-01-28 stsp return err;
7142 28cf319f 2021-01-28 stsp
7143 28cf319f 2021-01-28 stsp }
7144 e0870e44 2019-05-13 stsp
7145 33ad4cbe 2019-05-12 jcs static const struct got_error *
7146 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7147 33ad4cbe 2019-05-12 jcs void *arg)
7148 33ad4cbe 2019-05-12 jcs {
7149 76d98825 2019-06-03 stsp char *initial_content = NULL;
7150 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7151 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7152 e0870e44 2019-05-13 stsp char *template = NULL;
7153 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7154 1601cb9f 2020-09-11 naddy int initial_content_len;
7155 97972933 2020-09-11 stsp int fd = -1;
7156 33ad4cbe 2019-05-12 jcs size_t len;
7157 33ad4cbe 2019-05-12 jcs
7158 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7159 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7160 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7161 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7162 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7163 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7164 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7165 33ad4cbe 2019-05-12 jcs return NULL;
7166 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7167 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7168 33ad4cbe 2019-05-12 jcs
7169 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7170 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7171 76d98825 2019-06-03 stsp
7172 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7173 28cf319f 2021-01-28 stsp if (err)
7174 28cf319f 2021-01-28 stsp goto done;
7175 28cf319f 2021-01-28 stsp
7176 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7177 28cf319f 2021-01-28 stsp char *msg;
7178 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7179 28cf319f 2021-01-28 stsp if (err)
7180 28cf319f 2021-01-28 stsp goto done;
7181 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7182 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7183 28cf319f 2021-01-28 stsp free(msg);
7184 28cf319f 2021-01-28 stsp goto done;
7185 28cf319f 2021-01-28 stsp }
7186 28cf319f 2021-01-28 stsp free(msg);
7187 28cf319f 2021-01-28 stsp }
7188 28cf319f 2021-01-28 stsp
7189 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7190 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7191 1601cb9f 2020-09-11 naddy a->branch_name);
7192 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7193 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7194 e0870e44 2019-05-13 stsp goto done;
7195 28cf319f 2021-01-28 stsp }
7196 33ad4cbe 2019-05-12 jcs
7197 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7198 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7199 97972933 2020-09-11 stsp goto done;
7200 97972933 2020-09-11 stsp }
7201 33ad4cbe 2019-05-12 jcs
7202 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7203 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7204 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7205 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7206 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7207 33ad4cbe 2019-05-12 jcs }
7208 33ad4cbe 2019-05-12 jcs
7209 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7210 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7211 33ad4cbe 2019-05-12 jcs done:
7212 76d98825 2019-06-03 stsp free(initial_content);
7213 e0870e44 2019-05-13 stsp free(template);
7214 314a6357 2019-05-13 stsp
7215 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7216 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7217 97972933 2020-09-11 stsp
7218 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7219 59f86c76 2020-09-11 stsp if (err == NULL)
7220 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7221 59f86c76 2020-09-11 stsp if (err) {
7222 59f86c76 2020-09-11 stsp free(*logmsg);
7223 59f86c76 2020-09-11 stsp *logmsg = NULL;
7224 3ce1b845 2019-07-15 stsp }
7225 33ad4cbe 2019-05-12 jcs return err;
7226 6bad629b 2019-02-04 stsp }
7227 c4296144 2019-05-09 stsp
7228 c4296144 2019-05-09 stsp static const struct got_error *
7229 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7230 c4296144 2019-05-09 stsp {
7231 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7232 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7233 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7234 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7235 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7236 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7237 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7238 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7239 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7240 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7241 28cf319f 2021-01-28 stsp int allow_bad_symlinks = 0, non_interactive = 0;
7242 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7243 5c1e53bc 2019-07-28 stsp
7244 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7245 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7246 c4296144 2019-05-09 stsp
7247 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7248 c4296144 2019-05-09 stsp switch (ch) {
7249 28cf319f 2021-01-28 stsp case 'F':
7250 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7251 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7252 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7253 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7254 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7255 28cf319f 2021-01-28 stsp optarg);
7256 28cf319f 2021-01-28 stsp break;
7257 c4296144 2019-05-09 stsp case 'm':
7258 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7259 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7260 c4296144 2019-05-09 stsp logmsg = optarg;
7261 28cf319f 2021-01-28 stsp break;
7262 28cf319f 2021-01-28 stsp case 'N':
7263 28cf319f 2021-01-28 stsp non_interactive = 1;
7264 c4296144 2019-05-09 stsp break;
7265 35213c7c 2020-07-23 stsp case 'S':
7266 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7267 35213c7c 2020-07-23 stsp break;
7268 c4296144 2019-05-09 stsp default:
7269 c4296144 2019-05-09 stsp usage_commit();
7270 c4296144 2019-05-09 stsp /* NOTREACHED */
7271 c4296144 2019-05-09 stsp }
7272 c4296144 2019-05-09 stsp }
7273 c4296144 2019-05-09 stsp
7274 c4296144 2019-05-09 stsp argc -= optind;
7275 c4296144 2019-05-09 stsp argv += optind;
7276 c4296144 2019-05-09 stsp
7277 43012d58 2019-07-14 stsp #ifndef PROFILE
7278 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7279 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7280 43012d58 2019-07-14 stsp err(1, "pledge");
7281 43012d58 2019-07-14 stsp #endif
7282 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7283 c4296144 2019-05-09 stsp if (cwd == NULL) {
7284 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7285 c4296144 2019-05-09 stsp goto done;
7286 c4296144 2019-05-09 stsp }
7287 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7288 fa51e947 2020-03-27 stsp if (error) {
7289 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7290 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7291 7d5807f4 2019-07-11 stsp goto done;
7292 fa51e947 2020-03-27 stsp }
7293 7d5807f4 2019-07-11 stsp
7294 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7295 c4296144 2019-05-09 stsp if (error)
7296 a698f62e 2019-07-25 stsp goto done;
7297 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7298 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7299 c4296144 2019-05-09 stsp goto done;
7300 a698f62e 2019-07-25 stsp }
7301 5c1e53bc 2019-07-28 stsp
7302 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7303 916f288c 2019-07-30 stsp worktree);
7304 5c1e53bc 2019-07-28 stsp if (error)
7305 5c1e53bc 2019-07-28 stsp goto done;
7306 c4296144 2019-05-09 stsp
7307 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7308 c9956ddf 2019-09-08 stsp if (error)
7309 c9956ddf 2019-09-08 stsp goto done;
7310 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7311 c9956ddf 2019-09-08 stsp gitconfig_path);
7312 c4296144 2019-05-09 stsp if (error != NULL)
7313 c4296144 2019-05-09 stsp goto done;
7314 c4296144 2019-05-09 stsp
7315 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7316 aba9c984 2019-09-08 stsp if (error)
7317 aba9c984 2019-09-08 stsp return error;
7318 aba9c984 2019-09-08 stsp
7319 314a6357 2019-05-13 stsp /*
7320 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7321 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7322 314a6357 2019-05-13 stsp */
7323 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7324 314a6357 2019-05-13 stsp error = get_editor(&editor);
7325 314a6357 2019-05-13 stsp else
7326 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7327 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7328 c4296144 2019-05-09 stsp if (error)
7329 c4296144 2019-05-09 stsp goto done;
7330 c4296144 2019-05-09 stsp
7331 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7332 087fb88c 2019-08-04 stsp if (error)
7333 087fb88c 2019-08-04 stsp goto done;
7334 087fb88c 2019-08-04 stsp
7335 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7336 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7337 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7338 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7339 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7340 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7341 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7342 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7343 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7344 916f288c 2019-07-30 stsp goto done;
7345 916f288c 2019-07-30 stsp }
7346 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7347 916f288c 2019-07-30 stsp }
7348 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7349 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7350 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7351 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7352 e0870e44 2019-05-13 stsp if (error) {
7353 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7354 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7355 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7356 c4296144 2019-05-09 stsp goto done;
7357 e0870e44 2019-05-13 stsp }
7358 c4296144 2019-05-09 stsp
7359 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7360 c4296144 2019-05-09 stsp if (error)
7361 c4296144 2019-05-09 stsp goto done;
7362 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7363 c4296144 2019-05-09 stsp done:
7364 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7365 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7366 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7367 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7368 7266f21f 2019-10-21 stsp error == NULL)
7369 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7370 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7371 1d0f4054 2021-06-17 stsp if (repo) {
7372 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7373 1d0f4054 2021-06-17 stsp if (error == NULL)
7374 1d0f4054 2021-06-17 stsp error = close_err;
7375 1d0f4054 2021-06-17 stsp }
7376 c4296144 2019-05-09 stsp if (worktree)
7377 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7378 c4296144 2019-05-09 stsp free(cwd);
7379 c4296144 2019-05-09 stsp free(id_str);
7380 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7381 e2ba3d07 2019-05-13 stsp free(editor);
7382 aba9c984 2019-09-08 stsp free(author);
7383 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7384 f8a36e22 2021-08-26 stsp return error;
7385 f8a36e22 2021-08-26 stsp }
7386 f8a36e22 2021-08-26 stsp
7387 f8a36e22 2021-08-26 stsp __dead static void
7388 f8a36e22 2021-08-26 stsp usage_send(void)
7389 f8a36e22 2021-08-26 stsp {
7390 f8a36e22 2021-08-26 stsp fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7391 f8a36e22 2021-08-26 stsp "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7392 f8a36e22 2021-08-26 stsp "[remote-repository]\n", getprogname());
7393 f8a36e22 2021-08-26 stsp exit(1);
7394 f8a36e22 2021-08-26 stsp }
7395 f8a36e22 2021-08-26 stsp
7396 f8a36e22 2021-08-26 stsp struct got_send_progress_arg {
7397 f8a36e22 2021-08-26 stsp char last_scaled_packsize[FMT_SCALED_STRSIZE];
7398 f8a36e22 2021-08-26 stsp int verbosity;
7399 f8a36e22 2021-08-26 stsp int last_ncommits;
7400 f8a36e22 2021-08-26 stsp int last_nobj_total;
7401 f8a36e22 2021-08-26 stsp int last_p_deltify;
7402 f8a36e22 2021-08-26 stsp int last_p_written;
7403 f8a36e22 2021-08-26 stsp int last_p_sent;
7404 f8a36e22 2021-08-26 stsp int printed_something;
7405 f8a36e22 2021-08-26 stsp int sent_something;
7406 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches;
7407 f8a36e22 2021-08-26 stsp };
7408 f8a36e22 2021-08-26 stsp
7409 f8a36e22 2021-08-26 stsp static const struct got_error *
7410 f8a36e22 2021-08-26 stsp send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7411 f8a36e22 2021-08-26 stsp int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7412 f8a36e22 2021-08-26 stsp int success)
7413 f8a36e22 2021-08-26 stsp {
7414 f8a36e22 2021-08-26 stsp struct got_send_progress_arg *a = arg;
7415 f8a36e22 2021-08-26 stsp char scaled_packsize[FMT_SCALED_STRSIZE];
7416 f8a36e22 2021-08-26 stsp char scaled_sent[FMT_SCALED_STRSIZE];
7417 f8a36e22 2021-08-26 stsp int p_deltify = 0, p_written = 0, p_sent = 0;
7418 f8a36e22 2021-08-26 stsp int print_searching = 0, print_total = 0;
7419 f8a36e22 2021-08-26 stsp int print_deltify = 0, print_written = 0, print_sent = 0;
7420 f8a36e22 2021-08-26 stsp
7421 f8a36e22 2021-08-26 stsp if (a->verbosity < 0)
7422 f8a36e22 2021-08-26 stsp return NULL;
7423 f8a36e22 2021-08-26 stsp
7424 f8a36e22 2021-08-26 stsp if (refname) {
7425 f8a36e22 2021-08-26 stsp const char *status = success ? "accepted" : "rejected";
7426 f8a36e22 2021-08-26 stsp
7427 f8a36e22 2021-08-26 stsp if (success) {
7428 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7429 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, a->delete_branches, entry) {
7430 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7431 f8a36e22 2021-08-26 stsp if (got_path_cmp(branchname, refname,
7432 f8a36e22 2021-08-26 stsp strlen(branchname), strlen(refname)) == 0) {
7433 f8a36e22 2021-08-26 stsp status = "deleted";
7434 27b75514 2021-08-28 stsp a->sent_something = 1;
7435 f8a36e22 2021-08-26 stsp break;
7436 f8a36e22 2021-08-26 stsp }
7437 f8a36e22 2021-08-26 stsp }
7438 f8a36e22 2021-08-26 stsp }
7439 f8a36e22 2021-08-26 stsp
7440 27b75514 2021-08-28 stsp if (a->printed_something)
7441 27b75514 2021-08-28 stsp putchar('\n');
7442 27b75514 2021-08-28 stsp printf("Server has %s %s", status, refname);
7443 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7444 f8a36e22 2021-08-26 stsp return NULL;
7445 f8a36e22 2021-08-26 stsp }
7446 f8a36e22 2021-08-26 stsp
7447 f8a36e22 2021-08-26 stsp if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7448 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7449 f8a36e22 2021-08-26 stsp if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7450 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7451 f8a36e22 2021-08-26 stsp
7452 f8a36e22 2021-08-26 stsp if (a->last_ncommits != ncommits) {
7453 f8a36e22 2021-08-26 stsp print_searching = 1;
7454 f8a36e22 2021-08-26 stsp a->last_ncommits = ncommits;
7455 f8a36e22 2021-08-26 stsp }
7456 f8a36e22 2021-08-26 stsp
7457 f8a36e22 2021-08-26 stsp if (a->last_nobj_total != nobj_total) {
7458 f8a36e22 2021-08-26 stsp print_searching = 1;
7459 f8a36e22 2021-08-26 stsp print_total = 1;
7460 f8a36e22 2021-08-26 stsp a->last_nobj_total = nobj_total;
7461 f8a36e22 2021-08-26 stsp }
7462 f8a36e22 2021-08-26 stsp
7463 f8a36e22 2021-08-26 stsp if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7464 f8a36e22 2021-08-26 stsp strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7465 f8a36e22 2021-08-26 stsp if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7466 f8a36e22 2021-08-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7467 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
7468 f8a36e22 2021-08-26 stsp }
7469 f8a36e22 2021-08-26 stsp
7470 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0 || nobj_written > 0) {
7471 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0) {
7472 f8a36e22 2021-08-26 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
7473 f8a36e22 2021-08-26 stsp if (p_deltify != a->last_p_deltify) {
7474 f8a36e22 2021-08-26 stsp a->last_p_deltify = p_deltify;
7475 f8a36e22 2021-08-26 stsp print_searching = 1;
7476 f8a36e22 2021-08-26 stsp print_total = 1;
7477 f8a36e22 2021-08-26 stsp print_deltify = 1;
7478 f8a36e22 2021-08-26 stsp }
7479 f8a36e22 2021-08-26 stsp }
7480 f8a36e22 2021-08-26 stsp if (nobj_written > 0) {
7481 f8a36e22 2021-08-26 stsp p_written = (nobj_written * 100) / nobj_total;
7482 f8a36e22 2021-08-26 stsp if (p_written != a->last_p_written) {
7483 f8a36e22 2021-08-26 stsp a->last_p_written = p_written;
7484 f8a36e22 2021-08-26 stsp print_searching = 1;
7485 f8a36e22 2021-08-26 stsp print_total = 1;
7486 f8a36e22 2021-08-26 stsp print_deltify = 1;
7487 f8a36e22 2021-08-26 stsp print_written = 1;
7488 f8a36e22 2021-08-26 stsp }
7489 f8a36e22 2021-08-26 stsp }
7490 f8a36e22 2021-08-26 stsp }
7491 f8a36e22 2021-08-26 stsp
7492 f8a36e22 2021-08-26 stsp if (bytes_sent > 0) {
7493 f8a36e22 2021-08-26 stsp p_sent = (bytes_sent * 100) / packfile_size;
7494 f8a36e22 2021-08-26 stsp if (p_sent != a->last_p_sent) {
7495 f8a36e22 2021-08-26 stsp a->last_p_sent = p_sent;
7496 f8a36e22 2021-08-26 stsp print_searching = 1;
7497 f8a36e22 2021-08-26 stsp print_total = 1;
7498 f8a36e22 2021-08-26 stsp print_deltify = 1;
7499 f8a36e22 2021-08-26 stsp print_written = 1;
7500 f8a36e22 2021-08-26 stsp print_sent = 1;
7501 f8a36e22 2021-08-26 stsp }
7502 f8a36e22 2021-08-26 stsp a->sent_something = 1;
7503 f8a36e22 2021-08-26 stsp }
7504 f8a36e22 2021-08-26 stsp
7505 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify || print_written ||
7506 f8a36e22 2021-08-26 stsp print_sent)
7507 f8a36e22 2021-08-26 stsp printf("\r");
7508 f8a36e22 2021-08-26 stsp if (print_searching)
7509 f8a36e22 2021-08-26 stsp printf("packing %d reference%s", ncommits,
7510 f8a36e22 2021-08-26 stsp ncommits == 1 ? "" : "s");
7511 f8a36e22 2021-08-26 stsp if (print_total)
7512 f8a36e22 2021-08-26 stsp printf("; %d object%s", nobj_total,
7513 f8a36e22 2021-08-26 stsp nobj_total == 1 ? "" : "s");
7514 f8a36e22 2021-08-26 stsp if (print_deltify)
7515 f8a36e22 2021-08-26 stsp printf("; deltify: %d%%", p_deltify);
7516 f8a36e22 2021-08-26 stsp if (print_sent)
7517 f8a36e22 2021-08-26 stsp printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7518 f8a36e22 2021-08-26 stsp scaled_packsize, p_sent);
7519 f8a36e22 2021-08-26 stsp else if (print_written)
7520 f8a36e22 2021-08-26 stsp printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7521 f8a36e22 2021-08-26 stsp scaled_packsize, p_written);
7522 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify ||
7523 f8a36e22 2021-08-26 stsp print_written || print_sent) {
7524 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7525 f8a36e22 2021-08-26 stsp fflush(stdout);
7526 f8a36e22 2021-08-26 stsp }
7527 f8a36e22 2021-08-26 stsp return NULL;
7528 f8a36e22 2021-08-26 stsp }
7529 f8a36e22 2021-08-26 stsp
7530 f8a36e22 2021-08-26 stsp static const struct got_error *
7531 f8a36e22 2021-08-26 stsp cmd_send(int argc, char *argv[])
7532 f8a36e22 2021-08-26 stsp {
7533 f8a36e22 2021-08-26 stsp const struct got_error *error = NULL;
7534 f8a36e22 2021-08-26 stsp char *cwd = NULL, *repo_path = NULL;
7535 f8a36e22 2021-08-26 stsp const char *remote_name;
7536 f8a36e22 2021-08-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
7537 f8a36e22 2021-08-26 stsp char *repo_name = NULL, *server_path = NULL;
7538 f8a36e22 2021-08-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
7539 f8a36e22 2021-08-26 stsp int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7540 f8a36e22 2021-08-26 stsp struct got_repository *repo = NULL;
7541 f8a36e22 2021-08-26 stsp struct got_worktree *worktree = NULL;
7542 f8a36e22 2021-08-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7543 f8a36e22 2021-08-26 stsp struct got_pathlist_head branches;
7544 f8a36e22 2021-08-26 stsp struct got_pathlist_head tags;
7545 f8a36e22 2021-08-26 stsp struct got_reflist_head all_branches;
7546 f8a36e22 2021-08-26 stsp struct got_reflist_head all_tags;
7547 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_args;
7548 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_branches;
7549 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
7550 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7551 f8a36e22 2021-08-26 stsp int i, ch, sendfd = -1, sendstatus;
7552 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
7553 f8a36e22 2021-08-26 stsp struct got_send_progress_arg spa;
7554 f8a36e22 2021-08-26 stsp int verbosity = 0, overwrite_refs = 0;
7555 f8a36e22 2021-08-26 stsp int send_all_branches = 0, send_all_tags = 0;
7556 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
7557 f8a36e22 2021-08-26 stsp
7558 f8a36e22 2021-08-26 stsp TAILQ_INIT(&branches);
7559 f8a36e22 2021-08-26 stsp TAILQ_INIT(&tags);
7560 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_branches);
7561 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_tags);
7562 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_args);
7563 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_branches);
7564 f8a36e22 2021-08-26 stsp
7565 f8a36e22 2021-08-26 stsp while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7566 f8a36e22 2021-08-26 stsp switch (ch) {
7567 f8a36e22 2021-08-26 stsp case 'a':
7568 f8a36e22 2021-08-26 stsp send_all_branches = 1;
7569 f8a36e22 2021-08-26 stsp break;
7570 f8a36e22 2021-08-26 stsp case 'b':
7571 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, optarg, NULL);
7572 f8a36e22 2021-08-26 stsp if (error)
7573 f8a36e22 2021-08-26 stsp return error;
7574 f8a36e22 2021-08-26 stsp nbranches++;
7575 f8a36e22 2021-08-26 stsp break;
7576 f8a36e22 2021-08-26 stsp case 'd':
7577 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&delete_args, optarg, NULL);
7578 f8a36e22 2021-08-26 stsp if (error)
7579 f8a36e22 2021-08-26 stsp return error;
7580 f8a36e22 2021-08-26 stsp break;
7581 f8a36e22 2021-08-26 stsp case 'f':
7582 f8a36e22 2021-08-26 stsp overwrite_refs = 1;
7583 f8a36e22 2021-08-26 stsp break;
7584 f8a36e22 2021-08-26 stsp case 'r':
7585 f8a36e22 2021-08-26 stsp repo_path = realpath(optarg, NULL);
7586 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7587 f8a36e22 2021-08-26 stsp return got_error_from_errno2("realpath",
7588 f8a36e22 2021-08-26 stsp optarg);
7589 f8a36e22 2021-08-26 stsp got_path_strip_trailing_slashes(repo_path);
7590 f8a36e22 2021-08-26 stsp break;
7591 f8a36e22 2021-08-26 stsp case 't':
7592 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags, optarg, NULL);
7593 f8a36e22 2021-08-26 stsp if (error)
7594 f8a36e22 2021-08-26 stsp return error;
7595 f8a36e22 2021-08-26 stsp ntags++;
7596 f8a36e22 2021-08-26 stsp break;
7597 f8a36e22 2021-08-26 stsp case 'T':
7598 f8a36e22 2021-08-26 stsp send_all_tags = 1;
7599 f8a36e22 2021-08-26 stsp break;
7600 f8a36e22 2021-08-26 stsp case 'v':
7601 f8a36e22 2021-08-26 stsp if (verbosity < 0)
7602 f8a36e22 2021-08-26 stsp verbosity = 0;
7603 f8a36e22 2021-08-26 stsp else if (verbosity < 3)
7604 f8a36e22 2021-08-26 stsp verbosity++;
7605 f8a36e22 2021-08-26 stsp break;
7606 f8a36e22 2021-08-26 stsp case 'q':
7607 f8a36e22 2021-08-26 stsp verbosity = -1;
7608 f8a36e22 2021-08-26 stsp break;
7609 f8a36e22 2021-08-26 stsp default:
7610 f8a36e22 2021-08-26 stsp usage_send();
7611 f8a36e22 2021-08-26 stsp /* NOTREACHED */
7612 f8a36e22 2021-08-26 stsp }
7613 f8a36e22 2021-08-26 stsp }
7614 f8a36e22 2021-08-26 stsp argc -= optind;
7615 f8a36e22 2021-08-26 stsp argv += optind;
7616 f8a36e22 2021-08-26 stsp
7617 f8a36e22 2021-08-26 stsp if (send_all_branches && !TAILQ_EMPTY(&branches))
7618 f8a36e22 2021-08-26 stsp option_conflict('a', 'b');
7619 f8a36e22 2021-08-26 stsp if (send_all_tags && !TAILQ_EMPTY(&tags))
7620 f8a36e22 2021-08-26 stsp option_conflict('T', 't');
7621 f8a36e22 2021-08-26 stsp
7622 f8a36e22 2021-08-26 stsp
7623 f8a36e22 2021-08-26 stsp if (argc == 0)
7624 f8a36e22 2021-08-26 stsp remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7625 f8a36e22 2021-08-26 stsp else if (argc == 1)
7626 f8a36e22 2021-08-26 stsp remote_name = argv[0];
7627 f8a36e22 2021-08-26 stsp else
7628 f8a36e22 2021-08-26 stsp usage_send();
7629 f8a36e22 2021-08-26 stsp
7630 f8a36e22 2021-08-26 stsp cwd = getcwd(NULL, 0);
7631 f8a36e22 2021-08-26 stsp if (cwd == NULL) {
7632 f8a36e22 2021-08-26 stsp error = got_error_from_errno("getcwd");
7633 f8a36e22 2021-08-26 stsp goto done;
7634 f8a36e22 2021-08-26 stsp }
7635 f8a36e22 2021-08-26 stsp
7636 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7637 f8a36e22 2021-08-26 stsp error = got_worktree_open(&worktree, cwd);
7638 f8a36e22 2021-08-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7639 f8a36e22 2021-08-26 stsp goto done;
7640 f8a36e22 2021-08-26 stsp else
7641 f8a36e22 2021-08-26 stsp error = NULL;
7642 f8a36e22 2021-08-26 stsp if (worktree) {
7643 f8a36e22 2021-08-26 stsp repo_path =
7644 f8a36e22 2021-08-26 stsp strdup(got_worktree_get_repo_path(worktree));
7645 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7646 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7647 f8a36e22 2021-08-26 stsp if (error)
7648 f8a36e22 2021-08-26 stsp goto done;
7649 f8a36e22 2021-08-26 stsp } else {
7650 f8a36e22 2021-08-26 stsp repo_path = strdup(cwd);
7651 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7652 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7653 f8a36e22 2021-08-26 stsp goto done;
7654 f8a36e22 2021-08-26 stsp }
7655 f8a36e22 2021-08-26 stsp }
7656 f8a36e22 2021-08-26 stsp }
7657 f8a36e22 2021-08-26 stsp
7658 f8a36e22 2021-08-26 stsp error = got_repo_open(&repo, repo_path, NULL);
7659 f8a36e22 2021-08-26 stsp if (error)
7660 f8a36e22 2021-08-26 stsp goto done;
7661 f8a36e22 2021-08-26 stsp
7662 f8a36e22 2021-08-26 stsp if (worktree) {
7663 f8a36e22 2021-08-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
7664 f8a36e22 2021-08-26 stsp if (worktree_conf) {
7665 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7666 f8a36e22 2021-08-26 stsp worktree_conf);
7667 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7668 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7669 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7670 f8a36e22 2021-08-26 stsp break;
7671 f8a36e22 2021-08-26 stsp }
7672 f8a36e22 2021-08-26 stsp }
7673 f8a36e22 2021-08-26 stsp }
7674 f8a36e22 2021-08-26 stsp }
7675 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7676 f8a36e22 2021-08-26 stsp repo_conf = got_repo_get_gotconfig(repo);
7677 f8a36e22 2021-08-26 stsp if (repo_conf) {
7678 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7679 f8a36e22 2021-08-26 stsp repo_conf);
7680 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7681 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7682 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7683 f8a36e22 2021-08-26 stsp break;
7684 f8a36e22 2021-08-26 stsp }
7685 f8a36e22 2021-08-26 stsp }
7686 f8a36e22 2021-08-26 stsp }
7687 f8a36e22 2021-08-26 stsp }
7688 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7689 f8a36e22 2021-08-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7690 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7691 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7692 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7693 f8a36e22 2021-08-26 stsp break;
7694 f8a36e22 2021-08-26 stsp }
7695 f8a36e22 2021-08-26 stsp }
7696 f8a36e22 2021-08-26 stsp }
7697 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7698 f8a36e22 2021-08-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7699 f8a36e22 2021-08-26 stsp goto done;
7700 f8a36e22 2021-08-26 stsp }
7701 f8a36e22 2021-08-26 stsp
7702 f8a36e22 2021-08-26 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
7703 6480c871 2021-08-30 stsp &repo_name, remote->send_url);
7704 f8a36e22 2021-08-26 stsp if (error)
7705 f8a36e22 2021-08-26 stsp goto done;
7706 f8a36e22 2021-08-26 stsp
7707 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git") == 0) {
7708 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7709 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7710 f8a36e22 2021-08-26 stsp "sendfd dns inet unveil", NULL) == -1)
7711 f8a36e22 2021-08-26 stsp err(1, "pledge");
7712 f8a36e22 2021-08-26 stsp #endif
7713 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
7714 f8a36e22 2021-08-26 stsp strcmp(proto, "ssh") == 0) {
7715 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7716 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7717 f8a36e22 2021-08-26 stsp "sendfd unveil", NULL) == -1)
7718 f8a36e22 2021-08-26 stsp err(1, "pledge");
7719 f8a36e22 2021-08-26 stsp #endif
7720 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "http") == 0 ||
7721 f8a36e22 2021-08-26 stsp strcmp(proto, "git+http") == 0) {
7722 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7723 f8a36e22 2021-08-26 stsp goto done;
7724 f8a36e22 2021-08-26 stsp } else {
7725 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7726 f8a36e22 2021-08-26 stsp goto done;
7727 f8a36e22 2021-08-26 stsp }
7728 f8a36e22 2021-08-26 stsp
7729 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
7730 f8a36e22 2021-08-26 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
7731 f8a36e22 2021-08-26 stsp error = got_error_from_errno2("unveil",
7732 f8a36e22 2021-08-26 stsp GOT_FETCH_PATH_SSH);
7733 f8a36e22 2021-08-26 stsp goto done;
7734 f8a36e22 2021-08-26 stsp }
7735 f8a36e22 2021-08-26 stsp }
7736 f8a36e22 2021-08-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7737 f8a36e22 2021-08-26 stsp if (error)
7738 f8a36e22 2021-08-26 stsp goto done;
7739 f8a36e22 2021-08-26 stsp
7740 f8a36e22 2021-08-26 stsp if (send_all_branches) {
7741 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_branches, repo, "refs/heads",
7742 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7743 f8a36e22 2021-08-26 stsp if (error)
7744 f8a36e22 2021-08-26 stsp goto done;
7745 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_branches, entry) {
7746 f8a36e22 2021-08-26 stsp const char *branchname = got_ref_get_name(re->ref);
7747 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches,
7748 f8a36e22 2021-08-26 stsp branchname, NULL);
7749 f8a36e22 2021-08-26 stsp if (error)
7750 f8a36e22 2021-08-26 stsp goto done;
7751 f8a36e22 2021-08-26 stsp nbranches++;
7752 f8a36e22 2021-08-26 stsp }
7753 eac1df47 2021-09-01 stsp } else if (nbranches == 0) {
7754 eac1df47 2021-09-01 stsp for (i = 0; i < remote->nsend_branches; i++) {
7755 eac1df47 2021-09-01 stsp got_pathlist_append(&branches,
7756 eac1df47 2021-09-01 stsp remote->send_branches[i], NULL);
7757 eac1df47 2021-09-01 stsp }
7758 f8a36e22 2021-08-26 stsp }
7759 f8a36e22 2021-08-26 stsp
7760 f8a36e22 2021-08-26 stsp if (send_all_tags) {
7761 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_tags, repo, "refs/tags",
7762 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7763 f8a36e22 2021-08-26 stsp if (error)
7764 f8a36e22 2021-08-26 stsp goto done;
7765 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_tags, entry) {
7766 f8a36e22 2021-08-26 stsp const char *tagname = got_ref_get_name(re->ref);
7767 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags,
7768 f8a36e22 2021-08-26 stsp tagname, NULL);
7769 f8a36e22 2021-08-26 stsp if (error)
7770 f8a36e22 2021-08-26 stsp goto done;
7771 f8a36e22 2021-08-26 stsp ntags++;
7772 f8a36e22 2021-08-26 stsp }
7773 f8a36e22 2021-08-26 stsp }
7774 f8a36e22 2021-08-26 stsp
7775 f8a36e22 2021-08-26 stsp /*
7776 f8a36e22 2021-08-26 stsp * To prevent accidents only branches in refs/heads/ can be deleted
7777 f8a36e22 2021-08-26 stsp * with 'got send -d'.
7778 f8a36e22 2021-08-26 stsp * Deleting anything else requires local repository access or Git.
7779 f8a36e22 2021-08-26 stsp */
7780 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_args, entry) {
7781 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7782 f8a36e22 2021-08-26 stsp char *s;
7783 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *new;
7784 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0) {
7785 f8a36e22 2021-08-26 stsp s = strdup(branchname);
7786 f8a36e22 2021-08-26 stsp if (s == NULL) {
7787 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7788 f8a36e22 2021-08-26 stsp goto done;
7789 f8a36e22 2021-08-26 stsp }
7790 f8a36e22 2021-08-26 stsp } else {
7791 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
7792 f8a36e22 2021-08-26 stsp error = got_error_from_errno("asprintf");
7793 f8a36e22 2021-08-26 stsp goto done;
7794 f8a36e22 2021-08-26 stsp }
7795 f8a36e22 2021-08-26 stsp }
7796 f8a36e22 2021-08-26 stsp error = got_pathlist_insert(&new, &delete_branches, s, NULL);
7797 f8a36e22 2021-08-26 stsp if (error || new == NULL /* duplicate */)
7798 f8a36e22 2021-08-26 stsp free(s);
7799 f8a36e22 2021-08-26 stsp if (error)
7800 f8a36e22 2021-08-26 stsp goto done;
7801 f8a36e22 2021-08-26 stsp ndelete_branches++;
7802 f8a36e22 2021-08-26 stsp }
7803 f8a36e22 2021-08-26 stsp
7804 f8a36e22 2021-08-26 stsp if (nbranches == 0 && ndelete_branches == 0) {
7805 f8a36e22 2021-08-26 stsp struct got_reference *head_ref;
7806 f8a36e22 2021-08-26 stsp if (worktree)
7807 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo,
7808 f8a36e22 2021-08-26 stsp got_worktree_get_head_ref_name(worktree), 0);
7809 f8a36e22 2021-08-26 stsp else
7810 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
7811 f8a36e22 2021-08-26 stsp if (error)
7812 f8a36e22 2021-08-26 stsp goto done;
7813 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(head_ref)) {
7814 f8a36e22 2021-08-26 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
7815 f8a36e22 2021-08-26 stsp got_ref_close(head_ref);
7816 f8a36e22 2021-08-26 stsp if (error)
7817 f8a36e22 2021-08-26 stsp goto done;
7818 f8a36e22 2021-08-26 stsp } else
7819 f8a36e22 2021-08-26 stsp ref = head_ref;
7820 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, got_ref_get_name(ref),
7821 abc59930 2021-09-05 naddy NULL);
7822 f8a36e22 2021-08-26 stsp if (error)
7823 f8a36e22 2021-08-26 stsp goto done;
7824 f8a36e22 2021-08-26 stsp nbranches++;
7825 f8a36e22 2021-08-26 stsp }
7826 f8a36e22 2021-08-26 stsp
7827 f8a36e22 2021-08-26 stsp if (verbosity >= 0)
7828 f8a36e22 2021-08-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
7829 f8a36e22 2021-08-26 stsp port ? ":" : "", port ? port : "");
7830 f8a36e22 2021-08-26 stsp
7831 f8a36e22 2021-08-26 stsp error = got_send_connect(&sendpid, &sendfd, proto, host, port,
7832 f8a36e22 2021-08-26 stsp server_path, verbosity);
7833 f8a36e22 2021-08-26 stsp if (error)
7834 f8a36e22 2021-08-26 stsp goto done;
7835 f8a36e22 2021-08-26 stsp
7836 f8a36e22 2021-08-26 stsp memset(&spa, 0, sizeof(spa));
7837 f8a36e22 2021-08-26 stsp spa.last_scaled_packsize[0] = '\0';
7838 f8a36e22 2021-08-26 stsp spa.last_p_deltify = -1;
7839 f8a36e22 2021-08-26 stsp spa.last_p_written = -1;
7840 f8a36e22 2021-08-26 stsp spa.verbosity = verbosity;
7841 f8a36e22 2021-08-26 stsp spa.delete_branches = &delete_branches;
7842 f8a36e22 2021-08-26 stsp error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
7843 f8a36e22 2021-08-26 stsp verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
7844 f8a36e22 2021-08-26 stsp check_cancelled, NULL);
7845 f8a36e22 2021-08-26 stsp if (spa.printed_something)
7846 f8a36e22 2021-08-26 stsp putchar('\n');
7847 f8a36e22 2021-08-26 stsp if (error)
7848 f8a36e22 2021-08-26 stsp goto done;
7849 f8a36e22 2021-08-26 stsp if (!spa.sent_something && verbosity >= 0)
7850 f8a36e22 2021-08-26 stsp printf("Already up-to-date\n");
7851 f8a36e22 2021-08-26 stsp done:
7852 f8a36e22 2021-08-26 stsp if (sendpid > 0) {
7853 f8a36e22 2021-08-26 stsp if (kill(sendpid, SIGTERM) == -1)
7854 f8a36e22 2021-08-26 stsp error = got_error_from_errno("kill");
7855 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
7856 f8a36e22 2021-08-26 stsp error = got_error_from_errno("waitpid");
7857 f8a36e22 2021-08-26 stsp }
7858 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
7859 f8a36e22 2021-08-26 stsp error = got_error_from_errno("close");
7860 f8a36e22 2021-08-26 stsp if (repo) {
7861 f8a36e22 2021-08-26 stsp const struct got_error *close_err = got_repo_close(repo);
7862 f8a36e22 2021-08-26 stsp if (error == NULL)
7863 f8a36e22 2021-08-26 stsp error = close_err;
7864 f8a36e22 2021-08-26 stsp }
7865 f8a36e22 2021-08-26 stsp if (worktree)
7866 f8a36e22 2021-08-26 stsp got_worktree_close(worktree);
7867 f8a36e22 2021-08-26 stsp if (ref)
7868 f8a36e22 2021-08-26 stsp got_ref_close(ref);
7869 f8a36e22 2021-08-26 stsp got_pathlist_free(&branches);
7870 f8a36e22 2021-08-26 stsp got_pathlist_free(&tags);
7871 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_branches);
7872 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_tags);
7873 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_args);
7874 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_branches, entry)
7875 f8a36e22 2021-08-26 stsp free((char *)pe->path);
7876 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_branches);
7877 f8a36e22 2021-08-26 stsp free(cwd);
7878 f8a36e22 2021-08-26 stsp free(repo_path);
7879 f8a36e22 2021-08-26 stsp free(proto);
7880 f8a36e22 2021-08-26 stsp free(host);
7881 f8a36e22 2021-08-26 stsp free(port);
7882 f8a36e22 2021-08-26 stsp free(server_path);
7883 f8a36e22 2021-08-26 stsp free(repo_name);
7884 234035bc 2019-06-01 stsp return error;
7885 234035bc 2019-06-01 stsp }
7886 234035bc 2019-06-01 stsp
7887 234035bc 2019-06-01 stsp __dead static void
7888 234035bc 2019-06-01 stsp usage_cherrypick(void)
7889 234035bc 2019-06-01 stsp {
7890 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7891 234035bc 2019-06-01 stsp exit(1);
7892 234035bc 2019-06-01 stsp }
7893 234035bc 2019-06-01 stsp
7894 234035bc 2019-06-01 stsp static const struct got_error *
7895 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
7896 234035bc 2019-06-01 stsp {
7897 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
7898 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
7899 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
7900 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
7901 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
7902 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
7903 234035bc 2019-06-01 stsp struct got_object_qid *pid;
7904 9627c110 2020-04-18 stsp int ch;
7905 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7906 234035bc 2019-06-01 stsp
7907 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7908 234035bc 2019-06-01 stsp switch (ch) {
7909 234035bc 2019-06-01 stsp default:
7910 234035bc 2019-06-01 stsp usage_cherrypick();
7911 234035bc 2019-06-01 stsp /* NOTREACHED */
7912 234035bc 2019-06-01 stsp }
7913 234035bc 2019-06-01 stsp }
7914 234035bc 2019-06-01 stsp
7915 234035bc 2019-06-01 stsp argc -= optind;
7916 234035bc 2019-06-01 stsp argv += optind;
7917 234035bc 2019-06-01 stsp
7918 43012d58 2019-07-14 stsp #ifndef PROFILE
7919 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7920 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7921 43012d58 2019-07-14 stsp err(1, "pledge");
7922 43012d58 2019-07-14 stsp #endif
7923 234035bc 2019-06-01 stsp if (argc != 1)
7924 234035bc 2019-06-01 stsp usage_cherrypick();
7925 234035bc 2019-06-01 stsp
7926 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7927 234035bc 2019-06-01 stsp if (cwd == NULL) {
7928 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7929 234035bc 2019-06-01 stsp goto done;
7930 234035bc 2019-06-01 stsp }
7931 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7932 fa51e947 2020-03-27 stsp if (error) {
7933 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7934 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7935 fa51e947 2020-03-27 stsp cwd);
7936 234035bc 2019-06-01 stsp goto done;
7937 fa51e947 2020-03-27 stsp }
7938 234035bc 2019-06-01 stsp
7939 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7940 c9956ddf 2019-09-08 stsp NULL);
7941 234035bc 2019-06-01 stsp if (error != NULL)
7942 234035bc 2019-06-01 stsp goto done;
7943 234035bc 2019-06-01 stsp
7944 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7945 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7946 234035bc 2019-06-01 stsp if (error)
7947 234035bc 2019-06-01 stsp goto done;
7948 234035bc 2019-06-01 stsp
7949 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7950 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7951 234035bc 2019-06-01 stsp if (error != NULL) {
7952 234035bc 2019-06-01 stsp struct got_reference *ref;
7953 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7954 234035bc 2019-06-01 stsp goto done;
7955 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7956 234035bc 2019-06-01 stsp if (error != NULL)
7957 234035bc 2019-06-01 stsp goto done;
7958 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7959 234035bc 2019-06-01 stsp got_ref_close(ref);
7960 234035bc 2019-06-01 stsp if (error != NULL)
7961 234035bc 2019-06-01 stsp goto done;
7962 234035bc 2019-06-01 stsp }
7963 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7964 234035bc 2019-06-01 stsp if (error)
7965 234035bc 2019-06-01 stsp goto done;
7966 234035bc 2019-06-01 stsp
7967 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7968 234035bc 2019-06-01 stsp if (error)
7969 234035bc 2019-06-01 stsp goto done;
7970 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7971 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7972 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7973 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7974 03415a1a 2019-06-02 stsp NULL);
7975 234035bc 2019-06-01 stsp if (error != NULL)
7976 234035bc 2019-06-01 stsp goto done;
7977 234035bc 2019-06-01 stsp
7978 9627c110 2020-04-18 stsp if (upa.did_something)
7979 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7980 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7981 234035bc 2019-06-01 stsp done:
7982 234035bc 2019-06-01 stsp if (commit)
7983 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7984 234035bc 2019-06-01 stsp free(commit_id_str);
7985 234035bc 2019-06-01 stsp if (worktree)
7986 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7987 1d0f4054 2021-06-17 stsp if (repo) {
7988 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7989 1d0f4054 2021-06-17 stsp if (error == NULL)
7990 1d0f4054 2021-06-17 stsp error = close_err;
7991 1d0f4054 2021-06-17 stsp }
7992 c4296144 2019-05-09 stsp return error;
7993 c4296144 2019-05-09 stsp }
7994 5ef14e63 2019-06-02 stsp
7995 5ef14e63 2019-06-02 stsp __dead static void
7996 5ef14e63 2019-06-02 stsp usage_backout(void)
7997 5ef14e63 2019-06-02 stsp {
7998 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7999 5ef14e63 2019-06-02 stsp exit(1);
8000 5ef14e63 2019-06-02 stsp }
8001 5ef14e63 2019-06-02 stsp
8002 5ef14e63 2019-06-02 stsp static const struct got_error *
8003 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
8004 5ef14e63 2019-06-02 stsp {
8005 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
8006 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
8007 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
8008 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
8009 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
8010 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
8011 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
8012 9627c110 2020-04-18 stsp int ch;
8013 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8014 5ef14e63 2019-06-02 stsp
8015 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8016 5ef14e63 2019-06-02 stsp switch (ch) {
8017 5ef14e63 2019-06-02 stsp default:
8018 5ef14e63 2019-06-02 stsp usage_backout();
8019 5ef14e63 2019-06-02 stsp /* NOTREACHED */
8020 5ef14e63 2019-06-02 stsp }
8021 5ef14e63 2019-06-02 stsp }
8022 5ef14e63 2019-06-02 stsp
8023 5ef14e63 2019-06-02 stsp argc -= optind;
8024 5ef14e63 2019-06-02 stsp argv += optind;
8025 5ef14e63 2019-06-02 stsp
8026 43012d58 2019-07-14 stsp #ifndef PROFILE
8027 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8028 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8029 43012d58 2019-07-14 stsp err(1, "pledge");
8030 43012d58 2019-07-14 stsp #endif
8031 5ef14e63 2019-06-02 stsp if (argc != 1)
8032 5ef14e63 2019-06-02 stsp usage_backout();
8033 5ef14e63 2019-06-02 stsp
8034 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
8035 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
8036 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
8037 5ef14e63 2019-06-02 stsp goto done;
8038 5ef14e63 2019-06-02 stsp }
8039 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
8040 fa51e947 2020-03-27 stsp if (error) {
8041 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8042 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
8043 5ef14e63 2019-06-02 stsp goto done;
8044 fa51e947 2020-03-27 stsp }
8045 5ef14e63 2019-06-02 stsp
8046 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8047 c9956ddf 2019-09-08 stsp NULL);
8048 5ef14e63 2019-06-02 stsp if (error != NULL)
8049 5ef14e63 2019-06-02 stsp goto done;
8050 5ef14e63 2019-06-02 stsp
8051 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8052 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8053 5ef14e63 2019-06-02 stsp if (error)
8054 5ef14e63 2019-06-02 stsp goto done;
8055 5ef14e63 2019-06-02 stsp
8056 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8057 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8058 5ef14e63 2019-06-02 stsp if (error != NULL) {
8059 5ef14e63 2019-06-02 stsp struct got_reference *ref;
8060 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8061 5ef14e63 2019-06-02 stsp goto done;
8062 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8063 5ef14e63 2019-06-02 stsp if (error != NULL)
8064 5ef14e63 2019-06-02 stsp goto done;
8065 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
8066 5ef14e63 2019-06-02 stsp got_ref_close(ref);
8067 5ef14e63 2019-06-02 stsp if (error != NULL)
8068 5ef14e63 2019-06-02 stsp goto done;
8069 5ef14e63 2019-06-02 stsp }
8070 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
8071 5ef14e63 2019-06-02 stsp if (error)
8072 5ef14e63 2019-06-02 stsp goto done;
8073 5ef14e63 2019-06-02 stsp
8074 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8075 5ef14e63 2019-06-02 stsp if (error)
8076 5ef14e63 2019-06-02 stsp goto done;
8077 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8078 5ef14e63 2019-06-02 stsp if (pid == NULL) {
8079 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
8080 5ef14e63 2019-06-02 stsp goto done;
8081 5ef14e63 2019-06-02 stsp }
8082 5ef14e63 2019-06-02 stsp
8083 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8084 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
8085 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8086 5ef14e63 2019-06-02 stsp if (error != NULL)
8087 5ef14e63 2019-06-02 stsp goto done;
8088 5ef14e63 2019-06-02 stsp
8089 9627c110 2020-04-18 stsp if (upa.did_something)
8090 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
8091 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8092 5ef14e63 2019-06-02 stsp done:
8093 5ef14e63 2019-06-02 stsp if (commit)
8094 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
8095 5ef14e63 2019-06-02 stsp free(commit_id_str);
8096 818c7501 2019-07-11 stsp if (worktree)
8097 818c7501 2019-07-11 stsp got_worktree_close(worktree);
8098 1d0f4054 2021-06-17 stsp if (repo) {
8099 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8100 1d0f4054 2021-06-17 stsp if (error == NULL)
8101 1d0f4054 2021-06-17 stsp error = close_err;
8102 1d0f4054 2021-06-17 stsp }
8103 818c7501 2019-07-11 stsp return error;
8104 818c7501 2019-07-11 stsp }
8105 818c7501 2019-07-11 stsp
8106 818c7501 2019-07-11 stsp __dead static void
8107 818c7501 2019-07-11 stsp usage_rebase(void)
8108 818c7501 2019-07-11 stsp {
8109 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8110 af54c8f8 2019-07-11 stsp getprogname());
8111 818c7501 2019-07-11 stsp exit(1);
8112 0ebf8283 2019-07-24 stsp }
8113 0ebf8283 2019-07-24 stsp
8114 0ebf8283 2019-07-24 stsp void
8115 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
8116 0ebf8283 2019-07-24 stsp {
8117 0ebf8283 2019-07-24 stsp char *nl;
8118 0ebf8283 2019-07-24 stsp size_t len;
8119 0ebf8283 2019-07-24 stsp
8120 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
8121 0ebf8283 2019-07-24 stsp if (len > limit)
8122 0ebf8283 2019-07-24 stsp len = limit;
8123 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
8124 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
8125 0ebf8283 2019-07-24 stsp if (nl)
8126 0ebf8283 2019-07-24 stsp *nl = '\0';
8127 0ebf8283 2019-07-24 stsp }
8128 0ebf8283 2019-07-24 stsp
8129 0ebf8283 2019-07-24 stsp static const struct got_error *
8130 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8131 0ebf8283 2019-07-24 stsp {
8132 5943eee2 2019-08-13 stsp const struct got_error *err;
8133 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
8134 5943eee2 2019-08-13 stsp const char *s;
8135 0ebf8283 2019-07-24 stsp
8136 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8137 5943eee2 2019-08-13 stsp if (err)
8138 5943eee2 2019-08-13 stsp return err;
8139 0ebf8283 2019-07-24 stsp
8140 5943eee2 2019-08-13 stsp s = logmsg0;
8141 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
8142 5943eee2 2019-08-13 stsp s++;
8143 5943eee2 2019-08-13 stsp
8144 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
8145 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
8146 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
8147 5943eee2 2019-08-13 stsp goto done;
8148 5943eee2 2019-08-13 stsp }
8149 0ebf8283 2019-07-24 stsp
8150 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
8151 5943eee2 2019-08-13 stsp done:
8152 5943eee2 2019-08-13 stsp free(logmsg0);
8153 a0ea4fc0 2020-02-28 stsp return err;
8154 a0ea4fc0 2020-02-28 stsp }
8155 a0ea4fc0 2020-02-28 stsp
8156 a0ea4fc0 2020-02-28 stsp static const struct got_error *
8157 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8158 a0ea4fc0 2020-02-28 stsp {
8159 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
8160 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
8161 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
8162 a0ea4fc0 2020-02-28 stsp
8163 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
8164 a0ea4fc0 2020-02-28 stsp if (err)
8165 a0ea4fc0 2020-02-28 stsp return err;
8166 a0ea4fc0 2020-02-28 stsp
8167 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
8168 a0ea4fc0 2020-02-28 stsp if (err)
8169 a0ea4fc0 2020-02-28 stsp goto done;
8170 a0ea4fc0 2020-02-28 stsp
8171 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
8172 a0ea4fc0 2020-02-28 stsp
8173 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
8174 a0ea4fc0 2020-02-28 stsp if (err)
8175 a0ea4fc0 2020-02-28 stsp goto done;
8176 a0ea4fc0 2020-02-28 stsp
8177 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
8178 a0ea4fc0 2020-02-28 stsp done:
8179 a0ea4fc0 2020-02-28 stsp free(id_str);
8180 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
8181 a0ea4fc0 2020-02-28 stsp free(logmsg);
8182 5943eee2 2019-08-13 stsp return err;
8183 818c7501 2019-07-11 stsp }
8184 818c7501 2019-07-11 stsp
8185 818c7501 2019-07-11 stsp static const struct got_error *
8186 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
8187 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
8188 818c7501 2019-07-11 stsp {
8189 818c7501 2019-07-11 stsp const struct got_error *err;
8190 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8191 818c7501 2019-07-11 stsp
8192 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
8193 818c7501 2019-07-11 stsp if (err)
8194 818c7501 2019-07-11 stsp goto done;
8195 818c7501 2019-07-11 stsp
8196 ff0d2220 2019-07-11 stsp if (new_id) {
8197 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
8198 ff0d2220 2019-07-11 stsp if (err)
8199 ff0d2220 2019-07-11 stsp goto done;
8200 ff0d2220 2019-07-11 stsp }
8201 818c7501 2019-07-11 stsp
8202 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
8203 ff0d2220 2019-07-11 stsp if (new_id_str)
8204 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
8205 0ebf8283 2019-07-24 stsp
8206 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8207 0ebf8283 2019-07-24 stsp if (err)
8208 0ebf8283 2019-07-24 stsp goto done;
8209 0ebf8283 2019-07-24 stsp
8210 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
8211 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8212 818c7501 2019-07-11 stsp done:
8213 818c7501 2019-07-11 stsp free(old_id_str);
8214 818c7501 2019-07-11 stsp free(new_id_str);
8215 272a1371 2020-02-28 stsp free(logmsg);
8216 818c7501 2019-07-11 stsp return err;
8217 818c7501 2019-07-11 stsp }
8218 818c7501 2019-07-11 stsp
8219 1ee397ad 2019-07-12 stsp static const struct got_error *
8220 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8221 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
8222 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
8223 e600f124 2021-03-21 stsp int create_backup)
8224 818c7501 2019-07-11 stsp {
8225 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
8226 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
8227 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
8228 818c7501 2019-07-11 stsp }
8229 818c7501 2019-07-11 stsp
8230 818c7501 2019-07-11 stsp static const struct got_error *
8231 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
8232 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8233 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
8234 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8235 ff0d2220 2019-07-11 stsp {
8236 ff0d2220 2019-07-11 stsp const struct got_error *error;
8237 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
8238 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
8239 ff0d2220 2019-07-11 stsp
8240 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8241 ff0d2220 2019-07-11 stsp if (error)
8242 ff0d2220 2019-07-11 stsp return error;
8243 ff0d2220 2019-07-11 stsp
8244 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8245 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
8246 ff0d2220 2019-07-11 stsp if (error) {
8247 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8248 ff0d2220 2019-07-11 stsp goto done;
8249 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
8250 ff0d2220 2019-07-11 stsp } else {
8251 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
8252 ff0d2220 2019-07-11 stsp free(new_commit_id);
8253 ff0d2220 2019-07-11 stsp }
8254 ff0d2220 2019-07-11 stsp done:
8255 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
8256 ff0d2220 2019-07-11 stsp return error;
8257 64c6d990 2019-07-11 stsp }
8258 64c6d990 2019-07-11 stsp
8259 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
8260 64c6d990 2019-07-11 stsp const char *path_prefix;
8261 64c6d990 2019-07-11 stsp size_t len;
8262 8ca9bd68 2019-07-25 stsp int errcode;
8263 64c6d990 2019-07-11 stsp };
8264 64c6d990 2019-07-11 stsp
8265 64c6d990 2019-07-11 stsp static const struct got_error *
8266 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8267 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
8268 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
8269 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
8270 64c6d990 2019-07-11 stsp {
8271 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
8272 64c6d990 2019-07-11 stsp
8273 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8274 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8275 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
8276 64c6d990 2019-07-11 stsp
8277 64c6d990 2019-07-11 stsp return NULL;
8278 64c6d990 2019-07-11 stsp }
8279 64c6d990 2019-07-11 stsp
8280 64c6d990 2019-07-11 stsp static const struct got_error *
8281 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
8282 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
8283 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
8284 64c6d990 2019-07-11 stsp {
8285 64c6d990 2019-07-11 stsp const struct got_error *err;
8286 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8287 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
8288 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
8289 64c6d990 2019-07-11 stsp
8290 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
8291 64c6d990 2019-07-11 stsp return NULL;
8292 64c6d990 2019-07-11 stsp
8293 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8294 64c6d990 2019-07-11 stsp if (err)
8295 64c6d990 2019-07-11 stsp goto done;
8296 64c6d990 2019-07-11 stsp
8297 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8298 64c6d990 2019-07-11 stsp if (err)
8299 64c6d990 2019-07-11 stsp goto done;
8300 64c6d990 2019-07-11 stsp
8301 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
8302 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
8303 64c6d990 2019-07-11 stsp if (err)
8304 64c6d990 2019-07-11 stsp goto done;
8305 64c6d990 2019-07-11 stsp
8306 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
8307 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
8308 64c6d990 2019-07-11 stsp if (err)
8309 64c6d990 2019-07-11 stsp goto done;
8310 64c6d990 2019-07-11 stsp
8311 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
8312 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
8313 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
8314 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
8315 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
8316 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
8317 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
8318 64c6d990 2019-07-11 stsp done:
8319 64c6d990 2019-07-11 stsp if (tree1)
8320 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
8321 64c6d990 2019-07-11 stsp if (tree2)
8322 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
8323 64c6d990 2019-07-11 stsp if (commit)
8324 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
8325 64c6d990 2019-07-11 stsp if (parent_commit)
8326 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
8327 64c6d990 2019-07-11 stsp return err;
8328 ff0d2220 2019-07-11 stsp }
8329 ff0d2220 2019-07-11 stsp
8330 ff0d2220 2019-07-11 stsp static const struct got_error *
8331 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
8332 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
8333 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8334 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
8335 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
8336 af61c510 2019-07-19 stsp {
8337 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
8338 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
8339 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
8340 af61c510 2019-07-19 stsp struct got_object_qid *qid;
8341 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
8342 af61c510 2019-07-19 stsp
8343 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
8344 af61c510 2019-07-19 stsp if (err)
8345 af61c510 2019-07-19 stsp return err;
8346 af61c510 2019-07-19 stsp
8347 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8348 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8349 af61c510 2019-07-19 stsp if (err)
8350 af61c510 2019-07-19 stsp goto done;
8351 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8352 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
8353 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
8354 af61c510 2019-07-19 stsp if (err) {
8355 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
8356 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
8357 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
8358 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
8359 af61c510 2019-07-19 stsp "been reached?!?");
8360 ee780d5c 2020-01-04 stsp }
8361 ee780d5c 2020-01-04 stsp goto done;
8362 af61c510 2019-07-19 stsp } else {
8363 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
8364 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
8365 af61c510 2019-07-19 stsp if (err)
8366 af61c510 2019-07-19 stsp goto done;
8367 af61c510 2019-07-19 stsp
8368 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
8369 af61c510 2019-07-19 stsp if (err)
8370 af61c510 2019-07-19 stsp goto done;
8371 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
8372 af61c510 2019-07-19 stsp commit_id = parent_id;
8373 af61c510 2019-07-19 stsp }
8374 af61c510 2019-07-19 stsp }
8375 af61c510 2019-07-19 stsp done:
8376 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
8377 af61c510 2019-07-19 stsp return err;
8378 e600f124 2021-03-21 stsp }
8379 e600f124 2021-03-21 stsp
8380 e600f124 2021-03-21 stsp static const struct got_error *
8381 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8382 e600f124 2021-03-21 stsp {
8383 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8384 e600f124 2021-03-21 stsp time_t committer_time;
8385 e600f124 2021-03-21 stsp struct tm tm;
8386 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
8387 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
8388 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
8389 e600f124 2021-03-21 stsp
8390 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
8391 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
8392 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
8393 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8394 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
8395 e600f124 2021-03-21 stsp
8396 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
8397 e600f124 2021-03-21 stsp if (author0 == NULL)
8398 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
8399 e600f124 2021-03-21 stsp author = author0;
8400 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
8401 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
8402 e600f124 2021-03-21 stsp author = smallerthan + 1;
8403 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
8404 e600f124 2021-03-21 stsp
8405 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8406 e600f124 2021-03-21 stsp if (err)
8407 e600f124 2021-03-21 stsp goto done;
8408 e600f124 2021-03-21 stsp logmsg = logmsg0;
8409 e600f124 2021-03-21 stsp while (*logmsg == '\n')
8410 e600f124 2021-03-21 stsp logmsg++;
8411 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
8412 e600f124 2021-03-21 stsp if (newline)
8413 e600f124 2021-03-21 stsp *newline = '\0';
8414 e600f124 2021-03-21 stsp
8415 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
8416 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
8417 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
8418 e600f124 2021-03-21 stsp done:
8419 e600f124 2021-03-21 stsp free(author0);
8420 e600f124 2021-03-21 stsp free(logmsg0);
8421 643b85bc 2021-07-16 stsp return err;
8422 643b85bc 2021-07-16 stsp }
8423 643b85bc 2021-07-16 stsp
8424 643b85bc 2021-07-16 stsp static const struct got_error *
8425 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8426 643b85bc 2021-07-16 stsp struct got_repository *repo)
8427 643b85bc 2021-07-16 stsp {
8428 643b85bc 2021-07-16 stsp const struct got_error *err;
8429 643b85bc 2021-07-16 stsp char *id_str;
8430 643b85bc 2021-07-16 stsp
8431 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
8432 643b85bc 2021-07-16 stsp if (err)
8433 643b85bc 2021-07-16 stsp return err;
8434 643b85bc 2021-07-16 stsp
8435 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
8436 643b85bc 2021-07-16 stsp if (err)
8437 643b85bc 2021-07-16 stsp goto done;
8438 643b85bc 2021-07-16 stsp
8439 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8440 643b85bc 2021-07-16 stsp done:
8441 643b85bc 2021-07-16 stsp free(id_str);
8442 e600f124 2021-03-21 stsp return err;
8443 e600f124 2021-03-21 stsp }
8444 e600f124 2021-03-21 stsp
8445 e600f124 2021-03-21 stsp static const struct got_error *
8446 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
8447 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8448 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
8449 e600f124 2021-03-21 stsp struct got_repository *repo)
8450 e600f124 2021-03-21 stsp {
8451 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8452 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
8453 e600f124 2021-03-21 stsp char *refs_str = NULL;
8454 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
8455 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
8456 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
8457 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
8458 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
8459 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
8460 e600f124 2021-03-21 stsp char *custom_refs_str;
8461 e600f124 2021-03-21 stsp
8462 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8463 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
8464 e600f124 2021-03-21 stsp
8465 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8466 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
8467 e600f124 2021-03-21 stsp if (err)
8468 e600f124 2021-03-21 stsp goto done;
8469 e600f124 2021-03-21 stsp
8470 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8471 e600f124 2021-03-21 stsp if (err)
8472 e600f124 2021-03-21 stsp goto done;
8473 e600f124 2021-03-21 stsp
8474 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8475 e600f124 2021-03-21 stsp if (refs) {
8476 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8477 e600f124 2021-03-21 stsp if (err)
8478 e600f124 2021-03-21 stsp goto done;
8479 e600f124 2021-03-21 stsp }
8480 e600f124 2021-03-21 stsp
8481 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8482 e600f124 2021-03-21 stsp if (err)
8483 e600f124 2021-03-21 stsp goto done;
8484 e600f124 2021-03-21 stsp
8485 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8486 e600f124 2021-03-21 stsp if (err)
8487 e600f124 2021-03-21 stsp goto done;
8488 e600f124 2021-03-21 stsp
8489 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8490 e600f124 2021-03-21 stsp old_commit_id, new_commit_id, repo, check_cancelled, NULL);
8491 e600f124 2021-03-21 stsp if (err)
8492 e600f124 2021-03-21 stsp goto done;
8493 e600f124 2021-03-21 stsp
8494 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8495 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8496 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
8497 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8498 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
8499 e600f124 2021-03-21 stsp free(refs_str);
8500 e600f124 2021-03-21 stsp refs_str = NULL;
8501 e600f124 2021-03-21 stsp
8502 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8503 e600f124 2021-03-21 stsp if (err)
8504 e600f124 2021-03-21 stsp goto done;
8505 e600f124 2021-03-21 stsp
8506 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
8507 e600f124 2021-03-21 stsp if (err)
8508 e600f124 2021-03-21 stsp goto done;
8509 e600f124 2021-03-21 stsp
8510 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, yca_id);
8511 e600f124 2021-03-21 stsp if (err)
8512 e600f124 2021-03-21 stsp goto done;
8513 e600f124 2021-03-21 stsp
8514 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8515 e600f124 2021-03-21 stsp if (refs) {
8516 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
8517 e600f124 2021-03-21 stsp if (err)
8518 e600f124 2021-03-21 stsp goto done;
8519 e600f124 2021-03-21 stsp }
8520 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
8521 e600f124 2021-03-21 stsp yca_id_str,
8522 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8523 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
8524 e600f124 2021-03-21 stsp }
8525 e600f124 2021-03-21 stsp done:
8526 e600f124 2021-03-21 stsp free(custom_refs_str);
8527 e600f124 2021-03-21 stsp free(new_commit_id);
8528 e600f124 2021-03-21 stsp free(refs_str);
8529 e600f124 2021-03-21 stsp free(yca_id);
8530 e600f124 2021-03-21 stsp free(yca_id_str);
8531 e600f124 2021-03-21 stsp free(yca_brief_str);
8532 e600f124 2021-03-21 stsp if (new_commit)
8533 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
8534 e600f124 2021-03-21 stsp if (yca_commit)
8535 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
8536 e600f124 2021-03-21 stsp
8537 e600f124 2021-03-21 stsp return NULL;
8538 af61c510 2019-07-19 stsp }
8539 af61c510 2019-07-19 stsp
8540 af61c510 2019-07-19 stsp static const struct got_error *
8541 643b85bc 2021-07-16 stsp process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8542 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
8543 e600f124 2021-03-21 stsp {
8544 e600f124 2021-03-21 stsp const struct got_error *err;
8545 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
8546 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
8547 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8548 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
8549 e600f124 2021-03-21 stsp char *branch_name = NULL;
8550 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
8551 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
8552 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
8553 e600f124 2021-03-21 stsp
8554 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
8555 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
8556 e600f124 2021-03-21 stsp
8557 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8558 e600f124 2021-03-21 stsp if (err)
8559 e600f124 2021-03-21 stsp return err;
8560 e600f124 2021-03-21 stsp
8561 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8562 e600f124 2021-03-21 stsp if (err)
8563 e600f124 2021-03-21 stsp goto done;
8564 e600f124 2021-03-21 stsp
8565 e600f124 2021-03-21 stsp if (wanted_branch_name) {
8566 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8567 e600f124 2021-03-21 stsp wanted_branch_name += 11;
8568 e600f124 2021-03-21 stsp }
8569 e600f124 2021-03-21 stsp
8570 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8571 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
8572 e600f124 2021-03-21 stsp if (err)
8573 e600f124 2021-03-21 stsp goto done;
8574 e600f124 2021-03-21 stsp
8575 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
8576 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
8577 e600f124 2021-03-21 stsp char *slash;
8578 e600f124 2021-03-21 stsp
8579 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
8580 643b85bc 2021-07-16 stsp if (err)
8581 643b85bc 2021-07-16 stsp break;
8582 643b85bc 2021-07-16 stsp
8583 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
8584 e600f124 2021-03-21 stsp if (err)
8585 e600f124 2021-03-21 stsp break;
8586 e600f124 2021-03-21 stsp
8587 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
8588 e600f124 2021-03-21 stsp old_commit_id);
8589 e600f124 2021-03-21 stsp if (err)
8590 e600f124 2021-03-21 stsp break;
8591 e600f124 2021-03-21 stsp
8592 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
8593 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
8594 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
8595 e600f124 2021-03-21 stsp
8596 e600f124 2021-03-21 stsp while (refname[0] == '/')
8597 e600f124 2021-03-21 stsp refname++;
8598 e600f124 2021-03-21 stsp
8599 e600f124 2021-03-21 stsp branch_name = strdup(refname);
8600 e600f124 2021-03-21 stsp if (branch_name == NULL) {
8601 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
8602 e600f124 2021-03-21 stsp break;
8603 e600f124 2021-03-21 stsp }
8604 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
8605 e600f124 2021-03-21 stsp if (slash) {
8606 e600f124 2021-03-21 stsp *slash = '\0';
8607 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
8608 e600f124 2021-03-21 stsp }
8609 e600f124 2021-03-21 stsp
8610 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
8611 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
8612 9e822917 2021-03-23 stsp wanted_branch_found = 1;
8613 643b85bc 2021-07-16 stsp if (delete) {
8614 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
8615 643b85bc 2021-07-16 stsp old_commit_id, repo);
8616 643b85bc 2021-07-16 stsp } else {
8617 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
8618 abc59930 2021-09-05 naddy old_commit_id, old_commit, refs_idmap,
8619 abc59930 2021-09-05 naddy repo);
8620 643b85bc 2021-07-16 stsp }
8621 e600f124 2021-03-21 stsp if (err)
8622 e600f124 2021-03-21 stsp break;
8623 e600f124 2021-03-21 stsp }
8624 e600f124 2021-03-21 stsp
8625 e600f124 2021-03-21 stsp free(old_commit_id);
8626 e600f124 2021-03-21 stsp old_commit_id = NULL;
8627 e600f124 2021-03-21 stsp free(branch_name);
8628 e600f124 2021-03-21 stsp branch_name = NULL;
8629 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8630 e600f124 2021-03-21 stsp old_commit = NULL;
8631 e600f124 2021-03-21 stsp }
8632 9e822917 2021-03-23 stsp
8633 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
8634 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
8635 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
8636 9e822917 2021-03-23 stsp }
8637 e600f124 2021-03-21 stsp done:
8638 e600f124 2021-03-21 stsp if (refs_idmap)
8639 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
8640 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
8641 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
8642 e600f124 2021-03-21 stsp free(old_commit_id);
8643 e600f124 2021-03-21 stsp free(branch_name);
8644 e600f124 2021-03-21 stsp if (old_commit)
8645 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8646 e600f124 2021-03-21 stsp return err;
8647 e600f124 2021-03-21 stsp }
8648 e600f124 2021-03-21 stsp
8649 e600f124 2021-03-21 stsp static const struct got_error *
8650 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
8651 818c7501 2019-07-11 stsp {
8652 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
8653 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
8654 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
8655 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8656 818c7501 2019-07-11 stsp char *cwd = NULL;
8657 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
8658 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8659 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
8660 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
8661 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8662 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
8663 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8664 e600f124 2021-03-21 stsp int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8665 643b85bc 2021-07-16 stsp int delete_backups = 0;
8666 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8667 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
8668 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
8669 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
8670 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
8671 818c7501 2019-07-11 stsp
8672 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
8673 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
8674 818c7501 2019-07-11 stsp
8675 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
8676 818c7501 2019-07-11 stsp switch (ch) {
8677 818c7501 2019-07-11 stsp case 'a':
8678 818c7501 2019-07-11 stsp abort_rebase = 1;
8679 818c7501 2019-07-11 stsp break;
8680 818c7501 2019-07-11 stsp case 'c':
8681 818c7501 2019-07-11 stsp continue_rebase = 1;
8682 818c7501 2019-07-11 stsp break;
8683 e600f124 2021-03-21 stsp case 'l':
8684 e600f124 2021-03-21 stsp list_backups = 1;
8685 e600f124 2021-03-21 stsp break;
8686 643b85bc 2021-07-16 stsp case 'X':
8687 643b85bc 2021-07-16 stsp delete_backups = 1;
8688 643b85bc 2021-07-16 stsp break;
8689 818c7501 2019-07-11 stsp default:
8690 818c7501 2019-07-11 stsp usage_rebase();
8691 818c7501 2019-07-11 stsp /* NOTREACHED */
8692 818c7501 2019-07-11 stsp }
8693 818c7501 2019-07-11 stsp }
8694 818c7501 2019-07-11 stsp
8695 818c7501 2019-07-11 stsp argc -= optind;
8696 818c7501 2019-07-11 stsp argv += optind;
8697 818c7501 2019-07-11 stsp
8698 43012d58 2019-07-14 stsp #ifndef PROFILE
8699 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8700 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8701 43012d58 2019-07-14 stsp err(1, "pledge");
8702 43012d58 2019-07-14 stsp #endif
8703 e600f124 2021-03-21 stsp if (list_backups) {
8704 e600f124 2021-03-21 stsp if (abort_rebase)
8705 e600f124 2021-03-21 stsp option_conflict('l', 'a');
8706 e600f124 2021-03-21 stsp if (continue_rebase)
8707 e600f124 2021-03-21 stsp option_conflict('l', 'c');
8708 643b85bc 2021-07-16 stsp if (delete_backups)
8709 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8710 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
8711 643b85bc 2021-07-16 stsp usage_rebase();
8712 643b85bc 2021-07-16 stsp } else if (delete_backups) {
8713 643b85bc 2021-07-16 stsp if (abort_rebase)
8714 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
8715 643b85bc 2021-07-16 stsp if (continue_rebase)
8716 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
8717 643b85bc 2021-07-16 stsp if (list_backups)
8718 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8719 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
8720 818c7501 2019-07-11 stsp usage_rebase();
8721 e600f124 2021-03-21 stsp } else {
8722 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
8723 e600f124 2021-03-21 stsp usage_rebase();
8724 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
8725 e600f124 2021-03-21 stsp if (argc != 0)
8726 e600f124 2021-03-21 stsp usage_rebase();
8727 e600f124 2021-03-21 stsp } else if (argc != 1)
8728 e600f124 2021-03-21 stsp usage_rebase();
8729 e600f124 2021-03-21 stsp }
8730 818c7501 2019-07-11 stsp
8731 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
8732 818c7501 2019-07-11 stsp if (cwd == NULL) {
8733 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
8734 818c7501 2019-07-11 stsp goto done;
8735 818c7501 2019-07-11 stsp }
8736 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
8737 fa51e947 2020-03-27 stsp if (error) {
8738 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8739 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
8740 e600f124 2021-03-21 stsp goto done;
8741 e600f124 2021-03-21 stsp } else {
8742 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8743 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
8744 e600f124 2021-03-21 stsp "rebase", cwd);
8745 e600f124 2021-03-21 stsp goto done;
8746 e600f124 2021-03-21 stsp }
8747 fa51e947 2020-03-27 stsp }
8748 818c7501 2019-07-11 stsp
8749 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
8750 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8751 818c7501 2019-07-11 stsp if (error != NULL)
8752 818c7501 2019-07-11 stsp goto done;
8753 818c7501 2019-07-11 stsp
8754 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8755 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
8756 7ef62c4e 2020-02-24 stsp if (error)
8757 7ef62c4e 2020-02-24 stsp goto done;
8758 7ef62c4e 2020-02-24 stsp
8759 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8760 643b85bc 2021-07-16 stsp error = process_backup_refs(
8761 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8762 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
8763 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
8764 e600f124 2021-03-21 stsp }
8765 e600f124 2021-03-21 stsp
8766 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
8767 7ef62c4e 2020-02-24 stsp worktree);
8768 818c7501 2019-07-11 stsp if (error)
8769 7ef62c4e 2020-02-24 stsp goto done;
8770 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
8771 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8772 818c7501 2019-07-11 stsp goto done;
8773 7ef62c4e 2020-02-24 stsp }
8774 818c7501 2019-07-11 stsp
8775 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8776 818c7501 2019-07-11 stsp if (error)
8777 818c7501 2019-07-11 stsp goto done;
8778 818c7501 2019-07-11 stsp
8779 f6794adc 2019-07-23 stsp if (abort_rebase) {
8780 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8781 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8782 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8783 f6794adc 2019-07-23 stsp goto done;
8784 f6794adc 2019-07-23 stsp }
8785 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8786 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8787 3e3a69f1 2019-07-25 stsp worktree, repo);
8788 818c7501 2019-07-11 stsp if (error)
8789 818c7501 2019-07-11 stsp goto done;
8790 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
8791 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
8792 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8793 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
8794 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
8795 818c7501 2019-07-11 stsp if (error)
8796 818c7501 2019-07-11 stsp goto done;
8797 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8798 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8799 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
8800 818c7501 2019-07-11 stsp }
8801 818c7501 2019-07-11 stsp
8802 818c7501 2019-07-11 stsp if (continue_rebase) {
8803 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8804 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8805 f6794adc 2019-07-23 stsp goto done;
8806 f6794adc 2019-07-23 stsp }
8807 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8808 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8809 3e3a69f1 2019-07-25 stsp worktree, repo);
8810 818c7501 2019-07-11 stsp if (error)
8811 818c7501 2019-07-11 stsp goto done;
8812 818c7501 2019-07-11 stsp
8813 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8814 01757395 2019-07-12 stsp resume_commit_id, repo);
8815 818c7501 2019-07-11 stsp if (error)
8816 818c7501 2019-07-11 stsp goto done;
8817 818c7501 2019-07-11 stsp
8818 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
8819 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
8820 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
8821 818c7501 2019-07-11 stsp goto done;
8822 818c7501 2019-07-11 stsp }
8823 818c7501 2019-07-11 stsp } else {
8824 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
8825 818c7501 2019-07-11 stsp if (error != NULL)
8826 818c7501 2019-07-11 stsp goto done;
8827 ff0d2220 2019-07-11 stsp }
8828 818c7501 2019-07-11 stsp
8829 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8830 ff0d2220 2019-07-11 stsp if (error)
8831 ff0d2220 2019-07-11 stsp goto done;
8832 ff0d2220 2019-07-11 stsp
8833 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
8834 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
8835 a51a74b3 2019-07-27 stsp
8836 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
8837 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8838 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
8839 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8840 818c7501 2019-07-11 stsp if (error)
8841 818c7501 2019-07-11 stsp goto done;
8842 818c7501 2019-07-11 stsp if (yca_id == NULL) {
8843 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
8844 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
8845 818c7501 2019-07-11 stsp "with work tree's branch");
8846 818c7501 2019-07-11 stsp goto done;
8847 818c7501 2019-07-11 stsp }
8848 818c7501 2019-07-11 stsp
8849 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
8850 a51a74b3 2019-07-27 stsp if (error) {
8851 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
8852 a51a74b3 2019-07-27 stsp goto done;
8853 a51a74b3 2019-07-27 stsp error = NULL;
8854 a51a74b3 2019-07-27 stsp } else {
8855 df3ed485 2021-01-31 stsp static char msg[128];
8856 df3ed485 2021-01-31 stsp snprintf(msg, sizeof(msg),
8857 df3ed485 2021-01-31 stsp "%s is already based on %s",
8858 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
8859 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
8860 df3ed485 2021-01-31 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8861 a51a74b3 2019-07-27 stsp goto done;
8862 a51a74b3 2019-07-27 stsp }
8863 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
8864 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
8865 818c7501 2019-07-11 stsp if (error)
8866 818c7501 2019-07-11 stsp goto done;
8867 818c7501 2019-07-11 stsp }
8868 818c7501 2019-07-11 stsp
8869 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
8870 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8871 818c7501 2019-07-11 stsp if (error)
8872 818c7501 2019-07-11 stsp goto done;
8873 818c7501 2019-07-11 stsp
8874 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8875 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
8876 fc66b545 2019-08-12 stsp if (pid == NULL) {
8877 fc66b545 2019-08-12 stsp if (!continue_rebase) {
8878 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8879 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8880 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
8881 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
8882 fc66b545 2019-08-12 stsp if (error)
8883 fc66b545 2019-08-12 stsp goto done;
8884 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
8885 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
8886 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8887 9627c110 2020-04-18 stsp
8888 fc66b545 2019-08-12 stsp }
8889 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
8890 fc66b545 2019-08-12 stsp goto done;
8891 fc66b545 2019-08-12 stsp }
8892 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
8893 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
8894 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
8895 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8896 818c7501 2019-07-11 stsp commit = NULL;
8897 818c7501 2019-07-11 stsp if (error)
8898 818c7501 2019-07-11 stsp goto done;
8899 64c6d990 2019-07-11 stsp
8900 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
8901 38b0338b 2019-11-29 stsp if (continue_rebase) {
8902 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
8903 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
8904 e600f124 2021-03-21 stsp create_backup);
8905 38b0338b 2019-11-29 stsp goto done;
8906 38b0338b 2019-11-29 stsp } else {
8907 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
8908 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
8909 38b0338b 2019-11-29 stsp char *id_str;
8910 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
8911 38b0338b 2019-11-29 stsp new_base_branch);
8912 38b0338b 2019-11-29 stsp if (error)
8913 38b0338b 2019-11-29 stsp goto done;
8914 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
8915 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
8916 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
8917 38b0338b 2019-11-29 stsp free(id_str);
8918 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
8919 38b0338b 2019-11-29 stsp new_head_commit_id);
8920 38b0338b 2019-11-29 stsp if (error)
8921 38b0338b 2019-11-29 stsp goto done;
8922 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
8923 e600f124 2021-03-21 stsp create_backup = 0;
8924 38b0338b 2019-11-29 stsp }
8925 818c7501 2019-07-11 stsp }
8926 818c7501 2019-07-11 stsp
8927 818c7501 2019-07-11 stsp pid = NULL;
8928 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
8929 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8930 9627c110 2020-04-18 stsp
8931 818c7501 2019-07-11 stsp commit_id = qid->id;
8932 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
8933 818c7501 2019-07-11 stsp pid = qid;
8934 818c7501 2019-07-11 stsp
8935 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8936 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
8937 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
8938 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8939 818c7501 2019-07-11 stsp if (error)
8940 818c7501 2019-07-11 stsp goto done;
8941 9627c110 2020-04-18 stsp
8942 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8943 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8944 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8945 818c7501 2019-07-11 stsp
8946 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8947 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
8948 a0ea4fc0 2020-02-28 stsp if (error)
8949 a0ea4fc0 2020-02-28 stsp goto done;
8950 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8951 818c7501 2019-07-11 stsp break;
8952 01757395 2019-07-12 stsp }
8953 818c7501 2019-07-11 stsp
8954 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
8955 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
8956 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8957 818c7501 2019-07-11 stsp if (error)
8958 818c7501 2019-07-11 stsp goto done;
8959 818c7501 2019-07-11 stsp }
8960 818c7501 2019-07-11 stsp
8961 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8962 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
8963 818c7501 2019-07-11 stsp if (error)
8964 818c7501 2019-07-11 stsp goto done;
8965 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8966 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
8967 818c7501 2019-07-11 stsp } else
8968 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
8969 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
8970 818c7501 2019-07-11 stsp done:
8971 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
8972 818c7501 2019-07-11 stsp free(branch_head_commit_id);
8973 818c7501 2019-07-11 stsp free(resume_commit_id);
8974 818c7501 2019-07-11 stsp free(yca_id);
8975 818c7501 2019-07-11 stsp if (commit)
8976 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8977 818c7501 2019-07-11 stsp if (branch)
8978 818c7501 2019-07-11 stsp got_ref_close(branch);
8979 818c7501 2019-07-11 stsp if (new_base_branch)
8980 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
8981 818c7501 2019-07-11 stsp if (tmp_branch)
8982 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
8983 5ef14e63 2019-06-02 stsp if (worktree)
8984 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
8985 1d0f4054 2021-06-17 stsp if (repo) {
8986 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8987 1d0f4054 2021-06-17 stsp if (error == NULL)
8988 1d0f4054 2021-06-17 stsp error = close_err;
8989 1d0f4054 2021-06-17 stsp }
8990 5ef14e63 2019-06-02 stsp return error;
8991 0ebf8283 2019-07-24 stsp }
8992 0ebf8283 2019-07-24 stsp
8993 0ebf8283 2019-07-24 stsp __dead static void
8994 0ebf8283 2019-07-24 stsp usage_histedit(void)
8995 0ebf8283 2019-07-24 stsp {
8996 e600f124 2021-03-21 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
8997 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
8998 643b85bc 2021-07-16 stsp getprogname());
8999 0ebf8283 2019-07-24 stsp exit(1);
9000 0ebf8283 2019-07-24 stsp }
9001 0ebf8283 2019-07-24 stsp
9002 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
9003 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
9004 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
9005 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
9006 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
9007 0ebf8283 2019-07-24 stsp
9008 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
9009 0ebf8283 2019-07-24 stsp unsigned char code;
9010 0ebf8283 2019-07-24 stsp const char *name;
9011 0ebf8283 2019-07-24 stsp const char *desc;
9012 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
9013 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
9014 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9015 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9016 82997472 2020-01-29 stsp "be used" },
9017 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9018 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
9019 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
9020 0ebf8283 2019-07-24 stsp };
9021 0ebf8283 2019-07-24 stsp
9022 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
9023 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
9024 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
9025 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9026 0ebf8283 2019-07-24 stsp char *logmsg;
9027 0ebf8283 2019-07-24 stsp };
9028 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9029 0ebf8283 2019-07-24 stsp
9030 0ebf8283 2019-07-24 stsp static const struct got_error *
9031 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9032 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9033 0ebf8283 2019-07-24 stsp {
9034 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9035 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
9036 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9037 8138f3e1 2019-08-11 stsp int n;
9038 0ebf8283 2019-07-24 stsp
9039 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
9040 0ebf8283 2019-07-24 stsp if (err)
9041 0ebf8283 2019-07-24 stsp goto done;
9042 0ebf8283 2019-07-24 stsp
9043 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
9044 0ebf8283 2019-07-24 stsp if (err)
9045 0ebf8283 2019-07-24 stsp goto done;
9046 0ebf8283 2019-07-24 stsp
9047 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
9048 0ebf8283 2019-07-24 stsp if (err)
9049 0ebf8283 2019-07-24 stsp goto done;
9050 0ebf8283 2019-07-24 stsp
9051 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9052 0ebf8283 2019-07-24 stsp if (n < 0)
9053 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9054 0ebf8283 2019-07-24 stsp done:
9055 0ebf8283 2019-07-24 stsp if (commit)
9056 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9057 0ebf8283 2019-07-24 stsp free(id_str);
9058 0ebf8283 2019-07-24 stsp free(logmsg);
9059 0ebf8283 2019-07-24 stsp return err;
9060 0ebf8283 2019-07-24 stsp }
9061 0ebf8283 2019-07-24 stsp
9062 0ebf8283 2019-07-24 stsp static const struct got_error *
9063 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
9064 466785b9 2020-12-10 jrick FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
9065 0ebf8283 2019-07-24 stsp {
9066 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9067 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
9068 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
9069 0ebf8283 2019-07-24 stsp
9070 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9071 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9072 0ebf8283 2019-07-24 stsp
9073 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9074 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
9075 dbdddfee 2021-06-23 naddy if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9076 466785b9 2020-12-10 jrick histedit_cmd = "fold";
9077 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9078 0ebf8283 2019-07-24 stsp if (err)
9079 0ebf8283 2019-07-24 stsp break;
9080 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
9081 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9082 083957f4 2020-02-24 stsp if (n < 0) {
9083 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
9084 083957f4 2020-02-24 stsp break;
9085 083957f4 2020-02-24 stsp }
9086 083957f4 2020-02-24 stsp }
9087 0ebf8283 2019-07-24 stsp }
9088 0ebf8283 2019-07-24 stsp
9089 0ebf8283 2019-07-24 stsp return err;
9090 0ebf8283 2019-07-24 stsp }
9091 0ebf8283 2019-07-24 stsp
9092 0ebf8283 2019-07-24 stsp static const struct got_error *
9093 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
9094 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
9095 0ebf8283 2019-07-24 stsp {
9096 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9097 6059809a 2020-12-17 stsp size_t i;
9098 6059809a 2020-12-17 stsp int n;
9099 514f2ffe 2020-01-29 stsp char *id_str;
9100 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
9101 514f2ffe 2020-01-29 stsp
9102 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
9103 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
9104 514f2ffe 2020-01-29 stsp if (err)
9105 514f2ffe 2020-01-29 stsp return err;
9106 514f2ffe 2020-01-29 stsp
9107 514f2ffe 2020-01-29 stsp n = fprintf(f,
9108 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
9109 514f2ffe 2020-01-29 stsp "# commit %s\n"
9110 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
9111 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
9112 514f2ffe 2020-01-29 stsp if (n < 0) {
9113 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9114 514f2ffe 2020-01-29 stsp goto done;
9115 514f2ffe 2020-01-29 stsp }
9116 0ebf8283 2019-07-24 stsp
9117 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
9118 514f2ffe 2020-01-29 stsp if (n < 0) {
9119 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9120 514f2ffe 2020-01-29 stsp goto done;
9121 514f2ffe 2020-01-29 stsp }
9122 0ebf8283 2019-07-24 stsp
9123 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9124 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9125 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9126 0ebf8283 2019-07-24 stsp cmd->desc);
9127 0ebf8283 2019-07-24 stsp if (n < 0) {
9128 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9129 0ebf8283 2019-07-24 stsp break;
9130 0ebf8283 2019-07-24 stsp }
9131 0ebf8283 2019-07-24 stsp }
9132 514f2ffe 2020-01-29 stsp done:
9133 514f2ffe 2020-01-29 stsp free(id_str);
9134 0ebf8283 2019-07-24 stsp return err;
9135 0ebf8283 2019-07-24 stsp }
9136 0ebf8283 2019-07-24 stsp
9137 0ebf8283 2019-07-24 stsp static const struct got_error *
9138 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
9139 0ebf8283 2019-07-24 stsp {
9140 0ebf8283 2019-07-24 stsp static char msg[42];
9141 0ebf8283 2019-07-24 stsp int ret;
9142 0ebf8283 2019-07-24 stsp
9143 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9144 0ebf8283 2019-07-24 stsp lineno);
9145 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
9146 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9147 0ebf8283 2019-07-24 stsp
9148 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9149 0ebf8283 2019-07-24 stsp }
9150 0ebf8283 2019-07-24 stsp
9151 0ebf8283 2019-07-24 stsp static const struct got_error *
9152 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9153 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
9154 0ebf8283 2019-07-24 stsp {
9155 0ebf8283 2019-07-24 stsp const struct got_error *err;
9156 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
9157 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
9158 0ebf8283 2019-07-24 stsp
9159 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9160 0ebf8283 2019-07-24 stsp if (err)
9161 0ebf8283 2019-07-24 stsp return err;
9162 0ebf8283 2019-07-24 stsp
9163 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9164 0ebf8283 2019-07-24 stsp if (err)
9165 0ebf8283 2019-07-24 stsp goto done;
9166 0ebf8283 2019-07-24 stsp
9167 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9168 5943eee2 2019-08-13 stsp if (err)
9169 5943eee2 2019-08-13 stsp goto done;
9170 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9171 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9172 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
9173 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9174 0ebf8283 2019-07-24 stsp }
9175 0ebf8283 2019-07-24 stsp done:
9176 0ebf8283 2019-07-24 stsp if (folded_commit)
9177 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
9178 0ebf8283 2019-07-24 stsp free(id_str);
9179 5943eee2 2019-08-13 stsp free(folded_logmsg);
9180 0ebf8283 2019-07-24 stsp return err;
9181 0ebf8283 2019-07-24 stsp }
9182 0ebf8283 2019-07-24 stsp
9183 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
9184 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
9185 0ebf8283 2019-07-24 stsp {
9186 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
9187 0ebf8283 2019-07-24 stsp
9188 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
9189 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9190 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
9191 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9192 3f9de99f 2019-07-24 stsp folded = prev;
9193 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
9194 0ebf8283 2019-07-24 stsp }
9195 0ebf8283 2019-07-24 stsp
9196 0ebf8283 2019-07-24 stsp return folded;
9197 0ebf8283 2019-07-24 stsp }
9198 0ebf8283 2019-07-24 stsp
9199 0ebf8283 2019-07-24 stsp static const struct got_error *
9200 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9201 0ebf8283 2019-07-24 stsp struct got_repository *repo)
9202 0ebf8283 2019-07-24 stsp {
9203 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9204 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9205 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9206 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9207 1601cb9f 2020-09-11 naddy int logmsg_len;
9208 0ebf8283 2019-07-24 stsp int fd;
9209 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
9210 0ebf8283 2019-07-24 stsp
9211 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9212 0ebf8283 2019-07-24 stsp if (err)
9213 0ebf8283 2019-07-24 stsp return err;
9214 0ebf8283 2019-07-24 stsp
9215 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
9216 0ebf8283 2019-07-24 stsp if (folded) {
9217 0ebf8283 2019-07-24 stsp while (folded != hle) {
9218 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9219 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9220 3f9de99f 2019-07-24 stsp continue;
9221 3f9de99f 2019-07-24 stsp }
9222 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
9223 0ebf8283 2019-07-24 stsp logmsg, repo);
9224 0ebf8283 2019-07-24 stsp if (err)
9225 0ebf8283 2019-07-24 stsp goto done;
9226 0ebf8283 2019-07-24 stsp free(logmsg);
9227 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9228 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9229 0ebf8283 2019-07-24 stsp }
9230 0ebf8283 2019-07-24 stsp }
9231 0ebf8283 2019-07-24 stsp
9232 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9233 0ebf8283 2019-07-24 stsp if (err)
9234 0ebf8283 2019-07-24 stsp goto done;
9235 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9236 5943eee2 2019-08-13 stsp if (err)
9237 5943eee2 2019-08-13 stsp goto done;
9238 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
9239 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
9240 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
9241 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
9242 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9243 0ebf8283 2019-07-24 stsp goto done;
9244 0ebf8283 2019-07-24 stsp }
9245 0ebf8283 2019-07-24 stsp free(logmsg);
9246 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9247 0ebf8283 2019-07-24 stsp
9248 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9249 0ebf8283 2019-07-24 stsp if (err)
9250 0ebf8283 2019-07-24 stsp goto done;
9251 0ebf8283 2019-07-24 stsp
9252 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
9253 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
9254 0ebf8283 2019-07-24 stsp if (err)
9255 0ebf8283 2019-07-24 stsp goto done;
9256 0ebf8283 2019-07-24 stsp
9257 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
9258 0ebf8283 2019-07-24 stsp close(fd);
9259 0ebf8283 2019-07-24 stsp
9260 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9261 0ebf8283 2019-07-24 stsp if (err)
9262 0ebf8283 2019-07-24 stsp goto done;
9263 0ebf8283 2019-07-24 stsp
9264 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9265 0d5bb276 2020-12-15 stsp logmsg_len, 0);
9266 0ebf8283 2019-07-24 stsp if (err) {
9267 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9268 0ebf8283 2019-07-24 stsp goto done;
9269 71392a05 2020-12-13 stsp err = NULL;
9270 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
9271 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
9272 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
9273 0ebf8283 2019-07-24 stsp }
9274 0ebf8283 2019-07-24 stsp done:
9275 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9276 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
9277 0ebf8283 2019-07-24 stsp free(logmsg_path);
9278 0ebf8283 2019-07-24 stsp free(logmsg);
9279 5943eee2 2019-08-13 stsp free(orig_logmsg);
9280 0ebf8283 2019-07-24 stsp free(editor);
9281 0ebf8283 2019-07-24 stsp if (commit)
9282 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9283 0ebf8283 2019-07-24 stsp return err;
9284 5ef14e63 2019-06-02 stsp }
9285 0ebf8283 2019-07-24 stsp
9286 0ebf8283 2019-07-24 stsp static const struct got_error *
9287 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
9288 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9289 0ebf8283 2019-07-24 stsp {
9290 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9291 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
9292 6059809a 2020-12-17 stsp size_t i, size;
9293 0ebf8283 2019-07-24 stsp ssize_t len;
9294 6059809a 2020-12-17 stsp int lineno = 0;
9295 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9296 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
9297 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
9298 0ebf8283 2019-07-24 stsp
9299 0ebf8283 2019-07-24 stsp for (;;) {
9300 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
9301 0ebf8283 2019-07-24 stsp if (len == -1) {
9302 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
9303 0ebf8283 2019-07-24 stsp if (feof(f))
9304 0ebf8283 2019-07-24 stsp break;
9305 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
9306 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
9307 0ebf8283 2019-07-24 stsp break;
9308 0ebf8283 2019-07-24 stsp }
9309 0ebf8283 2019-07-24 stsp lineno++;
9310 0ebf8283 2019-07-24 stsp p = line;
9311 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9312 0ebf8283 2019-07-24 stsp p++;
9313 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
9314 0ebf8283 2019-07-24 stsp free(line);
9315 0ebf8283 2019-07-24 stsp line = NULL;
9316 0ebf8283 2019-07-24 stsp continue;
9317 0ebf8283 2019-07-24 stsp }
9318 0ebf8283 2019-07-24 stsp cmd = NULL;
9319 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9320 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
9321 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9322 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
9323 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
9324 0ebf8283 2019-07-24 stsp break;
9325 0ebf8283 2019-07-24 stsp }
9326 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9327 0ebf8283 2019-07-24 stsp p++;
9328 0ebf8283 2019-07-24 stsp break;
9329 0ebf8283 2019-07-24 stsp }
9330 0ebf8283 2019-07-24 stsp }
9331 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
9332 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9333 0ebf8283 2019-07-24 stsp break;
9334 0ebf8283 2019-07-24 stsp }
9335 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9336 0ebf8283 2019-07-24 stsp p++;
9337 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
9338 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
9339 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
9340 0ebf8283 2019-07-24 stsp break;
9341 0ebf8283 2019-07-24 stsp }
9342 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
9343 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9344 0ebf8283 2019-07-24 stsp if (err)
9345 0ebf8283 2019-07-24 stsp break;
9346 0ebf8283 2019-07-24 stsp } else {
9347 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
9348 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
9349 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9350 0ebf8283 2019-07-24 stsp break;
9351 0ebf8283 2019-07-24 stsp }
9352 0ebf8283 2019-07-24 stsp }
9353 0ebf8283 2019-07-24 stsp free(line);
9354 0ebf8283 2019-07-24 stsp line = NULL;
9355 0ebf8283 2019-07-24 stsp continue;
9356 0ebf8283 2019-07-24 stsp } else {
9357 0ebf8283 2019-07-24 stsp end = p;
9358 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
9359 0ebf8283 2019-07-24 stsp end++;
9360 0ebf8283 2019-07-24 stsp *end = '\0';
9361 0ebf8283 2019-07-24 stsp
9362 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
9363 0ebf8283 2019-07-24 stsp if (err) {
9364 0ebf8283 2019-07-24 stsp /* override error code */
9365 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9366 0ebf8283 2019-07-24 stsp break;
9367 0ebf8283 2019-07-24 stsp }
9368 0ebf8283 2019-07-24 stsp }
9369 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
9370 0ebf8283 2019-07-24 stsp if (hle == NULL) {
9371 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
9372 0ebf8283 2019-07-24 stsp break;
9373 0ebf8283 2019-07-24 stsp }
9374 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
9375 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
9376 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
9377 0ebf8283 2019-07-24 stsp commit_id = NULL;
9378 0ebf8283 2019-07-24 stsp free(line);
9379 0ebf8283 2019-07-24 stsp line = NULL;
9380 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9381 0ebf8283 2019-07-24 stsp }
9382 0ebf8283 2019-07-24 stsp
9383 0ebf8283 2019-07-24 stsp free(line);
9384 0ebf8283 2019-07-24 stsp free(commit_id);
9385 0ebf8283 2019-07-24 stsp return err;
9386 0ebf8283 2019-07-24 stsp }
9387 0ebf8283 2019-07-24 stsp
9388 0ebf8283 2019-07-24 stsp static const struct got_error *
9389 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
9390 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
9391 bfce7f83 2019-07-27 stsp {
9392 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
9393 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
9394 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9395 5b87815e 2020-03-05 stsp static char msg[92];
9396 bfce7f83 2019-07-27 stsp char *id_str;
9397 bfce7f83 2019-07-27 stsp
9398 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
9399 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9400 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
9401 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9402 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9403 5b87815e 2020-03-05 stsp
9404 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9405 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
9406 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9407 5b87815e 2020-03-05 stsp if (hle == hle2)
9408 5b87815e 2020-03-05 stsp continue;
9409 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
9410 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
9411 5b87815e 2020-03-05 stsp continue;
9412 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
9413 5b87815e 2020-03-05 stsp if (err)
9414 5b87815e 2020-03-05 stsp return err;
9415 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
9416 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
9417 5b87815e 2020-03-05 stsp free(id_str);
9418 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9419 5b87815e 2020-03-05 stsp }
9420 5b87815e 2020-03-05 stsp }
9421 bfce7f83 2019-07-27 stsp
9422 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9423 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9424 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9425 bfce7f83 2019-07-27 stsp break;
9426 bfce7f83 2019-07-27 stsp }
9427 bfce7f83 2019-07-27 stsp if (hle == NULL) {
9428 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
9429 bfce7f83 2019-07-27 stsp if (err)
9430 bfce7f83 2019-07-27 stsp return err;
9431 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
9432 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
9433 bfce7f83 2019-07-27 stsp free(id_str);
9434 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9435 bfce7f83 2019-07-27 stsp }
9436 bfce7f83 2019-07-27 stsp }
9437 bfce7f83 2019-07-27 stsp
9438 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9439 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9440 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9441 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
9442 bfce7f83 2019-07-27 stsp
9443 bfce7f83 2019-07-27 stsp return NULL;
9444 bfce7f83 2019-07-27 stsp }
9445 bfce7f83 2019-07-27 stsp
9446 bfce7f83 2019-07-27 stsp static const struct got_error *
9447 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
9448 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
9449 bfce7f83 2019-07-27 stsp struct got_repository *repo)
9450 0ebf8283 2019-07-24 stsp {
9451 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9452 0ebf8283 2019-07-24 stsp char *editor;
9453 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9454 0ebf8283 2019-07-24 stsp
9455 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9456 0ebf8283 2019-07-24 stsp if (err)
9457 0ebf8283 2019-07-24 stsp return err;
9458 0ebf8283 2019-07-24 stsp
9459 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
9460 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
9461 0ebf8283 2019-07-24 stsp goto done;
9462 0ebf8283 2019-07-24 stsp }
9463 0ebf8283 2019-07-24 stsp
9464 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9465 0ebf8283 2019-07-24 stsp if (f == NULL) {
9466 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
9467 0ebf8283 2019-07-24 stsp goto done;
9468 0ebf8283 2019-07-24 stsp }
9469 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9470 bfce7f83 2019-07-27 stsp if (err)
9471 bfce7f83 2019-07-27 stsp goto done;
9472 bfce7f83 2019-07-27 stsp
9473 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
9474 0ebf8283 2019-07-24 stsp done:
9475 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9476 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9477 0ebf8283 2019-07-24 stsp free(editor);
9478 0ebf8283 2019-07-24 stsp return err;
9479 0ebf8283 2019-07-24 stsp }
9480 0ebf8283 2019-07-24 stsp
9481 0ebf8283 2019-07-24 stsp static const struct got_error *
9482 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9483 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
9484 514f2ffe 2020-01-29 stsp struct got_repository *);
9485 0ebf8283 2019-07-24 stsp
9486 0ebf8283 2019-07-24 stsp static const struct got_error *
9487 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
9488 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
9489 466785b9 2020-12-10 jrick int edit_logmsg_only, int fold_only, struct got_repository *repo)
9490 0ebf8283 2019-07-24 stsp {
9491 0ebf8283 2019-07-24 stsp const struct got_error *err;
9492 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9493 0ebf8283 2019-07-24 stsp char *path = NULL;
9494 0ebf8283 2019-07-24 stsp
9495 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
9496 0ebf8283 2019-07-24 stsp if (err)
9497 0ebf8283 2019-07-24 stsp return err;
9498 0ebf8283 2019-07-24 stsp
9499 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
9500 0ebf8283 2019-07-24 stsp if (err)
9501 0ebf8283 2019-07-24 stsp goto done;
9502 0ebf8283 2019-07-24 stsp
9503 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9504 466785b9 2020-12-10 jrick fold_only, repo);
9505 0ebf8283 2019-07-24 stsp if (err)
9506 0ebf8283 2019-07-24 stsp goto done;
9507 0ebf8283 2019-07-24 stsp
9508 466785b9 2020-12-10 jrick if (edit_logmsg_only || fold_only) {
9509 083957f4 2020-02-24 stsp rewind(f);
9510 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9511 083957f4 2020-02-24 stsp } else {
9512 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
9513 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
9514 0ebf8283 2019-07-24 stsp goto done;
9515 083957f4 2020-02-24 stsp }
9516 083957f4 2020-02-24 stsp f = NULL;
9517 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
9518 083957f4 2020-02-24 stsp if (err) {
9519 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9520 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9521 083957f4 2020-02-24 stsp goto done;
9522 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
9523 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
9524 083957f4 2020-02-24 stsp }
9525 0ebf8283 2019-07-24 stsp }
9526 0ebf8283 2019-07-24 stsp done:
9527 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9528 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9529 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
9530 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
9531 0ebf8283 2019-07-24 stsp free(path);
9532 0ebf8283 2019-07-24 stsp return err;
9533 0ebf8283 2019-07-24 stsp }
9534 0ebf8283 2019-07-24 stsp
9535 0ebf8283 2019-07-24 stsp static const struct got_error *
9536 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
9537 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9538 0ebf8283 2019-07-24 stsp {
9539 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9540 0ebf8283 2019-07-24 stsp char *path = NULL;
9541 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9542 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9543 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9544 0ebf8283 2019-07-24 stsp
9545 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
9546 0ebf8283 2019-07-24 stsp if (err)
9547 0ebf8283 2019-07-24 stsp return err;
9548 0ebf8283 2019-07-24 stsp
9549 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
9550 0ebf8283 2019-07-24 stsp if (f == NULL) {
9551 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9552 0ebf8283 2019-07-24 stsp goto done;
9553 0ebf8283 2019-07-24 stsp }
9554 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9555 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9556 0ebf8283 2019-07-24 stsp repo);
9557 0ebf8283 2019-07-24 stsp if (err)
9558 0ebf8283 2019-07-24 stsp break;
9559 0ebf8283 2019-07-24 stsp
9560 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9561 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
9562 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
9563 0ebf8283 2019-07-24 stsp if (n < 0) {
9564 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9565 0ebf8283 2019-07-24 stsp break;
9566 0ebf8283 2019-07-24 stsp }
9567 0ebf8283 2019-07-24 stsp }
9568 0ebf8283 2019-07-24 stsp }
9569 0ebf8283 2019-07-24 stsp done:
9570 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9571 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9572 0ebf8283 2019-07-24 stsp free(path);
9573 0ebf8283 2019-07-24 stsp if (commit)
9574 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9575 0ebf8283 2019-07-24 stsp return err;
9576 0ebf8283 2019-07-24 stsp }
9577 0ebf8283 2019-07-24 stsp
9578 bfce7f83 2019-07-27 stsp void
9579 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
9580 bfce7f83 2019-07-27 stsp {
9581 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9582 bfce7f83 2019-07-27 stsp
9583 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
9584 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
9585 bfce7f83 2019-07-27 stsp free(hle);
9586 bfce7f83 2019-07-27 stsp }
9587 bfce7f83 2019-07-27 stsp }
9588 bfce7f83 2019-07-27 stsp
9589 0ebf8283 2019-07-24 stsp static const struct got_error *
9590 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
9591 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
9592 0ebf8283 2019-07-24 stsp {
9593 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9594 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9595 0ebf8283 2019-07-24 stsp
9596 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9597 0ebf8283 2019-07-24 stsp if (f == NULL) {
9598 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9599 0ebf8283 2019-07-24 stsp goto done;
9600 0ebf8283 2019-07-24 stsp }
9601 0ebf8283 2019-07-24 stsp
9602 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9603 0ebf8283 2019-07-24 stsp done:
9604 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9605 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9606 0ebf8283 2019-07-24 stsp return err;
9607 0ebf8283 2019-07-24 stsp }
9608 0ebf8283 2019-07-24 stsp
9609 0ebf8283 2019-07-24 stsp static const struct got_error *
9610 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9611 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
9612 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
9613 0ebf8283 2019-07-24 stsp {
9614 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
9615 0ebf8283 2019-07-24 stsp int resp = ' ';
9616 0ebf8283 2019-07-24 stsp
9617 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
9618 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9619 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
9620 0ebf8283 2019-07-24 stsp resp = getchar();
9621 a61a4414 2019-08-07 stsp if (resp == '\n')
9622 a61a4414 2019-08-07 stsp resp = getchar();
9623 426ebf2e 2019-07-25 stsp if (resp == 'c') {
9624 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9625 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
9626 bfce7f83 2019-07-27 stsp repo);
9627 426ebf2e 2019-07-25 stsp if (err) {
9628 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9629 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9630 426ebf2e 2019-07-25 stsp break;
9631 bfce7f83 2019-07-27 stsp prev_err = err;
9632 426ebf2e 2019-07-25 stsp resp = ' ';
9633 426ebf2e 2019-07-25 stsp continue;
9634 426ebf2e 2019-07-25 stsp }
9635 bfce7f83 2019-07-27 stsp break;
9636 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
9637 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9638 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
9639 466785b9 2020-12-10 jrick commits, branch_name, 0, 0, repo);
9640 426ebf2e 2019-07-25 stsp if (err) {
9641 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9642 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9643 426ebf2e 2019-07-25 stsp break;
9644 bfce7f83 2019-07-27 stsp prev_err = err;
9645 426ebf2e 2019-07-25 stsp resp = ' ';
9646 426ebf2e 2019-07-25 stsp continue;
9647 426ebf2e 2019-07-25 stsp }
9648 bfce7f83 2019-07-27 stsp break;
9649 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
9650 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9651 0ebf8283 2019-07-24 stsp break;
9652 426ebf2e 2019-07-25 stsp } else
9653 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
9654 0ebf8283 2019-07-24 stsp }
9655 0ebf8283 2019-07-24 stsp
9656 0ebf8283 2019-07-24 stsp return err;
9657 0ebf8283 2019-07-24 stsp }
9658 0ebf8283 2019-07-24 stsp
9659 0ebf8283 2019-07-24 stsp static const struct got_error *
9660 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
9661 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9662 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
9663 0ebf8283 2019-07-24 stsp {
9664 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9665 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9666 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9667 3e3a69f1 2019-07-25 stsp branch, repo);
9668 0ebf8283 2019-07-24 stsp }
9669 0ebf8283 2019-07-24 stsp
9670 0ebf8283 2019-07-24 stsp static const struct got_error *
9671 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
9672 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9673 0ebf8283 2019-07-24 stsp {
9674 0ebf8283 2019-07-24 stsp const struct got_error *err;
9675 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9676 0ebf8283 2019-07-24 stsp
9677 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
9678 0ebf8283 2019-07-24 stsp if (err)
9679 0ebf8283 2019-07-24 stsp goto done;
9680 0ebf8283 2019-07-24 stsp
9681 0ebf8283 2019-07-24 stsp if (new_id) {
9682 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
9683 0ebf8283 2019-07-24 stsp if (err)
9684 0ebf8283 2019-07-24 stsp goto done;
9685 0ebf8283 2019-07-24 stsp }
9686 0ebf8283 2019-07-24 stsp
9687 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
9688 0ebf8283 2019-07-24 stsp if (new_id_str)
9689 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
9690 0ebf8283 2019-07-24 stsp
9691 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9692 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
9693 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
9694 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9695 0ebf8283 2019-07-24 stsp goto done;
9696 0ebf8283 2019-07-24 stsp }
9697 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
9698 0ebf8283 2019-07-24 stsp } else {
9699 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
9700 0ebf8283 2019-07-24 stsp if (err)
9701 0ebf8283 2019-07-24 stsp goto done;
9702 0ebf8283 2019-07-24 stsp }
9703 0ebf8283 2019-07-24 stsp
9704 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
9705 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
9706 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
9707 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
9708 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
9709 0ebf8283 2019-07-24 stsp break;
9710 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
9711 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
9712 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9713 0ebf8283 2019-07-24 stsp logmsg);
9714 0ebf8283 2019-07-24 stsp break;
9715 0ebf8283 2019-07-24 stsp default:
9716 0ebf8283 2019-07-24 stsp break;
9717 0ebf8283 2019-07-24 stsp }
9718 0ebf8283 2019-07-24 stsp done:
9719 0ebf8283 2019-07-24 stsp free(old_id_str);
9720 0ebf8283 2019-07-24 stsp free(new_id_str);
9721 0ebf8283 2019-07-24 stsp return err;
9722 0ebf8283 2019-07-24 stsp }
9723 0ebf8283 2019-07-24 stsp
9724 0ebf8283 2019-07-24 stsp static const struct got_error *
9725 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
9726 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
9727 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9728 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
9729 0ebf8283 2019-07-24 stsp {
9730 0ebf8283 2019-07-24 stsp const struct got_error *err;
9731 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9732 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
9733 0ebf8283 2019-07-24 stsp
9734 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9735 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
9736 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9737 0ebf8283 2019-07-24 stsp if (err)
9738 0ebf8283 2019-07-24 stsp return err;
9739 0ebf8283 2019-07-24 stsp }
9740 0ebf8283 2019-07-24 stsp
9741 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9742 0ebf8283 2019-07-24 stsp if (err)
9743 0ebf8283 2019-07-24 stsp return err;
9744 0ebf8283 2019-07-24 stsp
9745 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9746 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
9747 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
9748 0ebf8283 2019-07-24 stsp if (err) {
9749 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9750 0ebf8283 2019-07-24 stsp goto done;
9751 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
9752 0ebf8283 2019-07-24 stsp } else {
9753 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
9754 0ebf8283 2019-07-24 stsp free(new_commit_id);
9755 0ebf8283 2019-07-24 stsp }
9756 0ebf8283 2019-07-24 stsp done:
9757 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9758 0ebf8283 2019-07-24 stsp return err;
9759 0ebf8283 2019-07-24 stsp }
9760 0ebf8283 2019-07-24 stsp
9761 0ebf8283 2019-07-24 stsp static const struct got_error *
9762 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
9763 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9764 0ebf8283 2019-07-24 stsp {
9765 0ebf8283 2019-07-24 stsp const struct got_error *error;
9766 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9767 0ebf8283 2019-07-24 stsp
9768 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9769 0ebf8283 2019-07-24 stsp repo);
9770 0ebf8283 2019-07-24 stsp if (error)
9771 0ebf8283 2019-07-24 stsp return error;
9772 0ebf8283 2019-07-24 stsp
9773 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9774 0ebf8283 2019-07-24 stsp if (error)
9775 0ebf8283 2019-07-24 stsp return error;
9776 0ebf8283 2019-07-24 stsp
9777 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
9778 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9779 0ebf8283 2019-07-24 stsp return error;
9780 ab20a43a 2020-01-29 stsp }
9781 ab20a43a 2020-01-29 stsp
9782 ab20a43a 2020-01-29 stsp static const struct got_error *
9783 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
9784 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
9785 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9786 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9787 ab20a43a 2020-01-29 stsp {
9788 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
9789 ab20a43a 2020-01-29 stsp
9790 ab20a43a 2020-01-29 stsp switch (status) {
9791 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9792 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9793 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9794 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
9795 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9796 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9797 ab20a43a 2020-01-29 stsp default:
9798 ab20a43a 2020-01-29 stsp break;
9799 ab20a43a 2020-01-29 stsp }
9800 ab20a43a 2020-01-29 stsp
9801 ab20a43a 2020-01-29 stsp switch (staged_status) {
9802 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9803 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9804 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9805 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9806 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9807 ab20a43a 2020-01-29 stsp default:
9808 ab20a43a 2020-01-29 stsp break;
9809 ab20a43a 2020-01-29 stsp }
9810 ab20a43a 2020-01-29 stsp
9811 ab20a43a 2020-01-29 stsp return NULL;
9812 0ebf8283 2019-07-24 stsp }
9813 0ebf8283 2019-07-24 stsp
9814 0ebf8283 2019-07-24 stsp static const struct got_error *
9815 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
9816 0ebf8283 2019-07-24 stsp {
9817 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
9818 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
9819 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9820 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
9821 0ebf8283 2019-07-24 stsp char *cwd = NULL;
9822 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
9823 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
9824 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
9825 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
9826 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
9827 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9828 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
9829 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9830 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9831 466785b9 2020-12-10 jrick int edit_logmsg_only = 0, fold_only = 0;
9832 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
9833 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
9834 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9835 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
9836 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
9837 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
9838 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
9839 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
9840 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9841 0ebf8283 2019-07-24 stsp
9842 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
9843 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
9844 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
9845 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9846 0ebf8283 2019-07-24 stsp
9847 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
9848 0ebf8283 2019-07-24 stsp switch (ch) {
9849 0ebf8283 2019-07-24 stsp case 'a':
9850 0ebf8283 2019-07-24 stsp abort_edit = 1;
9851 0ebf8283 2019-07-24 stsp break;
9852 0ebf8283 2019-07-24 stsp case 'c':
9853 0ebf8283 2019-07-24 stsp continue_edit = 1;
9854 0ebf8283 2019-07-24 stsp break;
9855 466785b9 2020-12-10 jrick case 'f':
9856 466785b9 2020-12-10 jrick fold_only = 1;
9857 466785b9 2020-12-10 jrick break;
9858 0ebf8283 2019-07-24 stsp case 'F':
9859 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
9860 0ebf8283 2019-07-24 stsp break;
9861 083957f4 2020-02-24 stsp case 'm':
9862 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
9863 083957f4 2020-02-24 stsp break;
9864 e600f124 2021-03-21 stsp case 'l':
9865 e600f124 2021-03-21 stsp list_backups = 1;
9866 e600f124 2021-03-21 stsp break;
9867 643b85bc 2021-07-16 stsp case 'X':
9868 643b85bc 2021-07-16 stsp delete_backups = 1;
9869 643b85bc 2021-07-16 stsp break;
9870 0ebf8283 2019-07-24 stsp default:
9871 0ebf8283 2019-07-24 stsp usage_histedit();
9872 0ebf8283 2019-07-24 stsp /* NOTREACHED */
9873 0ebf8283 2019-07-24 stsp }
9874 0ebf8283 2019-07-24 stsp }
9875 0ebf8283 2019-07-24 stsp
9876 0ebf8283 2019-07-24 stsp argc -= optind;
9877 0ebf8283 2019-07-24 stsp argv += optind;
9878 0ebf8283 2019-07-24 stsp
9879 0ebf8283 2019-07-24 stsp #ifndef PROFILE
9880 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9881 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
9882 0ebf8283 2019-07-24 stsp err(1, "pledge");
9883 0ebf8283 2019-07-24 stsp #endif
9884 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
9885 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
9886 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
9887 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
9888 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
9889 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
9890 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
9891 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
9892 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
9893 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
9894 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
9895 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
9896 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
9897 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
9898 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
9899 b3805337 2020-12-13 stsp option_conflict('F', 'f');
9900 e600f124 2021-03-21 stsp if (list_backups) {
9901 e600f124 2021-03-21 stsp if (abort_edit)
9902 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9903 e600f124 2021-03-21 stsp if (continue_edit)
9904 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9905 e600f124 2021-03-21 stsp if (edit_script_path)
9906 e600f124 2021-03-21 stsp option_conflict('l', 'F');
9907 e600f124 2021-03-21 stsp if (edit_logmsg_only)
9908 e600f124 2021-03-21 stsp option_conflict('l', 'm');
9909 e600f124 2021-03-21 stsp if (fold_only)
9910 e600f124 2021-03-21 stsp option_conflict('l', 'f');
9911 643b85bc 2021-07-16 stsp if (delete_backups)
9912 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9913 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9914 e600f124 2021-03-21 stsp usage_histedit();
9915 643b85bc 2021-07-16 stsp } else if (delete_backups) {
9916 643b85bc 2021-07-16 stsp if (abort_edit)
9917 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
9918 643b85bc 2021-07-16 stsp if (continue_edit)
9919 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
9920 643b85bc 2021-07-16 stsp if (edit_script_path)
9921 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
9922 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
9923 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
9924 643b85bc 2021-07-16 stsp if (fold_only)
9925 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
9926 643b85bc 2021-07-16 stsp if (list_backups)
9927 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
9928 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
9929 643b85bc 2021-07-16 stsp usage_histedit();
9930 e600f124 2021-03-21 stsp } else if (argc != 0)
9931 0ebf8283 2019-07-24 stsp usage_histedit();
9932 0ebf8283 2019-07-24 stsp
9933 0ebf8283 2019-07-24 stsp /*
9934 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
9935 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
9936 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
9937 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
9938 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
9939 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
9940 0ebf8283 2019-07-24 stsp */
9941 0ebf8283 2019-07-24 stsp
9942 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
9943 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
9944 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
9945 0ebf8283 2019-07-24 stsp goto done;
9946 0ebf8283 2019-07-24 stsp }
9947 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
9948 fa51e947 2020-03-27 stsp if (error) {
9949 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9950 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
9951 e600f124 2021-03-21 stsp goto done;
9952 e600f124 2021-03-21 stsp } else {
9953 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9954 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
9955 e600f124 2021-03-21 stsp "histedit", cwd);
9956 e600f124 2021-03-21 stsp goto done;
9957 e600f124 2021-03-21 stsp }
9958 e600f124 2021-03-21 stsp }
9959 e600f124 2021-03-21 stsp
9960 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9961 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
9962 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
9963 e600f124 2021-03-21 stsp NULL);
9964 e600f124 2021-03-21 stsp if (error != NULL)
9965 e600f124 2021-03-21 stsp goto done;
9966 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9967 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
9968 e600f124 2021-03-21 stsp if (error)
9969 e600f124 2021-03-21 stsp goto done;
9970 643b85bc 2021-07-16 stsp error = process_backup_refs(
9971 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
9972 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
9973 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
9974 fa51e947 2020-03-27 stsp }
9975 0ebf8283 2019-07-24 stsp
9976 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9977 c9956ddf 2019-09-08 stsp NULL);
9978 0ebf8283 2019-07-24 stsp if (error != NULL)
9979 0ebf8283 2019-07-24 stsp goto done;
9980 0ebf8283 2019-07-24 stsp
9981 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9982 0ebf8283 2019-07-24 stsp if (error)
9983 0ebf8283 2019-07-24 stsp goto done;
9984 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
9985 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
9986 0ebf8283 2019-07-24 stsp goto done;
9987 0ebf8283 2019-07-24 stsp }
9988 0ebf8283 2019-07-24 stsp
9989 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
9990 0ebf8283 2019-07-24 stsp if (error)
9991 0ebf8283 2019-07-24 stsp goto done;
9992 0ebf8283 2019-07-24 stsp
9993 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
9994 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9995 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
9996 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
9997 083957f4 2020-02-24 stsp "before the -m option can be used");
9998 083957f4 2020-02-24 stsp goto done;
9999 083957f4 2020-02-24 stsp }
10000 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
10001 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
10002 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
10003 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
10004 466785b9 2020-12-10 jrick "before the -f option can be used");
10005 466785b9 2020-12-10 jrick goto done;
10006 466785b9 2020-12-10 jrick }
10007 083957f4 2020-02-24 stsp
10008 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
10009 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10010 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10011 3e3a69f1 2019-07-25 stsp worktree, repo);
10012 0ebf8283 2019-07-24 stsp if (error)
10013 0ebf8283 2019-07-24 stsp goto done;
10014 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10015 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10016 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
10017 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
10018 0ebf8283 2019-07-24 stsp if (error)
10019 0ebf8283 2019-07-24 stsp goto done;
10020 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
10021 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10022 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10023 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
10024 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
10025 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10026 0ebf8283 2019-07-24 stsp goto done;
10027 0ebf8283 2019-07-24 stsp }
10028 0ebf8283 2019-07-24 stsp
10029 0ebf8283 2019-07-24 stsp if (continue_edit) {
10030 0ebf8283 2019-07-24 stsp char *path;
10031 0ebf8283 2019-07-24 stsp
10032 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
10033 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10034 0ebf8283 2019-07-24 stsp goto done;
10035 0ebf8283 2019-07-24 stsp }
10036 0ebf8283 2019-07-24 stsp
10037 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
10038 0ebf8283 2019-07-24 stsp if (error)
10039 0ebf8283 2019-07-24 stsp goto done;
10040 0ebf8283 2019-07-24 stsp
10041 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
10042 0ebf8283 2019-07-24 stsp free(path);
10043 0ebf8283 2019-07-24 stsp if (error)
10044 0ebf8283 2019-07-24 stsp goto done;
10045 0ebf8283 2019-07-24 stsp
10046 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10047 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10048 3e3a69f1 2019-07-25 stsp worktree, repo);
10049 0ebf8283 2019-07-24 stsp if (error)
10050 0ebf8283 2019-07-24 stsp goto done;
10051 0ebf8283 2019-07-24 stsp
10052 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10053 0ebf8283 2019-07-24 stsp if (error)
10054 0ebf8283 2019-07-24 stsp goto done;
10055 0ebf8283 2019-07-24 stsp
10056 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10057 0ebf8283 2019-07-24 stsp head_commit_id);
10058 0ebf8283 2019-07-24 stsp if (error)
10059 0ebf8283 2019-07-24 stsp goto done;
10060 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10061 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10062 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10063 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10064 3aac7cf7 2019-07-25 stsp goto done;
10065 3aac7cf7 2019-07-25 stsp }
10066 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10067 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
10068 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10069 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10070 0ebf8283 2019-07-24 stsp commit = NULL;
10071 0ebf8283 2019-07-24 stsp if (error)
10072 0ebf8283 2019-07-24 stsp goto done;
10073 0ebf8283 2019-07-24 stsp } else {
10074 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
10075 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
10076 0ebf8283 2019-07-24 stsp goto done;
10077 0ebf8283 2019-07-24 stsp }
10078 0ebf8283 2019-07-24 stsp
10079 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
10080 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
10081 0ebf8283 2019-07-24 stsp if (error != NULL)
10082 0ebf8283 2019-07-24 stsp goto done;
10083 0ebf8283 2019-07-24 stsp
10084 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10085 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10086 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
10087 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
10088 c7d20a3f 2019-07-30 stsp goto done;
10089 c7d20a3f 2019-07-30 stsp }
10090 c7d20a3f 2019-07-30 stsp
10091 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10092 bfce7f83 2019-07-27 stsp got_ref_close(branch);
10093 bfce7f83 2019-07-27 stsp branch = NULL;
10094 0ebf8283 2019-07-24 stsp if (error)
10095 0ebf8283 2019-07-24 stsp goto done;
10096 0ebf8283 2019-07-24 stsp
10097 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10098 0ebf8283 2019-07-24 stsp head_commit_id);
10099 0ebf8283 2019-07-24 stsp if (error)
10100 0ebf8283 2019-07-24 stsp goto done;
10101 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10102 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10103 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10104 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10105 3aac7cf7 2019-07-25 stsp goto done;
10106 3aac7cf7 2019-07-25 stsp }
10107 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10108 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
10109 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
10110 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10111 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10112 0ebf8283 2019-07-24 stsp commit = NULL;
10113 0ebf8283 2019-07-24 stsp if (error)
10114 0ebf8283 2019-07-24 stsp goto done;
10115 0ebf8283 2019-07-24 stsp
10116 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
10117 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10118 c5996fff 2020-01-29 stsp goto done;
10119 c5996fff 2020-01-29 stsp }
10120 c5996fff 2020-01-29 stsp
10121 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10122 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
10123 bfce7f83 2019-07-27 stsp if (error)
10124 bfce7f83 2019-07-27 stsp goto done;
10125 bfce7f83 2019-07-27 stsp
10126 0ebf8283 2019-07-24 stsp if (edit_script_path) {
10127 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
10128 0ebf8283 2019-07-24 stsp edit_script_path, repo);
10129 6ba2bea2 2019-07-27 stsp if (error) {
10130 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10131 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10132 9627c110 2020-04-18 stsp update_progress, &upa);
10133 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10134 0ebf8283 2019-07-24 stsp goto done;
10135 6ba2bea2 2019-07-27 stsp }
10136 0ebf8283 2019-07-24 stsp } else {
10137 514f2ffe 2020-01-29 stsp const char *branch_name;
10138 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
10139 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
10140 514f2ffe 2020-01-29 stsp branch_name += 11;
10141 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
10142 466785b9 2020-12-10 jrick branch_name, edit_logmsg_only, fold_only, repo);
10143 6ba2bea2 2019-07-27 stsp if (error) {
10144 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10145 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10146 9627c110 2020-04-18 stsp update_progress, &upa);
10147 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10148 0ebf8283 2019-07-24 stsp goto done;
10149 6ba2bea2 2019-07-27 stsp }
10150 0ebf8283 2019-07-24 stsp
10151 0ebf8283 2019-07-24 stsp }
10152 0ebf8283 2019-07-24 stsp
10153 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
10154 0ebf8283 2019-07-24 stsp repo);
10155 6ba2bea2 2019-07-27 stsp if (error) {
10156 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10157 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10158 9627c110 2020-04-18 stsp update_progress, &upa);
10159 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10160 0ebf8283 2019-07-24 stsp goto done;
10161 6ba2bea2 2019-07-27 stsp }
10162 0ebf8283 2019-07-24 stsp
10163 0ebf8283 2019-07-24 stsp }
10164 0ebf8283 2019-07-24 stsp
10165 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
10166 4ec14e60 2019-08-28 hiltjo if (error)
10167 0ebf8283 2019-07-24 stsp goto done;
10168 0ebf8283 2019-07-24 stsp
10169 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10170 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
10171 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
10172 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
10173 0ebf8283 2019-07-24 stsp continue;
10174 0ebf8283 2019-07-24 stsp
10175 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
10176 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10177 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
10178 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
10179 62d463ca 2020-10-20 naddy repo);
10180 ab20a43a 2020-01-29 stsp if (error)
10181 ab20a43a 2020-01-29 stsp goto done;
10182 0ebf8283 2019-07-24 stsp } else {
10183 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
10184 ab20a43a 2020-01-29 stsp int have_changes = 0;
10185 ab20a43a 2020-01-29 stsp
10186 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
10187 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
10188 ab20a43a 2020-01-29 stsp if (error)
10189 ab20a43a 2020-01-29 stsp goto done;
10190 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
10191 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
10192 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
10193 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
10194 ab20a43a 2020-01-29 stsp if (error) {
10195 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
10196 ab20a43a 2020-01-29 stsp goto done;
10197 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
10198 ab20a43a 2020-01-29 stsp goto done;
10199 ab20a43a 2020-01-29 stsp }
10200 ab20a43a 2020-01-29 stsp if (have_changes) {
10201 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
10202 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
10203 ab20a43a 2020-01-29 stsp if (error)
10204 ab20a43a 2020-01-29 stsp goto done;
10205 ab20a43a 2020-01-29 stsp } else {
10206 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
10207 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
10208 ab20a43a 2020-01-29 stsp if (error)
10209 ab20a43a 2020-01-29 stsp goto done;
10210 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
10211 ab20a43a 2020-01-29 stsp hle, NULL);
10212 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
10213 ab20a43a 2020-01-29 stsp commit = NULL;
10214 ab20a43a 2020-01-29 stsp if (error)
10215 ab20a43a 2020-01-29 stsp goto done;
10216 ab20a43a 2020-01-29 stsp }
10217 0ebf8283 2019-07-24 stsp }
10218 0ebf8283 2019-07-24 stsp continue;
10219 0ebf8283 2019-07-24 stsp }
10220 0ebf8283 2019-07-24 stsp
10221 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10222 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10223 0ebf8283 2019-07-24 stsp if (error)
10224 0ebf8283 2019-07-24 stsp goto done;
10225 0ebf8283 2019-07-24 stsp continue;
10226 0ebf8283 2019-07-24 stsp }
10227 0ebf8283 2019-07-24 stsp
10228 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10229 0ebf8283 2019-07-24 stsp hle->commit_id);
10230 0ebf8283 2019-07-24 stsp if (error)
10231 0ebf8283 2019-07-24 stsp goto done;
10232 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10233 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10234 0ebf8283 2019-07-24 stsp
10235 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
10236 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
10237 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
10238 0ebf8283 2019-07-24 stsp if (error)
10239 0ebf8283 2019-07-24 stsp goto done;
10240 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10241 0ebf8283 2019-07-24 stsp commit = NULL;
10242 0ebf8283 2019-07-24 stsp
10243 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10244 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
10245 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
10246 9627c110 2020-04-18 stsp
10247 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
10248 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
10249 a0ea4fc0 2020-02-28 stsp repo);
10250 a0ea4fc0 2020-02-28 stsp if (error)
10251 a0ea4fc0 2020-02-28 stsp goto done;
10252 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10253 0ebf8283 2019-07-24 stsp break;
10254 0ebf8283 2019-07-24 stsp }
10255 0ebf8283 2019-07-24 stsp
10256 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10257 0ebf8283 2019-07-24 stsp char *id_str;
10258 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
10259 0ebf8283 2019-07-24 stsp if (error)
10260 0ebf8283 2019-07-24 stsp goto done;
10261 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
10262 0ebf8283 2019-07-24 stsp id_str);
10263 0ebf8283 2019-07-24 stsp free(id_str);
10264 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10265 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
10266 3e3a69f1 2019-07-25 stsp fileindex);
10267 0ebf8283 2019-07-24 stsp goto done;
10268 0ebf8283 2019-07-24 stsp }
10269 0ebf8283 2019-07-24 stsp
10270 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10271 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10272 0ebf8283 2019-07-24 stsp if (error)
10273 0ebf8283 2019-07-24 stsp goto done;
10274 0ebf8283 2019-07-24 stsp continue;
10275 0ebf8283 2019-07-24 stsp }
10276 0ebf8283 2019-07-24 stsp
10277 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
10278 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
10279 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10280 0ebf8283 2019-07-24 stsp if (error)
10281 0ebf8283 2019-07-24 stsp goto done;
10282 0ebf8283 2019-07-24 stsp }
10283 0ebf8283 2019-07-24 stsp
10284 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
10285 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
10286 0ebf8283 2019-07-24 stsp if (error)
10287 0ebf8283 2019-07-24 stsp goto done;
10288 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10289 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
10290 0ebf8283 2019-07-24 stsp } else
10291 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
10292 3e3a69f1 2019-07-25 stsp branch, repo);
10293 0ebf8283 2019-07-24 stsp done:
10294 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
10295 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
10296 0ebf8283 2019-07-24 stsp free(head_commit_id);
10297 0ebf8283 2019-07-24 stsp free(base_commit_id);
10298 0ebf8283 2019-07-24 stsp free(resume_commit_id);
10299 0ebf8283 2019-07-24 stsp if (commit)
10300 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10301 0ebf8283 2019-07-24 stsp if (branch)
10302 0ebf8283 2019-07-24 stsp got_ref_close(branch);
10303 0ebf8283 2019-07-24 stsp if (tmp_branch)
10304 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
10305 0ebf8283 2019-07-24 stsp if (worktree)
10306 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
10307 1d0f4054 2021-06-17 stsp if (repo) {
10308 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10309 1d0f4054 2021-06-17 stsp if (error == NULL)
10310 1d0f4054 2021-06-17 stsp error = close_err;
10311 1d0f4054 2021-06-17 stsp }
10312 2822a352 2019-10-15 stsp return error;
10313 2822a352 2019-10-15 stsp }
10314 2822a352 2019-10-15 stsp
10315 2822a352 2019-10-15 stsp __dead static void
10316 2822a352 2019-10-15 stsp usage_integrate(void)
10317 2822a352 2019-10-15 stsp {
10318 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10319 2822a352 2019-10-15 stsp exit(1);
10320 2822a352 2019-10-15 stsp }
10321 2822a352 2019-10-15 stsp
10322 2822a352 2019-10-15 stsp static const struct got_error *
10323 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
10324 2822a352 2019-10-15 stsp {
10325 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
10326 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
10327 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
10328 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10329 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
10330 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10331 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
10332 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10333 9627c110 2020-04-18 stsp int ch;
10334 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10335 2822a352 2019-10-15 stsp
10336 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10337 2822a352 2019-10-15 stsp switch (ch) {
10338 2822a352 2019-10-15 stsp default:
10339 2822a352 2019-10-15 stsp usage_integrate();
10340 2822a352 2019-10-15 stsp /* NOTREACHED */
10341 2822a352 2019-10-15 stsp }
10342 2822a352 2019-10-15 stsp }
10343 2822a352 2019-10-15 stsp
10344 2822a352 2019-10-15 stsp argc -= optind;
10345 2822a352 2019-10-15 stsp argv += optind;
10346 2822a352 2019-10-15 stsp
10347 2822a352 2019-10-15 stsp if (argc != 1)
10348 2822a352 2019-10-15 stsp usage_integrate();
10349 2822a352 2019-10-15 stsp branch_arg = argv[0];
10350 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
10351 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10352 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
10353 2822a352 2019-10-15 stsp err(1, "pledge");
10354 b08a0ccd 2020-09-24 stsp #endif
10355 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
10356 2822a352 2019-10-15 stsp if (cwd == NULL) {
10357 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
10358 2822a352 2019-10-15 stsp goto done;
10359 2822a352 2019-10-15 stsp }
10360 2822a352 2019-10-15 stsp
10361 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
10362 fa51e947 2020-03-27 stsp if (error) {
10363 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10364 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
10365 fa51e947 2020-03-27 stsp cwd);
10366 2822a352 2019-10-15 stsp goto done;
10367 fa51e947 2020-03-27 stsp }
10368 2822a352 2019-10-15 stsp
10369 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
10370 2822a352 2019-10-15 stsp if (error)
10371 2822a352 2019-10-15 stsp goto done;
10372 2822a352 2019-10-15 stsp
10373 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10374 2822a352 2019-10-15 stsp NULL);
10375 2822a352 2019-10-15 stsp if (error != NULL)
10376 2822a352 2019-10-15 stsp goto done;
10377 2822a352 2019-10-15 stsp
10378 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10379 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
10380 2822a352 2019-10-15 stsp if (error)
10381 2822a352 2019-10-15 stsp goto done;
10382 2822a352 2019-10-15 stsp
10383 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10384 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
10385 2822a352 2019-10-15 stsp goto done;
10386 2822a352 2019-10-15 stsp }
10387 2822a352 2019-10-15 stsp
10388 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10389 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
10390 2822a352 2019-10-15 stsp if (error)
10391 2822a352 2019-10-15 stsp goto done;
10392 2822a352 2019-10-15 stsp
10393 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
10394 2822a352 2019-10-15 stsp if (refname == NULL) {
10395 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10396 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10397 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10398 2822a352 2019-10-15 stsp goto done;
10399 2822a352 2019-10-15 stsp }
10400 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
10401 2822a352 2019-10-15 stsp if (base_refname == NULL) {
10402 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10403 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10404 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10405 2822a352 2019-10-15 stsp goto done;
10406 2822a352 2019-10-15 stsp }
10407 2822a352 2019-10-15 stsp
10408 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
10409 2822a352 2019-10-15 stsp if (error)
10410 2822a352 2019-10-15 stsp goto done;
10411 2822a352 2019-10-15 stsp
10412 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10413 2822a352 2019-10-15 stsp if (error)
10414 2822a352 2019-10-15 stsp goto done;
10415 2822a352 2019-10-15 stsp
10416 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10417 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
10418 2822a352 2019-10-15 stsp "specified branch has already been integrated");
10419 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10420 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10421 2822a352 2019-10-15 stsp goto done;
10422 2822a352 2019-10-15 stsp }
10423 2822a352 2019-10-15 stsp
10424 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10425 2822a352 2019-10-15 stsp if (error) {
10426 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
10427 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
10428 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10429 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10430 2822a352 2019-10-15 stsp goto done;
10431 2822a352 2019-10-15 stsp }
10432 2822a352 2019-10-15 stsp
10433 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10434 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
10435 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
10436 2822a352 2019-10-15 stsp check_cancelled, NULL);
10437 2822a352 2019-10-15 stsp if (error)
10438 2822a352 2019-10-15 stsp goto done;
10439 2822a352 2019-10-15 stsp
10440 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
10441 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10442 2822a352 2019-10-15 stsp done:
10443 1d0f4054 2021-06-17 stsp if (repo) {
10444 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10445 1d0f4054 2021-06-17 stsp if (error == NULL)
10446 1d0f4054 2021-06-17 stsp error = close_err;
10447 1d0f4054 2021-06-17 stsp }
10448 2822a352 2019-10-15 stsp if (worktree)
10449 2822a352 2019-10-15 stsp got_worktree_close(worktree);
10450 2822a352 2019-10-15 stsp free(cwd);
10451 2822a352 2019-10-15 stsp free(base_commit_id);
10452 2822a352 2019-10-15 stsp free(commit_id);
10453 2822a352 2019-10-15 stsp free(refname);
10454 2822a352 2019-10-15 stsp free(base_refname);
10455 715dc77e 2019-08-03 stsp return error;
10456 715dc77e 2019-08-03 stsp }
10457 715dc77e 2019-08-03 stsp
10458 715dc77e 2019-08-03 stsp __dead static void
10459 715dc77e 2019-08-03 stsp usage_stage(void)
10460 715dc77e 2019-08-03 stsp {
10461 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
10462 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
10463 715dc77e 2019-08-03 stsp getprogname());
10464 715dc77e 2019-08-03 stsp exit(1);
10465 715dc77e 2019-08-03 stsp }
10466 715dc77e 2019-08-03 stsp
10467 715dc77e 2019-08-03 stsp static const struct got_error *
10468 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
10469 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
10470 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
10471 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
10472 408b4ebc 2019-08-03 stsp {
10473 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
10474 408b4ebc 2019-08-03 stsp char *id_str = NULL;
10475 408b4ebc 2019-08-03 stsp
10476 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
10477 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
10478 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
10479 408b4ebc 2019-08-03 stsp return NULL;
10480 408b4ebc 2019-08-03 stsp
10481 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
10482 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
10483 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
10484 408b4ebc 2019-08-03 stsp else
10485 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
10486 408b4ebc 2019-08-03 stsp if (err)
10487 408b4ebc 2019-08-03 stsp return err;
10488 408b4ebc 2019-08-03 stsp
10489 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
10490 408b4ebc 2019-08-03 stsp free(id_str);
10491 dc424a06 2019-08-07 stsp return NULL;
10492 dc424a06 2019-08-07 stsp }
10493 2e1f37b0 2019-08-08 stsp
10494 dc424a06 2019-08-07 stsp static const struct got_error *
10495 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
10496 715dc77e 2019-08-03 stsp {
10497 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
10498 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
10499 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
10500 715dc77e 2019-08-03 stsp char *cwd = NULL;
10501 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
10502 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
10503 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10504 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
10505 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10506 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10507 715dc77e 2019-08-03 stsp
10508 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
10509 715dc77e 2019-08-03 stsp
10510 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
10511 715dc77e 2019-08-03 stsp switch (ch) {
10512 408b4ebc 2019-08-03 stsp case 'l':
10513 408b4ebc 2019-08-03 stsp list_stage = 1;
10514 408b4ebc 2019-08-03 stsp break;
10515 dc424a06 2019-08-07 stsp case 'p':
10516 dc424a06 2019-08-07 stsp pflag = 1;
10517 dc424a06 2019-08-07 stsp break;
10518 dc424a06 2019-08-07 stsp case 'F':
10519 dc424a06 2019-08-07 stsp patch_script_path = optarg;
10520 dc424a06 2019-08-07 stsp break;
10521 35213c7c 2020-07-23 stsp case 'S':
10522 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
10523 35213c7c 2020-07-23 stsp break;
10524 715dc77e 2019-08-03 stsp default:
10525 715dc77e 2019-08-03 stsp usage_stage();
10526 715dc77e 2019-08-03 stsp /* NOTREACHED */
10527 715dc77e 2019-08-03 stsp }
10528 715dc77e 2019-08-03 stsp }
10529 715dc77e 2019-08-03 stsp
10530 715dc77e 2019-08-03 stsp argc -= optind;
10531 715dc77e 2019-08-03 stsp argv += optind;
10532 715dc77e 2019-08-03 stsp
10533 715dc77e 2019-08-03 stsp #ifndef PROFILE
10534 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10535 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
10536 715dc77e 2019-08-03 stsp err(1, "pledge");
10537 715dc77e 2019-08-03 stsp #endif
10538 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
10539 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
10540 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
10541 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
10542 715dc77e 2019-08-03 stsp
10543 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
10544 715dc77e 2019-08-03 stsp if (cwd == NULL) {
10545 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
10546 715dc77e 2019-08-03 stsp goto done;
10547 715dc77e 2019-08-03 stsp }
10548 715dc77e 2019-08-03 stsp
10549 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10550 fa51e947 2020-03-27 stsp if (error) {
10551 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10552 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
10553 715dc77e 2019-08-03 stsp goto done;
10554 fa51e947 2020-03-27 stsp }
10555 715dc77e 2019-08-03 stsp
10556 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10557 c9956ddf 2019-09-08 stsp NULL);
10558 715dc77e 2019-08-03 stsp if (error != NULL)
10559 715dc77e 2019-08-03 stsp goto done;
10560 715dc77e 2019-08-03 stsp
10561 dc424a06 2019-08-07 stsp if (patch_script_path) {
10562 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
10563 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
10564 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
10565 dc424a06 2019-08-07 stsp patch_script_path);
10566 dc424a06 2019-08-07 stsp goto done;
10567 dc424a06 2019-08-07 stsp }
10568 dc424a06 2019-08-07 stsp }
10569 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10570 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
10571 715dc77e 2019-08-03 stsp if (error)
10572 715dc77e 2019-08-03 stsp goto done;
10573 715dc77e 2019-08-03 stsp
10574 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10575 6d022e97 2019-08-04 stsp if (error)
10576 6d022e97 2019-08-04 stsp goto done;
10577 715dc77e 2019-08-03 stsp
10578 6d022e97 2019-08-04 stsp if (list_stage)
10579 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
10580 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
10581 2e1f37b0 2019-08-08 stsp else {
10582 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10583 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
10584 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
10585 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
10586 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
10587 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
10588 2e1f37b0 2019-08-08 stsp }
10589 ad493afc 2019-08-03 stsp done:
10590 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10591 2e1f37b0 2019-08-08 stsp error == NULL)
10592 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10593 1d0f4054 2021-06-17 stsp if (repo) {
10594 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10595 1d0f4054 2021-06-17 stsp if (error == NULL)
10596 1d0f4054 2021-06-17 stsp error = close_err;
10597 1d0f4054 2021-06-17 stsp }
10598 ad493afc 2019-08-03 stsp if (worktree)
10599 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
10600 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10601 ad493afc 2019-08-03 stsp free((char *)pe->path);
10602 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
10603 ad493afc 2019-08-03 stsp free(cwd);
10604 ad493afc 2019-08-03 stsp return error;
10605 ad493afc 2019-08-03 stsp }
10606 ad493afc 2019-08-03 stsp
10607 ad493afc 2019-08-03 stsp __dead static void
10608 ad493afc 2019-08-03 stsp usage_unstage(void)
10609 ad493afc 2019-08-03 stsp {
10610 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
10611 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
10612 ad493afc 2019-08-03 stsp getprogname());
10613 ad493afc 2019-08-03 stsp exit(1);
10614 ad493afc 2019-08-03 stsp }
10615 ad493afc 2019-08-03 stsp
10616 ad493afc 2019-08-03 stsp
10617 ad493afc 2019-08-03 stsp static const struct got_error *
10618 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
10619 ad493afc 2019-08-03 stsp {
10620 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
10621 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
10622 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
10623 ad493afc 2019-08-03 stsp char *cwd = NULL;
10624 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
10625 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
10626 9627c110 2020-04-18 stsp int ch, pflag = 0;
10627 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10628 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
10629 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10630 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10631 ad493afc 2019-08-03 stsp
10632 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
10633 ad493afc 2019-08-03 stsp
10634 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
10635 ad493afc 2019-08-03 stsp switch (ch) {
10636 2e1f37b0 2019-08-08 stsp case 'p':
10637 2e1f37b0 2019-08-08 stsp pflag = 1;
10638 2e1f37b0 2019-08-08 stsp break;
10639 2e1f37b0 2019-08-08 stsp case 'F':
10640 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
10641 2e1f37b0 2019-08-08 stsp break;
10642 ad493afc 2019-08-03 stsp default:
10643 ad493afc 2019-08-03 stsp usage_unstage();
10644 ad493afc 2019-08-03 stsp /* NOTREACHED */
10645 ad493afc 2019-08-03 stsp }
10646 ad493afc 2019-08-03 stsp }
10647 ad493afc 2019-08-03 stsp
10648 ad493afc 2019-08-03 stsp argc -= optind;
10649 ad493afc 2019-08-03 stsp argv += optind;
10650 ad493afc 2019-08-03 stsp
10651 ad493afc 2019-08-03 stsp #ifndef PROFILE
10652 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10653 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
10654 ad493afc 2019-08-03 stsp err(1, "pledge");
10655 ad493afc 2019-08-03 stsp #endif
10656 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
10657 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
10658 2e1f37b0 2019-08-08 stsp
10659 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
10660 ad493afc 2019-08-03 stsp if (cwd == NULL) {
10661 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
10662 ad493afc 2019-08-03 stsp goto done;
10663 ad493afc 2019-08-03 stsp }
10664 ad493afc 2019-08-03 stsp
10665 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10666 fa51e947 2020-03-27 stsp if (error) {
10667 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10668 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
10669 ad493afc 2019-08-03 stsp goto done;
10670 fa51e947 2020-03-27 stsp }
10671 ad493afc 2019-08-03 stsp
10672 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10673 c9956ddf 2019-09-08 stsp NULL);
10674 ad493afc 2019-08-03 stsp if (error != NULL)
10675 ad493afc 2019-08-03 stsp goto done;
10676 ad493afc 2019-08-03 stsp
10677 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
10678 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
10679 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
10680 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
10681 2e1f37b0 2019-08-08 stsp patch_script_path);
10682 2e1f37b0 2019-08-08 stsp goto done;
10683 2e1f37b0 2019-08-08 stsp }
10684 2e1f37b0 2019-08-08 stsp }
10685 2e1f37b0 2019-08-08 stsp
10686 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10687 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
10688 ad493afc 2019-08-03 stsp if (error)
10689 ad493afc 2019-08-03 stsp goto done;
10690 ad493afc 2019-08-03 stsp
10691 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10692 ad493afc 2019-08-03 stsp if (error)
10693 ad493afc 2019-08-03 stsp goto done;
10694 ad493afc 2019-08-03 stsp
10695 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10696 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
10697 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10698 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
10699 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
10700 9627c110 2020-04-18 stsp if (!error)
10701 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10702 715dc77e 2019-08-03 stsp done:
10703 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10704 2e1f37b0 2019-08-08 stsp error == NULL)
10705 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10706 1d0f4054 2021-06-17 stsp if (repo) {
10707 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10708 1d0f4054 2021-06-17 stsp if (error == NULL)
10709 1d0f4054 2021-06-17 stsp error = close_err;
10710 1d0f4054 2021-06-17 stsp }
10711 715dc77e 2019-08-03 stsp if (worktree)
10712 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
10713 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10714 715dc77e 2019-08-03 stsp free((char *)pe->path);
10715 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
10716 715dc77e 2019-08-03 stsp free(cwd);
10717 01073a5d 2019-08-22 stsp return error;
10718 01073a5d 2019-08-22 stsp }
10719 01073a5d 2019-08-22 stsp
10720 01073a5d 2019-08-22 stsp __dead static void
10721 01073a5d 2019-08-22 stsp usage_cat(void)
10722 01073a5d 2019-08-22 stsp {
10723 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10724 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
10725 01073a5d 2019-08-22 stsp exit(1);
10726 01073a5d 2019-08-22 stsp }
10727 01073a5d 2019-08-22 stsp
10728 01073a5d 2019-08-22 stsp static const struct got_error *
10729 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10730 01073a5d 2019-08-22 stsp {
10731 01073a5d 2019-08-22 stsp const struct got_error *err;
10732 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
10733 01073a5d 2019-08-22 stsp
10734 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
10735 01073a5d 2019-08-22 stsp if (err)
10736 01073a5d 2019-08-22 stsp return err;
10737 01073a5d 2019-08-22 stsp
10738 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10739 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
10740 01073a5d 2019-08-22 stsp return err;
10741 01073a5d 2019-08-22 stsp }
10742 01073a5d 2019-08-22 stsp
10743 01073a5d 2019-08-22 stsp static const struct got_error *
10744 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10745 01073a5d 2019-08-22 stsp {
10746 01073a5d 2019-08-22 stsp const struct got_error *err;
10747 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
10748 56e0773d 2019-11-28 stsp int nentries, i;
10749 01073a5d 2019-08-22 stsp
10750 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
10751 01073a5d 2019-08-22 stsp if (err)
10752 01073a5d 2019-08-22 stsp return err;
10753 01073a5d 2019-08-22 stsp
10754 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
10755 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
10756 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
10757 01073a5d 2019-08-22 stsp char *id_str;
10758 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
10759 01073a5d 2019-08-22 stsp break;
10760 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
10761 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10762 01073a5d 2019-08-22 stsp if (err)
10763 01073a5d 2019-08-22 stsp break;
10764 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
10765 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
10766 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
10767 01073a5d 2019-08-22 stsp free(id_str);
10768 01073a5d 2019-08-22 stsp }
10769 01073a5d 2019-08-22 stsp
10770 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
10771 01073a5d 2019-08-22 stsp return err;
10772 01073a5d 2019-08-22 stsp }
10773 01073a5d 2019-08-22 stsp
10774 01073a5d 2019-08-22 stsp static const struct got_error *
10775 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10776 01073a5d 2019-08-22 stsp {
10777 01073a5d 2019-08-22 stsp const struct got_error *err;
10778 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
10779 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
10780 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
10781 01073a5d 2019-08-22 stsp char *id_str = NULL;
10782 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
10783 01073a5d 2019-08-22 stsp
10784 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
10785 01073a5d 2019-08-22 stsp if (err)
10786 01073a5d 2019-08-22 stsp return err;
10787 01073a5d 2019-08-22 stsp
10788 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10789 01073a5d 2019-08-22 stsp if (err)
10790 01073a5d 2019-08-22 stsp goto done;
10791 01073a5d 2019-08-22 stsp
10792 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10793 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10794 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
10795 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
10796 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
10797 01073a5d 2019-08-22 stsp char *pid_str;
10798 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
10799 01073a5d 2019-08-22 stsp if (err)
10800 01073a5d 2019-08-22 stsp goto done;
10801 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10802 01073a5d 2019-08-22 stsp free(pid_str);
10803 01073a5d 2019-08-22 stsp }
10804 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10805 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10806 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
10807 01073a5d 2019-08-22 stsp
10808 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10809 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10810 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
10811 01073a5d 2019-08-22 stsp
10812 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
10813 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10814 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
10815 01073a5d 2019-08-22 stsp done:
10816 01073a5d 2019-08-22 stsp free(id_str);
10817 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
10818 01073a5d 2019-08-22 stsp return err;
10819 01073a5d 2019-08-22 stsp }
10820 01073a5d 2019-08-22 stsp
10821 01073a5d 2019-08-22 stsp static const struct got_error *
10822 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10823 01073a5d 2019-08-22 stsp {
10824 01073a5d 2019-08-22 stsp const struct got_error *err;
10825 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
10826 01073a5d 2019-08-22 stsp char *id_str = NULL;
10827 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
10828 01073a5d 2019-08-22 stsp
10829 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
10830 01073a5d 2019-08-22 stsp if (err)
10831 01073a5d 2019-08-22 stsp return err;
10832 01073a5d 2019-08-22 stsp
10833 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10834 01073a5d 2019-08-22 stsp if (err)
10835 01073a5d 2019-08-22 stsp goto done;
10836 01073a5d 2019-08-22 stsp
10837 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10838 e15d5241 2019-08-22 stsp
10839 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
10840 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10841 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10842 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
10843 01073a5d 2019-08-22 stsp break;
10844 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10845 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10846 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
10847 01073a5d 2019-08-22 stsp break;
10848 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10849 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10850 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
10851 01073a5d 2019-08-22 stsp break;
10852 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10853 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10854 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
10855 01073a5d 2019-08-22 stsp break;
10856 01073a5d 2019-08-22 stsp default:
10857 01073a5d 2019-08-22 stsp break;
10858 01073a5d 2019-08-22 stsp }
10859 01073a5d 2019-08-22 stsp
10860 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10861 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
10862 e15d5241 2019-08-22 stsp
10863 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10864 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
10865 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
10866 01073a5d 2019-08-22 stsp
10867 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
10868 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10869 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
10870 01073a5d 2019-08-22 stsp done:
10871 01073a5d 2019-08-22 stsp free(id_str);
10872 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
10873 01073a5d 2019-08-22 stsp return err;
10874 01073a5d 2019-08-22 stsp }
10875 01073a5d 2019-08-22 stsp
10876 01073a5d 2019-08-22 stsp static const struct got_error *
10877 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
10878 01073a5d 2019-08-22 stsp {
10879 01073a5d 2019-08-22 stsp const struct got_error *error;
10880 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
10881 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
10882 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
10883 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
10884 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
10885 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
10886 84de9106 2020-12-26 stsp struct got_reflist_head refs;
10887 84de9106 2020-12-26 stsp
10888 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
10889 01073a5d 2019-08-22 stsp
10890 01073a5d 2019-08-22 stsp #ifndef PROFILE
10891 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10892 01073a5d 2019-08-22 stsp NULL) == -1)
10893 01073a5d 2019-08-22 stsp err(1, "pledge");
10894 01073a5d 2019-08-22 stsp #endif
10895 01073a5d 2019-08-22 stsp
10896 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10897 01073a5d 2019-08-22 stsp switch (ch) {
10898 896e9b6f 2019-08-26 stsp case 'c':
10899 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
10900 896e9b6f 2019-08-26 stsp break;
10901 01073a5d 2019-08-22 stsp case 'r':
10902 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
10903 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10904 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
10905 9ba1d308 2019-10-21 stsp optarg);
10906 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
10907 01073a5d 2019-08-22 stsp break;
10908 896e9b6f 2019-08-26 stsp case 'P':
10909 896e9b6f 2019-08-26 stsp force_path = 1;
10910 896e9b6f 2019-08-26 stsp break;
10911 01073a5d 2019-08-22 stsp default:
10912 01073a5d 2019-08-22 stsp usage_cat();
10913 01073a5d 2019-08-22 stsp /* NOTREACHED */
10914 01073a5d 2019-08-22 stsp }
10915 01073a5d 2019-08-22 stsp }
10916 01073a5d 2019-08-22 stsp
10917 01073a5d 2019-08-22 stsp argc -= optind;
10918 01073a5d 2019-08-22 stsp argv += optind;
10919 01073a5d 2019-08-22 stsp
10920 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
10921 01073a5d 2019-08-22 stsp if (cwd == NULL) {
10922 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
10923 01073a5d 2019-08-22 stsp goto done;
10924 01073a5d 2019-08-22 stsp }
10925 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
10926 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
10927 01073a5d 2019-08-22 stsp goto done;
10928 01073a5d 2019-08-22 stsp if (worktree) {
10929 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10930 01073a5d 2019-08-22 stsp repo_path = strdup(
10931 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
10932 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10933 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
10934 01073a5d 2019-08-22 stsp goto done;
10935 01073a5d 2019-08-22 stsp }
10936 01073a5d 2019-08-22 stsp }
10937 01073a5d 2019-08-22 stsp }
10938 01073a5d 2019-08-22 stsp
10939 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10940 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
10941 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10942 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
10943 01073a5d 2019-08-22 stsp }
10944 01073a5d 2019-08-22 stsp
10945 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
10946 01073a5d 2019-08-22 stsp free(repo_path);
10947 01073a5d 2019-08-22 stsp if (error != NULL)
10948 01073a5d 2019-08-22 stsp goto done;
10949 01073a5d 2019-08-22 stsp
10950 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
10951 896e9b6f 2019-08-26 stsp if (error)
10952 896e9b6f 2019-08-26 stsp goto done;
10953 896e9b6f 2019-08-26 stsp
10954 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10955 84de9106 2020-12-26 stsp if (error)
10956 84de9106 2020-12-26 stsp goto done;
10957 84de9106 2020-12-26 stsp
10958 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
10959 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
10960 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
10961 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
10962 01073a5d 2019-08-22 stsp if (error)
10963 01073a5d 2019-08-22 stsp goto done;
10964 01073a5d 2019-08-22 stsp
10965 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
10966 896e9b6f 2019-08-26 stsp if (force_path) {
10967 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
10968 896e9b6f 2019-08-26 stsp argv[i]);
10969 896e9b6f 2019-08-26 stsp if (error)
10970 896e9b6f 2019-08-26 stsp break;
10971 896e9b6f 2019-08-26 stsp } else {
10972 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
10973 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
10974 84de9106 2020-12-26 stsp repo);
10975 896e9b6f 2019-08-26 stsp if (error) {
10976 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
10977 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
10978 896e9b6f 2019-08-26 stsp break;
10979 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
10980 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
10981 896e9b6f 2019-08-26 stsp if (error)
10982 896e9b6f 2019-08-26 stsp break;
10983 896e9b6f 2019-08-26 stsp }
10984 896e9b6f 2019-08-26 stsp }
10985 01073a5d 2019-08-22 stsp
10986 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
10987 01073a5d 2019-08-22 stsp if (error)
10988 01073a5d 2019-08-22 stsp break;
10989 01073a5d 2019-08-22 stsp
10990 01073a5d 2019-08-22 stsp switch (obj_type) {
10991 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10992 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
10993 01073a5d 2019-08-22 stsp break;
10994 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10995 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
10996 01073a5d 2019-08-22 stsp break;
10997 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10998 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
10999 01073a5d 2019-08-22 stsp break;
11000 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
11001 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
11002 01073a5d 2019-08-22 stsp break;
11003 01073a5d 2019-08-22 stsp default:
11004 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
11005 01073a5d 2019-08-22 stsp break;
11006 01073a5d 2019-08-22 stsp }
11007 01073a5d 2019-08-22 stsp if (error)
11008 01073a5d 2019-08-22 stsp break;
11009 01073a5d 2019-08-22 stsp free(label);
11010 01073a5d 2019-08-22 stsp label = NULL;
11011 01073a5d 2019-08-22 stsp free(id);
11012 01073a5d 2019-08-22 stsp id = NULL;
11013 01073a5d 2019-08-22 stsp }
11014 01073a5d 2019-08-22 stsp done:
11015 01073a5d 2019-08-22 stsp free(label);
11016 01073a5d 2019-08-22 stsp free(id);
11017 896e9b6f 2019-08-26 stsp free(commit_id);
11018 01073a5d 2019-08-22 stsp if (worktree)
11019 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
11020 01073a5d 2019-08-22 stsp if (repo) {
11021 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11022 01073a5d 2019-08-22 stsp if (error == NULL)
11023 1d0f4054 2021-06-17 stsp error = close_err;
11024 b2118c49 2020-07-28 stsp }
11025 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
11026 b2118c49 2020-07-28 stsp return error;
11027 b2118c49 2020-07-28 stsp }
11028 b2118c49 2020-07-28 stsp
11029 b2118c49 2020-07-28 stsp __dead static void
11030 b2118c49 2020-07-28 stsp usage_info(void)
11031 b2118c49 2020-07-28 stsp {
11032 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
11033 b2118c49 2020-07-28 stsp getprogname());
11034 b2118c49 2020-07-28 stsp exit(1);
11035 b2118c49 2020-07-28 stsp }
11036 b2118c49 2020-07-28 stsp
11037 b2118c49 2020-07-28 stsp static const struct got_error *
11038 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11039 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11040 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
11041 b2118c49 2020-07-28 stsp {
11042 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
11043 b2118c49 2020-07-28 stsp char *id_str = NULL;
11044 b2118c49 2020-07-28 stsp char datebuf[128];
11045 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
11046 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
11047 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11048 b2118c49 2020-07-28 stsp
11049 b2118c49 2020-07-28 stsp /*
11050 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
11051 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
11052 b2118c49 2020-07-28 stsp */
11053 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
11054 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
11055 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
11056 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
11057 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
11058 b2118c49 2020-07-28 stsp }
11059 b2118c49 2020-07-28 stsp
11060 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
11061 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
11062 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
11063 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
11064 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
11065 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11066 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
11067 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
11068 b2118c49 2020-07-28 stsp else
11069 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
11070 b2118c49 2020-07-28 stsp
11071 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
11072 b2118c49 2020-07-28 stsp if (tm == NULL)
11073 b2118c49 2020-07-28 stsp return NULL;
11074 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11075 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
11076 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
11077 b2118c49 2020-07-28 stsp
11078 b2118c49 2020-07-28 stsp if (blob_id) {
11079 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
11080 b2118c49 2020-07-28 stsp if (err)
11081 b2118c49 2020-07-28 stsp return err;
11082 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
11083 b2118c49 2020-07-28 stsp free(id_str);
11084 b2118c49 2020-07-28 stsp }
11085 b2118c49 2020-07-28 stsp
11086 b2118c49 2020-07-28 stsp if (staged_blob_id) {
11087 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
11088 b2118c49 2020-07-28 stsp if (err)
11089 b2118c49 2020-07-28 stsp return err;
11090 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
11091 b2118c49 2020-07-28 stsp free(id_str);
11092 b2118c49 2020-07-28 stsp }
11093 b2118c49 2020-07-28 stsp
11094 b2118c49 2020-07-28 stsp if (commit_id) {
11095 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
11096 b2118c49 2020-07-28 stsp if (err)
11097 b2118c49 2020-07-28 stsp return err;
11098 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
11099 b2118c49 2020-07-28 stsp free(id_str);
11100 b2118c49 2020-07-28 stsp }
11101 b2118c49 2020-07-28 stsp
11102 b2118c49 2020-07-28 stsp return NULL;
11103 b2118c49 2020-07-28 stsp }
11104 b2118c49 2020-07-28 stsp
11105 b2118c49 2020-07-28 stsp static const struct got_error *
11106 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
11107 b2118c49 2020-07-28 stsp {
11108 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
11109 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
11110 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
11111 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
11112 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11113 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
11114 b2118c49 2020-07-28 stsp int ch, show_files = 0;
11115 b2118c49 2020-07-28 stsp
11116 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
11117 b2118c49 2020-07-28 stsp
11118 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
11119 b2118c49 2020-07-28 stsp switch (ch) {
11120 b2118c49 2020-07-28 stsp default:
11121 b2118c49 2020-07-28 stsp usage_info();
11122 b2118c49 2020-07-28 stsp /* NOTREACHED */
11123 b2118c49 2020-07-28 stsp }
11124 01073a5d 2019-08-22 stsp }
11125 b2118c49 2020-07-28 stsp
11126 b2118c49 2020-07-28 stsp argc -= optind;
11127 b2118c49 2020-07-28 stsp argv += optind;
11128 b2118c49 2020-07-28 stsp
11129 b2118c49 2020-07-28 stsp #ifndef PROFILE
11130 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11131 b2118c49 2020-07-28 stsp NULL) == -1)
11132 b2118c49 2020-07-28 stsp err(1, "pledge");
11133 b2118c49 2020-07-28 stsp #endif
11134 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
11135 b2118c49 2020-07-28 stsp if (cwd == NULL) {
11136 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
11137 b2118c49 2020-07-28 stsp goto done;
11138 b2118c49 2020-07-28 stsp }
11139 b2118c49 2020-07-28 stsp
11140 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
11141 b2118c49 2020-07-28 stsp if (error) {
11142 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11143 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
11144 b2118c49 2020-07-28 stsp goto done;
11145 b2118c49 2020-07-28 stsp }
11146 b2118c49 2020-07-28 stsp
11147 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11148 b2118c49 2020-07-28 stsp if (error)
11149 b2118c49 2020-07-28 stsp goto done;
11150 b2118c49 2020-07-28 stsp
11151 b2118c49 2020-07-28 stsp if (argc >= 1) {
11152 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
11153 b2118c49 2020-07-28 stsp worktree);
11154 b2118c49 2020-07-28 stsp if (error)
11155 b2118c49 2020-07-28 stsp goto done;
11156 b2118c49 2020-07-28 stsp show_files = 1;
11157 b2118c49 2020-07-28 stsp }
11158 b2118c49 2020-07-28 stsp
11159 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
11160 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
11161 b2118c49 2020-07-28 stsp if (error)
11162 b2118c49 2020-07-28 stsp goto done;
11163 b2118c49 2020-07-28 stsp
11164 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
11165 b2118c49 2020-07-28 stsp if (error)
11166 b2118c49 2020-07-28 stsp goto done;
11167 b2118c49 2020-07-28 stsp
11168 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11169 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
11170 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
11171 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
11172 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
11173 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
11174 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
11175 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11176 b2118c49 2020-07-28 stsp
11177 b2118c49 2020-07-28 stsp if (show_files) {
11178 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11179 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11180 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
11181 b2118c49 2020-07-28 stsp continue;
11182 b2118c49 2020-07-28 stsp /*
11183 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
11184 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
11185 b2118c49 2020-07-28 stsp */
11186 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
11187 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
11188 b2118c49 2020-07-28 stsp }
11189 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
11190 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
11191 b2118c49 2020-07-28 stsp if (error)
11192 b2118c49 2020-07-28 stsp goto done;
11193 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11194 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
11195 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
11196 b2118c49 2020-07-28 stsp break;
11197 b2118c49 2020-07-28 stsp }
11198 b2118c49 2020-07-28 stsp }
11199 b2118c49 2020-07-28 stsp }
11200 b2118c49 2020-07-28 stsp done:
11201 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
11202 b2118c49 2020-07-28 stsp free((char *)pe->path);
11203 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
11204 b2118c49 2020-07-28 stsp free(cwd);
11205 b2118c49 2020-07-28 stsp free(id_str);
11206 b2118c49 2020-07-28 stsp free(uuidstr);
11207 0ebf8283 2019-07-24 stsp return error;
11208 0ebf8283 2019-07-24 stsp }