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>
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.
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.
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 d2cdc636 2020-03-18 stsp #include <util.h>
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 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"
59 5c860e29 2018-03-12 stsp #ifndef nitems
60 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
67 99437157 2018-11-11 stsp catch_sigint(int signo)
69 99437157 2018-11-11 stsp sigint_received = 1;
73 99437157 2018-11-11 stsp catch_sigpipe(int signo)
75 99437157 2018-11-11 stsp sigpipe_received = 1;
79 8cfb4057 2019-07-09 stsp struct got_cmd {
80 820059fa 2020-09-25 stsp const char *cmd_name;
81 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
83 97b3a7be 2019-07-09 stsp const char *cmd_alias;
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
88 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
89 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
90 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
91 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
92 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
95 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
96 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
97 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
98 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
99 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
100 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
101 d00136be 2019-03-26 stsp __dead static void usage_add(void);
102 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
103 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
104 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
105 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
106 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
107 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
108 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
109 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
110 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
111 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
112 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
113 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
115 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
116 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
117 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
118 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
120 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
122 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
123 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
124 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
125 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
126 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
127 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
128 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
129 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
130 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
131 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
132 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
133 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
134 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
135 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
136 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
137 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
138 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
139 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
140 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
141 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
143 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
144 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
145 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
146 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
147 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
148 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
149 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
150 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
151 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
152 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
153 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
154 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
155 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
156 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
157 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
158 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
159 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
160 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
161 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
162 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
164 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
165 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
166 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
167 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
168 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
169 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
170 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
173 ce5b7c56 2019-07-09 stsp static void
174 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
178 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
179 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
180 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
181 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
183 6879ba42 2020-10-01 naddy fputc('\n', fp);
186 ff69268e 2020-12-13 stsp __dead static void
187 ff69268e 2020-12-13 stsp option_conflict(char a, char b)
189 ff69268e 2020-12-13 stsp errx(1, "-%c and -%c options are mutually exclusive", a, b);
193 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
195 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
198 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
199 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
200 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
201 62d463ca 2020-10-20 naddy { NULL, 0, NULL, 0 }
204 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
206 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
207 5c860e29 2018-03-12 stsp switch (ch) {
215 6879ba42 2020-10-01 naddy usage(hflag, 1);
216 5c860e29 2018-03-12 stsp /* NOTREACHED */
220 5c860e29 2018-03-12 stsp argc -= optind;
221 5c860e29 2018-03-12 stsp argv += optind;
222 9814e6a3 2020-09-27 naddy optind = 1;
223 9814e6a3 2020-09-27 naddy optreset = 1;
225 53ccebc2 2019-07-30 stsp if (Vflag) {
226 53ccebc2 2019-07-30 stsp got_version_print_str();
230 5c860e29 2018-03-12 stsp if (argc <= 0)
231 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
233 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
234 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
236 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
237 d7d4f210 2018-03-12 stsp const struct got_error *error;
239 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
241 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
242 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
246 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
248 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
249 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
250 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
251 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
252 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
253 70015d7a 2019-11-08 stsp !(sigint_received &&
254 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
255 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
262 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
263 6879ba42 2020-10-01 naddy list_commands(stderr);
267 4ed7e80c 2018-05-20 stsp __dead static void
268 6879ba42 2020-10-01 naddy usage(int hflag, int status)
270 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
272 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
273 53ccebc2 2019-07-30 stsp getprogname());
275 6879ba42 2020-10-01 naddy list_commands(fp);
276 6879ba42 2020-10-01 naddy exit(status);
279 0266afb7 2019-01-04 stsp static const struct got_error *
280 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
282 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
283 e2ba3d07 2019-05-13 stsp const char *editor;
285 8920fa04 2019-08-18 stsp *abspath = NULL;
287 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
288 e2ba3d07 2019-05-13 stsp if (editor == NULL)
289 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
291 0ee7065d 2019-05-13 stsp if (editor) {
292 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
294 0ee7065d 2019-05-13 stsp return err;
297 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
298 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
299 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
300 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
303 e2ba3d07 2019-05-13 stsp return NULL;
306 e2ba3d07 2019-05-13 stsp static const struct got_error *
307 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
308 c530dc23 2019-07-23 stsp const char *worktree_path)
310 163ce85a 2019-05-13 stsp const struct got_error *err;
312 37c06ea4 2019-07-15 stsp #ifdef PROFILE
313 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
314 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
316 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
319 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
320 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
322 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
323 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
325 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
326 163ce85a 2019-05-13 stsp if (err != NULL)
327 163ce85a 2019-05-13 stsp return err;
329 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
330 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
332 0266afb7 2019-01-04 stsp return NULL;
335 2c7829a4 2019-06-17 stsp __dead static void
336 2c7829a4 2019-06-17 stsp usage_init(void)
338 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
342 2c7829a4 2019-06-17 stsp static const struct got_error *
343 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
345 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
346 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
349 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
350 2c7829a4 2019-06-17 stsp switch (ch) {
352 2c7829a4 2019-06-17 stsp usage_init();
353 2c7829a4 2019-06-17 stsp /* NOTREACHED */
357 2c7829a4 2019-06-17 stsp argc -= optind;
358 2c7829a4 2019-06-17 stsp argv += optind;
360 2c7829a4 2019-06-17 stsp #ifndef PROFILE
361 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
362 2c7829a4 2019-06-17 stsp err(1, "pledge");
364 2c7829a4 2019-06-17 stsp if (argc != 1)
365 bc20e173 2019-06-17 stsp usage_init();
367 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
368 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
369 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
371 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
373 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
374 2c7829a4 2019-06-17 stsp if (error &&
375 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
378 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
382 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
384 3ce1b845 2019-07-15 stsp free(repo_path);
385 3ce1b845 2019-07-15 stsp return error;
388 3ce1b845 2019-07-15 stsp __dead static void
389 3ce1b845 2019-07-15 stsp usage_import(void)
391 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
392 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
397 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
400 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
401 3ce1b845 2019-07-15 stsp int st = -1;
403 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
404 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
405 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
407 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
409 3ce1b845 2019-07-15 stsp goto doneediting;
411 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
412 3ce1b845 2019-07-15 stsp _exit(127);
415 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
416 3ce1b845 2019-07-15 stsp if (errno != EINTR)
419 3ce1b845 2019-07-15 stsp doneediting:
420 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
421 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
422 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
424 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
425 3ce1b845 2019-07-15 stsp errno = EINTR;
429 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
432 3ce1b845 2019-07-15 stsp static const struct got_error *
433 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
434 28cf319f 2021-01-28 stsp const char *initial_content, size_t initial_content_len,
435 28cf319f 2021-01-28 stsp int require_modification)
437 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
438 bfa12d5e 2020-09-26 stsp char *line = NULL;
439 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
440 bfa12d5e 2020-09-26 stsp ssize_t linelen;
441 3ce1b845 2019-07-15 stsp struct stat st, st2;
442 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
443 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
444 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
446 3ce1b845 2019-07-15 stsp *logmsg = NULL;
448 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
449 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
451 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
452 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
454 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
455 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
457 28cf319f 2021-01-28 stsp if (require_modification &&
458 28cf319f 2021-01-28 stsp st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
459 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
460 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
463 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
464 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
465 bfa12d5e 2020-09-26 stsp * has in fact been edited.
467 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
468 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
469 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
470 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
472 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
473 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
474 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
479 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
480 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
481 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
482 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
483 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
484 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
485 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
489 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
490 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
494 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
495 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
496 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
497 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
498 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
500 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
501 3ce1b845 2019-07-15 stsp if (fp == NULL) {
502 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
507 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
508 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
509 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
510 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
511 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
512 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
516 bfa12d5e 2020-09-26 stsp free(line);
517 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
518 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
521 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
522 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
526 bfa12d5e 2020-09-26 stsp if (len == 0) {
527 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
528 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
531 28cf319f 2021-01-28 stsp if (require_modification &&
532 28cf319f 2021-01-28 stsp strcmp(*logmsg, initial_content_stripped) == 0)
533 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
534 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
536 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
538 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
539 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
541 3ce1b845 2019-07-15 stsp free(*logmsg);
542 3ce1b845 2019-07-15 stsp *logmsg = NULL;
544 3ce1b845 2019-07-15 stsp return err;
547 3ce1b845 2019-07-15 stsp static const struct got_error *
548 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
549 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
551 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
552 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
553 1601cb9f 2020-09-11 naddy int initial_content_len;
554 97972933 2020-09-11 stsp int fd = -1;
556 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
557 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
558 1601cb9f 2020-09-11 naddy branch_name);
559 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
560 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
562 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
563 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
567 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
568 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
572 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
573 0d5bb276 2020-12-15 stsp initial_content_len, 1);
575 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
576 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
577 3ce1b845 2019-07-15 stsp free(initial_content);
579 59f86c76 2020-09-11 stsp free(*logmsg_path);
580 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
582 3ce1b845 2019-07-15 stsp return err;
585 3ce1b845 2019-07-15 stsp static const struct got_error *
586 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
588 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
589 3ce1b845 2019-07-15 stsp return NULL;
592 3ce1b845 2019-07-15 stsp static const struct got_error *
593 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
594 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
596 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
597 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
598 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
600 84792843 2019-08-09 stsp *author = NULL;
602 50b0790e 2020-09-11 stsp if (worktree)
603 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
604 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
607 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
608 50b0790e 2020-09-11 stsp * significant to least significant:
609 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
610 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
611 50b0790e 2020-09-11 stsp * 3) repository's git config file
612 50b0790e 2020-09-11 stsp * 4) environment variables
613 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
616 50b0790e 2020-09-11 stsp if (worktree_conf)
617 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
618 50b0790e 2020-09-11 stsp if (got_author == NULL)
619 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
620 84792843 2019-08-09 stsp if (got_author == NULL) {
621 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
622 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
623 c9956ddf 2019-09-08 stsp if (name && email) {
624 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
625 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
626 c9956ddf 2019-09-08 stsp return NULL;
629 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
630 257add31 2020-09-09 stsp if (got_author == NULL) {
631 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
632 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
634 257add31 2020-09-09 stsp if (name && email) {
635 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
637 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
638 257add31 2020-09-09 stsp return NULL;
640 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
641 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
645 aba9c984 2019-09-08 stsp *author = strdup(got_author);
646 aba9c984 2019-09-08 stsp if (*author == NULL)
647 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
650 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
651 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
653 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
654 84792843 2019-08-09 stsp got_author++;
655 aba9c984 2019-09-08 stsp if (*got_author != '<') {
656 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
659 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
660 84792843 2019-08-09 stsp got_author++;
661 aba9c984 2019-09-08 stsp if (*got_author != '@') {
662 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
665 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
666 84792843 2019-08-09 stsp got_author++;
667 84792843 2019-08-09 stsp if (*got_author != '>')
668 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
671 aba9c984 2019-09-08 stsp free(*author);
672 aba9c984 2019-09-08 stsp *author = NULL;
674 aba9c984 2019-09-08 stsp return err;
677 c9956ddf 2019-09-08 stsp static const struct got_error *
678 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
680 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
682 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
683 c9956ddf 2019-09-08 stsp if (homedir) {
684 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
685 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
688 c9956ddf 2019-09-08 stsp return NULL;
691 84792843 2019-08-09 stsp static const struct got_error *
692 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
694 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
695 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
696 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
697 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
698 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
699 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
700 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
701 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
703 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
704 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
705 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
707 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
709 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
710 3ce1b845 2019-07-15 stsp switch (ch) {
712 3ce1b845 2019-07-15 stsp branch_name = optarg;
715 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
716 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
717 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
722 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
723 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
724 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
730 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
732 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
738 b2b65d18 2019-08-22 stsp usage_import();
739 3ce1b845 2019-07-15 stsp /* NOTREACHED */
743 3ce1b845 2019-07-15 stsp argc -= optind;
744 3ce1b845 2019-07-15 stsp argv += optind;
746 3ce1b845 2019-07-15 stsp #ifndef PROFILE
747 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
749 3ce1b845 2019-07-15 stsp NULL) == -1)
750 3ce1b845 2019-07-15 stsp err(1, "pledge");
752 3ce1b845 2019-07-15 stsp if (argc != 1)
753 3ce1b845 2019-07-15 stsp usage_import();
755 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
756 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
757 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
758 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
760 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
761 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
764 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
768 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
770 aba9c984 2019-09-08 stsp return error;
773 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
774 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
775 e560b7e0 2019-11-28 stsp * an unintended typo.
777 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
778 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
780 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
781 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
785 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
786 3ce1b845 2019-07-15 stsp if (error) {
787 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
790 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
791 3ce1b845 2019-07-15 stsp "import target branch already exists");
795 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
796 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
797 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
800 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
803 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
804 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
806 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
807 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
810 8e158b01 2019-09-22 stsp free(logmsg);
811 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
812 ef293bdd 2019-10-21 stsp path_dir, refname);
813 ef293bdd 2019-10-21 stsp if (error) {
814 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
815 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
816 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
821 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
822 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
823 ef293bdd 2019-10-21 stsp if (logmsg_path)
824 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
828 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
829 ef293bdd 2019-10-21 stsp if (error) {
830 ef293bdd 2019-10-21 stsp if (logmsg_path)
831 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
835 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
836 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
837 ef293bdd 2019-10-21 stsp if (error) {
838 ef293bdd 2019-10-21 stsp if (logmsg_path)
839 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
843 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
844 ef293bdd 2019-10-21 stsp if (error) {
845 ef293bdd 2019-10-21 stsp if (logmsg_path)
846 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
850 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
851 ef293bdd 2019-10-21 stsp if (error) {
852 ef293bdd 2019-10-21 stsp if (logmsg_path)
853 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
857 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
858 ef293bdd 2019-10-21 stsp if (error) {
859 ef293bdd 2019-10-21 stsp if (logmsg_path)
860 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
864 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
865 3ce1b845 2019-07-15 stsp if (error) {
866 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
867 ef293bdd 2019-10-21 stsp if (logmsg_path)
868 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
872 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
873 3ce1b845 2019-07-15 stsp branch_ref);
874 ef293bdd 2019-10-21 stsp if (error) {
875 ef293bdd 2019-10-21 stsp if (logmsg_path)
876 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
880 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
881 ef293bdd 2019-10-21 stsp if (error) {
882 ef293bdd 2019-10-21 stsp if (logmsg_path)
883 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
888 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
889 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
891 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
892 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
893 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
894 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
895 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
896 8e158b01 2019-09-22 stsp free(logmsg);
897 ef293bdd 2019-10-21 stsp free(logmsg_path);
898 2c7829a4 2019-06-17 stsp free(repo_path);
899 3ce1b845 2019-07-15 stsp free(editor);
900 3ce1b845 2019-07-15 stsp free(refname);
901 3ce1b845 2019-07-15 stsp free(new_commit_id);
902 3ce1b845 2019-07-15 stsp free(id_str);
903 aba9c984 2019-09-08 stsp free(author);
904 c9956ddf 2019-09-08 stsp free(gitconfig_path);
905 3ce1b845 2019-07-15 stsp if (branch_ref)
906 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
907 3ce1b845 2019-07-15 stsp if (head_ref)
908 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
909 2c7829a4 2019-06-17 stsp return error;
912 93658fb9 2020-03-18 stsp __dead static void
913 93658fb9 2020-03-18 stsp usage_clone(void)
915 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
916 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
920 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
921 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
922 892ac3b6 2020-03-18 stsp int last_p_indexed;
923 892ac3b6 2020-03-18 stsp int last_p_resolved;
924 68999b92 2020-03-18 stsp int verbosity;
926 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
928 04d9a9ec 2020-09-24 stsp int create_configs;
929 04d9a9ec 2020-09-24 stsp int configs_created;
931 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
932 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
933 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs;
934 04d9a9ec 2020-09-24 stsp const char *proto;
935 04d9a9ec 2020-09-24 stsp const char *host;
936 04d9a9ec 2020-09-24 stsp const char *port;
937 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
938 04d9a9ec 2020-09-24 stsp const char *git_url;
939 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
940 04d9a9ec 2020-09-24 stsp int mirror_references;
941 04d9a9ec 2020-09-24 stsp } config_info;
944 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
945 93658fb9 2020-03-18 stsp static const struct got_error *
946 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
947 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
948 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
949 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
950 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo);
952 04d9a9ec 2020-09-24 stsp static const struct got_error *
953 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
954 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
956 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
957 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
958 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
959 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
960 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
963 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
964 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
965 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
966 04d9a9ec 2020-09-24 stsp * we have all required information.
968 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
969 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
970 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
971 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
972 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
973 62d463ca 2020-10-20 naddy a->config_info.git_url,
974 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
975 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
976 62d463ca 2020-10-20 naddy a->config_info.symrefs,
977 99495ddb 2021-01-10 stsp a->config_info.wanted_branches,
978 99495ddb 2021-01-10 stsp a->config_info.wanted_refs, a->repo);
980 04d9a9ec 2020-09-24 stsp return err;
981 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
984 68999b92 2020-03-18 stsp if (a->verbosity < 0)
985 68999b92 2020-03-18 stsp return NULL;
987 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
988 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
989 892ac3b6 2020-03-18 stsp fflush(stdout);
990 12d1281e 2020-03-19 stsp return NULL;
993 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
994 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
995 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
996 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
997 892ac3b6 2020-03-18 stsp print_size = 1;
998 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
999 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1000 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
1002 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
1003 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
1004 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
1005 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
1006 892ac3b6 2020-03-18 stsp print_indexed = 1;
1007 892ac3b6 2020-03-18 stsp print_size = 1;
1010 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
1011 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1012 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1013 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1014 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1015 892ac3b6 2020-03-18 stsp print_resolved = 1;
1016 892ac3b6 2020-03-18 stsp print_indexed = 1;
1017 892ac3b6 2020-03-18 stsp print_size = 1;
1022 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1023 892ac3b6 2020-03-18 stsp printf("\r");
1024 892ac3b6 2020-03-18 stsp if (print_size)
1025 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1026 d715f13e 2020-03-19 stsp if (print_indexed)
1027 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1028 d715f13e 2020-03-19 stsp if (print_resolved)
1029 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1030 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1031 892ac3b6 2020-03-18 stsp fflush(stdout);
1033 531c3985 2020-03-18 stsp return NULL;
1036 531c3985 2020-03-18 stsp static const struct got_error *
1037 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1038 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1040 4ba14133 2020-03-20 stsp const struct got_error *err;
1041 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1043 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1045 4ba14133 2020-03-20 stsp return err;
1047 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1048 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1049 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1050 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1052 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1053 4ba14133 2020-03-20 stsp return err;
1056 4ba14133 2020-03-20 stsp static const struct got_error *
1057 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1058 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1060 41b0de12 2020-03-21 stsp const struct got_error *err;
1061 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1063 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1064 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1065 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1067 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1070 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1071 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1072 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1073 41b0de12 2020-03-21 stsp char *id_str;
1075 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1077 41b0de12 2020-03-21 stsp return err;
1078 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1079 41b0de12 2020-03-21 stsp free(id_str);
1082 41b0de12 2020-03-21 stsp return NULL;
1085 6338a6a1 2020-03-21 stsp static const struct got_error *
1086 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1087 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1089 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1090 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1091 6338a6a1 2020-03-21 stsp char *id_str;
1093 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1095 6338a6a1 2020-03-21 stsp return err;
1097 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1099 6338a6a1 2020-03-21 stsp goto done;
1101 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1102 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1104 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1105 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1107 6338a6a1 2020-03-21 stsp free(id_str);
1108 6338a6a1 2020-03-21 stsp return err;
1111 0e4002ca 2020-03-21 stsp static int
1112 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1114 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1116 0e4002ca 2020-03-21 stsp refname += 5;
1119 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1120 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1122 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1124 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1127 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1128 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1130 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1131 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1134 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1135 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1138 0e4002ca 2020-03-21 stsp static int
1139 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1141 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1143 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1144 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1151 41b0de12 2020-03-21 stsp static const struct got_error *
1152 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1153 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1155 0e4002ca 2020-03-21 stsp const struct got_error *err;
1156 0e4002ca 2020-03-21 stsp char *remote_refname;
1158 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1159 0e4002ca 2020-03-21 stsp refname += 5;
1161 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1162 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1163 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1165 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1166 0e4002ca 2020-03-21 stsp free(remote_refname);
1167 7c0b7f42 2020-09-24 stsp return err;
1170 7c0b7f42 2020-09-24 stsp static const struct got_error *
1171 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1172 15d3c221 2021-01-05 stsp const char *remote_repo_path, const char *default_branch,
1173 0c8b29c5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1174 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1175 99495ddb 2021-01-10 stsp struct got_repository *repo)
1177 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1178 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1179 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1180 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1181 15d3c221 2021-01-05 stsp const char *branchname = NULL;
1182 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1183 7c0b7f42 2020-09-24 stsp ssize_t n;
1185 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1186 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1187 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1189 132af4a5 2021-01-05 stsp branchname = pe->path;
1190 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1191 132af4a5 2021-01-05 stsp branchname += 11;
1192 132af4a5 2021-01-05 stsp if (asprintf(&s, "%s\"%s\" ",
1193 132af4a5 2021-01-05 stsp branches ? branches : "", branchname) == -1) {
1194 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1195 132af4a5 2021-01-05 stsp goto done;
1197 132af4a5 2021-01-05 stsp free(branches);
1198 132af4a5 2021-01-05 stsp branches = s;
1200 0c8b29c5 2021-01-05 stsp } else if (!fetch_all_branches && default_branch) {
1201 15d3c221 2021-01-05 stsp branchname = default_branch;
1202 15d3c221 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1203 15d3c221 2021-01-05 stsp branchname += 11;
1204 132af4a5 2021-01-05 stsp if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1205 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1206 132af4a5 2021-01-05 stsp goto done;
1209 99495ddb 2021-01-10 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1210 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1211 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1213 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1214 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1215 99495ddb 2021-01-10 stsp branchname += 5;
1216 99495ddb 2021-01-10 stsp if (asprintf(&s, "%s\"%s\" ",
1217 99495ddb 2021-01-10 stsp refs ? refs : "", refname) == -1) {
1218 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1219 99495ddb 2021-01-10 stsp goto done;
1221 99495ddb 2021-01-10 stsp free(refs);
1226 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1227 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1228 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1229 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1230 7c0b7f42 2020-09-24 stsp goto done;
1232 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1233 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1234 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1235 7c0b7f42 2020-09-24 stsp goto done;
1237 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1238 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1239 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1240 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1242 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1248 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1249 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1250 132af4a5 2021-01-05 stsp remote_repo_path, branches ? "\tbranch { " : "",
1251 132af4a5 2021-01-05 stsp branches ? branches : "", branches ? "}\n" : "",
1252 99495ddb 2021-01-10 stsp refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1253 0c8b29c5 2021-01-05 stsp mirror_references ? "\tmirror-references yes\n" : "",
1254 0c8b29c5 2021-01-05 stsp fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1255 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1256 7c0b7f42 2020-09-24 stsp goto done;
1258 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1259 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1260 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1261 7c0b7f42 2020-09-24 stsp goto done;
1265 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1266 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1267 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1268 132af4a5 2021-01-05 stsp free(branches);
1269 7c0b7f42 2020-09-24 stsp return err;
1272 7c0b7f42 2020-09-24 stsp static const struct got_error *
1273 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1274 132af4a5 2021-01-05 stsp int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1275 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, int mirror_references,
1276 99495ddb 2021-01-10 stsp struct got_repository *repo)
1278 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1279 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1280 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1281 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1282 99495ddb 2021-01-10 stsp char *branches = NULL, *refs = NULL;
1283 56d0a753 2021-01-20 stsp const char *branchname;
1284 7c0b7f42 2020-09-24 stsp ssize_t n;
1286 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1287 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1288 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1289 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1290 7c0b7f42 2020-09-24 stsp goto done;
1292 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1293 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1294 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1295 7c0b7f42 2020-09-24 stsp goto done;
1297 56d0a753 2021-01-20 stsp if (fetch_all_branches) {
1298 56d0a753 2021-01-20 stsp if (mirror_references) {
1299 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1300 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1301 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1302 56d0a753 2021-01-20 stsp goto done;
1304 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1305 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1306 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1307 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1308 7c0b7f42 2020-09-24 stsp goto done;
1310 132af4a5 2021-01-05 stsp } else if (!TAILQ_EMPTY(wanted_branches)) {
1311 132af4a5 2021-01-05 stsp struct got_pathlist_entry *pe;
1312 132af4a5 2021-01-05 stsp TAILQ_FOREACH(pe, wanted_branches, entry) {
1314 132af4a5 2021-01-05 stsp branchname = pe->path;
1315 132af4a5 2021-01-05 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1316 132af4a5 2021-01-05 stsp branchname += 11;
1317 56d0a753 2021-01-20 stsp if (mirror_references) {
1318 56d0a753 2021-01-20 stsp if (asprintf(&s,
1319 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1320 56d0a753 2021-01-20 stsp branches ? branches : "",
1321 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1322 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1323 56d0a753 2021-01-20 stsp goto done;
1325 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1326 56d0a753 2021-01-20 stsp "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1327 132af4a5 2021-01-05 stsp branches ? branches : "",
1328 132af4a5 2021-01-05 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1329 132af4a5 2021-01-05 stsp branchname) == -1) {
1330 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1331 132af4a5 2021-01-05 stsp goto done;
1333 132af4a5 2021-01-05 stsp free(branches);
1334 132af4a5 2021-01-05 stsp branches = s;
1338 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1339 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1341 04d9a9ec 2020-09-24 stsp if (default_branch) {
1342 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1343 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1344 7c0b7f42 2020-09-24 stsp branchname += 11;
1346 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1347 56d0a753 2021-01-20 stsp if (mirror_references) {
1348 56d0a753 2021-01-20 stsp if (asprintf(&branches,
1349 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/heads/%s\n",
1350 56d0a753 2021-01-20 stsp branchname, branchname) == -1) {
1351 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1352 56d0a753 2021-01-20 stsp goto done;
1354 56d0a753 2021-01-20 stsp } else if (asprintf(&branches,
1355 56d0a753 2021-01-20 stsp "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1356 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1357 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1358 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1359 7c0b7f42 2020-09-24 stsp goto done;
1362 56d0a753 2021-01-20 stsp if (!TAILQ_EMPTY(wanted_refs)) {
1363 99495ddb 2021-01-10 stsp struct got_pathlist_entry *pe;
1364 99495ddb 2021-01-10 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1366 99495ddb 2021-01-10 stsp const char *refname = pe->path;
1367 99495ddb 2021-01-10 stsp if (strncmp(refname, "refs/", 5) == 0)
1368 99495ddb 2021-01-10 stsp refname += 5;
1369 56d0a753 2021-01-20 stsp if (mirror_references) {
1370 56d0a753 2021-01-20 stsp if (asprintf(&s,
1371 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/%s\n",
1372 56d0a753 2021-01-20 stsp refs ? refs : "", refname, refname) == -1) {
1373 56d0a753 2021-01-20 stsp err = got_error_from_errno("asprintf");
1374 56d0a753 2021-01-20 stsp goto done;
1376 56d0a753 2021-01-20 stsp } else if (asprintf(&s,
1377 56d0a753 2021-01-20 stsp "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1378 99495ddb 2021-01-10 stsp refs ? refs : "",
1379 99495ddb 2021-01-10 stsp refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1380 99495ddb 2021-01-10 stsp refname) == -1) {
1381 99495ddb 2021-01-10 stsp err = got_error_from_errno("asprintf");
1382 99495ddb 2021-01-10 stsp goto done;
1384 99495ddb 2021-01-10 stsp free(refs);
1389 132af4a5 2021-01-05 stsp if (asprintf(&gitconfig,
1390 132af4a5 2021-01-05 stsp "[remote \"%s\"]\n"
1391 132af4a5 2021-01-05 stsp "\turl = %s\n"
1394 56d0a753 2021-01-20 stsp "\tfetch = refs/tags/*:refs/tags/*\n",
1395 132af4a5 2021-01-05 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1396 56d0a753 2021-01-20 stsp refs ? refs : "") == -1) {
1397 132af4a5 2021-01-05 stsp err = got_error_from_errno("asprintf");
1398 132af4a5 2021-01-05 stsp goto done;
1400 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1401 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1402 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1403 7c0b7f42 2020-09-24 stsp goto done;
1406 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1407 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1408 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1409 132af4a5 2021-01-05 stsp free(branches);
1410 0e4002ca 2020-03-21 stsp return err;
1413 0e4002ca 2020-03-21 stsp static const struct got_error *
1414 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1415 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1416 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1417 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_branches,
1418 99495ddb 2021-01-10 stsp struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1420 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1421 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1422 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1425 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1426 04d9a9ec 2020-09-24 stsp * one of those.
1428 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1429 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1430 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1432 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1433 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1434 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1435 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1437 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1440 04d9a9ec 2020-09-24 stsp default_branch = target;
1445 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1446 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1447 0c8b29c5 2021-01-05 stsp default_branch, fetch_all_branches, wanted_branches,
1448 99495ddb 2021-01-10 stsp wanted_refs, mirror_references, repo);
1450 04d9a9ec 2020-09-24 stsp return err;
1452 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1453 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1454 99495ddb 2021-01-10 stsp wanted_branches, wanted_refs, mirror_references, repo);
1457 04d9a9ec 2020-09-24 stsp static const struct got_error *
1458 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1460 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1461 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1462 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1463 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1464 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1465 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1466 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1467 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1468 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1469 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1470 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1471 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1472 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1473 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1474 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1476 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1477 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1478 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1479 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1481 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1482 93658fb9 2020-03-18 stsp switch (ch) {
1484 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1487 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1488 4ba14133 2020-03-20 stsp optarg, NULL);
1489 4ba14133 2020-03-20 stsp if (error)
1490 4ba14133 2020-03-20 stsp return error;
1493 41b0de12 2020-03-21 stsp list_refs_only = 1;
1496 469dd726 2020-03-20 stsp mirror_references = 1;
1499 68999b92 2020-03-18 stsp if (verbosity < 0)
1500 68999b92 2020-03-18 stsp verbosity = 0;
1501 68999b92 2020-03-18 stsp else if (verbosity < 3)
1502 68999b92 2020-03-18 stsp verbosity++;
1505 68999b92 2020-03-18 stsp verbosity = -1;
1508 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1509 0e4002ca 2020-03-21 stsp optarg, NULL);
1510 0e4002ca 2020-03-21 stsp if (error)
1511 0e4002ca 2020-03-21 stsp return error;
1514 93658fb9 2020-03-18 stsp usage_clone();
1518 93658fb9 2020-03-18 stsp argc -= optind;
1519 93658fb9 2020-03-18 stsp argv += optind;
1521 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1522 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
1523 41b0de12 2020-03-21 stsp if (list_refs_only) {
1524 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1525 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
1526 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1527 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
1528 41b0de12 2020-03-21 stsp if (mirror_references)
1529 ff69268e 2020-12-13 stsp option_conflict('l', 'm');
1530 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1531 ff69268e 2020-12-13 stsp option_conflict('l', 'R');
1534 93658fb9 2020-03-18 stsp uri = argv[0];
1536 9df6f38b 2020-03-18 stsp if (argc == 1)
1537 93658fb9 2020-03-18 stsp dirname = NULL;
1538 9df6f38b 2020-03-18 stsp else if (argc == 2)
1539 93658fb9 2020-03-18 stsp dirname = argv[1];
1541 93658fb9 2020-03-18 stsp usage_clone();
1543 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1544 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1545 39c64a6a 2020-03-18 stsp if (error)
1546 09f63084 2020-03-20 stsp goto done;
1548 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1549 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1550 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1551 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1552 09838ffc 2020-03-18 stsp goto done;
1555 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1556 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1557 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1558 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1559 39c64a6a 2020-03-18 stsp err(1, "pledge");
1561 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1562 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1563 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1564 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1565 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1566 39c64a6a 2020-03-18 stsp err(1, "pledge");
1568 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1569 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1570 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1571 39c64a6a 2020-03-18 stsp goto done;
1573 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1574 39c64a6a 2020-03-18 stsp goto done;
1576 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1577 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1578 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1579 bb64b798 2020-03-18 stsp goto done;
1581 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1583 bb64b798 2020-03-18 stsp repo_path = dirname;
1585 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1586 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1587 2751fe64 2020-09-24 stsp if (error &&
1588 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1589 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1590 41b0de12 2020-03-21 stsp goto done;
1591 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1592 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1593 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1594 41b0de12 2020-03-21 stsp goto done;
1598 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1599 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1600 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1601 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1602 ee448f5f 2020-03-18 stsp goto done;
1605 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1606 ee448f5f 2020-03-18 stsp if (error)
1607 ee448f5f 2020-03-18 stsp goto done;
1609 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1610 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1611 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1613 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1614 9c52365f 2020-03-21 stsp server_path, verbosity);
1615 39c64a6a 2020-03-18 stsp if (error)
1616 bb64b798 2020-03-18 stsp goto done;
1618 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1619 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1620 2751fe64 2020-09-24 stsp if (error)
1621 2751fe64 2020-09-24 stsp goto done;
1622 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1623 2751fe64 2020-09-24 stsp if (error)
1624 2751fe64 2020-09-24 stsp goto done;
1627 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1628 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1629 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1630 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1631 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1632 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1633 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1634 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1635 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1636 99495ddb 2021-01-10 stsp fpa.config_info.wanted_refs = &wanted_refs;
1637 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1638 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1639 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1640 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1641 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1642 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1643 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1644 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1645 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1646 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1647 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1648 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1649 39c64a6a 2020-03-18 stsp if (error)
1650 d9b4d0c0 2020-03-18 stsp goto done;
1652 41b0de12 2020-03-21 stsp if (list_refs_only) {
1653 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1654 41b0de12 2020-03-21 stsp goto done;
1657 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1658 39c64a6a 2020-03-18 stsp if (error)
1659 d9b4d0c0 2020-03-18 stsp goto done;
1660 68999b92 2020-03-18 stsp if (verbosity >= 0)
1661 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1662 d9b4d0c0 2020-03-18 stsp free(id_str);
1664 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1665 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1666 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1667 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1668 7ebc0570 2020-03-18 stsp char *remote_refname;
1670 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1671 0e4002ca 2020-03-21 stsp !mirror_references) {
1672 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1673 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1674 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1675 0e4002ca 2020-03-21 stsp if (error)
1676 0e4002ca 2020-03-21 stsp goto done;
1680 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1681 39c64a6a 2020-03-18 stsp if (error)
1682 d9b4d0c0 2020-03-18 stsp goto done;
1684 469dd726 2020-03-20 stsp if (mirror_references)
1687 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1690 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1691 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1692 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1693 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1694 7ebc0570 2020-03-18 stsp goto done;
1696 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1697 f298ae0f 2020-03-25 stsp free(remote_refname);
1698 39c64a6a 2020-03-18 stsp if (error)
1699 d9b4d0c0 2020-03-18 stsp goto done;
1702 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1703 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1704 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1705 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1706 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1707 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1709 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1712 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1713 39c64a6a 2020-03-18 stsp if (error) {
1714 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1715 55330abe 2020-03-20 stsp error = NULL;
1718 d9b4d0c0 2020-03-18 stsp goto done;
1721 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1722 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1723 f298ae0f 2020-03-25 stsp if (error)
1724 f298ae0f 2020-03-25 stsp goto done;
1726 f298ae0f 2020-03-25 stsp if (mirror_references)
1729 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1732 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1733 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1734 f298ae0f 2020-03-25 stsp refname) == -1) {
1735 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1736 f298ae0f 2020-03-25 stsp goto done;
1738 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1739 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1740 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1741 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1742 f298ae0f 2020-03-25 stsp free(remote_refname);
1743 f298ae0f 2020-03-25 stsp goto done;
1745 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1746 f298ae0f 2020-03-25 stsp if (error) {
1747 f298ae0f 2020-03-25 stsp free(remote_refname);
1748 f298ae0f 2020-03-25 stsp free(remote_target);
1749 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1750 f298ae0f 2020-03-25 stsp error = NULL;
1753 f298ae0f 2020-03-25 stsp goto done;
1755 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1756 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1757 f298ae0f 2020-03-25 stsp free(remote_refname);
1758 f298ae0f 2020-03-25 stsp free(remote_target);
1759 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1760 39c64a6a 2020-03-18 stsp if (error)
1761 d9b4d0c0 2020-03-18 stsp goto done;
1763 4ba14133 2020-03-20 stsp if (pe == NULL) {
1765 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1766 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1767 4ba14133 2020-03-20 stsp * which could be fetched instead.
1769 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1770 4ba14133 2020-03-20 stsp const char *target = pe->path;
1771 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1773 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1774 4ba14133 2020-03-20 stsp if (error) {
1775 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1776 4ba14133 2020-03-20 stsp error = NULL;
1779 4ba14133 2020-03-20 stsp goto done;
1782 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1783 04d9a9ec 2020-09-24 stsp verbosity, repo);
1784 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1785 4ba14133 2020-03-20 stsp if (error)
1786 4ba14133 2020-03-20 stsp goto done;
1791 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1792 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1793 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1795 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1796 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1797 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1798 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1799 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1801 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1802 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1804 bb64b798 2020-03-18 stsp got_repo_close(repo);
1805 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1806 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1807 d9b4d0c0 2020-03-18 stsp free(pe->data);
1809 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1810 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1811 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1812 d9b4d0c0 2020-03-18 stsp free(pe->data);
1814 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1815 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1816 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1817 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1818 09838ffc 2020-03-18 stsp free(proto);
1819 09838ffc 2020-03-18 stsp free(host);
1820 09838ffc 2020-03-18 stsp free(port);
1821 09838ffc 2020-03-18 stsp free(server_path);
1822 09838ffc 2020-03-18 stsp free(repo_name);
1823 bb64b798 2020-03-18 stsp free(default_destdir);
1824 b46f3e71 2020-03-18 stsp free(git_url);
1825 39c64a6a 2020-03-18 stsp return error;
1828 7848a0e1 2020-03-19 stsp static const struct got_error *
1829 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1830 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1832 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1833 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1834 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1836 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1838 7848a0e1 2020-03-19 stsp goto done;
1840 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1841 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1842 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1844 88609724 2020-03-21 stsp goto done;
1845 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1846 88609724 2020-03-21 stsp goto done;
1847 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1848 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1849 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1851 db6d8ad8 2020-03-21 stsp goto done;
1854 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1855 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1856 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1857 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1858 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1860 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1862 7848a0e1 2020-03-19 stsp goto done;
1863 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1865 e8a967e0 2020-03-21 stsp goto done;
1867 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1869 7848a0e1 2020-03-19 stsp goto done;
1870 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1871 6338a6a1 2020-03-21 stsp goto done;
1873 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1875 6338a6a1 2020-03-21 stsp goto done;
1876 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1878 6338a6a1 2020-03-21 stsp goto done;
1881 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1882 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1883 6338a6a1 2020-03-21 stsp new_id_str);
1885 7848a0e1 2020-03-19 stsp free(old_id);
1886 7848a0e1 2020-03-19 stsp free(new_id_str);
1887 7848a0e1 2020-03-19 stsp return err;
1890 f1bcca34 2020-03-25 stsp static const struct got_error *
1891 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1892 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1894 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1895 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1896 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1898 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1899 bcf34b0e 2020-03-26 stsp if (err) {
1900 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1901 bcf34b0e 2020-03-26 stsp return err;
1902 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1904 bcf34b0e 2020-03-26 stsp goto done;
1906 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1908 bcf34b0e 2020-03-26 stsp goto done;
1910 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1911 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1912 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1913 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1915 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1917 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1918 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1919 bcf34b0e 2020-03-26 stsp goto done;
1921 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1922 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1924 bcf34b0e 2020-03-26 stsp goto done;
1926 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1928 bcf34b0e 2020-03-26 stsp goto done;
1930 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1931 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1932 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1936 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1937 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1938 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1939 bcf34b0e 2020-03-26 stsp err = unlock_err;
1941 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1942 f1bcca34 2020-03-25 stsp return err;
1945 2ab43947 2020-03-18 stsp __dead static void
1946 7848a0e1 2020-03-19 stsp usage_fetch(void)
1948 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1949 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1950 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1951 13f12b09 2020-03-21 stsp getprogname());
1955 7848a0e1 2020-03-19 stsp static const struct got_error *
1956 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1957 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1959 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1960 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1961 3789fd73 2020-03-26 stsp char *id_str = NULL;
1963 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1964 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1966 3789fd73 2020-03-26 stsp return err;
1967 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1968 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1969 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1970 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1973 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1975 3789fd73 2020-03-26 stsp return err;
1976 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1978 3789fd73 2020-03-26 stsp goto done;
1980 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1982 3789fd73 2020-03-26 stsp goto done;
1983 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1984 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1985 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1990 3789fd73 2020-03-26 stsp free(id_str);
1991 3789fd73 2020-03-26 stsp return NULL;
1994 3789fd73 2020-03-26 stsp static const struct got_error *
1995 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1996 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1997 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1998 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
2000 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
2001 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
2002 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
2003 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
2004 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
2005 3789fd73 2020-03-26 stsp char *local_refname = NULL;
2007 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&my_refs);
2009 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2011 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
2013 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2015 3789fd73 2020-03-26 stsp goto done;
2017 d9dff0e5 2020-12-26 stsp TAILQ_FOREACH(re, &my_refs, entry) {
2018 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
2020 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
2021 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
2022 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
2023 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
2024 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
2026 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
2027 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
2028 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
2029 3789fd73 2020-03-26 stsp goto done;
2031 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
2035 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
2036 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2039 f21ec2f0 2020-03-21 stsp if (pe != NULL)
2042 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
2043 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
2046 3789fd73 2020-03-26 stsp if (pe != NULL)
2049 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
2053 3789fd73 2020-03-26 stsp if (local_refname) {
2054 3789fd73 2020-03-26 stsp struct got_reference *ref;
2055 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
2056 3789fd73 2020-03-26 stsp if (err) {
2057 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
2059 3789fd73 2020-03-26 stsp free(local_refname);
2060 3789fd73 2020-03-26 stsp local_refname = NULL;
2063 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
2066 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
2067 3789fd73 2020-03-26 stsp got_ref_close(ref);
2068 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
2069 3789fd73 2020-03-26 stsp err = unlock_err;
2073 3789fd73 2020-03-26 stsp free(local_refname);
2074 3789fd73 2020-03-26 stsp local_refname = NULL;
2078 3789fd73 2020-03-26 stsp free(remote_namespace);
2079 3789fd73 2020-03-26 stsp free(local_refname);
2080 0e4002ca 2020-03-21 stsp return err;
2083 0e4002ca 2020-03-21 stsp static const struct got_error *
2084 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
2085 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
2087 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
2088 0e4002ca 2020-03-21 stsp char *remote_refname;
2089 0e4002ca 2020-03-21 stsp struct got_reference *ref;
2091 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
2092 0e4002ca 2020-03-21 stsp refname += 5;
2094 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2095 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
2096 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
2098 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
2099 0e4002ca 2020-03-21 stsp if (err) {
2100 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
2101 0e4002ca 2020-03-21 stsp goto done;
2102 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
2104 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
2105 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2106 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
2107 9f142382 2020-03-21 stsp err = unlock_err;
2108 0e4002ca 2020-03-21 stsp got_ref_close(ref);
2111 0e4002ca 2020-03-21 stsp free(remote_refname);
2112 f21ec2f0 2020-03-21 stsp return err;
2115 f21ec2f0 2020-03-21 stsp static const struct got_error *
2116 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
2118 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
2119 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
2120 7848a0e1 2020-03-19 stsp const char *remote_name;
2121 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
2122 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
2123 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
2124 7848a0e1 2020-03-19 stsp int nremotes;
2125 7848a0e1 2020-03-19 stsp char *id_str = NULL;
2126 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
2127 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
2128 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2129 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2130 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2131 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2132 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2133 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2134 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2135 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2136 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
2138 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2139 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2140 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2141 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2143 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2144 7848a0e1 2020-03-19 stsp switch (ch) {
2146 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2149 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2150 4ba14133 2020-03-20 stsp optarg, NULL);
2151 4ba14133 2020-03-20 stsp if (error)
2152 4ba14133 2020-03-20 stsp return error;
2155 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2158 41b0de12 2020-03-21 stsp list_refs_only = 1;
2161 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2162 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2163 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2165 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2168 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2171 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2172 7848a0e1 2020-03-19 stsp verbosity = 0;
2173 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2174 7848a0e1 2020-03-19 stsp verbosity++;
2177 7848a0e1 2020-03-19 stsp verbosity = -1;
2180 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2181 0e4002ca 2020-03-21 stsp optarg, NULL);
2182 0e4002ca 2020-03-21 stsp if (error)
2183 0e4002ca 2020-03-21 stsp return error;
2186 7848a0e1 2020-03-19 stsp usage_fetch();
2190 7848a0e1 2020-03-19 stsp argc -= optind;
2191 7848a0e1 2020-03-19 stsp argv += optind;
2193 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2194 ff69268e 2020-12-13 stsp option_conflict('a', 'b');
2195 41b0de12 2020-03-21 stsp if (list_refs_only) {
2196 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2197 ff69268e 2020-12-13 stsp option_conflict('l', 'b');
2198 ff69268e 2020-12-13 stsp if (fetch_all_branches)
2199 ff69268e 2020-12-13 stsp option_conflict('l', 'a');
2200 f21ec2f0 2020-03-21 stsp if (delete_refs)
2201 ff69268e 2020-12-13 stsp option_conflict('l', 'd');
2204 7848a0e1 2020-03-19 stsp if (argc == 0)
2205 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2206 7848a0e1 2020-03-19 stsp else if (argc == 1)
2207 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2209 7848a0e1 2020-03-19 stsp usage_fetch();
2211 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2212 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2213 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2214 7848a0e1 2020-03-19 stsp goto done;
2217 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2218 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2219 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2220 7848a0e1 2020-03-19 stsp goto done;
2222 7848a0e1 2020-03-19 stsp error = NULL;
2223 7848a0e1 2020-03-19 stsp if (worktree) {
2224 7848a0e1 2020-03-19 stsp repo_path =
2225 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2226 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2227 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2228 7848a0e1 2020-03-19 stsp if (error)
2229 7848a0e1 2020-03-19 stsp goto done;
2231 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2232 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2233 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2234 7848a0e1 2020-03-19 stsp goto done;
2239 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2240 7848a0e1 2020-03-19 stsp if (error)
2241 7848a0e1 2020-03-19 stsp goto done;
2243 50b0790e 2020-09-11 stsp if (worktree) {
2244 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2245 50b0790e 2020-09-11 stsp if (worktree_conf) {
2246 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2247 50b0790e 2020-09-11 stsp worktree_conf);
2248 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2249 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2250 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2256 50b0790e 2020-09-11 stsp if (remote == NULL) {
2257 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2258 50b0790e 2020-09-11 stsp if (repo_conf) {
2259 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2260 50b0790e 2020-09-11 stsp repo_conf);
2261 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2262 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2263 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2269 50b0790e 2020-09-11 stsp if (remote == NULL) {
2270 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2271 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2272 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2273 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2278 50b0790e 2020-09-11 stsp if (remote == NULL) {
2279 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2280 50b0790e 2020-09-11 stsp goto done;
2283 0c8b29c5 2021-01-05 stsp if (TAILQ_EMPTY(&wanted_branches)) {
2284 0c8b29c5 2021-01-05 stsp if (!fetch_all_branches)
2285 0c8b29c5 2021-01-05 stsp fetch_all_branches = remote->fetch_all_branches;
2286 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2287 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2288 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2291 99495ddb 2021-01-10 stsp if (TAILQ_EMPTY(&wanted_refs)) {
2292 99495ddb 2021-01-10 stsp for (i = 0; i < remote->nrefs; i++) {
2293 99495ddb 2021-01-10 stsp got_pathlist_append(&wanted_refs,
2294 99495ddb 2021-01-10 stsp remote->refs[i], NULL);
2298 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2299 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2300 7848a0e1 2020-03-19 stsp if (error)
2301 7848a0e1 2020-03-19 stsp goto done;
2303 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2304 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2305 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2306 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2307 7848a0e1 2020-03-19 stsp err(1, "pledge");
2309 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2310 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2311 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2312 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2313 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2314 7848a0e1 2020-03-19 stsp err(1, "pledge");
2316 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2317 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2318 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2319 7848a0e1 2020-03-19 stsp goto done;
2321 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2322 7848a0e1 2020-03-19 stsp goto done;
2325 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2326 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2327 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2328 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2329 7848a0e1 2020-03-19 stsp goto done;
2332 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2333 7848a0e1 2020-03-19 stsp if (error)
2334 7848a0e1 2020-03-19 stsp goto done;
2336 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2337 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2338 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2340 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2341 9c52365f 2020-03-21 stsp server_path, verbosity);
2342 7848a0e1 2020-03-19 stsp if (error)
2343 7848a0e1 2020-03-19 stsp goto done;
2345 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2346 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2347 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2348 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2349 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2350 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2351 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2352 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2353 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2354 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2355 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2356 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2357 7848a0e1 2020-03-19 stsp if (error)
2358 7848a0e1 2020-03-19 stsp goto done;
2360 41b0de12 2020-03-21 stsp if (list_refs_only) {
2361 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2362 41b0de12 2020-03-21 stsp goto done;
2365 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2366 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2367 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2368 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2369 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2370 984065c8 2020-03-19 stsp if (error)
2371 984065c8 2020-03-19 stsp goto done;
2372 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2373 984065c8 2020-03-19 stsp free(id_str);
2374 984065c8 2020-03-19 stsp id_str = NULL;
2377 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2378 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2379 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2380 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2381 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2382 7848a0e1 2020-03-19 stsp char *remote_refname;
2384 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2385 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2386 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2387 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2388 0e4002ca 2020-03-21 stsp if (error)
2389 0e4002ca 2020-03-21 stsp goto done;
2393 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2394 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2395 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2396 7848a0e1 2020-03-19 stsp if (error) {
2397 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2398 7848a0e1 2020-03-19 stsp goto done;
2399 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2401 7848a0e1 2020-03-19 stsp if (error)
2402 7848a0e1 2020-03-19 stsp goto done;
2404 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2405 db6d8ad8 2020-03-21 stsp verbosity, repo);
2406 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2407 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2408 9f142382 2020-03-21 stsp error = unlock_err;
2409 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2410 7848a0e1 2020-03-19 stsp if (error)
2411 7848a0e1 2020-03-19 stsp goto done;
2413 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2414 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2415 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2416 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2417 7848a0e1 2020-03-19 stsp goto done;
2420 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2421 7848a0e1 2020-03-19 stsp if (error) {
2422 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2423 7848a0e1 2020-03-19 stsp goto done;
2424 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2425 688f11b3 2020-03-21 stsp verbosity, repo);
2426 7848a0e1 2020-03-19 stsp if (error)
2427 7848a0e1 2020-03-19 stsp goto done;
2429 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2430 db6d8ad8 2020-03-21 stsp verbosity, repo);
2431 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2432 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2433 9f142382 2020-03-21 stsp error = unlock_err;
2434 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2435 7848a0e1 2020-03-19 stsp if (error)
2436 7848a0e1 2020-03-19 stsp goto done;
2439 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2440 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2441 2ec30c80 2020-03-20 stsp if (error) {
2442 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2443 2ec30c80 2020-03-20 stsp goto done;
2444 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2446 2ec30c80 2020-03-20 stsp if (error)
2447 2ec30c80 2020-03-20 stsp goto done;
2449 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2450 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2451 9f142382 2020-03-21 stsp error = unlock_err;
2452 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2456 f1bcca34 2020-03-25 stsp if (delete_refs) {
2457 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2458 3789fd73 2020-03-26 stsp verbosity, repo);
2459 f1bcca34 2020-03-25 stsp if (error)
2460 f1bcca34 2020-03-25 stsp goto done;
2463 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2464 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2465 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2466 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2467 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2468 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2469 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2471 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2474 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2477 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2478 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2479 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2480 f1bcca34 2020-03-25 stsp goto done;
2482 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2483 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2484 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2485 f1bcca34 2020-03-25 stsp free(remote_refname);
2486 f1bcca34 2020-03-25 stsp goto done;
2489 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2491 f1bcca34 2020-03-25 stsp if (error) {
2492 f1bcca34 2020-03-25 stsp free(remote_refname);
2493 f1bcca34 2020-03-25 stsp free(remote_target);
2494 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2495 f1bcca34 2020-03-25 stsp error = NULL;
2498 f1bcca34 2020-03-25 stsp goto done;
2500 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2501 f1bcca34 2020-03-25 stsp verbosity, repo);
2502 f1bcca34 2020-03-25 stsp free(remote_refname);
2503 f1bcca34 2020-03-25 stsp free(remote_target);
2504 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2505 f1bcca34 2020-03-25 stsp if (error)
2506 f1bcca34 2020-03-25 stsp goto done;
2510 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2511 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2512 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2513 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2514 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2516 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2517 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2519 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2520 7848a0e1 2020-03-19 stsp if (worktree)
2521 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2522 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2523 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2524 7848a0e1 2020-03-19 stsp free(pe->data);
2526 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2527 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2528 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2529 7848a0e1 2020-03-19 stsp free(pe->data);
2531 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2532 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2533 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2534 7848a0e1 2020-03-19 stsp free(id_str);
2535 7848a0e1 2020-03-19 stsp free(cwd);
2536 7848a0e1 2020-03-19 stsp free(repo_path);
2537 7848a0e1 2020-03-19 stsp free(pack_hash);
2538 7848a0e1 2020-03-19 stsp free(proto);
2539 7848a0e1 2020-03-19 stsp free(host);
2540 7848a0e1 2020-03-19 stsp free(port);
2541 7848a0e1 2020-03-19 stsp free(server_path);
2542 7848a0e1 2020-03-19 stsp free(repo_name);
2543 7848a0e1 2020-03-19 stsp return error;
2547 7848a0e1 2020-03-19 stsp __dead static void
2548 2ab43947 2020-03-18 stsp usage_checkout(void)
2550 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2551 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2555 2ab43947 2020-03-18 stsp static void
2556 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2558 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2559 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2560 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2561 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2562 2ab43947 2020-03-18 stsp getprogname());
2565 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2566 2ab43947 2020-03-18 stsp const char *worktree_path;
2567 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2570 2ab43947 2020-03-18 stsp static const struct got_error *
2571 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2573 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2575 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2576 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2577 2ab43947 2020-03-18 stsp return NULL;
2579 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2580 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2581 2ab43947 2020-03-18 stsp return NULL;
2584 2ab43947 2020-03-18 stsp while (path[0] == '/')
2587 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2588 2ab43947 2020-03-18 stsp return NULL;
2591 2ab43947 2020-03-18 stsp static const struct got_error *
2592 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2594 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2595 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2596 2ab43947 2020-03-18 stsp return NULL;
2599 2ab43947 2020-03-18 stsp static const struct got_error *
2600 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2601 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2602 2ab43947 2020-03-18 stsp struct got_repository *repo)
2604 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2605 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2607 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2608 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2610 2ab43947 2020-03-18 stsp return err;
2612 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2613 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2616 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2617 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2619 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2621 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2629 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2630 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2631 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2633 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2634 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2635 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2636 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2637 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2638 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2640 2ab43947 2020-03-18 stsp free(yca_id);
2641 2ab43947 2020-03-18 stsp return NULL;
2644 2ab43947 2020-03-18 stsp static const struct got_error *
2645 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2646 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2647 2ab43947 2020-03-18 stsp struct got_repository *repo)
2649 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2650 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2651 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2652 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2654 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2656 2ab43947 2020-03-18 stsp goto done;
2658 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2659 2ab43947 2020-03-18 stsp is_same_branch = 1;
2660 2ab43947 2020-03-18 stsp goto done;
2662 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2663 2ab43947 2020-03-18 stsp is_same_branch = 1;
2664 2ab43947 2020-03-18 stsp goto done;
2667 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2669 2ab43947 2020-03-18 stsp goto done;
2671 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2672 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2674 2ab43947 2020-03-18 stsp goto done;
2676 2ab43947 2020-03-18 stsp for (;;) {
2677 2ab43947 2020-03-18 stsp struct got_object_id *id;
2678 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2679 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2680 2ab43947 2020-03-18 stsp if (err) {
2681 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2682 2ab43947 2020-03-18 stsp err = NULL;
2687 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2689 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2690 2ab43947 2020-03-18 stsp is_same_branch = 1;
2696 2ab43947 2020-03-18 stsp if (graph)
2697 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2698 2ab43947 2020-03-18 stsp free(head_commit_id);
2699 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2700 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2701 2ab43947 2020-03-18 stsp return err;
2704 4ed7e80c 2018-05-20 stsp static const struct got_error *
2705 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2707 4b6c9460 2020-03-05 stsp static char msg[512];
2708 4b6c9460 2020-03-05 stsp const char *branch_name;
2710 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2711 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2713 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2715 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2716 4b6c9460 2020-03-05 stsp branch_name += 11;
2718 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2719 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2720 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2721 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2722 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2723 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2725 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2728 4b6c9460 2020-03-05 stsp static const struct got_error *
2729 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2731 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2732 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2733 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2734 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2735 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2736 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2737 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2738 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2739 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2740 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2741 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2742 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2743 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2745 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2747 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2748 0bb8a95e 2018-03-12 stsp switch (ch) {
2750 08573d5b 2019-05-14 stsp branch_name = optarg;
2753 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2754 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2755 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2758 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2761 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2764 2deda0b9 2019-03-07 stsp usage_checkout();
2765 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2769 0bb8a95e 2018-03-12 stsp argc -= optind;
2770 0bb8a95e 2018-03-12 stsp argv += optind;
2772 6715a751 2018-03-16 stsp #ifndef PROFILE
2773 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2774 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2775 c09a553d 2018-03-12 stsp err(1, "pledge");
2777 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2778 c47340da 2020-09-20 stsp char *base, *dotgit;
2779 f0207f6a 2020-10-19 stsp const char *path;
2780 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2781 76089277 2018-04-01 stsp if (repo_path == NULL)
2782 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2783 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2784 76089277 2018-04-01 stsp if (cwd == NULL) {
2785 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2786 76089277 2018-04-01 stsp goto done;
2788 c47340da 2020-09-20 stsp if (path_prefix[0])
2789 f0207f6a 2020-10-19 stsp path = path_prefix;
2791 f0207f6a 2020-10-19 stsp path = repo_path;
2792 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2793 f0207f6a 2020-10-19 stsp if (error)
2794 c47340da 2020-09-20 stsp goto done;
2795 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2796 c09a553d 2018-03-12 stsp if (dotgit)
2797 c09a553d 2018-03-12 stsp *dotgit = '\0';
2798 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2799 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2800 f0207f6a 2020-10-19 stsp free(base);
2801 76089277 2018-04-01 stsp goto done;
2803 f0207f6a 2020-10-19 stsp free(base);
2804 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2805 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2806 76089277 2018-04-01 stsp if (repo_path == NULL) {
2807 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2808 76089277 2018-04-01 stsp goto done;
2810 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2811 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2812 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2813 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2815 b4b3a7dd 2019-07-22 stsp goto done;
2817 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2818 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2819 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2820 b4b3a7dd 2019-07-22 stsp goto done;
2824 c09a553d 2018-03-12 stsp usage_checkout();
2826 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2827 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2829 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2830 c09a553d 2018-03-12 stsp if (error != NULL)
2831 c02c541e 2019-03-29 stsp goto done;
2833 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2834 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2835 c530dc23 2019-07-23 stsp if (error) {
2836 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2837 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2838 c530dc23 2019-07-23 stsp goto done;
2839 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2840 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2841 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2842 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2843 c530dc23 2019-07-23 stsp goto done;
2847 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2848 c02c541e 2019-03-29 stsp if (error)
2849 c09a553d 2018-03-12 stsp goto done;
2851 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2852 c09a553d 2018-03-12 stsp if (error != NULL)
2853 c09a553d 2018-03-12 stsp goto done;
2855 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2856 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2857 c09a553d 2018-03-12 stsp goto done;
2859 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2860 c09a553d 2018-03-12 stsp if (error != NULL)
2861 c09a553d 2018-03-12 stsp goto done;
2863 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2864 e5dc7198 2018-12-29 stsp path_prefix);
2865 e5dc7198 2018-12-29 stsp if (error != NULL)
2866 e5dc7198 2018-12-29 stsp goto done;
2867 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2868 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2869 49520a32 2018-12-29 stsp goto done;
2872 8069f636 2019-01-12 stsp if (commit_id_str) {
2873 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2874 84de9106 2020-12-26 stsp struct got_reflist_head refs;
2875 d9dff0e5 2020-12-26 stsp TAILQ_INIT(&refs);
2876 84de9106 2020-12-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
2878 84de9106 2020-12-26 stsp if (error)
2879 84de9106 2020-12-26 stsp goto done;
2880 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2881 84de9106 2020-12-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
2882 84de9106 2020-12-26 stsp got_ref_list_free(&refs);
2883 30837e32 2019-07-25 stsp if (error)
2884 8069f636 2019-01-12 stsp goto done;
2885 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2886 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2887 8069f636 2019-01-12 stsp if (error != NULL) {
2888 8069f636 2019-01-12 stsp free(commit_id);
2889 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2890 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2891 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2893 8069f636 2019-01-12 stsp goto done;
2895 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2896 4b6c9460 2020-03-05 stsp if (error) {
2897 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2898 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2899 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2901 45d344f6 2019-05-14 stsp goto done;
2903 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2904 8069f636 2019-01-12 stsp commit_id);
2905 8069f636 2019-01-12 stsp free(commit_id);
2906 8069f636 2019-01-12 stsp if (error)
2907 8069f636 2019-01-12 stsp goto done;
2910 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2911 f2ea84fa 2019-07-27 stsp if (error)
2912 f2ea84fa 2019-07-27 stsp goto done;
2913 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2914 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2915 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2916 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2917 c09a553d 2018-03-12 stsp if (error != NULL)
2918 c09a553d 2018-03-12 stsp goto done;
2920 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2921 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2922 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2924 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2925 8069f636 2019-01-12 stsp free(commit_id_str);
2926 76089277 2018-04-01 stsp free(repo_path);
2927 507dc3bb 2018-12-29 stsp free(worktree_path);
2928 c47340da 2020-09-20 stsp free(cwd);
2929 507dc3bb 2018-12-29 stsp return error;
2932 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2933 9627c110 2020-04-18 stsp int did_something;
2934 9627c110 2020-04-18 stsp int conflicts;
2935 9627c110 2020-04-18 stsp int obstructed;
2936 9627c110 2020-04-18 stsp int not_updated;
2940 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2942 9627c110 2020-04-18 stsp if (!upa->did_something)
2945 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2946 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2947 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2948 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2949 9627c110 2020-04-18 stsp upa->obstructed);
2950 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2951 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2952 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2955 507dc3bb 2018-12-29 stsp __dead static void
2956 507dc3bb 2018-12-29 stsp usage_update(void)
2958 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2959 507dc3bb 2018-12-29 stsp getprogname());
2963 1ee397ad 2019-07-12 stsp static const struct got_error *
2964 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2966 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2968 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2969 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2970 1ee397ad 2019-07-12 stsp return NULL;
2972 9627c110 2020-04-18 stsp upa->did_something = 1;
2974 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2975 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2976 1ee397ad 2019-07-12 stsp return NULL;
2978 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2979 9627c110 2020-04-18 stsp upa->conflicts++;
2980 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2981 9627c110 2020-04-18 stsp upa->obstructed++;
2982 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2983 9627c110 2020-04-18 stsp upa->not_updated++;
2985 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2987 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2988 1ee397ad 2019-07-12 stsp return NULL;
2991 be7061eb 2018-12-30 stsp static const struct got_error *
2992 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2993 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2994 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2996 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2997 a1fb16d8 2019-05-24 stsp char *base_id_str;
2998 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
3000 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
3001 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
3002 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
3003 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
3004 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
3005 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
3006 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
3009 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
3010 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3011 a1fb16d8 2019-05-24 stsp if (err) {
3012 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
3013 a1fb16d8 2019-05-24 stsp return err;
3014 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
3016 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
3017 a1fb16d8 2019-05-24 stsp return NULL;
3019 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
3020 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
3021 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
3023 a1fb16d8 2019-05-24 stsp return err;
3024 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
3025 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3026 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
3027 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
3028 0ebf8283 2019-07-24 stsp return NULL;
3031 0ebf8283 2019-07-24 stsp static const struct got_error *
3032 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3034 0ebf8283 2019-07-24 stsp const struct got_error *err;
3035 0ebf8283 2019-07-24 stsp int in_progress;
3037 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
3039 0ebf8283 2019-07-24 stsp return err;
3040 0ebf8283 2019-07-24 stsp if (in_progress)
3041 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
3043 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
3045 0ebf8283 2019-07-24 stsp return err;
3046 0ebf8283 2019-07-24 stsp if (in_progress)
3047 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
3049 a1fb16d8 2019-05-24 stsp return NULL;
3052 a5edda0a 2019-07-27 stsp static const struct got_error *
3053 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3054 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
3056 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
3057 a5edda0a 2019-07-27 stsp char *path;
3060 a5edda0a 2019-07-27 stsp if (argc == 0) {
3061 a5edda0a 2019-07-27 stsp path = strdup("");
3062 a5edda0a 2019-07-27 stsp if (path == NULL)
3063 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
3064 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
3067 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
3068 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
3071 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
3072 a5edda0a 2019-07-27 stsp if (err) {
3073 a5edda0a 2019-07-27 stsp free(path);