Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 5c860e29 2018-03-12 stsp *
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp *
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5c860e29 2018-03-12 stsp */
18 5c860e29 2018-03-12 stsp
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
23 f42b1b34 2018-03-12 stsp
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12463d8b 2019-12-13 stsp #include <fcntl.h>
27 12ce7a6c 2019-08-12 stsp #include <limits.h>
28 5c860e29 2018-03-12 stsp #include <locale.h>
29 818c7501 2019-07-11 stsp #include <ctype.h>
30 99437157 2018-11-11 stsp #include <signal.h>
31 5c860e29 2018-03-12 stsp #include <stdio.h>
32 5c860e29 2018-03-12 stsp #include <stdlib.h>
33 5c860e29 2018-03-12 stsp #include <string.h>
34 5c860e29 2018-03-12 stsp #include <unistd.h>
35 c09a553d 2018-03-12 stsp #include <libgen.h>
36 c0768b0f 2018-06-10 stsp #include <time.h>
37 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 6841bf13 2019-11-29 kn #include <regex.h>
39 83cd27f8 2020-01-13 stsp #include <getopt.h>
40 d2cdc636 2020-03-18 stsp #include <util.h>
41 5c860e29 2018-03-12 stsp
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 f8a36e22 2021-08-26 stsp #include "got_send.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
58 d65a88a2 2021-09-05 stsp #include "got_dial.h"
59 5c860e29 2018-03-12 stsp
60 5c860e29 2018-03-12 stsp #ifndef nitems
61 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 5c860e29 2018-03-12 stsp #endif
63 99437157 2018-11-11 stsp
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
65 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
66 99437157 2018-11-11 stsp
67 99437157 2018-11-11 stsp static void
68 99437157 2018-11-11 stsp catch_sigint(int signo)
69 99437157 2018-11-11 stsp {
70 99437157 2018-11-11 stsp sigint_received = 1;
71 99437157 2018-11-11 stsp }
72 99437157 2018-11-11 stsp
73 99437157 2018-11-11 stsp static void
74 99437157 2018-11-11 stsp catch_sigpipe(int signo)
75 99437157 2018-11-11 stsp {
76 99437157 2018-11-11 stsp sigpipe_received = 1;
77 99437157 2018-11-11 stsp }
78 5c860e29 2018-03-12 stsp
79 99437157 2018-11-11 stsp
80 8cfb4057 2019-07-09 stsp struct got_cmd {
81 820059fa 2020-09-25 stsp const char *cmd_name;
82 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
83 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
84 97b3a7be 2019-07-09 stsp const char *cmd_alias;
85 5c860e29 2018-03-12 stsp };
86 5c860e29 2018-03-12 stsp
87 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
88 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
89 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
90 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
91 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
92 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
93 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
95 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
96 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
97 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
98 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
99 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
100 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
101 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
102 d00136be 2019-03-26 stsp __dead static void usage_add(void);
103 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
104 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
105 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
106 f8a36e22 2021-08-26 stsp __dead static void usage_send(void);
107 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
108 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
109 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
110 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
111 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
112 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
113 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
114 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
115 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
116 5c860e29 2018-03-12 stsp
117 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
118 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
119 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
120 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
122 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
124 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
125 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
126 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
127 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
128 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
129 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
130 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
131 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
132 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
133 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
134 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
135 f8a36e22 2021-08-26 stsp static const struct got_error* cmd_send(int, char *[]);
136 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
137 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
138 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
139 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
140 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
141 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
142 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
143 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
144 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
145 5c860e29 2018-03-12 stsp
146 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
147 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
148 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
149 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
150 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
151 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
152 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
153 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
154 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
155 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
156 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
157 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
158 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
159 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
160 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
161 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
162 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
163 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
164 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
165 f8a36e22 2021-08-26 stsp { "send", cmd_send, usage_send, "se" },
166 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
167 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
168 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
169 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
170 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
171 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
172 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
173 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
174 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
175 5c860e29 2018-03-12 stsp };
176 ce5b7c56 2019-07-09 stsp
177 ce5b7c56 2019-07-09 stsp static void
178 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
179 ce5b7c56 2019-07-09 stsp {
180 6059809a 2020-12-17 stsp size_t i;
181 ce5b7c56 2019-07-09 stsp
182 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
183 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
184 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
185 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
186 ce5b7c56 2019-07-09 stsp }
187 6879ba42 2020-10-01 naddy fputc('\n', fp);
188 ce5b7c56 2019-07-09 stsp }
189 5c860e29 2018-03-12 stsp
190 ff69268e 2020-12-13 stsp __dead static void
191 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
192 ff69268e 2020-12-13 stsp {
193 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
194 ff69268e 2020-12-13 stsp }
195 ff69268e 2020-12-13 stsp
196 5c860e29 2018-03-12 stsp int
197 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
198 5c860e29 2018-03-12 stsp {
199 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
200 6059809a 2020-12-17 stsp size_t i;
201 5c860e29 2018-03-12 stsp int ch;
202 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
203 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
204 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
205 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
206 83cd27f8 2020-01-13 stsp };
207 5c860e29 2018-03-12 stsp
208 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
209 5c860e29 2018-03-12 stsp
210 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
211 5c860e29 2018-03-12 stsp switch (ch) {
212 1b6b95a8 2018-03-12 stsp case 'h':
213 1b6b95a8 2018-03-12 stsp hflag = 1;
214 53ccebc2 2019-07-30 stsp break;
215 53ccebc2 2019-07-30 stsp case 'V':
216 53ccebc2 2019-07-30 stsp Vflag = 1;
217 1b6b95a8 2018-03-12 stsp break;
218 5c860e29 2018-03-12 stsp default:
219 6879ba42 2020-10-01 naddy usage(hflag, 1);
220 5c860e29 2018-03-12 stsp /* NOTREACHED */
221 5c860e29 2018-03-12 stsp }
222 5c860e29 2018-03-12 stsp }
223 5c860e29 2018-03-12 stsp
224 5c860e29 2018-03-12 stsp argc -= optind;
225 5c860e29 2018-03-12 stsp argv += optind;
226 9814e6a3 2020-09-27 naddy optind = 1;
227 9814e6a3 2020-09-27 naddy optreset = 1;
228 53ccebc2 2019-07-30 stsp
229 53ccebc2 2019-07-30 stsp if (Vflag) {
230 53ccebc2 2019-07-30 stsp got_version_print_str();
231 6879ba42 2020-10-01 naddy return 0;
232 53ccebc2 2019-07-30 stsp }
233 5c860e29 2018-03-12 stsp
234 5c860e29 2018-03-12 stsp if (argc <= 0)
235 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
236 5c860e29 2018-03-12 stsp
237 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
238 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
239 99437157 2018-11-11 stsp
240 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
241 d7d4f210 2018-03-12 stsp const struct got_error *error;
242 d7d4f210 2018-03-12 stsp
243 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
244 5c860e29 2018-03-12 stsp
245 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
246 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
247 5c860e29 2018-03-12 stsp continue;
248 5c860e29 2018-03-12 stsp
249 1b6b95a8 2018-03-12 stsp if (hflag)
250 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
251 1b6b95a8 2018-03-12 stsp
252 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
253 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
254 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
255 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
256 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
257 70015d7a 2019-11-08 stsp !(sigint_received &&
258 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
259 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
260 d7d4f210 2018-03-12 stsp return 1;
261 d7d4f210 2018-03-12 stsp }
262 d7d4f210 2018-03-12 stsp
263 d7d4f210 2018-03-12 stsp return 0;
264 5c860e29 2018-03-12 stsp }
265 5c860e29 2018-03-12 stsp
266 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
267 6879ba42 2020-10-01 naddy list_commands(stderr);
268 5c860e29 2018-03-12 stsp return 1;
269 5c860e29 2018-03-12 stsp }
270 5c860e29 2018-03-12 stsp
271 4ed7e80c 2018-05-20 stsp __dead static void
272 6879ba42 2020-10-01 naddy usage(int hflag, int status)
273 5c860e29 2018-03-12 stsp {
274 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
275 6879ba42 2020-10-01 naddy
276 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
277 53ccebc2 2019-07-30 stsp getprogname());
278 ce5b7c56 2019-07-09 stsp if (hflag)
279 6879ba42 2020-10-01 naddy list_commands(fp);
280 6879ba42 2020-10-01 naddy exit(status);
281 5c860e29 2018-03-12 stsp }
282 5c860e29 2018-03-12 stsp
283 0266afb7 2019-01-04 stsp static const struct got_error *
284 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
285 e2ba3d07 2019-05-13 stsp {
286 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
287 e2ba3d07 2019-05-13 stsp const char *editor;
288 8920fa04 2019-08-18 stsp
289 8920fa04 2019-08-18 stsp *abspath = NULL;
290 e2ba3d07 2019-05-13 stsp
291 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
292 e2ba3d07 2019-05-13 stsp if (editor == NULL)
293 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
294 e2ba3d07 2019-05-13 stsp
295 0ee7065d 2019-05-13 stsp if (editor) {
296 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
297 0ee7065d 2019-05-13 stsp if (err)
298 0ee7065d 2019-05-13 stsp return err;
299 0ee7065d 2019-05-13 stsp }
300 e2ba3d07 2019-05-13 stsp
301 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
302 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
303 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
304 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
305 0ee7065d 2019-05-13 stsp }
306 0ee7065d 2019-05-13 stsp
307 e2ba3d07 2019-05-13 stsp return NULL;
308 e2ba3d07 2019-05-13 stsp }
309 e2ba3d07 2019-05-13 stsp
310 e2ba3d07 2019-05-13 stsp static const struct got_error *
311 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
312 c530dc23 2019-07-23 stsp const char *worktree_path)
313 0266afb7 2019-01-04 stsp {
314 163ce85a 2019-05-13 stsp const struct got_error *err;
315 0266afb7 2019-01-04 stsp
316 37c06ea4 2019-07-15 stsp #ifdef PROFILE
317 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
318 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
319 37c06ea4 2019-07-15 stsp #endif
320 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
321 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
322 0266afb7 2019-01-04 stsp
323 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
324 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
325 0266afb7 2019-01-04 stsp
326 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
327 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
328 0266afb7 2019-01-04 stsp
329 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
330 163ce85a 2019-05-13 stsp if (err != NULL)
331 163ce85a 2019-05-13 stsp return err;
332 0266afb7 2019-01-04 stsp
333 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
334 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
335 0266afb7 2019-01-04 stsp
336 0266afb7 2019-01-04 stsp return NULL;
337 2c7829a4 2019-06-17 stsp }
338 2c7829a4 2019-06-17 stsp
339 2c7829a4 2019-06-17 stsp __dead static void
340 2c7829a4 2019-06-17 stsp usage_init(void)
341 2c7829a4 2019-06-17 stsp {
342 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
343 2c7829a4 2019-06-17 stsp exit(1);
344 2c7829a4 2019-06-17 stsp }
345 2c7829a4 2019-06-17 stsp
346 2c7829a4 2019-06-17 stsp static const struct got_error *
347 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
348 2c7829a4 2019-06-17 stsp {
349 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
350 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
351 2c7829a4 2019-06-17 stsp int ch;
352 2c7829a4 2019-06-17 stsp
353 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
354 2c7829a4 2019-06-17 stsp switch (ch) {
355 2c7829a4 2019-06-17 stsp default:
356 2c7829a4 2019-06-17 stsp usage_init();
357 2c7829a4 2019-06-17 stsp /* NOTREACHED */
358 2c7829a4 2019-06-17 stsp }
359 2c7829a4 2019-06-17 stsp }
360 2c7829a4 2019-06-17 stsp
361 2c7829a4 2019-06-17 stsp argc -= optind;
362 2c7829a4 2019-06-17 stsp argv += optind;
363 2c7829a4 2019-06-17 stsp
364 2c7829a4 2019-06-17 stsp #ifndef PROFILE
365 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
366 2c7829a4 2019-06-17 stsp err(1, "pledge");
367 2c7829a4 2019-06-17 stsp #endif
368 2c7829a4 2019-06-17 stsp if (argc != 1)
369 bc20e173 2019-06-17 stsp usage_init();
370 2c7829a4 2019-06-17 stsp
371 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
372 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
373 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
374 2c7829a4 2019-06-17 stsp
375 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
376 2c7829a4 2019-06-17 stsp
377 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
378 2c7829a4 2019-06-17 stsp if (error &&
379 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
380 2c7829a4 2019-06-17 stsp goto done;
381 2c7829a4 2019-06-17 stsp
382 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
383 2c7829a4 2019-06-17 stsp if (error)
384 2c7829a4 2019-06-17 stsp goto done;
385 2c7829a4 2019-06-17 stsp
386 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
387 3ce1b845 2019-07-15 stsp done:
388 3ce1b845 2019-07-15 stsp free(repo_path);
389 3ce1b845 2019-07-15 stsp return error;
390 3ce1b845 2019-07-15 stsp }
391 3ce1b845 2019-07-15 stsp
392 3ce1b845 2019-07-15 stsp __dead static void
393 3ce1b845 2019-07-15 stsp usage_import(void)
394 3ce1b845 2019-07-15 stsp {
395 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
396 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
397 3ce1b845 2019-07-15 stsp exit(1);
398 3ce1b845 2019-07-15 stsp }
399 3ce1b845 2019-07-15 stsp
400 3ce1b845 2019-07-15 stsp int
401 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
402 3ce1b845 2019-07-15 stsp {
403 3ce1b845 2019-07-15 stsp pid_t pid;
404 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
405 3ce1b845 2019-07-15 stsp int st = -1;
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
408 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
409 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
410 3ce1b845 2019-07-15 stsp
411 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
412 3ce1b845 2019-07-15 stsp case -1:
413 3ce1b845 2019-07-15 stsp goto doneediting;
414 3ce1b845 2019-07-15 stsp case 0:
415 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
416 3ce1b845 2019-07-15 stsp _exit(127);
417 3ce1b845 2019-07-15 stsp }
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
420 3ce1b845 2019-07-15 stsp if (errno != EINTR)
421 3ce1b845 2019-07-15 stsp break;
422 3ce1b845 2019-07-15 stsp
423 3ce1b845 2019-07-15 stsp doneediting:
424 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
425 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
426 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
427 3ce1b845 2019-07-15 stsp
428 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
429 3ce1b845 2019-07-15 stsp errno = EINTR;
430 3ce1b845 2019-07-15 stsp return -1;
431 3ce1b845 2019-07-15 stsp }
432 3ce1b845 2019-07-15 stsp
433 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
434 3ce1b845 2019-07-15 stsp }
435 3ce1b845 2019-07-15 stsp
436 3ce1b845 2019-07-15 stsp static const struct got_error *
437 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
438 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
439 28cf319f 2021-01-28 stsp int require_modification)
440 3ce1b845 2019-07-15 stsp {
441 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
442 bfa12d5e 2020-09-26 stsp char *line = NULL;
443 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
444 bfa12d5e 2020-09-26 stsp ssize_t linelen;
445 3ce1b845 2019-07-15 stsp struct stat st, st2;
446 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
447 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
448 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
449 3ce1b845 2019-07-15 stsp
450 3ce1b845 2019-07-15 stsp *logmsg = NULL;
451 3ce1b845 2019-07-15 stsp
452 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
453 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
454 3ce1b845 2019-07-15 stsp
455 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
456 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
457 3ce1b845 2019-07-15 stsp
458 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
459 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
460 3ce1b845 2019-07-15 stsp
461 28cf319f 2021-01-28 stsp if (require_modification &&
462 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
463 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
464 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
465 3ce1b845 2019-07-15 stsp
466 bfa12d5e 2020-09-26 stsp /*
467 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
468 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
469 bfa12d5e 2020-09-26 stsp * has in fact been edited.
470 bfa12d5e 2020-09-26 stsp */
471 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
472 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
473 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
474 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
475 bfa12d5e 2020-09-26 stsp
476 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
477 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
478 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
479 bfa12d5e 2020-09-26 stsp goto done;
480 bfa12d5e 2020-09-26 stsp }
481 bfa12d5e 2020-09-26 stsp s = buf;
482 bfa12d5e 2020-09-26 stsp len = 0;
483 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
484 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
485 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
486 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
487 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
488 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
489 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
490 bfa12d5e 2020-09-26 stsp goto done;
491 bfa12d5e 2020-09-26 stsp }
492 bfa12d5e 2020-09-26 stsp }
493 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
494 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
495 bfa12d5e 2020-09-26 stsp len--;
496 bfa12d5e 2020-09-26 stsp }
497 bfa12d5e 2020-09-26 stsp
498 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
499 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
500 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
501 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
502 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
503 3ce1b845 2019-07-15 stsp
504 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
505 3ce1b845 2019-07-15 stsp if (fp == NULL) {
506 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
507 3ce1b845 2019-07-15 stsp goto done;
508 3ce1b845 2019-07-15 stsp }
509 bfa12d5e 2020-09-26 stsp
510 bfa12d5e 2020-09-26 stsp len = 0;
511 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
512 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
513 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
514 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
515 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
516 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
517 bfa12d5e 2020-09-26 stsp goto done;
518 bfa12d5e 2020-09-26 stsp }
519 3ce1b845 2019-07-15 stsp }
520 bfa12d5e 2020-09-26 stsp free(line);
521 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
522 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
523 bfa12d5e 2020-09-26 stsp goto done;
524 bfa12d5e 2020-09-26 stsp }
525 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
526 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
527 3ce1b845 2019-07-15 stsp len--;
528 3ce1b845 2019-07-15 stsp }
529 3ce1b845 2019-07-15 stsp
530 bfa12d5e 2020-09-26 stsp if (len == 0) {
531 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
532 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
533 bfa12d5e 2020-09-26 stsp goto done;
534 bfa12d5e 2020-09-26 stsp }
535 28cf319f 2021-01-28 stsp if (require_modification &&
536 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
537 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
538 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
539 3ce1b845 2019-07-15 stsp done:
540 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
541 bfa12d5e 2020-09-26 stsp free(buf);
542 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
543 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
544 3ce1b845 2019-07-15 stsp if (err) {
545 3ce1b845 2019-07-15 stsp free(*logmsg);
546 3ce1b845 2019-07-15 stsp *logmsg = NULL;
547 3ce1b845 2019-07-15 stsp }
548 3ce1b845 2019-07-15 stsp return err;
549 3ce1b845 2019-07-15 stsp }
550 3ce1b845 2019-07-15 stsp
551 3ce1b845 2019-07-15 stsp static const struct got_error *
552 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
553 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
554 3ce1b845 2019-07-15 stsp {
555 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
556 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
557 1601cb9f 2020-09-11 naddy int initial_content_len;
558 97972933 2020-09-11 stsp int fd = -1;
559 3ce1b845 2019-07-15 stsp
560 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
561 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
562 1601cb9f 2020-09-11 naddy branch_name);
563 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
564 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
565 3ce1b845 2019-07-15 stsp
566 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
567 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
568 3ce1b845 2019-07-15 stsp if (err)
569 3ce1b845 2019-07-15 stsp goto done;
570 3ce1b845 2019-07-15 stsp
571 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
572 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
573 97972933 2020-09-11 stsp goto done;
574 97972933 2020-09-11 stsp }
575 3ce1b845 2019-07-15 stsp
576 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
577 0d5bb276 2020-12-15 stsp initial_content_len, 1);
578 3ce1b845 2019-07-15 stsp done:
579 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
580 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
581 3ce1b845 2019-07-15 stsp free(initial_content);
582 59f86c76 2020-09-11 stsp if (err) {
583 59f86c76 2020-09-11 stsp free(*logmsg_path);
584 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
585 59f86c76 2020-09-11 stsp }
586 3ce1b845 2019-07-15 stsp return err;
587 3ce1b845 2019-07-15 stsp }
588 3ce1b845 2019-07-15 stsp
589 3ce1b845 2019-07-15 stsp static const struct got_error *
590 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
591 3ce1b845 2019-07-15 stsp {
592 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
593 3ce1b845 2019-07-15 stsp return NULL;
594 3ce1b845 2019-07-15 stsp }
595 3ce1b845 2019-07-15 stsp
596 3ce1b845 2019-07-15 stsp static const struct got_error *
597 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
598 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
599 84792843 2019-08-09 stsp {
600 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
601 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
602 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
603 84792843 2019-08-09 stsp
604 84792843 2019-08-09 stsp *author = NULL;
605 aba9c984 2019-09-08 stsp
606 50b0790e 2020-09-11 stsp if (worktree)
607 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
608 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
609 50b0790e 2020-09-11 stsp
610 50b0790e 2020-09-11 stsp /*
611 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
612 50b0790e 2020-09-11 stsp * significant to least significant:
613 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
614 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
615 50b0790e 2020-09-11 stsp * 3) repository's git config file
616 50b0790e 2020-09-11 stsp * 4) environment variables
617 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
618 50b0790e 2020-09-11 stsp */
619 50b0790e 2020-09-11 stsp
620 50b0790e 2020-09-11 stsp if (worktree_conf)
621 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
622 50b0790e 2020-09-11 stsp if (got_author == NULL)
623 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
624 84792843 2019-08-09 stsp if (got_author == NULL) {
625 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
626 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
627 c9956ddf 2019-09-08 stsp if (name && email) {
628 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
629 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
630 c9956ddf 2019-09-08 stsp return NULL;
631 c9956ddf 2019-09-08 stsp }
632 257add31 2020-09-09 stsp
633 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
634 257add31 2020-09-09 stsp if (got_author == NULL) {
635 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
636 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
637 257add31 2020-09-09 stsp repo);
638 257add31 2020-09-09 stsp if (name && email) {
639 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
640 257add31 2020-09-09 stsp == -1)
641 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
642 257add31 2020-09-09 stsp return NULL;
643 257add31 2020-09-09 stsp }
644 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
645 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
646 257add31 2020-09-09 stsp }
647 84792843 2019-08-09 stsp }
648 84792843 2019-08-09 stsp
649 aba9c984 2019-09-08 stsp *author = strdup(got_author);
650 aba9c984 2019-09-08 stsp if (*author == NULL)
651 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
652 84792843 2019-08-09 stsp
653 84792843 2019-08-09 stsp /*
654 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
655 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
656 84792843 2019-08-09 stsp */
657 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
658 84792843 2019-08-09 stsp got_author++;
659 aba9c984 2019-09-08 stsp if (*got_author != '<') {
660 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
661 aba9c984 2019-09-08 stsp goto done;
662 aba9c984 2019-09-08 stsp }
663 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
664 84792843 2019-08-09 stsp got_author++;
665 aba9c984 2019-09-08 stsp if (*got_author != '@') {
666 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
667 aba9c984 2019-09-08 stsp goto done;
668 aba9c984 2019-09-08 stsp }
669 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
670 84792843 2019-08-09 stsp got_author++;
671 84792843 2019-08-09 stsp if (*got_author != '>')
672 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
673 aba9c984 2019-09-08 stsp done:
674 aba9c984 2019-09-08 stsp if (err) {
675 aba9c984 2019-09-08 stsp free(*author);
676 aba9c984 2019-09-08 stsp *author = NULL;
677 aba9c984 2019-09-08 stsp }
678 aba9c984 2019-09-08 stsp return err;
679 c9956ddf 2019-09-08 stsp }
680 c9956ddf 2019-09-08 stsp
681 c9956ddf 2019-09-08 stsp static const struct got_error *
682 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
683 c9956ddf 2019-09-08 stsp {
684 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
685 c9956ddf 2019-09-08 stsp
686 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
687 c9956ddf 2019-09-08 stsp if (homedir) {
688 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
689 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
690 c9956ddf 2019-09-08 stsp
691 c9956ddf 2019-09-08 stsp }
692 c9956ddf 2019-09-08 stsp return NULL;
693 84792843 2019-08-09 stsp }
694 84792843 2019-08-09 stsp
695 84792843 2019-08-09 stsp static const struct got_error *
696 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
697 3ce1b845 2019-07-15 stsp {
698 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
699 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
700 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
701 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
702 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
703 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
704 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
705 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
706 3ce1b845 2019-07-15 stsp int ch;
707 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
708 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
709 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
710 3ce1b845 2019-07-15 stsp
711 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
712 3ce1b845 2019-07-15 stsp
713 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
714 3ce1b845 2019-07-15 stsp switch (ch) {
715 3ce1b845 2019-07-15 stsp case 'b':
716 3ce1b845 2019-07-15 stsp branch_name = optarg;
717 3ce1b845 2019-07-15 stsp break;
718 3ce1b845 2019-07-15 stsp case 'm':
719 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
720 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
721 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
722 3ce1b845 2019-07-15 stsp goto done;
723 3ce1b845 2019-07-15 stsp }
724 3ce1b845 2019-07-15 stsp break;
725 3ce1b845 2019-07-15 stsp case 'r':
726 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
727 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
728 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
729 9ba1d308 2019-10-21 stsp optarg);
730 3ce1b845 2019-07-15 stsp goto done;
731 3ce1b845 2019-07-15 stsp }
732 3ce1b845 2019-07-15 stsp break;
733 3ce1b845 2019-07-15 stsp case 'I':
734 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
735 3ce1b845 2019-07-15 stsp break;
736 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
737 3ce1b845 2019-07-15 stsp NULL);
738 3ce1b845 2019-07-15 stsp if (error)
739 3ce1b845 2019-07-15 stsp goto done;
740 3ce1b845 2019-07-15 stsp break;
741 3ce1b845 2019-07-15 stsp default:
742 b2b65d18 2019-08-22 stsp usage_import();
743 3ce1b845 2019-07-15 stsp /* NOTREACHED */
744 3ce1b845 2019-07-15 stsp }
745 3ce1b845 2019-07-15 stsp }
746 3ce1b845 2019-07-15 stsp
747 3ce1b845 2019-07-15 stsp argc -= optind;
748 3ce1b845 2019-07-15 stsp argv += optind;
749 3ce1b845 2019-07-15 stsp
750 3ce1b845 2019-07-15 stsp #ifndef PROFILE
751 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
752 aba9c984 2019-09-08 stsp "unveil",
753 3ce1b845 2019-07-15 stsp NULL) == -1)
754 3ce1b845 2019-07-15 stsp err(1, "pledge");
755 3ce1b845 2019-07-15 stsp #endif
756 3ce1b845 2019-07-15 stsp if (argc != 1)
757 3ce1b845 2019-07-15 stsp usage_import();
758 2c7829a4 2019-06-17 stsp
759 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
760 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
761 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
762 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
763 3ce1b845 2019-07-15 stsp }
764 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
765 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
766 c9956ddf 2019-09-08 stsp if (error)
767 c9956ddf 2019-09-08 stsp goto done;
768 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
769 3ce1b845 2019-07-15 stsp if (error)
770 3ce1b845 2019-07-15 stsp goto done;
771 aba9c984 2019-09-08 stsp
772 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
773 aba9c984 2019-09-08 stsp if (error)
774 aba9c984 2019-09-08 stsp return error;
775 e560b7e0 2019-11-28 stsp
776 e560b7e0 2019-11-28 stsp /*
777 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
778 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
779 e560b7e0 2019-11-28 stsp * an unintended typo.
780 e560b7e0 2019-11-28 stsp */
781 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
782 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
783 3ce1b845 2019-07-15 stsp
784 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
785 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
786 3ce1b845 2019-07-15 stsp goto done;
787 3ce1b845 2019-07-15 stsp }
788 3ce1b845 2019-07-15 stsp
789 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
790 3ce1b845 2019-07-15 stsp if (error) {
791 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
792 3ce1b845 2019-07-15 stsp goto done;
793 3ce1b845 2019-07-15 stsp } else {
794 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
795 3ce1b845 2019-07-15 stsp "import target branch already exists");
796 3ce1b845 2019-07-15 stsp goto done;
797 3ce1b845 2019-07-15 stsp }
798 3ce1b845 2019-07-15 stsp
799 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
800 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
801 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
802 3ce1b845 2019-07-15 stsp goto done;
803 3ce1b845 2019-07-15 stsp }
804 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
805 3ce1b845 2019-07-15 stsp
806 3ce1b845 2019-07-15 stsp /*
807 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
808 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
809 3ce1b845 2019-07-15 stsp */
810 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
811 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
812 3ce1b845 2019-07-15 stsp if (error)
813 3ce1b845 2019-07-15 stsp goto done;
814 8e158b01 2019-09-22 stsp free(logmsg);
815 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
816 ef293bdd 2019-10-21 stsp path_dir, refname);
817 ef293bdd 2019-10-21 stsp if (error) {
818 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
819 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
820 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
821 3ce1b845 2019-07-15 stsp goto done;
822 ef293bdd 2019-10-21 stsp }
823 3ce1b845 2019-07-15 stsp }
824 3ce1b845 2019-07-15 stsp
825 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
826 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
827 ef293bdd 2019-10-21 stsp if (logmsg_path)
828 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
829 3ce1b845 2019-07-15 stsp goto done;
830 ef293bdd 2019-10-21 stsp }
831 3ce1b845 2019-07-15 stsp
832 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
833 ef293bdd 2019-10-21 stsp if (error) {
834 ef293bdd 2019-10-21 stsp if (logmsg_path)
835 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
836 ef293bdd 2019-10-21 stsp goto done;
837 ef293bdd 2019-10-21 stsp }
838 ef293bdd 2019-10-21 stsp
839 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
840 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
841 ef293bdd 2019-10-21 stsp if (error) {
842 ef293bdd 2019-10-21 stsp if (logmsg_path)
843 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
844 3ce1b845 2019-07-15 stsp goto done;
845 ef293bdd 2019-10-21 stsp }
846 3ce1b845 2019-07-15 stsp
847 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
848 ef293bdd 2019-10-21 stsp if (error) {
849 ef293bdd 2019-10-21 stsp if (logmsg_path)
850 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
851 3ce1b845 2019-07-15 stsp goto done;
852 ef293bdd 2019-10-21 stsp }
853 3ce1b845 2019-07-15 stsp
854 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
855 ef293bdd 2019-10-21 stsp if (error) {
856 ef293bdd 2019-10-21 stsp if (logmsg_path)
857 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
858 3ce1b845 2019-07-15 stsp goto done;
859 ef293bdd 2019-10-21 stsp }
860 3ce1b845 2019-07-15 stsp
861 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
862 ef293bdd 2019-10-21 stsp if (error) {
863 ef293bdd 2019-10-21 stsp if (logmsg_path)
864 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
865 3ce1b845 2019-07-15 stsp goto done;
866 ef293bdd 2019-10-21 stsp }
867 3ce1b845 2019-07-15 stsp
868 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
869 3ce1b845 2019-07-15 stsp if (error) {
870 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
871 ef293bdd 2019-10-21 stsp if (logmsg_path)
872 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
873 3ce1b845 2019-07-15 stsp goto done;
874 ef293bdd 2019-10-21 stsp }
875 3ce1b845 2019-07-15 stsp
876 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
877 3ce1b845 2019-07-15 stsp branch_ref);
878 ef293bdd 2019-10-21 stsp if (error) {
879 ef293bdd 2019-10-21 stsp if (logmsg_path)
880 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
881 3ce1b845 2019-07-15 stsp goto done;
882 ef293bdd 2019-10-21 stsp }
883 3ce1b845 2019-07-15 stsp
884 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
885 ef293bdd 2019-10-21 stsp if (error) {
886 ef293bdd 2019-10-21 stsp if (logmsg_path)
887 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
888 3ce1b845 2019-07-15 stsp goto done;
889 ef293bdd 2019-10-21 stsp }
890 3ce1b845 2019-07-15 stsp }
891 3ce1b845 2019-07-15 stsp
892 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
893 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
894 2c7829a4 2019-06-17 stsp done:
895 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
896 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
897 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
898 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
899 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
900 8e158b01 2019-09-22 stsp free(logmsg);
901 ef293bdd 2019-10-21 stsp free(logmsg_path);
902 2c7829a4 2019-06-17 stsp free(repo_path);
903 3ce1b845 2019-07-15 stsp free(editor);
904 3ce1b845 2019-07-15 stsp free(refname);
905 3ce1b845 2019-07-15 stsp free(new_commit_id);
906 3ce1b845 2019-07-15 stsp free(id_str);
907 aba9c984 2019-09-08 stsp free(author);
908 c9956ddf 2019-09-08 stsp free(gitconfig_path);
909 3ce1b845 2019-07-15 stsp if (branch_ref)
910 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
911 3ce1b845 2019-07-15 stsp if (head_ref)
912 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
913 2c7829a4 2019-06-17 stsp return error;
914 93658fb9 2020-03-18 stsp }
915 93658fb9 2020-03-18 stsp
916 93658fb9 2020-03-18 stsp __dead static void
917 93658fb9 2020-03-18 stsp usage_clone(void)
918 93658fb9 2020-03-18 stsp {
919 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
920 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
921 93658fb9 2020-03-18 stsp exit(1);
922 93658fb9 2020-03-18 stsp }
923 892ac3b6 2020-03-18 stsp
924 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
925 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
926 892ac3b6 2020-03-18 stsp int last_p_indexed;
927 892ac3b6 2020-03-18 stsp int last_p_resolved;
928 68999b92 2020-03-18 stsp int verbosity;
929 04d9a9ec 2020-09-24 stsp
930 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
931 04d9a9ec 2020-09-24 stsp
932 04d9a9ec 2020-09-24 stsp int create_configs;
933 04d9a9ec 2020-09-24 stsp int configs_created;
934 04d9a9ec 2020-09-24 stsp struct {
935 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
936 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
937 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
938 04d9a9ec 2020-09-24 stsp const char *proto;
939 04d9a9ec 2020-09-24 stsp const char *host;
940 04d9a9ec 2020-09-24 stsp const char *port;
941 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
942 04d9a9ec 2020-09-24 stsp const char *git_url;
943 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
944 04d9a9ec 2020-09-24 stsp int mirror_references;
945 04d9a9ec 2020-09-24 stsp } config_info;
946 892ac3b6 2020-03-18 stsp };
947 93658fb9 2020-03-18 stsp
948 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
949 93658fb9 2020-03-18 stsp static const struct got_error *
950 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
951 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
952 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
953 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
954 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
955 04d9a9ec 2020-09-24 stsp
956 04d9a9ec 2020-09-24 stsp static const struct got_error *
957 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
958 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
959 531c3985 2020-03-18 stsp {
960 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
961 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
962 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
963 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
964 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
965 04d9a9ec 2020-09-24 stsp
966 04d9a9ec 2020-09-24 stsp /*
967 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
968 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
969 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
970 04d9a9ec 2020-09-24 stsp * we have all required information.
971 04d9a9ec 2020-09-24 stsp */
972 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
973 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
974 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
975 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
976 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
977 62d463ca 2020-10-20 naddy a->config_info.git_url,
978 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
979 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
980 62d463ca 2020-10-20 naddy a->config_info.symrefs,
981 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
982 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
983 04d9a9ec 2020-09-24 stsp if (err)
984 04d9a9ec 2020-09-24 stsp return err;
985 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
986 04d9a9ec 2020-09-24 stsp }
987 b2409d58 2020-03-18 stsp
988 68999b92 2020-03-18 stsp if (a->verbosity < 0)
989 68999b92 2020-03-18 stsp return NULL;
990 68999b92 2020-03-18 stsp
991 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
992 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
993 892ac3b6 2020-03-18 stsp fflush(stdout);
994 12d1281e 2020-03-19 stsp return NULL;
995 b2409d58 2020-03-18 stsp }
996 b2409d58 2020-03-18 stsp
997 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
998 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
999 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
1000 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
1001 892ac3b6 2020-03-18 stsp print_size = 1;
1002 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
1003 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1004 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1005 892ac3b6 2020-03-18 stsp }
1006 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1007 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1008 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1009 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1010 892ac3b6 2020-03-18 stsp print_indexed = 1;
1011 892ac3b6 2020-03-18 stsp print_size = 1;
1012 892ac3b6 2020-03-18 stsp }
1013 61cc1a7a 2020-03-18 stsp }
1014 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1015 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1016 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1017 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1018 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1019 892ac3b6 2020-03-18 stsp print_resolved = 1;
1020 892ac3b6 2020-03-18 stsp print_indexed = 1;
1021 892ac3b6 2020-03-18 stsp print_size = 1;
1022 892ac3b6 2020-03-18 stsp }
1023 61cc1a7a 2020-03-18 stsp }
1024 3168e5da 2020-09-10 stsp
1025 d2cdc636 2020-03-18 stsp }
1026 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1027 892ac3b6 2020-03-18 stsp printf("\r");
1028 892ac3b6 2020-03-18 stsp if (print_size)
1029 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1030 d715f13e 2020-03-19 stsp if (print_indexed)
1031 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1032 d715f13e 2020-03-19 stsp if (print_resolved)
1033 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1034 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1035 892ac3b6 2020-03-18 stsp fflush(stdout);
1036 892ac3b6 2020-03-18 stsp
1037 531c3985 2020-03-18 stsp return NULL;
1038 531c3985 2020-03-18 stsp }
1039 531c3985 2020-03-18 stsp
1040 531c3985 2020-03-18 stsp static const struct got_error *
1041 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1042 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1043 4ba14133 2020-03-20 stsp {
1044 4ba14133 2020-03-20 stsp const struct got_error *err;
1045 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1046 4ba14133 2020-03-20 stsp
1047 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1048 4ba14133 2020-03-20 stsp if (err)
1049 4ba14133 2020-03-20 stsp return err;
1050 4ba14133 2020-03-20 stsp
1051 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1052 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1053 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1054 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1055 6338a6a1 2020-03-21 stsp }
1056 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1057 4ba14133 2020-03-20 stsp return err;
1058 4ba14133 2020-03-20 stsp }
1059 4ba14133 2020-03-20 stsp
1060 4ba14133 2020-03-20 stsp static const struct got_error *
1061 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1062 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1063 41b0de12 2020-03-21 stsp {
1064 41b0de12 2020-03-21 stsp const struct got_error *err;
1065 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1066 41b0de12 2020-03-21 stsp
1067 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1068 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1069 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1070 41b0de12 2020-03-21 stsp
1071 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1072 41b0de12 2020-03-21 stsp }
1073 41b0de12 2020-03-21 stsp
1074 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1075 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1076 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1077 41b0de12 2020-03-21 stsp char *id_str;
1078 41b0de12 2020-03-21 stsp
1079 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1080 41b0de12 2020-03-21 stsp if (err)
1081 41b0de12 2020-03-21 stsp return err;
1082 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1083 41b0de12 2020-03-21 stsp free(id_str);
1084 41b0de12 2020-03-21 stsp }
1085 41b0de12 2020-03-21 stsp
1086 41b0de12 2020-03-21 stsp return NULL;
1087 6338a6a1 2020-03-21 stsp }
1088 6338a6a1 2020-03-21 stsp
1089 6338a6a1 2020-03-21 stsp static const struct got_error *
1090 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1091 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1092 6338a6a1 2020-03-21 stsp {
1093 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1094 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1095 6338a6a1 2020-03-21 stsp char *id_str;
1096 6338a6a1 2020-03-21 stsp
1097 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1098 6338a6a1 2020-03-21 stsp if (err)
1099 6338a6a1 2020-03-21 stsp return err;
1100 6338a6a1 2020-03-21 stsp
1101 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1102 6338a6a1 2020-03-21 stsp if (err)
1103 6338a6a1 2020-03-21 stsp goto done;
1104 6338a6a1 2020-03-21 stsp
1105 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1106 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1107 6338a6a1 2020-03-21 stsp
1108 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1109 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1110 6338a6a1 2020-03-21 stsp done:
1111 6338a6a1 2020-03-21 stsp free(id_str);
1112 6338a6a1 2020-03-21 stsp return err;
1113 0e4002ca 2020-03-21 stsp }
1114 0e4002ca 2020-03-21 stsp
1115 0e4002ca 2020-03-21 stsp static int
1116 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1117 0e4002ca 2020-03-21 stsp {
1118 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1119 0e4002ca 2020-03-21 stsp return 0;
1120 0e4002ca 2020-03-21 stsp refname += 5;
1121 0e4002ca 2020-03-21 stsp
1122 0e4002ca 2020-03-21 stsp /*
1123 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1124 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1125 0e4002ca 2020-03-21 stsp */
1126 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1127 0e4002ca 2020-03-21 stsp return 0;
1128 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1129 0e4002ca 2020-03-21 stsp return 0;
1130 0e4002ca 2020-03-21 stsp
1131 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1132 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1133 0e4002ca 2020-03-21 stsp
1134 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1135 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1136 0e4002ca 2020-03-21 stsp return 1;
1137 0e4002ca 2020-03-21 stsp
1138 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1139 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1140 41b0de12 2020-03-21 stsp }
1141 41b0de12 2020-03-21 stsp
1142 0e4002ca 2020-03-21 stsp static int
1143 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1144 0e4002ca 2020-03-21 stsp {
1145 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1146 0e4002ca 2020-03-21 stsp
1147 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1148 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1149 0e4002ca 2020-03-21 stsp return 1;
1150 0e4002ca 2020-03-21 stsp }
1151 0e4002ca 2020-03-21 stsp
1152 0e4002ca 2020-03-21 stsp return 0;
1153 0e4002ca 2020-03-21 stsp }
1154 0e4002ca 2020-03-21 stsp
1155 41b0de12 2020-03-21 stsp static const struct got_error *
1156 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1157 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1158 0e4002ca 2020-03-21 stsp {
1159 0e4002ca 2020-03-21 stsp const struct got_error *err;
1160 0e4002ca 2020-03-21 stsp char *remote_refname;
1161 0e4002ca 2020-03-21 stsp
1162 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1163 0e4002ca 2020-03-21 stsp refname += 5;
1164 0e4002ca 2020-03-21 stsp
1165 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1166 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1167 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1168 0e4002ca 2020-03-21 stsp
1169 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1170 0e4002ca 2020-03-21 stsp free(remote_refname);
1171 7c0b7f42 2020-09-24 stsp return err;
1172 7c0b7f42 2020-09-24 stsp }
1173 7c0b7f42 2020-09-24 stsp
1174 7c0b7f42 2020-09-24 stsp static const struct got_error *
1175 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1176 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1177 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1178 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1179 99495ddb 2021-01-10 stsp struct got_repository *repo)
1180 7c0b7f42 2020-09-24 stsp {
1181 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1182 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1183 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1184 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1185 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1186 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1187 7c0b7f42 2020-09-24 stsp ssize_t n;
1188 7c0b7f42 2020-09-24 stsp
1189 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1190 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1191 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1192 132af4a5 2021-01-05 stsp char *s;
1193 132af4a5 2021-01-05 stsp branchname = pe->path;
1194 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1195 132af4a5 2021-01-05 stsp branchname += 11;
1196 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1197 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1198 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1199 132af4a5 2021-01-05 stsp goto done;
1200 132af4a5 2021-01-05 stsp }
1201 132af4a5 2021-01-05 stsp free(branches);
1202 132af4a5 2021-01-05 stsp branches = s;
1203 132af4a5 2021-01-05 stsp }
1204 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1205 15d3c221 2021-01-05 stsp branchname = default_branch;
1206 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1207 15d3c221 2021-01-05 stsp branchname += 11;
1208 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1209 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1210 132af4a5 2021-01-05 stsp goto done;
1211 99495ddb 2021-01-10 stsp }
1212 99495ddb 2021-01-10 stsp }
1213 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1214 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1215 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1216 99495ddb 2021-01-10 stsp char *s;
1217 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1218 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1219 99495ddb 2021-01-10 stsp branchname += 5;
1220 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1221 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1222 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1223 99495ddb 2021-01-10 stsp goto done;
1224 99495ddb 2021-01-10 stsp }
1225 99495ddb 2021-01-10 stsp free(refs);
1226 99495ddb 2021-01-10 stsp refs = s;
1227 132af4a5 2021-01-05 stsp }
1228 15d3c221 2021-01-05 stsp }
1229 15d3c221 2021-01-05 stsp
1230 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1231 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1232 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1233 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1234 7c0b7f42 2020-09-24 stsp goto done;
1235 7c0b7f42 2020-09-24 stsp }
1236 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1237 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1238 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1239 7c0b7f42 2020-09-24 stsp goto done;
1240 7c0b7f42 2020-09-24 stsp }
1241 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1242 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1243 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1244 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1245 7c0b7f42 2020-09-24 stsp "%s%s%s"
1246 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1247 15d3c221 2021-01-05 stsp "%s%s%s"
1248 99495ddb 2021-01-10 stsp "%s%s%s"
1249 7c0b7f42 2020-09-24 stsp "%s"
1250 0c8b29c5 2021-01-05 stsp "%s"
1251 7c0b7f42 2020-09-24 stsp "}\n",
1252 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1253 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1254 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1255 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1256 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1257 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1258 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1259 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1260 7c0b7f42 2020-09-24 stsp goto done;
1261 7c0b7f42 2020-09-24 stsp }
1262 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1263 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1264 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1265 7c0b7f42 2020-09-24 stsp goto done;
1266 7c0b7f42 2020-09-24 stsp }
1267 7c0b7f42 2020-09-24 stsp
1268 7c0b7f42 2020-09-24 stsp done:
1269 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1270 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1271 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1272 132af4a5 2021-01-05 stsp free(branches);
1273 7c0b7f42 2020-09-24 stsp return err;
1274 7c0b7f42 2020-09-24 stsp }
1275 7c0b7f42 2020-09-24 stsp
1276 7c0b7f42 2020-09-24 stsp static const struct got_error *
1277 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1278 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1279 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1280 99495ddb 2021-01-10 stsp struct got_repository *repo)
1281 7c0b7f42 2020-09-24 stsp {
1282 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1283 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1284 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1285 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1286 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1287 56d0a753 2021-01-20 stsp const char *branchname;
1288 7c0b7f42 2020-09-24 stsp ssize_t n;
1289 7c0b7f42 2020-09-24 stsp
1290 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1291 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1292 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1293 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1294 7c0b7f42 2020-09-24 stsp goto done;
1295 7c0b7f42 2020-09-24 stsp }
1296 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1297 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1298 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1299 7c0b7f42 2020-09-24 stsp goto done;
1300 7c0b7f42 2020-09-24 stsp }
1301 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1302 56d0a753 2021-01-20 stsp if (mirror_references) {
1303 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1304 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1305 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1306 56d0a753 2021-01-20 stsp goto done;
1307 56d0a753 2021-01-20 stsp }
1308 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1309 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1310 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1311 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1312 7c0b7f42 2020-09-24 stsp goto done;
1313 132af4a5 2021-01-05 stsp }
1314 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1315 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1316 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1317 132af4a5 2021-01-05 stsp char *s;
1318 132af4a5 2021-01-05 stsp branchname = pe->path;
1319 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1320 132af4a5 2021-01-05 stsp branchname += 11;
1321 56d0a753 2021-01-20 stsp if (mirror_references) {
1322 56d0a753 2021-01-20 stsp if (asprintf(&s,
1323 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1324 56d0a753 2021-01-20 stsp branches ? branches : "",
1325 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1326 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1327 56d0a753 2021-01-20 stsp goto done;
1328 56d0a753 2021-01-20 stsp }
1329 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1330 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1331 132af4a5 2021-01-05 stsp branches ? branches : "",
1332 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1333 132af4a5 2021-01-05 stsp branchname) == -1) {
1334 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1335 132af4a5 2021-01-05 stsp goto done;
1336 132af4a5 2021-01-05 stsp }
1337 132af4a5 2021-01-05 stsp free(branches);
1338 132af4a5 2021-01-05 stsp branches = s;
1339 7c0b7f42 2020-09-24 stsp }
1340 7c0b7f42 2020-09-24 stsp } else {
1341 7c0b7f42 2020-09-24 stsp /*
1342 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1343 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1344 7c0b7f42 2020-09-24 stsp */
1345 04d9a9ec 2020-09-24 stsp if (default_branch) {
1346 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1347 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1348 7c0b7f42 2020-09-24 stsp branchname += 11;
1349 7c0b7f42 2020-09-24 stsp } else
1350 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1351 56d0a753 2021-01-20 stsp if (mirror_references) {
1352 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1353 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1354 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1355 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1356 56d0a753 2021-01-20 stsp goto done;
1357 56d0a753 2021-01-20 stsp }
1358 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1359 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1360 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1361 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1362 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1363 7c0b7f42 2020-09-24 stsp goto done;
1364 99495ddb 2021-01-10 stsp }
1365 99495ddb 2021-01-10 stsp }
1366 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1367 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1368 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1369 99495ddb 2021-01-10 stsp char *s;
1370 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1371 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1372 99495ddb 2021-01-10 stsp refname += 5;
1373 56d0a753 2021-01-20 stsp if (mirror_references) {
1374 56d0a753 2021-01-20 stsp if (asprintf(&s,
1375 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1376 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1377 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1378 56d0a753 2021-01-20 stsp goto done;
1379 56d0a753 2021-01-20 stsp }
1380 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1381 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1382 99495ddb 2021-01-10 stsp refs ? refs : "",
1383 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1384 99495ddb 2021-01-10 stsp refname) == -1) {
1385 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1386 99495ddb 2021-01-10 stsp goto done;
1387 99495ddb 2021-01-10 stsp }
1388 99495ddb 2021-01-10 stsp free(refs);
1389 99495ddb 2021-01-10 stsp refs = s;
1390 7c0b7f42 2020-09-24 stsp }
1391 132af4a5 2021-01-05 stsp }
1392 99495ddb 2021-01-10 stsp
1393 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1394 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1395 132af4a5 2021-01-05 stsp "\turl = %s\n"
1396 132af4a5 2021-01-05 stsp "%s"
1397 99495ddb 2021-01-10 stsp "%s"
1398 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1399 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1400 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1401 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1402 132af4a5 2021-01-05 stsp goto done;
1403 7c0b7f42 2020-09-24 stsp }
1404 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1405 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1406 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1407 7c0b7f42 2020-09-24 stsp goto done;
1408 7c0b7f42 2020-09-24 stsp }
1409 7c0b7f42 2020-09-24 stsp done:
1410 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1411 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1412 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1413 132af4a5 2021-01-05 stsp free(branches);
1414 0e4002ca 2020-03-21 stsp return err;
1415 0e4002ca 2020-03-21 stsp }
1416 0e4002ca 2020-03-21 stsp
1417 0e4002ca 2020-03-21 stsp static const struct got_error *
1418 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1419 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1420 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1421 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1422 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1423 04d9a9ec 2020-09-24 stsp {
1424 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1425 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1426 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1427 04d9a9ec 2020-09-24 stsp
1428 04d9a9ec 2020-09-24 stsp /*
1429 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1430 04d9a9ec 2020-09-24 stsp * one of those.
1431 04d9a9ec 2020-09-24 stsp */
1432 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1433 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1434 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1435 04d9a9ec 2020-09-24 stsp } else {
1436 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1437 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1438 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1439 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1440 04d9a9ec 2020-09-24 stsp
1441 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1442 04d9a9ec 2020-09-24 stsp continue;
1443 04d9a9ec 2020-09-24 stsp
1444 04d9a9ec 2020-09-24 stsp default_branch = target;
1445 04d9a9ec 2020-09-24 stsp break;
1446 04d9a9ec 2020-09-24 stsp }
1447 04d9a9ec 2020-09-24 stsp }
1448 04d9a9ec 2020-09-24 stsp
1449 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1450 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1451 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1452 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1453 04d9a9ec 2020-09-24 stsp if (err)
1454 04d9a9ec 2020-09-24 stsp return err;
1455 04d9a9ec 2020-09-24 stsp
1456 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1457 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1458 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1459 04d9a9ec 2020-09-24 stsp }
1460 04d9a9ec 2020-09-24 stsp
1461 04d9a9ec 2020-09-24 stsp static const struct got_error *
1462 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1463 93658fb9 2020-03-18 stsp {
1464 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1465 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1466 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1467 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1468 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1469 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1470 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1471 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1472 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1473 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1474 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1475 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1476 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1477 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1478 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1479 93658fb9 2020-03-18 stsp
1480 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1481 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1482 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1483 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1484 d9b4d0c0 2020-03-18 stsp
1485 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1486 93658fb9 2020-03-18 stsp switch (ch) {
1487 659e7fbd 2020-03-20 stsp case 'a':
1488 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1489 4ba14133 2020-03-20 stsp break;
1490 4ba14133 2020-03-20 stsp case 'b':
1491 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1492 4ba14133 2020-03-20 stsp optarg, NULL);
1493 4ba14133 2020-03-20 stsp if (error)
1494 4ba14133 2020-03-20 stsp return error;
1495 659e7fbd 2020-03-20 stsp break;
1496 41b0de12 2020-03-21 stsp case 'l':
1497 41b0de12 2020-03-21 stsp list_refs_only = 1;
1498 41b0de12 2020-03-21 stsp break;
1499 469dd726 2020-03-20 stsp case 'm':
1500 469dd726 2020-03-20 stsp mirror_references = 1;
1501 469dd726 2020-03-20 stsp break;
1502 68999b92 2020-03-18 stsp case 'v':
1503 68999b92 2020-03-18 stsp if (verbosity < 0)
1504 68999b92 2020-03-18 stsp verbosity = 0;
1505 68999b92 2020-03-18 stsp else if (verbosity < 3)
1506 68999b92 2020-03-18 stsp verbosity++;
1507 68999b92 2020-03-18 stsp break;
1508 68999b92 2020-03-18 stsp case 'q':
1509 68999b92 2020-03-18 stsp verbosity = -1;
1510 68999b92 2020-03-18 stsp break;
1511 0e4002ca 2020-03-21 stsp case 'R':
1512 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1513 0e4002ca 2020-03-21 stsp optarg, NULL);
1514 0e4002ca 2020-03-21 stsp if (error)
1515 0e4002ca 2020-03-21 stsp return error;
1516 0e4002ca 2020-03-21 stsp break;
1517 93658fb9 2020-03-18 stsp default:
1518 93658fb9 2020-03-18 stsp usage_clone();
1519 93658fb9 2020-03-18 stsp break;
1520 93658fb9 2020-03-18 stsp }
1521 93658fb9 2020-03-18 stsp }
1522 93658fb9 2020-03-18 stsp argc -= optind;
1523 93658fb9 2020-03-18 stsp argv += optind;
1524 39c64a6a 2020-03-18 stsp
1525 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1526 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1527 41b0de12 2020-03-21 stsp if (list_refs_only) {
1528 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1529 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1530 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1531 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1532 41b0de12 2020-03-21 stsp if (mirror_references)
1533 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1534 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1535 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1536 41b0de12 2020-03-21 stsp }
1537 4ba14133 2020-03-20 stsp
1538 93658fb9 2020-03-18 stsp uri = argv[0];
1539 9df6f38b 2020-03-18 stsp
1540 9df6f38b 2020-03-18 stsp if (argc == 1)
1541 93658fb9 2020-03-18 stsp dirname = NULL;
1542 9df6f38b 2020-03-18 stsp else if (argc == 2)
1543 93658fb9 2020-03-18 stsp dirname = argv[1];
1544 93658fb9 2020-03-18 stsp else
1545 93658fb9 2020-03-18 stsp usage_clone();
1546 09838ffc 2020-03-18 stsp
1547 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1548 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1549 39c64a6a 2020-03-18 stsp if (error)
1550 09f63084 2020-03-20 stsp goto done;
1551 09f63084 2020-03-20 stsp
1552 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1553 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1554 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1555 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1556 09838ffc 2020-03-18 stsp goto done;
1557 09f63084 2020-03-20 stsp }
1558 09838ffc 2020-03-18 stsp
1559 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1560 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1561 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1562 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1563 39c64a6a 2020-03-18 stsp err(1, "pledge");
1564 b46f3e71 2020-03-18 stsp #endif
1565 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1566 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1567 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1568 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1569 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1570 39c64a6a 2020-03-18 stsp err(1, "pledge");
1571 b46f3e71 2020-03-18 stsp #endif
1572 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1573 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1574 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1575 39c64a6a 2020-03-18 stsp goto done;
1576 39c64a6a 2020-03-18 stsp } else {
1577 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1578 39c64a6a 2020-03-18 stsp goto done;
1579 39c64a6a 2020-03-18 stsp }
1580 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1581 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1582 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1583 bb64b798 2020-03-18 stsp goto done;
1584 bb64b798 2020-03-18 stsp }
1585 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1586 bb64b798 2020-03-18 stsp } else
1587 bb64b798 2020-03-18 stsp repo_path = dirname;
1588 bb64b798 2020-03-18 stsp
1589 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1590 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1591 2751fe64 2020-09-24 stsp if (error &&
1592 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1593 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1594 41b0de12 2020-03-21 stsp goto done;
1595 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1596 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1597 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1598 41b0de12 2020-03-21 stsp goto done;
1599 2751fe64 2020-09-24 stsp }
1600 41b0de12 2020-03-21 stsp }
1601 bb64b798 2020-03-18 stsp
1602 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
1603 d65a88a2 2021-09-05 stsp if (error)
1604 d65a88a2 2021-09-05 stsp goto done;
1605 d65a88a2 2021-09-05 stsp
1606 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1607 ee448f5f 2020-03-18 stsp if (error)
1608 ee448f5f 2020-03-18 stsp goto done;
1609 f79e6490 2020-04-19 stsp
1610 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1611 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1612 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1613 ee448f5f 2020-03-18 stsp
1614 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1615 9c52365f 2020-03-21 stsp server_path, verbosity);
1616 39c64a6a 2020-03-18 stsp if (error)
1617 bb64b798 2020-03-18 stsp goto done;
1618 bb64b798 2020-03-18 stsp
1619 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1620 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1621 2751fe64 2020-09-24 stsp if (error)
1622 2751fe64 2020-09-24 stsp goto done;
1623 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1624 2751fe64 2020-09-24 stsp if (error)
1625 2751fe64 2020-09-24 stsp goto done;
1626 2751fe64 2020-09-24 stsp }
1627 2751fe64 2020-09-24 stsp
1628 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1629 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1630 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1631 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1632 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1633 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1634 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1635 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1636 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1637 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1639 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1640 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1641 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1644 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1645 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1646 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1647 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1648 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1649 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1650 39c64a6a 2020-03-18 stsp if (error)
1651 d9b4d0c0 2020-03-18 stsp goto done;
1652 d9b4d0c0 2020-03-18 stsp
1653 41b0de12 2020-03-21 stsp if (list_refs_only) {
1654 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1655 41b0de12 2020-03-21 stsp goto done;
1656 41b0de12 2020-03-21 stsp }
1657 41b0de12 2020-03-21 stsp
1658 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1659 39c64a6a 2020-03-18 stsp if (error)
1660 d9b4d0c0 2020-03-18 stsp goto done;
1661 68999b92 2020-03-18 stsp if (verbosity >= 0)
1662 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1663 d9b4d0c0 2020-03-18 stsp free(id_str);
1664 d9b4d0c0 2020-03-18 stsp
1665 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1666 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1667 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1668 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1669 7ebc0570 2020-03-18 stsp char *remote_refname;
1670 0e4002ca 2020-03-21 stsp
1671 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1672 0e4002ca 2020-03-21 stsp !mirror_references) {
1673 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1674 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1675 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1676 0e4002ca 2020-03-21 stsp if (error)
1677 0e4002ca 2020-03-21 stsp goto done;
1678 0e4002ca 2020-03-21 stsp continue;
1679 0e4002ca 2020-03-21 stsp }
1680 668a20f6 2020-03-18 stsp
1681 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1682 39c64a6a 2020-03-18 stsp if (error)
1683 d9b4d0c0 2020-03-18 stsp goto done;
1684 d9b4d0c0 2020-03-18 stsp
1685 469dd726 2020-03-20 stsp if (mirror_references)
1686 469dd726 2020-03-20 stsp continue;
1687 469dd726 2020-03-20 stsp
1688 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1689 7ebc0570 2020-03-18 stsp continue;
1690 7ebc0570 2020-03-18 stsp
1691 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1692 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1693 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1694 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1695 7ebc0570 2020-03-18 stsp goto done;
1696 7ebc0570 2020-03-18 stsp }
1697 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1698 f298ae0f 2020-03-25 stsp free(remote_refname);
1699 39c64a6a 2020-03-18 stsp if (error)
1700 d9b4d0c0 2020-03-18 stsp goto done;
1701 d9b4d0c0 2020-03-18 stsp }
1702 d9b4d0c0 2020-03-18 stsp
1703 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1704 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1705 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1706 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1707 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1708 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1709 d9b4d0c0 2020-03-18 stsp
1710 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1711 d9b4d0c0 2020-03-18 stsp continue;
1712 d9b4d0c0 2020-03-18 stsp
1713 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1714 39c64a6a 2020-03-18 stsp if (error) {
1715 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1716 55330abe 2020-03-20 stsp error = NULL;
1717 d9b4d0c0 2020-03-18 stsp continue;
1718 55330abe 2020-03-20 stsp }
1719 d9b4d0c0 2020-03-18 stsp goto done;
1720 d9b4d0c0 2020-03-18 stsp }
1721 d9b4d0c0 2020-03-18 stsp
1722 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1723 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1724 f298ae0f 2020-03-25 stsp if (error)
1725 f298ae0f 2020-03-25 stsp goto done;
1726 f298ae0f 2020-03-25 stsp
1727 f298ae0f 2020-03-25 stsp if (mirror_references)
1728 f298ae0f 2020-03-25 stsp continue;
1729 f298ae0f 2020-03-25 stsp
1730 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1731 f298ae0f 2020-03-25 stsp continue;
1732 f298ae0f 2020-03-25 stsp
1733 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1734 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1735 f298ae0f 2020-03-25 stsp refname) == -1) {
1736 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1737 f298ae0f 2020-03-25 stsp goto done;
1738 f298ae0f 2020-03-25 stsp }
1739 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1740 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1741 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1742 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1743 f298ae0f 2020-03-25 stsp free(remote_refname);
1744 f298ae0f 2020-03-25 stsp goto done;
1745 f298ae0f 2020-03-25 stsp }
1746 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1747 f298ae0f 2020-03-25 stsp if (error) {
1748 f298ae0f 2020-03-25 stsp free(remote_refname);
1749 f298ae0f 2020-03-25 stsp free(remote_target);
1750 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1751 f298ae0f 2020-03-25 stsp error = NULL;
1752 f298ae0f 2020-03-25 stsp continue;
1753 f298ae0f 2020-03-25 stsp }
1754 f298ae0f 2020-03-25 stsp goto done;
1755 f298ae0f 2020-03-25 stsp }
1756 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1757 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1758 f298ae0f 2020-03-25 stsp free(remote_refname);
1759 f298ae0f 2020-03-25 stsp free(remote_target);
1760 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1761 39c64a6a 2020-03-18 stsp if (error)
1762 d9b4d0c0 2020-03-18 stsp goto done;
1763 4ba14133 2020-03-20 stsp }
1764 4ba14133 2020-03-20 stsp if (pe == NULL) {
1765 4ba14133 2020-03-20 stsp /*
1766 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1767 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1768 4ba14133 2020-03-20 stsp * which could be fetched instead.
1769 4ba14133 2020-03-20 stsp */
1770 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1771 4ba14133 2020-03-20 stsp const char *target = pe->path;
1772 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1773 d9b4d0c0 2020-03-18 stsp
1774 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1775 4ba14133 2020-03-20 stsp if (error) {
1776 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1777 4ba14133 2020-03-20 stsp error = NULL;
1778 4ba14133 2020-03-20 stsp continue;
1779 4ba14133 2020-03-20 stsp }
1780 4ba14133 2020-03-20 stsp goto done;
1781 4ba14133 2020-03-20 stsp }
1782 4ba14133 2020-03-20 stsp
1783 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1784 04d9a9ec 2020-09-24 stsp verbosity, repo);
1785 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1786 4ba14133 2020-03-20 stsp if (error)
1787 4ba14133 2020-03-20 stsp goto done;
1788 4ba14133 2020-03-20 stsp break;
1789 4ba14133 2020-03-20 stsp }
1790 659e7fbd 2020-03-20 stsp }
1791 659e7fbd 2020-03-20 stsp
1792 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1793 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1794 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1795 09838ffc 2020-03-18 stsp done:
1796 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1797 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1798 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1799 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1800 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1801 9c52365f 2020-03-21 stsp }
1802 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1803 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1804 1d0f4054 2021-06-17 stsp if (repo) {
1805 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
1806 1d0f4054 2021-06-17 stsp if (error == NULL)
1807 1d0f4054 2021-06-17 stsp error = close_err;
1808 1d0f4054 2021-06-17 stsp }
1809 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1810 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1811 d9b4d0c0 2020-03-18 stsp free(pe->data);
1812 d9b4d0c0 2020-03-18 stsp }
1813 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1814 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1815 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1816 d9b4d0c0 2020-03-18 stsp free(pe->data);
1817 d9b4d0c0 2020-03-18 stsp }
1818 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1819 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1820 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1821 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1822 09838ffc 2020-03-18 stsp free(proto);
1823 09838ffc 2020-03-18 stsp free(host);
1824 09838ffc 2020-03-18 stsp free(port);
1825 09838ffc 2020-03-18 stsp free(server_path);
1826 09838ffc 2020-03-18 stsp free(repo_name);
1827 bb64b798 2020-03-18 stsp free(default_destdir);
1828 b46f3e71 2020-03-18 stsp free(git_url);
1829 39c64a6a 2020-03-18 stsp return error;
1830 7848a0e1 2020-03-19 stsp }
1831 7848a0e1 2020-03-19 stsp
1832 7848a0e1 2020-03-19 stsp static const struct got_error *
1833 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1834 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1835 7848a0e1 2020-03-19 stsp {
1836 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1837 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1838 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1839 7848a0e1 2020-03-19 stsp
1840 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1841 7848a0e1 2020-03-19 stsp if (err)
1842 7848a0e1 2020-03-19 stsp goto done;
1843 7848a0e1 2020-03-19 stsp
1844 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1845 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1846 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1847 88609724 2020-03-21 stsp if (err)
1848 88609724 2020-03-21 stsp goto done;
1849 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1850 88609724 2020-03-21 stsp goto done;
1851 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1852 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1853 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1854 db6d8ad8 2020-03-21 stsp }
1855 db6d8ad8 2020-03-21 stsp goto done;
1856 db6d8ad8 2020-03-21 stsp }
1857 db6d8ad8 2020-03-21 stsp
1858 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1859 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1860 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1861 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1862 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1863 688f11b3 2020-03-21 stsp }
1864 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1865 7848a0e1 2020-03-19 stsp if (err)
1866 7848a0e1 2020-03-19 stsp goto done;
1867 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1868 e8a967e0 2020-03-21 stsp if (err)
1869 e8a967e0 2020-03-21 stsp goto done;
1870 7848a0e1 2020-03-19 stsp } else {
1871 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1872 7848a0e1 2020-03-19 stsp if (err)
1873 7848a0e1 2020-03-19 stsp goto done;
1874 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1875 6338a6a1 2020-03-21 stsp goto done;
1876 6338a6a1 2020-03-21 stsp
1877 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1878 6338a6a1 2020-03-21 stsp if (err)
1879 6338a6a1 2020-03-21 stsp goto done;
1880 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1881 6338a6a1 2020-03-21 stsp if (err)
1882 6338a6a1 2020-03-21 stsp goto done;
1883 7848a0e1 2020-03-19 stsp }
1884 6338a6a1 2020-03-21 stsp
1885 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1886 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1887 6338a6a1 2020-03-21 stsp new_id_str);
1888 7848a0e1 2020-03-19 stsp done:
1889 7848a0e1 2020-03-19 stsp free(old_id);
1890 7848a0e1 2020-03-19 stsp free(new_id_str);
1891 7848a0e1 2020-03-19 stsp return err;
1892 2ab43947 2020-03-18 stsp }
1893 f1bcca34 2020-03-25 stsp
1894 f1bcca34 2020-03-25 stsp static const struct got_error *
1895 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1896 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1897 f1bcca34 2020-03-25 stsp {
1898 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1899 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1900 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1901 f1bcca34 2020-03-25 stsp
1902 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1903 bcf34b0e 2020-03-26 stsp if (err) {
1904 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1905 bcf34b0e 2020-03-26 stsp return err;
1906 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1907 bcf34b0e 2020-03-26 stsp if (err)
1908 bcf34b0e 2020-03-26 stsp goto done;
1909 2ab43947 2020-03-18 stsp
1910 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1911 bcf34b0e 2020-03-26 stsp if (err)
1912 bcf34b0e 2020-03-26 stsp goto done;
1913 f1bcca34 2020-03-25 stsp
1914 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1915 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1916 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1917 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1918 bcf34b0e 2020-03-26 stsp } else {
1919 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1920 f1bcca34 2020-03-25 stsp
1921 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1922 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1923 bcf34b0e 2020-03-26 stsp goto done;
1924 bcf34b0e 2020-03-26 stsp
1925 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1926 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1927 bcf34b0e 2020-03-26 stsp if (err)
1928 bcf34b0e 2020-03-26 stsp goto done;
1929 bcf34b0e 2020-03-26 stsp
1930 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1931 bcf34b0e 2020-03-26 stsp if (err)
1932 bcf34b0e 2020-03-26 stsp goto done;
1933 bcf34b0e 2020-03-26 stsp
1934 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1935 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1936 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1937 bcf34b0e 2020-03-26 stsp
1938 bcf34b0e 2020-03-26 stsp }
1939 f1bcca34 2020-03-25 stsp done:
1940 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1941 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1942 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1943 bcf34b0e 2020-03-26 stsp err = unlock_err;
1944 bcf34b0e 2020-03-26 stsp }
1945 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1946 f1bcca34 2020-03-25 stsp return err;
1947 f1bcca34 2020-03-25 stsp }
1948 f1bcca34 2020-03-25 stsp
1949 2ab43947 2020-03-18 stsp __dead static void
1950 7848a0e1 2020-03-19 stsp usage_fetch(void)
1951 7848a0e1 2020-03-19 stsp {
1952 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1953 161728eb 2021-07-24 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1954 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1955 13f12b09 2020-03-21 stsp getprogname());
1956 7848a0e1 2020-03-19 stsp exit(1);
1957 7848a0e1 2020-03-19 stsp }
1958 7848a0e1 2020-03-19 stsp
1959 7848a0e1 2020-03-19 stsp static const struct got_error *
1960 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1961 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1962 f21ec2f0 2020-03-21 stsp {
1963 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1964 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1965 3789fd73 2020-03-26 stsp char *id_str = NULL;
1966 3789fd73 2020-03-26 stsp
1967 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1968 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1969 3789fd73 2020-03-26 stsp if (err)
1970 3789fd73 2020-03-26 stsp return err;
1971 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1972 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1973 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1974 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1975 3789fd73 2020-03-26 stsp }
1976 3789fd73 2020-03-26 stsp } else {
1977 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1978 3789fd73 2020-03-26 stsp if (err)
1979 3789fd73 2020-03-26 stsp return err;
1980 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1981 3789fd73 2020-03-26 stsp if (err)
1982 3789fd73 2020-03-26 stsp goto done;
1983 3168e5da 2020-09-10 stsp
1984 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1985 3789fd73 2020-03-26 stsp if (err)
1986 3789fd73 2020-03-26 stsp goto done;
1987 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1988 f9d54ee6 2021-07-16 stsp printf("Deleted %s: %s\n",
1989 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1990 3789fd73 2020-03-26 stsp }
1991 3789fd73 2020-03-26 stsp }
1992 3789fd73 2020-03-26 stsp done:
1993 3789fd73 2020-03-26 stsp free(id);
1994 3789fd73 2020-03-26 stsp free(id_str);
1995 3789fd73 2020-03-26 stsp return NULL;
1996 3789fd73 2020-03-26 stsp }
1997 3789fd73 2020-03-26 stsp
1998 3789fd73 2020-03-26 stsp static const struct got_error *
1999 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
2000 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
2001 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
2002 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2003 3789fd73 2020-03-26 stsp {
2004 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2005 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2006 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2007 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2008 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2009 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2010 f21ec2f0 2020-03-21 stsp
2011 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2012 f21ec2f0 2020-03-21 stsp
2013 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2014 3789fd73 2020-03-26 stsp == -1)
2015 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2016 3789fd73 2020-03-26 stsp
2017 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2018 f21ec2f0 2020-03-21 stsp if (err)
2019 3789fd73 2020-03-26 stsp goto done;
2020 f21ec2f0 2020-03-21 stsp
2021 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2022 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2023 f21ec2f0 2020-03-21 stsp
2024 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
2025 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2026 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2027 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2028 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2029 3789fd73 2020-03-26 stsp continue;
2030 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2031 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2032 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2033 3789fd73 2020-03-26 stsp goto done;
2034 3789fd73 2020-03-26 stsp }
2035 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2036 3789fd73 2020-03-26 stsp continue;
2037 3789fd73 2020-03-26 stsp }
2038 f21ec2f0 2020-03-21 stsp
2039 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2040 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2041 f21ec2f0 2020-03-21 stsp break;
2042 f21ec2f0 2020-03-21 stsp }
2043 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2044 f21ec2f0 2020-03-21 stsp continue;
2045 f21ec2f0 2020-03-21 stsp
2046 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2047 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2048 3789fd73 2020-03-26 stsp break;
2049 3789fd73 2020-03-26 stsp }
2050 3789fd73 2020-03-26 stsp if (pe != NULL)
2051 3789fd73 2020-03-26 stsp continue;
2052 f21ec2f0 2020-03-21 stsp
2053 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2054 f21ec2f0 2020-03-21 stsp if (err)
2055 f21ec2f0 2020-03-21 stsp break;
2056 3789fd73 2020-03-26 stsp
2057 3789fd73 2020-03-26 stsp if (local_refname) {
2058 3789fd73 2020-03-26 stsp struct got_reference *ref;
2059 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2060 3789fd73 2020-03-26 stsp if (err) {
2061 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2062 3789fd73 2020-03-26 stsp break;
2063 3789fd73 2020-03-26 stsp free(local_refname);
2064 3789fd73 2020-03-26 stsp local_refname = NULL;
2065 3789fd73 2020-03-26 stsp continue;
2066 3789fd73 2020-03-26 stsp }
2067 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2068 3789fd73 2020-03-26 stsp if (err)
2069 3789fd73 2020-03-26 stsp break;
2070 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2071 3789fd73 2020-03-26 stsp got_ref_close(ref);
2072 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2073 3789fd73 2020-03-26 stsp err = unlock_err;
2074 3789fd73 2020-03-26 stsp break;
2075 3789fd73 2020-03-26 stsp }
2076 3789fd73 2020-03-26 stsp
2077 3789fd73 2020-03-26 stsp free(local_refname);
2078 3789fd73 2020-03-26 stsp local_refname = NULL;
2079 6338a6a1 2020-03-21 stsp }
2080 f21ec2f0 2020-03-21 stsp }
2081 3789fd73 2020-03-26 stsp done:
2082 3789fd73 2020-03-26 stsp free(remote_namespace);
2083 3789fd73 2020-03-26 stsp free(local_refname);
2084 0e4002ca 2020-03-21 stsp return err;
2085 0e4002ca 2020-03-21 stsp }
2086 0e4002ca 2020-03-21 stsp
2087 0e4002ca 2020-03-21 stsp static const struct got_error *
2088 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2089 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2090 0e4002ca 2020-03-21 stsp {
2091 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2092 0e4002ca 2020-03-21 stsp char *remote_refname;
2093 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2094 0e4002ca 2020-03-21 stsp
2095 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2096 0e4002ca 2020-03-21 stsp refname += 5;
2097 0e4002ca 2020-03-21 stsp
2098 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2099 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2100 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2101 f21ec2f0 2020-03-21 stsp
2102 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2103 0e4002ca 2020-03-21 stsp if (err) {
2104 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2105 0e4002ca 2020-03-21 stsp goto done;
2106 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2107 0e4002ca 2020-03-21 stsp } else {
2108 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2109 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2110 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2111 9f142382 2020-03-21 stsp err = unlock_err;
2112 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2113 0e4002ca 2020-03-21 stsp }
2114 0e4002ca 2020-03-21 stsp done:
2115 0e4002ca 2020-03-21 stsp free(remote_refname);
2116 161728eb 2021-07-24 stsp return err;
2117 161728eb 2021-07-24 stsp }
2118 161728eb 2021-07-24 stsp
2119 161728eb 2021-07-24 stsp static const struct got_error *
2120 161728eb 2021-07-24 stsp delete_ref(struct got_repository *repo, struct got_reference *ref)
2121 161728eb 2021-07-24 stsp {
2122 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2123 161728eb 2021-07-24 stsp struct got_object_id *id = NULL;
2124 161728eb 2021-07-24 stsp char *id_str = NULL;
2125 161728eb 2021-07-24 stsp const char *target;
2126 161728eb 2021-07-24 stsp
2127 161728eb 2021-07-24 stsp if (got_ref_is_symbolic(ref)) {
2128 161728eb 2021-07-24 stsp target = got_ref_get_symref_target(ref);
2129 161728eb 2021-07-24 stsp } else {
2130 161728eb 2021-07-24 stsp err = got_ref_resolve(&id, repo, ref);
2131 161728eb 2021-07-24 stsp if (err)
2132 161728eb 2021-07-24 stsp goto done;
2133 161728eb 2021-07-24 stsp err = got_object_id_str(&id_str, id);
2134 161728eb 2021-07-24 stsp if (err)
2135 161728eb 2021-07-24 stsp goto done;
2136 161728eb 2021-07-24 stsp target = id_str;
2137 161728eb 2021-07-24 stsp }
2138 161728eb 2021-07-24 stsp
2139 161728eb 2021-07-24 stsp err = got_ref_delete(ref, repo);
2140 161728eb 2021-07-24 stsp if (err)
2141 161728eb 2021-07-24 stsp goto done;
2142 161728eb 2021-07-24 stsp
2143 161728eb 2021-07-24 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2144 161728eb 2021-07-24 stsp done:
2145 161728eb 2021-07-24 stsp free(id);
2146 161728eb 2021-07-24 stsp free(id_str);
2147 f21ec2f0 2020-03-21 stsp return err;
2148 f21ec2f0 2020-03-21 stsp }
2149 f21ec2f0 2020-03-21 stsp
2150 f21ec2f0 2020-03-21 stsp static const struct got_error *
2151 161728eb 2021-07-24 stsp delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2152 161728eb 2021-07-24 stsp {
2153 161728eb 2021-07-24 stsp const struct got_error *err = NULL;
2154 161728eb 2021-07-24 stsp struct got_reflist_head refs;
2155 161728eb 2021-07-24 stsp struct got_reflist_entry *re;
2156 161728eb 2021-07-24 stsp char *prefix;
2157 161728eb 2021-07-24 stsp
2158 161728eb 2021-07-24 stsp TAILQ_INIT(&refs);
2159 161728eb 2021-07-24 stsp
2160 161728eb 2021-07-24 stsp if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2161 161728eb 2021-07-24 stsp err = got_error_from_errno("asprintf");
2162 161728eb 2021-07-24 stsp goto done;
2163 161728eb 2021-07-24 stsp }
2164 161728eb 2021-07-24 stsp err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2165 161728eb 2021-07-24 stsp if (err)
2166 161728eb 2021-07-24 stsp goto done;
2167 161728eb 2021-07-24 stsp
2168 161728eb 2021-07-24 stsp TAILQ_FOREACH(re, &refs, entry)
2169 161728eb 2021-07-24 stsp delete_ref(repo, re->ref);
2170 161728eb 2021-07-24 stsp done:
2171 161728eb 2021-07-24 stsp got_ref_list_free(&refs);
2172 161728eb 2021-07-24 stsp return err;
2173 161728eb 2021-07-24 stsp }
2174 161728eb 2021-07-24 stsp
2175 161728eb 2021-07-24 stsp static const struct got_error *
2176 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2177 7848a0e1 2020-03-19 stsp {
2178 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2179 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2180 7848a0e1 2020-03-19 stsp const char *remote_name;
2181 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2182 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2183 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2184 7848a0e1 2020-03-19 stsp int nremotes;
2185 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2186 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2187 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2188 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2189 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2190 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2191 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2192 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2193 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2194 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2195 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2196 161728eb 2021-07-24 stsp int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2197 7848a0e1 2020-03-19 stsp
2198 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2199 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2200 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2201 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2202 7848a0e1 2020-03-19 stsp
2203 161728eb 2021-07-24 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2204 7848a0e1 2020-03-19 stsp switch (ch) {
2205 659e7fbd 2020-03-20 stsp case 'a':
2206 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2207 4ba14133 2020-03-20 stsp break;
2208 4ba14133 2020-03-20 stsp case 'b':
2209 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2210 4ba14133 2020-03-20 stsp optarg, NULL);
2211 4ba14133 2020-03-20 stsp if (error)
2212 4ba14133 2020-03-20 stsp return error;
2213 41b0de12 2020-03-21 stsp break;
2214 f21ec2f0 2020-03-21 stsp case 'd':
2215 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2216 f21ec2f0 2020-03-21 stsp break;
2217 41b0de12 2020-03-21 stsp case 'l':
2218 41b0de12 2020-03-21 stsp list_refs_only = 1;
2219 659e7fbd 2020-03-20 stsp break;
2220 7848a0e1 2020-03-19 stsp case 'r':
2221 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2222 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2223 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2224 7848a0e1 2020-03-19 stsp optarg);
2225 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2226 7848a0e1 2020-03-19 stsp break;
2227 db6d8ad8 2020-03-21 stsp case 't':
2228 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2229 db6d8ad8 2020-03-21 stsp break;
2230 7848a0e1 2020-03-19 stsp case 'v':
2231 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2232 7848a0e1 2020-03-19 stsp verbosity = 0;
2233 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2234 7848a0e1 2020-03-19 stsp verbosity++;
2235 7848a0e1 2020-03-19 stsp break;
2236 7848a0e1 2020-03-19 stsp case 'q':
2237 7848a0e1 2020-03-19 stsp verbosity = -1;
2238 7848a0e1 2020-03-19 stsp break;
2239 0e4002ca 2020-03-21 stsp case 'R':
2240 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2241 0e4002ca 2020-03-21 stsp optarg, NULL);
2242 0e4002ca 2020-03-21 stsp if (error)
2243 0e4002ca 2020-03-21 stsp return error;
2244 0e4002ca 2020-03-21 stsp break;
2245 161728eb 2021-07-24 stsp case 'X':
2246 161728eb 2021-07-24 stsp delete_remote = 1;
2247 161728eb 2021-07-24 stsp break;
2248 7848a0e1 2020-03-19 stsp default:
2249 7848a0e1 2020-03-19 stsp usage_fetch();
2250 7848a0e1 2020-03-19 stsp break;
2251 7848a0e1 2020-03-19 stsp }
2252 7848a0e1 2020-03-19 stsp }
2253 7848a0e1 2020-03-19 stsp argc -= optind;
2254 7848a0e1 2020-03-19 stsp argv += optind;
2255 7848a0e1 2020-03-19 stsp
2256 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2257 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2258 41b0de12 2020-03-21 stsp if (list_refs_only) {
2259 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2260 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2261 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2262 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2263 f21ec2f0 2020-03-21 stsp if (delete_refs)
2264 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2265 161728eb 2021-07-24 stsp if (delete_remote)
2266 161728eb 2021-07-24 stsp option_conflict('l', 'X');
2267 41b0de12 2020-03-21 stsp }
2268 161728eb 2021-07-24 stsp if (delete_remote) {
2269 161728eb 2021-07-24 stsp if (fetch_all_branches)
2270 161728eb 2021-07-24 stsp option_conflict('X', 'a');
2271 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_branches))
2272 161728eb 2021-07-24 stsp option_conflict('X', 'b');
2273 161728eb 2021-07-24 stsp if (delete_refs)
2274 161728eb 2021-07-24 stsp option_conflict('X', 'd');
2275 161728eb 2021-07-24 stsp if (replace_tags)
2276 161728eb 2021-07-24 stsp option_conflict('X', 't');
2277 161728eb 2021-07-24 stsp if (!TAILQ_EMPTY(&wanted_refs))
2278 161728eb 2021-07-24 stsp option_conflict('X', 'R');
2279 161728eb 2021-07-24 stsp }
2280 161728eb 2021-07-24 stsp
2281 161728eb 2021-07-24 stsp if (argc == 0) {
2282 161728eb 2021-07-24 stsp if (delete_remote)
2283 161728eb 2021-07-24 stsp errx(1, "-X option requires a remote name");
2284 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2285 161728eb 2021-07-24 stsp } else if (argc == 1)
2286 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2287 7848a0e1 2020-03-19 stsp else
2288 7848a0e1 2020-03-19 stsp usage_fetch();
2289 7848a0e1 2020-03-19 stsp
2290 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2291 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2292 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2293 7848a0e1 2020-03-19 stsp goto done;
2294 7848a0e1 2020-03-19 stsp }
2295 7848a0e1 2020-03-19 stsp
2296 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2297 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2298 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2299 7848a0e1 2020-03-19 stsp goto done;
2300 7848a0e1 2020-03-19 stsp else
2301 7848a0e1 2020-03-19 stsp error = NULL;
2302 7848a0e1 2020-03-19 stsp if (worktree) {
2303 7848a0e1 2020-03-19 stsp repo_path =
2304 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2305 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2306 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2307 7848a0e1 2020-03-19 stsp if (error)
2308 7848a0e1 2020-03-19 stsp goto done;
2309 7848a0e1 2020-03-19 stsp } else {
2310 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2311 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2312 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2313 7848a0e1 2020-03-19 stsp goto done;
2314 7848a0e1 2020-03-19 stsp }
2315 7848a0e1 2020-03-19 stsp }
2316 7848a0e1 2020-03-19 stsp }
2317 7848a0e1 2020-03-19 stsp
2318 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2319 7848a0e1 2020-03-19 stsp if (error)
2320 7848a0e1 2020-03-19 stsp goto done;
2321 7848a0e1 2020-03-19 stsp
2322 161728eb 2021-07-24 stsp if (delete_remote) {
2323 161728eb 2021-07-24 stsp error = delete_refs_for_remote(repo, remote_name);
2324 161728eb 2021-07-24 stsp goto done; /* nothing else to do */
2325 161728eb 2021-07-24 stsp }
2326 161728eb 2021-07-24 stsp
2327 50b0790e 2020-09-11 stsp if (worktree) {
2328 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2329 50b0790e 2020-09-11 stsp if (worktree_conf) {
2330 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2331 50b0790e 2020-09-11 stsp worktree_conf);
2332 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2333 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2334 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2335 50b0790e 2020-09-11 stsp break;
2336 54eb00d5 2020-10-20 stsp }
2337 50b0790e 2020-09-11 stsp }
2338 50b0790e 2020-09-11 stsp }
2339 7848a0e1 2020-03-19 stsp }
2340 50b0790e 2020-09-11 stsp if (remote == NULL) {
2341 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2342 50b0790e 2020-09-11 stsp if (repo_conf) {
2343 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2344 50b0790e 2020-09-11 stsp repo_conf);
2345 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2346 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2347 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2348 50b0790e 2020-09-11 stsp break;
2349 54eb00d5 2020-10-20 stsp }
2350 50b0790e 2020-09-11 stsp }
2351 50b0790e 2020-09-11 stsp }
2352 50b0790e 2020-09-11 stsp }
2353 50b0790e 2020-09-11 stsp if (remote == NULL) {
2354 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2355 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2356 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2357 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2358 257add31 2020-09-09 stsp break;
2359 54eb00d5 2020-10-20 stsp }
2360 257add31 2020-09-09 stsp }
2361 7848a0e1 2020-03-19 stsp }
2362 50b0790e 2020-09-11 stsp if (remote == NULL) {
2363 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2364 50b0790e 2020-09-11 stsp goto done;
2365 b8adfa55 2020-09-25 stsp }
2366 b8adfa55 2020-09-25 stsp
2367 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2368 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2369 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2370 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_branches; i++) {
2371 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2372 6480c871 2021-08-30 stsp remote->fetch_branches[i], NULL);
2373 99495ddb 2021-01-10 stsp }
2374 99495ddb 2021-01-10 stsp }
2375 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2376 6480c871 2021-08-30 stsp for (i = 0; i < remote->nfetch_refs; i++) {
2377 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2378 6480c871 2021-08-30 stsp remote->fetch_refs[i], NULL);
2379 b8adfa55 2020-09-25 stsp }
2380 50b0790e 2020-09-11 stsp }
2381 7848a0e1 2020-03-19 stsp
2382 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2383 6480c871 2021-08-30 stsp &repo_name, remote->fetch_url);
2384 7848a0e1 2020-03-19 stsp if (error)
2385 7848a0e1 2020-03-19 stsp goto done;
2386 7848a0e1 2020-03-19 stsp
2387 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2388 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2389 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2390 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2391 7848a0e1 2020-03-19 stsp err(1, "pledge");
2392 7848a0e1 2020-03-19 stsp #endif
2393 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2394 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2395 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2396 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2397 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2398 7848a0e1 2020-03-19 stsp err(1, "pledge");
2399 7848a0e1 2020-03-19 stsp #endif
2400 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2401 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2402 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2403 7848a0e1 2020-03-19 stsp goto done;
2404 7848a0e1 2020-03-19 stsp } else {
2405 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2406 7848a0e1 2020-03-19 stsp goto done;
2407 7848a0e1 2020-03-19 stsp }
2408 7848a0e1 2020-03-19 stsp
2409 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
2410 d65a88a2 2021-09-05 stsp if (error)
2411 d65a88a2 2021-09-05 stsp goto done;
2412 d65a88a2 2021-09-05 stsp
2413 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2414 7848a0e1 2020-03-19 stsp if (error)
2415 7848a0e1 2020-03-19 stsp goto done;
2416 f79e6490 2020-04-19 stsp
2417 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2418 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2419 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2420 7848a0e1 2020-03-19 stsp
2421 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2422 9c52365f 2020-03-21 stsp server_path, verbosity);
2423 7848a0e1 2020-03-19 stsp if (error)
2424 7848a0e1 2020-03-19 stsp goto done;
2425 7848a0e1 2020-03-19 stsp
2426 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2427 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2428 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2429 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2430 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2431 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2432 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2433 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2434 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2435 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2436 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2437 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2438 7848a0e1 2020-03-19 stsp if (error)
2439 7848a0e1 2020-03-19 stsp goto done;
2440 7848a0e1 2020-03-19 stsp
2441 41b0de12 2020-03-21 stsp if (list_refs_only) {
2442 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2443 41b0de12 2020-03-21 stsp goto done;
2444 41b0de12 2020-03-21 stsp }
2445 41b0de12 2020-03-21 stsp
2446 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2447 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2448 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2449 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2450 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2451 984065c8 2020-03-19 stsp if (error)
2452 984065c8 2020-03-19 stsp goto done;
2453 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2454 984065c8 2020-03-19 stsp free(id_str);
2455 984065c8 2020-03-19 stsp id_str = NULL;
2456 984065c8 2020-03-19 stsp }
2457 7848a0e1 2020-03-19 stsp
2458 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2459 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2460 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2461 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2462 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2463 7848a0e1 2020-03-19 stsp char *remote_refname;
2464 7848a0e1 2020-03-19 stsp
2465 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2466 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2467 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2468 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2469 0e4002ca 2020-03-21 stsp if (error)
2470 0e4002ca 2020-03-21 stsp goto done;
2471 0e4002ca 2020-03-21 stsp continue;
2472 0e4002ca 2020-03-21 stsp }
2473 0e4002ca 2020-03-21 stsp
2474 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2475 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2476 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2477 7848a0e1 2020-03-19 stsp if (error) {
2478 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2479 7848a0e1 2020-03-19 stsp goto done;
2480 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2481 6338a6a1 2020-03-21 stsp repo);
2482 7848a0e1 2020-03-19 stsp if (error)
2483 7848a0e1 2020-03-19 stsp goto done;
2484 7848a0e1 2020-03-19 stsp } else {
2485 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2486 db6d8ad8 2020-03-21 stsp verbosity, repo);
2487 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2488 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2489 9f142382 2020-03-21 stsp error = unlock_err;
2490 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2491 7848a0e1 2020-03-19 stsp if (error)
2492 7848a0e1 2020-03-19 stsp goto done;
2493 7848a0e1 2020-03-19 stsp }
2494 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2495 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2496 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2497 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2498 7848a0e1 2020-03-19 stsp goto done;
2499 7848a0e1 2020-03-19 stsp }
2500 7848a0e1 2020-03-19 stsp
2501 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2502 7848a0e1 2020-03-19 stsp if (error) {
2503 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2504 7848a0e1 2020-03-19 stsp goto done;
2505 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2506 688f11b3 2020-03-21 stsp verbosity, repo);
2507 7848a0e1 2020-03-19 stsp if (error)
2508 7848a0e1 2020-03-19 stsp goto done;
2509 7848a0e1 2020-03-19 stsp } else {
2510 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2511 db6d8ad8 2020-03-21 stsp verbosity, repo);
2512 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2513 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2514 9f142382 2020-03-21 stsp error = unlock_err;
2515 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2516 7848a0e1 2020-03-19 stsp if (error)
2517 7848a0e1 2020-03-19 stsp goto done;
2518 7848a0e1 2020-03-19 stsp }
2519 2ec30c80 2020-03-20 stsp
2520 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2521 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2522 2ec30c80 2020-03-20 stsp if (error) {
2523 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2524 2ec30c80 2020-03-20 stsp goto done;
2525 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2526 6338a6a1 2020-03-21 stsp repo);
2527 2ec30c80 2020-03-20 stsp if (error)
2528 2ec30c80 2020-03-20 stsp goto done;
2529 9f142382 2020-03-21 stsp } else {
2530 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2531 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2532 9f142382 2020-03-21 stsp error = unlock_err;
2533 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2534 9f142382 2020-03-21 stsp }
2535 7848a0e1 2020-03-19 stsp }
2536 7848a0e1 2020-03-19 stsp }
2537 f1bcca34 2020-03-25 stsp if (delete_refs) {
2538 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2539 3789fd73 2020-03-26 stsp verbosity, repo);
2540 f1bcca34 2020-03-25 stsp if (error)
2541 f1bcca34 2020-03-25 stsp goto done;
2542 f1bcca34 2020-03-25 stsp }
2543 f1bcca34 2020-03-25 stsp
2544 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2545 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2546 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2547 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2548 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2549 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2550 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2551 f1bcca34 2020-03-25 stsp
2552 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2553 f1bcca34 2020-03-25 stsp continue;
2554 f1bcca34 2020-03-25 stsp
2555 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2556 f1bcca34 2020-03-25 stsp continue;
2557 f1bcca34 2020-03-25 stsp
2558 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2559 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2560 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2561 f1bcca34 2020-03-25 stsp goto done;
2562 f1bcca34 2020-03-25 stsp }
2563 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2564 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2565 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2566 f1bcca34 2020-03-25 stsp free(remote_refname);
2567 f1bcca34 2020-03-25 stsp goto done;
2568 f1bcca34 2020-03-25 stsp }
2569 f1bcca34 2020-03-25 stsp
2570 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2571 f1bcca34 2020-03-25 stsp 0);
2572 f1bcca34 2020-03-25 stsp if (error) {
2573 f1bcca34 2020-03-25 stsp free(remote_refname);
2574 f1bcca34 2020-03-25 stsp free(remote_target);
2575 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2576 f1bcca34 2020-03-25 stsp error = NULL;
2577 f1bcca34 2020-03-25 stsp continue;
2578 f1bcca34 2020-03-25 stsp }
2579 f1bcca34 2020-03-25 stsp goto done;
2580 f1bcca34 2020-03-25 stsp }
2581 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2582 f1bcca34 2020-03-25 stsp verbosity, repo);
2583 f1bcca34 2020-03-25 stsp free(remote_refname);
2584 f1bcca34 2020-03-25 stsp free(remote_target);
2585 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2586 f1bcca34 2020-03-25 stsp if (error)
2587 f1bcca34 2020-03-25 stsp goto done;
2588 f1bcca34 2020-03-25 stsp }
2589 f1bcca34 2020-03-25 stsp }
2590 7848a0e1 2020-03-19 stsp done:
2591 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2592 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2593 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2594 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2595 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2596 9c52365f 2020-03-21 stsp }
2597 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2598 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2599 1d0f4054 2021-06-17 stsp if (repo) {
2600 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
2601 1d0f4054 2021-06-17 stsp if (error == NULL)
2602 1d0f4054 2021-06-17 stsp error = close_err;
2603 1d0f4054 2021-06-17 stsp }
2604 7848a0e1 2020-03-19 stsp if (worktree)
2605 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2606 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2607 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2608 7848a0e1 2020-03-19 stsp free(pe->data);
2609 7848a0e1 2020-03-19 stsp }
2610 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2611 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, 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(&symrefs);
2616 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2617 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2618 7848a0e1 2020-03-19 stsp free(id_str);
2619 7848a0e1 2020-03-19 stsp free(cwd);
2620 7848a0e1 2020-03-19 stsp free(repo_path);
2621 7848a0e1 2020-03-19 stsp free(pack_hash);
2622 7848a0e1 2020-03-19 stsp free(proto);
2623 7848a0e1 2020-03-19 stsp free(host);
2624 7848a0e1 2020-03-19 stsp free(port);
2625 7848a0e1 2020-03-19 stsp free(server_path);
2626 7848a0e1 2020-03-19 stsp free(repo_name);
2627 7848a0e1 2020-03-19 stsp return error;
2628 7848a0e1 2020-03-19 stsp }
2629 7848a0e1 2020-03-19 stsp
2630 7848a0e1 2020-03-19 stsp
2631 7848a0e1 2020-03-19 stsp __dead static void
2632 2ab43947 2020-03-18 stsp usage_checkout(void)
2633 2ab43947 2020-03-18 stsp {
2634 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2635 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2636 2ab43947 2020-03-18 stsp exit(1);
2637 2ab43947 2020-03-18 stsp }
2638 2ab43947 2020-03-18 stsp
2639 2ab43947 2020-03-18 stsp static void
2640 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2641 2ab43947 2020-03-18 stsp {
2642 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2643 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2644 e6786710 2021-07-03 stsp "garbage-collected by Git or 'gotadmin cleanup'; making the "
2645 e6786710 2021-07-03 stsp "repository writable and running 'got update' will prevent this\n",
2646 2ab43947 2020-03-18 stsp getprogname());
2647 2ab43947 2020-03-18 stsp }
2648 2ab43947 2020-03-18 stsp
2649 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2650 2ab43947 2020-03-18 stsp const char *worktree_path;
2651 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2652 2ab43947 2020-03-18 stsp };
2653 2ab43947 2020-03-18 stsp
2654 2ab43947 2020-03-18 stsp static const struct got_error *
2655 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2656 2ab43947 2020-03-18 stsp {
2657 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2658 2ab43947 2020-03-18 stsp
2659 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2660 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2661 2ab43947 2020-03-18 stsp return NULL;
2662 2ab43947 2020-03-18 stsp
2663 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2664 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2665 2ab43947 2020-03-18 stsp return NULL;
2666 2ab43947 2020-03-18 stsp }
2667 2ab43947 2020-03-18 stsp
2668 2ab43947 2020-03-18 stsp while (path[0] == '/')
2669 2ab43947 2020-03-18 stsp path++;
2670 2ab43947 2020-03-18 stsp
2671 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2672 2ab43947 2020-03-18 stsp return NULL;
2673 2ab43947 2020-03-18 stsp }
2674 2ab43947 2020-03-18 stsp
2675 2ab43947 2020-03-18 stsp static const struct got_error *
2676 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2677 2ab43947 2020-03-18 stsp {
2678 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2679 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2680 2ab43947 2020-03-18 stsp return NULL;
2681 2ab43947 2020-03-18 stsp }
2682 2ab43947 2020-03-18 stsp
2683 2ab43947 2020-03-18 stsp static const struct got_error *
2684 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2685 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2686 2ab43947 2020-03-18 stsp struct got_repository *repo)
2687 2ab43947 2020-03-18 stsp {
2688 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2689 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2690 2ab43947 2020-03-18 stsp
2691 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2692 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2693 2ab43947 2020-03-18 stsp if (err)
2694 2ab43947 2020-03-18 stsp return err;
2695 2ab43947 2020-03-18 stsp
2696 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2697 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2698 2ab43947 2020-03-18 stsp
2699 2ab43947 2020-03-18 stsp /*
2700 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2701 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2702 2ab43947 2020-03-18 stsp *
2703 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2704 2ab43947 2020-03-18 stsp *
2705 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2706 2ab43947 2020-03-18 stsp * \ /
2707 2ab43947 2020-03-18 stsp * C E
2708 2ab43947 2020-03-18 stsp * \ /
2709 2ab43947 2020-03-18 stsp * B (yca)
2710 2ab43947 2020-03-18 stsp * |
2711 2ab43947 2020-03-18 stsp * A
2712 2ab43947 2020-03-18 stsp *
2713 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2714 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2715 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2716 2ab43947 2020-03-18 stsp */
2717 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2718 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2719 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2720 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2721 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2722 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2723 2ab43947 2020-03-18 stsp
2724 2ab43947 2020-03-18 stsp free(yca_id);
2725 2ab43947 2020-03-18 stsp return NULL;
2726 2ab43947 2020-03-18 stsp }
2727 2ab43947 2020-03-18 stsp
2728 2ab43947 2020-03-18 stsp static const struct got_error *
2729 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2730 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2731 2ab43947 2020-03-18 stsp struct got_repository *repo)
2732 2ab43947 2020-03-18 stsp {
2733 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2734 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2735 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2736 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2737 2ab43947 2020-03-18 stsp
2738 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2739 2ab43947 2020-03-18 stsp if (err)
2740 2ab43947 2020-03-18 stsp goto done;
2741 2ab43947 2020-03-18 stsp
2742 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2743 2ab43947 2020-03-18 stsp is_same_branch = 1;
2744 2ab43947 2020-03-18 stsp goto done;
2745 2ab43947 2020-03-18 stsp }
2746 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2747 2ab43947 2020-03-18 stsp is_same_branch = 1;
2748 2ab43947 2020-03-18 stsp goto done;
2749 2ab43947 2020-03-18 stsp }
2750 2ab43947 2020-03-18 stsp
2751 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2752 2ab43947 2020-03-18 stsp if (err)
2753 2ab43947 2020-03-18 stsp goto done;
2754 2ab43947 2020-03-18 stsp
2755 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2756 2ab43947 2020-03-18 stsp check_cancelled, NULL);
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 for (;;) {
2761 2ab43947 2020-03-18 stsp struct got_object_id *id;
2762 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2763 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2764 2ab43947 2020-03-18 stsp if (err) {
2765 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2766 2ab43947 2020-03-18 stsp err = NULL;
2767 2ab43947 2020-03-18 stsp break;
2768 2ab43947 2020-03-18 stsp }
2769 2ab43947 2020-03-18 stsp
2770 2ab43947 2020-03-18 stsp if (id) {
2771 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2772 2ab43947 2020-03-18 stsp break;
2773 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2774 2ab43947 2020-03-18 stsp is_same_branch = 1;
2775 2ab43947 2020-03-18 stsp break;
2776 2ab43947 2020-03-18 stsp }
2777 2ab43947 2020-03-18 stsp }
2778 2ab43947 2020-03-18 stsp }
2779 2ab43947 2020-03-18 stsp done:
2780 2ab43947 2020-03-18 stsp if (graph)
2781 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2782 2ab43947 2020-03-18 stsp free(head_commit_id);
2783 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2784 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2785 2ab43947 2020-03-18 stsp return err;
2786 a367ff0f 2019-05-14 stsp }
2787 8069f636 2019-01-12 stsp
2788 4ed7e80c 2018-05-20 stsp static const struct got_error *
2789 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2790 4b6c9460 2020-03-05 stsp {
2791 4b6c9460 2020-03-05 stsp static char msg[512];
2792 4b6c9460 2020-03-05 stsp const char *branch_name;
2793 4b6c9460 2020-03-05 stsp
2794 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2795 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2796 4b6c9460 2020-03-05 stsp else
2797 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2798 4b6c9460 2020-03-05 stsp
2799 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2800 4b6c9460 2020-03-05 stsp branch_name += 11;
2801 4b6c9460 2020-03-05 stsp
2802 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2803 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2804 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2805 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2806 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2807 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2808 4b6c9460 2020-03-05 stsp
2809 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2810 4b6c9460 2020-03-05 stsp }
2811 4b6c9460 2020-03-05 stsp
2812 4b6c9460 2020-03-05 stsp static const struct got_error *
2813 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2814 c09a553d 2018-03-12 stsp {
2815 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2816 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2817 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2818 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2819 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2820 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2821 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2822 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2823 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2824 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2825 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2826 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2827 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2828 f2ea84fa 2019-07-27 stsp
2829 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2830 c09a553d 2018-03-12 stsp
2831 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2832 0bb8a95e 2018-03-12 stsp switch (ch) {
2833 08573d5b 2019-05-14 stsp case 'b':
2834 08573d5b 2019-05-14 stsp branch_name = optarg;
2835 08573d5b 2019-05-14 stsp break;
2836 8069f636 2019-01-12 stsp case 'c':
2837 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2838 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2839 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2840 bb51a5b4 2020-01-13 stsp break;
2841 bb51a5b4 2020-01-13 stsp case 'E':
2842 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2843 8069f636 2019-01-12 stsp break;
2844 0bb8a95e 2018-03-12 stsp case 'p':
2845 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2846 0bb8a95e 2018-03-12 stsp break;
2847 0bb8a95e 2018-03-12 stsp default:
2848 2deda0b9 2019-03-07 stsp usage_checkout();
2849 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2850 0bb8a95e 2018-03-12 stsp }
2851 0bb8a95e 2018-03-12 stsp }
2852 0bb8a95e 2018-03-12 stsp
2853 0bb8a95e 2018-03-12 stsp argc -= optind;
2854 0bb8a95e 2018-03-12 stsp argv += optind;
2855 0bb8a95e 2018-03-12 stsp
2856 6715a751 2018-03-16 stsp #ifndef PROFILE
2857 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2858 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2859 c09a553d 2018-03-12 stsp err(1, "pledge");
2860 6715a751 2018-03-16 stsp #endif
2861 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2862 c47340da 2020-09-20 stsp char *base, *dotgit;
2863 f0207f6a 2020-10-19 stsp const char *path;
2864 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2865 76089277 2018-04-01 stsp if (repo_path == NULL)
2866 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2867 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2868 76089277 2018-04-01 stsp if (cwd == NULL) {
2869 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2870 76089277 2018-04-01 stsp goto done;
2871 76089277 2018-04-01 stsp }
2872 c47340da 2020-09-20 stsp if (path_prefix[0])
2873 f0207f6a 2020-10-19 stsp path = path_prefix;
2874 c47340da 2020-09-20 stsp else
2875 f0207f6a 2020-10-19 stsp path = repo_path;
2876 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2877 f0207f6a 2020-10-19 stsp if (error)
2878 c47340da 2020-09-20 stsp goto done;
2879 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2880 c09a553d 2018-03-12 stsp if (dotgit)
2881 c09a553d 2018-03-12 stsp *dotgit = '\0';
2882 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2883 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2884 f0207f6a 2020-10-19 stsp free(base);
2885 76089277 2018-04-01 stsp goto done;
2886 c09a553d 2018-03-12 stsp }
2887 f0207f6a 2020-10-19 stsp free(base);
2888 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2889 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2890 76089277 2018-04-01 stsp if (repo_path == NULL) {
2891 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2892 76089277 2018-04-01 stsp goto done;
2893 76089277 2018-04-01 stsp }
2894 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2895 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2896 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2897 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2898 b4b3a7dd 2019-07-22 stsp argv[1]);
2899 b4b3a7dd 2019-07-22 stsp goto done;
2900 b4b3a7dd 2019-07-22 stsp }
2901 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2902 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2903 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2904 b4b3a7dd 2019-07-22 stsp goto done;
2905 b4b3a7dd 2019-07-22 stsp }
2906 76089277 2018-04-01 stsp }
2907 c09a553d 2018-03-12 stsp } else
2908 c09a553d 2018-03-12 stsp usage_checkout();
2909 c09a553d 2018-03-12 stsp
2910 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2911 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2912 13bfb272 2019-05-10 jcs
2913 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2914 c09a553d 2018-03-12 stsp if (error != NULL)
2915 c02c541e 2019-03-29 stsp goto done;
2916 c02c541e 2019-03-29 stsp
2917 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2918 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2919 c530dc23 2019-07-23 stsp if (error) {
2920 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2921 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2922 c530dc23 2019-07-23 stsp goto done;
2923 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2924 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2925 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2926 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2927 c530dc23 2019-07-23 stsp goto done;
2928 c530dc23 2019-07-23 stsp }
2929 c530dc23 2019-07-23 stsp }
2930 c530dc23 2019-07-23 stsp
2931 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2932 c02c541e 2019-03-29 stsp if (error)
2933 c09a553d 2018-03-12 stsp goto done;
2934 8069f636 2019-01-12 stsp
2935 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2936 c09a553d 2018-03-12 stsp if (error != NULL)
2937 c09a553d 2018-03-12 stsp goto done;
2938 c09a553d 2018-03-12 stsp
2939 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2940 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2941 c09a553d 2018-03-12 stsp goto done;
2942 c09a553d 2018-03-12 stsp
2943 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2944 c09a553d 2018-03-12 stsp if (error != NULL)
2945 c09a553d 2018-03-12 stsp goto done;
2946 c09a553d 2018-03-12 stsp
2947 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2948 e5dc7198 2018-12-29 stsp path_prefix);
2949 e5dc7198 2018-12-29 stsp if (error != NULL)
2950 e5dc7198 2018-12-29 stsp goto done;
2951 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2952 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2953 49520a32 2018-12-29 stsp goto done;
2954 49520a32 2018-12-29 stsp }
2955 49520a32 2018-12-29 stsp
2956 8069f636 2019-01-12 stsp if (commit_id_str) {
2957 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2958 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2959 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2960 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2961 84de9106 2020-12-26 stsp NULL);
2962 84de9106 2020-12-26 stsp if (error)
2963 84de9106 2020-12-26 stsp goto done;
2964 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2965 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2966 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2967 30837e32 2019-07-25 stsp if (error)
2968 8069f636 2019-01-12 stsp goto done;
2969 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2970 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2971 8069f636 2019-01-12 stsp if (error != NULL) {
2972 8069f636 2019-01-12 stsp free(commit_id);
2973 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2974 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2975 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2976 4b6c9460 2020-03-05 stsp }
2977 8069f636 2019-01-12 stsp goto done;
2978 8069f636 2019-01-12 stsp }
2979 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2980 4b6c9460 2020-03-05 stsp if (error) {
2981 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2982 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2983 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2984 4b6c9460 2020-03-05 stsp }
2985 45d344f6 2019-05-14 stsp goto done;
2986 4b6c9460 2020-03-05 stsp }
2987 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2988 8069f636 2019-01-12 stsp commit_id);
2989 8069f636 2019-01-12 stsp free(commit_id);
2990 8069f636 2019-01-12 stsp if (error)
2991 8069f636 2019-01-12 stsp goto done;
2992 8069f636 2019-01-12 stsp }
2993 8069f636 2019-01-12 stsp
2994 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2995 f2ea84fa 2019-07-27 stsp if (error)
2996 f2ea84fa 2019-07-27 stsp goto done;
2997 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2998 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2999 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3000 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
3001 c09a553d 2018-03-12 stsp if (error != NULL)
3002 c09a553d 2018-03-12 stsp goto done;
3003 c09a553d 2018-03-12 stsp
3004 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
3005 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
3006 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
3007 c09a553d 2018-03-12 stsp done:
3008 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3009 8069f636 2019-01-12 stsp free(commit_id_str);
3010 76089277 2018-04-01 stsp free(repo_path);
3011 507dc3bb 2018-12-29 stsp free(worktree_path);
3012 c47340da 2020-09-20 stsp free(cwd);
3013 507dc3bb 2018-12-29 stsp return error;
3014 9627c110 2020-04-18 stsp }
3015 9627c110 2020-04-18 stsp
3016 9627c110 2020-04-18 stsp struct got_update_progress_arg {
3017 9627c110 2020-04-18 stsp int did_something;
3018 9627c110 2020-04-18 stsp int conflicts;
3019 9627c110 2020-04-18 stsp int obstructed;
3020 9627c110 2020-04-18 stsp int not_updated;
3021 9627c110 2020-04-18 stsp };
3022 9627c110 2020-04-18 stsp
3023 9627c110 2020-04-18 stsp void
3024 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
3025 9627c110 2020-04-18 stsp {
3026 9627c110 2020-04-18 stsp if (!upa->did_something)
3027 9627c110 2020-04-18 stsp return;
3028 9627c110 2020-04-18 stsp
3029 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
3030 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
3031 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
3032 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
3033 9627c110 2020-04-18 stsp upa->obstructed);
3034 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
3035 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
3036 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
3037 507dc3bb 2018-12-29 stsp }
3038 507dc3bb 2018-12-29 stsp
3039 507dc3bb 2018-12-29 stsp __dead static void
3040 507dc3bb 2018-12-29 stsp usage_update(void)
3041 507dc3bb 2018-12-29 stsp {
3042 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
3043 507dc3bb 2018-12-29 stsp getprogname());
3044 507dc3bb 2018-12-29 stsp exit(1);
3045 507dc3bb 2018-12-29 stsp }
3046 507dc3bb 2018-12-29 stsp
3047 1ee397ad 2019-07-12 stsp static const struct got_error *
3048 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
3049 507dc3bb 2018-12-29 stsp {
3050 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
3051 784955db 2019-01-12 stsp
3052 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
3053 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
3054 1ee397ad 2019-07-12 stsp return NULL;
3055 507dc3bb 2018-12-29 stsp
3056 9627c110 2020-04-18 stsp upa->did_something = 1;
3057 a484d721 2019-06-10 stsp
3058 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
3059 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
3060 1ee397ad 2019-07-12 stsp return NULL;
3061 a484d721 2019-06-10 stsp
3062 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
3063 9627c110 2020-04-18 stsp upa->conflicts++;
3064 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
3065 9627c110 2020-04-18 stsp upa->obstructed++;
3066 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
3067 9627c110 2020-04-18 stsp upa->not_updated++;
3068 9627c110 2020-04-18 stsp
3069 507dc3bb 2018-12-29 stsp while (path[0] == '/')
3070 507dc3bb 2018-12-29 stsp path++;
3071 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
3072 1ee397ad 2019-07-12 stsp return NULL;
3073 be7061eb 2018-12-30 stsp }
3074 be7061eb 2018-12-30 stsp
3075 be7061eb 2018-12-30 stsp static const struct got_error *
3076 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
3077 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
3078 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
3079 a1fb16d8 2019-05-24 stsp {
3080 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
3081 a1fb16d8 2019-05-24 stsp char *base_id_str;
3082 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3083 a1fb16d8 2019-05-24 stsp
3084 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3085 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3086 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3087 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3088 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3089 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3090 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3091 a1fb16d8 2019-05-24 stsp }
3092 a1fb16d8 2019-05-24 stsp
3093 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3094 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3095 a1fb16d8 2019-05-24 stsp if (err) {
3096 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3097 a1fb16d8 2019-05-24 stsp return err;
3098 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3099 a1fb16d8 2019-05-24 stsp }
3100 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3101 a1fb16d8 2019-05-24 stsp return NULL;
3102 a1fb16d8 2019-05-24 stsp
3103 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3104 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3105 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3106 a1fb16d8 2019-05-24 stsp if (err)
3107 a1fb16d8 2019-05-24 stsp return err;
3108 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3109 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3110 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3111 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3112 0ebf8283 2019-07-24 stsp return NULL;
3113 0ebf8283 2019-07-24 stsp }
3114 0ebf8283 2019-07-24 stsp
3115 0ebf8283 2019-07-24 stsp static const struct got_error *
3116 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3117 0ebf8283 2019-07-24 stsp {
3118 0ebf8283 2019-07-24 stsp const struct got_error *err;
3119 0ebf8283 2019-07-24 stsp int in_progress;
3120 0ebf8283 2019-07-24 stsp
3121 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3122 0ebf8283 2019-07-24 stsp if (err)
3123 0ebf8283 2019-07-24 stsp return err;
3124 0ebf8283 2019-07-24 stsp if (in_progress)
3125 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3126 0ebf8283 2019-07-24 stsp
3127 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3128 0ebf8283 2019-07-24 stsp if (err)
3129 0ebf8283 2019-07-24 stsp return err;
3130 0ebf8283 2019-07-24 stsp if (in_progress)
3131 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3132 0ebf8283 2019-07-24 stsp
3133 a1fb16d8 2019-05-24 stsp return NULL;
3134 a5edda0a 2019-07-27 stsp }
3135 a5edda0a 2019-07-27 stsp
3136 a5edda0a 2019-07-27 stsp static const struct got_error *
3137 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3138 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3139 a5edda0a 2019-07-27 stsp {
3140 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3141 a5edda0a 2019-07-27 stsp char *path;
3142 a5edda0a 2019-07-27 stsp int i;
3143 a5edda0a 2019-07-27 stsp
3144 a5edda0a 2019-07-27 stsp if (argc == 0) {
3145 a5edda0a 2019-07-27 stsp path = strdup("");
3146 a5edda0a 2019-07-27 stsp if (path == NULL)
3147 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3148 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3149 a5edda0a 2019-07-27 stsp }
3150 a5edda0a 2019-07-27 stsp
3151 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3152 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3153 a5edda0a 2019-07-27 stsp if (err)
3154 a5edda0a 2019-07-27 stsp break;
3155 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3156 a5edda0a 2019-07-27 stsp if (err) {
3157 a5edda0a 2019-07-27 stsp free(path);
3158 a5edda0a 2019-07-27 stsp break;
3159 a5edda0a 2019-07-27 stsp }
3160 a5edda0a 2019-07-27 stsp }
3161 a5edda0a 2019-07-27 stsp
3162 a5edda0a 2019-07-27 stsp return err;
3163 a1fb16d8 2019-05-24 stsp }
3164 a1fb16d8 2019-05-24 stsp
3165 a1fb16d8 2019-05-24 stsp static const struct got_error *
3166 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
3167 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
3168 fa51e947 2020-03-27 stsp {
3169 fa51e947 2020-03-27 stsp const struct got_error *err;
3170 fa51e947 2020-03-27 stsp struct got_repository *repo;
3171 fa51e947 2020-03-27 stsp static char msg[512];
3172 fa51e947 2020-03-27 stsp
3173 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
3174 fa51e947 2020-03-27 stsp if (err)
3175 fa51e947 2020-03-27 stsp return orig_err;
3176 fa51e947 2020-03-27 stsp
3177 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
3178 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
3179 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
3180 fa51e947 2020-03-27 stsp "'got checkout'.\n"
3181 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
3182 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3183 fa51e947 2020-03-27 stsp got_repo_close(repo);
3184 fa51e947 2020-03-27 stsp return err;
3185 fa51e947 2020-03-27 stsp }
3186 fa51e947 2020-03-27 stsp
3187 fa51e947 2020-03-27 stsp static const struct got_error *
3188 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
3189 507dc3bb 2018-12-29 stsp {
3190 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
3191 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
3192 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
3193 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
3194 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
3195 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
3196 024e9686 2019-05-14 stsp const char *branch_name = NULL;
3197 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
3198 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
3199 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
3200 9627c110 2020-04-18 stsp int ch;
3201 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
3202 507dc3bb 2018-12-29 stsp
3203 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
3204 f2ea84fa 2019-07-27 stsp
3205 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
3206 507dc3bb 2018-12-29 stsp switch (ch) {
3207 024e9686 2019-05-14 stsp case 'b':
3208 024e9686 2019-05-14 stsp branch_name = optarg;
3209 024e9686 2019-05-14 stsp break;
3210 507dc3bb 2018-12-29 stsp case 'c':
3211 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
3212 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
3213 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
3214 507dc3bb 2018-12-29 stsp break;
3215 507dc3bb 2018-12-29 stsp default:
3216 2deda0b9 2019-03-07 stsp usage_update();
3217 507dc3bb 2018-12-29 stsp /* NOTREACHED */
3218 507dc3bb 2018-12-29 stsp }
3219 507dc3bb 2018-12-29 stsp }
3220 507dc3bb 2018-12-29 stsp
3221 507dc3bb 2018-12-29 stsp argc -= optind;
3222 507dc3bb 2018-12-29 stsp argv += optind;
3223 507dc3bb 2018-12-29 stsp
3224 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3225 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3226 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3227 507dc3bb 2018-12-29 stsp err(1, "pledge");
3228 507dc3bb 2018-12-29 stsp #endif
3229 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3230 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3231 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3232 c4cdcb68 2019-04-03 stsp goto done;
3233 c4cdcb68 2019-04-03 stsp }
3234 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3235 fa51e947 2020-03-27 stsp if (error) {
3236 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3237 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3238 fa51e947 2020-03-27 stsp worktree_path);
3239 7d5807f4 2019-07-11 stsp goto done;
3240 fa51e947 2020-03-27 stsp }
3241 7d5807f4 2019-07-11 stsp
3242 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3243 f2ea84fa 2019-07-27 stsp if (error)
3244 f2ea84fa 2019-07-27 stsp goto done;
3245 507dc3bb 2018-12-29 stsp
3246 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3247 c9956ddf 2019-09-08 stsp NULL);
3248 507dc3bb 2018-12-29 stsp if (error != NULL)
3249 507dc3bb 2018-12-29 stsp goto done;
3250 507dc3bb 2018-12-29 stsp
3251 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3252 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3253 087fb88c 2019-08-04 stsp if (error)
3254 087fb88c 2019-08-04 stsp goto done;
3255 087fb88c 2019-08-04 stsp
3256 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3257 0266afb7 2019-01-04 stsp if (error)
3258 0266afb7 2019-01-04 stsp goto done;
3259 0266afb7 2019-01-04 stsp
3260 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3261 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3262 024e9686 2019-05-14 stsp if (error != NULL)
3263 024e9686 2019-05-14 stsp goto done;
3264 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3265 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3266 9c4b8182 2019-01-02 stsp if (error != NULL)
3267 9c4b8182 2019-01-02 stsp goto done;
3268 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3269 507dc3bb 2018-12-29 stsp if (error != NULL)
3270 507dc3bb 2018-12-29 stsp goto done;
3271 507dc3bb 2018-12-29 stsp } else {
3272 84de9106 2020-12-26 stsp struct got_reflist_head refs;
3273 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3274 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3275 84de9106 2020-12-26 stsp NULL);
3276 84de9106 2020-12-26 stsp if (error)
3277 84de9106 2020-12-26 stsp goto done;
3278 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3279 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3280 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
3281 04f57cb3 2019-07-25 stsp free(commit_id_str);
3282 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3283 30837e32 2019-07-25 stsp if (error)
3284 a297e751 2019-07-11 stsp goto done;
3285 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3286 a297e751 2019-07-11 stsp if (error)
3287 507dc3bb 2018-12-29 stsp goto done;
3288 507dc3bb 2018-12-29 stsp }
3289 35c965b2 2018-12-29 stsp
3290 a1fb16d8 2019-05-24 stsp if (branch_name) {
3291 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3292 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3293 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3294 f2ea84fa 2019-07-27 stsp continue;
3295 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3296 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3297 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3298 024e9686 2019-05-14 stsp goto done;
3299 024e9686 2019-05-14 stsp }
3300 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3301 024e9686 2019-05-14 stsp if (error)
3302 024e9686 2019-05-14 stsp goto done;
3303 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3304 3aef623b 2019-10-15 stsp repo);
3305 a367ff0f 2019-05-14 stsp free(head_commit_id);
3306 024e9686 2019-05-14 stsp if (error != NULL)
3307 024e9686 2019-05-14 stsp goto done;
3308 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3309 a367ff0f 2019-05-14 stsp if (error)
3310 a367ff0f 2019-05-14 stsp goto done;
3311 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3312 024e9686 2019-05-14 stsp if (error)
3313 024e9686 2019-05-14 stsp goto done;
3314 024e9686 2019-05-14 stsp } else {
3315 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3316 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3317 a367ff0f 2019-05-14 stsp if (error != NULL) {
3318 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3319 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3320 024e9686 2019-05-14 stsp goto done;
3321 a367ff0f 2019-05-14 stsp }
3322 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3323 a367ff0f 2019-05-14 stsp if (error)
3324 a367ff0f 2019-05-14 stsp goto done;
3325 024e9686 2019-05-14 stsp }
3326 507dc3bb 2018-12-29 stsp
3327 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3328 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3329 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3330 507dc3bb 2018-12-29 stsp commit_id);
3331 507dc3bb 2018-12-29 stsp if (error)
3332 507dc3bb 2018-12-29 stsp goto done;
3333 507dc3bb 2018-12-29 stsp }
3334 507dc3bb 2018-12-29 stsp
3335 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3336 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3337 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3338 507dc3bb 2018-12-29 stsp if (error != NULL)
3339 507dc3bb 2018-12-29 stsp goto done;
3340 9c4b8182 2019-01-02 stsp
3341 9627c110 2020-04-18 stsp if (upa.did_something)
3342 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3343 784955db 2019-01-12 stsp else
3344 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3345 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3346 507dc3bb 2018-12-29 stsp done:
3347 c09a553d 2018-03-12 stsp free(worktree_path);
3348 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3349 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3350 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3351 507dc3bb 2018-12-29 stsp free(commit_id);
3352 9c4b8182 2019-01-02 stsp free(commit_id_str);
3353 c09a553d 2018-03-12 stsp return error;
3354 c09a553d 2018-03-12 stsp }
3355 c09a553d 2018-03-12 stsp
3356 f42b1b34 2018-03-12 stsp static const struct got_error *
3357 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3358 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3359 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3360 5c860e29 2018-03-12 stsp {
3361 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3362 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3363 79109fed 2018-03-27 stsp
3364 44392932 2019-08-25 stsp if (blob_id1) {
3365 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3366 44392932 2019-08-25 stsp if (err)
3367 44392932 2019-08-25 stsp goto done;
3368 44392932 2019-08-25 stsp }
3369 79109fed 2018-03-27 stsp
3370 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3371 44392932 2019-08-25 stsp if (err)
3372 44392932 2019-08-25 stsp goto done;
3373 79109fed 2018-03-27 stsp
3374 44392932 2019-08-25 stsp while (path[0] == '/')
3375 44392932 2019-08-25 stsp path++;
3376 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3377 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, stdout);
3378 44392932 2019-08-25 stsp done:
3379 44392932 2019-08-25 stsp if (blob1)
3380 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3381 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3382 44392932 2019-08-25 stsp return err;
3383 44392932 2019-08-25 stsp }
3384 44392932 2019-08-25 stsp
3385 44392932 2019-08-25 stsp static const struct got_error *
3386 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3387 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3388 64453f7e 2020-11-21 stsp int force_text_diff, struct got_repository *repo)
3389 44392932 2019-08-25 stsp {
3390 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3391 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3392 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3393 44392932 2019-08-25 stsp
3394 44392932 2019-08-25 stsp if (tree_id1) {
3395 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3396 79109fed 2018-03-27 stsp if (err)
3397 44392932 2019-08-25 stsp goto done;
3398 79109fed 2018-03-27 stsp }
3399 79109fed 2018-03-27 stsp
3400 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3401 0f2b3dca 2018-12-22 stsp if (err)
3402 0f2b3dca 2018-12-22 stsp goto done;
3403 0f2b3dca 2018-12-22 stsp
3404 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3405 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3406 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
3407 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3408 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3409 fe621944 2020-11-10 stsp arg.nlines = 0;
3410 44392932 2019-08-25 stsp while (path[0] == '/')
3411 44392932 2019-08-25 stsp path++;
3412 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3413 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3414 0f2b3dca 2018-12-22 stsp done:
3415 79109fed 2018-03-27 stsp if (tree1)
3416 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3417 366e0a5f 2019-10-10 stsp if (tree2)
3418 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3419 44392932 2019-08-25 stsp return err;
3420 44392932 2019-08-25 stsp }
3421 44392932 2019-08-25 stsp
3422 44392932 2019-08-25 stsp static const struct got_error *
3423 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3424 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3425 0208f208 2020-05-05 stsp {
3426 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3427 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3428 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3429 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3430 0208f208 2020-05-05 stsp
3431 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3432 0208f208 2020-05-05 stsp if (qid != NULL) {
3433 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3434 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3435 0208f208 2020-05-05 stsp qid->id);
3436 0208f208 2020-05-05 stsp if (err)
3437 0208f208 2020-05-05 stsp return err;
3438 0208f208 2020-05-05 stsp
3439 aa8b5dd0 2021-08-01 stsp tree_id1 = got_object_id_dup(
3440 aa8b5dd0 2021-08-01 stsp got_object_commit_get_tree_id(pcommit));
3441 aa8b5dd0 2021-08-01 stsp if (tree_id1 == NULL) {
3442 aa8b5dd0 2021-08-01 stsp got_object_commit_close(pcommit);
3443 aa8b5dd0 2021-08-01 stsp return got_error_from_errno("got_object_id_dup");
3444 aa8b5dd0 2021-08-01 stsp }
3445 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3446 0208f208 2020-05-05 stsp
3447 0208f208 2020-05-05 stsp }
3448 0208f208 2020-05-05 stsp
3449 0208f208 2020-05-05 stsp if (tree_id1) {
3450 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3451 0208f208 2020-05-05 stsp if (err)
3452 0208f208 2020-05-05 stsp goto done;
3453 0208f208 2020-05-05 stsp }
3454 0208f208 2020-05-05 stsp
3455 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3456 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3457 0208f208 2020-05-05 stsp if (err)
3458 0208f208 2020-05-05 stsp goto done;
3459 0208f208 2020-05-05 stsp
3460 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3461 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3462 0208f208 2020-05-05 stsp done:
3463 0208f208 2020-05-05 stsp if (tree1)
3464 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3465 0208f208 2020-05-05 stsp if (tree2)
3466 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3467 aa8b5dd0 2021-08-01 stsp free(tree_id1);
3468 0208f208 2020-05-05 stsp return err;
3469 0208f208 2020-05-05 stsp }
3470 0208f208 2020-05-05 stsp
3471 0208f208 2020-05-05 stsp static const struct got_error *
3472 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3473 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3474 44392932 2019-08-25 stsp {
3475 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3476 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3477 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3478 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3479 44392932 2019-08-25 stsp struct got_object_qid *qid;
3480 44392932 2019-08-25 stsp
3481 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3482 44392932 2019-08-25 stsp if (qid != NULL) {
3483 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3484 44392932 2019-08-25 stsp qid->id);
3485 44392932 2019-08-25 stsp if (err)
3486 44392932 2019-08-25 stsp return err;
3487 44392932 2019-08-25 stsp }
3488 44392932 2019-08-25 stsp
3489 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3490 44392932 2019-08-25 stsp int obj_type;
3491 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3492 44392932 2019-08-25 stsp if (err)
3493 44392932 2019-08-25 stsp goto done;
3494 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3495 44392932 2019-08-25 stsp if (err) {
3496 44392932 2019-08-25 stsp free(obj_id2);
3497 44392932 2019-08-25 stsp goto done;
3498 44392932 2019-08-25 stsp }
3499 44392932 2019-08-25 stsp if (pcommit) {
3500 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3501 44392932 2019-08-25 stsp qid->id, path);
3502 44392932 2019-08-25 stsp if (err) {
3503 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3504 2e8c69d1 2020-05-04 stsp free(obj_id2);
3505 2e8c69d1 2020-05-04 stsp goto done;
3506 2e8c69d1 2020-05-04 stsp }
3507 2e8c69d1 2020-05-04 stsp } else {
3508 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3509 2e8c69d1 2020-05-04 stsp if (err) {
3510 2e8c69d1 2020-05-04 stsp free(obj_id2);
3511 2e8c69d1 2020-05-04 stsp goto done;
3512 2e8c69d1 2020-05-04 stsp }
3513 44392932 2019-08-25 stsp }
3514 44392932 2019-08-25 stsp }
3515 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3516 44392932 2019-08-25 stsp if (err) {
3517 44392932 2019-08-25 stsp free(obj_id2);
3518 44392932 2019-08-25 stsp goto done;
3519 44392932 2019-08-25 stsp }
3520 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3521 44392932 2019-08-25 stsp switch (obj_type) {
3522 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3523 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3524 64453f7e 2020-11-21 stsp 0, 0, repo);
3525 44392932 2019-08-25 stsp break;
3526 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3527 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3528 64453f7e 2020-11-21 stsp 0, 0, repo);
3529 44392932 2019-08-25 stsp break;
3530 44392932 2019-08-25 stsp default:
3531 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3532 44392932 2019-08-25 stsp break;
3533 44392932 2019-08-25 stsp }
3534 44392932 2019-08-25 stsp free(obj_id1);
3535 44392932 2019-08-25 stsp free(obj_id2);
3536 44392932 2019-08-25 stsp } else {
3537 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3538 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3539 44392932 2019-08-25 stsp if (err)
3540 44392932 2019-08-25 stsp goto done;
3541 579bd556 2020-10-24 stsp if (pcommit) {
3542 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3543 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3544 579bd556 2020-10-24 stsp if (err)
3545 579bd556 2020-10-24 stsp goto done;
3546 579bd556 2020-10-24 stsp }
3547 64453f7e 2020-11-21 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
3548 64453f7e 2020-11-21 stsp id_str2);
3549 64453f7e 2020-11-21 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3550 64453f7e 2020-11-21 stsp repo);
3551 44392932 2019-08-25 stsp }
3552 44392932 2019-08-25 stsp done:
3553 0f2b3dca 2018-12-22 stsp free(id_str1);
3554 0f2b3dca 2018-12-22 stsp free(id_str2);
3555 44392932 2019-08-25 stsp if (pcommit)
3556 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3557 79109fed 2018-03-27 stsp return err;
3558 79109fed 2018-03-27 stsp }
3559 79109fed 2018-03-27 stsp
3560 4bb494d5 2018-06-16 stsp static char *
3561 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3562 6c281f94 2018-06-11 stsp {
3563 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3564 09867e48 2019-08-13 stsp char *p, *s;
3565 09867e48 2019-08-13 stsp
3566 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3567 09867e48 2019-08-13 stsp if (tm == NULL)
3568 09867e48 2019-08-13 stsp return NULL;
3569 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3570 09867e48 2019-08-13 stsp if (s == NULL)
3571 09867e48 2019-08-13 stsp return NULL;
3572 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3573 6c281f94 2018-06-11 stsp if (p)
3574 6c281f94 2018-06-11 stsp *p = '\0';
3575 6c281f94 2018-06-11 stsp return s;
3576 6c281f94 2018-06-11 stsp }
3577 dc424a06 2019-08-07 stsp
3578 6841bf13 2019-11-29 kn static const struct got_error *
3579 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3580 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3581 6841bf13 2019-11-29 kn {
3582 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3583 6841bf13 2019-11-29 kn regmatch_t regmatch;
3584 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3585 6841bf13 2019-11-29 kn
3586 6841bf13 2019-11-29 kn *have_match = 0;
3587 6841bf13 2019-11-29 kn
3588 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3589 6841bf13 2019-11-29 kn if (err)
3590 6841bf13 2019-11-29 kn return err;
3591 6841bf13 2019-11-29 kn
3592 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3593 6841bf13 2019-11-29 kn if (err)
3594 6841bf13 2019-11-29 kn goto done;
3595 6841bf13 2019-11-29 kn
3596 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3597 6841bf13 2019-11-29 kn *have_match = 1;
3598 6841bf13 2019-11-29 kn done:
3599 6841bf13 2019-11-29 kn free(id_str);
3600 6841bf13 2019-11-29 kn free(logmsg);
3601 6841bf13 2019-11-29 kn return err;
3602 6841bf13 2019-11-29 kn }
3603 6841bf13 2019-11-29 kn
3604 0208f208 2020-05-05 stsp static void
3605 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3606 0208f208 2020-05-05 stsp regex_t *regex)
3607 0208f208 2020-05-05 stsp {
3608 0208f208 2020-05-05 stsp regmatch_t regmatch;
3609 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3610 0208f208 2020-05-05 stsp
3611 0208f208 2020-05-05 stsp *have_match = 0;
3612 0208f208 2020-05-05 stsp
3613 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3614 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3615 0208f208 2020-05-05 stsp *have_match = 1;
3616 0208f208 2020-05-05 stsp break;
3617 0208f208 2020-05-05 stsp }
3618 0208f208 2020-05-05 stsp }
3619 0208f208 2020-05-05 stsp }
3620 0208f208 2020-05-05 stsp
3621 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3622 6c281f94 2018-06-11 stsp
3623 888b7d99 2020-12-26 stsp static const struct got_error*
3624 888b7d99 2020-12-26 stsp build_refs_str(char **refs_str, struct got_reflist_head *refs,
3625 888b7d99 2020-12-26 stsp struct got_object_id *id, struct got_repository *repo)
3626 888b7d99 2020-12-26 stsp {
3627 888b7d99 2020-12-26 stsp static const struct got_error *err = NULL;
3628 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3629 888b7d99 2020-12-26 stsp char *s;
3630 888b7d99 2020-12-26 stsp const char *name;
3631 5c860e29 2018-03-12 stsp
3632 888b7d99 2020-12-26 stsp *refs_str = NULL;
3633 888b7d99 2020-12-26 stsp
3634 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, refs, entry) {
3635 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3636 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3637 a436ad14 2019-08-13 stsp int cmp;
3638 a436ad14 2019-08-13 stsp
3639 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3640 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3641 d9498b20 2019-02-05 stsp continue;
3642 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3643 199a4027 2019-02-02 stsp name += 5;
3644 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3645 7143d404 2019-03-12 stsp continue;
3646 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3647 e34f9ed6 2019-02-02 stsp name += 6;
3648 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3649 141c2bff 2019-02-04 stsp name += 8;
3650 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3651 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3652 4343a07f 2020-04-24 stsp continue;
3653 4343a07f 2020-04-24 stsp }
3654 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3655 48cae60d 2020-09-22 stsp if (err)
3656 888b7d99 2020-12-26 stsp break;
3657 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3658 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3659 5d844a1e 2019-08-13 stsp if (err) {
3660 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3661 48cae60d 2020-09-22 stsp free(ref_id);
3662 888b7d99 2020-12-26 stsp break;
3663 48cae60d 2020-09-22 stsp }
3664 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3665 5d844a1e 2019-08-13 stsp err = NULL;
3666 5d844a1e 2019-08-13 stsp tag = NULL;
3667 5d844a1e 2019-08-13 stsp }
3668 a436ad14 2019-08-13 stsp }
3669 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3670 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3671 48cae60d 2020-09-22 stsp free(ref_id);
3672 a436ad14 2019-08-13 stsp if (tag)
3673 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3674 a436ad14 2019-08-13 stsp if (cmp != 0)
3675 a436ad14 2019-08-13 stsp continue;
3676 888b7d99 2020-12-26 stsp s = *refs_str;
3677 888b7d99 2020-12-26 stsp if (asprintf(refs_str, "%s%s%s", s ? s : "",
3678 888b7d99 2020-12-26 stsp s ? ", " : "", name) == -1) {
3679 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3680 199a4027 2019-02-02 stsp free(s);
3681 888b7d99 2020-12-26 stsp *refs_str = NULL;
3682 888b7d99 2020-12-26 stsp break;
3683 199a4027 2019-02-02 stsp }
3684 199a4027 2019-02-02 stsp free(s);
3685 199a4027 2019-02-02 stsp }
3686 888b7d99 2020-12-26 stsp
3687 888b7d99 2020-12-26 stsp return err;
3688 888b7d99 2020-12-26 stsp }
3689 888b7d99 2020-12-26 stsp
3690 888b7d99 2020-12-26 stsp static const struct got_error *
3691 888b7d99 2020-12-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3692 888b7d99 2020-12-26 stsp struct got_repository *repo, const char *path,
3693 888b7d99 2020-12-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3694 e600f124 2021-03-21 stsp int diff_context, struct got_reflist_object_id_map *refs_idmap,
3695 e600f124 2021-03-21 stsp const char *custom_refs_str)
3696 888b7d99 2020-12-26 stsp {
3697 888b7d99 2020-12-26 stsp const struct got_error *err = NULL;
3698 888b7d99 2020-12-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3699 888b7d99 2020-12-26 stsp char datebuf[26];
3700 888b7d99 2020-12-26 stsp time_t committer_time;
3701 888b7d99 2020-12-26 stsp const char *author, *committer;
3702 888b7d99 2020-12-26 stsp char *refs_str = NULL;
3703 888b7d99 2020-12-26 stsp
3704 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3705 8bf5b3c9 2018-03-17 stsp if (err)
3706 8bf5b3c9 2018-03-17 stsp return err;
3707 788c352e 2018-06-16 stsp
3708 e600f124 2021-03-21 stsp if (custom_refs_str == NULL) {
3709 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
3710 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3711 e600f124 2021-03-21 stsp if (refs) {
3712 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, id, repo);
3713 e600f124 2021-03-21 stsp if (err)
3714 e600f124 2021-03-21 stsp goto done;
3715 e600f124 2021-03-21 stsp }
3716 888b7d99 2020-12-26 stsp }
3717 888b7d99 2020-12-26 stsp
3718 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3719 e600f124 2021-03-21 stsp if (custom_refs_str)
3720 e600f124 2021-03-21 stsp printf("commit %s (%s)\n", id_str, custom_refs_str);
3721 e600f124 2021-03-21 stsp else
3722 e600f124 2021-03-21 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3723 e600f124 2021-03-21 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3724 832c249c 2018-06-10 stsp free(id_str);
3725 d3d493d7 2019-02-21 stsp id_str = NULL;
3726 d3d493d7 2019-02-21 stsp free(refs_str);
3727 d3d493d7 2019-02-21 stsp refs_str = NULL;
3728 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3729 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3730 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3731 09867e48 2019-08-13 stsp if (datestr)
3732 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3733 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3734 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3735 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3736 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3737 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3738 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3739 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3740 3fe1abad 2018-06-10 stsp int n = 1;
3741 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3742 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, parent_ids, entry) {
3743 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3744 a0603db2 2018-06-10 stsp if (err)
3745 888b7d99 2020-12-26 stsp goto done;
3746 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3747 a0603db2 2018-06-10 stsp free(id_str);
3748 888b7d99 2020-12-26 stsp id_str = NULL;
3749 a0603db2 2018-06-10 stsp }
3750 a0603db2 2018-06-10 stsp }
3751 832c249c 2018-06-10 stsp
3752 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3753 5943eee2 2019-08-13 stsp if (err)
3754 888b7d99 2020-12-26 stsp goto done;
3755 8bf5b3c9 2018-03-17 stsp
3756 621015ac 2018-07-23 stsp logmsg = logmsg0;
3757 832c249c 2018-06-10 stsp do {
3758 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3759 832c249c 2018-06-10 stsp if (line)
3760 832c249c 2018-06-10 stsp printf(" %s\n", line);
3761 832c249c 2018-06-10 stsp } while (line);
3762 621015ac 2018-07-23 stsp free(logmsg0);
3763 832c249c 2018-06-10 stsp
3764 0208f208 2020-05-05 stsp if (changed_paths) {
3765 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3766 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3767 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3768 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3769 0208f208 2020-05-05 stsp }
3770 0208f208 2020-05-05 stsp printf("\n");
3771 0208f208 2020-05-05 stsp }
3772 971751ac 2018-03-27 stsp if (show_patch) {
3773 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3774 971751ac 2018-03-27 stsp if (err == 0)
3775 971751ac 2018-03-27 stsp printf("\n");
3776 971751ac 2018-03-27 stsp }
3777 07862c20 2018-09-15 stsp
3778 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3779 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3780 888b7d99 2020-12-26 stsp done:
3781 888b7d99 2020-12-26 stsp free(id_str);
3782 888b7d99 2020-12-26 stsp free(refs_str);
3783 07862c20 2018-09-15 stsp return err;
3784 f42b1b34 2018-03-12 stsp }
3785 5c860e29 2018-03-12 stsp
3786 f42b1b34 2018-03-12 stsp static const struct got_error *
3787 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3788 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3789 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3790 888b7d99 2020-12-26 stsp int log_branches, int reverse_display_order,
3791 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap)
3792 f42b1b34 2018-03-12 stsp {
3793 f42b1b34 2018-03-12 stsp const struct got_error *err;
3794 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3795 6841bf13 2019-11-29 kn regex_t regex;
3796 6841bf13 2019-11-29 kn int have_match;
3797 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3798 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3799 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3800 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3801 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3802 dbec59df 2020-04-18 stsp
3803 dbdddfee 2021-06-23 naddy STAILQ_INIT(&reversed_commits);
3804 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3805 372ccdbb 2018-06-10 stsp
3806 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3807 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3808 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3809 6841bf13 2019-11-29 kn
3810 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3811 f42b1b34 2018-03-12 stsp if (err)
3812 f42b1b34 2018-03-12 stsp return err;
3813 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3814 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3815 372ccdbb 2018-06-10 stsp if (err)
3816 fcc85cad 2018-07-22 stsp goto done;
3817 656b1f76 2019-05-11 jcs for (;;) {
3818 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3819 8bf5b3c9 2018-03-17 stsp
3820 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3821 84453469 2018-11-11 stsp break;
3822 84453469 2018-11-11 stsp
3823 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3824 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3825 372ccdbb 2018-06-10 stsp if (err) {
3826 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3827 9ba79e04 2018-06-11 stsp err = NULL;
3828 ee780d5c 2020-01-04 stsp break;
3829 7e665116 2018-04-02 stsp }
3830 b43fbaa0 2018-06-11 stsp if (id == NULL)
3831 7e665116 2018-04-02 stsp break;
3832 b43fbaa0 2018-06-11 stsp
3833 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3834 b43fbaa0 2018-06-11 stsp if (err)
3835 fcc85cad 2018-07-22 stsp break;
3836 6841bf13 2019-11-29 kn
3837 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3838 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3839 0208f208 2020-05-05 stsp if (err)
3840 0208f208 2020-05-05 stsp break;
3841 0208f208 2020-05-05 stsp }
3842 0208f208 2020-05-05 stsp
3843 6841bf13 2019-11-29 kn if (search_pattern) {
3844 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3845 6841bf13 2019-11-29 kn if (err) {
3846 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3847 6841bf13 2019-11-29 kn break;
3848 6841bf13 2019-11-29 kn }
3849 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3850 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3851 0208f208 2020-05-05 stsp &changed_paths, &regex);
3852 6841bf13 2019-11-29 kn if (have_match == 0) {
3853 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3854 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3855 0208f208 2020-05-05 stsp free((char *)pe->path);
3856 0208f208 2020-05-05 stsp free(pe->data);
3857 0208f208 2020-05-05 stsp }
3858 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3859 6841bf13 2019-11-29 kn continue;
3860 6841bf13 2019-11-29 kn }
3861 6841bf13 2019-11-29 kn }
3862 6841bf13 2019-11-29 kn
3863 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3864 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3865 dbec59df 2020-04-18 stsp if (err)
3866 dbec59df 2020-04-18 stsp break;
3867 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
3868 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3869 dbec59df 2020-04-18 stsp } else {
3870 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3871 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3872 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3873 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3874 dbec59df 2020-04-18 stsp if (err)
3875 dbec59df 2020-04-18 stsp break;
3876 dbec59df 2020-04-18 stsp }
3877 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3878 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3879 7e665116 2018-04-02 stsp break;
3880 0208f208 2020-05-05 stsp
3881 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3882 0208f208 2020-05-05 stsp free((char *)pe->path);
3883 0208f208 2020-05-05 stsp free(pe->data);
3884 0208f208 2020-05-05 stsp }
3885 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3886 31cedeaf 2018-09-15 stsp }
3887 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3888 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &reversed_commits, entry) {
3889 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3890 dbec59df 2020-04-18 stsp if (err)
3891 dbec59df 2020-04-18 stsp break;
3892 502b9684 2020-07-31 stsp if (show_changed_paths) {
3893 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3894 502b9684 2020-07-31 stsp commit, repo);
3895 502b9684 2020-07-31 stsp if (err)
3896 502b9684 2020-07-31 stsp break;
3897 502b9684 2020-07-31 stsp }
3898 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3899 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3900 e600f124 2021-03-21 stsp show_patch, diff_context, refs_idmap, NULL);
3901 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3902 dbec59df 2020-04-18 stsp if (err)
3903 dbec59df 2020-04-18 stsp break;
3904 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3905 502b9684 2020-07-31 stsp free((char *)pe->path);
3906 502b9684 2020-07-31 stsp free(pe->data);
3907 502b9684 2020-07-31 stsp }
3908 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3909 dbec59df 2020-04-18 stsp }
3910 dbec59df 2020-04-18 stsp }
3911 fcc85cad 2018-07-22 stsp done:
3912 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&reversed_commits)) {
3913 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&reversed_commits);
3914 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&reversed_commits, entry);
3915 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3916 dbec59df 2020-04-18 stsp }
3917 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3918 0208f208 2020-05-05 stsp free((char *)pe->path);
3919 0208f208 2020-05-05 stsp free(pe->data);
3920 0208f208 2020-05-05 stsp }
3921 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3922 6841bf13 2019-11-29 kn if (search_pattern)
3923 6841bf13 2019-11-29 kn regfree(&regex);
3924 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3925 f42b1b34 2018-03-12 stsp return err;
3926 f42b1b34 2018-03-12 stsp }
3927 5c860e29 2018-03-12 stsp
3928 4ed7e80c 2018-05-20 stsp __dead static void
3929 6f3d1eb0 2018-03-12 stsp usage_log(void)
3930 6f3d1eb0 2018-03-12 stsp {
3931 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3932 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3933 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3934 6f3d1eb0 2018-03-12 stsp exit(1);
3935 b1ebc001 2019-08-13 stsp }
3936 b1ebc001 2019-08-13 stsp
3937 b1ebc001 2019-08-13 stsp static int
3938 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3939 b1ebc001 2019-08-13 stsp {
3940 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3941 b1ebc001 2019-08-13 stsp long long n;
3942 b1ebc001 2019-08-13 stsp const char *errstr;
3943 b1ebc001 2019-08-13 stsp
3944 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3945 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3946 b1ebc001 2019-08-13 stsp return 0;
3947 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3948 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3949 b1ebc001 2019-08-13 stsp return 0;
3950 b1ebc001 2019-08-13 stsp return n;
3951 6f3d1eb0 2018-03-12 stsp }
3952 6f3d1eb0 2018-03-12 stsp
3953 4ed7e80c 2018-05-20 stsp static const struct got_error *
3954 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3955 f42b1b34 2018-03-12 stsp {
3956 f42b1b34 2018-03-12 stsp const struct got_error *error;
3957 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3958 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3959 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3960 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3961 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3962 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3963 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3964 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3965 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3966 64a96a6d 2018-04-01 stsp const char *errstr;
3967 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3968 888b7d99 2020-12-26 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
3969 1b3893a2 2019-03-18 stsp
3970 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
3971 5c860e29 2018-03-12 stsp
3972 6715a751 2018-03-16 stsp #ifndef PROFILE
3973 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3974 6098196c 2019-01-04 stsp NULL)
3975 ad242220 2018-09-08 stsp == -1)
3976 f42b1b34 2018-03-12 stsp err(1, "pledge");
3977 6715a751 2018-03-16 stsp #endif
3978 79109fed 2018-03-27 stsp
3979 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3980 b1ebc001 2019-08-13 stsp
3981 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3982 79109fed 2018-03-27 stsp switch (ch) {
3983 79109fed 2018-03-27 stsp case 'p':
3984 79109fed 2018-03-27 stsp show_patch = 1;
3985 d142fc45 2018-04-01 stsp break;
3986 0208f208 2020-05-05 stsp case 'P':
3987 0208f208 2020-05-05 stsp show_changed_paths = 1;
3988 0208f208 2020-05-05 stsp break;
3989 d142fc45 2018-04-01 stsp case 'c':
3990 d142fc45 2018-04-01 stsp start_commit = optarg;
3991 64a96a6d 2018-04-01 stsp break;
3992 c0cc5c62 2018-10-18 stsp case 'C':
3993 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3994 4a8520aa 2018-10-18 stsp &errstr);
3995 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3996 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3997 c0cc5c62 2018-10-18 stsp break;
3998 64a96a6d 2018-04-01 stsp case 'l':
3999 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
4000 64a96a6d 2018-04-01 stsp if (errstr != NULL)
4001 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
4002 cc54c501 2019-07-15 stsp break;
4003 48c8c60d 2020-01-27 stsp case 'b':
4004 48c8c60d 2020-01-27 stsp log_branches = 1;
4005 a0603db2 2018-06-10 stsp break;
4006 04ca23f4 2018-07-16 stsp case 'r':
4007 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
4008 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
4009 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4010 9ba1d308 2019-10-21 stsp optarg);
4011 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4012 04ca23f4 2018-07-16 stsp break;
4013 dbec59df 2020-04-18 stsp case 'R':
4014 dbec59df 2020-04-18 stsp reverse_display_order = 1;
4015 dbec59df 2020-04-18 stsp break;
4016 6841bf13 2019-11-29 kn case 's':
4017 6841bf13 2019-11-29 kn search_pattern = optarg;
4018 6841bf13 2019-11-29 kn break;
4019 d1fe46f9 2020-04-18 stsp case 'x':
4020 d1fe46f9 2020-04-18 stsp end_commit = optarg;
4021 d1fe46f9 2020-04-18 stsp break;
4022 79109fed 2018-03-27 stsp default:
4023 2deda0b9 2019-03-07 stsp usage_log();
4024 79109fed 2018-03-27 stsp /* NOTREACHED */
4025 79109fed 2018-03-27 stsp }
4026 79109fed 2018-03-27 stsp }
4027 79109fed 2018-03-27 stsp
4028 79109fed 2018-03-27 stsp argc -= optind;
4029 79109fed 2018-03-27 stsp argv += optind;
4030 f42b1b34 2018-03-12 stsp
4031 dc1edbfa 2019-11-29 kn if (diff_context == -1)
4032 dc1edbfa 2019-11-29 kn diff_context = 3;
4033 dc1edbfa 2019-11-29 kn else if (!show_patch)
4034 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
4035 dc1edbfa 2019-11-29 kn
4036 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
4037 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
4038 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4039 04ca23f4 2018-07-16 stsp goto done;
4040 04ca23f4 2018-07-16 stsp }
4041 cffc0aa4 2019-02-05 stsp
4042 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
4043 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
4044 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4045 50f2fada 2020-04-24 stsp goto done;
4046 50f2fada 2020-04-24 stsp error = NULL;
4047 50f2fada 2020-04-24 stsp }
4048 cffc0aa4 2019-02-05 stsp
4049 603cdeb0 2020-10-22 stsp if (argc == 1) {
4050 e7301579 2019-03-18 stsp if (worktree) {
4051 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4052 e7301579 2019-03-18 stsp argv[0]);
4053 e7301579 2019-03-18 stsp if (error)
4054 e7301579 2019-03-18 stsp goto done;
4055 e7301579 2019-03-18 stsp } else {
4056 e7301579 2019-03-18 stsp path = strdup(argv[0]);
4057 e7301579 2019-03-18 stsp if (path == NULL) {
4058 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4059 e7301579 2019-03-18 stsp goto done;
4060 e7301579 2019-03-18 stsp }
4061 e7301579 2019-03-18 stsp }
4062 603cdeb0 2020-10-22 stsp } else if (argc != 0)
4063 cbd1af7a 2019-03-18 stsp usage_log();
4064 cbd1af7a 2019-03-18 stsp
4065 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
4066 5486daa2 2019-05-11 stsp repo_path = worktree ?
4067 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4068 5486daa2 2019-05-11 stsp }
4069 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
4070 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4071 cffc0aa4 2019-02-05 stsp goto done;
4072 04ca23f4 2018-07-16 stsp }
4073 6098196c 2019-01-04 stsp
4074 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4075 d7d4f210 2018-03-12 stsp if (error != NULL)
4076 04ca23f4 2018-07-16 stsp goto done;
4077 f42b1b34 2018-03-12 stsp
4078 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4079 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4080 c02c541e 2019-03-29 stsp if (error)
4081 c02c541e 2019-03-29 stsp goto done;
4082 c02c541e 2019-03-29 stsp
4083 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4084 84de9106 2020-12-26 stsp if (error)
4085 84de9106 2020-12-26 stsp goto done;
4086 84de9106 2020-12-26 stsp
4087 888b7d99 2020-12-26 stsp error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4088 888b7d99 2020-12-26 stsp if (error)
4089 888b7d99 2020-12-26 stsp goto done;
4090 888b7d99 2020-12-26 stsp
4091 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
4092 3235492e 2018-04-01 stsp struct got_reference *head_ref;
4093 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
4094 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
4095 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4096 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
4097 3235492e 2018-04-01 stsp if (error != NULL)
4098 d1fe46f9 2020-04-18 stsp goto done;
4099 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
4100 3235492e 2018-04-01 stsp got_ref_close(head_ref);
4101 3235492e 2018-04-01 stsp if (error != NULL)
4102 d1fe46f9 2020-04-18 stsp goto done;
4103 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
4104 d1fe46f9 2020-04-18 stsp start_id);
4105 d1fe46f9 2020-04-18 stsp if (error != NULL)
4106 d1fe46f9 2020-04-18 stsp goto done;
4107 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
4108 3235492e 2018-04-01 stsp } else {
4109 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
4110 84de9106 2020-12-26 stsp start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4111 d1fe46f9 2020-04-18 stsp if (error != NULL)
4112 d1fe46f9 2020-04-18 stsp goto done;
4113 3235492e 2018-04-01 stsp }
4114 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
4115 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
4116 84de9106 2020-12-26 stsp end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4117 d1fe46f9 2020-04-18 stsp if (error != NULL)
4118 d1fe46f9 2020-04-18 stsp goto done;
4119 d1fe46f9 2020-04-18 stsp }
4120 04ca23f4 2018-07-16 stsp
4121 deeabeae 2019-08-27 stsp if (worktree) {
4122 603cdeb0 2020-10-22 stsp /*
4123 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
4124 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
4125 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
4126 603cdeb0 2020-10-22 stsp */
4127 603cdeb0 2020-10-22 stsp if (path) {
4128 603cdeb0 2020-10-22 stsp const char *prefix;
4129 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
4130 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4131 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
4132 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
4133 603cdeb0 2020-10-22 stsp goto done;
4134 603cdeb0 2020-10-22 stsp }
4135 603cdeb0 2020-10-22 stsp }
4136 deeabeae 2019-08-27 stsp } else
4137 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
4138 8fa913ec 2020-11-14 stsp path ? path : "");
4139 04ca23f4 2018-07-16 stsp if (error != NULL)
4140 04ca23f4 2018-07-16 stsp goto done;
4141 04ca23f4 2018-07-16 stsp if (in_repo_path) {
4142 04ca23f4 2018-07-16 stsp free(path);
4143 04ca23f4 2018-07-16 stsp path = in_repo_path;
4144 04ca23f4 2018-07-16 stsp }
4145 199a4027 2019-02-02 stsp
4146 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
4147 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
4148 888b7d99 2020-12-26 stsp limit, log_branches, reverse_display_order, refs_idmap);
4149 04ca23f4 2018-07-16 stsp done:
4150 04ca23f4 2018-07-16 stsp free(path);
4151 04ca23f4 2018-07-16 stsp free(repo_path);
4152 04ca23f4 2018-07-16 stsp free(cwd);
4153 cffc0aa4 2019-02-05 stsp if (worktree)
4154 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
4155 ad242220 2018-09-08 stsp if (repo) {
4156 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4157 ad242220 2018-09-08 stsp if (error == NULL)
4158 1d0f4054 2021-06-17 stsp error = close_err;
4159 ad242220 2018-09-08 stsp }
4160 888b7d99 2020-12-26 stsp if (refs_idmap)
4161 888b7d99 2020-12-26 stsp got_reflist_object_id_map_free(refs_idmap);
4162 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4163 8bf5b3c9 2018-03-17 stsp return error;
4164 5c860e29 2018-03-12 stsp }
4165 5c860e29 2018-03-12 stsp
4166 4ed7e80c 2018-05-20 stsp __dead static void
4167 3f8b7d6a 2018-04-01 stsp usage_diff(void)
4168 3f8b7d6a 2018-04-01 stsp {
4169 64453f7e 2020-11-21 stsp fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
4170 64453f7e 2020-11-21 stsp "[-s] [-w] [object1 object2 | path]\n", getprogname());
4171 3f8b7d6a 2018-04-01 stsp exit(1);
4172 b00d56cd 2018-04-01 stsp }
4173 b00d56cd 2018-04-01 stsp
4174 b72f483a 2019-02-05 stsp struct print_diff_arg {
4175 b72f483a 2019-02-05 stsp struct got_repository *repo;
4176 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
4177 b72f483a 2019-02-05 stsp int diff_context;
4178 3fc0c068 2019-02-10 stsp const char *id_str;
4179 3fc0c068 2019-02-10 stsp int header_shown;
4180 98eaaa12 2019-08-03 stsp int diff_staged;
4181 63035f9f 2019-10-06 stsp int ignore_whitespace;
4182 64453f7e 2020-11-21 stsp int force_text_diff;
4183 b72f483a 2019-02-05 stsp };
4184 39449a05 2020-07-23 stsp
4185 39449a05 2020-07-23 stsp /*
4186 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
4187 39449a05 2020-07-23 stsp * it as content to the diff engine.
4188 39449a05 2020-07-23 stsp */
4189 39449a05 2020-07-23 stsp static const struct got_error *
4190 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4191 39449a05 2020-07-23 stsp const char *abspath)
4192 39449a05 2020-07-23 stsp {
4193 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
4194 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
4195 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
4196 39449a05 2020-07-23 stsp
4197 39449a05 2020-07-23 stsp *fd = -1;
4198 39449a05 2020-07-23 stsp
4199 39449a05 2020-07-23 stsp if (dirfd != -1) {
4200 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4201 39449a05 2020-07-23 stsp if (target_len == -1)
4202 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
4203 39449a05 2020-07-23 stsp } else {
4204 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
4205 39449a05 2020-07-23 stsp if (target_len == -1)
4206 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
4207 39449a05 2020-07-23 stsp }
4208 39449a05 2020-07-23 stsp
4209 39449a05 2020-07-23 stsp *fd = got_opentempfd();
4210 39449a05 2020-07-23 stsp if (*fd == -1)
4211 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
4212 39449a05 2020-07-23 stsp
4213 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
4214 39449a05 2020-07-23 stsp if (outlen == -1) {
4215 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
4216 39449a05 2020-07-23 stsp goto done;
4217 39449a05 2020-07-23 stsp }
4218 39449a05 2020-07-23 stsp
4219 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
4220 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
4221 39449a05 2020-07-23 stsp goto done;
4222 39449a05 2020-07-23 stsp }
4223 39449a05 2020-07-23 stsp done:
4224 39449a05 2020-07-23 stsp if (err) {
4225 39449a05 2020-07-23 stsp close(*fd);
4226 39449a05 2020-07-23 stsp *fd = -1;
4227 39449a05 2020-07-23 stsp }
4228 39449a05 2020-07-23 stsp return err;
4229 39449a05 2020-07-23 stsp }
4230 b72f483a 2019-02-05 stsp
4231 4ed7e80c 2018-05-20 stsp static const struct got_error *
4232 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
4233 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4234 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4235 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4236 b72f483a 2019-02-05 stsp {
4237 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
4238 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4239 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4240 12463d8b 2019-12-13 stsp int fd = -1;
4241 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4242 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4243 b72f483a 2019-02-05 stsp struct stat sb;
4244 b72f483a 2019-02-05 stsp
4245 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4246 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4247 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4248 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4249 98eaaa12 2019-08-03 stsp return NULL;
4250 98eaaa12 2019-08-03 stsp } else {
4251 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4252 98eaaa12 2019-08-03 stsp return NULL;
4253 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4254 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4255 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4256 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4257 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4258 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4259 98eaaa12 2019-08-03 stsp return NULL;
4260 98eaaa12 2019-08-03 stsp }
4261 3fc0c068 2019-02-10 stsp
4262 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4263 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4264 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4265 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4266 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4267 3fc0c068 2019-02-10 stsp }
4268 b72f483a 2019-02-05 stsp
4269 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4270 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4271 98eaaa12 2019-08-03 stsp switch (staged_status) {
4272 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4273 98eaaa12 2019-08-03 stsp label1 = path;
4274 98eaaa12 2019-08-03 stsp label2 = path;
4275 98eaaa12 2019-08-03 stsp break;
4276 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4277 98eaaa12 2019-08-03 stsp label2 = path;
4278 98eaaa12 2019-08-03 stsp break;
4279 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4280 98eaaa12 2019-08-03 stsp label1 = path;
4281 98eaaa12 2019-08-03 stsp break;
4282 98eaaa12 2019-08-03 stsp default:
4283 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4284 98eaaa12 2019-08-03 stsp }
4285 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4286 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4287 64453f7e 2020-11-21 stsp a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4288 98eaaa12 2019-08-03 stsp }
4289 98eaaa12 2019-08-03 stsp
4290 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4291 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4292 4ce46740 2019-08-08 stsp char *id_str;
4293 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4294 408b4ebc 2019-08-03 stsp 8192);
4295 4ce46740 2019-08-08 stsp if (err)
4296 4ce46740 2019-08-08 stsp goto done;
4297 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4298 4ce46740 2019-08-08 stsp if (err)
4299 4ce46740 2019-08-08 stsp goto done;
4300 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4301 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4302 4ce46740 2019-08-08 stsp free(id_str);
4303 4ce46740 2019-08-08 stsp goto done;
4304 4ce46740 2019-08-08 stsp }
4305 4ce46740 2019-08-08 stsp free(id_str);
4306 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4307 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4308 4ce46740 2019-08-08 stsp if (err)
4309 4ce46740 2019-08-08 stsp goto done;
4310 4ce46740 2019-08-08 stsp }
4311 d00136be 2019-03-26 stsp
4312 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4313 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4314 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4315 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4316 2ec1f75b 2019-03-26 stsp goto done;
4317 2ec1f75b 2019-03-26 stsp }
4318 b72f483a 2019-02-05 stsp
4319 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4320 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4321 12463d8b 2019-12-13 stsp if (fd == -1) {
4322 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4323 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4324 39449a05 2020-07-23 stsp abspath);
4325 39449a05 2020-07-23 stsp goto done;
4326 39449a05 2020-07-23 stsp }
4327 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4328 39449a05 2020-07-23 stsp de_name, abspath);
4329 39449a05 2020-07-23 stsp if (err)
4330 39449a05 2020-07-23 stsp goto done;
4331 12463d8b 2019-12-13 stsp }
4332 12463d8b 2019-12-13 stsp } else {
4333 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4334 12463d8b 2019-12-13 stsp if (fd == -1) {
4335 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4336 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4337 39449a05 2020-07-23 stsp abspath);
4338 39449a05 2020-07-23 stsp goto done;
4339 39449a05 2020-07-23 stsp }
4340 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4341 39449a05 2020-07-23 stsp de_name, abspath);
4342 39449a05 2020-07-23 stsp if (err)
4343 39449a05 2020-07-23 stsp goto done;
4344 12463d8b 2019-12-13 stsp }
4345 12463d8b 2019-12-13 stsp }
4346 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4347 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4348 2ec1f75b 2019-03-26 stsp goto done;
4349 2ec1f75b 2019-03-26 stsp }
4350 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4351 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4352 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4353 2ec1f75b 2019-03-26 stsp goto done;
4354 2ec1f75b 2019-03-26 stsp }
4355 12463d8b 2019-12-13 stsp fd = -1;
4356 2ec1f75b 2019-03-26 stsp } else
4357 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4358 b72f483a 2019-02-05 stsp
4359 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4360 64453f7e 2020-11-21 stsp a->diff_context, a->ignore_whitespace, a->force_text_diff, stdout);
4361 b72f483a 2019-02-05 stsp done:
4362 b72f483a 2019-02-05 stsp if (blob1)
4363 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4364 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4365 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4366 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4367 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4368 b72f483a 2019-02-05 stsp free(abspath);
4369 927df6b7 2019-02-10 stsp return err;
4370 927df6b7 2019-02-10 stsp }
4371 927df6b7 2019-02-10 stsp
4372 927df6b7 2019-02-10 stsp static const struct got_error *
4373 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4374 b00d56cd 2018-04-01 stsp {
4375 b00d56cd 2018-04-01 stsp const struct got_error *error;
4376 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4377 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4378 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4379 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4380 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4381 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4382 15a94983 2018-12-23 stsp int type1, type2;
4383 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4384 64453f7e 2020-11-21 stsp int force_text_diff = 0;
4385 c0cc5c62 2018-10-18 stsp const char *errstr;
4386 927df6b7 2019-02-10 stsp char *path = NULL;
4387 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4388 b00d56cd 2018-04-01 stsp
4389 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4390 84de9106 2020-12-26 stsp
4391 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4392 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4393 25eccc22 2019-01-04 stsp NULL) == -1)
4394 b00d56cd 2018-04-01 stsp err(1, "pledge");
4395 b00d56cd 2018-04-01 stsp #endif
4396 b00d56cd 2018-04-01 stsp
4397 64453f7e 2020-11-21 stsp while ((ch = getopt(argc, argv, "aC:r:sw")) != -1) {
4398 b00d56cd 2018-04-01 stsp switch (ch) {
4399 64453f7e 2020-11-21 stsp case 'a':
4400 64453f7e 2020-11-21 stsp force_text_diff = 1;
4401 64453f7e 2020-11-21 stsp break;
4402 c0cc5c62 2018-10-18 stsp case 'C':
4403 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4404 dfcab68b 2019-11-29 kn &errstr);
4405 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4406 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4407 c0cc5c62 2018-10-18 stsp break;
4408 b72f483a 2019-02-05 stsp case 'r':
4409 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4410 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4411 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4412 9ba1d308 2019-10-21 stsp optarg);
4413 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4414 b72f483a 2019-02-05 stsp break;
4415 98eaaa12 2019-08-03 stsp case 's':
4416 98eaaa12 2019-08-03 stsp diff_staged = 1;
4417 63035f9f 2019-10-06 stsp break;
4418 63035f9f 2019-10-06 stsp case 'w':
4419 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4420 98eaaa12 2019-08-03 stsp break;
4421 b00d56cd 2018-04-01 stsp default:
4422 2deda0b9 2019-03-07 stsp usage_diff();
4423 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4424 b00d56cd 2018-04-01 stsp }
4425 b00d56cd 2018-04-01 stsp }
4426 b00d56cd 2018-04-01 stsp
4427 b00d56cd 2018-04-01 stsp argc -= optind;
4428 b00d56cd 2018-04-01 stsp argv += optind;
4429 b00d56cd 2018-04-01 stsp
4430 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4431 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4432 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4433 b72f483a 2019-02-05 stsp goto done;
4434 b72f483a 2019-02-05 stsp }
4435 927df6b7 2019-02-10 stsp if (argc <= 1) {
4436 b72f483a 2019-02-05 stsp if (repo_path)
4437 b72f483a 2019-02-05 stsp errx(1,
4438 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4439 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4440 fa51e947 2020-03-27 stsp if (error) {
4441 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4442 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4443 fa51e947 2020-03-27 stsp cwd);
4444 1f03b8da 2020-03-20 stsp goto done;
4445 fa51e947 2020-03-27 stsp }
4446 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4447 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4448 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4449 927df6b7 2019-02-10 stsp goto done;
4450 927df6b7 2019-02-10 stsp }
4451 927df6b7 2019-02-10 stsp if (argc == 1) {
4452 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4453 cbd1af7a 2019-03-18 stsp argv[0]);
4454 927df6b7 2019-02-10 stsp if (error)
4455 927df6b7 2019-02-10 stsp goto done;
4456 927df6b7 2019-02-10 stsp } else {
4457 927df6b7 2019-02-10 stsp path = strdup("");
4458 927df6b7 2019-02-10 stsp if (path == NULL) {
4459 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4460 927df6b7 2019-02-10 stsp goto done;
4461 927df6b7 2019-02-10 stsp }
4462 927df6b7 2019-02-10 stsp }
4463 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4464 98eaaa12 2019-08-03 stsp if (diff_staged)
4465 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4466 98eaaa12 2019-08-03 stsp "objects in repository");
4467 15a94983 2018-12-23 stsp id_str1 = argv[0];
4468 15a94983 2018-12-23 stsp id_str2 = argv[1];
4469 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4470 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4471 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4472 30db809c 2019-06-05 stsp goto done;
4473 1a1242a9 2021-04-01 kn repo_path = strdup(worktree ?
4474 1a1242a9 2021-04-01 kn got_worktree_get_repo_path(worktree) : cwd);
4475 1a1242a9 2021-04-01 kn if (repo_path == NULL) {
4476 1a1242a9 2021-04-01 kn error = got_error_from_errno("strdup");
4477 1a1242a9 2021-04-01 kn goto done;
4478 30db809c 2019-06-05 stsp }
4479 30db809c 2019-06-05 stsp }
4480 b00d56cd 2018-04-01 stsp } else
4481 b00d56cd 2018-04-01 stsp usage_diff();
4482 b00d56cd 2018-04-01 stsp
4483 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4484 76089277 2018-04-01 stsp free(repo_path);
4485 b00d56cd 2018-04-01 stsp if (error != NULL)
4486 b00d56cd 2018-04-01 stsp goto done;
4487 b00d56cd 2018-04-01 stsp
4488 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4489 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4490 c02c541e 2019-03-29 stsp if (error)
4491 c02c541e 2019-03-29 stsp goto done;
4492 c02c541e 2019-03-29 stsp
4493 30db809c 2019-06-05 stsp if (argc <= 1) {
4494 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4495 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4496 b72f483a 2019-02-05 stsp char *id_str;
4497 72ea6654 2019-07-27 stsp
4498 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4499 72ea6654 2019-07-27 stsp
4500 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4501 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4502 b72f483a 2019-02-05 stsp if (error)
4503 b72f483a 2019-02-05 stsp goto done;
4504 b72f483a 2019-02-05 stsp arg.repo = repo;
4505 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4506 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4507 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4508 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4509 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4510 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4511 64453f7e 2020-11-21 stsp arg.force_text_diff = force_text_diff;
4512 b72f483a 2019-02-05 stsp
4513 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4514 72ea6654 2019-07-27 stsp if (error)
4515 72ea6654 2019-07-27 stsp goto done;
4516 72ea6654 2019-07-27 stsp
4517 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
4518 f6343036 2021-06-22 stsp print_diff, &arg, check_cancelled, NULL);
4519 b72f483a 2019-02-05 stsp free(id_str);
4520 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4521 b72f483a 2019-02-05 stsp goto done;
4522 b72f483a 2019-02-05 stsp }
4523 84de9106 2020-12-26 stsp
4524 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4525 84de9106 2020-12-26 stsp if (error)
4526 84de9106 2020-12-26 stsp return error;
4527 b72f483a 2019-02-05 stsp
4528 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4529 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, &refs, repo);
4530 0adb7196 2019-08-11 stsp if (error)
4531 0adb7196 2019-08-11 stsp goto done;
4532 b00d56cd 2018-04-01 stsp
4533 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
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 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4539 15a94983 2018-12-23 stsp if (error)
4540 15a94983 2018-12-23 stsp goto done;
4541 15a94983 2018-12-23 stsp
4542 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4543 15a94983 2018-12-23 stsp if (error)
4544 15a94983 2018-12-23 stsp goto done;
4545 15a94983 2018-12-23 stsp
4546 15a94983 2018-12-23 stsp if (type1 != type2) {
4547 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4548 b00d56cd 2018-04-01 stsp goto done;
4549 b00d56cd 2018-04-01 stsp }
4550 b00d56cd 2018-04-01 stsp
4551 15a94983 2018-12-23 stsp switch (type1) {
4552 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4553 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4554 64453f7e 2020-11-21 stsp NULL, NULL, diff_context, ignore_whitespace,
4555 64453f7e 2020-11-21 stsp force_text_diff, repo, stdout);
4556 b00d56cd 2018-04-01 stsp break;
4557 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4558 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4559 64453f7e 2020-11-21 stsp "", "", diff_context, ignore_whitespace, force_text_diff,
4560 64453f7e 2020-11-21 stsp repo, stdout);
4561 b00d56cd 2018-04-01 stsp break;
4562 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4563 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4564 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4565 64453f7e 2020-11-21 stsp diff_context, ignore_whitespace, force_text_diff, repo,
4566 64453f7e 2020-11-21 stsp stdout);
4567 b00d56cd 2018-04-01 stsp break;
4568 b00d56cd 2018-04-01 stsp default:
4569 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4570 b00d56cd 2018-04-01 stsp }
4571 b00d56cd 2018-04-01 stsp done:
4572 5e70831e 2019-05-28 stsp free(label1);
4573 5e70831e 2019-05-28 stsp free(label2);
4574 15a94983 2018-12-23 stsp free(id1);
4575 15a94983 2018-12-23 stsp free(id2);
4576 927df6b7 2019-02-10 stsp free(path);
4577 b72f483a 2019-02-05 stsp if (worktree)
4578 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4579 ad242220 2018-09-08 stsp if (repo) {
4580 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4581 ad242220 2018-09-08 stsp if (error == NULL)
4582 1d0f4054 2021-06-17 stsp error = close_err;
4583 ad242220 2018-09-08 stsp }
4584 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4585 b00d56cd 2018-04-01 stsp return error;
4586 404c43c4 2018-06-21 stsp }
4587 404c43c4 2018-06-21 stsp
4588 404c43c4 2018-06-21 stsp __dead static void
4589 404c43c4 2018-06-21 stsp usage_blame(void)
4590 404c43c4 2018-06-21 stsp {
4591 9270e621 2019-02-05 stsp fprintf(stderr,
4592 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4593 404c43c4 2018-06-21 stsp getprogname());
4594 404c43c4 2018-06-21 stsp exit(1);
4595 b00d56cd 2018-04-01 stsp }
4596 b00d56cd 2018-04-01 stsp
4597 28315671 2019-08-14 stsp struct blame_line {
4598 28315671 2019-08-14 stsp int annotated;
4599 28315671 2019-08-14 stsp char *id_str;
4600 82f6abb8 2019-08-14 stsp char *committer;
4601 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4602 28315671 2019-08-14 stsp };
4603 28315671 2019-08-14 stsp
4604 28315671 2019-08-14 stsp struct blame_cb_args {
4605 28315671 2019-08-14 stsp struct blame_line *lines;
4606 28315671 2019-08-14 stsp int nlines;
4607 7ef28ff8 2019-08-14 stsp int nlines_prec;
4608 28315671 2019-08-14 stsp int lineno_cur;
4609 28315671 2019-08-14 stsp off_t *line_offsets;
4610 28315671 2019-08-14 stsp FILE *f;
4611 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4612 28315671 2019-08-14 stsp };
4613 28315671 2019-08-14 stsp
4614 404c43c4 2018-06-21 stsp static const struct got_error *
4615 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4616 28315671 2019-08-14 stsp {
4617 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4618 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4619 28315671 2019-08-14 stsp struct blame_line *bline;
4620 82f6abb8 2019-08-14 stsp char *line = NULL;
4621 28315671 2019-08-14 stsp size_t linesize = 0;
4622 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4623 28315671 2019-08-14 stsp off_t offset;
4624 bcb49d15 2019-08-14 stsp struct tm tm;
4625 bcb49d15 2019-08-14 stsp time_t committer_time;
4626 28315671 2019-08-14 stsp
4627 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4628 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4629 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4630 28315671 2019-08-14 stsp
4631 28315671 2019-08-14 stsp if (sigint_received)
4632 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4633 28315671 2019-08-14 stsp
4634 2839f8b9 2019-08-18 stsp if (lineno == -1)
4635 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4636 2839f8b9 2019-08-18 stsp
4637 28315671 2019-08-14 stsp /* Annotate this line. */
4638 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4639 28315671 2019-08-14 stsp if (bline->annotated)
4640 28315671 2019-08-14 stsp return NULL;
4641 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4642 28315671 2019-08-14 stsp if (err)
4643 28315671 2019-08-14 stsp return err;
4644 82f6abb8 2019-08-14 stsp
4645 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4646 82f6abb8 2019-08-14 stsp if (err)
4647 82f6abb8 2019-08-14 stsp goto done;
4648 82f6abb8 2019-08-14 stsp
4649 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4650 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4651 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4652 bcb49d15 2019-08-14 stsp goto done;
4653 bcb49d15 2019-08-14 stsp }
4654 bcb49d15 2019-08-14 stsp
4655 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4656 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
4657 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
4658 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4659 ec6d1a36 2021-03-21 jrick &tm) == 0) {
4660 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4661 82f6abb8 2019-08-14 stsp goto done;
4662 82f6abb8 2019-08-14 stsp }
4663 28315671 2019-08-14 stsp bline->annotated = 1;
4664 28315671 2019-08-14 stsp
4665 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4666 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4667 28315671 2019-08-14 stsp if (!bline->annotated)
4668 82f6abb8 2019-08-14 stsp goto done;
4669 28315671 2019-08-14 stsp
4670 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4671 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4672 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4673 82f6abb8 2019-08-14 stsp goto done;
4674 82f6abb8 2019-08-14 stsp }
4675 28315671 2019-08-14 stsp
4676 28315671 2019-08-14 stsp while (bline->annotated) {
4677 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4678 82f6abb8 2019-08-14 stsp size_t len;
4679 82f6abb8 2019-08-14 stsp
4680 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4681 28315671 2019-08-14 stsp if (ferror(a->f))
4682 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4683 28315671 2019-08-14 stsp break;
4684 28315671 2019-08-14 stsp }
4685 82f6abb8 2019-08-14 stsp
4686 82f6abb8 2019-08-14 stsp committer = bline->committer;
4687 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4688 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4689 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4690 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4691 82f6abb8 2019-08-14 stsp if (at)
4692 82f6abb8 2019-08-14 stsp *at = '\0';
4693 82f6abb8 2019-08-14 stsp len = strlen(committer);
4694 82f6abb8 2019-08-14 stsp if (len >= 9)
4695 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4696 28315671 2019-08-14 stsp
4697 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4698 28315671 2019-08-14 stsp if (nl)
4699 28315671 2019-08-14 stsp *nl = '\0';
4700 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4701 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4702 28315671 2019-08-14 stsp
4703 28315671 2019-08-14 stsp a->lineno_cur++;
4704 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4705 28315671 2019-08-14 stsp }
4706 82f6abb8 2019-08-14 stsp done:
4707 82f6abb8 2019-08-14 stsp if (commit)
4708 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4709 28315671 2019-08-14 stsp free(line);
4710 28315671 2019-08-14 stsp return err;
4711 28315671 2019-08-14 stsp }
4712 28315671 2019-08-14 stsp
4713 28315671 2019-08-14 stsp static const struct got_error *
4714 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4715 404c43c4 2018-06-21 stsp {
4716 404c43c4 2018-06-21 stsp const struct got_error *error;
4717 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4718 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4719 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4720 0587e10c 2020-07-23 stsp char *link_target = NULL;
4721 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4722 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4723 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4724 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4725 28315671 2019-08-14 stsp struct blame_cb_args bca;
4726 28315671 2019-08-14 stsp int ch, obj_type, i;
4727 be659d10 2020-11-18 stsp off_t filesize;
4728 90f3c347 2019-08-19 stsp
4729 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4730 404c43c4 2018-06-21 stsp
4731 404c43c4 2018-06-21 stsp #ifndef PROFILE
4732 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4733 36e2fb66 2019-01-04 stsp NULL) == -1)
4734 404c43c4 2018-06-21 stsp err(1, "pledge");
4735 404c43c4 2018-06-21 stsp #endif
4736 404c43c4 2018-06-21 stsp
4737 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4738 404c43c4 2018-06-21 stsp switch (ch) {
4739 404c43c4 2018-06-21 stsp case 'c':
4740 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4741 404c43c4 2018-06-21 stsp break;
4742 66bea077 2018-08-02 stsp case 'r':
4743 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4744 66bea077 2018-08-02 stsp if (repo_path == NULL)
4745 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4746 9ba1d308 2019-10-21 stsp optarg);
4747 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4748 66bea077 2018-08-02 stsp break;
4749 404c43c4 2018-06-21 stsp default:
4750 2deda0b9 2019-03-07 stsp usage_blame();
4751 404c43c4 2018-06-21 stsp /* NOTREACHED */
4752 404c43c4 2018-06-21 stsp }
4753 404c43c4 2018-06-21 stsp }
4754 404c43c4 2018-06-21 stsp
4755 404c43c4 2018-06-21 stsp argc -= optind;
4756 404c43c4 2018-06-21 stsp argv += optind;
4757 404c43c4 2018-06-21 stsp
4758 a39318fd 2018-08-02 stsp if (argc == 1)
4759 404c43c4 2018-06-21 stsp path = argv[0];
4760 a39318fd 2018-08-02 stsp else
4761 404c43c4 2018-06-21 stsp usage_blame();
4762 404c43c4 2018-06-21 stsp
4763 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4764 66bea077 2018-08-02 stsp if (cwd == NULL) {
4765 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4766 66bea077 2018-08-02 stsp goto done;
4767 66bea077 2018-08-02 stsp }
4768 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4769 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4770 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4771 66bea077 2018-08-02 stsp goto done;
4772 0c06baac 2019-02-05 stsp else
4773 0c06baac 2019-02-05 stsp error = NULL;
4774 0c06baac 2019-02-05 stsp if (worktree) {
4775 0c06baac 2019-02-05 stsp repo_path =
4776 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4777 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4778 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4779 1f03b8da 2020-03-20 stsp if (error)
4780 1f03b8da 2020-03-20 stsp goto done;
4781 1f03b8da 2020-03-20 stsp }
4782 0c06baac 2019-02-05 stsp } else {
4783 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4784 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4785 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4786 0c06baac 2019-02-05 stsp goto done;
4787 0c06baac 2019-02-05 stsp }
4788 66bea077 2018-08-02 stsp }
4789 66bea077 2018-08-02 stsp }
4790 36e2fb66 2019-01-04 stsp
4791 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4792 c02c541e 2019-03-29 stsp if (error != NULL)
4793 404c43c4 2018-06-21 stsp goto done;
4794 404c43c4 2018-06-21 stsp
4795 0c06baac 2019-02-05 stsp if (worktree) {
4796 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4797 01740607 2020-11-04 stsp char *p;
4798 01740607 2020-11-04 stsp
4799 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4800 01740607 2020-11-04 stsp if (error)
4801 01740607 2020-11-04 stsp goto done;
4802 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4803 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4804 01740607 2020-11-04 stsp p) == -1) {
4805 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4806 01740607 2020-11-04 stsp free(p);
4807 0c06baac 2019-02-05 stsp goto done;
4808 0c06baac 2019-02-05 stsp }
4809 0c06baac 2019-02-05 stsp free(p);
4810 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4811 0c06baac 2019-02-05 stsp } else {
4812 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4813 01740607 2020-11-04 stsp if (error)
4814 01740607 2020-11-04 stsp goto done;
4815 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4816 0c06baac 2019-02-05 stsp }
4817 0c06baac 2019-02-05 stsp if (error)
4818 66bea077 2018-08-02 stsp goto done;
4819 66bea077 2018-08-02 stsp
4820 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4821 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4822 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4823 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4824 404c43c4 2018-06-21 stsp if (error != NULL)
4825 66bea077 2018-08-02 stsp goto done;
4826 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4827 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4828 404c43c4 2018-06-21 stsp if (error != NULL)
4829 66bea077 2018-08-02 stsp goto done;
4830 404c43c4 2018-06-21 stsp } else {
4831 84de9106 2020-12-26 stsp struct got_reflist_head refs;
4832 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
4833 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4834 84de9106 2020-12-26 stsp NULL);
4835 84de9106 2020-12-26 stsp if (error)
4836 84de9106 2020-12-26 stsp goto done;
4837 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4838 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4839 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
4840 30837e32 2019-07-25 stsp if (error)
4841 66bea077 2018-08-02 stsp goto done;
4842 404c43c4 2018-06-21 stsp }
4843 404c43c4 2018-06-21 stsp
4844 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4845 0587e10c 2020-07-23 stsp commit_id, repo);
4846 0587e10c 2020-07-23 stsp if (error)
4847 0587e10c 2020-07-23 stsp goto done;
4848 0587e10c 2020-07-23 stsp
4849 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4850 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4851 28315671 2019-08-14 stsp if (error)
4852 28315671 2019-08-14 stsp goto done;
4853 28315671 2019-08-14 stsp
4854 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4855 28315671 2019-08-14 stsp if (error)
4856 28315671 2019-08-14 stsp goto done;
4857 28315671 2019-08-14 stsp
4858 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4859 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4860 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4861 28315671 2019-08-14 stsp goto done;
4862 28315671 2019-08-14 stsp }
4863 28315671 2019-08-14 stsp
4864 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4865 28315671 2019-08-14 stsp if (error)
4866 28315671 2019-08-14 stsp goto done;
4867 28315671 2019-08-14 stsp bca.f = got_opentemp();
4868 28315671 2019-08-14 stsp if (bca.f == NULL) {
4869 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4870 28315671 2019-08-14 stsp goto done;
4871 28315671 2019-08-14 stsp }
4872 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4873 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4874 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4875 28315671 2019-08-14 stsp goto done;
4876 28315671 2019-08-14 stsp
4877 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4878 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4879 b02560ec 2019-08-19 stsp bca.nlines--;
4880 b02560ec 2019-08-19 stsp
4881 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4882 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4883 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4884 28315671 2019-08-14 stsp goto done;
4885 28315671 2019-08-14 stsp }
4886 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4887 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4888 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4889 7ef28ff8 2019-08-14 stsp while (i > 0) {
4890 7ef28ff8 2019-08-14 stsp i /= 10;
4891 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4892 7ef28ff8 2019-08-14 stsp }
4893 82f6abb8 2019-08-14 stsp bca.repo = repo;
4894 7ef28ff8 2019-08-14 stsp
4895 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4896 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4897 404c43c4 2018-06-21 stsp done:
4898 66bea077 2018-08-02 stsp free(in_repo_path);
4899 0587e10c 2020-07-23 stsp free(link_target);
4900 66bea077 2018-08-02 stsp free(repo_path);
4901 66bea077 2018-08-02 stsp free(cwd);
4902 404c43c4 2018-06-21 stsp free(commit_id);
4903 28315671 2019-08-14 stsp free(obj_id);
4904 28315671 2019-08-14 stsp if (blob)
4905 28315671 2019-08-14 stsp got_object_blob_close(blob);
4906 0c06baac 2019-02-05 stsp if (worktree)
4907 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4908 ad242220 2018-09-08 stsp if (repo) {
4909 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
4910 ad242220 2018-09-08 stsp if (error == NULL)
4911 1d0f4054 2021-06-17 stsp error = close_err;
4912 ad242220 2018-09-08 stsp }
4913 3affba96 2019-09-22 stsp if (bca.lines) {
4914 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4915 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4916 3affba96 2019-09-22 stsp free(bline->id_str);
4917 3affba96 2019-09-22 stsp free(bline->committer);
4918 3affba96 2019-09-22 stsp }
4919 3affba96 2019-09-22 stsp free(bca.lines);
4920 28315671 2019-08-14 stsp }
4921 28315671 2019-08-14 stsp free(bca.line_offsets);
4922 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4923 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4924 404c43c4 2018-06-21 stsp return error;
4925 5de5890b 2018-10-18 stsp }
4926 5de5890b 2018-10-18 stsp
4927 5de5890b 2018-10-18 stsp __dead static void
4928 5de5890b 2018-10-18 stsp usage_tree(void)
4929 5de5890b 2018-10-18 stsp {
4930 c1669e2e 2019-01-09 stsp fprintf(stderr,
4931 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4932 5de5890b 2018-10-18 stsp getprogname());
4933 5de5890b 2018-10-18 stsp exit(1);
4934 5de5890b 2018-10-18 stsp }
4935 5de5890b 2018-10-18 stsp
4936 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4937 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4938 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4939 c1669e2e 2019-01-09 stsp {
4940 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4941 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4942 848d6979 2019-08-12 stsp const char *modestr = "";
4943 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4944 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4945 5de5890b 2018-10-18 stsp
4946 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4947 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4948 c1669e2e 2019-01-09 stsp path++;
4949 c1669e2e 2019-01-09 stsp
4950 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4951 63c5ca5d 2019-08-24 stsp modestr = "$";
4952 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4953 0d6c6ee3 2020-05-20 stsp int i;
4954 0d6c6ee3 2020-05-20 stsp
4955 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4956 0d6c6ee3 2020-05-20 stsp if (err)
4957 0d6c6ee3 2020-05-20 stsp return err;
4958 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4959 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4960 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4961 0d6c6ee3 2020-05-20 stsp }
4962 0d6c6ee3 2020-05-20 stsp
4963 848d6979 2019-08-12 stsp modestr = "@";
4964 0d6c6ee3 2020-05-20 stsp }
4965 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4966 848d6979 2019-08-12 stsp modestr = "/";
4967 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4968 848d6979 2019-08-12 stsp modestr = "*";
4969 848d6979 2019-08-12 stsp
4970 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4971 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4972 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4973 0d6c6ee3 2020-05-20 stsp
4974 0d6c6ee3 2020-05-20 stsp free(link_target);
4975 0d6c6ee3 2020-05-20 stsp return NULL;
4976 c1669e2e 2019-01-09 stsp }
4977 c1669e2e 2019-01-09 stsp
4978 5de5890b 2018-10-18 stsp static const struct got_error *
4979 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4980 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4981 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4982 5de5890b 2018-10-18 stsp {
4983 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4984 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4985 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4986 56e0773d 2019-11-28 stsp int nentries, i;
4987 5de5890b 2018-10-18 stsp
4988 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4989 5de5890b 2018-10-18 stsp if (err)
4990 5de5890b 2018-10-18 stsp goto done;
4991 5de5890b 2018-10-18 stsp
4992 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4993 5de5890b 2018-10-18 stsp if (err)
4994 5de5890b 2018-10-18 stsp goto done;
4995 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4996 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4997 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4998 5de5890b 2018-10-18 stsp char *id = NULL;
4999 84453469 2018-11-11 stsp
5000 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
5001 84453469 2018-11-11 stsp break;
5002 84453469 2018-11-11 stsp
5003 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
5004 5de5890b 2018-10-18 stsp if (show_ids) {
5005 5de5890b 2018-10-18 stsp char *id_str;
5006 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
5007 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
5008 5de5890b 2018-10-18 stsp if (err)
5009 5de5890b 2018-10-18 stsp goto done;
5010 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
5011 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5012 5de5890b 2018-10-18 stsp free(id_str);
5013 5de5890b 2018-10-18 stsp goto done;
5014 5de5890b 2018-10-18 stsp }
5015 5de5890b 2018-10-18 stsp free(id_str);
5016 5de5890b 2018-10-18 stsp }
5017 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
5018 5de5890b 2018-10-18 stsp free(id);
5019 0d6c6ee3 2020-05-20 stsp if (err)
5020 0d6c6ee3 2020-05-20 stsp goto done;
5021 c1669e2e 2019-01-09 stsp
5022 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5023 c1669e2e 2019-01-09 stsp char *child_path;
5024 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
5025 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
5026 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
5027 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
5028 c1669e2e 2019-01-09 stsp goto done;
5029 c1669e2e 2019-01-09 stsp }
5030 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
5031 c1669e2e 2019-01-09 stsp root_path, repo);
5032 c1669e2e 2019-01-09 stsp free(child_path);
5033 c1669e2e 2019-01-09 stsp if (err)
5034 c1669e2e 2019-01-09 stsp goto done;
5035 c1669e2e 2019-01-09 stsp }
5036 5de5890b 2018-10-18 stsp }
5037 5de5890b 2018-10-18 stsp done:
5038 5de5890b 2018-10-18 stsp if (tree)
5039 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
5040 5de5890b 2018-10-18 stsp free(tree_id);
5041 5de5890b 2018-10-18 stsp return err;
5042 404c43c4 2018-06-21 stsp }
5043 404c43c4 2018-06-21 stsp
5044 5de5890b 2018-10-18 stsp static const struct got_error *
5045 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
5046 5de5890b 2018-10-18 stsp {
5047 5de5890b 2018-10-18 stsp const struct got_error *error;
5048 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
5049 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
5050 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
5051 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5052 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
5053 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
5054 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
5055 5de5890b 2018-10-18 stsp int ch;
5056 5de5890b 2018-10-18 stsp
5057 5de5890b 2018-10-18 stsp #ifndef PROFILE
5058 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5059 0f8d269b 2019-01-04 stsp NULL) == -1)
5060 5de5890b 2018-10-18 stsp err(1, "pledge");
5061 5de5890b 2018-10-18 stsp #endif
5062 5de5890b 2018-10-18 stsp
5063 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5064 5de5890b 2018-10-18 stsp switch (ch) {
5065 5de5890b 2018-10-18 stsp case 'c':
5066 5de5890b 2018-10-18 stsp commit_id_str = optarg;
5067 5de5890b 2018-10-18 stsp break;
5068 5de5890b 2018-10-18 stsp case 'r':
5069 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
5070 5de5890b 2018-10-18 stsp if (repo_path == NULL)
5071 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5072 9ba1d308 2019-10-21 stsp optarg);
5073 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5074 5de5890b 2018-10-18 stsp break;
5075 5de5890b 2018-10-18 stsp case 'i':
5076 5de5890b 2018-10-18 stsp show_ids = 1;
5077 5de5890b 2018-10-18 stsp break;
5078 c1669e2e 2019-01-09 stsp case 'R':
5079 c1669e2e 2019-01-09 stsp recurse = 1;
5080 c1669e2e 2019-01-09 stsp break;
5081 5de5890b 2018-10-18 stsp default:
5082 2deda0b9 2019-03-07 stsp usage_tree();
5083 5de5890b 2018-10-18 stsp /* NOTREACHED */
5084 5de5890b 2018-10-18 stsp }
5085 5de5890b 2018-10-18 stsp }
5086 5de5890b 2018-10-18 stsp
5087 5de5890b 2018-10-18 stsp argc -= optind;
5088 5de5890b 2018-10-18 stsp argv += optind;
5089 5de5890b 2018-10-18 stsp
5090 5de5890b 2018-10-18 stsp if (argc == 1)
5091 5de5890b 2018-10-18 stsp path = argv[0];
5092 5de5890b 2018-10-18 stsp else if (argc > 1)
5093 5de5890b 2018-10-18 stsp usage_tree();
5094 5de5890b 2018-10-18 stsp else
5095 7a2c19d6 2019-02-05 stsp path = NULL;
5096 7a2c19d6 2019-02-05 stsp
5097 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
5098 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
5099 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5100 9bf7a39b 2019-02-05 stsp goto done;
5101 9bf7a39b 2019-02-05 stsp }
5102 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
5103 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
5104 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5105 7a2c19d6 2019-02-05 stsp goto done;
5106 7a2c19d6 2019-02-05 stsp else
5107 7a2c19d6 2019-02-05 stsp error = NULL;
5108 7a2c19d6 2019-02-05 stsp if (worktree) {
5109 7a2c19d6 2019-02-05 stsp repo_path =
5110 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
5111 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
5112 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5113 7a2c19d6 2019-02-05 stsp if (error)
5114 7a2c19d6 2019-02-05 stsp goto done;
5115 7a2c19d6 2019-02-05 stsp } else {
5116 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
5117 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
5118 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5119 7a2c19d6 2019-02-05 stsp goto done;
5120 7a2c19d6 2019-02-05 stsp }
5121 5de5890b 2018-10-18 stsp }
5122 5de5890b 2018-10-18 stsp }
5123 5de5890b 2018-10-18 stsp
5124 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5125 5de5890b 2018-10-18 stsp if (error != NULL)
5126 c02c541e 2019-03-29 stsp goto done;
5127 c02c541e 2019-03-29 stsp
5128 4fedbf4c 2020-11-07 stsp if (worktree) {
5129 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
5130 4fedbf4c 2020-11-07 stsp char *p;
5131 5de5890b 2018-10-18 stsp
5132 4fedbf4c 2020-11-07 stsp if (path == NULL)
5133 4fedbf4c 2020-11-07 stsp path = "";
5134 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
5135 4fedbf4c 2020-11-07 stsp if (error)
5136 4fedbf4c 2020-11-07 stsp goto done;
5137 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
5138 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5139 4fedbf4c 2020-11-07 stsp p) == -1) {
5140 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
5141 9bf7a39b 2019-02-05 stsp free(p);
5142 4fedbf4c 2020-11-07 stsp goto done;
5143 4fedbf4c 2020-11-07 stsp }
5144 4fedbf4c 2020-11-07 stsp free(p);
5145 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5146 4fedbf4c 2020-11-07 stsp if (error)
5147 4fedbf4c 2020-11-07 stsp goto done;
5148 4fedbf4c 2020-11-07 stsp } else {
5149 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5150 4fedbf4c 2020-11-07 stsp if (error)
5151 4fedbf4c 2020-11-07 stsp goto done;
5152 4fedbf4c 2020-11-07 stsp if (path == NULL)
5153 9bf7a39b 2019-02-05 stsp path = "/";
5154 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
5155 9bf7a39b 2019-02-05 stsp if (error != NULL)
5156 9bf7a39b 2019-02-05 stsp goto done;
5157 9bf7a39b 2019-02-05 stsp }
5158 5de5890b 2018-10-18 stsp
5159 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
5160 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
5161 4e0a20a4 2020-03-23 tracey if (worktree)
5162 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
5163 4e0a20a4 2020-03-23 tracey else
5164 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
5165 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
5166 5de5890b 2018-10-18 stsp if (error != NULL)
5167 5de5890b 2018-10-18 stsp goto done;
5168 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5169 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
5170 5de5890b 2018-10-18 stsp if (error != NULL)
5171 5de5890b 2018-10-18 stsp goto done;
5172 5de5890b 2018-10-18 stsp } else {
5173 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5174 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5175 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5176 84de9106 2020-12-26 stsp NULL);
5177 84de9106 2020-12-26 stsp if (error)
5178 84de9106 2020-12-26 stsp goto done;
5179 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5180 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5181 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5182 30837e32 2019-07-25 stsp if (error)
5183 5de5890b 2018-10-18 stsp goto done;
5184 5de5890b 2018-10-18 stsp }
5185 5de5890b 2018-10-18 stsp
5186 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
5187 c1669e2e 2019-01-09 stsp in_repo_path, repo);
5188 5de5890b 2018-10-18 stsp done:
5189 5de5890b 2018-10-18 stsp free(in_repo_path);
5190 5de5890b 2018-10-18 stsp free(repo_path);
5191 5de5890b 2018-10-18 stsp free(cwd);
5192 5de5890b 2018-10-18 stsp free(commit_id);
5193 7a2c19d6 2019-02-05 stsp if (worktree)
5194 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
5195 5de5890b 2018-10-18 stsp if (repo) {
5196 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5197 5de5890b 2018-10-18 stsp if (error == NULL)
5198 1d0f4054 2021-06-17 stsp error = close_err;
5199 5de5890b 2018-10-18 stsp }
5200 5de5890b 2018-10-18 stsp return error;
5201 5de5890b 2018-10-18 stsp }
5202 5de5890b 2018-10-18 stsp
5203 6bad629b 2019-02-04 stsp __dead static void
5204 6bad629b 2019-02-04 stsp usage_status(void)
5205 6bad629b 2019-02-04 stsp {
5206 f6343036 2021-06-22 stsp fprintf(stderr, "usage: %s status [-I] [-s status-codes ] [path ...]\n",
5207 081470ac 2020-08-13 stsp getprogname());
5208 6bad629b 2019-02-04 stsp exit(1);
5209 6bad629b 2019-02-04 stsp }
5210 5c860e29 2018-03-12 stsp
5211 b72f483a 2019-02-05 stsp static const struct got_error *
5212 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
5213 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5214 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5215 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
5216 6bad629b 2019-02-04 stsp {
5217 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5218 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
5219 081470ac 2020-08-13 stsp if (arg) {
5220 081470ac 2020-08-13 stsp char *status_codes = arg;
5221 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
5222 081470ac 2020-08-13 stsp int i;
5223 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
5224 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
5225 081470ac 2020-08-13 stsp staged_status == status_codes[i])
5226 081470ac 2020-08-13 stsp break;
5227 081470ac 2020-08-13 stsp }
5228 081470ac 2020-08-13 stsp if (i == ncodes)
5229 081470ac 2020-08-13 stsp return NULL;
5230 081470ac 2020-08-13 stsp }
5231 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
5232 b72f483a 2019-02-05 stsp return NULL;
5233 6bad629b 2019-02-04 stsp }
5234 5c860e29 2018-03-12 stsp
5235 6bad629b 2019-02-04 stsp static const struct got_error *
5236 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
5237 6bad629b 2019-02-04 stsp {
5238 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
5239 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
5240 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
5241 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
5242 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
5243 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
5244 f6343036 2021-06-22 stsp int ch, i, no_ignores = 0;
5245 5c860e29 2018-03-12 stsp
5246 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
5247 72ea6654 2019-07-27 stsp
5248 f6343036 2021-06-22 stsp while ((ch = getopt(argc, argv, "Is:")) != -1) {
5249 6bad629b 2019-02-04 stsp switch (ch) {
5250 f6343036 2021-06-22 stsp case 'I':
5251 f6343036 2021-06-22 stsp no_ignores = 1;
5252 f6343036 2021-06-22 stsp break;
5253 081470ac 2020-08-13 stsp case 's':
5254 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
5255 081470ac 2020-08-13 stsp switch (optarg[i]) {
5256 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
5257 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
5258 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
5259 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
5260 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
5261 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
5262 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
5263 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
5264 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
5265 081470ac 2020-08-13 stsp break;
5266 081470ac 2020-08-13 stsp default:
5267 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5268 081470ac 2020-08-13 stsp optarg[i]);
5269 081470ac 2020-08-13 stsp }
5270 081470ac 2020-08-13 stsp }
5271 081470ac 2020-08-13 stsp status_codes = optarg;
5272 081470ac 2020-08-13 stsp break;
5273 5c860e29 2018-03-12 stsp default:
5274 2deda0b9 2019-03-07 stsp usage_status();
5275 6bad629b 2019-02-04 stsp /* NOTREACHED */
5276 5c860e29 2018-03-12 stsp }
5277 5c860e29 2018-03-12 stsp }
5278 5c860e29 2018-03-12 stsp
5279 6bad629b 2019-02-04 stsp argc -= optind;
5280 6bad629b 2019-02-04 stsp argv += optind;
5281 5c860e29 2018-03-12 stsp
5282 6bad629b 2019-02-04 stsp #ifndef PROFILE
5283 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5284 6bad629b 2019-02-04 stsp NULL) == -1)
5285 6bad629b 2019-02-04 stsp err(1, "pledge");
5286 f42b1b34 2018-03-12 stsp #endif
5287 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5288 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5289 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5290 927df6b7 2019-02-10 stsp goto done;
5291 927df6b7 2019-02-10 stsp }
5292 927df6b7 2019-02-10 stsp
5293 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5294 fa51e947 2020-03-27 stsp if (error) {
5295 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5296 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5297 a5edda0a 2019-07-27 stsp goto done;
5298 fa51e947 2020-03-27 stsp }
5299 6bad629b 2019-02-04 stsp
5300 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5301 c9956ddf 2019-09-08 stsp NULL);
5302 6bad629b 2019-02-04 stsp if (error != NULL)
5303 6bad629b 2019-02-04 stsp goto done;
5304 6bad629b 2019-02-04 stsp
5305 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5306 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5307 087fb88c 2019-08-04 stsp if (error)
5308 087fb88c 2019-08-04 stsp goto done;
5309 087fb88c 2019-08-04 stsp
5310 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5311 6bad629b 2019-02-04 stsp if (error)
5312 6bad629b 2019-02-04 stsp goto done;
5313 6bad629b 2019-02-04 stsp
5314 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, no_ignores,
5315 f6343036 2021-06-22 stsp print_status, status_codes, check_cancelled, NULL);
5316 6bad629b 2019-02-04 stsp done:
5317 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5318 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5319 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5320 927df6b7 2019-02-10 stsp free(cwd);
5321 d0eebce4 2019-03-11 stsp return error;
5322 d0eebce4 2019-03-11 stsp }
5323 d0eebce4 2019-03-11 stsp
5324 d0eebce4 2019-03-11 stsp __dead static void
5325 d0eebce4 2019-03-11 stsp usage_ref(void)
5326 d0eebce4 2019-03-11 stsp {
5327 d0eebce4 2019-03-11 stsp fprintf(stderr,
5328 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5329 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5330 d0eebce4 2019-03-11 stsp getprogname());
5331 d0eebce4 2019-03-11 stsp exit(1);
5332 d0eebce4 2019-03-11 stsp }
5333 d0eebce4 2019-03-11 stsp
5334 d0eebce4 2019-03-11 stsp static const struct got_error *
5335 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5336 d0eebce4 2019-03-11 stsp {
5337 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5338 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5339 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5340 d0eebce4 2019-03-11 stsp
5341 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5342 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5343 d0eebce4 2019-03-11 stsp if (err)
5344 d0eebce4 2019-03-11 stsp return err;
5345 d0eebce4 2019-03-11 stsp
5346 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
5347 d0eebce4 2019-03-11 stsp char *refstr;
5348 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5349 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5350 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5351 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5352 d0eebce4 2019-03-11 stsp free(refstr);
5353 d0eebce4 2019-03-11 stsp }
5354 d0eebce4 2019-03-11 stsp
5355 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5356 d0eebce4 2019-03-11 stsp return NULL;
5357 d0eebce4 2019-03-11 stsp }
5358 d0eebce4 2019-03-11 stsp
5359 d0eebce4 2019-03-11 stsp static const struct got_error *
5360 161728eb 2021-07-24 stsp delete_ref_by_name(struct got_repository *repo, const char *refname)
5361 d0eebce4 2019-03-11 stsp {
5362 161728eb 2021-07-24 stsp const struct got_error *err;
5363 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5364 d0eebce4 2019-03-11 stsp
5365 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5366 d0eebce4 2019-03-11 stsp if (err)
5367 d0eebce4 2019-03-11 stsp return err;
5368 993f033b 2021-07-16 stsp
5369 161728eb 2021-07-24 stsp err = delete_ref(repo, ref);
5370 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5371 d0eebce4 2019-03-11 stsp return err;
5372 d0eebce4 2019-03-11 stsp }
5373 d0eebce4 2019-03-11 stsp
5374 d0eebce4 2019-03-11 stsp static const struct got_error *
5375 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5376 d0eebce4 2019-03-11 stsp {
5377 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5378 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5379 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5380 d1644381 2019-07-14 stsp
5381 d1644381 2019-07-14 stsp /*
5382 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5383 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5384 d1644381 2019-07-14 stsp * an unintended typo.
5385 d1644381 2019-07-14 stsp */
5386 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5387 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5388 d0eebce4 2019-03-11 stsp
5389 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5390 dd88155e 2019-06-29 stsp repo);
5391 d83d9d5c 2019-05-13 stsp if (err) {
5392 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5393 d0eebce4 2019-03-11 stsp
5394 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5395 d83d9d5c 2019-05-13 stsp return err;
5396 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5397 d83d9d5c 2019-05-13 stsp if (err)
5398 d83d9d5c 2019-05-13 stsp return err;
5399 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5400 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5401 d83d9d5c 2019-05-13 stsp if (err)
5402 d83d9d5c 2019-05-13 stsp return err;
5403 d83d9d5c 2019-05-13 stsp }
5404 d83d9d5c 2019-05-13 stsp
5405 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5406 d0eebce4 2019-03-11 stsp if (err)
5407 d0eebce4 2019-03-11 stsp goto done;
5408 d0eebce4 2019-03-11 stsp
5409 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5410 d0eebce4 2019-03-11 stsp done:
5411 d0eebce4 2019-03-11 stsp if (ref)
5412 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5413 d0eebce4 2019-03-11 stsp free(id);
5414 d1c1ae5f 2019-08-12 stsp return err;
5415 d1c1ae5f 2019-08-12 stsp }
5416 d1c1ae5f 2019-08-12 stsp
5417 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5418 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5419 d1c1ae5f 2019-08-12 stsp {
5420 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5421 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5422 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5423 d1c1ae5f 2019-08-12 stsp
5424 d1c1ae5f 2019-08-12 stsp /*
5425 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5426 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5427 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5428 d1c1ae5f 2019-08-12 stsp */
5429 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5430 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5431 d1c1ae5f 2019-08-12 stsp
5432 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5433 d1c1ae5f 2019-08-12 stsp if (err)
5434 d1c1ae5f 2019-08-12 stsp return err;
5435 d1c1ae5f 2019-08-12 stsp
5436 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5437 d1c1ae5f 2019-08-12 stsp if (err)
5438 d1c1ae5f 2019-08-12 stsp goto done;
5439 d1c1ae5f 2019-08-12 stsp
5440 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5441 d1c1ae5f 2019-08-12 stsp done:
5442 d1c1ae5f 2019-08-12 stsp if (target_ref)
5443 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5444 d1c1ae5f 2019-08-12 stsp if (ref)
5445 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5446 d0eebce4 2019-03-11 stsp return err;
5447 d0eebce4 2019-03-11 stsp }
5448 d0eebce4 2019-03-11 stsp
5449 d0eebce4 2019-03-11 stsp static const struct got_error *
5450 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5451 d0eebce4 2019-03-11 stsp {
5452 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5453 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5454 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5455 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5456 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5457 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5458 b2070a3f 2020-03-22 stsp char *refname = NULL;
5459 d0eebce4 2019-03-11 stsp
5460 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5461 d0eebce4 2019-03-11 stsp switch (ch) {
5462 e31abbf2 2020-03-22 stsp case 'c':
5463 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5464 e31abbf2 2020-03-22 stsp break;
5465 d0eebce4 2019-03-11 stsp case 'd':
5466 e31abbf2 2020-03-22 stsp do_delete = 1;
5467 d0eebce4 2019-03-11 stsp break;
5468 d0eebce4 2019-03-11 stsp case 'r':
5469 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5470 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5471 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5472 9ba1d308 2019-10-21 stsp optarg);
5473 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5474 d0eebce4 2019-03-11 stsp break;
5475 d0eebce4 2019-03-11 stsp case 'l':
5476 d0eebce4 2019-03-11 stsp do_list = 1;
5477 d1c1ae5f 2019-08-12 stsp break;
5478 d1c1ae5f 2019-08-12 stsp case 's':
5479 e31abbf2 2020-03-22 stsp symref_target = optarg;
5480 d0eebce4 2019-03-11 stsp break;
5481 d0eebce4 2019-03-11 stsp default:
5482 d0eebce4 2019-03-11 stsp usage_ref();
5483 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5484 d0eebce4 2019-03-11 stsp }
5485 d0eebce4 2019-03-11 stsp }
5486 d0eebce4 2019-03-11 stsp
5487 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5488 ff69268e 2020-12-13 stsp option_conflict('c', 'l');
5489 ff69268e 2020-12-13 stsp if (obj_arg && do_delete)
5490 ff69268e 2020-12-13 stsp option_conflict('c', 'd');
5491 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5492 ff69268e 2020-12-13 stsp option_conflict('c', 's');
5493 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5494 ff69268e 2020-12-13 stsp option_conflict('s', 'd');
5495 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5496 ff69268e 2020-12-13 stsp option_conflict('s', 'l');
5497 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5498 ff69268e 2020-12-13 stsp option_conflict('d', 'l');
5499 d0eebce4 2019-03-11 stsp
5500 d0eebce4 2019-03-11 stsp argc -= optind;
5501 d0eebce4 2019-03-11 stsp argv += optind;
5502 d0eebce4 2019-03-11 stsp
5503 e31abbf2 2020-03-22 stsp if (do_list) {
5504 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5505 d0eebce4 2019-03-11 stsp usage_ref();
5506 b2070a3f 2020-03-22 stsp if (argc == 1) {
5507 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5508 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5509 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5510 b2070a3f 2020-03-22 stsp goto done;
5511 b2070a3f 2020-03-22 stsp }
5512 b2070a3f 2020-03-22 stsp }
5513 e31abbf2 2020-03-22 stsp } else {
5514 e31abbf2 2020-03-22 stsp if (argc != 1)
5515 e31abbf2 2020-03-22 stsp usage_ref();
5516 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5517 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5518 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5519 b2070a3f 2020-03-22 stsp goto done;
5520 b2070a3f 2020-03-22 stsp }
5521 e31abbf2 2020-03-22 stsp }
5522 b2070a3f 2020-03-22 stsp
5523 b2070a3f 2020-03-22 stsp if (refname)
5524 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5525 d0eebce4 2019-03-11 stsp
5526 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5527 e0b57350 2019-03-12 stsp if (do_list) {
5528 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5529 e0b57350 2019-03-12 stsp NULL) == -1)
5530 e0b57350 2019-03-12 stsp err(1, "pledge");
5531 e0b57350 2019-03-12 stsp } else {
5532 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5533 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5534 e0b57350 2019-03-12 stsp err(1, "pledge");
5535 e0b57350 2019-03-12 stsp }
5536 d0eebce4 2019-03-11 stsp #endif
5537 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5538 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5539 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5540 d0eebce4 2019-03-11 stsp goto done;
5541 d0eebce4 2019-03-11 stsp }
5542 d0eebce4 2019-03-11 stsp
5543 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5544 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5545 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5546 d0eebce4 2019-03-11 stsp goto done;
5547 d0eebce4 2019-03-11 stsp else
5548 d0eebce4 2019-03-11 stsp error = NULL;
5549 d0eebce4 2019-03-11 stsp if (worktree) {
5550 d0eebce4 2019-03-11 stsp repo_path =
5551 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5552 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5553 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5554 d0eebce4 2019-03-11 stsp if (error)
5555 d0eebce4 2019-03-11 stsp goto done;
5556 d0eebce4 2019-03-11 stsp } else {
5557 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5558 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5559 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5560 d0eebce4 2019-03-11 stsp goto done;
5561 d0eebce4 2019-03-11 stsp }
5562 d0eebce4 2019-03-11 stsp }
5563 d0eebce4 2019-03-11 stsp }
5564 d0eebce4 2019-03-11 stsp
5565 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5566 d0eebce4 2019-03-11 stsp if (error != NULL)
5567 d0eebce4 2019-03-11 stsp goto done;
5568 d0eebce4 2019-03-11 stsp
5569 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5570 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5571 c02c541e 2019-03-29 stsp if (error)
5572 c02c541e 2019-03-29 stsp goto done;
5573 c02c541e 2019-03-29 stsp
5574 d0eebce4 2019-03-11 stsp if (do_list)
5575 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5576 e31abbf2 2020-03-22 stsp else if (do_delete)
5577 161728eb 2021-07-24 stsp error = delete_ref_by_name(repo, refname);
5578 e31abbf2 2020-03-22 stsp else if (symref_target)
5579 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5580 e31abbf2 2020-03-22 stsp else {
5581 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5582 e31abbf2 2020-03-22 stsp usage_ref();
5583 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5584 e31abbf2 2020-03-22 stsp }
5585 4e759de4 2019-06-26 stsp done:
5586 b2070a3f 2020-03-22 stsp free(refname);
5587 1d0f4054 2021-06-17 stsp if (repo) {
5588 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
5589 1d0f4054 2021-06-17 stsp if (error == NULL)
5590 1d0f4054 2021-06-17 stsp error = close_err;
5591 1d0f4054 2021-06-17 stsp }
5592 4e759de4 2019-06-26 stsp if (worktree)
5593 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5594 4e759de4 2019-06-26 stsp free(cwd);
5595 4e759de4 2019-06-26 stsp free(repo_path);
5596 4e759de4 2019-06-26 stsp return error;
5597 4e759de4 2019-06-26 stsp }
5598 4e759de4 2019-06-26 stsp
5599 4e759de4 2019-06-26 stsp __dead static void
5600 4e759de4 2019-06-26 stsp usage_branch(void)
5601 4e759de4 2019-06-26 stsp {
5602 4e759de4 2019-06-26 stsp fprintf(stderr,
5603 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5604 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5605 4e759de4 2019-06-26 stsp exit(1);
5606 4e759de4 2019-06-26 stsp }
5607 4e759de4 2019-06-26 stsp
5608 4e759de4 2019-06-26 stsp static const struct got_error *
5609 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5610 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5611 4e99b47e 2019-10-04 stsp {
5612 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5613 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5614 4e99b47e 2019-10-04 stsp char *refstr;
5615 4e99b47e 2019-10-04 stsp
5616 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5617 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5618 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5619 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5620 4e99b47e 2019-10-04 stsp
5621 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5622 4e99b47e 2019-10-04 stsp if (err)
5623 4e99b47e 2019-10-04 stsp return err;
5624 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5625 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5626 4e99b47e 2019-10-04 stsp marker = "* ";
5627 4e99b47e 2019-10-04 stsp else
5628 4e99b47e 2019-10-04 stsp marker = "~ ";
5629 4e99b47e 2019-10-04 stsp free(id);
5630 4e99b47e 2019-10-04 stsp }
5631 4e99b47e 2019-10-04 stsp
5632 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5633 4e99b47e 2019-10-04 stsp refname += 11;
5634 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5635 4e99b47e 2019-10-04 stsp refname += 18;
5636 34d4e04c 2021-02-08 stsp if (strncmp(refname, "refs/remotes/", 13) == 0)
5637 34d4e04c 2021-02-08 stsp refname += 13;
5638 4e99b47e 2019-10-04 stsp
5639 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5640 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5641 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5642 4e99b47e 2019-10-04 stsp
5643 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5644 4e99b47e 2019-10-04 stsp free(refstr);
5645 4e99b47e 2019-10-04 stsp return NULL;
5646 4e99b47e 2019-10-04 stsp }
5647 4e99b47e 2019-10-04 stsp
5648 4e99b47e 2019-10-04 stsp static const struct got_error *
5649 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5650 ad89fa31 2019-10-04 stsp {
5651 ad89fa31 2019-10-04 stsp const char *refname;
5652 ad89fa31 2019-10-04 stsp
5653 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5654 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5655 ad89fa31 2019-10-04 stsp
5656 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5657 ad89fa31 2019-10-04 stsp
5658 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5659 ad89fa31 2019-10-04 stsp refname += 11;
5660 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5661 ad89fa31 2019-10-04 stsp refname += 18;
5662 ad89fa31 2019-10-04 stsp
5663 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5664 ad89fa31 2019-10-04 stsp
5665 ad89fa31 2019-10-04 stsp return NULL;
5666 ad89fa31 2019-10-04 stsp }
5667 ad89fa31 2019-10-04 stsp
5668 ad89fa31 2019-10-04 stsp static const struct got_error *
5669 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5670 4e759de4 2019-06-26 stsp {
5671 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5672 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5673 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5674 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5675 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5676 4e759de4 2019-06-26 stsp
5677 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5678 ba882ee3 2019-07-11 stsp
5679 4e99b47e 2019-10-04 stsp if (worktree) {
5680 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5681 4e99b47e 2019-10-04 stsp worktree);
5682 4e99b47e 2019-10-04 stsp if (err)
5683 4e99b47e 2019-10-04 stsp return err;
5684 4e99b47e 2019-10-04 stsp
5685 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_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 if (rebase_in_progress || histedit_in_progress) {
5691 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5692 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5693 4e99b47e 2019-10-04 stsp if (err)
5694 4e99b47e 2019-10-04 stsp return err;
5695 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5696 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5697 4e99b47e 2019-10-04 stsp }
5698 4e99b47e 2019-10-04 stsp }
5699 4e99b47e 2019-10-04 stsp
5700 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5701 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5702 4e759de4 2019-06-26 stsp if (err)
5703 4e759de4 2019-06-26 stsp return err;
5704 4e759de4 2019-06-26 stsp
5705 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry)
5706 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5707 4e759de4 2019-06-26 stsp
5708 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5709 34d4e04c 2021-02-08 stsp
5710 34d4e04c 2021-02-08 stsp err = got_ref_list(&refs, repo, "refs/remotes",
5711 34d4e04c 2021-02-08 stsp got_ref_cmp_by_name, NULL);
5712 34d4e04c 2021-02-08 stsp if (err)
5713 34d4e04c 2021-02-08 stsp return err;
5714 34d4e04c 2021-02-08 stsp
5715 34d4e04c 2021-02-08 stsp TAILQ_FOREACH(re, &refs, entry)
5716 34d4e04c 2021-02-08 stsp list_branch(repo, worktree, re->ref);
5717 34d4e04c 2021-02-08 stsp
5718 34d4e04c 2021-02-08 stsp got_ref_list_free(&refs);
5719 34d4e04c 2021-02-08 stsp
5720 4e759de4 2019-06-26 stsp return NULL;
5721 4e759de4 2019-06-26 stsp }
5722 4e759de4 2019-06-26 stsp
5723 4e759de4 2019-06-26 stsp static const struct got_error *
5724 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5725 45cd4e47 2019-08-25 stsp const char *branch_name)
5726 4e759de4 2019-06-26 stsp {
5727 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5728 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5729 2f1457c6 2021-08-27 stsp char *refname, *remote_refname = NULL;
5730 4e759de4 2019-06-26 stsp
5731 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/", 5) == 0)
5732 2f1457c6 2021-08-27 stsp branch_name += 5;
5733 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "heads/", 6) == 0)
5734 2f1457c6 2021-08-27 stsp branch_name += 6;
5735 2f1457c6 2021-08-27 stsp else if (strncmp(branch_name, "remotes/", 8) == 0)
5736 2f1457c6 2021-08-27 stsp branch_name += 8;
5737 2f1457c6 2021-08-27 stsp
5738 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5739 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5740 4e759de4 2019-06-26 stsp
5741 2f1457c6 2021-08-27 stsp if (asprintf(&remote_refname, "refs/remotes/%s",
5742 2f1457c6 2021-08-27 stsp branch_name) == -1) {
5743 2f1457c6 2021-08-27 stsp err = got_error_from_errno("asprintf");
5744 4e759de4 2019-06-26 stsp goto done;
5745 2f1457c6 2021-08-27 stsp }
5746 4e759de4 2019-06-26 stsp
5747 2f1457c6 2021-08-27 stsp err = got_ref_open(&ref, repo, refname, 0);
5748 2f1457c6 2021-08-27 stsp if (err) {
5749 2f1457c6 2021-08-27 stsp const struct got_error *err2;
5750 2f1457c6 2021-08-27 stsp if (err->code != GOT_ERR_NOT_REF)
5751 2f1457c6 2021-08-27 stsp goto done;
5752 2f1457c6 2021-08-27 stsp /*
5753 2f1457c6 2021-08-27 stsp * Keep 'err' intact such that if neither branch exists
5754 2f1457c6 2021-08-27 stsp * we report "refs/heads" rather than "refs/remotes" in
5755 2f1457c6 2021-08-27 stsp * our error message.
5756 2f1457c6 2021-08-27 stsp */
5757 2f1457c6 2021-08-27 stsp err2 = got_ref_open(&ref, repo, remote_refname, 0);
5758 2f1457c6 2021-08-27 stsp if (err2)
5759 2f1457c6 2021-08-27 stsp goto done;
5760 2f1457c6 2021-08-27 stsp err = NULL;
5761 2f1457c6 2021-08-27 stsp }
5762 2f1457c6 2021-08-27 stsp
5763 45cd4e47 2019-08-25 stsp if (worktree &&
5764 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5765 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5766 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5767 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5768 45cd4e47 2019-08-25 stsp goto done;
5769 45cd4e47 2019-08-25 stsp }
5770 45cd4e47 2019-08-25 stsp
5771 978a28a1 2021-09-04 naddy err = delete_ref(repo, ref);
5772 4e759de4 2019-06-26 stsp done:
5773 45cd4e47 2019-08-25 stsp if (ref)
5774 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5775 4e759de4 2019-06-26 stsp free(refname);
5776 2f1457c6 2021-08-27 stsp free(remote_refname);
5777 4e759de4 2019-06-26 stsp return err;
5778 4e759de4 2019-06-26 stsp }
5779 4e759de4 2019-06-26 stsp
5780 4e759de4 2019-06-26 stsp static const struct got_error *
5781 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5782 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5783 4e759de4 2019-06-26 stsp {
5784 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5785 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5786 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5787 d3f84d51 2019-07-11 stsp
5788 d3f84d51 2019-07-11 stsp /*
5789 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5790 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5791 d3f84d51 2019-07-11 stsp * an unintended typo.
5792 d3f84d51 2019-07-11 stsp */
5793 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5794 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5795 2f1457c6 2021-08-27 stsp
5796 2f1457c6 2021-08-27 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
5797 2f1457c6 2021-08-27 stsp branch_name += 11;
5798 4e759de4 2019-06-26 stsp
5799 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5800 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5801 62d463ca 2020-10-20 naddy goto done;
5802 4e759de4 2019-06-26 stsp }
5803 4e759de4 2019-06-26 stsp
5804 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5805 4e759de4 2019-06-26 stsp if (err == NULL) {
5806 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5807 4e759de4 2019-06-26 stsp goto done;
5808 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5809 4e759de4 2019-06-26 stsp goto done;
5810 4e759de4 2019-06-26 stsp
5811 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5812 4e759de4 2019-06-26 stsp if (err)
5813 4e759de4 2019-06-26 stsp goto done;
5814 4e759de4 2019-06-26 stsp
5815 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5816 d0eebce4 2019-03-11 stsp done:
5817 4e759de4 2019-06-26 stsp if (ref)
5818 4e759de4 2019-06-26 stsp got_ref_close(ref);
5819 4e759de4 2019-06-26 stsp free(base_refname);
5820 4e759de4 2019-06-26 stsp free(refname);
5821 4e759de4 2019-06-26 stsp return err;
5822 4e759de4 2019-06-26 stsp }
5823 4e759de4 2019-06-26 stsp
5824 4e759de4 2019-06-26 stsp static const struct got_error *
5825 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5826 4e759de4 2019-06-26 stsp {
5827 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5828 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5829 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5830 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5831 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5832 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5833 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5834 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5835 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5836 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5837 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5838 4e759de4 2019-06-26 stsp
5839 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5840 da76fce2 2020-02-24 stsp
5841 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5842 4e759de4 2019-06-26 stsp switch (ch) {
5843 a74f7e83 2019-11-10 stsp case 'c':
5844 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5845 a74f7e83 2019-11-10 stsp break;
5846 4e759de4 2019-06-26 stsp case 'd':
5847 4e759de4 2019-06-26 stsp delref = optarg;
5848 4e759de4 2019-06-26 stsp break;
5849 4e759de4 2019-06-26 stsp case 'r':
5850 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5851 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5852 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5853 9ba1d308 2019-10-21 stsp optarg);
5854 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5855 4e759de4 2019-06-26 stsp break;
5856 4e759de4 2019-06-26 stsp case 'l':
5857 4e759de4 2019-06-26 stsp do_list = 1;
5858 da76fce2 2020-02-24 stsp break;
5859 da76fce2 2020-02-24 stsp case 'n':
5860 da76fce2 2020-02-24 stsp do_update = 0;
5861 4e759de4 2019-06-26 stsp break;
5862 4e759de4 2019-06-26 stsp default:
5863 4e759de4 2019-06-26 stsp usage_branch();
5864 4e759de4 2019-06-26 stsp /* NOTREACHED */
5865 4e759de4 2019-06-26 stsp }
5866 4e759de4 2019-06-26 stsp }
5867 4e759de4 2019-06-26 stsp
5868 4e759de4 2019-06-26 stsp if (do_list && delref)
5869 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
5870 4e759de4 2019-06-26 stsp
5871 4e759de4 2019-06-26 stsp argc -= optind;
5872 4e759de4 2019-06-26 stsp argv += optind;
5873 ad89fa31 2019-10-04 stsp
5874 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5875 ad89fa31 2019-10-04 stsp do_show = 1;
5876 4e759de4 2019-06-26 stsp
5877 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5878 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5879 a74f7e83 2019-11-10 stsp
5880 4e759de4 2019-06-26 stsp if (do_list || delref) {
5881 4e759de4 2019-06-26 stsp if (argc > 0)
5882 4e759de4 2019-06-26 stsp usage_branch();
5883 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5884 4e759de4 2019-06-26 stsp usage_branch();
5885 4e759de4 2019-06-26 stsp
5886 4e759de4 2019-06-26 stsp #ifndef PROFILE
5887 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5888 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5889 4e759de4 2019-06-26 stsp NULL) == -1)
5890 4e759de4 2019-06-26 stsp err(1, "pledge");
5891 4e759de4 2019-06-26 stsp } else {
5892 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5893 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5894 4e759de4 2019-06-26 stsp err(1, "pledge");
5895 4e759de4 2019-06-26 stsp }
5896 4e759de4 2019-06-26 stsp #endif
5897 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5898 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5899 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5900 4e759de4 2019-06-26 stsp goto done;
5901 4e759de4 2019-06-26 stsp }
5902 4e759de4 2019-06-26 stsp
5903 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5904 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5905 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5906 4e759de4 2019-06-26 stsp goto done;
5907 4e759de4 2019-06-26 stsp else
5908 4e759de4 2019-06-26 stsp error = NULL;
5909 4e759de4 2019-06-26 stsp if (worktree) {
5910 4e759de4 2019-06-26 stsp repo_path =
5911 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5912 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5913 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5914 4e759de4 2019-06-26 stsp if (error)
5915 4e759de4 2019-06-26 stsp goto done;
5916 4e759de4 2019-06-26 stsp } else {
5917 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5918 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5919 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5920 4e759de4 2019-06-26 stsp goto done;
5921 4e759de4 2019-06-26 stsp }
5922 4e759de4 2019-06-26 stsp }
5923 4e759de4 2019-06-26 stsp }
5924 4e759de4 2019-06-26 stsp
5925 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5926 4e759de4 2019-06-26 stsp if (error != NULL)
5927 4e759de4 2019-06-26 stsp goto done;
5928 4e759de4 2019-06-26 stsp
5929 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5930 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5931 4e759de4 2019-06-26 stsp if (error)
5932 4e759de4 2019-06-26 stsp goto done;
5933 4e759de4 2019-06-26 stsp
5934 ad89fa31 2019-10-04 stsp if (do_show)
5935 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5936 ad89fa31 2019-10-04 stsp else if (do_list)
5937 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5938 4e759de4 2019-06-26 stsp else if (delref)
5939 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5940 4e759de4 2019-06-26 stsp else {
5941 84de9106 2020-12-26 stsp struct got_reflist_head refs;
5942 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
5943 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5944 84de9106 2020-12-26 stsp NULL);
5945 84de9106 2020-12-26 stsp if (error)
5946 84de9106 2020-12-26 stsp goto done;
5947 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5948 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5949 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5950 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5951 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5952 84de9106 2020-12-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5953 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
5954 a74f7e83 2019-11-10 stsp if (error)
5955 a74f7e83 2019-11-10 stsp goto done;
5956 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5957 da76fce2 2020-02-24 stsp if (error)
5958 da76fce2 2020-02-24 stsp goto done;
5959 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5960 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5961 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5962 da76fce2 2020-02-24 stsp
5963 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5964 da76fce2 2020-02-24 stsp if (error)
5965 da76fce2 2020-02-24 stsp goto done;
5966 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5967 da76fce2 2020-02-24 stsp worktree);
5968 da76fce2 2020-02-24 stsp if (error)
5969 da76fce2 2020-02-24 stsp goto done;
5970 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5971 da76fce2 2020-02-24 stsp == -1) {
5972 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5973 da76fce2 2020-02-24 stsp goto done;
5974 da76fce2 2020-02-24 stsp }
5975 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5976 da76fce2 2020-02-24 stsp free(branch_refname);
5977 da76fce2 2020-02-24 stsp if (error)
5978 da76fce2 2020-02-24 stsp goto done;
5979 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5980 da76fce2 2020-02-24 stsp repo);
5981 da76fce2 2020-02-24 stsp if (error)
5982 da76fce2 2020-02-24 stsp goto done;
5983 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5984 da76fce2 2020-02-24 stsp commit_id);
5985 da76fce2 2020-02-24 stsp if (error)
5986 da76fce2 2020-02-24 stsp goto done;
5987 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5988 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5989 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5990 9627c110 2020-04-18 stsp NULL);
5991 da76fce2 2020-02-24 stsp if (error)
5992 da76fce2 2020-02-24 stsp goto done;
5993 9627c110 2020-04-18 stsp if (upa.did_something)
5994 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5995 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5996 da76fce2 2020-02-24 stsp }
5997 4e759de4 2019-06-26 stsp }
5998 4e759de4 2019-06-26 stsp done:
5999 da76fce2 2020-02-24 stsp if (ref)
6000 da76fce2 2020-02-24 stsp got_ref_close(ref);
6001 1d0f4054 2021-06-17 stsp if (repo) {
6002 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6003 1d0f4054 2021-06-17 stsp if (error == NULL)
6004 1d0f4054 2021-06-17 stsp error = close_err;
6005 1d0f4054 2021-06-17 stsp }
6006 d0eebce4 2019-03-11 stsp if (worktree)
6007 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
6008 d0eebce4 2019-03-11 stsp free(cwd);
6009 d0eebce4 2019-03-11 stsp free(repo_path);
6010 da76fce2 2020-02-24 stsp free(commit_id);
6011 da76fce2 2020-02-24 stsp free(commit_id_str);
6012 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
6013 da76fce2 2020-02-24 stsp free((char *)pe->path);
6014 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
6015 d00136be 2019-03-26 stsp return error;
6016 d00136be 2019-03-26 stsp }
6017 d00136be 2019-03-26 stsp
6018 8e7bd50a 2019-08-22 stsp
6019 d00136be 2019-03-26 stsp __dead static void
6020 8e7bd50a 2019-08-22 stsp usage_tag(void)
6021 8e7bd50a 2019-08-22 stsp {
6022 8e7bd50a 2019-08-22 stsp fprintf(stderr,
6023 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
6024 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
6025 8e7bd50a 2019-08-22 stsp exit(1);
6026 b8bad2ba 2019-08-23 stsp }
6027 b8bad2ba 2019-08-23 stsp
6028 b8bad2ba 2019-08-23 stsp #if 0
6029 b8bad2ba 2019-08-23 stsp static const struct got_error *
6030 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6031 b8bad2ba 2019-08-23 stsp {
6032 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
6033 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
6034 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
6035 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
6036 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
6037 b8bad2ba 2019-08-23 stsp
6038 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(re, tags, entry) {
6039 dbdddfee 2021-06-23 naddy se = STAILQ_FIRST(sorted);
6040 b8bad2ba 2019-08-23 stsp if (se == NULL) {
6041 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6042 b8bad2ba 2019-08-23 stsp if (err)
6043 b8bad2ba 2019-08-23 stsp return err;
6044 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(sorted, new, entry);
6045 b8bad2ba 2019-08-23 stsp continue;
6046 b8bad2ba 2019-08-23 stsp } else {
6047 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
6048 b8bad2ba 2019-08-23 stsp if (err)
6049 b8bad2ba 2019-08-23 stsp break;
6050 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
6051 b8bad2ba 2019-08-23 stsp free(re_id);
6052 b8bad2ba 2019-08-23 stsp if (err)
6053 b8bad2ba 2019-08-23 stsp break;
6054 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
6055 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
6056 b8bad2ba 2019-08-23 stsp }
6057 b8bad2ba 2019-08-23 stsp
6058 b8bad2ba 2019-08-23 stsp while (se) {
6059 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
6060 b8bad2ba 2019-08-23 stsp if (err)
6061 b8bad2ba 2019-08-23 stsp break;
6062 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
6063 b8bad2ba 2019-08-23 stsp free(se_id);
6064 b8bad2ba 2019-08-23 stsp if (err)
6065 b8bad2ba 2019-08-23 stsp break;
6066 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
6067 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
6068 b8bad2ba 2019-08-23 stsp
6069 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
6070 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
6071 b8bad2ba 2019-08-23 stsp if (err)
6072 b8bad2ba 2019-08-23 stsp return err;
6073 dbdddfee 2021-06-23 naddy STAILQ_INSERT_AFTER(sorted, se, new, entry);
6074 b8bad2ba 2019-08-23 stsp break;
6075 b8bad2ba 2019-08-23 stsp }
6076 dbdddfee 2021-06-23 naddy se = STAILQ_NEXT(se, entry);
6077 b8bad2ba 2019-08-23 stsp continue;
6078 b8bad2ba 2019-08-23 stsp }
6079 b8bad2ba 2019-08-23 stsp }
6080 b8bad2ba 2019-08-23 stsp done:
6081 b8bad2ba 2019-08-23 stsp return err;
6082 8e7bd50a 2019-08-22 stsp }
6083 b8bad2ba 2019-08-23 stsp #endif
6084 b8bad2ba 2019-08-23 stsp
6085 b8bad2ba 2019-08-23 stsp static const struct got_error *
6086 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
6087 8e7bd50a 2019-08-22 stsp {
6088 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
6089 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
6090 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
6091 8e7bd50a 2019-08-22 stsp
6092 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6093 8e7bd50a 2019-08-22 stsp
6094 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6095 8e7bd50a 2019-08-22 stsp if (err)
6096 8e7bd50a 2019-08-22 stsp return err;
6097 8e7bd50a 2019-08-22 stsp
6098 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &refs, entry) {
6099 8e7bd50a 2019-08-22 stsp const char *refname;
6100 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6101 8e7bd50a 2019-08-22 stsp char datebuf[26];
6102 d4efa91b 2020-01-14 stsp const char *tagger;
6103 8e7bd50a 2019-08-22 stsp time_t tagger_time;
6104 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
6105 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
6106 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
6107 8e7bd50a 2019-08-22 stsp
6108 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
6109 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
6110 8e7bd50a 2019-08-22 stsp continue;
6111 8e7bd50a 2019-08-22 stsp refname += 10;
6112 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
6113 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
6114 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
6115 8e7bd50a 2019-08-22 stsp break;
6116 8e7bd50a 2019-08-22 stsp }
6117 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6118 8e7bd50a 2019-08-22 stsp free(refstr);
6119 8e7bd50a 2019-08-22 stsp
6120 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
6121 8e7bd50a 2019-08-22 stsp if (err)
6122 8e7bd50a 2019-08-22 stsp break;
6123 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6124 d4efa91b 2020-01-14 stsp if (err) {
6125 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
6126 d4efa91b 2020-01-14 stsp free(id);
6127 d4efa91b 2020-01-14 stsp break;
6128 d4efa91b 2020-01-14 stsp }
6129 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
6130 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
6131 d4efa91b 2020-01-14 stsp if (err) {
6132 d4efa91b 2020-01-14 stsp free(id);
6133 d4efa91b 2020-01-14 stsp break;
6134 d4efa91b 2020-01-14 stsp }
6135 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
6136 d4efa91b 2020-01-14 stsp tagger_time =
6137 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
6138 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
6139 d4efa91b 2020-01-14 stsp free(id);
6140 d4efa91b 2020-01-14 stsp if (err)
6141 d4efa91b 2020-01-14 stsp break;
6142 d4efa91b 2020-01-14 stsp } else {
6143 d4efa91b 2020-01-14 stsp free(id);
6144 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
6145 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
6146 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
6147 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
6148 d4efa91b 2020-01-14 stsp if (err)
6149 d4efa91b 2020-01-14 stsp break;
6150 d4efa91b 2020-01-14 stsp }
6151 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
6152 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
6153 2417344c 2019-08-23 stsp if (datestr)
6154 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
6155 d4efa91b 2020-01-14 stsp if (commit)
6156 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6157 d4efa91b 2020-01-14 stsp else {
6158 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
6159 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
6160 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6161 d4efa91b 2020-01-14 stsp id_str);
6162 d4efa91b 2020-01-14 stsp break;
6163 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
6164 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6165 d4efa91b 2020-01-14 stsp id_str);
6166 d4efa91b 2020-01-14 stsp break;
6167 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
6168 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6169 d4efa91b 2020-01-14 stsp id_str);
6170 d4efa91b 2020-01-14 stsp break;
6171 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
6172 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6173 d4efa91b 2020-01-14 stsp id_str);
6174 d4efa91b 2020-01-14 stsp break;
6175 d4efa91b 2020-01-14 stsp default:
6176 d4efa91b 2020-01-14 stsp break;
6177 d4efa91b 2020-01-14 stsp }
6178 8e7bd50a 2019-08-22 stsp }
6179 8e7bd50a 2019-08-22 stsp free(id_str);
6180 d4efa91b 2020-01-14 stsp if (commit) {
6181 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
6182 d4efa91b 2020-01-14 stsp if (err)
6183 d4efa91b 2020-01-14 stsp break;
6184 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
6185 d4efa91b 2020-01-14 stsp } else {
6186 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
6187 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
6188 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
6189 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
6190 d4efa91b 2020-01-14 stsp break;
6191 d4efa91b 2020-01-14 stsp }
6192 8e7bd50a 2019-08-22 stsp }
6193 8e7bd50a 2019-08-22 stsp
6194 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
6195 8e7bd50a 2019-08-22 stsp do {
6196 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
6197 8e7bd50a 2019-08-22 stsp if (line)
6198 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
6199 8e7bd50a 2019-08-22 stsp } while (line);
6200 8e7bd50a 2019-08-22 stsp free(tagmsg0);
6201 8e7bd50a 2019-08-22 stsp }
6202 8e7bd50a 2019-08-22 stsp
6203 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
6204 8e7bd50a 2019-08-22 stsp return NULL;
6205 8e7bd50a 2019-08-22 stsp }
6206 8e7bd50a 2019-08-22 stsp
6207 8e7bd50a 2019-08-22 stsp static const struct got_error *
6208 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6209 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
6210 8e7bd50a 2019-08-22 stsp {
6211 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6212 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
6213 f372d5cd 2019-10-21 stsp char *editor = NULL;
6214 1601cb9f 2020-09-11 naddy int initial_content_len;
6215 8e7bd50a 2019-08-22 stsp int fd = -1;
6216 8e7bd50a 2019-08-22 stsp
6217 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6218 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6219 8e7bd50a 2019-08-22 stsp goto done;
6220 8e7bd50a 2019-08-22 stsp }
6221 8e7bd50a 2019-08-22 stsp
6222 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6223 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
6224 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
6225 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
6226 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
6227 8e7bd50a 2019-08-22 stsp goto done;
6228 8e7bd50a 2019-08-22 stsp }
6229 8e7bd50a 2019-08-22 stsp
6230 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6231 8e7bd50a 2019-08-22 stsp if (err)
6232 8e7bd50a 2019-08-22 stsp goto done;
6233 8e7bd50a 2019-08-22 stsp
6234 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6235 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
6236 97972933 2020-09-11 stsp goto done;
6237 97972933 2020-09-11 stsp }
6238 8e7bd50a 2019-08-22 stsp
6239 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
6240 8e7bd50a 2019-08-22 stsp if (err)
6241 8e7bd50a 2019-08-22 stsp goto done;
6242 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6243 0d5bb276 2020-12-15 stsp initial_content_len, 1);
6244 8e7bd50a 2019-08-22 stsp done:
6245 8e7bd50a 2019-08-22 stsp free(initial_content);
6246 8e7bd50a 2019-08-22 stsp free(template);
6247 8e7bd50a 2019-08-22 stsp free(editor);
6248 8e7bd50a 2019-08-22 stsp
6249 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6250 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
6251 97972933 2020-09-11 stsp
6252 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
6253 59f86c76 2020-09-11 stsp if (err == NULL)
6254 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
6255 59f86c76 2020-09-11 stsp if (err) {
6256 59f86c76 2020-09-11 stsp free(*tagmsg);
6257 59f86c76 2020-09-11 stsp *tagmsg = NULL;
6258 8e7bd50a 2019-08-22 stsp }
6259 8e7bd50a 2019-08-22 stsp return err;
6260 8e7bd50a 2019-08-22 stsp }
6261 8e7bd50a 2019-08-22 stsp
6262 8e7bd50a 2019-08-22 stsp static const struct got_error *
6263 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
6264 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
6265 8e7bd50a 2019-08-22 stsp {
6266 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
6267 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
6268 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
6269 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
6270 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
6271 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
6272 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
6273 84de9106 2020-12-26 stsp struct got_reflist_head refs;
6274 8e7bd50a 2019-08-22 stsp
6275 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
6276 84de9106 2020-12-26 stsp
6277 8e7bd50a 2019-08-22 stsp /*
6278 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
6279 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
6280 8e7bd50a 2019-08-22 stsp * an unintended typo.
6281 8e7bd50a 2019-08-22 stsp */
6282 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
6283 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
6284 8e7bd50a 2019-08-22 stsp
6285 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
6286 8e7bd50a 2019-08-22 stsp if (err)
6287 8e7bd50a 2019-08-22 stsp return err;
6288 8e7bd50a 2019-08-22 stsp
6289 84de9106 2020-12-26 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6290 84de9106 2020-12-26 stsp if (err)
6291 84de9106 2020-12-26 stsp goto done;
6292 84de9106 2020-12-26 stsp
6293 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
6294 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_COMMIT, &refs, repo);
6295 8e7bd50a 2019-08-22 stsp if (err)
6296 8e7bd50a 2019-08-22 stsp goto done;
6297 8e7bd50a 2019-08-22 stsp
6298 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
6299 8e7bd50a 2019-08-22 stsp if (err)
6300 8e7bd50a 2019-08-22 stsp goto done;
6301 8e7bd50a 2019-08-22 stsp
6302 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
6303 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
6304 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
6305 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
6306 62d463ca 2020-10-20 naddy goto done;
6307 8e7bd50a 2019-08-22 stsp }
6308 8e7bd50a 2019-08-22 stsp tag_name += 10;
6309 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
6310 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
6311 62d463ca 2020-10-20 naddy goto done;
6312 8e7bd50a 2019-08-22 stsp }
6313 8e7bd50a 2019-08-22 stsp
6314 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
6315 8e7bd50a 2019-08-22 stsp if (err == NULL) {
6316 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
6317 8e7bd50a 2019-08-22 stsp goto done;
6318 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
6319 8e7bd50a 2019-08-22 stsp goto done;
6320 8e7bd50a 2019-08-22 stsp
6321 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
6322 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
6323 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
6324 f372d5cd 2019-10-21 stsp if (err) {
6325 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6326 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
6327 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6328 8e7bd50a 2019-08-22 stsp goto done;
6329 f372d5cd 2019-10-21 stsp }
6330 8e7bd50a 2019-08-22 stsp }
6331 8e7bd50a 2019-08-22 stsp
6332 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6333 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6334 f372d5cd 2019-10-21 stsp if (err) {
6335 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6336 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6337 8e7bd50a 2019-08-22 stsp goto done;
6338 f372d5cd 2019-10-21 stsp }
6339 8e7bd50a 2019-08-22 stsp
6340 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6341 f372d5cd 2019-10-21 stsp if (err) {
6342 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6343 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6344 8e7bd50a 2019-08-22 stsp goto done;
6345 f372d5cd 2019-10-21 stsp }
6346 8e7bd50a 2019-08-22 stsp
6347 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6348 f372d5cd 2019-10-21 stsp if (err) {
6349 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6350 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6351 f372d5cd 2019-10-21 stsp goto done;
6352 f372d5cd 2019-10-21 stsp }
6353 8e7bd50a 2019-08-22 stsp
6354 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6355 f372d5cd 2019-10-21 stsp if (err) {
6356 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6357 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6358 f372d5cd 2019-10-21 stsp goto done;
6359 8e7bd50a 2019-08-22 stsp }
6360 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6361 8e7bd50a 2019-08-22 stsp done:
6362 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6363 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6364 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6365 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6366 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6367 f372d5cd 2019-10-21 stsp free(tag_id_str);
6368 8e7bd50a 2019-08-22 stsp if (ref)
6369 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6370 8e7bd50a 2019-08-22 stsp free(commit_id);
6371 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6372 8e7bd50a 2019-08-22 stsp free(refname);
6373 8e7bd50a 2019-08-22 stsp free(tagmsg);
6374 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6375 aba9c984 2019-09-08 stsp free(tagger);
6376 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
6377 8e7bd50a 2019-08-22 stsp return err;
6378 8e7bd50a 2019-08-22 stsp }
6379 8e7bd50a 2019-08-22 stsp
6380 8e7bd50a 2019-08-22 stsp static const struct got_error *
6381 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6382 8e7bd50a 2019-08-22 stsp {
6383 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6384 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6385 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6386 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6387 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6388 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6389 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6390 8e7bd50a 2019-08-22 stsp
6391 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6392 8e7bd50a 2019-08-22 stsp switch (ch) {
6393 80106605 2020-02-24 stsp case 'c':
6394 80106605 2020-02-24 stsp commit_id_arg = optarg;
6395 80106605 2020-02-24 stsp break;
6396 8e7bd50a 2019-08-22 stsp case 'm':
6397 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6398 8e7bd50a 2019-08-22 stsp break;
6399 8e7bd50a 2019-08-22 stsp case 'r':
6400 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6401 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6402 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6403 9ba1d308 2019-10-21 stsp optarg);
6404 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6405 8e7bd50a 2019-08-22 stsp break;
6406 8e7bd50a 2019-08-22 stsp case 'l':
6407 8e7bd50a 2019-08-22 stsp do_list = 1;
6408 8e7bd50a 2019-08-22 stsp break;
6409 8e7bd50a 2019-08-22 stsp default:
6410 8e7bd50a 2019-08-22 stsp usage_tag();
6411 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6412 8e7bd50a 2019-08-22 stsp }
6413 8e7bd50a 2019-08-22 stsp }
6414 8e7bd50a 2019-08-22 stsp
6415 8e7bd50a 2019-08-22 stsp argc -= optind;
6416 8e7bd50a 2019-08-22 stsp argv += optind;
6417 8e7bd50a 2019-08-22 stsp
6418 8e7bd50a 2019-08-22 stsp if (do_list) {
6419 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6420 775ce909 2020-03-22 stsp errx(1,
6421 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6422 8e7bd50a 2019-08-22 stsp if (tagmsg)
6423 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
6424 8e7bd50a 2019-08-22 stsp if (argc > 0)
6425 8e7bd50a 2019-08-22 stsp usage_tag();
6426 80106605 2020-02-24 stsp } else if (argc != 1)
6427 8e7bd50a 2019-08-22 stsp usage_tag();
6428 80106605 2020-02-24 stsp
6429 a2887370 2019-08-23 stsp tag_name = argv[0];
6430 8e7bd50a 2019-08-22 stsp
6431 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6432 8e7bd50a 2019-08-22 stsp if (do_list) {
6433 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6434 8e7bd50a 2019-08-22 stsp NULL) == -1)
6435 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6436 8e7bd50a 2019-08-22 stsp } else {
6437 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6438 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6439 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6440 8e7bd50a 2019-08-22 stsp }
6441 8e7bd50a 2019-08-22 stsp #endif
6442 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6443 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6444 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6445 8e7bd50a 2019-08-22 stsp goto done;
6446 8e7bd50a 2019-08-22 stsp }
6447 8e7bd50a 2019-08-22 stsp
6448 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6449 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6450 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6451 8e7bd50a 2019-08-22 stsp goto done;
6452 8e7bd50a 2019-08-22 stsp else
6453 8e7bd50a 2019-08-22 stsp error = NULL;
6454 8e7bd50a 2019-08-22 stsp if (worktree) {
6455 8e7bd50a 2019-08-22 stsp repo_path =
6456 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6457 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6458 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6459 8e7bd50a 2019-08-22 stsp if (error)
6460 8e7bd50a 2019-08-22 stsp goto done;
6461 8e7bd50a 2019-08-22 stsp } else {
6462 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6463 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6464 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6465 8e7bd50a 2019-08-22 stsp goto done;
6466 8e7bd50a 2019-08-22 stsp }
6467 8e7bd50a 2019-08-22 stsp }
6468 8e7bd50a 2019-08-22 stsp }
6469 8e7bd50a 2019-08-22 stsp
6470 8e7bd50a 2019-08-22 stsp if (do_list) {
6471 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6472 c9956ddf 2019-09-08 stsp if (error != NULL)
6473 c9956ddf 2019-09-08 stsp goto done;
6474 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6475 8e7bd50a 2019-08-22 stsp if (error)
6476 8e7bd50a 2019-08-22 stsp goto done;
6477 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6478 8e7bd50a 2019-08-22 stsp } else {
6479 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6480 c9956ddf 2019-09-08 stsp if (error)
6481 c9956ddf 2019-09-08 stsp goto done;
6482 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6483 c9956ddf 2019-09-08 stsp if (error != NULL)
6484 c9956ddf 2019-09-08 stsp goto done;
6485 c9956ddf 2019-09-08 stsp
6486 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6487 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6488 8e7bd50a 2019-08-22 stsp if (error)
6489 8e7bd50a 2019-08-22 stsp goto done;
6490 8e7bd50a 2019-08-22 stsp }
6491 8e7bd50a 2019-08-22 stsp
6492 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6493 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6494 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6495 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6496 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6497 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6498 8e7bd50a 2019-08-22 stsp if (error)
6499 8e7bd50a 2019-08-22 stsp goto done;
6500 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6501 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6502 8e7bd50a 2019-08-22 stsp if (error)
6503 8e7bd50a 2019-08-22 stsp goto done;
6504 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6505 8e7bd50a 2019-08-22 stsp free(commit_id);
6506 8e7bd50a 2019-08-22 stsp if (error)
6507 8e7bd50a 2019-08-22 stsp goto done;
6508 8e7bd50a 2019-08-22 stsp }
6509 8e7bd50a 2019-08-22 stsp
6510 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6511 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6512 8e7bd50a 2019-08-22 stsp }
6513 8e7bd50a 2019-08-22 stsp done:
6514 1d0f4054 2021-06-17 stsp if (repo) {
6515 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6516 1d0f4054 2021-06-17 stsp if (error == NULL)
6517 1d0f4054 2021-06-17 stsp error = close_err;
6518 1d0f4054 2021-06-17 stsp }
6519 8e7bd50a 2019-08-22 stsp if (worktree)
6520 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6521 8e7bd50a 2019-08-22 stsp free(cwd);
6522 8e7bd50a 2019-08-22 stsp free(repo_path);
6523 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6524 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6525 8e7bd50a 2019-08-22 stsp return error;
6526 8e7bd50a 2019-08-22 stsp }
6527 8e7bd50a 2019-08-22 stsp
6528 8e7bd50a 2019-08-22 stsp __dead static void
6529 d00136be 2019-03-26 stsp usage_add(void)
6530 d00136be 2019-03-26 stsp {
6531 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6532 022fae89 2019-12-06 tracey getprogname());
6533 d00136be 2019-03-26 stsp exit(1);
6534 d00136be 2019-03-26 stsp }
6535 d00136be 2019-03-26 stsp
6536 d00136be 2019-03-26 stsp static const struct got_error *
6537 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6538 4e68cba3 2019-11-23 stsp {
6539 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6540 4e68cba3 2019-11-23 stsp path++;
6541 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6542 4e68cba3 2019-11-23 stsp return NULL;
6543 4e68cba3 2019-11-23 stsp }
6544 4e68cba3 2019-11-23 stsp
6545 4e68cba3 2019-11-23 stsp static const struct got_error *
6546 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6547 d00136be 2019-03-26 stsp {
6548 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6549 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6550 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6551 1dd54920 2019-05-11 stsp char *cwd = NULL;
6552 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6553 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6554 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6555 1dd54920 2019-05-11 stsp
6556 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6557 d00136be 2019-03-26 stsp
6558 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6559 d00136be 2019-03-26 stsp switch (ch) {
6560 022fae89 2019-12-06 tracey case 'I':
6561 022fae89 2019-12-06 tracey no_ignores = 1;
6562 022fae89 2019-12-06 tracey break;
6563 4e68cba3 2019-11-23 stsp case 'R':
6564 4e68cba3 2019-11-23 stsp can_recurse = 1;
6565 4e68cba3 2019-11-23 stsp break;
6566 d00136be 2019-03-26 stsp default:
6567 d00136be 2019-03-26 stsp usage_add();
6568 d00136be 2019-03-26 stsp /* NOTREACHED */
6569 d00136be 2019-03-26 stsp }
6570 d00136be 2019-03-26 stsp }
6571 d00136be 2019-03-26 stsp
6572 d00136be 2019-03-26 stsp argc -= optind;
6573 d00136be 2019-03-26 stsp argv += optind;
6574 d00136be 2019-03-26 stsp
6575 43012d58 2019-07-14 stsp #ifndef PROFILE
6576 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6577 43012d58 2019-07-14 stsp NULL) == -1)
6578 43012d58 2019-07-14 stsp err(1, "pledge");
6579 43012d58 2019-07-14 stsp #endif
6580 723c305c 2019-05-11 jcs if (argc < 1)
6581 d00136be 2019-03-26 stsp usage_add();
6582 d00136be 2019-03-26 stsp
6583 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6584 d00136be 2019-03-26 stsp if (cwd == NULL) {
6585 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6586 d00136be 2019-03-26 stsp goto done;
6587 d00136be 2019-03-26 stsp }
6588 723c305c 2019-05-11 jcs
6589 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6590 fa51e947 2020-03-27 stsp if (error) {
6591 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6592 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6593 d00136be 2019-03-26 stsp goto done;
6594 fa51e947 2020-03-27 stsp }
6595 d00136be 2019-03-26 stsp
6596 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6597 c9956ddf 2019-09-08 stsp NULL);
6598 031a5338 2019-03-26 stsp if (error != NULL)
6599 031a5338 2019-03-26 stsp goto done;
6600 031a5338 2019-03-26 stsp
6601 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6602 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6603 d00136be 2019-03-26 stsp if (error)
6604 d00136be 2019-03-26 stsp goto done;
6605 d00136be 2019-03-26 stsp
6606 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6607 6d022e97 2019-08-04 stsp if (error)
6608 022fae89 2019-12-06 tracey goto done;
6609 022fae89 2019-12-06 tracey
6610 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6611 4e68cba3 2019-11-23 stsp char *ondisk_path;
6612 4e68cba3 2019-11-23 stsp struct stat sb;
6613 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6614 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6615 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6616 62d463ca 2020-10-20 naddy pe->path) == -1) {
6617 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6618 4e68cba3 2019-11-23 stsp goto done;
6619 4e68cba3 2019-11-23 stsp }
6620 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6621 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6622 4e68cba3 2019-11-23 stsp free(ondisk_path);
6623 4e68cba3 2019-11-23 stsp continue;
6624 4e68cba3 2019-11-23 stsp }
6625 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6626 4e68cba3 2019-11-23 stsp ondisk_path);
6627 4e68cba3 2019-11-23 stsp free(ondisk_path);
6628 4e68cba3 2019-11-23 stsp goto done;
6629 4e68cba3 2019-11-23 stsp }
6630 4e68cba3 2019-11-23 stsp free(ondisk_path);
6631 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6632 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6633 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6634 4e68cba3 2019-11-23 stsp goto done;
6635 4e68cba3 2019-11-23 stsp }
6636 4e68cba3 2019-11-23 stsp }
6637 4e68cba3 2019-11-23 stsp }
6638 022fae89 2019-12-06 tracey
6639 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6640 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6641 d00136be 2019-03-26 stsp done:
6642 1d0f4054 2021-06-17 stsp if (repo) {
6643 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6644 1d0f4054 2021-06-17 stsp if (error == NULL)
6645 1d0f4054 2021-06-17 stsp error = close_err;
6646 1d0f4054 2021-06-17 stsp }
6647 d00136be 2019-03-26 stsp if (worktree)
6648 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6649 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6650 1dd54920 2019-05-11 stsp free((char *)pe->path);
6651 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6652 2ec1f75b 2019-03-26 stsp free(cwd);
6653 2ec1f75b 2019-03-26 stsp return error;
6654 2ec1f75b 2019-03-26 stsp }
6655 2ec1f75b 2019-03-26 stsp
6656 2ec1f75b 2019-03-26 stsp __dead static void
6657 648e4ef7 2019-07-09 stsp usage_remove(void)
6658 2ec1f75b 2019-03-26 stsp {
6659 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6660 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6661 2ec1f75b 2019-03-26 stsp exit(1);
6662 2a06fe5f 2019-08-24 stsp }
6663 2a06fe5f 2019-08-24 stsp
6664 2a06fe5f 2019-08-24 stsp static const struct got_error *
6665 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6666 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6667 2a06fe5f 2019-08-24 stsp {
6668 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6669 f2a9dc41 2019-12-13 tracey path++;
6670 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6671 2a06fe5f 2019-08-24 stsp return NULL;
6672 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6673 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6674 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6675 2a06fe5f 2019-08-24 stsp return NULL;
6676 2ec1f75b 2019-03-26 stsp }
6677 2ec1f75b 2019-03-26 stsp
6678 2ec1f75b 2019-03-26 stsp static const struct got_error *
6679 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6680 2ec1f75b 2019-03-26 stsp {
6681 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6682 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6683 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6684 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6685 17ed4618 2019-06-02 stsp char *cwd = NULL;
6686 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6687 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6688 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6689 2ec1f75b 2019-03-26 stsp
6690 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6691 17ed4618 2019-06-02 stsp
6692 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6693 2ec1f75b 2019-03-26 stsp switch (ch) {
6694 2ec1f75b 2019-03-26 stsp case 'f':
6695 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6696 2ec1f75b 2019-03-26 stsp break;
6697 70e3e7f5 2019-12-13 tracey case 'k':
6698 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6699 70e3e7f5 2019-12-13 tracey break;
6700 f2a9dc41 2019-12-13 tracey case 'R':
6701 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6702 f2a9dc41 2019-12-13 tracey break;
6703 766841c2 2020-08-13 stsp case 's':
6704 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6705 766841c2 2020-08-13 stsp switch (optarg[i]) {
6706 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6707 766841c2 2020-08-13 stsp delete_local_mods = 1;
6708 766841c2 2020-08-13 stsp break;
6709 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6710 766841c2 2020-08-13 stsp break;
6711 766841c2 2020-08-13 stsp default:
6712 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6713 766841c2 2020-08-13 stsp optarg[i]);
6714 766841c2 2020-08-13 stsp }
6715 766841c2 2020-08-13 stsp }
6716 766841c2 2020-08-13 stsp status_codes = optarg;
6717 766841c2 2020-08-13 stsp break;
6718 2ec1f75b 2019-03-26 stsp default:
6719 f2a9dc41 2019-12-13 tracey usage_remove();
6720 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6721 2ec1f75b 2019-03-26 stsp }
6722 2ec1f75b 2019-03-26 stsp }
6723 2ec1f75b 2019-03-26 stsp
6724 2ec1f75b 2019-03-26 stsp argc -= optind;
6725 2ec1f75b 2019-03-26 stsp argv += optind;
6726 2ec1f75b 2019-03-26 stsp
6727 43012d58 2019-07-14 stsp #ifndef PROFILE
6728 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6729 43012d58 2019-07-14 stsp NULL) == -1)
6730 43012d58 2019-07-14 stsp err(1, "pledge");
6731 43012d58 2019-07-14 stsp #endif
6732 17ed4618 2019-06-02 stsp if (argc < 1)
6733 648e4ef7 2019-07-09 stsp usage_remove();
6734 2ec1f75b 2019-03-26 stsp
6735 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6736 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6737 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6738 2ec1f75b 2019-03-26 stsp goto done;
6739 2ec1f75b 2019-03-26 stsp }
6740 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6741 fa51e947 2020-03-27 stsp if (error) {
6742 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6743 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6744 2ec1f75b 2019-03-26 stsp goto done;
6745 fa51e947 2020-03-27 stsp }
6746 2ec1f75b 2019-03-26 stsp
6747 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6748 c9956ddf 2019-09-08 stsp NULL);
6749 2af4a041 2019-05-11 jcs if (error)
6750 2ec1f75b 2019-03-26 stsp goto done;
6751 2ec1f75b 2019-03-26 stsp
6752 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6753 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6754 2ec1f75b 2019-03-26 stsp if (error)
6755 2ec1f75b 2019-03-26 stsp goto done;
6756 2ec1f75b 2019-03-26 stsp
6757 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6758 6d022e97 2019-08-04 stsp if (error)
6759 6d022e97 2019-08-04 stsp goto done;
6760 17ed4618 2019-06-02 stsp
6761 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6762 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6763 f2a9dc41 2019-12-13 tracey struct stat sb;
6764 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6765 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6766 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6767 62d463ca 2020-10-20 naddy pe->path) == -1) {
6768 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6769 f2a9dc41 2019-12-13 tracey goto done;
6770 f2a9dc41 2019-12-13 tracey }
6771 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6772 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6773 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6774 f2a9dc41 2019-12-13 tracey continue;
6775 f2a9dc41 2019-12-13 tracey }
6776 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6777 f2a9dc41 2019-12-13 tracey ondisk_path);
6778 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6779 f2a9dc41 2019-12-13 tracey goto done;
6780 f2a9dc41 2019-12-13 tracey }
6781 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6782 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6783 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6784 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6785 f2a9dc41 2019-12-13 tracey goto done;
6786 f2a9dc41 2019-12-13 tracey }
6787 f2a9dc41 2019-12-13 tracey }
6788 f2a9dc41 2019-12-13 tracey }
6789 f2a9dc41 2019-12-13 tracey
6790 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6791 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6792 766841c2 2020-08-13 stsp repo, keep_on_disk);
6793 a129376b 2019-03-28 stsp done:
6794 1d0f4054 2021-06-17 stsp if (repo) {
6795 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
6796 1d0f4054 2021-06-17 stsp if (error == NULL)
6797 1d0f4054 2021-06-17 stsp error = close_err;
6798 1d0f4054 2021-06-17 stsp }
6799 a129376b 2019-03-28 stsp if (worktree)
6800 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6801 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6802 17ed4618 2019-06-02 stsp free((char *)pe->path);
6803 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6804 a129376b 2019-03-28 stsp free(cwd);
6805 a129376b 2019-03-28 stsp return error;
6806 a129376b 2019-03-28 stsp }
6807 a129376b 2019-03-28 stsp
6808 a129376b 2019-03-28 stsp __dead static void
6809 a129376b 2019-03-28 stsp usage_revert(void)
6810 a129376b 2019-03-28 stsp {
6811 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6812 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6813 a129376b 2019-03-28 stsp exit(1);
6814 a129376b 2019-03-28 stsp }
6815 a129376b 2019-03-28 stsp
6816 1ee397ad 2019-07-12 stsp static const struct got_error *
6817 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6818 a129376b 2019-03-28 stsp {
6819 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6820 fb9704af 2020-01-27 stsp return NULL;
6821 fb9704af 2020-01-27 stsp
6822 a129376b 2019-03-28 stsp while (path[0] == '/')
6823 a129376b 2019-03-28 stsp path++;
6824 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6825 33aa809d 2019-08-08 stsp return NULL;
6826 33aa809d 2019-08-08 stsp }
6827 33aa809d 2019-08-08 stsp
6828 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6829 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6830 33aa809d 2019-08-08 stsp const char *action;
6831 33aa809d 2019-08-08 stsp };
6832 33aa809d 2019-08-08 stsp
6833 33aa809d 2019-08-08 stsp static const struct got_error *
6834 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6835 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6836 33aa809d 2019-08-08 stsp {
6837 33aa809d 2019-08-08 stsp char *line = NULL;
6838 33aa809d 2019-08-08 stsp size_t linesize = 0;
6839 33aa809d 2019-08-08 stsp ssize_t linelen;
6840 33aa809d 2019-08-08 stsp
6841 33aa809d 2019-08-08 stsp switch (status) {
6842 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6843 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6844 33aa809d 2019-08-08 stsp break;
6845 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6846 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6847 33aa809d 2019-08-08 stsp break;
6848 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6849 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6850 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6851 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6852 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6853 33aa809d 2019-08-08 stsp printf("%s", line);
6854 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6855 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6856 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6857 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6858 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6859 33aa809d 2019-08-08 stsp break;
6860 33aa809d 2019-08-08 stsp default:
6861 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6862 33aa809d 2019-08-08 stsp }
6863 33aa809d 2019-08-08 stsp
6864 33aa809d 2019-08-08 stsp return NULL;
6865 33aa809d 2019-08-08 stsp }
6866 33aa809d 2019-08-08 stsp
6867 33aa809d 2019-08-08 stsp static const struct got_error *
6868 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6869 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6870 33aa809d 2019-08-08 stsp {
6871 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6872 33aa809d 2019-08-08 stsp char *line = NULL;
6873 33aa809d 2019-08-08 stsp size_t linesize = 0;
6874 33aa809d 2019-08-08 stsp ssize_t linelen;
6875 33aa809d 2019-08-08 stsp int resp = ' ';
6876 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6877 33aa809d 2019-08-08 stsp
6878 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6879 33aa809d 2019-08-08 stsp
6880 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6881 33aa809d 2019-08-08 stsp char *nl;
6882 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6883 33aa809d 2019-08-08 stsp a->action);
6884 33aa809d 2019-08-08 stsp if (err)
6885 33aa809d 2019-08-08 stsp return err;
6886 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6887 33aa809d 2019-08-08 stsp if (linelen == -1) {
6888 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6889 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6890 33aa809d 2019-08-08 stsp return NULL;
6891 33aa809d 2019-08-08 stsp }
6892 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6893 33aa809d 2019-08-08 stsp if (nl)
6894 33aa809d 2019-08-08 stsp *nl = '\0';
6895 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6896 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6897 33aa809d 2019-08-08 stsp printf("y\n");
6898 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6899 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6900 33aa809d 2019-08-08 stsp printf("n\n");
6901 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6902 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6903 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6904 33aa809d 2019-08-08 stsp printf("q\n");
6905 33aa809d 2019-08-08 stsp } else
6906 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6907 33aa809d 2019-08-08 stsp free(line);
6908 33aa809d 2019-08-08 stsp return NULL;
6909 33aa809d 2019-08-08 stsp }
6910 33aa809d 2019-08-08 stsp
6911 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6912 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6913 33aa809d 2019-08-08 stsp a->action);
6914 33aa809d 2019-08-08 stsp if (err)
6915 33aa809d 2019-08-08 stsp return err;
6916 33aa809d 2019-08-08 stsp resp = getchar();
6917 33aa809d 2019-08-08 stsp if (resp == '\n')
6918 33aa809d 2019-08-08 stsp resp = getchar();
6919 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6920 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6921 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6922 33aa809d 2019-08-08 stsp resp = ' ';
6923 33aa809d 2019-08-08 stsp }
6924 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6925 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6926 33aa809d 2019-08-08 stsp resp = ' ';
6927 33aa809d 2019-08-08 stsp }
6928 33aa809d 2019-08-08 stsp }
6929 33aa809d 2019-08-08 stsp
6930 33aa809d 2019-08-08 stsp if (resp == 'y')
6931 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6932 33aa809d 2019-08-08 stsp else if (resp == 'n')
6933 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6934 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6935 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6936 33aa809d 2019-08-08 stsp
6937 1ee397ad 2019-07-12 stsp return NULL;
6938 a129376b 2019-03-28 stsp }
6939 a129376b 2019-03-28 stsp
6940 33aa809d 2019-08-08 stsp
6941 a129376b 2019-03-28 stsp static const struct got_error *
6942 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6943 a129376b 2019-03-28 stsp {
6944 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6945 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6946 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6947 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6948 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6949 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6950 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6951 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6952 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6953 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6954 a129376b 2019-03-28 stsp
6955 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6956 e20a8b6f 2019-06-04 stsp
6957 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6958 a129376b 2019-03-28 stsp switch (ch) {
6959 33aa809d 2019-08-08 stsp case 'p':
6960 33aa809d 2019-08-08 stsp pflag = 1;
6961 33aa809d 2019-08-08 stsp break;
6962 33aa809d 2019-08-08 stsp case 'F':
6963 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6964 33aa809d 2019-08-08 stsp break;
6965 0f6d7415 2019-08-08 stsp case 'R':
6966 0f6d7415 2019-08-08 stsp can_recurse = 1;
6967 0f6d7415 2019-08-08 stsp break;
6968 a129376b 2019-03-28 stsp default:
6969 a129376b 2019-03-28 stsp usage_revert();
6970 a129376b 2019-03-28 stsp /* NOTREACHED */
6971 a129376b 2019-03-28 stsp }
6972 a129376b 2019-03-28 stsp }
6973 a129376b 2019-03-28 stsp
6974 a129376b 2019-03-28 stsp argc -= optind;
6975 a129376b 2019-03-28 stsp argv += optind;
6976 a129376b 2019-03-28 stsp
6977 43012d58 2019-07-14 stsp #ifndef PROFILE
6978 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6979 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6980 43012d58 2019-07-14 stsp err(1, "pledge");
6981 43012d58 2019-07-14 stsp #endif
6982 e20a8b6f 2019-06-04 stsp if (argc < 1)
6983 a129376b 2019-03-28 stsp usage_revert();
6984 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6985 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6986 a129376b 2019-03-28 stsp
6987 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6988 a129376b 2019-03-28 stsp if (cwd == NULL) {
6989 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6990 a129376b 2019-03-28 stsp goto done;
6991 a129376b 2019-03-28 stsp }
6992 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6993 fa51e947 2020-03-27 stsp if (error) {
6994 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6995 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6996 2ec1f75b 2019-03-26 stsp goto done;
6997 fa51e947 2020-03-27 stsp }
6998 a129376b 2019-03-28 stsp
6999 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7000 c9956ddf 2019-09-08 stsp NULL);
7001 a129376b 2019-03-28 stsp if (error != NULL)
7002 a129376b 2019-03-28 stsp goto done;
7003 a129376b 2019-03-28 stsp
7004 33aa809d 2019-08-08 stsp if (patch_script_path) {
7005 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7006 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
7007 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
7008 33aa809d 2019-08-08 stsp patch_script_path);
7009 33aa809d 2019-08-08 stsp goto done;
7010 33aa809d 2019-08-08 stsp }
7011 33aa809d 2019-08-08 stsp }
7012 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
7013 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7014 a129376b 2019-03-28 stsp if (error)
7015 a129376b 2019-03-28 stsp goto done;
7016 a129376b 2019-03-28 stsp
7017 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7018 6d022e97 2019-08-04 stsp if (error)
7019 6d022e97 2019-08-04 stsp goto done;
7020 00db391e 2019-08-03 stsp
7021 0f6d7415 2019-08-08 stsp if (!can_recurse) {
7022 0f6d7415 2019-08-08 stsp char *ondisk_path;
7023 0f6d7415 2019-08-08 stsp struct stat sb;
7024 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
7025 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
7026 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
7027 62d463ca 2020-10-20 naddy pe->path) == -1) {
7028 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
7029 0f6d7415 2019-08-08 stsp goto done;
7030 0f6d7415 2019-08-08 stsp }
7031 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
7032 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
7033 0f6d7415 2019-08-08 stsp free(ondisk_path);
7034 0f6d7415 2019-08-08 stsp continue;
7035 0f6d7415 2019-08-08 stsp }
7036 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
7037 0f6d7415 2019-08-08 stsp ondisk_path);
7038 0f6d7415 2019-08-08 stsp free(ondisk_path);
7039 0f6d7415 2019-08-08 stsp goto done;
7040 0f6d7415 2019-08-08 stsp }
7041 0f6d7415 2019-08-08 stsp free(ondisk_path);
7042 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
7043 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
7044 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
7045 0f6d7415 2019-08-08 stsp goto done;
7046 0f6d7415 2019-08-08 stsp }
7047 0f6d7415 2019-08-08 stsp }
7048 0f6d7415 2019-08-08 stsp }
7049 0f6d7415 2019-08-08 stsp
7050 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7051 33aa809d 2019-08-08 stsp cpa.action = "revert";
7052 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
7053 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7054 2ec1f75b 2019-03-26 stsp done:
7055 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7056 33aa809d 2019-08-08 stsp error == NULL)
7057 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7058 1d0f4054 2021-06-17 stsp if (repo) {
7059 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7060 1d0f4054 2021-06-17 stsp if (error == NULL)
7061 1d0f4054 2021-06-17 stsp error = close_err;
7062 1d0f4054 2021-06-17 stsp }
7063 2ec1f75b 2019-03-26 stsp if (worktree)
7064 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
7065 2ec1f75b 2019-03-26 stsp free(path);
7066 d00136be 2019-03-26 stsp free(cwd);
7067 6bad629b 2019-02-04 stsp return error;
7068 c4296144 2019-05-09 stsp }
7069 c4296144 2019-05-09 stsp
7070 c4296144 2019-05-09 stsp __dead static void
7071 c4296144 2019-05-09 stsp usage_commit(void)
7072 c4296144 2019-05-09 stsp {
7073 28cf319f 2021-01-28 stsp fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
7074 28cf319f 2021-01-28 stsp "[path ...]\n", getprogname());
7075 c4296144 2019-05-09 stsp exit(1);
7076 33ad4cbe 2019-05-12 jcs }
7077 33ad4cbe 2019-05-12 jcs
7078 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
7079 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
7080 28cf319f 2021-01-28 stsp const char *prepared_log;
7081 28cf319f 2021-01-28 stsp int non_interactive;
7082 e2ba3d07 2019-05-13 stsp const char *editor;
7083 e0870e44 2019-05-13 stsp const char *worktree_path;
7084 76d98825 2019-06-03 stsp const char *branch_name;
7085 314a6357 2019-05-13 stsp const char *repo_path;
7086 e0870e44 2019-05-13 stsp char *logmsg_path;
7087 e2ba3d07 2019-05-13 stsp
7088 e2ba3d07 2019-05-13 stsp };
7089 28cf319f 2021-01-28 stsp
7090 28cf319f 2021-01-28 stsp static const struct got_error *
7091 28cf319f 2021-01-28 stsp read_prepared_logmsg(char **logmsg, const char *path)
7092 28cf319f 2021-01-28 stsp {
7093 28cf319f 2021-01-28 stsp const struct got_error *err = NULL;
7094 28cf319f 2021-01-28 stsp FILE *f = NULL;
7095 28cf319f 2021-01-28 stsp struct stat sb;
7096 28cf319f 2021-01-28 stsp size_t r;
7097 28cf319f 2021-01-28 stsp
7098 28cf319f 2021-01-28 stsp *logmsg = NULL;
7099 28cf319f 2021-01-28 stsp memset(&sb, 0, sizeof(sb));
7100 28cf319f 2021-01-28 stsp
7101 28cf319f 2021-01-28 stsp f = fopen(path, "r");
7102 28cf319f 2021-01-28 stsp if (f == NULL)
7103 28cf319f 2021-01-28 stsp return got_error_from_errno2("fopen", path);
7104 28cf319f 2021-01-28 stsp
7105 28cf319f 2021-01-28 stsp if (fstat(fileno(f), &sb) == -1) {
7106 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fstat", path);
7107 28cf319f 2021-01-28 stsp goto done;
7108 28cf319f 2021-01-28 stsp }
7109 28cf319f 2021-01-28 stsp if (sb.st_size == 0) {
7110 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
7111 28cf319f 2021-01-28 stsp goto done;
7112 28cf319f 2021-01-28 stsp }
7113 28cf319f 2021-01-28 stsp
7114 28cf319f 2021-01-28 stsp *logmsg = malloc(sb.st_size + 1);
7115 28cf319f 2021-01-28 stsp if (*logmsg == NULL) {
7116 28cf319f 2021-01-28 stsp err = got_error_from_errno("malloc");
7117 28cf319f 2021-01-28 stsp goto done;
7118 28cf319f 2021-01-28 stsp }
7119 28cf319f 2021-01-28 stsp
7120 28cf319f 2021-01-28 stsp r = fread(*logmsg, 1, sb.st_size, f);
7121 28cf319f 2021-01-28 stsp if (r != sb.st_size) {
7122 28cf319f 2021-01-28 stsp if (ferror(f))
7123 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fread", path);
7124 28cf319f 2021-01-28 stsp else
7125 28cf319f 2021-01-28 stsp err = got_error(GOT_ERR_IO);
7126 28cf319f 2021-01-28 stsp goto done;
7127 28cf319f 2021-01-28 stsp }
7128 28cf319f 2021-01-28 stsp (*logmsg)[sb.st_size] = '\0';
7129 28cf319f 2021-01-28 stsp done:
7130 28cf319f 2021-01-28 stsp if (fclose(f) == EOF && err == NULL)
7131 28cf319f 2021-01-28 stsp err = got_error_from_errno2("fclose", path);
7132 28cf319f 2021-01-28 stsp if (err) {
7133 28cf319f 2021-01-28 stsp free(*logmsg);
7134 28cf319f 2021-01-28 stsp *logmsg = NULL;
7135 28cf319f 2021-01-28 stsp }
7136 28cf319f 2021-01-28 stsp return err;
7137 28cf319f 2021-01-28 stsp
7138 28cf319f 2021-01-28 stsp }
7139 e0870e44 2019-05-13 stsp
7140 33ad4cbe 2019-05-12 jcs static const struct got_error *
7141 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
7142 33ad4cbe 2019-05-12 jcs void *arg)
7143 33ad4cbe 2019-05-12 jcs {
7144 76d98825 2019-06-03 stsp char *initial_content = NULL;
7145 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
7146 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
7147 e0870e44 2019-05-13 stsp char *template = NULL;
7148 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
7149 1601cb9f 2020-09-11 naddy int initial_content_len;
7150 97972933 2020-09-11 stsp int fd = -1;
7151 33ad4cbe 2019-05-12 jcs size_t len;
7152 33ad4cbe 2019-05-12 jcs
7153 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
7154 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
7155 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
7156 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
7157 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
7158 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
7159 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
7160 33ad4cbe 2019-05-12 jcs return NULL;
7161 28cf319f 2021-01-28 stsp } else if (a->prepared_log != NULL && a->non_interactive)
7162 28cf319f 2021-01-28 stsp return read_prepared_logmsg(logmsg, a->prepared_log);
7163 33ad4cbe 2019-05-12 jcs
7164 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
7165 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
7166 76d98825 2019-06-03 stsp
7167 28cf319f 2021-01-28 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
7168 28cf319f 2021-01-28 stsp if (err)
7169 28cf319f 2021-01-28 stsp goto done;
7170 28cf319f 2021-01-28 stsp
7171 28cf319f 2021-01-28 stsp if (a->prepared_log) {
7172 28cf319f 2021-01-28 stsp char *msg;
7173 28cf319f 2021-01-28 stsp err = read_prepared_logmsg(&msg, a->prepared_log);
7174 28cf319f 2021-01-28 stsp if (err)
7175 28cf319f 2021-01-28 stsp goto done;
7176 28cf319f 2021-01-28 stsp if (write(fd, msg, strlen(msg)) == -1) {
7177 28cf319f 2021-01-28 stsp err = got_error_from_errno2("write", a->logmsg_path);
7178 28cf319f 2021-01-28 stsp free(msg);
7179 28cf319f 2021-01-28 stsp goto done;
7180 28cf319f 2021-01-28 stsp }
7181 28cf319f 2021-01-28 stsp free(msg);
7182 28cf319f 2021-01-28 stsp }
7183 28cf319f 2021-01-28 stsp
7184 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
7185 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
7186 1601cb9f 2020-09-11 naddy a->branch_name);
7187 28cf319f 2021-01-28 stsp if (initial_content_len == -1) {
7188 28cf319f 2021-01-28 stsp err = got_error_from_errno("asprintf");
7189 e0870e44 2019-05-13 stsp goto done;
7190 28cf319f 2021-01-28 stsp }
7191 33ad4cbe 2019-05-12 jcs
7192 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
7193 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
7194 97972933 2020-09-11 stsp goto done;
7195 97972933 2020-09-11 stsp }
7196 33ad4cbe 2019-05-12 jcs
7197 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
7198 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
7199 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
7200 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
7201 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
7202 33ad4cbe 2019-05-12 jcs }
7203 33ad4cbe 2019-05-12 jcs
7204 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
7205 28cf319f 2021-01-28 stsp initial_content_len, a->prepared_log ? 0 : 1);
7206 33ad4cbe 2019-05-12 jcs done:
7207 76d98825 2019-06-03 stsp free(initial_content);
7208 e0870e44 2019-05-13 stsp free(template);
7209 314a6357 2019-05-13 stsp
7210 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
7211 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
7212 97972933 2020-09-11 stsp
7213 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
7214 59f86c76 2020-09-11 stsp if (err == NULL)
7215 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
7216 59f86c76 2020-09-11 stsp if (err) {
7217 59f86c76 2020-09-11 stsp free(*logmsg);
7218 59f86c76 2020-09-11 stsp *logmsg = NULL;
7219 3ce1b845 2019-07-15 stsp }
7220 33ad4cbe 2019-05-12 jcs return err;
7221 6bad629b 2019-02-04 stsp }
7222 c4296144 2019-05-09 stsp
7223 c4296144 2019-05-09 stsp static const struct got_error *
7224 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
7225 c4296144 2019-05-09 stsp {
7226 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
7227 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
7228 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
7229 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
7230 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
7231 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
7232 28cf319f 2021-01-28 stsp char *prepared_logmsg = NULL;
7233 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
7234 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
7235 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
7236 28cf319f 2021-01-28 stsp int allow_bad_symlinks = 0, non_interactive = 0;
7237 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
7238 5c1e53bc 2019-07-28 stsp
7239 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
7240 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
7241 c4296144 2019-05-09 stsp
7242 28cf319f 2021-01-28 stsp while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
7243 c4296144 2019-05-09 stsp switch (ch) {
7244 28cf319f 2021-01-28 stsp case 'F':
7245 28cf319f 2021-01-28 stsp if (logmsg != NULL)
7246 28cf319f 2021-01-28 stsp option_conflict('F', 'm');
7247 28cf319f 2021-01-28 stsp prepared_logmsg = realpath(optarg, NULL);
7248 28cf319f 2021-01-28 stsp if (prepared_logmsg == NULL)
7249 28cf319f 2021-01-28 stsp return got_error_from_errno2("realpath",
7250 28cf319f 2021-01-28 stsp optarg);
7251 28cf319f 2021-01-28 stsp break;
7252 c4296144 2019-05-09 stsp case 'm':
7253 28cf319f 2021-01-28 stsp if (prepared_logmsg)
7254 28cf319f 2021-01-28 stsp option_conflict('m', 'F');
7255 c4296144 2019-05-09 stsp logmsg = optarg;
7256 28cf319f 2021-01-28 stsp break;
7257 28cf319f 2021-01-28 stsp case 'N':
7258 28cf319f 2021-01-28 stsp non_interactive = 1;
7259 c4296144 2019-05-09 stsp break;
7260 35213c7c 2020-07-23 stsp case 'S':
7261 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
7262 35213c7c 2020-07-23 stsp break;
7263 c4296144 2019-05-09 stsp default:
7264 c4296144 2019-05-09 stsp usage_commit();
7265 c4296144 2019-05-09 stsp /* NOTREACHED */
7266 c4296144 2019-05-09 stsp }
7267 c4296144 2019-05-09 stsp }
7268 c4296144 2019-05-09 stsp
7269 c4296144 2019-05-09 stsp argc -= optind;
7270 c4296144 2019-05-09 stsp argv += optind;
7271 c4296144 2019-05-09 stsp
7272 43012d58 2019-07-14 stsp #ifndef PROFILE
7273 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7274 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7275 43012d58 2019-07-14 stsp err(1, "pledge");
7276 43012d58 2019-07-14 stsp #endif
7277 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
7278 c4296144 2019-05-09 stsp if (cwd == NULL) {
7279 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
7280 c4296144 2019-05-09 stsp goto done;
7281 c4296144 2019-05-09 stsp }
7282 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
7283 fa51e947 2020-03-27 stsp if (error) {
7284 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7285 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
7286 7d5807f4 2019-07-11 stsp goto done;
7287 fa51e947 2020-03-27 stsp }
7288 7d5807f4 2019-07-11 stsp
7289 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7290 c4296144 2019-05-09 stsp if (error)
7291 a698f62e 2019-07-25 stsp goto done;
7292 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
7293 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
7294 c4296144 2019-05-09 stsp goto done;
7295 a698f62e 2019-07-25 stsp }
7296 5c1e53bc 2019-07-28 stsp
7297 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7298 916f288c 2019-07-30 stsp worktree);
7299 5c1e53bc 2019-07-28 stsp if (error)
7300 5c1e53bc 2019-07-28 stsp goto done;
7301 c4296144 2019-05-09 stsp
7302 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
7303 c9956ddf 2019-09-08 stsp if (error)
7304 c9956ddf 2019-09-08 stsp goto done;
7305 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7306 c9956ddf 2019-09-08 stsp gitconfig_path);
7307 c4296144 2019-05-09 stsp if (error != NULL)
7308 c4296144 2019-05-09 stsp goto done;
7309 c4296144 2019-05-09 stsp
7310 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
7311 aba9c984 2019-09-08 stsp if (error)
7312 aba9c984 2019-09-08 stsp return error;
7313 aba9c984 2019-09-08 stsp
7314 314a6357 2019-05-13 stsp /*
7315 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
7316 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
7317 314a6357 2019-05-13 stsp */
7318 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
7319 314a6357 2019-05-13 stsp error = get_editor(&editor);
7320 314a6357 2019-05-13 stsp else
7321 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7322 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7323 c4296144 2019-05-09 stsp if (error)
7324 c4296144 2019-05-09 stsp goto done;
7325 c4296144 2019-05-09 stsp
7326 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7327 087fb88c 2019-08-04 stsp if (error)
7328 087fb88c 2019-08-04 stsp goto done;
7329 087fb88c 2019-08-04 stsp
7330 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
7331 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
7332 28cf319f 2021-01-28 stsp cl_arg.prepared_log = prepared_logmsg;
7333 28cf319f 2021-01-28 stsp cl_arg.non_interactive = non_interactive;
7334 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
7335 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
7336 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
7337 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
7338 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
7339 916f288c 2019-07-30 stsp goto done;
7340 916f288c 2019-07-30 stsp }
7341 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
7342 916f288c 2019-07-30 stsp }
7343 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
7344 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
7345 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
7346 35213c7c 2020-07-23 stsp print_status, NULL, repo);
7347 e0870e44 2019-05-13 stsp if (error) {
7348 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7349 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
7350 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
7351 c4296144 2019-05-09 stsp goto done;
7352 e0870e44 2019-05-13 stsp }
7353 c4296144 2019-05-09 stsp
7354 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
7355 c4296144 2019-05-09 stsp if (error)
7356 c4296144 2019-05-09 stsp goto done;
7357 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
7358 c4296144 2019-05-09 stsp done:
7359 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
7360 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
7361 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
7362 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
7363 7266f21f 2019-10-21 stsp error == NULL)
7364 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
7365 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
7366 1d0f4054 2021-06-17 stsp if (repo) {
7367 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7368 1d0f4054 2021-06-17 stsp if (error == NULL)
7369 1d0f4054 2021-06-17 stsp error = close_err;
7370 1d0f4054 2021-06-17 stsp }
7371 c4296144 2019-05-09 stsp if (worktree)
7372 c4296144 2019-05-09 stsp got_worktree_close(worktree);
7373 c4296144 2019-05-09 stsp free(cwd);
7374 c4296144 2019-05-09 stsp free(id_str);
7375 c9956ddf 2019-09-08 stsp free(gitconfig_path);
7376 e2ba3d07 2019-05-13 stsp free(editor);
7377 aba9c984 2019-09-08 stsp free(author);
7378 28cf319f 2021-01-28 stsp free(prepared_logmsg);
7379 f8a36e22 2021-08-26 stsp return error;
7380 f8a36e22 2021-08-26 stsp }
7381 f8a36e22 2021-08-26 stsp
7382 f8a36e22 2021-08-26 stsp __dead static void
7383 f8a36e22 2021-08-26 stsp usage_send(void)
7384 f8a36e22 2021-08-26 stsp {
7385 f8a36e22 2021-08-26 stsp fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
7386 f8a36e22 2021-08-26 stsp "[-r repository-path] [-t tag] [-T] [-q] [-v] "
7387 f8a36e22 2021-08-26 stsp "[remote-repository]\n", getprogname());
7388 f8a36e22 2021-08-26 stsp exit(1);
7389 f8a36e22 2021-08-26 stsp }
7390 f8a36e22 2021-08-26 stsp
7391 f8a36e22 2021-08-26 stsp struct got_send_progress_arg {
7392 f8a36e22 2021-08-26 stsp char last_scaled_packsize[FMT_SCALED_STRSIZE];
7393 f8a36e22 2021-08-26 stsp int verbosity;
7394 f8a36e22 2021-08-26 stsp int last_ncommits;
7395 f8a36e22 2021-08-26 stsp int last_nobj_total;
7396 f8a36e22 2021-08-26 stsp int last_p_deltify;
7397 f8a36e22 2021-08-26 stsp int last_p_written;
7398 f8a36e22 2021-08-26 stsp int last_p_sent;
7399 f8a36e22 2021-08-26 stsp int printed_something;
7400 f8a36e22 2021-08-26 stsp int sent_something;
7401 f8a36e22 2021-08-26 stsp struct got_pathlist_head *delete_branches;
7402 f8a36e22 2021-08-26 stsp };
7403 f8a36e22 2021-08-26 stsp
7404 f8a36e22 2021-08-26 stsp static const struct got_error *
7405 f8a36e22 2021-08-26 stsp send_progress(void *arg, off_t packfile_size, int ncommits, int nobj_total,
7406 f8a36e22 2021-08-26 stsp int nobj_deltify, int nobj_written, off_t bytes_sent, const char *refname,
7407 f8a36e22 2021-08-26 stsp int success)
7408 f8a36e22 2021-08-26 stsp {
7409 f8a36e22 2021-08-26 stsp struct got_send_progress_arg *a = arg;
7410 f8a36e22 2021-08-26 stsp char scaled_packsize[FMT_SCALED_STRSIZE];
7411 f8a36e22 2021-08-26 stsp char scaled_sent[FMT_SCALED_STRSIZE];
7412 f8a36e22 2021-08-26 stsp int p_deltify = 0, p_written = 0, p_sent = 0;
7413 f8a36e22 2021-08-26 stsp int print_searching = 0, print_total = 0;
7414 f8a36e22 2021-08-26 stsp int print_deltify = 0, print_written = 0, print_sent = 0;
7415 f8a36e22 2021-08-26 stsp
7416 f8a36e22 2021-08-26 stsp if (a->verbosity < 0)
7417 f8a36e22 2021-08-26 stsp return NULL;
7418 f8a36e22 2021-08-26 stsp
7419 f8a36e22 2021-08-26 stsp if (refname) {
7420 f8a36e22 2021-08-26 stsp const char *status = success ? "accepted" : "rejected";
7421 f8a36e22 2021-08-26 stsp
7422 f8a36e22 2021-08-26 stsp if (success) {
7423 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7424 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, a->delete_branches, entry) {
7425 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7426 f8a36e22 2021-08-26 stsp if (got_path_cmp(branchname, refname,
7427 f8a36e22 2021-08-26 stsp strlen(branchname), strlen(refname)) == 0) {
7428 f8a36e22 2021-08-26 stsp status = "deleted";
7429 27b75514 2021-08-28 stsp a->sent_something = 1;
7430 f8a36e22 2021-08-26 stsp break;
7431 f8a36e22 2021-08-26 stsp }
7432 f8a36e22 2021-08-26 stsp }
7433 f8a36e22 2021-08-26 stsp }
7434 f8a36e22 2021-08-26 stsp
7435 27b75514 2021-08-28 stsp if (a->printed_something)
7436 27b75514 2021-08-28 stsp putchar('\n');
7437 27b75514 2021-08-28 stsp printf("Server has %s %s", status, refname);
7438 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7439 f8a36e22 2021-08-26 stsp return NULL;
7440 f8a36e22 2021-08-26 stsp }
7441 f8a36e22 2021-08-26 stsp
7442 f8a36e22 2021-08-26 stsp if (fmt_scaled(packfile_size, scaled_packsize) == -1)
7443 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7444 f8a36e22 2021-08-26 stsp if (fmt_scaled(bytes_sent, scaled_sent) == -1)
7445 f8a36e22 2021-08-26 stsp return got_error_from_errno("fmt_scaled");
7446 f8a36e22 2021-08-26 stsp
7447 f8a36e22 2021-08-26 stsp if (a->last_ncommits != ncommits) {
7448 f8a36e22 2021-08-26 stsp print_searching = 1;
7449 f8a36e22 2021-08-26 stsp a->last_ncommits = ncommits;
7450 f8a36e22 2021-08-26 stsp }
7451 f8a36e22 2021-08-26 stsp
7452 f8a36e22 2021-08-26 stsp if (a->last_nobj_total != nobj_total) {
7453 f8a36e22 2021-08-26 stsp print_searching = 1;
7454 f8a36e22 2021-08-26 stsp print_total = 1;
7455 f8a36e22 2021-08-26 stsp a->last_nobj_total = nobj_total;
7456 f8a36e22 2021-08-26 stsp }
7457 f8a36e22 2021-08-26 stsp
7458 f8a36e22 2021-08-26 stsp if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
7459 f8a36e22 2021-08-26 stsp strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
7460 f8a36e22 2021-08-26 stsp if (strlcpy(a->last_scaled_packsize, scaled_packsize,
7461 f8a36e22 2021-08-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
7462 f8a36e22 2021-08-26 stsp return got_error(GOT_ERR_NO_SPACE);
7463 f8a36e22 2021-08-26 stsp }
7464 f8a36e22 2021-08-26 stsp
7465 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0 || nobj_written > 0) {
7466 f8a36e22 2021-08-26 stsp if (nobj_deltify > 0) {
7467 f8a36e22 2021-08-26 stsp p_deltify = (nobj_deltify * 100) / nobj_total;
7468 f8a36e22 2021-08-26 stsp if (p_deltify != a->last_p_deltify) {
7469 f8a36e22 2021-08-26 stsp a->last_p_deltify = p_deltify;
7470 f8a36e22 2021-08-26 stsp print_searching = 1;
7471 f8a36e22 2021-08-26 stsp print_total = 1;
7472 f8a36e22 2021-08-26 stsp print_deltify = 1;
7473 f8a36e22 2021-08-26 stsp }
7474 f8a36e22 2021-08-26 stsp }
7475 f8a36e22 2021-08-26 stsp if (nobj_written > 0) {
7476 f8a36e22 2021-08-26 stsp p_written = (nobj_written * 100) / nobj_total;
7477 f8a36e22 2021-08-26 stsp if (p_written != a->last_p_written) {
7478 f8a36e22 2021-08-26 stsp a->last_p_written = p_written;
7479 f8a36e22 2021-08-26 stsp print_searching = 1;
7480 f8a36e22 2021-08-26 stsp print_total = 1;
7481 f8a36e22 2021-08-26 stsp print_deltify = 1;
7482 f8a36e22 2021-08-26 stsp print_written = 1;
7483 f8a36e22 2021-08-26 stsp }
7484 f8a36e22 2021-08-26 stsp }
7485 f8a36e22 2021-08-26 stsp }
7486 f8a36e22 2021-08-26 stsp
7487 f8a36e22 2021-08-26 stsp if (bytes_sent > 0) {
7488 f8a36e22 2021-08-26 stsp p_sent = (bytes_sent * 100) / packfile_size;
7489 f8a36e22 2021-08-26 stsp if (p_sent != a->last_p_sent) {
7490 f8a36e22 2021-08-26 stsp a->last_p_sent = p_sent;
7491 f8a36e22 2021-08-26 stsp print_searching = 1;
7492 f8a36e22 2021-08-26 stsp print_total = 1;
7493 f8a36e22 2021-08-26 stsp print_deltify = 1;
7494 f8a36e22 2021-08-26 stsp print_written = 1;
7495 f8a36e22 2021-08-26 stsp print_sent = 1;
7496 f8a36e22 2021-08-26 stsp }
7497 f8a36e22 2021-08-26 stsp a->sent_something = 1;
7498 f8a36e22 2021-08-26 stsp }
7499 f8a36e22 2021-08-26 stsp
7500 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify || print_written ||
7501 f8a36e22 2021-08-26 stsp print_sent)
7502 f8a36e22 2021-08-26 stsp printf("\r");
7503 f8a36e22 2021-08-26 stsp if (print_searching)
7504 f8a36e22 2021-08-26 stsp printf("packing %d reference%s", ncommits,
7505 f8a36e22 2021-08-26 stsp ncommits == 1 ? "" : "s");
7506 f8a36e22 2021-08-26 stsp if (print_total)
7507 f8a36e22 2021-08-26 stsp printf("; %d object%s", nobj_total,
7508 f8a36e22 2021-08-26 stsp nobj_total == 1 ? "" : "s");
7509 f8a36e22 2021-08-26 stsp if (print_deltify)
7510 f8a36e22 2021-08-26 stsp printf("; deltify: %d%%", p_deltify);
7511 f8a36e22 2021-08-26 stsp if (print_sent)
7512 f8a36e22 2021-08-26 stsp printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE,
7513 f8a36e22 2021-08-26 stsp scaled_packsize, p_sent);
7514 f8a36e22 2021-08-26 stsp else if (print_written)
7515 f8a36e22 2021-08-26 stsp printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE,
7516 f8a36e22 2021-08-26 stsp scaled_packsize, p_written);
7517 f8a36e22 2021-08-26 stsp if (print_searching || print_total || print_deltify ||
7518 f8a36e22 2021-08-26 stsp print_written || print_sent) {
7519 f8a36e22 2021-08-26 stsp a->printed_something = 1;
7520 f8a36e22 2021-08-26 stsp fflush(stdout);
7521 f8a36e22 2021-08-26 stsp }
7522 f8a36e22 2021-08-26 stsp return NULL;
7523 f8a36e22 2021-08-26 stsp }
7524 f8a36e22 2021-08-26 stsp
7525 f8a36e22 2021-08-26 stsp static const struct got_error *
7526 f8a36e22 2021-08-26 stsp cmd_send(int argc, char *argv[])
7527 f8a36e22 2021-08-26 stsp {
7528 f8a36e22 2021-08-26 stsp const struct got_error *error = NULL;
7529 f8a36e22 2021-08-26 stsp char *cwd = NULL, *repo_path = NULL;
7530 f8a36e22 2021-08-26 stsp const char *remote_name;
7531 f8a36e22 2021-08-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
7532 f8a36e22 2021-08-26 stsp char *repo_name = NULL, *server_path = NULL;
7533 f8a36e22 2021-08-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
7534 f8a36e22 2021-08-26 stsp int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
7535 f8a36e22 2021-08-26 stsp struct got_repository *repo = NULL;
7536 f8a36e22 2021-08-26 stsp struct got_worktree *worktree = NULL;
7537 f8a36e22 2021-08-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
7538 f8a36e22 2021-08-26 stsp struct got_pathlist_head branches;
7539 f8a36e22 2021-08-26 stsp struct got_pathlist_head tags;
7540 f8a36e22 2021-08-26 stsp struct got_reflist_head all_branches;
7541 f8a36e22 2021-08-26 stsp struct got_reflist_head all_tags;
7542 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_args;
7543 f8a36e22 2021-08-26 stsp struct got_pathlist_head delete_branches;
7544 f8a36e22 2021-08-26 stsp struct got_reflist_entry *re;
7545 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *pe;
7546 f8a36e22 2021-08-26 stsp int i, ch, sendfd = -1, sendstatus;
7547 f8a36e22 2021-08-26 stsp pid_t sendpid = -1;
7548 f8a36e22 2021-08-26 stsp struct got_send_progress_arg spa;
7549 f8a36e22 2021-08-26 stsp int verbosity = 0, overwrite_refs = 0;
7550 f8a36e22 2021-08-26 stsp int send_all_branches = 0, send_all_tags = 0;
7551 f8a36e22 2021-08-26 stsp struct got_reference *ref = NULL;
7552 f8a36e22 2021-08-26 stsp
7553 f8a36e22 2021-08-26 stsp TAILQ_INIT(&branches);
7554 f8a36e22 2021-08-26 stsp TAILQ_INIT(&tags);
7555 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_branches);
7556 f8a36e22 2021-08-26 stsp TAILQ_INIT(&all_tags);
7557 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_args);
7558 f8a36e22 2021-08-26 stsp TAILQ_INIT(&delete_branches);
7559 f8a36e22 2021-08-26 stsp
7560 f8a36e22 2021-08-26 stsp while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
7561 f8a36e22 2021-08-26 stsp switch (ch) {
7562 f8a36e22 2021-08-26 stsp case 'a':
7563 f8a36e22 2021-08-26 stsp send_all_branches = 1;
7564 f8a36e22 2021-08-26 stsp break;
7565 f8a36e22 2021-08-26 stsp case 'b':
7566 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, optarg, NULL);
7567 f8a36e22 2021-08-26 stsp if (error)
7568 f8a36e22 2021-08-26 stsp return error;
7569 f8a36e22 2021-08-26 stsp nbranches++;
7570 f8a36e22 2021-08-26 stsp break;
7571 f8a36e22 2021-08-26 stsp case 'd':
7572 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&delete_args, optarg, NULL);
7573 f8a36e22 2021-08-26 stsp if (error)
7574 f8a36e22 2021-08-26 stsp return error;
7575 f8a36e22 2021-08-26 stsp break;
7576 f8a36e22 2021-08-26 stsp case 'f':
7577 f8a36e22 2021-08-26 stsp overwrite_refs = 1;
7578 f8a36e22 2021-08-26 stsp break;
7579 f8a36e22 2021-08-26 stsp case 'r':
7580 f8a36e22 2021-08-26 stsp repo_path = realpath(optarg, NULL);
7581 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7582 f8a36e22 2021-08-26 stsp return got_error_from_errno2("realpath",
7583 f8a36e22 2021-08-26 stsp optarg);
7584 f8a36e22 2021-08-26 stsp got_path_strip_trailing_slashes(repo_path);
7585 f8a36e22 2021-08-26 stsp break;
7586 f8a36e22 2021-08-26 stsp case 't':
7587 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags, optarg, NULL);
7588 f8a36e22 2021-08-26 stsp if (error)
7589 f8a36e22 2021-08-26 stsp return error;
7590 f8a36e22 2021-08-26 stsp ntags++;
7591 f8a36e22 2021-08-26 stsp break;
7592 f8a36e22 2021-08-26 stsp case 'T':
7593 f8a36e22 2021-08-26 stsp send_all_tags = 1;
7594 f8a36e22 2021-08-26 stsp break;
7595 f8a36e22 2021-08-26 stsp case 'v':
7596 f8a36e22 2021-08-26 stsp if (verbosity < 0)
7597 f8a36e22 2021-08-26 stsp verbosity = 0;
7598 f8a36e22 2021-08-26 stsp else if (verbosity < 3)
7599 f8a36e22 2021-08-26 stsp verbosity++;
7600 f8a36e22 2021-08-26 stsp break;
7601 f8a36e22 2021-08-26 stsp case 'q':
7602 f8a36e22 2021-08-26 stsp verbosity = -1;
7603 f8a36e22 2021-08-26 stsp break;
7604 f8a36e22 2021-08-26 stsp default:
7605 f8a36e22 2021-08-26 stsp usage_send();
7606 f8a36e22 2021-08-26 stsp /* NOTREACHED */
7607 f8a36e22 2021-08-26 stsp }
7608 f8a36e22 2021-08-26 stsp }
7609 f8a36e22 2021-08-26 stsp argc -= optind;
7610 f8a36e22 2021-08-26 stsp argv += optind;
7611 f8a36e22 2021-08-26 stsp
7612 f8a36e22 2021-08-26 stsp if (send_all_branches && !TAILQ_EMPTY(&branches))
7613 f8a36e22 2021-08-26 stsp option_conflict('a', 'b');
7614 f8a36e22 2021-08-26 stsp if (send_all_tags && !TAILQ_EMPTY(&tags))
7615 f8a36e22 2021-08-26 stsp option_conflict('T', 't');
7616 f8a36e22 2021-08-26 stsp
7617 f8a36e22 2021-08-26 stsp
7618 f8a36e22 2021-08-26 stsp if (argc == 0)
7619 f8a36e22 2021-08-26 stsp remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
7620 f8a36e22 2021-08-26 stsp else if (argc == 1)
7621 f8a36e22 2021-08-26 stsp remote_name = argv[0];
7622 f8a36e22 2021-08-26 stsp else
7623 f8a36e22 2021-08-26 stsp usage_send();
7624 f8a36e22 2021-08-26 stsp
7625 f8a36e22 2021-08-26 stsp cwd = getcwd(NULL, 0);
7626 f8a36e22 2021-08-26 stsp if (cwd == NULL) {
7627 f8a36e22 2021-08-26 stsp error = got_error_from_errno("getcwd");
7628 f8a36e22 2021-08-26 stsp goto done;
7629 f8a36e22 2021-08-26 stsp }
7630 f8a36e22 2021-08-26 stsp
7631 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7632 f8a36e22 2021-08-26 stsp error = got_worktree_open(&worktree, cwd);
7633 f8a36e22 2021-08-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7634 f8a36e22 2021-08-26 stsp goto done;
7635 f8a36e22 2021-08-26 stsp else
7636 f8a36e22 2021-08-26 stsp error = NULL;
7637 f8a36e22 2021-08-26 stsp if (worktree) {
7638 f8a36e22 2021-08-26 stsp repo_path =
7639 f8a36e22 2021-08-26 stsp strdup(got_worktree_get_repo_path(worktree));
7640 f8a36e22 2021-08-26 stsp if (repo_path == NULL)
7641 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7642 f8a36e22 2021-08-26 stsp if (error)
7643 f8a36e22 2021-08-26 stsp goto done;
7644 f8a36e22 2021-08-26 stsp } else {
7645 f8a36e22 2021-08-26 stsp repo_path = strdup(cwd);
7646 f8a36e22 2021-08-26 stsp if (repo_path == NULL) {
7647 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7648 f8a36e22 2021-08-26 stsp goto done;
7649 f8a36e22 2021-08-26 stsp }
7650 f8a36e22 2021-08-26 stsp }
7651 f8a36e22 2021-08-26 stsp }
7652 f8a36e22 2021-08-26 stsp
7653 f8a36e22 2021-08-26 stsp error = got_repo_open(&repo, repo_path, NULL);
7654 f8a36e22 2021-08-26 stsp if (error)
7655 f8a36e22 2021-08-26 stsp goto done;
7656 f8a36e22 2021-08-26 stsp
7657 f8a36e22 2021-08-26 stsp if (worktree) {
7658 f8a36e22 2021-08-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
7659 f8a36e22 2021-08-26 stsp if (worktree_conf) {
7660 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7661 f8a36e22 2021-08-26 stsp worktree_conf);
7662 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7663 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7664 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7665 f8a36e22 2021-08-26 stsp break;
7666 f8a36e22 2021-08-26 stsp }
7667 f8a36e22 2021-08-26 stsp }
7668 f8a36e22 2021-08-26 stsp }
7669 f8a36e22 2021-08-26 stsp }
7670 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7671 f8a36e22 2021-08-26 stsp repo_conf = got_repo_get_gotconfig(repo);
7672 f8a36e22 2021-08-26 stsp if (repo_conf) {
7673 f8a36e22 2021-08-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
7674 f8a36e22 2021-08-26 stsp repo_conf);
7675 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7676 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7677 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7678 f8a36e22 2021-08-26 stsp break;
7679 f8a36e22 2021-08-26 stsp }
7680 f8a36e22 2021-08-26 stsp }
7681 f8a36e22 2021-08-26 stsp }
7682 f8a36e22 2021-08-26 stsp }
7683 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7684 f8a36e22 2021-08-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
7685 f8a36e22 2021-08-26 stsp for (i = 0; i < nremotes; i++) {
7686 f8a36e22 2021-08-26 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
7687 f8a36e22 2021-08-26 stsp remote = &remotes[i];
7688 f8a36e22 2021-08-26 stsp break;
7689 f8a36e22 2021-08-26 stsp }
7690 f8a36e22 2021-08-26 stsp }
7691 f8a36e22 2021-08-26 stsp }
7692 f8a36e22 2021-08-26 stsp if (remote == NULL) {
7693 f8a36e22 2021-08-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
7694 f8a36e22 2021-08-26 stsp goto done;
7695 f8a36e22 2021-08-26 stsp }
7696 f8a36e22 2021-08-26 stsp
7697 5e5da8c4 2021-09-05 stsp error = got_dial_parse_uri(&proto, &host, &port, &server_path,
7698 6480c871 2021-08-30 stsp &repo_name, remote->send_url);
7699 f8a36e22 2021-08-26 stsp if (error)
7700 f8a36e22 2021-08-26 stsp goto done;
7701 f8a36e22 2021-08-26 stsp
7702 f8a36e22 2021-08-26 stsp if (strcmp(proto, "git") == 0) {
7703 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7704 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7705 f8a36e22 2021-08-26 stsp "sendfd dns inet unveil", NULL) == -1)
7706 f8a36e22 2021-08-26 stsp err(1, "pledge");
7707 f8a36e22 2021-08-26 stsp #endif
7708 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
7709 f8a36e22 2021-08-26 stsp strcmp(proto, "ssh") == 0) {
7710 f8a36e22 2021-08-26 stsp #ifndef PROFILE
7711 f8a36e22 2021-08-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7712 f8a36e22 2021-08-26 stsp "sendfd unveil", NULL) == -1)
7713 f8a36e22 2021-08-26 stsp err(1, "pledge");
7714 f8a36e22 2021-08-26 stsp #endif
7715 f8a36e22 2021-08-26 stsp } else if (strcmp(proto, "http") == 0 ||
7716 f8a36e22 2021-08-26 stsp strcmp(proto, "git+http") == 0) {
7717 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
7718 f8a36e22 2021-08-26 stsp goto done;
7719 f8a36e22 2021-08-26 stsp } else {
7720 f8a36e22 2021-08-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
7721 f8a36e22 2021-08-26 stsp goto done;
7722 f8a36e22 2021-08-26 stsp }
7723 f8a36e22 2021-08-26 stsp
7724 d65a88a2 2021-09-05 stsp error = got_dial_apply_unveil(proto);
7725 d65a88a2 2021-09-05 stsp if (error)
7726 d65a88a2 2021-09-05 stsp goto done;
7727 d65a88a2 2021-09-05 stsp
7728 f8a36e22 2021-08-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7729 f8a36e22 2021-08-26 stsp if (error)
7730 f8a36e22 2021-08-26 stsp goto done;
7731 f8a36e22 2021-08-26 stsp
7732 f8a36e22 2021-08-26 stsp if (send_all_branches) {
7733 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_branches, repo, "refs/heads",
7734 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7735 f8a36e22 2021-08-26 stsp if (error)
7736 f8a36e22 2021-08-26 stsp goto done;
7737 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_branches, entry) {
7738 f8a36e22 2021-08-26 stsp const char *branchname = got_ref_get_name(re->ref);
7739 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches,
7740 f8a36e22 2021-08-26 stsp branchname, NULL);
7741 f8a36e22 2021-08-26 stsp if (error)
7742 f8a36e22 2021-08-26 stsp goto done;
7743 f8a36e22 2021-08-26 stsp nbranches++;
7744 f8a36e22 2021-08-26 stsp }
7745 eac1df47 2021-09-01 stsp } else if (nbranches == 0) {
7746 eac1df47 2021-09-01 stsp for (i = 0; i < remote->nsend_branches; i++) {
7747 eac1df47 2021-09-01 stsp got_pathlist_append(&branches,
7748 eac1df47 2021-09-01 stsp remote->send_branches[i], NULL);
7749 eac1df47 2021-09-01 stsp }
7750 f8a36e22 2021-08-26 stsp }
7751 f8a36e22 2021-08-26 stsp
7752 f8a36e22 2021-08-26 stsp if (send_all_tags) {
7753 f8a36e22 2021-08-26 stsp error = got_ref_list(&all_tags, repo, "refs/tags",
7754 f8a36e22 2021-08-26 stsp got_ref_cmp_by_name, NULL);
7755 f8a36e22 2021-08-26 stsp if (error)
7756 f8a36e22 2021-08-26 stsp goto done;
7757 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(re, &all_tags, entry) {
7758 f8a36e22 2021-08-26 stsp const char *tagname = got_ref_get_name(re->ref);
7759 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&tags,
7760 f8a36e22 2021-08-26 stsp tagname, NULL);
7761 f8a36e22 2021-08-26 stsp if (error)
7762 f8a36e22 2021-08-26 stsp goto done;
7763 f8a36e22 2021-08-26 stsp ntags++;
7764 f8a36e22 2021-08-26 stsp }
7765 f8a36e22 2021-08-26 stsp }
7766 f8a36e22 2021-08-26 stsp
7767 f8a36e22 2021-08-26 stsp /*
7768 f8a36e22 2021-08-26 stsp * To prevent accidents only branches in refs/heads/ can be deleted
7769 f8a36e22 2021-08-26 stsp * with 'got send -d'.
7770 f8a36e22 2021-08-26 stsp * Deleting anything else requires local repository access or Git.
7771 f8a36e22 2021-08-26 stsp */
7772 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_args, entry) {
7773 f8a36e22 2021-08-26 stsp const char *branchname = pe->path;
7774 f8a36e22 2021-08-26 stsp char *s;
7775 f8a36e22 2021-08-26 stsp struct got_pathlist_entry *new;
7776 f8a36e22 2021-08-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0) {
7777 f8a36e22 2021-08-26 stsp s = strdup(branchname);
7778 f8a36e22 2021-08-26 stsp if (s == NULL) {
7779 f8a36e22 2021-08-26 stsp error = got_error_from_errno("strdup");
7780 f8a36e22 2021-08-26 stsp goto done;
7781 f8a36e22 2021-08-26 stsp }
7782 f8a36e22 2021-08-26 stsp } else {
7783 f8a36e22 2021-08-26 stsp if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
7784 f8a36e22 2021-08-26 stsp error = got_error_from_errno("asprintf");
7785 f8a36e22 2021-08-26 stsp goto done;
7786 f8a36e22 2021-08-26 stsp }
7787 f8a36e22 2021-08-26 stsp }
7788 f8a36e22 2021-08-26 stsp error = got_pathlist_insert(&new, &delete_branches, s, NULL);
7789 f8a36e22 2021-08-26 stsp if (error || new == NULL /* duplicate */)
7790 f8a36e22 2021-08-26 stsp free(s);
7791 f8a36e22 2021-08-26 stsp if (error)
7792 f8a36e22 2021-08-26 stsp goto done;
7793 f8a36e22 2021-08-26 stsp ndelete_branches++;
7794 f8a36e22 2021-08-26 stsp }
7795 f8a36e22 2021-08-26 stsp
7796 f8a36e22 2021-08-26 stsp if (nbranches == 0 && ndelete_branches == 0) {
7797 f8a36e22 2021-08-26 stsp struct got_reference *head_ref;
7798 f8a36e22 2021-08-26 stsp if (worktree)
7799 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo,
7800 f8a36e22 2021-08-26 stsp got_worktree_get_head_ref_name(worktree), 0);
7801 f8a36e22 2021-08-26 stsp else
7802 f8a36e22 2021-08-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
7803 f8a36e22 2021-08-26 stsp if (error)
7804 f8a36e22 2021-08-26 stsp goto done;
7805 f8a36e22 2021-08-26 stsp if (got_ref_is_symbolic(head_ref)) {
7806 f8a36e22 2021-08-26 stsp error = got_ref_resolve_symbolic(&ref, repo, head_ref);
7807 f8a36e22 2021-08-26 stsp got_ref_close(head_ref);
7808 f8a36e22 2021-08-26 stsp if (error)
7809 f8a36e22 2021-08-26 stsp goto done;
7810 f8a36e22 2021-08-26 stsp } else
7811 f8a36e22 2021-08-26 stsp ref = head_ref;
7812 f8a36e22 2021-08-26 stsp error = got_pathlist_append(&branches, got_ref_get_name(ref),
7813 abc59930 2021-09-05 naddy NULL);
7814 f8a36e22 2021-08-26 stsp if (error)
7815 f8a36e22 2021-08-26 stsp goto done;
7816 f8a36e22 2021-08-26 stsp nbranches++;
7817 f8a36e22 2021-08-26 stsp }
7818 f8a36e22 2021-08-26 stsp
7819 f8a36e22 2021-08-26 stsp if (verbosity >= 0)
7820 f8a36e22 2021-08-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
7821 f8a36e22 2021-08-26 stsp port ? ":" : "", port ? port : "");
7822 f8a36e22 2021-08-26 stsp
7823 f8a36e22 2021-08-26 stsp error = got_send_connect(&sendpid, &sendfd, proto, host, port,
7824 f8a36e22 2021-08-26 stsp server_path, verbosity);
7825 f8a36e22 2021-08-26 stsp if (error)
7826 f8a36e22 2021-08-26 stsp goto done;
7827 f8a36e22 2021-08-26 stsp
7828 f8a36e22 2021-08-26 stsp memset(&spa, 0, sizeof(spa));
7829 f8a36e22 2021-08-26 stsp spa.last_scaled_packsize[0] = '\0';
7830 f8a36e22 2021-08-26 stsp spa.last_p_deltify = -1;
7831 f8a36e22 2021-08-26 stsp spa.last_p_written = -1;
7832 f8a36e22 2021-08-26 stsp spa.verbosity = verbosity;
7833 f8a36e22 2021-08-26 stsp spa.delete_branches = &delete_branches;
7834 f8a36e22 2021-08-26 stsp error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
7835 f8a36e22 2021-08-26 stsp verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
7836 f8a36e22 2021-08-26 stsp check_cancelled, NULL);
7837 f8a36e22 2021-08-26 stsp if (spa.printed_something)
7838 f8a36e22 2021-08-26 stsp putchar('\n');
7839 f8a36e22 2021-08-26 stsp if (error)
7840 f8a36e22 2021-08-26 stsp goto done;
7841 f8a36e22 2021-08-26 stsp if (!spa.sent_something && verbosity >= 0)
7842 f8a36e22 2021-08-26 stsp printf("Already up-to-date\n");
7843 f8a36e22 2021-08-26 stsp done:
7844 f8a36e22 2021-08-26 stsp if (sendpid > 0) {
7845 f8a36e22 2021-08-26 stsp if (kill(sendpid, SIGTERM) == -1)
7846 f8a36e22 2021-08-26 stsp error = got_error_from_errno("kill");
7847 f8a36e22 2021-08-26 stsp if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
7848 f8a36e22 2021-08-26 stsp error = got_error_from_errno("waitpid");
7849 f8a36e22 2021-08-26 stsp }
7850 f8a36e22 2021-08-26 stsp if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
7851 f8a36e22 2021-08-26 stsp error = got_error_from_errno("close");
7852 f8a36e22 2021-08-26 stsp if (repo) {
7853 f8a36e22 2021-08-26 stsp const struct got_error *close_err = got_repo_close(repo);
7854 f8a36e22 2021-08-26 stsp if (error == NULL)
7855 f8a36e22 2021-08-26 stsp error = close_err;
7856 f8a36e22 2021-08-26 stsp }
7857 f8a36e22 2021-08-26 stsp if (worktree)
7858 f8a36e22 2021-08-26 stsp got_worktree_close(worktree);
7859 f8a36e22 2021-08-26 stsp if (ref)
7860 f8a36e22 2021-08-26 stsp got_ref_close(ref);
7861 f8a36e22 2021-08-26 stsp got_pathlist_free(&branches);
7862 f8a36e22 2021-08-26 stsp got_pathlist_free(&tags);
7863 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_branches);
7864 f8a36e22 2021-08-26 stsp got_ref_list_free(&all_tags);
7865 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_args);
7866 f8a36e22 2021-08-26 stsp TAILQ_FOREACH(pe, &delete_branches, entry)
7867 f8a36e22 2021-08-26 stsp free((char *)pe->path);
7868 f8a36e22 2021-08-26 stsp got_pathlist_free(&delete_branches);
7869 f8a36e22 2021-08-26 stsp free(cwd);
7870 f8a36e22 2021-08-26 stsp free(repo_path);
7871 f8a36e22 2021-08-26 stsp free(proto);
7872 f8a36e22 2021-08-26 stsp free(host);
7873 f8a36e22 2021-08-26 stsp free(port);
7874 f8a36e22 2021-08-26 stsp free(server_path);
7875 f8a36e22 2021-08-26 stsp free(repo_name);
7876 234035bc 2019-06-01 stsp return error;
7877 234035bc 2019-06-01 stsp }
7878 234035bc 2019-06-01 stsp
7879 234035bc 2019-06-01 stsp __dead static void
7880 234035bc 2019-06-01 stsp usage_cherrypick(void)
7881 234035bc 2019-06-01 stsp {
7882 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
7883 234035bc 2019-06-01 stsp exit(1);
7884 234035bc 2019-06-01 stsp }
7885 234035bc 2019-06-01 stsp
7886 234035bc 2019-06-01 stsp static const struct got_error *
7887 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
7888 234035bc 2019-06-01 stsp {
7889 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
7890 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
7891 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
7892 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
7893 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
7894 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
7895 234035bc 2019-06-01 stsp struct got_object_qid *pid;
7896 9627c110 2020-04-18 stsp int ch;
7897 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7898 234035bc 2019-06-01 stsp
7899 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7900 234035bc 2019-06-01 stsp switch (ch) {
7901 234035bc 2019-06-01 stsp default:
7902 234035bc 2019-06-01 stsp usage_cherrypick();
7903 234035bc 2019-06-01 stsp /* NOTREACHED */
7904 234035bc 2019-06-01 stsp }
7905 234035bc 2019-06-01 stsp }
7906 234035bc 2019-06-01 stsp
7907 234035bc 2019-06-01 stsp argc -= optind;
7908 234035bc 2019-06-01 stsp argv += optind;
7909 234035bc 2019-06-01 stsp
7910 43012d58 2019-07-14 stsp #ifndef PROFILE
7911 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7912 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7913 43012d58 2019-07-14 stsp err(1, "pledge");
7914 43012d58 2019-07-14 stsp #endif
7915 234035bc 2019-06-01 stsp if (argc != 1)
7916 234035bc 2019-06-01 stsp usage_cherrypick();
7917 234035bc 2019-06-01 stsp
7918 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7919 234035bc 2019-06-01 stsp if (cwd == NULL) {
7920 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7921 234035bc 2019-06-01 stsp goto done;
7922 234035bc 2019-06-01 stsp }
7923 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7924 fa51e947 2020-03-27 stsp if (error) {
7925 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7926 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7927 fa51e947 2020-03-27 stsp cwd);
7928 234035bc 2019-06-01 stsp goto done;
7929 fa51e947 2020-03-27 stsp }
7930 234035bc 2019-06-01 stsp
7931 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7932 c9956ddf 2019-09-08 stsp NULL);
7933 234035bc 2019-06-01 stsp if (error != NULL)
7934 234035bc 2019-06-01 stsp goto done;
7935 234035bc 2019-06-01 stsp
7936 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7937 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7938 234035bc 2019-06-01 stsp if (error)
7939 234035bc 2019-06-01 stsp goto done;
7940 234035bc 2019-06-01 stsp
7941 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7942 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7943 234035bc 2019-06-01 stsp if (error != NULL) {
7944 234035bc 2019-06-01 stsp struct got_reference *ref;
7945 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7946 234035bc 2019-06-01 stsp goto done;
7947 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7948 234035bc 2019-06-01 stsp if (error != NULL)
7949 234035bc 2019-06-01 stsp goto done;
7950 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7951 234035bc 2019-06-01 stsp got_ref_close(ref);
7952 234035bc 2019-06-01 stsp if (error != NULL)
7953 234035bc 2019-06-01 stsp goto done;
7954 234035bc 2019-06-01 stsp }
7955 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7956 234035bc 2019-06-01 stsp if (error)
7957 234035bc 2019-06-01 stsp goto done;
7958 234035bc 2019-06-01 stsp
7959 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7960 234035bc 2019-06-01 stsp if (error)
7961 234035bc 2019-06-01 stsp goto done;
7962 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
7963 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7964 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7965 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7966 03415a1a 2019-06-02 stsp NULL);
7967 234035bc 2019-06-01 stsp if (error != NULL)
7968 234035bc 2019-06-01 stsp goto done;
7969 234035bc 2019-06-01 stsp
7970 9627c110 2020-04-18 stsp if (upa.did_something)
7971 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7972 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7973 234035bc 2019-06-01 stsp done:
7974 234035bc 2019-06-01 stsp if (commit)
7975 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7976 234035bc 2019-06-01 stsp free(commit_id_str);
7977 234035bc 2019-06-01 stsp if (worktree)
7978 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7979 1d0f4054 2021-06-17 stsp if (repo) {
7980 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
7981 1d0f4054 2021-06-17 stsp if (error == NULL)
7982 1d0f4054 2021-06-17 stsp error = close_err;
7983 1d0f4054 2021-06-17 stsp }
7984 c4296144 2019-05-09 stsp return error;
7985 c4296144 2019-05-09 stsp }
7986 5ef14e63 2019-06-02 stsp
7987 5ef14e63 2019-06-02 stsp __dead static void
7988 5ef14e63 2019-06-02 stsp usage_backout(void)
7989 5ef14e63 2019-06-02 stsp {
7990 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7991 5ef14e63 2019-06-02 stsp exit(1);
7992 5ef14e63 2019-06-02 stsp }
7993 5ef14e63 2019-06-02 stsp
7994 5ef14e63 2019-06-02 stsp static const struct got_error *
7995 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7996 5ef14e63 2019-06-02 stsp {
7997 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7998 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7999 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
8000 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
8001 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
8002 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
8003 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
8004 9627c110 2020-04-18 stsp int ch;
8005 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8006 5ef14e63 2019-06-02 stsp
8007 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8008 5ef14e63 2019-06-02 stsp switch (ch) {
8009 5ef14e63 2019-06-02 stsp default:
8010 5ef14e63 2019-06-02 stsp usage_backout();
8011 5ef14e63 2019-06-02 stsp /* NOTREACHED */
8012 5ef14e63 2019-06-02 stsp }
8013 5ef14e63 2019-06-02 stsp }
8014 5ef14e63 2019-06-02 stsp
8015 5ef14e63 2019-06-02 stsp argc -= optind;
8016 5ef14e63 2019-06-02 stsp argv += optind;
8017 5ef14e63 2019-06-02 stsp
8018 43012d58 2019-07-14 stsp #ifndef PROFILE
8019 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8020 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8021 43012d58 2019-07-14 stsp err(1, "pledge");
8022 43012d58 2019-07-14 stsp #endif
8023 5ef14e63 2019-06-02 stsp if (argc != 1)
8024 5ef14e63 2019-06-02 stsp usage_backout();
8025 5ef14e63 2019-06-02 stsp
8026 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
8027 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
8028 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
8029 5ef14e63 2019-06-02 stsp goto done;
8030 5ef14e63 2019-06-02 stsp }
8031 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
8032 fa51e947 2020-03-27 stsp if (error) {
8033 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8034 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
8035 5ef14e63 2019-06-02 stsp goto done;
8036 fa51e947 2020-03-27 stsp }
8037 5ef14e63 2019-06-02 stsp
8038 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8039 c9956ddf 2019-09-08 stsp NULL);
8040 5ef14e63 2019-06-02 stsp if (error != NULL)
8041 5ef14e63 2019-06-02 stsp goto done;
8042 5ef14e63 2019-06-02 stsp
8043 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8044 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
8045 5ef14e63 2019-06-02 stsp if (error)
8046 5ef14e63 2019-06-02 stsp goto done;
8047 5ef14e63 2019-06-02 stsp
8048 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
8049 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
8050 5ef14e63 2019-06-02 stsp if (error != NULL) {
8051 5ef14e63 2019-06-02 stsp struct got_reference *ref;
8052 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
8053 5ef14e63 2019-06-02 stsp goto done;
8054 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
8055 5ef14e63 2019-06-02 stsp if (error != NULL)
8056 5ef14e63 2019-06-02 stsp goto done;
8057 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
8058 5ef14e63 2019-06-02 stsp got_ref_close(ref);
8059 5ef14e63 2019-06-02 stsp if (error != NULL)
8060 5ef14e63 2019-06-02 stsp goto done;
8061 5ef14e63 2019-06-02 stsp }
8062 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
8063 5ef14e63 2019-06-02 stsp if (error)
8064 5ef14e63 2019-06-02 stsp goto done;
8065 5ef14e63 2019-06-02 stsp
8066 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8067 5ef14e63 2019-06-02 stsp if (error)
8068 5ef14e63 2019-06-02 stsp goto done;
8069 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8070 5ef14e63 2019-06-02 stsp if (pid == NULL) {
8071 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
8072 5ef14e63 2019-06-02 stsp goto done;
8073 5ef14e63 2019-06-02 stsp }
8074 5ef14e63 2019-06-02 stsp
8075 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8076 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
8077 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8078 5ef14e63 2019-06-02 stsp if (error != NULL)
8079 5ef14e63 2019-06-02 stsp goto done;
8080 5ef14e63 2019-06-02 stsp
8081 9627c110 2020-04-18 stsp if (upa.did_something)
8082 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
8083 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8084 5ef14e63 2019-06-02 stsp done:
8085 5ef14e63 2019-06-02 stsp if (commit)
8086 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
8087 5ef14e63 2019-06-02 stsp free(commit_id_str);
8088 818c7501 2019-07-11 stsp if (worktree)
8089 818c7501 2019-07-11 stsp got_worktree_close(worktree);
8090 1d0f4054 2021-06-17 stsp if (repo) {
8091 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8092 1d0f4054 2021-06-17 stsp if (error == NULL)
8093 1d0f4054 2021-06-17 stsp error = close_err;
8094 1d0f4054 2021-06-17 stsp }
8095 818c7501 2019-07-11 stsp return error;
8096 818c7501 2019-07-11 stsp }
8097 818c7501 2019-07-11 stsp
8098 818c7501 2019-07-11 stsp __dead static void
8099 818c7501 2019-07-11 stsp usage_rebase(void)
8100 818c7501 2019-07-11 stsp {
8101 643b85bc 2021-07-16 stsp fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
8102 af54c8f8 2019-07-11 stsp getprogname());
8103 818c7501 2019-07-11 stsp exit(1);
8104 0ebf8283 2019-07-24 stsp }
8105 0ebf8283 2019-07-24 stsp
8106 0ebf8283 2019-07-24 stsp void
8107 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
8108 0ebf8283 2019-07-24 stsp {
8109 0ebf8283 2019-07-24 stsp char *nl;
8110 0ebf8283 2019-07-24 stsp size_t len;
8111 0ebf8283 2019-07-24 stsp
8112 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
8113 0ebf8283 2019-07-24 stsp if (len > limit)
8114 0ebf8283 2019-07-24 stsp len = limit;
8115 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
8116 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
8117 0ebf8283 2019-07-24 stsp if (nl)
8118 0ebf8283 2019-07-24 stsp *nl = '\0';
8119 0ebf8283 2019-07-24 stsp }
8120 0ebf8283 2019-07-24 stsp
8121 0ebf8283 2019-07-24 stsp static const struct got_error *
8122 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
8123 0ebf8283 2019-07-24 stsp {
8124 5943eee2 2019-08-13 stsp const struct got_error *err;
8125 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
8126 5943eee2 2019-08-13 stsp const char *s;
8127 0ebf8283 2019-07-24 stsp
8128 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8129 5943eee2 2019-08-13 stsp if (err)
8130 5943eee2 2019-08-13 stsp return err;
8131 0ebf8283 2019-07-24 stsp
8132 5943eee2 2019-08-13 stsp s = logmsg0;
8133 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
8134 5943eee2 2019-08-13 stsp s++;
8135 5943eee2 2019-08-13 stsp
8136 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
8137 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
8138 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
8139 5943eee2 2019-08-13 stsp goto done;
8140 5943eee2 2019-08-13 stsp }
8141 0ebf8283 2019-07-24 stsp
8142 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
8143 5943eee2 2019-08-13 stsp done:
8144 5943eee2 2019-08-13 stsp free(logmsg0);
8145 a0ea4fc0 2020-02-28 stsp return err;
8146 a0ea4fc0 2020-02-28 stsp }
8147 a0ea4fc0 2020-02-28 stsp
8148 a0ea4fc0 2020-02-28 stsp static const struct got_error *
8149 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
8150 a0ea4fc0 2020-02-28 stsp {
8151 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
8152 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
8153 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
8154 a0ea4fc0 2020-02-28 stsp
8155 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
8156 a0ea4fc0 2020-02-28 stsp if (err)
8157 a0ea4fc0 2020-02-28 stsp return err;
8158 a0ea4fc0 2020-02-28 stsp
8159 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
8160 a0ea4fc0 2020-02-28 stsp if (err)
8161 a0ea4fc0 2020-02-28 stsp goto done;
8162 a0ea4fc0 2020-02-28 stsp
8163 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
8164 a0ea4fc0 2020-02-28 stsp
8165 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
8166 a0ea4fc0 2020-02-28 stsp if (err)
8167 a0ea4fc0 2020-02-28 stsp goto done;
8168 a0ea4fc0 2020-02-28 stsp
8169 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
8170 a0ea4fc0 2020-02-28 stsp done:
8171 a0ea4fc0 2020-02-28 stsp free(id_str);
8172 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
8173 a0ea4fc0 2020-02-28 stsp free(logmsg);
8174 5943eee2 2019-08-13 stsp return err;
8175 818c7501 2019-07-11 stsp }
8176 818c7501 2019-07-11 stsp
8177 818c7501 2019-07-11 stsp static const struct got_error *
8178 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
8179 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
8180 818c7501 2019-07-11 stsp {
8181 818c7501 2019-07-11 stsp const struct got_error *err;
8182 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8183 818c7501 2019-07-11 stsp
8184 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
8185 818c7501 2019-07-11 stsp if (err)
8186 818c7501 2019-07-11 stsp goto done;
8187 818c7501 2019-07-11 stsp
8188 ff0d2220 2019-07-11 stsp if (new_id) {
8189 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
8190 ff0d2220 2019-07-11 stsp if (err)
8191 ff0d2220 2019-07-11 stsp goto done;
8192 ff0d2220 2019-07-11 stsp }
8193 818c7501 2019-07-11 stsp
8194 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
8195 ff0d2220 2019-07-11 stsp if (new_id_str)
8196 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
8197 0ebf8283 2019-07-24 stsp
8198 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8199 0ebf8283 2019-07-24 stsp if (err)
8200 0ebf8283 2019-07-24 stsp goto done;
8201 0ebf8283 2019-07-24 stsp
8202 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
8203 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8204 818c7501 2019-07-11 stsp done:
8205 818c7501 2019-07-11 stsp free(old_id_str);
8206 818c7501 2019-07-11 stsp free(new_id_str);
8207 272a1371 2020-02-28 stsp free(logmsg);
8208 818c7501 2019-07-11 stsp return err;
8209 818c7501 2019-07-11 stsp }
8210 818c7501 2019-07-11 stsp
8211 1ee397ad 2019-07-12 stsp static const struct got_error *
8212 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
8213 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
8214 e600f124 2021-03-21 stsp struct got_reference *tmp_branch, struct got_repository *repo,
8215 e600f124 2021-03-21 stsp int create_backup)
8216 818c7501 2019-07-11 stsp {
8217 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
8218 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
8219 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, branch, repo, create_backup);
8220 818c7501 2019-07-11 stsp }
8221 818c7501 2019-07-11 stsp
8222 818c7501 2019-07-11 stsp static const struct got_error *
8223 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
8224 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8225 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
8226 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
8227 ff0d2220 2019-07-11 stsp {
8228 ff0d2220 2019-07-11 stsp const struct got_error *error;
8229 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
8230 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
8231 ff0d2220 2019-07-11 stsp
8232 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8233 ff0d2220 2019-07-11 stsp if (error)
8234 ff0d2220 2019-07-11 stsp return error;
8235 ff0d2220 2019-07-11 stsp
8236 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
8237 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
8238 ff0d2220 2019-07-11 stsp if (error) {
8239 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
8240 ff0d2220 2019-07-11 stsp goto done;
8241 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
8242 ff0d2220 2019-07-11 stsp } else {
8243 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
8244 ff0d2220 2019-07-11 stsp free(new_commit_id);
8245 ff0d2220 2019-07-11 stsp }
8246 ff0d2220 2019-07-11 stsp done:
8247 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
8248 ff0d2220 2019-07-11 stsp return error;
8249 64c6d990 2019-07-11 stsp }
8250 64c6d990 2019-07-11 stsp
8251 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
8252 64c6d990 2019-07-11 stsp const char *path_prefix;
8253 64c6d990 2019-07-11 stsp size_t len;
8254 8ca9bd68 2019-07-25 stsp int errcode;
8255 64c6d990 2019-07-11 stsp };
8256 64c6d990 2019-07-11 stsp
8257 64c6d990 2019-07-11 stsp static const struct got_error *
8258 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
8259 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
8260 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
8261 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
8262 64c6d990 2019-07-11 stsp {
8263 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
8264 64c6d990 2019-07-11 stsp
8265 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
8266 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
8267 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
8268 64c6d990 2019-07-11 stsp
8269 64c6d990 2019-07-11 stsp return NULL;
8270 64c6d990 2019-07-11 stsp }
8271 64c6d990 2019-07-11 stsp
8272 64c6d990 2019-07-11 stsp static const struct got_error *
8273 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
8274 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
8275 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
8276 64c6d990 2019-07-11 stsp {
8277 64c6d990 2019-07-11 stsp const struct got_error *err;
8278 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
8279 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
8280 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
8281 64c6d990 2019-07-11 stsp
8282 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
8283 64c6d990 2019-07-11 stsp return NULL;
8284 64c6d990 2019-07-11 stsp
8285 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
8286 64c6d990 2019-07-11 stsp if (err)
8287 64c6d990 2019-07-11 stsp goto done;
8288 64c6d990 2019-07-11 stsp
8289 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
8290 64c6d990 2019-07-11 stsp if (err)
8291 64c6d990 2019-07-11 stsp goto done;
8292 64c6d990 2019-07-11 stsp
8293 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
8294 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
8295 64c6d990 2019-07-11 stsp if (err)
8296 64c6d990 2019-07-11 stsp goto done;
8297 64c6d990 2019-07-11 stsp
8298 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
8299 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
8300 64c6d990 2019-07-11 stsp if (err)
8301 64c6d990 2019-07-11 stsp goto done;
8302 64c6d990 2019-07-11 stsp
8303 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
8304 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
8305 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
8306 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
8307 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
8308 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
8309 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
8310 64c6d990 2019-07-11 stsp done:
8311 64c6d990 2019-07-11 stsp if (tree1)
8312 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
8313 64c6d990 2019-07-11 stsp if (tree2)
8314 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
8315 64c6d990 2019-07-11 stsp if (commit)
8316 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
8317 64c6d990 2019-07-11 stsp if (parent_commit)
8318 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
8319 64c6d990 2019-07-11 stsp return err;
8320 ff0d2220 2019-07-11 stsp }
8321 ff0d2220 2019-07-11 stsp
8322 ff0d2220 2019-07-11 stsp static const struct got_error *
8323 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
8324 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
8325 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
8326 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
8327 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
8328 af61c510 2019-07-19 stsp {
8329 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
8330 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
8331 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
8332 af61c510 2019-07-19 stsp struct got_object_qid *qid;
8333 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
8334 af61c510 2019-07-19 stsp
8335 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
8336 af61c510 2019-07-19 stsp if (err)
8337 af61c510 2019-07-19 stsp return err;
8338 af61c510 2019-07-19 stsp
8339 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
8340 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8341 af61c510 2019-07-19 stsp if (err)
8342 af61c510 2019-07-19 stsp goto done;
8343 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
8344 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
8345 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
8346 af61c510 2019-07-19 stsp if (err) {
8347 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
8348 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
8349 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
8350 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
8351 af61c510 2019-07-19 stsp "been reached?!?");
8352 ee780d5c 2020-01-04 stsp }
8353 ee780d5c 2020-01-04 stsp goto done;
8354 af61c510 2019-07-19 stsp } else {
8355 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
8356 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
8357 af61c510 2019-07-19 stsp if (err)
8358 af61c510 2019-07-19 stsp goto done;
8359 af61c510 2019-07-19 stsp
8360 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
8361 af61c510 2019-07-19 stsp if (err)
8362 af61c510 2019-07-19 stsp goto done;
8363 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(commits, qid, entry);
8364 af61c510 2019-07-19 stsp commit_id = parent_id;
8365 af61c510 2019-07-19 stsp }
8366 af61c510 2019-07-19 stsp }
8367 af61c510 2019-07-19 stsp done:
8368 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
8369 af61c510 2019-07-19 stsp return err;
8370 e600f124 2021-03-21 stsp }
8371 e600f124 2021-03-21 stsp
8372 e600f124 2021-03-21 stsp static const struct got_error *
8373 e600f124 2021-03-21 stsp get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
8374 e600f124 2021-03-21 stsp {
8375 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8376 e600f124 2021-03-21 stsp time_t committer_time;
8377 e600f124 2021-03-21 stsp struct tm tm;
8378 e600f124 2021-03-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
8379 e600f124 2021-03-21 stsp char *author0 = NULL, *author, *smallerthan;
8380 e600f124 2021-03-21 stsp char *logmsg0 = NULL, *logmsg, *newline;
8381 e600f124 2021-03-21 stsp
8382 e600f124 2021-03-21 stsp committer_time = got_object_commit_get_committer_time(commit);
8383 e385fc42 2021-08-30 stsp if (gmtime_r(&committer_time, &tm) == NULL)
8384 e385fc42 2021-08-30 stsp return got_error_from_errno("gmtime_r");
8385 e3199de8 2021-03-21 stsp if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
8386 e600f124 2021-03-21 stsp return got_error(GOT_ERR_NO_SPACE);
8387 e600f124 2021-03-21 stsp
8388 e600f124 2021-03-21 stsp author0 = strdup(got_object_commit_get_author(commit));
8389 e600f124 2021-03-21 stsp if (author0 == NULL)
8390 e600f124 2021-03-21 stsp return got_error_from_errno("strdup");
8391 e600f124 2021-03-21 stsp author = author0;
8392 e600f124 2021-03-21 stsp smallerthan = strchr(author, '<');
8393 e600f124 2021-03-21 stsp if (smallerthan && smallerthan[1] != '\0')
8394 e600f124 2021-03-21 stsp author = smallerthan + 1;
8395 e600f124 2021-03-21 stsp author[strcspn(author, "@>")] = '\0';
8396 e600f124 2021-03-21 stsp
8397 e600f124 2021-03-21 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
8398 e600f124 2021-03-21 stsp if (err)
8399 e600f124 2021-03-21 stsp goto done;
8400 e600f124 2021-03-21 stsp logmsg = logmsg0;
8401 e600f124 2021-03-21 stsp while (*logmsg == '\n')
8402 e600f124 2021-03-21 stsp logmsg++;
8403 e600f124 2021-03-21 stsp newline = strchr(logmsg, '\n');
8404 e600f124 2021-03-21 stsp if (newline)
8405 e600f124 2021-03-21 stsp *newline = '\0';
8406 e600f124 2021-03-21 stsp
8407 e600f124 2021-03-21 stsp if (asprintf(brief_str, "%s %s %s",
8408 e600f124 2021-03-21 stsp datebuf, author, logmsg) == -1)
8409 e600f124 2021-03-21 stsp err = got_error_from_errno("asprintf");
8410 e600f124 2021-03-21 stsp done:
8411 e600f124 2021-03-21 stsp free(author0);
8412 e600f124 2021-03-21 stsp free(logmsg0);
8413 643b85bc 2021-07-16 stsp return err;
8414 643b85bc 2021-07-16 stsp }
8415 643b85bc 2021-07-16 stsp
8416 643b85bc 2021-07-16 stsp static const struct got_error *
8417 643b85bc 2021-07-16 stsp delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
8418 643b85bc 2021-07-16 stsp struct got_repository *repo)
8419 643b85bc 2021-07-16 stsp {
8420 643b85bc 2021-07-16 stsp const struct got_error *err;
8421 643b85bc 2021-07-16 stsp char *id_str;
8422 643b85bc 2021-07-16 stsp
8423 643b85bc 2021-07-16 stsp err = got_object_id_str(&id_str, id);
8424 643b85bc 2021-07-16 stsp if (err)
8425 643b85bc 2021-07-16 stsp return err;
8426 643b85bc 2021-07-16 stsp
8427 643b85bc 2021-07-16 stsp err = got_ref_delete(ref, repo);
8428 643b85bc 2021-07-16 stsp if (err)
8429 643b85bc 2021-07-16 stsp goto done;
8430 643b85bc 2021-07-16 stsp
8431 643b85bc 2021-07-16 stsp printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
8432 643b85bc 2021-07-16 stsp done:
8433 643b85bc 2021-07-16 stsp free(id_str);
8434 e600f124 2021-03-21 stsp return err;
8435 e600f124 2021-03-21 stsp }
8436 e600f124 2021-03-21 stsp
8437 e600f124 2021-03-21 stsp static const struct got_error *
8438 e600f124 2021-03-21 stsp print_backup_ref(const char *branch_name, const char *new_id_str,
8439 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
8440 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap,
8441 e600f124 2021-03-21 stsp struct got_repository *repo)
8442 e600f124 2021-03-21 stsp {
8443 e600f124 2021-03-21 stsp const struct got_error *err = NULL;
8444 e600f124 2021-03-21 stsp struct got_reflist_head *refs;
8445 e600f124 2021-03-21 stsp char *refs_str = NULL;
8446 e600f124 2021-03-21 stsp struct got_object_id *new_commit_id = NULL;
8447 e600f124 2021-03-21 stsp struct got_commit_object *new_commit = NULL;
8448 e600f124 2021-03-21 stsp char *new_commit_brief_str = NULL;
8449 e600f124 2021-03-21 stsp struct got_object_id *yca_id = NULL;
8450 e600f124 2021-03-21 stsp struct got_commit_object *yca_commit = NULL;
8451 e600f124 2021-03-21 stsp char *yca_id_str = NULL, *yca_brief_str = NULL;
8452 e600f124 2021-03-21 stsp char *custom_refs_str;
8453 e600f124 2021-03-21 stsp
8454 e600f124 2021-03-21 stsp if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
8455 e600f124 2021-03-21 stsp return got_error_from_errno("asprintf");
8456 e600f124 2021-03-21 stsp
8457 e600f124 2021-03-21 stsp err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
8458 e600f124 2021-03-21 stsp 0, 0, refs_idmap, custom_refs_str);
8459 e600f124 2021-03-21 stsp if (err)
8460 e600f124 2021-03-21 stsp goto done;
8461 e600f124 2021-03-21 stsp
8462 e600f124 2021-03-21 stsp err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
8463 e600f124 2021-03-21 stsp if (err)
8464 e600f124 2021-03-21 stsp goto done;
8465 e600f124 2021-03-21 stsp
8466 e600f124 2021-03-21 stsp refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
8467 e600f124 2021-03-21 stsp if (refs) {
8468 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, new_commit_id, repo);
8469 e600f124 2021-03-21 stsp if (err)
8470 e600f124 2021-03-21 stsp goto done;
8471 e600f124 2021-03-21 stsp }
8472 e600f124 2021-03-21 stsp
8473 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
8474 e600f124 2021-03-21 stsp if (err)
8475 e600f124 2021-03-21 stsp goto done;
8476 e600f124 2021-03-21 stsp
8477 e600f124 2021-03-21 stsp err = get_commit_brief_str(&new_commit_brief_str, new_commit);
8478 e600f124 2021-03-21 stsp if (err)
8479 e600f124 2021-03-21 stsp goto done;
8480 e600f124 2021-03-21 stsp
8481 e600f124 2021-03-21 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8482 e600f124 2021-03-21 stsp old_commit_id, new_commit_id, repo, check_cancelled, NULL);
8483 e600f124 2021-03-21 stsp if (err)
8484 e600f124 2021-03-21 stsp goto done;
8485 e600f124 2021-03-21 stsp
8486 e600f124 2021-03-21 stsp printf("has become commit %s%s%s%s\n %s\n", new_id_str,
8487 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8488 e600f124 2021-03-21 stsp refs_str ? ")" : "", new_commit_brief_str);
8489 e600f124 2021-03-21 stsp if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
8490 e600f124 2021-03-21 stsp got_object_id_cmp(yca_id, old_commit_id) != 0) {
8491 e600f124 2021-03-21 stsp free(refs_str);
8492 e600f124 2021-03-21 stsp refs_str = NULL;
8493 e600f124 2021-03-21 stsp
8494 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&yca_commit, repo, yca_id);
8495 e600f124 2021-03-21 stsp if (err)
8496 e600f124 2021-03-21 stsp goto done;
8497 e600f124 2021-03-21 stsp
8498 e600f124 2021-03-21 stsp err = get_commit_brief_str(&yca_brief_str, yca_commit);
8499 e600f124 2021-03-21 stsp if (err)
8500 e600f124 2021-03-21 stsp goto done;
8501 e600f124 2021-03-21 stsp
8502 e600f124 2021-03-21 stsp err = got_object_id_str(&yca_id_str, 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 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
8507 e600f124 2021-03-21 stsp if (refs) {
8508 e600f124 2021-03-21 stsp err = build_refs_str(&refs_str, refs, yca_id, repo);
8509 e600f124 2021-03-21 stsp if (err)
8510 e600f124 2021-03-21 stsp goto done;
8511 e600f124 2021-03-21 stsp }
8512 e600f124 2021-03-21 stsp printf("history forked at %s%s%s%s\n %s\n",
8513 e600f124 2021-03-21 stsp yca_id_str,
8514 e600f124 2021-03-21 stsp refs_str ? " (" : "", refs_str ? refs_str : "",
8515 e600f124 2021-03-21 stsp refs_str ? ")" : "", yca_brief_str);
8516 e600f124 2021-03-21 stsp }
8517 e600f124 2021-03-21 stsp done:
8518 e600f124 2021-03-21 stsp free(custom_refs_str);
8519 e600f124 2021-03-21 stsp free(new_commit_id);
8520 e600f124 2021-03-21 stsp free(refs_str);
8521 e600f124 2021-03-21 stsp free(yca_id);
8522 e600f124 2021-03-21 stsp free(yca_id_str);
8523 e600f124 2021-03-21 stsp free(yca_brief_str);
8524 e600f124 2021-03-21 stsp if (new_commit)
8525 e600f124 2021-03-21 stsp got_object_commit_close(new_commit);
8526 e600f124 2021-03-21 stsp if (yca_commit)
8527 e600f124 2021-03-21 stsp got_object_commit_close(yca_commit);
8528 e600f124 2021-03-21 stsp
8529 e600f124 2021-03-21 stsp return NULL;
8530 af61c510 2019-07-19 stsp }
8531 af61c510 2019-07-19 stsp
8532 af61c510 2019-07-19 stsp static const struct got_error *
8533 643b85bc 2021-07-16 stsp process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
8534 643b85bc 2021-07-16 stsp int delete, struct got_repository *repo)
8535 e600f124 2021-03-21 stsp {
8536 e600f124 2021-03-21 stsp const struct got_error *err;
8537 e600f124 2021-03-21 stsp struct got_reflist_head refs, backup_refs;
8538 e600f124 2021-03-21 stsp struct got_reflist_entry *re;
8539 e600f124 2021-03-21 stsp const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
8540 e600f124 2021-03-21 stsp struct got_object_id *old_commit_id = NULL;
8541 e600f124 2021-03-21 stsp char *branch_name = NULL;
8542 e600f124 2021-03-21 stsp struct got_commit_object *old_commit = NULL;
8543 e600f124 2021-03-21 stsp struct got_reflist_object_id_map *refs_idmap = NULL;
8544 9e822917 2021-03-23 stsp int wanted_branch_found = 0;
8545 e600f124 2021-03-21 stsp
8546 e600f124 2021-03-21 stsp TAILQ_INIT(&refs);
8547 e600f124 2021-03-21 stsp TAILQ_INIT(&backup_refs);
8548 e600f124 2021-03-21 stsp
8549 e600f124 2021-03-21 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8550 e600f124 2021-03-21 stsp if (err)
8551 e600f124 2021-03-21 stsp return err;
8552 e600f124 2021-03-21 stsp
8553 e600f124 2021-03-21 stsp err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
8554 e600f124 2021-03-21 stsp if (err)
8555 e600f124 2021-03-21 stsp goto done;
8556 e600f124 2021-03-21 stsp
8557 e600f124 2021-03-21 stsp if (wanted_branch_name) {
8558 e600f124 2021-03-21 stsp if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
8559 e600f124 2021-03-21 stsp wanted_branch_name += 11;
8560 e600f124 2021-03-21 stsp }
8561 e600f124 2021-03-21 stsp
8562 e600f124 2021-03-21 stsp err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
8563 e600f124 2021-03-21 stsp got_ref_cmp_by_commit_timestamp_descending, repo);
8564 e600f124 2021-03-21 stsp if (err)
8565 e600f124 2021-03-21 stsp goto done;
8566 e600f124 2021-03-21 stsp
8567 e600f124 2021-03-21 stsp TAILQ_FOREACH(re, &backup_refs, entry) {
8568 e600f124 2021-03-21 stsp const char *refname = got_ref_get_name(re->ref);
8569 e600f124 2021-03-21 stsp char *slash;
8570 e600f124 2021-03-21 stsp
8571 643b85bc 2021-07-16 stsp err = check_cancelled(NULL);
8572 643b85bc 2021-07-16 stsp if (err)
8573 643b85bc 2021-07-16 stsp break;
8574 643b85bc 2021-07-16 stsp
8575 e600f124 2021-03-21 stsp err = got_ref_resolve(&old_commit_id, repo, re->ref);
8576 e600f124 2021-03-21 stsp if (err)
8577 e600f124 2021-03-21 stsp break;
8578 e600f124 2021-03-21 stsp
8579 e600f124 2021-03-21 stsp err = got_object_open_as_commit(&old_commit, repo,
8580 e600f124 2021-03-21 stsp old_commit_id);
8581 e600f124 2021-03-21 stsp if (err)
8582 e600f124 2021-03-21 stsp break;
8583 e600f124 2021-03-21 stsp
8584 e600f124 2021-03-21 stsp if (strncmp(backup_ref_prefix, refname,
8585 e600f124 2021-03-21 stsp backup_ref_prefix_len) == 0)
8586 e600f124 2021-03-21 stsp refname += backup_ref_prefix_len;
8587 e600f124 2021-03-21 stsp
8588 e600f124 2021-03-21 stsp while (refname[0] == '/')
8589 e600f124 2021-03-21 stsp refname++;
8590 e600f124 2021-03-21 stsp
8591 e600f124 2021-03-21 stsp branch_name = strdup(refname);
8592 e600f124 2021-03-21 stsp if (branch_name == NULL) {
8593 e600f124 2021-03-21 stsp err = got_error_from_errno("strdup");
8594 e600f124 2021-03-21 stsp break;
8595 e600f124 2021-03-21 stsp }
8596 e600f124 2021-03-21 stsp slash = strrchr(branch_name, '/');
8597 e600f124 2021-03-21 stsp if (slash) {
8598 e600f124 2021-03-21 stsp *slash = '\0';
8599 e600f124 2021-03-21 stsp refname += strlen(branch_name) + 1;
8600 e600f124 2021-03-21 stsp }
8601 e600f124 2021-03-21 stsp
8602 e600f124 2021-03-21 stsp if (wanted_branch_name == NULL ||
8603 e600f124 2021-03-21 stsp strcmp(wanted_branch_name, branch_name) == 0) {
8604 9e822917 2021-03-23 stsp wanted_branch_found = 1;
8605 643b85bc 2021-07-16 stsp if (delete) {
8606 643b85bc 2021-07-16 stsp err = delete_backup_ref(re->ref,
8607 643b85bc 2021-07-16 stsp old_commit_id, repo);
8608 643b85bc 2021-07-16 stsp } else {
8609 643b85bc 2021-07-16 stsp err = print_backup_ref(branch_name, refname,
8610 abc59930 2021-09-05 naddy old_commit_id, old_commit, refs_idmap,
8611 abc59930 2021-09-05 naddy repo);
8612 643b85bc 2021-07-16 stsp }
8613 e600f124 2021-03-21 stsp if (err)
8614 e600f124 2021-03-21 stsp break;
8615 e600f124 2021-03-21 stsp }
8616 e600f124 2021-03-21 stsp
8617 e600f124 2021-03-21 stsp free(old_commit_id);
8618 e600f124 2021-03-21 stsp old_commit_id = NULL;
8619 e600f124 2021-03-21 stsp free(branch_name);
8620 e600f124 2021-03-21 stsp branch_name = NULL;
8621 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8622 e600f124 2021-03-21 stsp old_commit = NULL;
8623 e600f124 2021-03-21 stsp }
8624 9e822917 2021-03-23 stsp
8625 9e822917 2021-03-23 stsp if (wanted_branch_name && !wanted_branch_found) {
8626 9e822917 2021-03-23 stsp err = got_error_fmt(GOT_ERR_NOT_REF,
8627 9e822917 2021-03-23 stsp "%s/%s/", backup_ref_prefix, wanted_branch_name);
8628 9e822917 2021-03-23 stsp }
8629 e600f124 2021-03-21 stsp done:
8630 e600f124 2021-03-21 stsp if (refs_idmap)
8631 e600f124 2021-03-21 stsp got_reflist_object_id_map_free(refs_idmap);
8632 e600f124 2021-03-21 stsp got_ref_list_free(&refs);
8633 e600f124 2021-03-21 stsp got_ref_list_free(&backup_refs);
8634 e600f124 2021-03-21 stsp free(old_commit_id);
8635 e600f124 2021-03-21 stsp free(branch_name);
8636 e600f124 2021-03-21 stsp if (old_commit)
8637 e600f124 2021-03-21 stsp got_object_commit_close(old_commit);
8638 e600f124 2021-03-21 stsp return err;
8639 e600f124 2021-03-21 stsp }
8640 e600f124 2021-03-21 stsp
8641 e600f124 2021-03-21 stsp static const struct got_error *
8642 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
8643 818c7501 2019-07-11 stsp {
8644 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
8645 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
8646 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
8647 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8648 818c7501 2019-07-11 stsp char *cwd = NULL;
8649 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
8650 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
8651 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
8652 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
8653 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
8654 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
8655 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
8656 e600f124 2021-03-21 stsp int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
8657 643b85bc 2021-07-16 stsp int delete_backups = 0;
8658 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8659 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
8660 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
8661 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
8662 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
8663 818c7501 2019-07-11 stsp
8664 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
8665 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
8666 818c7501 2019-07-11 stsp
8667 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "aclX")) != -1) {
8668 818c7501 2019-07-11 stsp switch (ch) {
8669 818c7501 2019-07-11 stsp case 'a':
8670 818c7501 2019-07-11 stsp abort_rebase = 1;
8671 818c7501 2019-07-11 stsp break;
8672 818c7501 2019-07-11 stsp case 'c':
8673 818c7501 2019-07-11 stsp continue_rebase = 1;
8674 818c7501 2019-07-11 stsp break;
8675 e600f124 2021-03-21 stsp case 'l':
8676 e600f124 2021-03-21 stsp list_backups = 1;
8677 e600f124 2021-03-21 stsp break;
8678 643b85bc 2021-07-16 stsp case 'X':
8679 643b85bc 2021-07-16 stsp delete_backups = 1;
8680 643b85bc 2021-07-16 stsp break;
8681 818c7501 2019-07-11 stsp default:
8682 818c7501 2019-07-11 stsp usage_rebase();
8683 818c7501 2019-07-11 stsp /* NOTREACHED */
8684 818c7501 2019-07-11 stsp }
8685 818c7501 2019-07-11 stsp }
8686 818c7501 2019-07-11 stsp
8687 818c7501 2019-07-11 stsp argc -= optind;
8688 818c7501 2019-07-11 stsp argv += optind;
8689 818c7501 2019-07-11 stsp
8690 43012d58 2019-07-14 stsp #ifndef PROFILE
8691 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8692 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
8693 43012d58 2019-07-14 stsp err(1, "pledge");
8694 43012d58 2019-07-14 stsp #endif
8695 e600f124 2021-03-21 stsp if (list_backups) {
8696 e600f124 2021-03-21 stsp if (abort_rebase)
8697 e600f124 2021-03-21 stsp option_conflict('l', 'a');
8698 e600f124 2021-03-21 stsp if (continue_rebase)
8699 e600f124 2021-03-21 stsp option_conflict('l', 'c');
8700 643b85bc 2021-07-16 stsp if (delete_backups)
8701 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8702 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
8703 643b85bc 2021-07-16 stsp usage_rebase();
8704 643b85bc 2021-07-16 stsp } else if (delete_backups) {
8705 643b85bc 2021-07-16 stsp if (abort_rebase)
8706 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
8707 643b85bc 2021-07-16 stsp if (continue_rebase)
8708 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
8709 643b85bc 2021-07-16 stsp if (list_backups)
8710 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
8711 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
8712 818c7501 2019-07-11 stsp usage_rebase();
8713 e600f124 2021-03-21 stsp } else {
8714 e600f124 2021-03-21 stsp if (abort_rebase && continue_rebase)
8715 e600f124 2021-03-21 stsp usage_rebase();
8716 e600f124 2021-03-21 stsp else if (abort_rebase || continue_rebase) {
8717 e600f124 2021-03-21 stsp if (argc != 0)
8718 e600f124 2021-03-21 stsp usage_rebase();
8719 e600f124 2021-03-21 stsp } else if (argc != 1)
8720 e600f124 2021-03-21 stsp usage_rebase();
8721 e600f124 2021-03-21 stsp }
8722 818c7501 2019-07-11 stsp
8723 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
8724 818c7501 2019-07-11 stsp if (cwd == NULL) {
8725 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
8726 818c7501 2019-07-11 stsp goto done;
8727 818c7501 2019-07-11 stsp }
8728 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
8729 fa51e947 2020-03-27 stsp if (error) {
8730 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8731 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
8732 e600f124 2021-03-21 stsp goto done;
8733 e600f124 2021-03-21 stsp } else {
8734 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8735 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
8736 e600f124 2021-03-21 stsp "rebase", cwd);
8737 e600f124 2021-03-21 stsp goto done;
8738 e600f124 2021-03-21 stsp }
8739 fa51e947 2020-03-27 stsp }
8740 818c7501 2019-07-11 stsp
8741 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
8742 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL);
8743 818c7501 2019-07-11 stsp if (error != NULL)
8744 818c7501 2019-07-11 stsp goto done;
8745 818c7501 2019-07-11 stsp
8746 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8747 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
8748 7ef62c4e 2020-02-24 stsp if (error)
8749 7ef62c4e 2020-02-24 stsp goto done;
8750 7ef62c4e 2020-02-24 stsp
8751 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
8752 643b85bc 2021-07-16 stsp error = process_backup_refs(
8753 643b85bc 2021-07-16 stsp GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
8754 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
8755 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
8756 e600f124 2021-03-21 stsp }
8757 e600f124 2021-03-21 stsp
8758 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
8759 7ef62c4e 2020-02-24 stsp worktree);
8760 818c7501 2019-07-11 stsp if (error)
8761 7ef62c4e 2020-02-24 stsp goto done;
8762 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
8763 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8764 818c7501 2019-07-11 stsp goto done;
8765 7ef62c4e 2020-02-24 stsp }
8766 818c7501 2019-07-11 stsp
8767 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8768 818c7501 2019-07-11 stsp if (error)
8769 818c7501 2019-07-11 stsp goto done;
8770 818c7501 2019-07-11 stsp
8771 f6794adc 2019-07-23 stsp if (abort_rebase) {
8772 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8773 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8774 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8775 f6794adc 2019-07-23 stsp goto done;
8776 f6794adc 2019-07-23 stsp }
8777 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8778 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8779 3e3a69f1 2019-07-25 stsp worktree, repo);
8780 818c7501 2019-07-11 stsp if (error)
8781 818c7501 2019-07-11 stsp goto done;
8782 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
8783 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
8784 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8785 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
8786 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
8787 818c7501 2019-07-11 stsp if (error)
8788 818c7501 2019-07-11 stsp goto done;
8789 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
8790 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8791 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
8792 818c7501 2019-07-11 stsp }
8793 818c7501 2019-07-11 stsp
8794 818c7501 2019-07-11 stsp if (continue_rebase) {
8795 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
8796 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
8797 f6794adc 2019-07-23 stsp goto done;
8798 f6794adc 2019-07-23 stsp }
8799 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
8800 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
8801 3e3a69f1 2019-07-25 stsp worktree, repo);
8802 818c7501 2019-07-11 stsp if (error)
8803 818c7501 2019-07-11 stsp goto done;
8804 818c7501 2019-07-11 stsp
8805 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
8806 01757395 2019-07-12 stsp resume_commit_id, repo);
8807 818c7501 2019-07-11 stsp if (error)
8808 818c7501 2019-07-11 stsp goto done;
8809 818c7501 2019-07-11 stsp
8810 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
8811 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
8812 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
8813 818c7501 2019-07-11 stsp goto done;
8814 818c7501 2019-07-11 stsp }
8815 818c7501 2019-07-11 stsp } else {
8816 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
8817 818c7501 2019-07-11 stsp if (error != NULL)
8818 818c7501 2019-07-11 stsp goto done;
8819 ff0d2220 2019-07-11 stsp }
8820 818c7501 2019-07-11 stsp
8821 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
8822 ff0d2220 2019-07-11 stsp if (error)
8823 ff0d2220 2019-07-11 stsp goto done;
8824 ff0d2220 2019-07-11 stsp
8825 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
8826 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
8827 a51a74b3 2019-07-27 stsp
8828 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
8829 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
8830 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
8831 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
8832 818c7501 2019-07-11 stsp if (error)
8833 818c7501 2019-07-11 stsp goto done;
8834 818c7501 2019-07-11 stsp if (yca_id == NULL) {
8835 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
8836 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
8837 818c7501 2019-07-11 stsp "with work tree's branch");
8838 818c7501 2019-07-11 stsp goto done;
8839 818c7501 2019-07-11 stsp }
8840 818c7501 2019-07-11 stsp
8841 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
8842 a51a74b3 2019-07-27 stsp if (error) {
8843 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
8844 a51a74b3 2019-07-27 stsp goto done;
8845 a51a74b3 2019-07-27 stsp error = NULL;
8846 a51a74b3 2019-07-27 stsp } else {
8847 df3ed485 2021-01-31 stsp static char msg[128];
8848 df3ed485 2021-01-31 stsp snprintf(msg, sizeof(msg),
8849 df3ed485 2021-01-31 stsp "%s is already based on %s",
8850 df3ed485 2021-01-31 stsp got_ref_get_name(branch),
8851 df3ed485 2021-01-31 stsp got_worktree_get_head_ref_name(worktree));
8852 df3ed485 2021-01-31 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
8853 a51a74b3 2019-07-27 stsp goto done;
8854 a51a74b3 2019-07-27 stsp }
8855 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
8856 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
8857 818c7501 2019-07-11 stsp if (error)
8858 818c7501 2019-07-11 stsp goto done;
8859 818c7501 2019-07-11 stsp }
8860 818c7501 2019-07-11 stsp
8861 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
8862 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
8863 818c7501 2019-07-11 stsp if (error)
8864 818c7501 2019-07-11 stsp goto done;
8865 818c7501 2019-07-11 stsp
8866 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8867 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
8868 fc66b545 2019-08-12 stsp if (pid == NULL) {
8869 fc66b545 2019-08-12 stsp if (!continue_rebase) {
8870 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8871 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8872 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
8873 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
8874 fc66b545 2019-08-12 stsp if (error)
8875 fc66b545 2019-08-12 stsp goto done;
8876 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
8877 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
8878 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8879 9627c110 2020-04-18 stsp
8880 fc66b545 2019-08-12 stsp }
8881 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
8882 fc66b545 2019-08-12 stsp goto done;
8883 fc66b545 2019-08-12 stsp }
8884 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
8885 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
8886 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
8887 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8888 818c7501 2019-07-11 stsp commit = NULL;
8889 818c7501 2019-07-11 stsp if (error)
8890 818c7501 2019-07-11 stsp goto done;
8891 64c6d990 2019-07-11 stsp
8892 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
8893 38b0338b 2019-11-29 stsp if (continue_rebase) {
8894 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
8895 e600f124 2021-03-21 stsp branch, new_base_branch, tmp_branch, repo,
8896 e600f124 2021-03-21 stsp create_backup);
8897 38b0338b 2019-11-29 stsp goto done;
8898 38b0338b 2019-11-29 stsp } else {
8899 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
8900 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
8901 38b0338b 2019-11-29 stsp char *id_str;
8902 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
8903 38b0338b 2019-11-29 stsp new_base_branch);
8904 38b0338b 2019-11-29 stsp if (error)
8905 38b0338b 2019-11-29 stsp goto done;
8906 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
8907 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
8908 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
8909 38b0338b 2019-11-29 stsp free(id_str);
8910 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
8911 38b0338b 2019-11-29 stsp new_head_commit_id);
8912 38b0338b 2019-11-29 stsp if (error)
8913 38b0338b 2019-11-29 stsp goto done;
8914 e600f124 2021-03-21 stsp /* No backup needed since objects did not change. */
8915 e600f124 2021-03-21 stsp create_backup = 0;
8916 38b0338b 2019-11-29 stsp }
8917 818c7501 2019-07-11 stsp }
8918 818c7501 2019-07-11 stsp
8919 818c7501 2019-07-11 stsp pid = NULL;
8920 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, &commits, entry) {
8921 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8922 9627c110 2020-04-18 stsp
8923 818c7501 2019-07-11 stsp commit_id = qid->id;
8924 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
8925 818c7501 2019-07-11 stsp pid = qid;
8926 818c7501 2019-07-11 stsp
8927 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8928 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
8929 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
8930 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8931 818c7501 2019-07-11 stsp if (error)
8932 818c7501 2019-07-11 stsp goto done;
8933 9627c110 2020-04-18 stsp
8934 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8935 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8936 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8937 818c7501 2019-07-11 stsp
8938 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8939 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
8940 a0ea4fc0 2020-02-28 stsp if (error)
8941 a0ea4fc0 2020-02-28 stsp goto done;
8942 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8943 818c7501 2019-07-11 stsp break;
8944 01757395 2019-07-12 stsp }
8945 818c7501 2019-07-11 stsp
8946 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
8947 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
8948 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8949 818c7501 2019-07-11 stsp if (error)
8950 818c7501 2019-07-11 stsp goto done;
8951 818c7501 2019-07-11 stsp }
8952 818c7501 2019-07-11 stsp
8953 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8954 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
8955 818c7501 2019-07-11 stsp if (error)
8956 818c7501 2019-07-11 stsp goto done;
8957 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8958 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
8959 818c7501 2019-07-11 stsp } else
8960 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
8961 e600f124 2021-03-21 stsp new_base_branch, tmp_branch, repo, create_backup);
8962 818c7501 2019-07-11 stsp done:
8963 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
8964 818c7501 2019-07-11 stsp free(branch_head_commit_id);
8965 818c7501 2019-07-11 stsp free(resume_commit_id);
8966 818c7501 2019-07-11 stsp free(yca_id);
8967 818c7501 2019-07-11 stsp if (commit)
8968 818c7501 2019-07-11 stsp got_object_commit_close(commit);
8969 818c7501 2019-07-11 stsp if (branch)
8970 818c7501 2019-07-11 stsp got_ref_close(branch);
8971 818c7501 2019-07-11 stsp if (new_base_branch)
8972 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
8973 818c7501 2019-07-11 stsp if (tmp_branch)
8974 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
8975 5ef14e63 2019-06-02 stsp if (worktree)
8976 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
8977 1d0f4054 2021-06-17 stsp if (repo) {
8978 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
8979 1d0f4054 2021-06-17 stsp if (error == NULL)
8980 1d0f4054 2021-06-17 stsp error = close_err;
8981 1d0f4054 2021-06-17 stsp }
8982 5ef14e63 2019-06-02 stsp return error;
8983 0ebf8283 2019-07-24 stsp }
8984 0ebf8283 2019-07-24 stsp
8985 0ebf8283 2019-07-24 stsp __dead static void
8986 0ebf8283 2019-07-24 stsp usage_histedit(void)
8987 0ebf8283 2019-07-24 stsp {
8988 e600f124 2021-03-21 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
8989 643b85bc 2021-07-16 stsp "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
8990 643b85bc 2021-07-16 stsp getprogname());
8991 0ebf8283 2019-07-24 stsp exit(1);
8992 0ebf8283 2019-07-24 stsp }
8993 0ebf8283 2019-07-24 stsp
8994 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
8995 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
8996 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
8997 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
8998 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
8999 0ebf8283 2019-07-24 stsp
9000 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
9001 0ebf8283 2019-07-24 stsp unsigned char code;
9002 0ebf8283 2019-07-24 stsp const char *name;
9003 0ebf8283 2019-07-24 stsp const char *desc;
9004 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
9005 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
9006 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
9007 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
9008 82997472 2020-01-29 stsp "be used" },
9009 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
9010 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
9011 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
9012 0ebf8283 2019-07-24 stsp };
9013 0ebf8283 2019-07-24 stsp
9014 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
9015 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
9016 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
9017 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9018 0ebf8283 2019-07-24 stsp char *logmsg;
9019 0ebf8283 2019-07-24 stsp };
9020 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
9021 0ebf8283 2019-07-24 stsp
9022 0ebf8283 2019-07-24 stsp static const struct got_error *
9023 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
9024 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9025 0ebf8283 2019-07-24 stsp {
9026 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9027 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
9028 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9029 8138f3e1 2019-08-11 stsp int n;
9030 0ebf8283 2019-07-24 stsp
9031 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
9032 0ebf8283 2019-07-24 stsp if (err)
9033 0ebf8283 2019-07-24 stsp goto done;
9034 0ebf8283 2019-07-24 stsp
9035 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
9036 0ebf8283 2019-07-24 stsp if (err)
9037 0ebf8283 2019-07-24 stsp goto done;
9038 0ebf8283 2019-07-24 stsp
9039 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, 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 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
9044 0ebf8283 2019-07-24 stsp if (n < 0)
9045 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9046 0ebf8283 2019-07-24 stsp done:
9047 0ebf8283 2019-07-24 stsp if (commit)
9048 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9049 0ebf8283 2019-07-24 stsp free(id_str);
9050 0ebf8283 2019-07-24 stsp free(logmsg);
9051 0ebf8283 2019-07-24 stsp return err;
9052 0ebf8283 2019-07-24 stsp }
9053 0ebf8283 2019-07-24 stsp
9054 0ebf8283 2019-07-24 stsp static const struct got_error *
9055 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
9056 466785b9 2020-12-10 jrick FILE *f, int edit_logmsg_only, int fold_only, struct got_repository *repo)
9057 0ebf8283 2019-07-24 stsp {
9058 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9059 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
9060 466785b9 2020-12-10 jrick const char *histedit_cmd = NULL;
9061 0ebf8283 2019-07-24 stsp
9062 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9063 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9064 0ebf8283 2019-07-24 stsp
9065 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9066 466785b9 2020-12-10 jrick histedit_cmd = got_histedit_cmds[0].name;
9067 dbdddfee 2021-06-23 naddy if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
9068 466785b9 2020-12-10 jrick histedit_cmd = "fold";
9069 466785b9 2020-12-10 jrick err = histedit_write_commit(qid->id, histedit_cmd, f, repo);
9070 0ebf8283 2019-07-24 stsp if (err)
9071 0ebf8283 2019-07-24 stsp break;
9072 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
9073 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
9074 083957f4 2020-02-24 stsp if (n < 0) {
9075 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
9076 083957f4 2020-02-24 stsp break;
9077 083957f4 2020-02-24 stsp }
9078 083957f4 2020-02-24 stsp }
9079 0ebf8283 2019-07-24 stsp }
9080 0ebf8283 2019-07-24 stsp
9081 0ebf8283 2019-07-24 stsp return err;
9082 0ebf8283 2019-07-24 stsp }
9083 0ebf8283 2019-07-24 stsp
9084 0ebf8283 2019-07-24 stsp static const struct got_error *
9085 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
9086 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
9087 0ebf8283 2019-07-24 stsp {
9088 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9089 6059809a 2020-12-17 stsp size_t i;
9090 6059809a 2020-12-17 stsp int n;
9091 514f2ffe 2020-01-29 stsp char *id_str;
9092 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
9093 514f2ffe 2020-01-29 stsp
9094 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(commits);
9095 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
9096 514f2ffe 2020-01-29 stsp if (err)
9097 514f2ffe 2020-01-29 stsp return err;
9098 514f2ffe 2020-01-29 stsp
9099 514f2ffe 2020-01-29 stsp n = fprintf(f,
9100 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
9101 514f2ffe 2020-01-29 stsp "# commit %s\n"
9102 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
9103 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
9104 514f2ffe 2020-01-29 stsp if (n < 0) {
9105 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9106 514f2ffe 2020-01-29 stsp goto done;
9107 514f2ffe 2020-01-29 stsp }
9108 0ebf8283 2019-07-24 stsp
9109 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
9110 514f2ffe 2020-01-29 stsp if (n < 0) {
9111 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
9112 514f2ffe 2020-01-29 stsp goto done;
9113 514f2ffe 2020-01-29 stsp }
9114 0ebf8283 2019-07-24 stsp
9115 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9116 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
9117 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
9118 0ebf8283 2019-07-24 stsp cmd->desc);
9119 0ebf8283 2019-07-24 stsp if (n < 0) {
9120 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9121 0ebf8283 2019-07-24 stsp break;
9122 0ebf8283 2019-07-24 stsp }
9123 0ebf8283 2019-07-24 stsp }
9124 514f2ffe 2020-01-29 stsp done:
9125 514f2ffe 2020-01-29 stsp free(id_str);
9126 0ebf8283 2019-07-24 stsp return err;
9127 0ebf8283 2019-07-24 stsp }
9128 0ebf8283 2019-07-24 stsp
9129 0ebf8283 2019-07-24 stsp static const struct got_error *
9130 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
9131 0ebf8283 2019-07-24 stsp {
9132 0ebf8283 2019-07-24 stsp static char msg[42];
9133 0ebf8283 2019-07-24 stsp int ret;
9134 0ebf8283 2019-07-24 stsp
9135 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
9136 0ebf8283 2019-07-24 stsp lineno);
9137 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
9138 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
9139 0ebf8283 2019-07-24 stsp
9140 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
9141 0ebf8283 2019-07-24 stsp }
9142 0ebf8283 2019-07-24 stsp
9143 0ebf8283 2019-07-24 stsp static const struct got_error *
9144 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
9145 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
9146 0ebf8283 2019-07-24 stsp {
9147 0ebf8283 2019-07-24 stsp const struct got_error *err;
9148 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
9149 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
9150 0ebf8283 2019-07-24 stsp
9151 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9152 0ebf8283 2019-07-24 stsp if (err)
9153 0ebf8283 2019-07-24 stsp return err;
9154 0ebf8283 2019-07-24 stsp
9155 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
9156 0ebf8283 2019-07-24 stsp if (err)
9157 0ebf8283 2019-07-24 stsp goto done;
9158 0ebf8283 2019-07-24 stsp
9159 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
9160 5943eee2 2019-08-13 stsp if (err)
9161 5943eee2 2019-08-13 stsp goto done;
9162 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
9163 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
9164 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
9165 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9166 0ebf8283 2019-07-24 stsp }
9167 0ebf8283 2019-07-24 stsp done:
9168 0ebf8283 2019-07-24 stsp if (folded_commit)
9169 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
9170 0ebf8283 2019-07-24 stsp free(id_str);
9171 5943eee2 2019-08-13 stsp free(folded_logmsg);
9172 0ebf8283 2019-07-24 stsp return err;
9173 0ebf8283 2019-07-24 stsp }
9174 0ebf8283 2019-07-24 stsp
9175 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
9176 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
9177 0ebf8283 2019-07-24 stsp {
9178 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
9179 0ebf8283 2019-07-24 stsp
9180 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
9181 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
9182 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
9183 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
9184 3f9de99f 2019-07-24 stsp folded = prev;
9185 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
9186 0ebf8283 2019-07-24 stsp }
9187 0ebf8283 2019-07-24 stsp
9188 0ebf8283 2019-07-24 stsp return folded;
9189 0ebf8283 2019-07-24 stsp }
9190 0ebf8283 2019-07-24 stsp
9191 0ebf8283 2019-07-24 stsp static const struct got_error *
9192 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
9193 0ebf8283 2019-07-24 stsp struct got_repository *repo)
9194 0ebf8283 2019-07-24 stsp {
9195 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
9196 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
9197 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9198 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9199 1601cb9f 2020-09-11 naddy int logmsg_len;
9200 0ebf8283 2019-07-24 stsp int fd;
9201 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
9202 0ebf8283 2019-07-24 stsp
9203 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9204 0ebf8283 2019-07-24 stsp if (err)
9205 0ebf8283 2019-07-24 stsp return err;
9206 0ebf8283 2019-07-24 stsp
9207 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
9208 0ebf8283 2019-07-24 stsp if (folded) {
9209 0ebf8283 2019-07-24 stsp while (folded != hle) {
9210 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
9211 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9212 3f9de99f 2019-07-24 stsp continue;
9213 3f9de99f 2019-07-24 stsp }
9214 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
9215 0ebf8283 2019-07-24 stsp logmsg, repo);
9216 0ebf8283 2019-07-24 stsp if (err)
9217 0ebf8283 2019-07-24 stsp goto done;
9218 0ebf8283 2019-07-24 stsp free(logmsg);
9219 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9220 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
9221 0ebf8283 2019-07-24 stsp }
9222 0ebf8283 2019-07-24 stsp }
9223 0ebf8283 2019-07-24 stsp
9224 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9225 0ebf8283 2019-07-24 stsp if (err)
9226 0ebf8283 2019-07-24 stsp goto done;
9227 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
9228 5943eee2 2019-08-13 stsp if (err)
9229 5943eee2 2019-08-13 stsp goto done;
9230 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
9231 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
9232 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
9233 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
9234 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
9235 0ebf8283 2019-07-24 stsp goto done;
9236 0ebf8283 2019-07-24 stsp }
9237 0ebf8283 2019-07-24 stsp free(logmsg);
9238 0ebf8283 2019-07-24 stsp logmsg = new_msg;
9239 0ebf8283 2019-07-24 stsp
9240 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
9241 0ebf8283 2019-07-24 stsp if (err)
9242 0ebf8283 2019-07-24 stsp goto done;
9243 0ebf8283 2019-07-24 stsp
9244 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
9245 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
9246 0ebf8283 2019-07-24 stsp if (err)
9247 0ebf8283 2019-07-24 stsp goto done;
9248 0ebf8283 2019-07-24 stsp
9249 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
9250 0ebf8283 2019-07-24 stsp close(fd);
9251 0ebf8283 2019-07-24 stsp
9252 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9253 0ebf8283 2019-07-24 stsp if (err)
9254 0ebf8283 2019-07-24 stsp goto done;
9255 0ebf8283 2019-07-24 stsp
9256 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
9257 0d5bb276 2020-12-15 stsp logmsg_len, 0);
9258 0ebf8283 2019-07-24 stsp if (err) {
9259 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
9260 0ebf8283 2019-07-24 stsp goto done;
9261 71392a05 2020-12-13 stsp err = NULL;
9262 71392a05 2020-12-13 stsp hle->logmsg = strdup(new_msg);
9263 71392a05 2020-12-13 stsp if (hle->logmsg == NULL)
9264 71392a05 2020-12-13 stsp err = got_error_from_errno("strdup");
9265 0ebf8283 2019-07-24 stsp }
9266 0ebf8283 2019-07-24 stsp done:
9267 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
9268 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
9269 0ebf8283 2019-07-24 stsp free(logmsg_path);
9270 0ebf8283 2019-07-24 stsp free(logmsg);
9271 5943eee2 2019-08-13 stsp free(orig_logmsg);
9272 0ebf8283 2019-07-24 stsp free(editor);
9273 0ebf8283 2019-07-24 stsp if (commit)
9274 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9275 0ebf8283 2019-07-24 stsp return err;
9276 5ef14e63 2019-06-02 stsp }
9277 0ebf8283 2019-07-24 stsp
9278 0ebf8283 2019-07-24 stsp static const struct got_error *
9279 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
9280 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
9281 0ebf8283 2019-07-24 stsp {
9282 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9283 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
9284 6059809a 2020-12-17 stsp size_t i, size;
9285 0ebf8283 2019-07-24 stsp ssize_t len;
9286 6059809a 2020-12-17 stsp int lineno = 0;
9287 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
9288 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
9289 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
9290 0ebf8283 2019-07-24 stsp
9291 0ebf8283 2019-07-24 stsp for (;;) {
9292 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
9293 0ebf8283 2019-07-24 stsp if (len == -1) {
9294 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
9295 0ebf8283 2019-07-24 stsp if (feof(f))
9296 0ebf8283 2019-07-24 stsp break;
9297 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
9298 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
9299 0ebf8283 2019-07-24 stsp break;
9300 0ebf8283 2019-07-24 stsp }
9301 0ebf8283 2019-07-24 stsp lineno++;
9302 0ebf8283 2019-07-24 stsp p = line;
9303 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9304 0ebf8283 2019-07-24 stsp p++;
9305 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
9306 0ebf8283 2019-07-24 stsp free(line);
9307 0ebf8283 2019-07-24 stsp line = NULL;
9308 0ebf8283 2019-07-24 stsp continue;
9309 0ebf8283 2019-07-24 stsp }
9310 0ebf8283 2019-07-24 stsp cmd = NULL;
9311 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
9312 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
9313 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
9314 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
9315 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
9316 0ebf8283 2019-07-24 stsp break;
9317 0ebf8283 2019-07-24 stsp }
9318 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
9319 0ebf8283 2019-07-24 stsp p++;
9320 0ebf8283 2019-07-24 stsp break;
9321 0ebf8283 2019-07-24 stsp }
9322 0ebf8283 2019-07-24 stsp }
9323 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
9324 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9325 0ebf8283 2019-07-24 stsp break;
9326 0ebf8283 2019-07-24 stsp }
9327 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
9328 0ebf8283 2019-07-24 stsp p++;
9329 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
9330 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
9331 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
9332 0ebf8283 2019-07-24 stsp break;
9333 0ebf8283 2019-07-24 stsp }
9334 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
9335 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9336 0ebf8283 2019-07-24 stsp if (err)
9337 0ebf8283 2019-07-24 stsp break;
9338 0ebf8283 2019-07-24 stsp } else {
9339 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
9340 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
9341 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9342 0ebf8283 2019-07-24 stsp break;
9343 0ebf8283 2019-07-24 stsp }
9344 0ebf8283 2019-07-24 stsp }
9345 0ebf8283 2019-07-24 stsp free(line);
9346 0ebf8283 2019-07-24 stsp line = NULL;
9347 0ebf8283 2019-07-24 stsp continue;
9348 0ebf8283 2019-07-24 stsp } else {
9349 0ebf8283 2019-07-24 stsp end = p;
9350 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
9351 0ebf8283 2019-07-24 stsp end++;
9352 0ebf8283 2019-07-24 stsp *end = '\0';
9353 0ebf8283 2019-07-24 stsp
9354 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
9355 0ebf8283 2019-07-24 stsp if (err) {
9356 0ebf8283 2019-07-24 stsp /* override error code */
9357 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
9358 0ebf8283 2019-07-24 stsp break;
9359 0ebf8283 2019-07-24 stsp }
9360 0ebf8283 2019-07-24 stsp }
9361 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
9362 0ebf8283 2019-07-24 stsp if (hle == NULL) {
9363 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
9364 0ebf8283 2019-07-24 stsp break;
9365 0ebf8283 2019-07-24 stsp }
9366 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
9367 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
9368 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
9369 0ebf8283 2019-07-24 stsp commit_id = NULL;
9370 0ebf8283 2019-07-24 stsp free(line);
9371 0ebf8283 2019-07-24 stsp line = NULL;
9372 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
9373 0ebf8283 2019-07-24 stsp }
9374 0ebf8283 2019-07-24 stsp
9375 0ebf8283 2019-07-24 stsp free(line);
9376 0ebf8283 2019-07-24 stsp free(commit_id);
9377 0ebf8283 2019-07-24 stsp return err;
9378 0ebf8283 2019-07-24 stsp }
9379 0ebf8283 2019-07-24 stsp
9380 0ebf8283 2019-07-24 stsp static const struct got_error *
9381 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
9382 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
9383 bfce7f83 2019-07-27 stsp {
9384 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
9385 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
9386 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9387 5b87815e 2020-03-05 stsp static char msg[92];
9388 bfce7f83 2019-07-27 stsp char *id_str;
9389 bfce7f83 2019-07-27 stsp
9390 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
9391 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
9392 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
9393 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(commits))
9394 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
9395 5b87815e 2020-03-05 stsp
9396 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9397 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
9398 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
9399 5b87815e 2020-03-05 stsp if (hle == hle2)
9400 5b87815e 2020-03-05 stsp continue;
9401 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
9402 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
9403 5b87815e 2020-03-05 stsp continue;
9404 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
9405 5b87815e 2020-03-05 stsp if (err)
9406 5b87815e 2020-03-05 stsp return err;
9407 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
9408 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
9409 5b87815e 2020-03-05 stsp free(id_str);
9410 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9411 5b87815e 2020-03-05 stsp }
9412 5b87815e 2020-03-05 stsp }
9413 bfce7f83 2019-07-27 stsp
9414 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(qid, commits, entry) {
9415 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9416 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
9417 bfce7f83 2019-07-27 stsp break;
9418 bfce7f83 2019-07-27 stsp }
9419 bfce7f83 2019-07-27 stsp if (hle == NULL) {
9420 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
9421 bfce7f83 2019-07-27 stsp if (err)
9422 bfce7f83 2019-07-27 stsp return err;
9423 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
9424 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
9425 bfce7f83 2019-07-27 stsp free(id_str);
9426 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
9427 bfce7f83 2019-07-27 stsp }
9428 bfce7f83 2019-07-27 stsp }
9429 bfce7f83 2019-07-27 stsp
9430 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
9431 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
9432 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
9433 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
9434 bfce7f83 2019-07-27 stsp
9435 bfce7f83 2019-07-27 stsp return NULL;
9436 bfce7f83 2019-07-27 stsp }
9437 bfce7f83 2019-07-27 stsp
9438 bfce7f83 2019-07-27 stsp static const struct got_error *
9439 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
9440 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
9441 bfce7f83 2019-07-27 stsp struct got_repository *repo)
9442 0ebf8283 2019-07-24 stsp {
9443 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9444 0ebf8283 2019-07-24 stsp char *editor;
9445 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9446 0ebf8283 2019-07-24 stsp
9447 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
9448 0ebf8283 2019-07-24 stsp if (err)
9449 0ebf8283 2019-07-24 stsp return err;
9450 0ebf8283 2019-07-24 stsp
9451 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
9452 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
9453 0ebf8283 2019-07-24 stsp goto done;
9454 0ebf8283 2019-07-24 stsp }
9455 0ebf8283 2019-07-24 stsp
9456 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9457 0ebf8283 2019-07-24 stsp if (f == NULL) {
9458 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
9459 0ebf8283 2019-07-24 stsp goto done;
9460 0ebf8283 2019-07-24 stsp }
9461 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9462 bfce7f83 2019-07-27 stsp if (err)
9463 bfce7f83 2019-07-27 stsp goto done;
9464 bfce7f83 2019-07-27 stsp
9465 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
9466 0ebf8283 2019-07-24 stsp done:
9467 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9468 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9469 0ebf8283 2019-07-24 stsp free(editor);
9470 0ebf8283 2019-07-24 stsp return err;
9471 0ebf8283 2019-07-24 stsp }
9472 0ebf8283 2019-07-24 stsp
9473 0ebf8283 2019-07-24 stsp static const struct got_error *
9474 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
9475 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
9476 514f2ffe 2020-01-29 stsp struct got_repository *);
9477 0ebf8283 2019-07-24 stsp
9478 0ebf8283 2019-07-24 stsp static const struct got_error *
9479 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
9480 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
9481 466785b9 2020-12-10 jrick int edit_logmsg_only, int fold_only, struct got_repository *repo)
9482 0ebf8283 2019-07-24 stsp {
9483 0ebf8283 2019-07-24 stsp const struct got_error *err;
9484 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9485 0ebf8283 2019-07-24 stsp char *path = NULL;
9486 0ebf8283 2019-07-24 stsp
9487 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
9488 0ebf8283 2019-07-24 stsp if (err)
9489 0ebf8283 2019-07-24 stsp return err;
9490 0ebf8283 2019-07-24 stsp
9491 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
9492 0ebf8283 2019-07-24 stsp if (err)
9493 0ebf8283 2019-07-24 stsp goto done;
9494 0ebf8283 2019-07-24 stsp
9495 466785b9 2020-12-10 jrick err = histedit_write_commit_list(commits, f, edit_logmsg_only,
9496 466785b9 2020-12-10 jrick fold_only, repo);
9497 0ebf8283 2019-07-24 stsp if (err)
9498 0ebf8283 2019-07-24 stsp goto done;
9499 0ebf8283 2019-07-24 stsp
9500 466785b9 2020-12-10 jrick if (edit_logmsg_only || fold_only) {
9501 083957f4 2020-02-24 stsp rewind(f);
9502 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9503 083957f4 2020-02-24 stsp } else {
9504 56b63ca4 2021-01-22 stsp if (fclose(f) == EOF) {
9505 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
9506 0ebf8283 2019-07-24 stsp goto done;
9507 083957f4 2020-02-24 stsp }
9508 083957f4 2020-02-24 stsp f = NULL;
9509 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
9510 083957f4 2020-02-24 stsp if (err) {
9511 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9512 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9513 083957f4 2020-02-24 stsp goto done;
9514 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
9515 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
9516 083957f4 2020-02-24 stsp }
9517 0ebf8283 2019-07-24 stsp }
9518 0ebf8283 2019-07-24 stsp done:
9519 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9520 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9521 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
9522 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
9523 0ebf8283 2019-07-24 stsp free(path);
9524 0ebf8283 2019-07-24 stsp return err;
9525 0ebf8283 2019-07-24 stsp }
9526 0ebf8283 2019-07-24 stsp
9527 0ebf8283 2019-07-24 stsp static const struct got_error *
9528 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
9529 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9530 0ebf8283 2019-07-24 stsp {
9531 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9532 0ebf8283 2019-07-24 stsp char *path = NULL;
9533 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9534 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9535 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9536 0ebf8283 2019-07-24 stsp
9537 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
9538 0ebf8283 2019-07-24 stsp if (err)
9539 0ebf8283 2019-07-24 stsp return err;
9540 0ebf8283 2019-07-24 stsp
9541 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
9542 0ebf8283 2019-07-24 stsp if (f == NULL) {
9543 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9544 0ebf8283 2019-07-24 stsp goto done;
9545 0ebf8283 2019-07-24 stsp }
9546 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
9547 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
9548 0ebf8283 2019-07-24 stsp repo);
9549 0ebf8283 2019-07-24 stsp if (err)
9550 0ebf8283 2019-07-24 stsp break;
9551 0ebf8283 2019-07-24 stsp
9552 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9553 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
9554 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
9555 0ebf8283 2019-07-24 stsp if (n < 0) {
9556 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
9557 0ebf8283 2019-07-24 stsp break;
9558 0ebf8283 2019-07-24 stsp }
9559 0ebf8283 2019-07-24 stsp }
9560 0ebf8283 2019-07-24 stsp }
9561 0ebf8283 2019-07-24 stsp done:
9562 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9563 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9564 0ebf8283 2019-07-24 stsp free(path);
9565 0ebf8283 2019-07-24 stsp if (commit)
9566 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9567 0ebf8283 2019-07-24 stsp return err;
9568 0ebf8283 2019-07-24 stsp }
9569 0ebf8283 2019-07-24 stsp
9570 bfce7f83 2019-07-27 stsp void
9571 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
9572 bfce7f83 2019-07-27 stsp {
9573 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
9574 bfce7f83 2019-07-27 stsp
9575 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
9576 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
9577 bfce7f83 2019-07-27 stsp free(hle);
9578 bfce7f83 2019-07-27 stsp }
9579 bfce7f83 2019-07-27 stsp }
9580 bfce7f83 2019-07-27 stsp
9581 0ebf8283 2019-07-24 stsp static const struct got_error *
9582 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
9583 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
9584 0ebf8283 2019-07-24 stsp {
9585 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
9586 0ebf8283 2019-07-24 stsp FILE *f = NULL;
9587 0ebf8283 2019-07-24 stsp
9588 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
9589 0ebf8283 2019-07-24 stsp if (f == NULL) {
9590 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
9591 0ebf8283 2019-07-24 stsp goto done;
9592 0ebf8283 2019-07-24 stsp }
9593 0ebf8283 2019-07-24 stsp
9594 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
9595 0ebf8283 2019-07-24 stsp done:
9596 56b63ca4 2021-01-22 stsp if (f && fclose(f) == EOF && err == NULL)
9597 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
9598 0ebf8283 2019-07-24 stsp return err;
9599 0ebf8283 2019-07-24 stsp }
9600 0ebf8283 2019-07-24 stsp
9601 0ebf8283 2019-07-24 stsp static const struct got_error *
9602 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
9603 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
9604 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
9605 0ebf8283 2019-07-24 stsp {
9606 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
9607 0ebf8283 2019-07-24 stsp int resp = ' ';
9608 0ebf8283 2019-07-24 stsp
9609 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
9610 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
9611 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
9612 0ebf8283 2019-07-24 stsp resp = getchar();
9613 a61a4414 2019-08-07 stsp if (resp == '\n')
9614 a61a4414 2019-08-07 stsp resp = getchar();
9615 426ebf2e 2019-07-25 stsp if (resp == 'c') {
9616 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9617 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
9618 bfce7f83 2019-07-27 stsp repo);
9619 426ebf2e 2019-07-25 stsp if (err) {
9620 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9621 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9622 426ebf2e 2019-07-25 stsp break;
9623 bfce7f83 2019-07-27 stsp prev_err = err;
9624 426ebf2e 2019-07-25 stsp resp = ' ';
9625 426ebf2e 2019-07-25 stsp continue;
9626 426ebf2e 2019-07-25 stsp }
9627 bfce7f83 2019-07-27 stsp break;
9628 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
9629 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
9630 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
9631 466785b9 2020-12-10 jrick commits, branch_name, 0, 0, repo);
9632 426ebf2e 2019-07-25 stsp if (err) {
9633 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
9634 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
9635 426ebf2e 2019-07-25 stsp break;
9636 bfce7f83 2019-07-27 stsp prev_err = err;
9637 426ebf2e 2019-07-25 stsp resp = ' ';
9638 426ebf2e 2019-07-25 stsp continue;
9639 426ebf2e 2019-07-25 stsp }
9640 bfce7f83 2019-07-27 stsp break;
9641 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
9642 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
9643 0ebf8283 2019-07-24 stsp break;
9644 426ebf2e 2019-07-25 stsp } else
9645 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
9646 0ebf8283 2019-07-24 stsp }
9647 0ebf8283 2019-07-24 stsp
9648 0ebf8283 2019-07-24 stsp return err;
9649 0ebf8283 2019-07-24 stsp }
9650 0ebf8283 2019-07-24 stsp
9651 0ebf8283 2019-07-24 stsp static const struct got_error *
9652 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
9653 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
9654 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
9655 0ebf8283 2019-07-24 stsp {
9656 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
9657 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
9658 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
9659 3e3a69f1 2019-07-25 stsp branch, repo);
9660 0ebf8283 2019-07-24 stsp }
9661 0ebf8283 2019-07-24 stsp
9662 0ebf8283 2019-07-24 stsp static const struct got_error *
9663 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
9664 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
9665 0ebf8283 2019-07-24 stsp {
9666 0ebf8283 2019-07-24 stsp const struct got_error *err;
9667 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9668 0ebf8283 2019-07-24 stsp
9669 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
9670 0ebf8283 2019-07-24 stsp if (err)
9671 0ebf8283 2019-07-24 stsp goto done;
9672 0ebf8283 2019-07-24 stsp
9673 0ebf8283 2019-07-24 stsp if (new_id) {
9674 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
9675 0ebf8283 2019-07-24 stsp if (err)
9676 0ebf8283 2019-07-24 stsp goto done;
9677 0ebf8283 2019-07-24 stsp }
9678 0ebf8283 2019-07-24 stsp
9679 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
9680 0ebf8283 2019-07-24 stsp if (new_id_str)
9681 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
9682 0ebf8283 2019-07-24 stsp
9683 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
9684 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
9685 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
9686 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
9687 0ebf8283 2019-07-24 stsp goto done;
9688 0ebf8283 2019-07-24 stsp }
9689 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
9690 0ebf8283 2019-07-24 stsp } else {
9691 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
9692 0ebf8283 2019-07-24 stsp if (err)
9693 0ebf8283 2019-07-24 stsp goto done;
9694 0ebf8283 2019-07-24 stsp }
9695 0ebf8283 2019-07-24 stsp
9696 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
9697 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
9698 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
9699 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
9700 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
9701 0ebf8283 2019-07-24 stsp break;
9702 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
9703 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
9704 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
9705 0ebf8283 2019-07-24 stsp logmsg);
9706 0ebf8283 2019-07-24 stsp break;
9707 0ebf8283 2019-07-24 stsp default:
9708 0ebf8283 2019-07-24 stsp break;
9709 0ebf8283 2019-07-24 stsp }
9710 0ebf8283 2019-07-24 stsp done:
9711 0ebf8283 2019-07-24 stsp free(old_id_str);
9712 0ebf8283 2019-07-24 stsp free(new_id_str);
9713 0ebf8283 2019-07-24 stsp return err;
9714 0ebf8283 2019-07-24 stsp }
9715 0ebf8283 2019-07-24 stsp
9716 0ebf8283 2019-07-24 stsp static const struct got_error *
9717 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
9718 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
9719 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
9720 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
9721 0ebf8283 2019-07-24 stsp {
9722 0ebf8283 2019-07-24 stsp const struct got_error *err;
9723 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9724 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
9725 0ebf8283 2019-07-24 stsp
9726 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
9727 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
9728 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
9729 0ebf8283 2019-07-24 stsp if (err)
9730 0ebf8283 2019-07-24 stsp return err;
9731 0ebf8283 2019-07-24 stsp }
9732 0ebf8283 2019-07-24 stsp
9733 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
9734 0ebf8283 2019-07-24 stsp if (err)
9735 0ebf8283 2019-07-24 stsp return err;
9736 0ebf8283 2019-07-24 stsp
9737 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
9738 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
9739 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
9740 0ebf8283 2019-07-24 stsp if (err) {
9741 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
9742 0ebf8283 2019-07-24 stsp goto done;
9743 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
9744 0ebf8283 2019-07-24 stsp } else {
9745 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
9746 0ebf8283 2019-07-24 stsp free(new_commit_id);
9747 0ebf8283 2019-07-24 stsp }
9748 0ebf8283 2019-07-24 stsp done:
9749 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9750 0ebf8283 2019-07-24 stsp return err;
9751 0ebf8283 2019-07-24 stsp }
9752 0ebf8283 2019-07-24 stsp
9753 0ebf8283 2019-07-24 stsp static const struct got_error *
9754 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
9755 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
9756 0ebf8283 2019-07-24 stsp {
9757 0ebf8283 2019-07-24 stsp const struct got_error *error;
9758 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
9759 0ebf8283 2019-07-24 stsp
9760 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
9761 0ebf8283 2019-07-24 stsp repo);
9762 0ebf8283 2019-07-24 stsp if (error)
9763 0ebf8283 2019-07-24 stsp return error;
9764 0ebf8283 2019-07-24 stsp
9765 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
9766 0ebf8283 2019-07-24 stsp if (error)
9767 0ebf8283 2019-07-24 stsp return error;
9768 0ebf8283 2019-07-24 stsp
9769 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
9770 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
9771 0ebf8283 2019-07-24 stsp return error;
9772 ab20a43a 2020-01-29 stsp }
9773 ab20a43a 2020-01-29 stsp
9774 ab20a43a 2020-01-29 stsp static const struct got_error *
9775 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
9776 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
9777 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9778 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
9779 ab20a43a 2020-01-29 stsp {
9780 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
9781 ab20a43a 2020-01-29 stsp
9782 ab20a43a 2020-01-29 stsp switch (status) {
9783 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9784 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9785 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9786 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
9787 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9788 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9789 ab20a43a 2020-01-29 stsp default:
9790 ab20a43a 2020-01-29 stsp break;
9791 ab20a43a 2020-01-29 stsp }
9792 ab20a43a 2020-01-29 stsp
9793 ab20a43a 2020-01-29 stsp switch (staged_status) {
9794 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
9795 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
9796 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
9797 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
9798 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
9799 ab20a43a 2020-01-29 stsp default:
9800 ab20a43a 2020-01-29 stsp break;
9801 ab20a43a 2020-01-29 stsp }
9802 ab20a43a 2020-01-29 stsp
9803 ab20a43a 2020-01-29 stsp return NULL;
9804 0ebf8283 2019-07-24 stsp }
9805 0ebf8283 2019-07-24 stsp
9806 0ebf8283 2019-07-24 stsp static const struct got_error *
9807 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
9808 0ebf8283 2019-07-24 stsp {
9809 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
9810 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
9811 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
9812 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
9813 0ebf8283 2019-07-24 stsp char *cwd = NULL;
9814 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
9815 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
9816 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
9817 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
9818 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
9819 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
9820 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
9821 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9822 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
9823 466785b9 2020-12-10 jrick int edit_logmsg_only = 0, fold_only = 0;
9824 643b85bc 2021-07-16 stsp int list_backups = 0, delete_backups = 0;
9825 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
9826 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
9827 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
9828 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
9829 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
9830 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
9831 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
9832 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
9833 0ebf8283 2019-07-24 stsp
9834 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commits);
9835 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
9836 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
9837 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9838 0ebf8283 2019-07-24 stsp
9839 643b85bc 2021-07-16 stsp while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
9840 0ebf8283 2019-07-24 stsp switch (ch) {
9841 0ebf8283 2019-07-24 stsp case 'a':
9842 0ebf8283 2019-07-24 stsp abort_edit = 1;
9843 0ebf8283 2019-07-24 stsp break;
9844 0ebf8283 2019-07-24 stsp case 'c':
9845 0ebf8283 2019-07-24 stsp continue_edit = 1;
9846 0ebf8283 2019-07-24 stsp break;
9847 466785b9 2020-12-10 jrick case 'f':
9848 466785b9 2020-12-10 jrick fold_only = 1;
9849 466785b9 2020-12-10 jrick break;
9850 0ebf8283 2019-07-24 stsp case 'F':
9851 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
9852 0ebf8283 2019-07-24 stsp break;
9853 083957f4 2020-02-24 stsp case 'm':
9854 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
9855 083957f4 2020-02-24 stsp break;
9856 e600f124 2021-03-21 stsp case 'l':
9857 e600f124 2021-03-21 stsp list_backups = 1;
9858 e600f124 2021-03-21 stsp break;
9859 643b85bc 2021-07-16 stsp case 'X':
9860 643b85bc 2021-07-16 stsp delete_backups = 1;
9861 643b85bc 2021-07-16 stsp break;
9862 0ebf8283 2019-07-24 stsp default:
9863 0ebf8283 2019-07-24 stsp usage_histedit();
9864 0ebf8283 2019-07-24 stsp /* NOTREACHED */
9865 0ebf8283 2019-07-24 stsp }
9866 0ebf8283 2019-07-24 stsp }
9867 0ebf8283 2019-07-24 stsp
9868 0ebf8283 2019-07-24 stsp argc -= optind;
9869 0ebf8283 2019-07-24 stsp argv += optind;
9870 0ebf8283 2019-07-24 stsp
9871 0ebf8283 2019-07-24 stsp #ifndef PROFILE
9872 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9873 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
9874 0ebf8283 2019-07-24 stsp err(1, "pledge");
9875 0ebf8283 2019-07-24 stsp #endif
9876 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
9877 ff69268e 2020-12-13 stsp option_conflict('a', 'c');
9878 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
9879 ff69268e 2020-12-13 stsp option_conflict('F', 'm');
9880 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
9881 ff69268e 2020-12-13 stsp option_conflict('a', 'm');
9882 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
9883 ff69268e 2020-12-13 stsp option_conflict('c', 'm');
9884 466785b9 2020-12-10 jrick if (abort_edit && fold_only)
9885 ff69268e 2020-12-13 stsp option_conflict('a', 'f');
9886 ff69268e 2020-12-13 stsp if (continue_edit && fold_only)
9887 ff69268e 2020-12-13 stsp option_conflict('c', 'f');
9888 466785b9 2020-12-10 jrick if (fold_only && edit_logmsg_only)
9889 ff69268e 2020-12-13 stsp option_conflict('f', 'm');
9890 b3805337 2020-12-13 stsp if (edit_script_path && fold_only)
9891 b3805337 2020-12-13 stsp option_conflict('F', 'f');
9892 e600f124 2021-03-21 stsp if (list_backups) {
9893 e600f124 2021-03-21 stsp if (abort_edit)
9894 e600f124 2021-03-21 stsp option_conflict('l', 'a');
9895 e600f124 2021-03-21 stsp if (continue_edit)
9896 e600f124 2021-03-21 stsp option_conflict('l', 'c');
9897 e600f124 2021-03-21 stsp if (edit_script_path)
9898 e600f124 2021-03-21 stsp option_conflict('l', 'F');
9899 e600f124 2021-03-21 stsp if (edit_logmsg_only)
9900 e600f124 2021-03-21 stsp option_conflict('l', 'm');
9901 e600f124 2021-03-21 stsp if (fold_only)
9902 e600f124 2021-03-21 stsp option_conflict('l', 'f');
9903 643b85bc 2021-07-16 stsp if (delete_backups)
9904 643b85bc 2021-07-16 stsp option_conflict('l', 'X');
9905 e600f124 2021-03-21 stsp if (argc != 0 && argc != 1)
9906 e600f124 2021-03-21 stsp usage_histedit();
9907 643b85bc 2021-07-16 stsp } else if (delete_backups) {
9908 643b85bc 2021-07-16 stsp if (abort_edit)
9909 643b85bc 2021-07-16 stsp option_conflict('X', 'a');
9910 643b85bc 2021-07-16 stsp if (continue_edit)
9911 643b85bc 2021-07-16 stsp option_conflict('X', 'c');
9912 643b85bc 2021-07-16 stsp if (edit_script_path)
9913 643b85bc 2021-07-16 stsp option_conflict('X', 'F');
9914 643b85bc 2021-07-16 stsp if (edit_logmsg_only)
9915 643b85bc 2021-07-16 stsp option_conflict('X', 'm');
9916 643b85bc 2021-07-16 stsp if (fold_only)
9917 643b85bc 2021-07-16 stsp option_conflict('X', 'f');
9918 643b85bc 2021-07-16 stsp if (list_backups)
9919 643b85bc 2021-07-16 stsp option_conflict('X', 'l');
9920 643b85bc 2021-07-16 stsp if (argc != 0 && argc != 1)
9921 643b85bc 2021-07-16 stsp usage_histedit();
9922 e600f124 2021-03-21 stsp } else if (argc != 0)
9923 0ebf8283 2019-07-24 stsp usage_histedit();
9924 0ebf8283 2019-07-24 stsp
9925 0ebf8283 2019-07-24 stsp /*
9926 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
9927 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
9928 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
9929 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
9930 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
9931 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
9932 0ebf8283 2019-07-24 stsp */
9933 0ebf8283 2019-07-24 stsp
9934 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
9935 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
9936 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
9937 0ebf8283 2019-07-24 stsp goto done;
9938 0ebf8283 2019-07-24 stsp }
9939 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
9940 fa51e947 2020-03-27 stsp if (error) {
9941 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9942 e600f124 2021-03-21 stsp if (error->code != GOT_ERR_NOT_WORKTREE)
9943 e600f124 2021-03-21 stsp goto done;
9944 e600f124 2021-03-21 stsp } else {
9945 e600f124 2021-03-21 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9946 e600f124 2021-03-21 stsp error = wrap_not_worktree_error(error,
9947 e600f124 2021-03-21 stsp "histedit", cwd);
9948 e600f124 2021-03-21 stsp goto done;
9949 e600f124 2021-03-21 stsp }
9950 e600f124 2021-03-21 stsp }
9951 e600f124 2021-03-21 stsp
9952 643b85bc 2021-07-16 stsp if (list_backups || delete_backups) {
9953 e600f124 2021-03-21 stsp error = got_repo_open(&repo,
9954 e600f124 2021-03-21 stsp worktree ? got_worktree_get_repo_path(worktree) : cwd,
9955 e600f124 2021-03-21 stsp NULL);
9956 e600f124 2021-03-21 stsp if (error != NULL)
9957 e600f124 2021-03-21 stsp goto done;
9958 e600f124 2021-03-21 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9959 e600f124 2021-03-21 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
9960 e600f124 2021-03-21 stsp if (error)
9961 e600f124 2021-03-21 stsp goto done;
9962 643b85bc 2021-07-16 stsp error = process_backup_refs(
9963 e600f124 2021-03-21 stsp GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
9964 643b85bc 2021-07-16 stsp argc == 1 ? argv[0] : NULL, delete_backups, repo);
9965 e600f124 2021-03-21 stsp goto done; /* nothing else to do */
9966 fa51e947 2020-03-27 stsp }
9967 0ebf8283 2019-07-24 stsp
9968 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9969 c9956ddf 2019-09-08 stsp NULL);
9970 0ebf8283 2019-07-24 stsp if (error != NULL)
9971 0ebf8283 2019-07-24 stsp goto done;
9972 0ebf8283 2019-07-24 stsp
9973 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9974 0ebf8283 2019-07-24 stsp if (error)
9975 0ebf8283 2019-07-24 stsp goto done;
9976 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
9977 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
9978 0ebf8283 2019-07-24 stsp goto done;
9979 0ebf8283 2019-07-24 stsp }
9980 0ebf8283 2019-07-24 stsp
9981 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_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
9985 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
9986 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9987 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
9988 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
9989 083957f4 2020-02-24 stsp "before the -m option can be used");
9990 083957f4 2020-02-24 stsp goto done;
9991 083957f4 2020-02-24 stsp }
9992 466785b9 2020-12-10 jrick if (edit_in_progress && fold_only) {
9993 466785b9 2020-12-10 jrick error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
9994 466785b9 2020-12-10 jrick "histedit operation is in progress in this "
9995 466785b9 2020-12-10 jrick "work tree and must be continued or aborted "
9996 466785b9 2020-12-10 jrick "before the -f option can be used");
9997 466785b9 2020-12-10 jrick goto done;
9998 466785b9 2020-12-10 jrick }
9999 083957f4 2020-02-24 stsp
10000 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
10001 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10002 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10003 3e3a69f1 2019-07-25 stsp worktree, repo);
10004 0ebf8283 2019-07-24 stsp if (error)
10005 0ebf8283 2019-07-24 stsp goto done;
10006 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
10007 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10008 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
10009 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
10010 0ebf8283 2019-07-24 stsp if (error)
10011 0ebf8283 2019-07-24 stsp goto done;
10012 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
10013 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
10014 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10015 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
10016 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
10017 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
10018 0ebf8283 2019-07-24 stsp goto done;
10019 0ebf8283 2019-07-24 stsp }
10020 0ebf8283 2019-07-24 stsp
10021 0ebf8283 2019-07-24 stsp if (continue_edit) {
10022 0ebf8283 2019-07-24 stsp char *path;
10023 0ebf8283 2019-07-24 stsp
10024 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
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 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
10030 0ebf8283 2019-07-24 stsp if (error)
10031 0ebf8283 2019-07-24 stsp goto done;
10032 0ebf8283 2019-07-24 stsp
10033 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
10034 0ebf8283 2019-07-24 stsp free(path);
10035 0ebf8283 2019-07-24 stsp if (error)
10036 0ebf8283 2019-07-24 stsp goto done;
10037 0ebf8283 2019-07-24 stsp
10038 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
10039 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
10040 3e3a69f1 2019-07-25 stsp worktree, repo);
10041 0ebf8283 2019-07-24 stsp if (error)
10042 0ebf8283 2019-07-24 stsp goto done;
10043 0ebf8283 2019-07-24 stsp
10044 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10045 0ebf8283 2019-07-24 stsp if (error)
10046 0ebf8283 2019-07-24 stsp goto done;
10047 0ebf8283 2019-07-24 stsp
10048 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10049 0ebf8283 2019-07-24 stsp head_commit_id);
10050 0ebf8283 2019-07-24 stsp if (error)
10051 0ebf8283 2019-07-24 stsp goto done;
10052 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10053 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10054 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10055 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10056 3aac7cf7 2019-07-25 stsp goto done;
10057 3aac7cf7 2019-07-25 stsp }
10058 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10059 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
10060 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10061 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10062 0ebf8283 2019-07-24 stsp commit = NULL;
10063 0ebf8283 2019-07-24 stsp if (error)
10064 0ebf8283 2019-07-24 stsp goto done;
10065 0ebf8283 2019-07-24 stsp } else {
10066 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
10067 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
10068 0ebf8283 2019-07-24 stsp goto done;
10069 0ebf8283 2019-07-24 stsp }
10070 0ebf8283 2019-07-24 stsp
10071 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
10072 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
10073 0ebf8283 2019-07-24 stsp if (error != NULL)
10074 0ebf8283 2019-07-24 stsp goto done;
10075 0ebf8283 2019-07-24 stsp
10076 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
10077 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
10078 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
10079 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
10080 c7d20a3f 2019-07-30 stsp goto done;
10081 c7d20a3f 2019-07-30 stsp }
10082 c7d20a3f 2019-07-30 stsp
10083 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
10084 bfce7f83 2019-07-27 stsp got_ref_close(branch);
10085 bfce7f83 2019-07-27 stsp branch = NULL;
10086 0ebf8283 2019-07-24 stsp if (error)
10087 0ebf8283 2019-07-24 stsp goto done;
10088 0ebf8283 2019-07-24 stsp
10089 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10090 0ebf8283 2019-07-24 stsp head_commit_id);
10091 0ebf8283 2019-07-24 stsp if (error)
10092 0ebf8283 2019-07-24 stsp goto done;
10093 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10094 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10095 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
10096 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10097 3aac7cf7 2019-07-25 stsp goto done;
10098 3aac7cf7 2019-07-25 stsp }
10099 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
10100 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
10101 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
10102 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
10103 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10104 0ebf8283 2019-07-24 stsp commit = NULL;
10105 0ebf8283 2019-07-24 stsp if (error)
10106 0ebf8283 2019-07-24 stsp goto done;
10107 0ebf8283 2019-07-24 stsp
10108 dbdddfee 2021-06-23 naddy if (STAILQ_EMPTY(&commits)) {
10109 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
10110 c5996fff 2020-01-29 stsp goto done;
10111 c5996fff 2020-01-29 stsp }
10112 c5996fff 2020-01-29 stsp
10113 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
10114 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
10115 bfce7f83 2019-07-27 stsp if (error)
10116 bfce7f83 2019-07-27 stsp goto done;
10117 bfce7f83 2019-07-27 stsp
10118 0ebf8283 2019-07-24 stsp if (edit_script_path) {
10119 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
10120 0ebf8283 2019-07-24 stsp edit_script_path, repo);
10121 6ba2bea2 2019-07-27 stsp if (error) {
10122 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10123 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10124 9627c110 2020-04-18 stsp update_progress, &upa);
10125 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10126 0ebf8283 2019-07-24 stsp goto done;
10127 6ba2bea2 2019-07-27 stsp }
10128 0ebf8283 2019-07-24 stsp } else {
10129 514f2ffe 2020-01-29 stsp const char *branch_name;
10130 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
10131 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
10132 514f2ffe 2020-01-29 stsp branch_name += 11;
10133 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
10134 466785b9 2020-12-10 jrick branch_name, edit_logmsg_only, fold_only, repo);
10135 6ba2bea2 2019-07-27 stsp if (error) {
10136 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10137 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10138 9627c110 2020-04-18 stsp update_progress, &upa);
10139 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10140 0ebf8283 2019-07-24 stsp goto done;
10141 6ba2bea2 2019-07-27 stsp }
10142 0ebf8283 2019-07-24 stsp
10143 0ebf8283 2019-07-24 stsp }
10144 0ebf8283 2019-07-24 stsp
10145 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
10146 0ebf8283 2019-07-24 stsp repo);
10147 6ba2bea2 2019-07-27 stsp if (error) {
10148 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
10149 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
10150 9627c110 2020-04-18 stsp update_progress, &upa);
10151 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10152 0ebf8283 2019-07-24 stsp goto done;
10153 6ba2bea2 2019-07-27 stsp }
10154 0ebf8283 2019-07-24 stsp
10155 0ebf8283 2019-07-24 stsp }
10156 0ebf8283 2019-07-24 stsp
10157 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
10158 4ec14e60 2019-08-28 hiltjo if (error)
10159 0ebf8283 2019-07-24 stsp goto done;
10160 0ebf8283 2019-07-24 stsp
10161 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
10162 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
10163 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
10164 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
10165 0ebf8283 2019-07-24 stsp continue;
10166 0ebf8283 2019-07-24 stsp
10167 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
10168 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
10169 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
10170 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
10171 62d463ca 2020-10-20 naddy repo);
10172 ab20a43a 2020-01-29 stsp if (error)
10173 ab20a43a 2020-01-29 stsp goto done;
10174 0ebf8283 2019-07-24 stsp } else {
10175 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
10176 ab20a43a 2020-01-29 stsp int have_changes = 0;
10177 ab20a43a 2020-01-29 stsp
10178 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
10179 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
10180 ab20a43a 2020-01-29 stsp if (error)
10181 ab20a43a 2020-01-29 stsp goto done;
10182 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
10183 f6343036 2021-06-22 stsp repo, 0, check_local_changes, &have_changes,
10184 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
10185 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
10186 ab20a43a 2020-01-29 stsp if (error) {
10187 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
10188 ab20a43a 2020-01-29 stsp goto done;
10189 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
10190 ab20a43a 2020-01-29 stsp goto done;
10191 ab20a43a 2020-01-29 stsp }
10192 ab20a43a 2020-01-29 stsp if (have_changes) {
10193 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
10194 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
10195 ab20a43a 2020-01-29 stsp if (error)
10196 ab20a43a 2020-01-29 stsp goto done;
10197 ab20a43a 2020-01-29 stsp } else {
10198 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
10199 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
10200 ab20a43a 2020-01-29 stsp if (error)
10201 ab20a43a 2020-01-29 stsp goto done;
10202 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
10203 ab20a43a 2020-01-29 stsp hle, NULL);
10204 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
10205 ab20a43a 2020-01-29 stsp commit = NULL;
10206 ab20a43a 2020-01-29 stsp if (error)
10207 ab20a43a 2020-01-29 stsp goto done;
10208 ab20a43a 2020-01-29 stsp }
10209 0ebf8283 2019-07-24 stsp }
10210 0ebf8283 2019-07-24 stsp continue;
10211 0ebf8283 2019-07-24 stsp }
10212 0ebf8283 2019-07-24 stsp
10213 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
10214 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10215 0ebf8283 2019-07-24 stsp if (error)
10216 0ebf8283 2019-07-24 stsp goto done;
10217 0ebf8283 2019-07-24 stsp continue;
10218 0ebf8283 2019-07-24 stsp }
10219 0ebf8283 2019-07-24 stsp
10220 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
10221 0ebf8283 2019-07-24 stsp hle->commit_id);
10222 0ebf8283 2019-07-24 stsp if (error)
10223 0ebf8283 2019-07-24 stsp goto done;
10224 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10225 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(parent_ids);
10226 0ebf8283 2019-07-24 stsp
10227 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
10228 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
10229 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
10230 0ebf8283 2019-07-24 stsp if (error)
10231 0ebf8283 2019-07-24 stsp goto done;
10232 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10233 0ebf8283 2019-07-24 stsp commit = NULL;
10234 0ebf8283 2019-07-24 stsp
10235 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10236 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
10237 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
10238 9627c110 2020-04-18 stsp
10239 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
10240 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
10241 a0ea4fc0 2020-02-28 stsp repo);
10242 a0ea4fc0 2020-02-28 stsp if (error)
10243 a0ea4fc0 2020-02-28 stsp goto done;
10244 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10245 0ebf8283 2019-07-24 stsp break;
10246 0ebf8283 2019-07-24 stsp }
10247 0ebf8283 2019-07-24 stsp
10248 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
10249 0ebf8283 2019-07-24 stsp char *id_str;
10250 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
10251 0ebf8283 2019-07-24 stsp if (error)
10252 0ebf8283 2019-07-24 stsp goto done;
10253 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
10254 0ebf8283 2019-07-24 stsp id_str);
10255 0ebf8283 2019-07-24 stsp free(id_str);
10256 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10257 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
10258 3e3a69f1 2019-07-25 stsp fileindex);
10259 0ebf8283 2019-07-24 stsp goto done;
10260 0ebf8283 2019-07-24 stsp }
10261 0ebf8283 2019-07-24 stsp
10262 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
10263 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
10264 0ebf8283 2019-07-24 stsp if (error)
10265 0ebf8283 2019-07-24 stsp goto done;
10266 0ebf8283 2019-07-24 stsp continue;
10267 0ebf8283 2019-07-24 stsp }
10268 0ebf8283 2019-07-24 stsp
10269 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
10270 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
10271 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
10272 0ebf8283 2019-07-24 stsp if (error)
10273 0ebf8283 2019-07-24 stsp goto done;
10274 0ebf8283 2019-07-24 stsp }
10275 0ebf8283 2019-07-24 stsp
10276 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
10277 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
10278 0ebf8283 2019-07-24 stsp if (error)
10279 0ebf8283 2019-07-24 stsp goto done;
10280 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
10281 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
10282 0ebf8283 2019-07-24 stsp } else
10283 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
10284 3e3a69f1 2019-07-25 stsp branch, repo);
10285 0ebf8283 2019-07-24 stsp done:
10286 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
10287 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
10288 0ebf8283 2019-07-24 stsp free(head_commit_id);
10289 0ebf8283 2019-07-24 stsp free(base_commit_id);
10290 0ebf8283 2019-07-24 stsp free(resume_commit_id);
10291 0ebf8283 2019-07-24 stsp if (commit)
10292 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
10293 0ebf8283 2019-07-24 stsp if (branch)
10294 0ebf8283 2019-07-24 stsp got_ref_close(branch);
10295 0ebf8283 2019-07-24 stsp if (tmp_branch)
10296 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
10297 0ebf8283 2019-07-24 stsp if (worktree)
10298 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
10299 1d0f4054 2021-06-17 stsp if (repo) {
10300 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10301 1d0f4054 2021-06-17 stsp if (error == NULL)
10302 1d0f4054 2021-06-17 stsp error = close_err;
10303 1d0f4054 2021-06-17 stsp }
10304 2822a352 2019-10-15 stsp return error;
10305 2822a352 2019-10-15 stsp }
10306 2822a352 2019-10-15 stsp
10307 2822a352 2019-10-15 stsp __dead static void
10308 2822a352 2019-10-15 stsp usage_integrate(void)
10309 2822a352 2019-10-15 stsp {
10310 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
10311 2822a352 2019-10-15 stsp exit(1);
10312 2822a352 2019-10-15 stsp }
10313 2822a352 2019-10-15 stsp
10314 2822a352 2019-10-15 stsp static const struct got_error *
10315 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
10316 2822a352 2019-10-15 stsp {
10317 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
10318 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
10319 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
10320 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
10321 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
10322 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
10323 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
10324 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
10325 9627c110 2020-04-18 stsp int ch;
10326 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10327 2822a352 2019-10-15 stsp
10328 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
10329 2822a352 2019-10-15 stsp switch (ch) {
10330 2822a352 2019-10-15 stsp default:
10331 2822a352 2019-10-15 stsp usage_integrate();
10332 2822a352 2019-10-15 stsp /* NOTREACHED */
10333 2822a352 2019-10-15 stsp }
10334 2822a352 2019-10-15 stsp }
10335 2822a352 2019-10-15 stsp
10336 2822a352 2019-10-15 stsp argc -= optind;
10337 2822a352 2019-10-15 stsp argv += optind;
10338 2822a352 2019-10-15 stsp
10339 2822a352 2019-10-15 stsp if (argc != 1)
10340 2822a352 2019-10-15 stsp usage_integrate();
10341 2822a352 2019-10-15 stsp branch_arg = argv[0];
10342 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
10343 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10344 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
10345 2822a352 2019-10-15 stsp err(1, "pledge");
10346 b08a0ccd 2020-09-24 stsp #endif
10347 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
10348 2822a352 2019-10-15 stsp if (cwd == NULL) {
10349 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
10350 2822a352 2019-10-15 stsp goto done;
10351 2822a352 2019-10-15 stsp }
10352 2822a352 2019-10-15 stsp
10353 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
10354 fa51e947 2020-03-27 stsp if (error) {
10355 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10356 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
10357 fa51e947 2020-03-27 stsp cwd);
10358 2822a352 2019-10-15 stsp goto done;
10359 fa51e947 2020-03-27 stsp }
10360 2822a352 2019-10-15 stsp
10361 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
10362 2822a352 2019-10-15 stsp if (error)
10363 2822a352 2019-10-15 stsp goto done;
10364 2822a352 2019-10-15 stsp
10365 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10366 2822a352 2019-10-15 stsp NULL);
10367 2822a352 2019-10-15 stsp if (error != NULL)
10368 2822a352 2019-10-15 stsp goto done;
10369 2822a352 2019-10-15 stsp
10370 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10371 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
10372 2822a352 2019-10-15 stsp if (error)
10373 2822a352 2019-10-15 stsp goto done;
10374 2822a352 2019-10-15 stsp
10375 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
10376 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
10377 2822a352 2019-10-15 stsp goto done;
10378 2822a352 2019-10-15 stsp }
10379 2822a352 2019-10-15 stsp
10380 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
10381 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
10382 2822a352 2019-10-15 stsp if (error)
10383 2822a352 2019-10-15 stsp goto done;
10384 2822a352 2019-10-15 stsp
10385 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
10386 2822a352 2019-10-15 stsp if (refname == NULL) {
10387 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10388 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10389 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10390 2822a352 2019-10-15 stsp goto done;
10391 2822a352 2019-10-15 stsp }
10392 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
10393 2822a352 2019-10-15 stsp if (base_refname == NULL) {
10394 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
10395 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10396 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10397 2822a352 2019-10-15 stsp goto done;
10398 2822a352 2019-10-15 stsp }
10399 2822a352 2019-10-15 stsp
10400 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
10401 2822a352 2019-10-15 stsp if (error)
10402 2822a352 2019-10-15 stsp goto done;
10403 2822a352 2019-10-15 stsp
10404 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
10405 2822a352 2019-10-15 stsp if (error)
10406 2822a352 2019-10-15 stsp goto done;
10407 2822a352 2019-10-15 stsp
10408 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
10409 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
10410 2822a352 2019-10-15 stsp "specified branch has already been integrated");
10411 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10412 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10413 2822a352 2019-10-15 stsp goto done;
10414 2822a352 2019-10-15 stsp }
10415 2822a352 2019-10-15 stsp
10416 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
10417 2822a352 2019-10-15 stsp if (error) {
10418 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
10419 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
10420 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
10421 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
10422 2822a352 2019-10-15 stsp goto done;
10423 2822a352 2019-10-15 stsp }
10424 2822a352 2019-10-15 stsp
10425 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10426 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
10427 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
10428 2822a352 2019-10-15 stsp check_cancelled, NULL);
10429 2822a352 2019-10-15 stsp if (error)
10430 2822a352 2019-10-15 stsp goto done;
10431 2822a352 2019-10-15 stsp
10432 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
10433 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10434 2822a352 2019-10-15 stsp done:
10435 1d0f4054 2021-06-17 stsp if (repo) {
10436 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10437 1d0f4054 2021-06-17 stsp if (error == NULL)
10438 1d0f4054 2021-06-17 stsp error = close_err;
10439 1d0f4054 2021-06-17 stsp }
10440 2822a352 2019-10-15 stsp if (worktree)
10441 2822a352 2019-10-15 stsp got_worktree_close(worktree);
10442 2822a352 2019-10-15 stsp free(cwd);
10443 2822a352 2019-10-15 stsp free(base_commit_id);
10444 2822a352 2019-10-15 stsp free(commit_id);
10445 2822a352 2019-10-15 stsp free(refname);
10446 2822a352 2019-10-15 stsp free(base_refname);
10447 715dc77e 2019-08-03 stsp return error;
10448 715dc77e 2019-08-03 stsp }
10449 715dc77e 2019-08-03 stsp
10450 715dc77e 2019-08-03 stsp __dead static void
10451 715dc77e 2019-08-03 stsp usage_stage(void)
10452 715dc77e 2019-08-03 stsp {
10453 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
10454 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
10455 715dc77e 2019-08-03 stsp getprogname());
10456 715dc77e 2019-08-03 stsp exit(1);
10457 715dc77e 2019-08-03 stsp }
10458 715dc77e 2019-08-03 stsp
10459 715dc77e 2019-08-03 stsp static const struct got_error *
10460 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
10461 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
10462 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
10463 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
10464 408b4ebc 2019-08-03 stsp {
10465 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
10466 408b4ebc 2019-08-03 stsp char *id_str = NULL;
10467 408b4ebc 2019-08-03 stsp
10468 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
10469 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
10470 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
10471 408b4ebc 2019-08-03 stsp return NULL;
10472 408b4ebc 2019-08-03 stsp
10473 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
10474 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
10475 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
10476 408b4ebc 2019-08-03 stsp else
10477 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
10478 408b4ebc 2019-08-03 stsp if (err)
10479 408b4ebc 2019-08-03 stsp return err;
10480 408b4ebc 2019-08-03 stsp
10481 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
10482 408b4ebc 2019-08-03 stsp free(id_str);
10483 dc424a06 2019-08-07 stsp return NULL;
10484 dc424a06 2019-08-07 stsp }
10485 2e1f37b0 2019-08-08 stsp
10486 dc424a06 2019-08-07 stsp static const struct got_error *
10487 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
10488 715dc77e 2019-08-03 stsp {
10489 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
10490 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
10491 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
10492 715dc77e 2019-08-03 stsp char *cwd = NULL;
10493 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
10494 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
10495 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
10496 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
10497 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10498 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10499 715dc77e 2019-08-03 stsp
10500 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
10501 715dc77e 2019-08-03 stsp
10502 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
10503 715dc77e 2019-08-03 stsp switch (ch) {
10504 408b4ebc 2019-08-03 stsp case 'l':
10505 408b4ebc 2019-08-03 stsp list_stage = 1;
10506 408b4ebc 2019-08-03 stsp break;
10507 dc424a06 2019-08-07 stsp case 'p':
10508 dc424a06 2019-08-07 stsp pflag = 1;
10509 dc424a06 2019-08-07 stsp break;
10510 dc424a06 2019-08-07 stsp case 'F':
10511 dc424a06 2019-08-07 stsp patch_script_path = optarg;
10512 dc424a06 2019-08-07 stsp break;
10513 35213c7c 2020-07-23 stsp case 'S':
10514 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
10515 35213c7c 2020-07-23 stsp break;
10516 715dc77e 2019-08-03 stsp default:
10517 715dc77e 2019-08-03 stsp usage_stage();
10518 715dc77e 2019-08-03 stsp /* NOTREACHED */
10519 715dc77e 2019-08-03 stsp }
10520 715dc77e 2019-08-03 stsp }
10521 715dc77e 2019-08-03 stsp
10522 715dc77e 2019-08-03 stsp argc -= optind;
10523 715dc77e 2019-08-03 stsp argv += optind;
10524 715dc77e 2019-08-03 stsp
10525 715dc77e 2019-08-03 stsp #ifndef PROFILE
10526 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10527 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
10528 715dc77e 2019-08-03 stsp err(1, "pledge");
10529 715dc77e 2019-08-03 stsp #endif
10530 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
10531 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
10532 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
10533 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
10534 715dc77e 2019-08-03 stsp
10535 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
10536 715dc77e 2019-08-03 stsp if (cwd == NULL) {
10537 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
10538 715dc77e 2019-08-03 stsp goto done;
10539 715dc77e 2019-08-03 stsp }
10540 715dc77e 2019-08-03 stsp
10541 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10542 fa51e947 2020-03-27 stsp if (error) {
10543 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10544 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
10545 715dc77e 2019-08-03 stsp goto done;
10546 fa51e947 2020-03-27 stsp }
10547 715dc77e 2019-08-03 stsp
10548 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10549 c9956ddf 2019-09-08 stsp NULL);
10550 715dc77e 2019-08-03 stsp if (error != NULL)
10551 715dc77e 2019-08-03 stsp goto done;
10552 715dc77e 2019-08-03 stsp
10553 dc424a06 2019-08-07 stsp if (patch_script_path) {
10554 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
10555 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
10556 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
10557 dc424a06 2019-08-07 stsp patch_script_path);
10558 dc424a06 2019-08-07 stsp goto done;
10559 dc424a06 2019-08-07 stsp }
10560 dc424a06 2019-08-07 stsp }
10561 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10562 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
10563 715dc77e 2019-08-03 stsp if (error)
10564 715dc77e 2019-08-03 stsp goto done;
10565 715dc77e 2019-08-03 stsp
10566 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10567 6d022e97 2019-08-04 stsp if (error)
10568 6d022e97 2019-08-04 stsp goto done;
10569 715dc77e 2019-08-03 stsp
10570 6d022e97 2019-08-04 stsp if (list_stage)
10571 f6343036 2021-06-22 stsp error = got_worktree_status(worktree, &paths, repo, 0,
10572 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
10573 2e1f37b0 2019-08-08 stsp else {
10574 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10575 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
10576 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
10577 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
10578 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
10579 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
10580 2e1f37b0 2019-08-08 stsp }
10581 ad493afc 2019-08-03 stsp done:
10582 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10583 2e1f37b0 2019-08-08 stsp error == NULL)
10584 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10585 1d0f4054 2021-06-17 stsp if (repo) {
10586 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10587 1d0f4054 2021-06-17 stsp if (error == NULL)
10588 1d0f4054 2021-06-17 stsp error = close_err;
10589 1d0f4054 2021-06-17 stsp }
10590 ad493afc 2019-08-03 stsp if (worktree)
10591 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
10592 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10593 ad493afc 2019-08-03 stsp free((char *)pe->path);
10594 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
10595 ad493afc 2019-08-03 stsp free(cwd);
10596 ad493afc 2019-08-03 stsp return error;
10597 ad493afc 2019-08-03 stsp }
10598 ad493afc 2019-08-03 stsp
10599 ad493afc 2019-08-03 stsp __dead static void
10600 ad493afc 2019-08-03 stsp usage_unstage(void)
10601 ad493afc 2019-08-03 stsp {
10602 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
10603 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
10604 ad493afc 2019-08-03 stsp getprogname());
10605 ad493afc 2019-08-03 stsp exit(1);
10606 ad493afc 2019-08-03 stsp }
10607 ad493afc 2019-08-03 stsp
10608 ad493afc 2019-08-03 stsp
10609 ad493afc 2019-08-03 stsp static const struct got_error *
10610 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
10611 ad493afc 2019-08-03 stsp {
10612 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
10613 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
10614 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
10615 ad493afc 2019-08-03 stsp char *cwd = NULL;
10616 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
10617 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
10618 9627c110 2020-04-18 stsp int ch, pflag = 0;
10619 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
10620 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
10621 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
10622 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
10623 ad493afc 2019-08-03 stsp
10624 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
10625 ad493afc 2019-08-03 stsp
10626 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
10627 ad493afc 2019-08-03 stsp switch (ch) {
10628 2e1f37b0 2019-08-08 stsp case 'p':
10629 2e1f37b0 2019-08-08 stsp pflag = 1;
10630 2e1f37b0 2019-08-08 stsp break;
10631 2e1f37b0 2019-08-08 stsp case 'F':
10632 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
10633 2e1f37b0 2019-08-08 stsp break;
10634 ad493afc 2019-08-03 stsp default:
10635 ad493afc 2019-08-03 stsp usage_unstage();
10636 ad493afc 2019-08-03 stsp /* NOTREACHED */
10637 ad493afc 2019-08-03 stsp }
10638 ad493afc 2019-08-03 stsp }
10639 ad493afc 2019-08-03 stsp
10640 ad493afc 2019-08-03 stsp argc -= optind;
10641 ad493afc 2019-08-03 stsp argv += optind;
10642 ad493afc 2019-08-03 stsp
10643 ad493afc 2019-08-03 stsp #ifndef PROFILE
10644 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10645 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
10646 ad493afc 2019-08-03 stsp err(1, "pledge");
10647 ad493afc 2019-08-03 stsp #endif
10648 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
10649 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
10650 2e1f37b0 2019-08-08 stsp
10651 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
10652 ad493afc 2019-08-03 stsp if (cwd == NULL) {
10653 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
10654 ad493afc 2019-08-03 stsp goto done;
10655 ad493afc 2019-08-03 stsp }
10656 ad493afc 2019-08-03 stsp
10657 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
10658 fa51e947 2020-03-27 stsp if (error) {
10659 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
10660 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
10661 ad493afc 2019-08-03 stsp goto done;
10662 fa51e947 2020-03-27 stsp }
10663 ad493afc 2019-08-03 stsp
10664 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
10665 c9956ddf 2019-09-08 stsp NULL);
10666 ad493afc 2019-08-03 stsp if (error != NULL)
10667 ad493afc 2019-08-03 stsp goto done;
10668 ad493afc 2019-08-03 stsp
10669 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
10670 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
10671 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
10672 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
10673 2e1f37b0 2019-08-08 stsp patch_script_path);
10674 2e1f37b0 2019-08-08 stsp goto done;
10675 2e1f37b0 2019-08-08 stsp }
10676 2e1f37b0 2019-08-08 stsp }
10677 2e1f37b0 2019-08-08 stsp
10678 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
10679 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
10680 ad493afc 2019-08-03 stsp if (error)
10681 ad493afc 2019-08-03 stsp goto done;
10682 ad493afc 2019-08-03 stsp
10683 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
10684 ad493afc 2019-08-03 stsp if (error)
10685 ad493afc 2019-08-03 stsp goto done;
10686 ad493afc 2019-08-03 stsp
10687 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
10688 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
10689 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
10690 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
10691 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
10692 9627c110 2020-04-18 stsp if (!error)
10693 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
10694 715dc77e 2019-08-03 stsp done:
10695 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
10696 2e1f37b0 2019-08-08 stsp error == NULL)
10697 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
10698 1d0f4054 2021-06-17 stsp if (repo) {
10699 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
10700 1d0f4054 2021-06-17 stsp if (error == NULL)
10701 1d0f4054 2021-06-17 stsp error = close_err;
10702 1d0f4054 2021-06-17 stsp }
10703 715dc77e 2019-08-03 stsp if (worktree)
10704 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
10705 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
10706 715dc77e 2019-08-03 stsp free((char *)pe->path);
10707 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
10708 715dc77e 2019-08-03 stsp free(cwd);
10709 01073a5d 2019-08-22 stsp return error;
10710 01073a5d 2019-08-22 stsp }
10711 01073a5d 2019-08-22 stsp
10712 01073a5d 2019-08-22 stsp __dead static void
10713 01073a5d 2019-08-22 stsp usage_cat(void)
10714 01073a5d 2019-08-22 stsp {
10715 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
10716 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
10717 01073a5d 2019-08-22 stsp exit(1);
10718 01073a5d 2019-08-22 stsp }
10719 01073a5d 2019-08-22 stsp
10720 01073a5d 2019-08-22 stsp static const struct got_error *
10721 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10722 01073a5d 2019-08-22 stsp {
10723 01073a5d 2019-08-22 stsp const struct got_error *err;
10724 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
10725 01073a5d 2019-08-22 stsp
10726 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
10727 01073a5d 2019-08-22 stsp if (err)
10728 01073a5d 2019-08-22 stsp return err;
10729 01073a5d 2019-08-22 stsp
10730 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
10731 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
10732 01073a5d 2019-08-22 stsp return err;
10733 01073a5d 2019-08-22 stsp }
10734 01073a5d 2019-08-22 stsp
10735 01073a5d 2019-08-22 stsp static const struct got_error *
10736 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10737 01073a5d 2019-08-22 stsp {
10738 01073a5d 2019-08-22 stsp const struct got_error *err;
10739 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
10740 56e0773d 2019-11-28 stsp int nentries, i;
10741 01073a5d 2019-08-22 stsp
10742 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
10743 01073a5d 2019-08-22 stsp if (err)
10744 01073a5d 2019-08-22 stsp return err;
10745 01073a5d 2019-08-22 stsp
10746 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
10747 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
10748 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
10749 01073a5d 2019-08-22 stsp char *id_str;
10750 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
10751 01073a5d 2019-08-22 stsp break;
10752 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
10753 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
10754 01073a5d 2019-08-22 stsp if (err)
10755 01073a5d 2019-08-22 stsp break;
10756 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
10757 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
10758 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
10759 01073a5d 2019-08-22 stsp free(id_str);
10760 01073a5d 2019-08-22 stsp }
10761 01073a5d 2019-08-22 stsp
10762 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
10763 01073a5d 2019-08-22 stsp return err;
10764 01073a5d 2019-08-22 stsp }
10765 01073a5d 2019-08-22 stsp
10766 01073a5d 2019-08-22 stsp static const struct got_error *
10767 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10768 01073a5d 2019-08-22 stsp {
10769 01073a5d 2019-08-22 stsp const struct got_error *err;
10770 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
10771 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
10772 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
10773 01073a5d 2019-08-22 stsp char *id_str = NULL;
10774 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
10775 01073a5d 2019-08-22 stsp
10776 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
10777 01073a5d 2019-08-22 stsp if (err)
10778 01073a5d 2019-08-22 stsp return err;
10779 01073a5d 2019-08-22 stsp
10780 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
10781 01073a5d 2019-08-22 stsp if (err)
10782 01073a5d 2019-08-22 stsp goto done;
10783 01073a5d 2019-08-22 stsp
10784 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
10785 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
10786 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
10787 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
10788 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parent_ids, entry) {
10789 01073a5d 2019-08-22 stsp char *pid_str;
10790 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
10791 01073a5d 2019-08-22 stsp if (err)
10792 01073a5d 2019-08-22 stsp goto done;
10793 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
10794 01073a5d 2019-08-22 stsp free(pid_str);
10795 01073a5d 2019-08-22 stsp }
10796 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
10797 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10798 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
10799 01073a5d 2019-08-22 stsp
10800 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
10801 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
10802 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
10803 01073a5d 2019-08-22 stsp
10804 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
10805 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
10806 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
10807 01073a5d 2019-08-22 stsp done:
10808 01073a5d 2019-08-22 stsp free(id_str);
10809 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
10810 01073a5d 2019-08-22 stsp return err;
10811 01073a5d 2019-08-22 stsp }
10812 01073a5d 2019-08-22 stsp
10813 01073a5d 2019-08-22 stsp static const struct got_error *
10814 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
10815 01073a5d 2019-08-22 stsp {
10816 01073a5d 2019-08-22 stsp const struct got_error *err;
10817 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
10818 01073a5d 2019-08-22 stsp char *id_str = NULL;
10819 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
10820 01073a5d 2019-08-22 stsp
10821 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
10822 01073a5d 2019-08-22 stsp if (err)
10823 01073a5d 2019-08-22 stsp return err;
10824 01073a5d 2019-08-22 stsp
10825 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
10826 01073a5d 2019-08-22 stsp if (err)
10827 01073a5d 2019-08-22 stsp goto done;
10828 01073a5d 2019-08-22 stsp
10829 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
10830 e15d5241 2019-08-22 stsp
10831 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
10832 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10833 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10834 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
10835 01073a5d 2019-08-22 stsp break;
10836 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10837 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10838 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
10839 01073a5d 2019-08-22 stsp break;
10840 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10841 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10842 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
10843 01073a5d 2019-08-22 stsp break;
10844 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10845 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
10846 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
10847 01073a5d 2019-08-22 stsp break;
10848 01073a5d 2019-08-22 stsp default:
10849 01073a5d 2019-08-22 stsp break;
10850 01073a5d 2019-08-22 stsp }
10851 01073a5d 2019-08-22 stsp
10852 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
10853 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
10854 e15d5241 2019-08-22 stsp
10855 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
10856 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
10857 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
10858 01073a5d 2019-08-22 stsp
10859 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
10860 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
10861 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
10862 01073a5d 2019-08-22 stsp done:
10863 01073a5d 2019-08-22 stsp free(id_str);
10864 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
10865 01073a5d 2019-08-22 stsp return err;
10866 01073a5d 2019-08-22 stsp }
10867 01073a5d 2019-08-22 stsp
10868 01073a5d 2019-08-22 stsp static const struct got_error *
10869 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
10870 01073a5d 2019-08-22 stsp {
10871 01073a5d 2019-08-22 stsp const struct got_error *error;
10872 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
10873 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
10874 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
10875 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
10876 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
10877 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
10878 84de9106 2020-12-26 stsp struct got_reflist_head refs;
10879 84de9106 2020-12-26 stsp
10880 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
10881 01073a5d 2019-08-22 stsp
10882 01073a5d 2019-08-22 stsp #ifndef PROFILE
10883 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
10884 01073a5d 2019-08-22 stsp NULL) == -1)
10885 01073a5d 2019-08-22 stsp err(1, "pledge");
10886 01073a5d 2019-08-22 stsp #endif
10887 01073a5d 2019-08-22 stsp
10888 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
10889 01073a5d 2019-08-22 stsp switch (ch) {
10890 896e9b6f 2019-08-26 stsp case 'c':
10891 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
10892 896e9b6f 2019-08-26 stsp break;
10893 01073a5d 2019-08-22 stsp case 'r':
10894 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
10895 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10896 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
10897 9ba1d308 2019-10-21 stsp optarg);
10898 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
10899 01073a5d 2019-08-22 stsp break;
10900 896e9b6f 2019-08-26 stsp case 'P':
10901 896e9b6f 2019-08-26 stsp force_path = 1;
10902 896e9b6f 2019-08-26 stsp break;
10903 01073a5d 2019-08-22 stsp default:
10904 01073a5d 2019-08-22 stsp usage_cat();
10905 01073a5d 2019-08-22 stsp /* NOTREACHED */
10906 01073a5d 2019-08-22 stsp }
10907 01073a5d 2019-08-22 stsp }
10908 01073a5d 2019-08-22 stsp
10909 01073a5d 2019-08-22 stsp argc -= optind;
10910 01073a5d 2019-08-22 stsp argv += optind;
10911 01073a5d 2019-08-22 stsp
10912 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
10913 01073a5d 2019-08-22 stsp if (cwd == NULL) {
10914 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
10915 01073a5d 2019-08-22 stsp goto done;
10916 01073a5d 2019-08-22 stsp }
10917 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
10918 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
10919 01073a5d 2019-08-22 stsp goto done;
10920 01073a5d 2019-08-22 stsp if (worktree) {
10921 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10922 01073a5d 2019-08-22 stsp repo_path = strdup(
10923 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
10924 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10925 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
10926 01073a5d 2019-08-22 stsp goto done;
10927 01073a5d 2019-08-22 stsp }
10928 01073a5d 2019-08-22 stsp }
10929 01073a5d 2019-08-22 stsp }
10930 01073a5d 2019-08-22 stsp
10931 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
10932 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
10933 01073a5d 2019-08-22 stsp if (repo_path == NULL)
10934 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
10935 01073a5d 2019-08-22 stsp }
10936 01073a5d 2019-08-22 stsp
10937 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
10938 01073a5d 2019-08-22 stsp free(repo_path);
10939 01073a5d 2019-08-22 stsp if (error != NULL)
10940 01073a5d 2019-08-22 stsp goto done;
10941 01073a5d 2019-08-22 stsp
10942 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
10943 896e9b6f 2019-08-26 stsp if (error)
10944 896e9b6f 2019-08-26 stsp goto done;
10945 896e9b6f 2019-08-26 stsp
10946 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10947 84de9106 2020-12-26 stsp if (error)
10948 84de9106 2020-12-26 stsp goto done;
10949 84de9106 2020-12-26 stsp
10950 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
10951 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
10952 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
10953 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
10954 01073a5d 2019-08-22 stsp if (error)
10955 01073a5d 2019-08-22 stsp goto done;
10956 01073a5d 2019-08-22 stsp
10957 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
10958 896e9b6f 2019-08-26 stsp if (force_path) {
10959 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
10960 896e9b6f 2019-08-26 stsp argv[i]);
10961 896e9b6f 2019-08-26 stsp if (error)
10962 896e9b6f 2019-08-26 stsp break;
10963 896e9b6f 2019-08-26 stsp } else {
10964 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
10965 84de9106 2020-12-26 stsp GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
10966 84de9106 2020-12-26 stsp repo);
10967 896e9b6f 2019-08-26 stsp if (error) {
10968 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
10969 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
10970 896e9b6f 2019-08-26 stsp break;
10971 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
10972 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
10973 896e9b6f 2019-08-26 stsp if (error)
10974 896e9b6f 2019-08-26 stsp break;
10975 896e9b6f 2019-08-26 stsp }
10976 896e9b6f 2019-08-26 stsp }
10977 01073a5d 2019-08-22 stsp
10978 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
10979 01073a5d 2019-08-22 stsp if (error)
10980 01073a5d 2019-08-22 stsp break;
10981 01073a5d 2019-08-22 stsp
10982 01073a5d 2019-08-22 stsp switch (obj_type) {
10983 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
10984 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
10985 01073a5d 2019-08-22 stsp break;
10986 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
10987 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
10988 01073a5d 2019-08-22 stsp break;
10989 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
10990 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
10991 01073a5d 2019-08-22 stsp break;
10992 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
10993 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
10994 01073a5d 2019-08-22 stsp break;
10995 01073a5d 2019-08-22 stsp default:
10996 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
10997 01073a5d 2019-08-22 stsp break;
10998 01073a5d 2019-08-22 stsp }
10999 01073a5d 2019-08-22 stsp if (error)
11000 01073a5d 2019-08-22 stsp break;
11001 01073a5d 2019-08-22 stsp free(label);
11002 01073a5d 2019-08-22 stsp label = NULL;
11003 01073a5d 2019-08-22 stsp free(id);
11004 01073a5d 2019-08-22 stsp id = NULL;
11005 01073a5d 2019-08-22 stsp }
11006 01073a5d 2019-08-22 stsp done:
11007 01073a5d 2019-08-22 stsp free(label);
11008 01073a5d 2019-08-22 stsp free(id);
11009 896e9b6f 2019-08-26 stsp free(commit_id);
11010 01073a5d 2019-08-22 stsp if (worktree)
11011 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
11012 01073a5d 2019-08-22 stsp if (repo) {
11013 1d0f4054 2021-06-17 stsp const struct got_error *close_err = got_repo_close(repo);
11014 01073a5d 2019-08-22 stsp if (error == NULL)
11015 1d0f4054 2021-06-17 stsp error = close_err;
11016 b2118c49 2020-07-28 stsp }
11017 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
11018 b2118c49 2020-07-28 stsp return error;
11019 b2118c49 2020-07-28 stsp }
11020 b2118c49 2020-07-28 stsp
11021 b2118c49 2020-07-28 stsp __dead static void
11022 b2118c49 2020-07-28 stsp usage_info(void)
11023 b2118c49 2020-07-28 stsp {
11024 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
11025 b2118c49 2020-07-28 stsp getprogname());
11026 b2118c49 2020-07-28 stsp exit(1);
11027 b2118c49 2020-07-28 stsp }
11028 b2118c49 2020-07-28 stsp
11029 b2118c49 2020-07-28 stsp static const struct got_error *
11030 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
11031 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
11032 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
11033 b2118c49 2020-07-28 stsp {
11034 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
11035 b2118c49 2020-07-28 stsp char *id_str = NULL;
11036 b2118c49 2020-07-28 stsp char datebuf[128];
11037 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
11038 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
11039 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11040 b2118c49 2020-07-28 stsp
11041 b2118c49 2020-07-28 stsp /*
11042 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
11043 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
11044 b2118c49 2020-07-28 stsp */
11045 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
11046 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
11047 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
11048 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
11049 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
11050 b2118c49 2020-07-28 stsp }
11051 b2118c49 2020-07-28 stsp
11052 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
11053 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
11054 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
11055 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
11056 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
11057 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
11058 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
11059 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
11060 b2118c49 2020-07-28 stsp else
11061 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
11062 b2118c49 2020-07-28 stsp
11063 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
11064 b2118c49 2020-07-28 stsp if (tm == NULL)
11065 b2118c49 2020-07-28 stsp return NULL;
11066 ec6d1a36 2021-03-21 jrick if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
11067 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
11068 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
11069 b2118c49 2020-07-28 stsp
11070 b2118c49 2020-07-28 stsp if (blob_id) {
11071 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
11072 b2118c49 2020-07-28 stsp if (err)
11073 b2118c49 2020-07-28 stsp return err;
11074 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
11075 b2118c49 2020-07-28 stsp free(id_str);
11076 b2118c49 2020-07-28 stsp }
11077 b2118c49 2020-07-28 stsp
11078 b2118c49 2020-07-28 stsp if (staged_blob_id) {
11079 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_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 staged 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 (commit_id) {
11087 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_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 commit: %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 return NULL;
11095 b2118c49 2020-07-28 stsp }
11096 b2118c49 2020-07-28 stsp
11097 b2118c49 2020-07-28 stsp static const struct got_error *
11098 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
11099 b2118c49 2020-07-28 stsp {
11100 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
11101 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
11102 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
11103 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
11104 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11105 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
11106 b2118c49 2020-07-28 stsp int ch, show_files = 0;
11107 b2118c49 2020-07-28 stsp
11108 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
11109 b2118c49 2020-07-28 stsp
11110 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
11111 b2118c49 2020-07-28 stsp switch (ch) {
11112 b2118c49 2020-07-28 stsp default:
11113 b2118c49 2020-07-28 stsp usage_info();
11114 b2118c49 2020-07-28 stsp /* NOTREACHED */
11115 b2118c49 2020-07-28 stsp }
11116 01073a5d 2019-08-22 stsp }
11117 b2118c49 2020-07-28 stsp
11118 b2118c49 2020-07-28 stsp argc -= optind;
11119 b2118c49 2020-07-28 stsp argv += optind;
11120 b2118c49 2020-07-28 stsp
11121 b2118c49 2020-07-28 stsp #ifndef PROFILE
11122 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
11123 b2118c49 2020-07-28 stsp NULL) == -1)
11124 b2118c49 2020-07-28 stsp err(1, "pledge");
11125 b2118c49 2020-07-28 stsp #endif
11126 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
11127 b2118c49 2020-07-28 stsp if (cwd == NULL) {
11128 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
11129 b2118c49 2020-07-28 stsp goto done;
11130 b2118c49 2020-07-28 stsp }
11131 b2118c49 2020-07-28 stsp
11132 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
11133 b2118c49 2020-07-28 stsp if (error) {
11134 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
11135 8ea5c997 2021-02-07 naddy error = wrap_not_worktree_error(error, "info", cwd);
11136 b2118c49 2020-07-28 stsp goto done;
11137 b2118c49 2020-07-28 stsp }
11138 b2118c49 2020-07-28 stsp
11139 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
11140 b2118c49 2020-07-28 stsp if (error)
11141 b2118c49 2020-07-28 stsp goto done;
11142 b2118c49 2020-07-28 stsp
11143 b2118c49 2020-07-28 stsp if (argc >= 1) {
11144 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
11145 b2118c49 2020-07-28 stsp worktree);
11146 b2118c49 2020-07-28 stsp if (error)
11147 b2118c49 2020-07-28 stsp goto done;
11148 b2118c49 2020-07-28 stsp show_files = 1;
11149 b2118c49 2020-07-28 stsp }
11150 b2118c49 2020-07-28 stsp
11151 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
11152 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
11153 b2118c49 2020-07-28 stsp if (error)
11154 b2118c49 2020-07-28 stsp goto done;
11155 b2118c49 2020-07-28 stsp
11156 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
11157 b2118c49 2020-07-28 stsp if (error)
11158 b2118c49 2020-07-28 stsp goto done;
11159 b2118c49 2020-07-28 stsp
11160 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
11161 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
11162 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
11163 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
11164 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
11165 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
11166 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
11167 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
11168 b2118c49 2020-07-28 stsp
11169 b2118c49 2020-07-28 stsp if (show_files) {
11170 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
11171 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11172 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
11173 b2118c49 2020-07-28 stsp continue;
11174 b2118c49 2020-07-28 stsp /*
11175 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
11176 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
11177 b2118c49 2020-07-28 stsp */
11178 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
11179 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
11180 b2118c49 2020-07-28 stsp }
11181 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
11182 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
11183 b2118c49 2020-07-28 stsp if (error)
11184 b2118c49 2020-07-28 stsp goto done;
11185 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
11186 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
11187 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
11188 b2118c49 2020-07-28 stsp break;
11189 b2118c49 2020-07-28 stsp }
11190 b2118c49 2020-07-28 stsp }
11191 b2118c49 2020-07-28 stsp }
11192 b2118c49 2020-07-28 stsp done:
11193 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
11194 b2118c49 2020-07-28 stsp free((char *)pe->path);
11195 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
11196 b2118c49 2020-07-28 stsp free(cwd);
11197 b2118c49 2020-07-28 stsp free(id_str);
11198 b2118c49 2020-07-28 stsp free(uuidstr);
11199 0ebf8283 2019-07-24 stsp return error;
11200 0ebf8283 2019-07-24 stsp }