Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 5c860e29 2018-03-12 stsp *
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp *
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5c860e29 2018-03-12 stsp */
18 5c860e29 2018-03-12 stsp
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 f42b1b34 2018-03-12 stsp
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 d2cdc636 2020-03-18 stsp #include <util.h>
42 5c860e29 2018-03-12 stsp
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
58 5c860e29 2018-03-12 stsp
59 5c860e29 2018-03-12 stsp #ifndef nitems
60 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 5c860e29 2018-03-12 stsp #endif
62 99437157 2018-11-11 stsp
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
65 99437157 2018-11-11 stsp
66 99437157 2018-11-11 stsp static void
67 99437157 2018-11-11 stsp catch_sigint(int signo)
68 99437157 2018-11-11 stsp {
69 99437157 2018-11-11 stsp sigint_received = 1;
70 99437157 2018-11-11 stsp }
71 99437157 2018-11-11 stsp
72 99437157 2018-11-11 stsp static void
73 99437157 2018-11-11 stsp catch_sigpipe(int signo)
74 99437157 2018-11-11 stsp {
75 99437157 2018-11-11 stsp sigpipe_received = 1;
76 99437157 2018-11-11 stsp }
77 5c860e29 2018-03-12 stsp
78 99437157 2018-11-11 stsp
79 8cfb4057 2019-07-09 stsp struct got_cmd {
80 820059fa 2020-09-25 stsp const char *cmd_name;
81 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
83 97b3a7be 2019-07-09 stsp const char *cmd_alias;
84 5c860e29 2018-03-12 stsp };
85 5c860e29 2018-03-12 stsp
86 ce5b7c56 2019-07-09 stsp __dead static void usage(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);
114 5c860e29 2018-03-12 stsp
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 *[]);
142 5c860e29 2018-03-12 stsp
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, "" },
171 5c860e29 2018-03-12 stsp };
172 ce5b7c56 2019-07-09 stsp
173 ce5b7c56 2019-07-09 stsp static void
174 ce5b7c56 2019-07-09 stsp list_commands(void)
175 ce5b7c56 2019-07-09 stsp {
176 ce5b7c56 2019-07-09 stsp int i;
177 ce5b7c56 2019-07-09 stsp
178 ce5b7c56 2019-07-09 stsp fprintf(stderr, "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 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
182 ce5b7c56 2019-07-09 stsp }
183 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
184 ce5b7c56 2019-07-09 stsp }
185 5c860e29 2018-03-12 stsp
186 5c860e29 2018-03-12 stsp int
187 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
188 5c860e29 2018-03-12 stsp {
189 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
190 5c860e29 2018-03-12 stsp unsigned int i;
191 5c860e29 2018-03-12 stsp int ch;
192 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
193 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
194 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
195 820059fa 2020-09-25 stsp { NULL, 0, NULL, 0 }
196 83cd27f8 2020-01-13 stsp };
197 5c860e29 2018-03-12 stsp
198 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
199 5c860e29 2018-03-12 stsp
200 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
201 5c860e29 2018-03-12 stsp switch (ch) {
202 1b6b95a8 2018-03-12 stsp case 'h':
203 1b6b95a8 2018-03-12 stsp hflag = 1;
204 53ccebc2 2019-07-30 stsp break;
205 53ccebc2 2019-07-30 stsp case 'V':
206 53ccebc2 2019-07-30 stsp Vflag = 1;
207 1b6b95a8 2018-03-12 stsp break;
208 5c860e29 2018-03-12 stsp default:
209 ce5b7c56 2019-07-09 stsp usage(hflag);
210 5c860e29 2018-03-12 stsp /* NOTREACHED */
211 5c860e29 2018-03-12 stsp }
212 5c860e29 2018-03-12 stsp }
213 5c860e29 2018-03-12 stsp
214 5c860e29 2018-03-12 stsp argc -= optind;
215 5c860e29 2018-03-12 stsp argv += optind;
216 9814e6a3 2020-09-27 naddy optind = 1;
217 9814e6a3 2020-09-27 naddy optreset = 1;
218 53ccebc2 2019-07-30 stsp
219 53ccebc2 2019-07-30 stsp if (Vflag) {
220 53ccebc2 2019-07-30 stsp got_version_print_str();
221 53ccebc2 2019-07-30 stsp return 1;
222 53ccebc2 2019-07-30 stsp }
223 5c860e29 2018-03-12 stsp
224 5c860e29 2018-03-12 stsp if (argc <= 0)
225 ce5b7c56 2019-07-09 stsp usage(hflag);
226 5c860e29 2018-03-12 stsp
227 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
228 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
229 99437157 2018-11-11 stsp
230 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
231 d7d4f210 2018-03-12 stsp const struct got_error *error;
232 d7d4f210 2018-03-12 stsp
233 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
234 5c860e29 2018-03-12 stsp
235 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
236 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
237 5c860e29 2018-03-12 stsp continue;
238 5c860e29 2018-03-12 stsp
239 1b6b95a8 2018-03-12 stsp if (hflag)
240 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
241 1b6b95a8 2018-03-12 stsp
242 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
243 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
244 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
245 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
246 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
247 70015d7a 2019-11-08 stsp !(sigint_received &&
248 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
249 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
250 d7d4f210 2018-03-12 stsp return 1;
251 d7d4f210 2018-03-12 stsp }
252 d7d4f210 2018-03-12 stsp
253 d7d4f210 2018-03-12 stsp return 0;
254 5c860e29 2018-03-12 stsp }
255 5c860e29 2018-03-12 stsp
256 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
257 ce5b7c56 2019-07-09 stsp list_commands();
258 5c860e29 2018-03-12 stsp return 1;
259 5c860e29 2018-03-12 stsp }
260 5c860e29 2018-03-12 stsp
261 4ed7e80c 2018-05-20 stsp __dead static void
262 ce5b7c56 2019-07-09 stsp usage(int hflag)
263 5c860e29 2018-03-12 stsp {
264 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
265 53ccebc2 2019-07-30 stsp getprogname());
266 ce5b7c56 2019-07-09 stsp if (hflag)
267 ce5b7c56 2019-07-09 stsp list_commands();
268 5c860e29 2018-03-12 stsp exit(1);
269 5c860e29 2018-03-12 stsp }
270 5c860e29 2018-03-12 stsp
271 0266afb7 2019-01-04 stsp static const struct got_error *
272 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
273 e2ba3d07 2019-05-13 stsp {
274 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
275 e2ba3d07 2019-05-13 stsp const char *editor;
276 8920fa04 2019-08-18 stsp
277 8920fa04 2019-08-18 stsp *abspath = NULL;
278 e2ba3d07 2019-05-13 stsp
279 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
280 e2ba3d07 2019-05-13 stsp if (editor == NULL)
281 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
282 e2ba3d07 2019-05-13 stsp
283 0ee7065d 2019-05-13 stsp if (editor) {
284 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
285 0ee7065d 2019-05-13 stsp if (err)
286 0ee7065d 2019-05-13 stsp return err;
287 0ee7065d 2019-05-13 stsp }
288 e2ba3d07 2019-05-13 stsp
289 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
290 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
291 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
292 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
293 0ee7065d 2019-05-13 stsp }
294 0ee7065d 2019-05-13 stsp
295 e2ba3d07 2019-05-13 stsp return NULL;
296 e2ba3d07 2019-05-13 stsp }
297 e2ba3d07 2019-05-13 stsp
298 e2ba3d07 2019-05-13 stsp static const struct got_error *
299 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
300 c530dc23 2019-07-23 stsp const char *worktree_path)
301 0266afb7 2019-01-04 stsp {
302 163ce85a 2019-05-13 stsp const struct got_error *err;
303 0266afb7 2019-01-04 stsp
304 37c06ea4 2019-07-15 stsp #ifdef PROFILE
305 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
306 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
307 37c06ea4 2019-07-15 stsp #endif
308 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
309 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
310 0266afb7 2019-01-04 stsp
311 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
312 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
313 0266afb7 2019-01-04 stsp
314 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
315 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
316 0266afb7 2019-01-04 stsp
317 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
318 163ce85a 2019-05-13 stsp if (err != NULL)
319 163ce85a 2019-05-13 stsp return err;
320 0266afb7 2019-01-04 stsp
321 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
322 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
323 0266afb7 2019-01-04 stsp
324 0266afb7 2019-01-04 stsp return NULL;
325 2c7829a4 2019-06-17 stsp }
326 2c7829a4 2019-06-17 stsp
327 2c7829a4 2019-06-17 stsp __dead static void
328 2c7829a4 2019-06-17 stsp usage_init(void)
329 2c7829a4 2019-06-17 stsp {
330 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
331 2c7829a4 2019-06-17 stsp exit(1);
332 2c7829a4 2019-06-17 stsp }
333 2c7829a4 2019-06-17 stsp
334 2c7829a4 2019-06-17 stsp static const struct got_error *
335 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
336 2c7829a4 2019-06-17 stsp {
337 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
338 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
339 2c7829a4 2019-06-17 stsp int ch;
340 2c7829a4 2019-06-17 stsp
341 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
342 2c7829a4 2019-06-17 stsp switch (ch) {
343 2c7829a4 2019-06-17 stsp default:
344 2c7829a4 2019-06-17 stsp usage_init();
345 2c7829a4 2019-06-17 stsp /* NOTREACHED */
346 2c7829a4 2019-06-17 stsp }
347 2c7829a4 2019-06-17 stsp }
348 2c7829a4 2019-06-17 stsp
349 2c7829a4 2019-06-17 stsp argc -= optind;
350 2c7829a4 2019-06-17 stsp argv += optind;
351 2c7829a4 2019-06-17 stsp
352 2c7829a4 2019-06-17 stsp #ifndef PROFILE
353 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
354 2c7829a4 2019-06-17 stsp err(1, "pledge");
355 2c7829a4 2019-06-17 stsp #endif
356 2c7829a4 2019-06-17 stsp if (argc != 1)
357 bc20e173 2019-06-17 stsp usage_init();
358 2c7829a4 2019-06-17 stsp
359 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
360 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
361 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
362 2c7829a4 2019-06-17 stsp
363 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
364 2c7829a4 2019-06-17 stsp
365 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
366 2c7829a4 2019-06-17 stsp if (error &&
367 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
368 2c7829a4 2019-06-17 stsp goto done;
369 2c7829a4 2019-06-17 stsp
370 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
371 2c7829a4 2019-06-17 stsp if (error)
372 2c7829a4 2019-06-17 stsp goto done;
373 2c7829a4 2019-06-17 stsp
374 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
375 3ce1b845 2019-07-15 stsp done:
376 3ce1b845 2019-07-15 stsp free(repo_path);
377 3ce1b845 2019-07-15 stsp return error;
378 3ce1b845 2019-07-15 stsp }
379 3ce1b845 2019-07-15 stsp
380 3ce1b845 2019-07-15 stsp __dead static void
381 3ce1b845 2019-07-15 stsp usage_import(void)
382 3ce1b845 2019-07-15 stsp {
383 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
384 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
385 3ce1b845 2019-07-15 stsp exit(1);
386 3ce1b845 2019-07-15 stsp }
387 3ce1b845 2019-07-15 stsp
388 3ce1b845 2019-07-15 stsp int
389 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
390 3ce1b845 2019-07-15 stsp {
391 3ce1b845 2019-07-15 stsp pid_t pid;
392 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
393 3ce1b845 2019-07-15 stsp int st = -1;
394 3ce1b845 2019-07-15 stsp
395 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
396 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
397 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
398 3ce1b845 2019-07-15 stsp
399 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
400 3ce1b845 2019-07-15 stsp case -1:
401 3ce1b845 2019-07-15 stsp goto doneediting;
402 3ce1b845 2019-07-15 stsp case 0:
403 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
404 3ce1b845 2019-07-15 stsp _exit(127);
405 3ce1b845 2019-07-15 stsp }
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
408 3ce1b845 2019-07-15 stsp if (errno != EINTR)
409 3ce1b845 2019-07-15 stsp break;
410 3ce1b845 2019-07-15 stsp
411 3ce1b845 2019-07-15 stsp doneediting:
412 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
413 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
414 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
415 3ce1b845 2019-07-15 stsp
416 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
417 3ce1b845 2019-07-15 stsp errno = EINTR;
418 3ce1b845 2019-07-15 stsp return -1;
419 3ce1b845 2019-07-15 stsp }
420 3ce1b845 2019-07-15 stsp
421 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
422 3ce1b845 2019-07-15 stsp }
423 3ce1b845 2019-07-15 stsp
424 3ce1b845 2019-07-15 stsp static const struct got_error *
425 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
426 bfa12d5e 2020-09-26 stsp const char *initial_content, size_t initial_content_len)
427 3ce1b845 2019-07-15 stsp {
428 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
429 bfa12d5e 2020-09-26 stsp char *line = NULL;
430 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
431 bfa12d5e 2020-09-26 stsp ssize_t linelen;
432 3ce1b845 2019-07-15 stsp struct stat st, st2;
433 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
434 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
435 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
436 3ce1b845 2019-07-15 stsp
437 3ce1b845 2019-07-15 stsp *logmsg = NULL;
438 3ce1b845 2019-07-15 stsp
439 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
440 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
441 3ce1b845 2019-07-15 stsp
442 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
446 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
447 3ce1b845 2019-07-15 stsp
448 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
449 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
450 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
451 3ce1b845 2019-07-15 stsp
452 bfa12d5e 2020-09-26 stsp /*
453 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
454 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
455 bfa12d5e 2020-09-26 stsp * has in fact been edited.
456 bfa12d5e 2020-09-26 stsp */
457 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
458 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
459 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
460 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
461 bfa12d5e 2020-09-26 stsp
462 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
463 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
464 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
465 bfa12d5e 2020-09-26 stsp goto done;
466 bfa12d5e 2020-09-26 stsp }
467 bfa12d5e 2020-09-26 stsp s = buf;
468 bfa12d5e 2020-09-26 stsp len = 0;
469 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
470 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
471 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
472 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
473 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
474 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
475 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
476 bfa12d5e 2020-09-26 stsp goto done;
477 bfa12d5e 2020-09-26 stsp }
478 bfa12d5e 2020-09-26 stsp }
479 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
480 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
481 bfa12d5e 2020-09-26 stsp len--;
482 bfa12d5e 2020-09-26 stsp }
483 bfa12d5e 2020-09-26 stsp
484 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
485 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
486 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
487 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
488 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
489 3ce1b845 2019-07-15 stsp
490 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
491 3ce1b845 2019-07-15 stsp if (fp == NULL) {
492 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
493 3ce1b845 2019-07-15 stsp goto done;
494 3ce1b845 2019-07-15 stsp }
495 bfa12d5e 2020-09-26 stsp
496 bfa12d5e 2020-09-26 stsp len = 0;
497 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
498 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
499 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
500 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
501 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
502 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
503 bfa12d5e 2020-09-26 stsp goto done;
504 bfa12d5e 2020-09-26 stsp }
505 3ce1b845 2019-07-15 stsp }
506 bfa12d5e 2020-09-26 stsp free(line);
507 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
508 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
509 bfa12d5e 2020-09-26 stsp goto done;
510 bfa12d5e 2020-09-26 stsp }
511 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
512 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
513 3ce1b845 2019-07-15 stsp len--;
514 3ce1b845 2019-07-15 stsp }
515 3ce1b845 2019-07-15 stsp
516 bfa12d5e 2020-09-26 stsp if (len == 0) {
517 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
518 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
519 bfa12d5e 2020-09-26 stsp goto done;
520 bfa12d5e 2020-09-26 stsp }
521 bfa12d5e 2020-09-26 stsp if (strcmp(*logmsg, initial_content_stripped) == 0)
522 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
523 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
524 3ce1b845 2019-07-15 stsp done:
525 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
526 bfa12d5e 2020-09-26 stsp free(buf);
527 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
528 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
529 3ce1b845 2019-07-15 stsp if (err) {
530 3ce1b845 2019-07-15 stsp free(*logmsg);
531 3ce1b845 2019-07-15 stsp *logmsg = NULL;
532 3ce1b845 2019-07-15 stsp }
533 3ce1b845 2019-07-15 stsp return err;
534 3ce1b845 2019-07-15 stsp }
535 3ce1b845 2019-07-15 stsp
536 3ce1b845 2019-07-15 stsp static const struct got_error *
537 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
538 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
539 3ce1b845 2019-07-15 stsp {
540 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
541 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
542 1601cb9f 2020-09-11 naddy int initial_content_len;
543 97972933 2020-09-11 stsp int fd = -1;
544 3ce1b845 2019-07-15 stsp
545 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
546 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
547 1601cb9f 2020-09-11 naddy branch_name);
548 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
549 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
550 3ce1b845 2019-07-15 stsp
551 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
552 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
553 3ce1b845 2019-07-15 stsp if (err)
554 3ce1b845 2019-07-15 stsp goto done;
555 3ce1b845 2019-07-15 stsp
556 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
557 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
558 97972933 2020-09-11 stsp goto done;
559 97972933 2020-09-11 stsp }
560 3ce1b845 2019-07-15 stsp
561 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
562 bfa12d5e 2020-09-26 stsp initial_content_len);
563 3ce1b845 2019-07-15 stsp done:
564 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
565 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
566 3ce1b845 2019-07-15 stsp free(initial_content);
567 59f86c76 2020-09-11 stsp if (err) {
568 59f86c76 2020-09-11 stsp free(*logmsg_path);
569 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
570 59f86c76 2020-09-11 stsp }
571 3ce1b845 2019-07-15 stsp return err;
572 3ce1b845 2019-07-15 stsp }
573 3ce1b845 2019-07-15 stsp
574 3ce1b845 2019-07-15 stsp static const struct got_error *
575 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
576 3ce1b845 2019-07-15 stsp {
577 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
578 3ce1b845 2019-07-15 stsp return NULL;
579 3ce1b845 2019-07-15 stsp }
580 3ce1b845 2019-07-15 stsp
581 3ce1b845 2019-07-15 stsp static const struct got_error *
582 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
583 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
584 84792843 2019-08-09 stsp {
585 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
586 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
587 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
588 84792843 2019-08-09 stsp
589 84792843 2019-08-09 stsp *author = NULL;
590 aba9c984 2019-09-08 stsp
591 50b0790e 2020-09-11 stsp if (worktree)
592 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
593 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
594 50b0790e 2020-09-11 stsp
595 50b0790e 2020-09-11 stsp /*
596 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
597 50b0790e 2020-09-11 stsp * significant to least significant:
598 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
599 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
600 50b0790e 2020-09-11 stsp * 3) repository's git config file
601 50b0790e 2020-09-11 stsp * 4) environment variables
602 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
603 50b0790e 2020-09-11 stsp */
604 50b0790e 2020-09-11 stsp
605 50b0790e 2020-09-11 stsp if (worktree_conf)
606 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
607 50b0790e 2020-09-11 stsp if (got_author == NULL)
608 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
609 84792843 2019-08-09 stsp if (got_author == NULL) {
610 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
611 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
612 c9956ddf 2019-09-08 stsp if (name && email) {
613 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
614 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
615 c9956ddf 2019-09-08 stsp return NULL;
616 c9956ddf 2019-09-08 stsp }
617 257add31 2020-09-09 stsp
618 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
619 257add31 2020-09-09 stsp if (got_author == NULL) {
620 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
621 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
622 257add31 2020-09-09 stsp repo);
623 257add31 2020-09-09 stsp if (name && email) {
624 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
625 257add31 2020-09-09 stsp == -1)
626 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
627 257add31 2020-09-09 stsp return NULL;
628 257add31 2020-09-09 stsp }
629 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
630 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
631 257add31 2020-09-09 stsp }
632 84792843 2019-08-09 stsp }
633 84792843 2019-08-09 stsp
634 aba9c984 2019-09-08 stsp *author = strdup(got_author);
635 aba9c984 2019-09-08 stsp if (*author == NULL)
636 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
637 84792843 2019-08-09 stsp
638 84792843 2019-08-09 stsp /*
639 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
640 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
641 84792843 2019-08-09 stsp */
642 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
643 84792843 2019-08-09 stsp got_author++;
644 aba9c984 2019-09-08 stsp if (*got_author != '<') {
645 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
646 aba9c984 2019-09-08 stsp goto done;
647 aba9c984 2019-09-08 stsp }
648 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
649 84792843 2019-08-09 stsp got_author++;
650 aba9c984 2019-09-08 stsp if (*got_author != '@') {
651 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
652 aba9c984 2019-09-08 stsp goto done;
653 aba9c984 2019-09-08 stsp }
654 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
655 84792843 2019-08-09 stsp got_author++;
656 84792843 2019-08-09 stsp if (*got_author != '>')
657 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
658 aba9c984 2019-09-08 stsp done:
659 aba9c984 2019-09-08 stsp if (err) {
660 aba9c984 2019-09-08 stsp free(*author);
661 aba9c984 2019-09-08 stsp *author = NULL;
662 aba9c984 2019-09-08 stsp }
663 aba9c984 2019-09-08 stsp return err;
664 c9956ddf 2019-09-08 stsp }
665 c9956ddf 2019-09-08 stsp
666 c9956ddf 2019-09-08 stsp static const struct got_error *
667 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
668 c9956ddf 2019-09-08 stsp {
669 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
670 c9956ddf 2019-09-08 stsp
671 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
672 c9956ddf 2019-09-08 stsp if (homedir) {
673 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
674 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
675 c9956ddf 2019-09-08 stsp
676 c9956ddf 2019-09-08 stsp }
677 c9956ddf 2019-09-08 stsp return NULL;
678 84792843 2019-08-09 stsp }
679 84792843 2019-08-09 stsp
680 84792843 2019-08-09 stsp static const struct got_error *
681 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
682 3ce1b845 2019-07-15 stsp {
683 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
684 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
685 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
686 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
687 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
688 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
689 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
690 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
691 3ce1b845 2019-07-15 stsp int ch;
692 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
693 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
694 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
695 3ce1b845 2019-07-15 stsp
696 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
697 3ce1b845 2019-07-15 stsp
698 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
699 3ce1b845 2019-07-15 stsp switch (ch) {
700 3ce1b845 2019-07-15 stsp case 'b':
701 3ce1b845 2019-07-15 stsp branch_name = optarg;
702 3ce1b845 2019-07-15 stsp break;
703 3ce1b845 2019-07-15 stsp case 'm':
704 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
705 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
706 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
707 3ce1b845 2019-07-15 stsp goto done;
708 3ce1b845 2019-07-15 stsp }
709 3ce1b845 2019-07-15 stsp break;
710 3ce1b845 2019-07-15 stsp case 'r':
711 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
712 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
713 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
714 9ba1d308 2019-10-21 stsp optarg);
715 3ce1b845 2019-07-15 stsp goto done;
716 3ce1b845 2019-07-15 stsp }
717 3ce1b845 2019-07-15 stsp break;
718 3ce1b845 2019-07-15 stsp case 'I':
719 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
720 3ce1b845 2019-07-15 stsp break;
721 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
722 3ce1b845 2019-07-15 stsp NULL);
723 3ce1b845 2019-07-15 stsp if (error)
724 3ce1b845 2019-07-15 stsp goto done;
725 3ce1b845 2019-07-15 stsp break;
726 3ce1b845 2019-07-15 stsp default:
727 b2b65d18 2019-08-22 stsp usage_import();
728 3ce1b845 2019-07-15 stsp /* NOTREACHED */
729 3ce1b845 2019-07-15 stsp }
730 3ce1b845 2019-07-15 stsp }
731 3ce1b845 2019-07-15 stsp
732 3ce1b845 2019-07-15 stsp argc -= optind;
733 3ce1b845 2019-07-15 stsp argv += optind;
734 3ce1b845 2019-07-15 stsp
735 3ce1b845 2019-07-15 stsp #ifndef PROFILE
736 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
737 aba9c984 2019-09-08 stsp "unveil",
738 3ce1b845 2019-07-15 stsp NULL) == -1)
739 3ce1b845 2019-07-15 stsp err(1, "pledge");
740 3ce1b845 2019-07-15 stsp #endif
741 3ce1b845 2019-07-15 stsp if (argc != 1)
742 3ce1b845 2019-07-15 stsp usage_import();
743 2c7829a4 2019-06-17 stsp
744 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
745 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
746 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
747 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
748 3ce1b845 2019-07-15 stsp }
749 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
750 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
751 c9956ddf 2019-09-08 stsp if (error)
752 c9956ddf 2019-09-08 stsp goto done;
753 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
754 3ce1b845 2019-07-15 stsp if (error)
755 3ce1b845 2019-07-15 stsp goto done;
756 aba9c984 2019-09-08 stsp
757 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
758 aba9c984 2019-09-08 stsp if (error)
759 aba9c984 2019-09-08 stsp return error;
760 e560b7e0 2019-11-28 stsp
761 e560b7e0 2019-11-28 stsp /*
762 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
763 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
764 e560b7e0 2019-11-28 stsp * an unintended typo.
765 e560b7e0 2019-11-28 stsp */
766 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
767 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
768 3ce1b845 2019-07-15 stsp
769 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
770 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
771 3ce1b845 2019-07-15 stsp goto done;
772 3ce1b845 2019-07-15 stsp }
773 3ce1b845 2019-07-15 stsp
774 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
775 3ce1b845 2019-07-15 stsp if (error) {
776 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
777 3ce1b845 2019-07-15 stsp goto done;
778 3ce1b845 2019-07-15 stsp } else {
779 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
780 3ce1b845 2019-07-15 stsp "import target branch already exists");
781 3ce1b845 2019-07-15 stsp goto done;
782 3ce1b845 2019-07-15 stsp }
783 3ce1b845 2019-07-15 stsp
784 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
785 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
786 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
787 3ce1b845 2019-07-15 stsp goto done;
788 3ce1b845 2019-07-15 stsp }
789 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
790 3ce1b845 2019-07-15 stsp
791 3ce1b845 2019-07-15 stsp /*
792 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
793 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
794 3ce1b845 2019-07-15 stsp */
795 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
796 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
797 3ce1b845 2019-07-15 stsp if (error)
798 3ce1b845 2019-07-15 stsp goto done;
799 8e158b01 2019-09-22 stsp free(logmsg);
800 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
801 ef293bdd 2019-10-21 stsp path_dir, refname);
802 ef293bdd 2019-10-21 stsp if (error) {
803 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
804 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
805 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
806 3ce1b845 2019-07-15 stsp goto done;
807 ef293bdd 2019-10-21 stsp }
808 3ce1b845 2019-07-15 stsp }
809 3ce1b845 2019-07-15 stsp
810 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
811 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
812 ef293bdd 2019-10-21 stsp if (logmsg_path)
813 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
814 3ce1b845 2019-07-15 stsp goto done;
815 ef293bdd 2019-10-21 stsp }
816 3ce1b845 2019-07-15 stsp
817 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
818 ef293bdd 2019-10-21 stsp if (error) {
819 ef293bdd 2019-10-21 stsp if (logmsg_path)
820 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
821 ef293bdd 2019-10-21 stsp goto done;
822 ef293bdd 2019-10-21 stsp }
823 ef293bdd 2019-10-21 stsp
824 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
825 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
826 ef293bdd 2019-10-21 stsp if (error) {
827 ef293bdd 2019-10-21 stsp if (logmsg_path)
828 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
829 3ce1b845 2019-07-15 stsp goto done;
830 ef293bdd 2019-10-21 stsp }
831 3ce1b845 2019-07-15 stsp
832 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
833 ef293bdd 2019-10-21 stsp if (error) {
834 ef293bdd 2019-10-21 stsp if (logmsg_path)
835 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
836 3ce1b845 2019-07-15 stsp goto done;
837 ef293bdd 2019-10-21 stsp }
838 3ce1b845 2019-07-15 stsp
839 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
840 ef293bdd 2019-10-21 stsp if (error) {
841 ef293bdd 2019-10-21 stsp if (logmsg_path)
842 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
843 3ce1b845 2019-07-15 stsp goto done;
844 ef293bdd 2019-10-21 stsp }
845 3ce1b845 2019-07-15 stsp
846 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
847 ef293bdd 2019-10-21 stsp if (error) {
848 ef293bdd 2019-10-21 stsp if (logmsg_path)
849 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
850 3ce1b845 2019-07-15 stsp goto done;
851 ef293bdd 2019-10-21 stsp }
852 3ce1b845 2019-07-15 stsp
853 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
854 3ce1b845 2019-07-15 stsp if (error) {
855 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
856 ef293bdd 2019-10-21 stsp if (logmsg_path)
857 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
858 3ce1b845 2019-07-15 stsp goto done;
859 ef293bdd 2019-10-21 stsp }
860 3ce1b845 2019-07-15 stsp
861 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
862 3ce1b845 2019-07-15 stsp branch_ref);
863 ef293bdd 2019-10-21 stsp if (error) {
864 ef293bdd 2019-10-21 stsp if (logmsg_path)
865 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
866 3ce1b845 2019-07-15 stsp goto done;
867 ef293bdd 2019-10-21 stsp }
868 3ce1b845 2019-07-15 stsp
869 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
870 ef293bdd 2019-10-21 stsp if (error) {
871 ef293bdd 2019-10-21 stsp if (logmsg_path)
872 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
873 3ce1b845 2019-07-15 stsp goto done;
874 ef293bdd 2019-10-21 stsp }
875 3ce1b845 2019-07-15 stsp }
876 3ce1b845 2019-07-15 stsp
877 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
878 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
879 2c7829a4 2019-06-17 stsp done:
880 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
881 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
882 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
883 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
884 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
885 8e158b01 2019-09-22 stsp free(logmsg);
886 ef293bdd 2019-10-21 stsp free(logmsg_path);
887 2c7829a4 2019-06-17 stsp free(repo_path);
888 3ce1b845 2019-07-15 stsp free(editor);
889 3ce1b845 2019-07-15 stsp free(refname);
890 3ce1b845 2019-07-15 stsp free(new_commit_id);
891 3ce1b845 2019-07-15 stsp free(id_str);
892 aba9c984 2019-09-08 stsp free(author);
893 c9956ddf 2019-09-08 stsp free(gitconfig_path);
894 3ce1b845 2019-07-15 stsp if (branch_ref)
895 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
896 3ce1b845 2019-07-15 stsp if (head_ref)
897 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
898 2c7829a4 2019-06-17 stsp return error;
899 93658fb9 2020-03-18 stsp }
900 93658fb9 2020-03-18 stsp
901 93658fb9 2020-03-18 stsp __dead static void
902 93658fb9 2020-03-18 stsp usage_clone(void)
903 93658fb9 2020-03-18 stsp {
904 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
905 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
906 93658fb9 2020-03-18 stsp exit(1);
907 93658fb9 2020-03-18 stsp }
908 892ac3b6 2020-03-18 stsp
909 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
910 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
911 892ac3b6 2020-03-18 stsp int last_p_indexed;
912 892ac3b6 2020-03-18 stsp int last_p_resolved;
913 68999b92 2020-03-18 stsp int verbosity;
914 04d9a9ec 2020-09-24 stsp
915 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
916 04d9a9ec 2020-09-24 stsp
917 04d9a9ec 2020-09-24 stsp int create_configs;
918 04d9a9ec 2020-09-24 stsp int configs_created;
919 04d9a9ec 2020-09-24 stsp struct {
920 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
921 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
922 04d9a9ec 2020-09-24 stsp const char *proto;
923 04d9a9ec 2020-09-24 stsp const char *host;
924 04d9a9ec 2020-09-24 stsp const char *port;
925 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
926 04d9a9ec 2020-09-24 stsp const char *git_url;
927 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
928 04d9a9ec 2020-09-24 stsp int mirror_references;
929 04d9a9ec 2020-09-24 stsp } config_info;
930 892ac3b6 2020-03-18 stsp };
931 93658fb9 2020-03-18 stsp
932 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
933 93658fb9 2020-03-18 stsp static const struct got_error *
934 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
935 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
936 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
937 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo);
938 04d9a9ec 2020-09-24 stsp
939 04d9a9ec 2020-09-24 stsp static const struct got_error *
940 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
941 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
942 531c3985 2020-03-18 stsp {
943 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
944 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
945 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
946 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
947 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
948 04d9a9ec 2020-09-24 stsp
949 04d9a9ec 2020-09-24 stsp /*
950 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
951 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
952 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
953 04d9a9ec 2020-09-24 stsp * we have all required information.
954 04d9a9ec 2020-09-24 stsp */
955 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
956 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
957 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
958 04d9a9ec 2020-09-24 stsp a->config_info.host, a->config_info.port,
959 04d9a9ec 2020-09-24 stsp a->config_info.remote_repo_path,
960 04d9a9ec 2020-09-24 stsp a->config_info.git_url,
961 04d9a9ec 2020-09-24 stsp a->config_info.fetch_all_branches,
962 04d9a9ec 2020-09-24 stsp a->config_info.mirror_references,
963 04d9a9ec 2020-09-24 stsp a->config_info.symrefs,
964 04d9a9ec 2020-09-24 stsp a->config_info.wanted_branches, a->repo);
965 04d9a9ec 2020-09-24 stsp if (err)
966 04d9a9ec 2020-09-24 stsp return err;
967 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
968 04d9a9ec 2020-09-24 stsp }
969 b2409d58 2020-03-18 stsp
970 68999b92 2020-03-18 stsp if (a->verbosity < 0)
971 68999b92 2020-03-18 stsp return NULL;
972 68999b92 2020-03-18 stsp
973 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
974 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
975 892ac3b6 2020-03-18 stsp fflush(stdout);
976 12d1281e 2020-03-19 stsp return NULL;
977 b2409d58 2020-03-18 stsp }
978 b2409d58 2020-03-18 stsp
979 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
980 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
981 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
982 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
983 892ac3b6 2020-03-18 stsp print_size = 1;
984 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
985 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
986 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
987 892ac3b6 2020-03-18 stsp }
988 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
989 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
990 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
991 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
992 892ac3b6 2020-03-18 stsp print_indexed = 1;
993 892ac3b6 2020-03-18 stsp print_size = 1;
994 892ac3b6 2020-03-18 stsp }
995 61cc1a7a 2020-03-18 stsp }
996 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
997 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
998 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
999 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1000 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1001 892ac3b6 2020-03-18 stsp print_resolved = 1;
1002 892ac3b6 2020-03-18 stsp print_indexed = 1;
1003 892ac3b6 2020-03-18 stsp print_size = 1;
1004 892ac3b6 2020-03-18 stsp }
1005 61cc1a7a 2020-03-18 stsp }
1006 3168e5da 2020-09-10 stsp
1007 d2cdc636 2020-03-18 stsp }
1008 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1009 892ac3b6 2020-03-18 stsp printf("\r");
1010 892ac3b6 2020-03-18 stsp if (print_size)
1011 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1012 d715f13e 2020-03-19 stsp if (print_indexed)
1013 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1014 d715f13e 2020-03-19 stsp if (print_resolved)
1015 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1016 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1017 892ac3b6 2020-03-18 stsp fflush(stdout);
1018 892ac3b6 2020-03-18 stsp
1019 531c3985 2020-03-18 stsp return NULL;
1020 531c3985 2020-03-18 stsp }
1021 531c3985 2020-03-18 stsp
1022 531c3985 2020-03-18 stsp static const struct got_error *
1023 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1024 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1025 4ba14133 2020-03-20 stsp {
1026 4ba14133 2020-03-20 stsp const struct got_error *err;
1027 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1028 4ba14133 2020-03-20 stsp
1029 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1030 4ba14133 2020-03-20 stsp if (err)
1031 4ba14133 2020-03-20 stsp return err;
1032 4ba14133 2020-03-20 stsp
1033 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1034 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1035 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1036 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1037 6338a6a1 2020-03-21 stsp }
1038 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1039 4ba14133 2020-03-20 stsp return err;
1040 4ba14133 2020-03-20 stsp }
1041 4ba14133 2020-03-20 stsp
1042 4ba14133 2020-03-20 stsp static const struct got_error *
1043 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1044 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1045 41b0de12 2020-03-21 stsp {
1046 41b0de12 2020-03-21 stsp const struct got_error *err;
1047 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1048 41b0de12 2020-03-21 stsp
1049 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1050 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1051 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1052 41b0de12 2020-03-21 stsp
1053 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1054 41b0de12 2020-03-21 stsp }
1055 41b0de12 2020-03-21 stsp
1056 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1057 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1058 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1059 41b0de12 2020-03-21 stsp char *id_str;
1060 41b0de12 2020-03-21 stsp
1061 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1062 41b0de12 2020-03-21 stsp if (err)
1063 41b0de12 2020-03-21 stsp return err;
1064 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1065 41b0de12 2020-03-21 stsp free(id_str);
1066 41b0de12 2020-03-21 stsp }
1067 41b0de12 2020-03-21 stsp
1068 41b0de12 2020-03-21 stsp return NULL;
1069 6338a6a1 2020-03-21 stsp }
1070 6338a6a1 2020-03-21 stsp
1071 6338a6a1 2020-03-21 stsp static const struct got_error *
1072 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1073 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1074 6338a6a1 2020-03-21 stsp {
1075 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1076 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1077 6338a6a1 2020-03-21 stsp char *id_str;
1078 6338a6a1 2020-03-21 stsp
1079 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1080 6338a6a1 2020-03-21 stsp if (err)
1081 6338a6a1 2020-03-21 stsp return err;
1082 6338a6a1 2020-03-21 stsp
1083 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1084 6338a6a1 2020-03-21 stsp if (err)
1085 6338a6a1 2020-03-21 stsp goto done;
1086 6338a6a1 2020-03-21 stsp
1087 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1088 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1089 6338a6a1 2020-03-21 stsp
1090 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1091 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1092 6338a6a1 2020-03-21 stsp done:
1093 6338a6a1 2020-03-21 stsp free(id_str);
1094 6338a6a1 2020-03-21 stsp return err;
1095 0e4002ca 2020-03-21 stsp }
1096 0e4002ca 2020-03-21 stsp
1097 0e4002ca 2020-03-21 stsp static int
1098 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1099 0e4002ca 2020-03-21 stsp {
1100 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1101 0e4002ca 2020-03-21 stsp return 0;
1102 0e4002ca 2020-03-21 stsp refname += 5;
1103 0e4002ca 2020-03-21 stsp
1104 0e4002ca 2020-03-21 stsp /*
1105 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1106 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1107 0e4002ca 2020-03-21 stsp */
1108 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1109 0e4002ca 2020-03-21 stsp return 0;
1110 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1111 0e4002ca 2020-03-21 stsp return 0;
1112 0e4002ca 2020-03-21 stsp
1113 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1114 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1115 0e4002ca 2020-03-21 stsp
1116 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1117 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1118 0e4002ca 2020-03-21 stsp return 1;
1119 0e4002ca 2020-03-21 stsp
1120 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1121 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1122 41b0de12 2020-03-21 stsp }
1123 41b0de12 2020-03-21 stsp
1124 0e4002ca 2020-03-21 stsp static int
1125 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1126 0e4002ca 2020-03-21 stsp {
1127 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1128 0e4002ca 2020-03-21 stsp
1129 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1130 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1131 0e4002ca 2020-03-21 stsp return 1;
1132 0e4002ca 2020-03-21 stsp }
1133 0e4002ca 2020-03-21 stsp
1134 0e4002ca 2020-03-21 stsp return 0;
1135 0e4002ca 2020-03-21 stsp }
1136 0e4002ca 2020-03-21 stsp
1137 41b0de12 2020-03-21 stsp static const struct got_error *
1138 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1139 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1140 0e4002ca 2020-03-21 stsp {
1141 0e4002ca 2020-03-21 stsp const struct got_error *err;
1142 0e4002ca 2020-03-21 stsp char *remote_refname;
1143 0e4002ca 2020-03-21 stsp
1144 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1145 0e4002ca 2020-03-21 stsp refname += 5;
1146 0e4002ca 2020-03-21 stsp
1147 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1148 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1149 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1150 0e4002ca 2020-03-21 stsp
1151 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1152 0e4002ca 2020-03-21 stsp free(remote_refname);
1153 7c0b7f42 2020-09-24 stsp return err;
1154 7c0b7f42 2020-09-24 stsp }
1155 7c0b7f42 2020-09-24 stsp
1156 7c0b7f42 2020-09-24 stsp static const struct got_error *
1157 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1158 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, int fetch_all_branches, int mirror_references,
1159 7c0b7f42 2020-09-24 stsp struct got_repository *repo)
1160 7c0b7f42 2020-09-24 stsp {
1161 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1162 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1163 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1164 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1165 7c0b7f42 2020-09-24 stsp ssize_t n;
1166 7c0b7f42 2020-09-24 stsp
1167 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1168 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1169 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1170 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1171 7c0b7f42 2020-09-24 stsp goto done;
1172 7c0b7f42 2020-09-24 stsp }
1173 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1174 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1175 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1176 7c0b7f42 2020-09-24 stsp goto done;
1177 7c0b7f42 2020-09-24 stsp }
1178 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1179 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1180 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1181 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1182 7c0b7f42 2020-09-24 stsp "%s%s%s"
1183 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1184 7c0b7f42 2020-09-24 stsp "%s"
1185 7c0b7f42 2020-09-24 stsp "}\n",
1186 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1187 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1188 7c0b7f42 2020-09-24 stsp remote_repo_path,
1189 7c0b7f42 2020-09-24 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1190 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1191 7c0b7f42 2020-09-24 stsp goto done;
1192 7c0b7f42 2020-09-24 stsp }
1193 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1194 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1195 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1196 7c0b7f42 2020-09-24 stsp goto done;
1197 7c0b7f42 2020-09-24 stsp }
1198 7c0b7f42 2020-09-24 stsp
1199 7c0b7f42 2020-09-24 stsp done:
1200 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1201 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1202 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1203 7c0b7f42 2020-09-24 stsp return err;
1204 7c0b7f42 2020-09-24 stsp }
1205 7c0b7f42 2020-09-24 stsp
1206 7c0b7f42 2020-09-24 stsp static const struct got_error *
1207 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1208 7c0b7f42 2020-09-24 stsp int fetch_all_branches, int mirror_references, struct got_repository *repo)
1209 7c0b7f42 2020-09-24 stsp {
1210 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1211 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1212 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1213 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1214 7c0b7f42 2020-09-24 stsp ssize_t n;
1215 7c0b7f42 2020-09-24 stsp
1216 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1217 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1218 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1219 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1220 7c0b7f42 2020-09-24 stsp goto done;
1221 7c0b7f42 2020-09-24 stsp }
1222 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1223 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1224 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1225 7c0b7f42 2020-09-24 stsp goto done;
1226 7c0b7f42 2020-09-24 stsp }
1227 7c0b7f42 2020-09-24 stsp if (mirror_references) {
1228 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1229 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1230 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1231 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/*:refs/*\n"
1232 7c0b7f42 2020-09-24 stsp "\tmirror = true\n",
1233 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1234 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1235 7c0b7f42 2020-09-24 stsp goto done;
1236 7c0b7f42 2020-09-24 stsp }
1237 7c0b7f42 2020-09-24 stsp } else if (fetch_all_branches) {
1238 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1239 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1240 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1241 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1242 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1243 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1244 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1245 7c0b7f42 2020-09-24 stsp goto done;
1246 7c0b7f42 2020-09-24 stsp }
1247 7c0b7f42 2020-09-24 stsp } else {
1248 7c0b7f42 2020-09-24 stsp const char *branchname;
1249 7c0b7f42 2020-09-24 stsp
1250 7c0b7f42 2020-09-24 stsp /*
1251 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1252 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1253 7c0b7f42 2020-09-24 stsp */
1254 04d9a9ec 2020-09-24 stsp if (default_branch) {
1255 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1256 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1257 7c0b7f42 2020-09-24 stsp branchname += 11;
1258 7c0b7f42 2020-09-24 stsp } else
1259 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1260 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1261 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1262 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1263 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1264 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1265 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1266 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1267 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1268 7c0b7f42 2020-09-24 stsp goto done;
1269 7c0b7f42 2020-09-24 stsp }
1270 7c0b7f42 2020-09-24 stsp }
1271 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1272 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1273 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1274 7c0b7f42 2020-09-24 stsp goto done;
1275 7c0b7f42 2020-09-24 stsp }
1276 7c0b7f42 2020-09-24 stsp done:
1277 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1278 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1279 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1280 0e4002ca 2020-03-21 stsp return err;
1281 0e4002ca 2020-03-21 stsp }
1282 0e4002ca 2020-03-21 stsp
1283 0e4002ca 2020-03-21 stsp static const struct got_error *
1284 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1285 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1286 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1287 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo)
1288 04d9a9ec 2020-09-24 stsp {
1289 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1290 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1291 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1292 04d9a9ec 2020-09-24 stsp
1293 04d9a9ec 2020-09-24 stsp /*
1294 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1295 04d9a9ec 2020-09-24 stsp * one of those.
1296 04d9a9ec 2020-09-24 stsp */
1297 04d9a9ec 2020-09-24 stsp if (!TAILQ_EMPTY(wanted_branches)) {
1298 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1299 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1300 04d9a9ec 2020-09-24 stsp } else {
1301 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1302 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1303 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1304 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1305 04d9a9ec 2020-09-24 stsp
1306 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1307 04d9a9ec 2020-09-24 stsp continue;
1308 04d9a9ec 2020-09-24 stsp
1309 04d9a9ec 2020-09-24 stsp default_branch = target;
1310 04d9a9ec 2020-09-24 stsp break;
1311 04d9a9ec 2020-09-24 stsp }
1312 04d9a9ec 2020-09-24 stsp }
1313 04d9a9ec 2020-09-24 stsp
1314 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1315 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1316 04d9a9ec 2020-09-24 stsp fetch_all_branches, mirror_references, repo);
1317 04d9a9ec 2020-09-24 stsp if (err)
1318 04d9a9ec 2020-09-24 stsp return err;
1319 04d9a9ec 2020-09-24 stsp
1320 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1321 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1322 04d9a9ec 2020-09-24 stsp mirror_references, repo);
1323 04d9a9ec 2020-09-24 stsp }
1324 04d9a9ec 2020-09-24 stsp
1325 04d9a9ec 2020-09-24 stsp static const struct got_error *
1326 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1327 93658fb9 2020-03-18 stsp {
1328 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1329 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1330 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1331 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1332 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1333 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1334 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1335 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1336 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1337 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1338 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1339 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1340 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1341 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1342 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1343 93658fb9 2020-03-18 stsp
1344 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1345 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1346 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1347 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1348 d9b4d0c0 2020-03-18 stsp
1349 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1350 93658fb9 2020-03-18 stsp switch (ch) {
1351 659e7fbd 2020-03-20 stsp case 'a':
1352 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1353 4ba14133 2020-03-20 stsp break;
1354 4ba14133 2020-03-20 stsp case 'b':
1355 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1356 4ba14133 2020-03-20 stsp optarg, NULL);
1357 4ba14133 2020-03-20 stsp if (error)
1358 4ba14133 2020-03-20 stsp return error;
1359 659e7fbd 2020-03-20 stsp break;
1360 41b0de12 2020-03-21 stsp case 'l':
1361 41b0de12 2020-03-21 stsp list_refs_only = 1;
1362 41b0de12 2020-03-21 stsp break;
1363 469dd726 2020-03-20 stsp case 'm':
1364 469dd726 2020-03-20 stsp mirror_references = 1;
1365 469dd726 2020-03-20 stsp break;
1366 68999b92 2020-03-18 stsp case 'v':
1367 68999b92 2020-03-18 stsp if (verbosity < 0)
1368 68999b92 2020-03-18 stsp verbosity = 0;
1369 68999b92 2020-03-18 stsp else if (verbosity < 3)
1370 68999b92 2020-03-18 stsp verbosity++;
1371 68999b92 2020-03-18 stsp break;
1372 68999b92 2020-03-18 stsp case 'q':
1373 68999b92 2020-03-18 stsp verbosity = -1;
1374 68999b92 2020-03-18 stsp break;
1375 0e4002ca 2020-03-21 stsp case 'R':
1376 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1377 0e4002ca 2020-03-21 stsp optarg, NULL);
1378 0e4002ca 2020-03-21 stsp if (error)
1379 0e4002ca 2020-03-21 stsp return error;
1380 0e4002ca 2020-03-21 stsp break;
1381 93658fb9 2020-03-18 stsp default:
1382 93658fb9 2020-03-18 stsp usage_clone();
1383 93658fb9 2020-03-18 stsp break;
1384 93658fb9 2020-03-18 stsp }
1385 93658fb9 2020-03-18 stsp }
1386 93658fb9 2020-03-18 stsp argc -= optind;
1387 93658fb9 2020-03-18 stsp argv += optind;
1388 39c64a6a 2020-03-18 stsp
1389 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1390 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1391 41b0de12 2020-03-21 stsp if (list_refs_only) {
1392 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1393 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1394 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1395 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1396 41b0de12 2020-03-21 stsp if (mirror_references)
1397 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1398 41b0de12 2020-03-21 stsp if (verbosity == -1)
1399 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1400 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1401 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1402 41b0de12 2020-03-21 stsp }
1403 4ba14133 2020-03-20 stsp
1404 93658fb9 2020-03-18 stsp uri = argv[0];
1405 9df6f38b 2020-03-18 stsp
1406 9df6f38b 2020-03-18 stsp if (argc == 1)
1407 93658fb9 2020-03-18 stsp dirname = NULL;
1408 9df6f38b 2020-03-18 stsp else if (argc == 2)
1409 93658fb9 2020-03-18 stsp dirname = argv[1];
1410 93658fb9 2020-03-18 stsp else
1411 93658fb9 2020-03-18 stsp usage_clone();
1412 09838ffc 2020-03-18 stsp
1413 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1414 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1415 39c64a6a 2020-03-18 stsp if (error)
1416 09f63084 2020-03-20 stsp goto done;
1417 09f63084 2020-03-20 stsp
1418 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1419 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1420 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1421 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1422 09838ffc 2020-03-18 stsp goto done;
1423 09f63084 2020-03-20 stsp }
1424 09838ffc 2020-03-18 stsp
1425 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1426 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1427 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1428 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1429 39c64a6a 2020-03-18 stsp err(1, "pledge");
1430 b46f3e71 2020-03-18 stsp #endif
1431 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1432 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1433 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1434 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1435 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1436 39c64a6a 2020-03-18 stsp err(1, "pledge");
1437 b46f3e71 2020-03-18 stsp #endif
1438 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1439 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1440 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1441 39c64a6a 2020-03-18 stsp goto done;
1442 39c64a6a 2020-03-18 stsp } else {
1443 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1444 39c64a6a 2020-03-18 stsp goto done;
1445 39c64a6a 2020-03-18 stsp }
1446 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1447 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1448 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1449 bb64b798 2020-03-18 stsp goto done;
1450 bb64b798 2020-03-18 stsp }
1451 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1452 bb64b798 2020-03-18 stsp } else
1453 bb64b798 2020-03-18 stsp repo_path = dirname;
1454 bb64b798 2020-03-18 stsp
1455 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1456 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1457 2751fe64 2020-09-24 stsp if (error &&
1458 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1459 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1460 41b0de12 2020-03-21 stsp goto done;
1461 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1462 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1463 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1464 41b0de12 2020-03-21 stsp goto done;
1465 2751fe64 2020-09-24 stsp }
1466 41b0de12 2020-03-21 stsp }
1467 bb64b798 2020-03-18 stsp
1468 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1469 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1470 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1471 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1472 ee448f5f 2020-03-18 stsp goto done;
1473 ee448f5f 2020-03-18 stsp }
1474 ee448f5f 2020-03-18 stsp }
1475 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1476 ee448f5f 2020-03-18 stsp if (error)
1477 ee448f5f 2020-03-18 stsp goto done;
1478 f79e6490 2020-04-19 stsp
1479 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1480 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1481 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1482 ee448f5f 2020-03-18 stsp
1483 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1484 9c52365f 2020-03-21 stsp server_path, verbosity);
1485 39c64a6a 2020-03-18 stsp if (error)
1486 bb64b798 2020-03-18 stsp goto done;
1487 bb64b798 2020-03-18 stsp
1488 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1489 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1490 2751fe64 2020-09-24 stsp if (error)
1491 2751fe64 2020-09-24 stsp goto done;
1492 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1493 2751fe64 2020-09-24 stsp if (error)
1494 2751fe64 2020-09-24 stsp goto done;
1495 2751fe64 2020-09-24 stsp }
1496 2751fe64 2020-09-24 stsp
1497 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1498 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1499 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1500 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1501 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1502 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1503 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1504 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1505 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1506 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1507 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1508 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1509 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1510 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1511 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1512 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1513 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1514 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1515 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1516 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1517 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1518 39c64a6a 2020-03-18 stsp if (error)
1519 d9b4d0c0 2020-03-18 stsp goto done;
1520 d9b4d0c0 2020-03-18 stsp
1521 41b0de12 2020-03-21 stsp if (list_refs_only) {
1522 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1523 41b0de12 2020-03-21 stsp goto done;
1524 41b0de12 2020-03-21 stsp }
1525 41b0de12 2020-03-21 stsp
1526 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1527 39c64a6a 2020-03-18 stsp if (error)
1528 d9b4d0c0 2020-03-18 stsp goto done;
1529 68999b92 2020-03-18 stsp if (verbosity >= 0)
1530 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1531 d9b4d0c0 2020-03-18 stsp free(id_str);
1532 d9b4d0c0 2020-03-18 stsp
1533 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1534 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1535 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1536 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1537 7ebc0570 2020-03-18 stsp char *remote_refname;
1538 0e4002ca 2020-03-21 stsp
1539 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1540 0e4002ca 2020-03-21 stsp !mirror_references) {
1541 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1542 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1543 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1544 0e4002ca 2020-03-21 stsp if (error)
1545 0e4002ca 2020-03-21 stsp goto done;
1546 0e4002ca 2020-03-21 stsp continue;
1547 0e4002ca 2020-03-21 stsp }
1548 668a20f6 2020-03-18 stsp
1549 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1550 39c64a6a 2020-03-18 stsp if (error)
1551 d9b4d0c0 2020-03-18 stsp goto done;
1552 d9b4d0c0 2020-03-18 stsp
1553 469dd726 2020-03-20 stsp if (mirror_references)
1554 469dd726 2020-03-20 stsp continue;
1555 469dd726 2020-03-20 stsp
1556 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1557 7ebc0570 2020-03-18 stsp continue;
1558 7ebc0570 2020-03-18 stsp
1559 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1560 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1561 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1562 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1563 7ebc0570 2020-03-18 stsp goto done;
1564 7ebc0570 2020-03-18 stsp }
1565 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1566 f298ae0f 2020-03-25 stsp free(remote_refname);
1567 39c64a6a 2020-03-18 stsp if (error)
1568 d9b4d0c0 2020-03-18 stsp goto done;
1569 d9b4d0c0 2020-03-18 stsp }
1570 d9b4d0c0 2020-03-18 stsp
1571 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1572 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1573 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1574 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1575 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1576 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1577 d9b4d0c0 2020-03-18 stsp
1578 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1579 d9b4d0c0 2020-03-18 stsp continue;
1580 d9b4d0c0 2020-03-18 stsp
1581 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1582 39c64a6a 2020-03-18 stsp if (error) {
1583 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1584 55330abe 2020-03-20 stsp error = NULL;
1585 d9b4d0c0 2020-03-18 stsp continue;
1586 55330abe 2020-03-20 stsp }
1587 d9b4d0c0 2020-03-18 stsp goto done;
1588 d9b4d0c0 2020-03-18 stsp }
1589 d9b4d0c0 2020-03-18 stsp
1590 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1591 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1592 f298ae0f 2020-03-25 stsp if (error)
1593 f298ae0f 2020-03-25 stsp goto done;
1594 f298ae0f 2020-03-25 stsp
1595 f298ae0f 2020-03-25 stsp if (mirror_references)
1596 f298ae0f 2020-03-25 stsp continue;
1597 f298ae0f 2020-03-25 stsp
1598 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1599 f298ae0f 2020-03-25 stsp continue;
1600 f298ae0f 2020-03-25 stsp
1601 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1602 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1603 f298ae0f 2020-03-25 stsp refname) == -1) {
1604 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1605 f298ae0f 2020-03-25 stsp goto done;
1606 f298ae0f 2020-03-25 stsp }
1607 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1608 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1609 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1610 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1611 f298ae0f 2020-03-25 stsp free(remote_refname);
1612 f298ae0f 2020-03-25 stsp goto done;
1613 f298ae0f 2020-03-25 stsp }
1614 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1615 f298ae0f 2020-03-25 stsp if (error) {
1616 f298ae0f 2020-03-25 stsp free(remote_refname);
1617 f298ae0f 2020-03-25 stsp free(remote_target);
1618 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1619 f298ae0f 2020-03-25 stsp error = NULL;
1620 f298ae0f 2020-03-25 stsp continue;
1621 f298ae0f 2020-03-25 stsp }
1622 f298ae0f 2020-03-25 stsp goto done;
1623 f298ae0f 2020-03-25 stsp }
1624 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1625 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1626 f298ae0f 2020-03-25 stsp free(remote_refname);
1627 f298ae0f 2020-03-25 stsp free(remote_target);
1628 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1629 39c64a6a 2020-03-18 stsp if (error)
1630 d9b4d0c0 2020-03-18 stsp goto done;
1631 4ba14133 2020-03-20 stsp }
1632 4ba14133 2020-03-20 stsp if (pe == NULL) {
1633 4ba14133 2020-03-20 stsp /*
1634 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1635 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1636 4ba14133 2020-03-20 stsp * which could be fetched instead.
1637 4ba14133 2020-03-20 stsp */
1638 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1639 4ba14133 2020-03-20 stsp const char *target = pe->path;
1640 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1641 d9b4d0c0 2020-03-18 stsp
1642 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1643 4ba14133 2020-03-20 stsp if (error) {
1644 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1645 4ba14133 2020-03-20 stsp error = NULL;
1646 4ba14133 2020-03-20 stsp continue;
1647 4ba14133 2020-03-20 stsp }
1648 4ba14133 2020-03-20 stsp goto done;
1649 4ba14133 2020-03-20 stsp }
1650 4ba14133 2020-03-20 stsp
1651 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1652 04d9a9ec 2020-09-24 stsp verbosity, repo);
1653 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1654 4ba14133 2020-03-20 stsp if (error)
1655 4ba14133 2020-03-20 stsp goto done;
1656 4ba14133 2020-03-20 stsp break;
1657 4ba14133 2020-03-20 stsp }
1658 659e7fbd 2020-03-20 stsp }
1659 659e7fbd 2020-03-20 stsp
1660 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1661 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1662 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1663 09838ffc 2020-03-18 stsp done:
1664 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1665 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1666 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1667 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1668 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1669 9c52365f 2020-03-21 stsp }
1670 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1671 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1672 bb64b798 2020-03-18 stsp if (repo)
1673 bb64b798 2020-03-18 stsp got_repo_close(repo);
1674 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1675 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1676 d9b4d0c0 2020-03-18 stsp free(pe->data);
1677 d9b4d0c0 2020-03-18 stsp }
1678 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1679 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1680 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1681 d9b4d0c0 2020-03-18 stsp free(pe->data);
1682 d9b4d0c0 2020-03-18 stsp }
1683 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1684 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1685 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1686 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1687 09838ffc 2020-03-18 stsp free(proto);
1688 09838ffc 2020-03-18 stsp free(host);
1689 09838ffc 2020-03-18 stsp free(port);
1690 09838ffc 2020-03-18 stsp free(server_path);
1691 09838ffc 2020-03-18 stsp free(repo_name);
1692 bb64b798 2020-03-18 stsp free(default_destdir);
1693 b46f3e71 2020-03-18 stsp free(git_url);
1694 39c64a6a 2020-03-18 stsp return error;
1695 7848a0e1 2020-03-19 stsp }
1696 7848a0e1 2020-03-19 stsp
1697 7848a0e1 2020-03-19 stsp static const struct got_error *
1698 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1699 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1700 7848a0e1 2020-03-19 stsp {
1701 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1702 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1703 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1704 7848a0e1 2020-03-19 stsp
1705 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1706 7848a0e1 2020-03-19 stsp if (err)
1707 7848a0e1 2020-03-19 stsp goto done;
1708 7848a0e1 2020-03-19 stsp
1709 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1710 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1711 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1712 88609724 2020-03-21 stsp if (err)
1713 88609724 2020-03-21 stsp goto done;
1714 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1715 88609724 2020-03-21 stsp goto done;
1716 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1717 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1718 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1719 db6d8ad8 2020-03-21 stsp }
1720 db6d8ad8 2020-03-21 stsp goto done;
1721 db6d8ad8 2020-03-21 stsp }
1722 db6d8ad8 2020-03-21 stsp
1723 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1724 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1725 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1726 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1727 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1728 688f11b3 2020-03-21 stsp }
1729 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1730 7848a0e1 2020-03-19 stsp if (err)
1731 7848a0e1 2020-03-19 stsp goto done;
1732 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1733 e8a967e0 2020-03-21 stsp if (err)
1734 e8a967e0 2020-03-21 stsp goto done;
1735 7848a0e1 2020-03-19 stsp } else {
1736 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1737 7848a0e1 2020-03-19 stsp if (err)
1738 7848a0e1 2020-03-19 stsp goto done;
1739 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1740 6338a6a1 2020-03-21 stsp goto done;
1741 6338a6a1 2020-03-21 stsp
1742 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1743 6338a6a1 2020-03-21 stsp if (err)
1744 6338a6a1 2020-03-21 stsp goto done;
1745 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1746 6338a6a1 2020-03-21 stsp if (err)
1747 6338a6a1 2020-03-21 stsp goto done;
1748 7848a0e1 2020-03-19 stsp }
1749 6338a6a1 2020-03-21 stsp
1750 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1751 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1752 6338a6a1 2020-03-21 stsp new_id_str);
1753 7848a0e1 2020-03-19 stsp done:
1754 7848a0e1 2020-03-19 stsp free(old_id);
1755 7848a0e1 2020-03-19 stsp free(new_id_str);
1756 7848a0e1 2020-03-19 stsp return err;
1757 2ab43947 2020-03-18 stsp }
1758 f1bcca34 2020-03-25 stsp
1759 f1bcca34 2020-03-25 stsp static const struct got_error *
1760 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1761 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1762 f1bcca34 2020-03-25 stsp {
1763 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1764 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1765 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1766 f1bcca34 2020-03-25 stsp
1767 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1768 bcf34b0e 2020-03-26 stsp if (err) {
1769 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1770 bcf34b0e 2020-03-26 stsp return err;
1771 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1772 bcf34b0e 2020-03-26 stsp if (err)
1773 bcf34b0e 2020-03-26 stsp goto done;
1774 2ab43947 2020-03-18 stsp
1775 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1776 bcf34b0e 2020-03-26 stsp if (err)
1777 bcf34b0e 2020-03-26 stsp goto done;
1778 f1bcca34 2020-03-25 stsp
1779 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1780 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1781 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1782 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1783 bcf34b0e 2020-03-26 stsp } else {
1784 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1785 f1bcca34 2020-03-25 stsp
1786 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1787 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1788 bcf34b0e 2020-03-26 stsp goto done;
1789 bcf34b0e 2020-03-26 stsp
1790 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1791 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1792 bcf34b0e 2020-03-26 stsp if (err)
1793 bcf34b0e 2020-03-26 stsp goto done;
1794 bcf34b0e 2020-03-26 stsp
1795 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1796 bcf34b0e 2020-03-26 stsp if (err)
1797 bcf34b0e 2020-03-26 stsp goto done;
1798 bcf34b0e 2020-03-26 stsp
1799 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1800 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1801 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1802 bcf34b0e 2020-03-26 stsp
1803 bcf34b0e 2020-03-26 stsp }
1804 f1bcca34 2020-03-25 stsp done:
1805 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1806 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1807 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1808 bcf34b0e 2020-03-26 stsp err = unlock_err;
1809 bcf34b0e 2020-03-26 stsp }
1810 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1811 f1bcca34 2020-03-25 stsp return err;
1812 f1bcca34 2020-03-25 stsp }
1813 f1bcca34 2020-03-25 stsp
1814 2ab43947 2020-03-18 stsp __dead static void
1815 7848a0e1 2020-03-19 stsp usage_fetch(void)
1816 7848a0e1 2020-03-19 stsp {
1817 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1818 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1819 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1820 13f12b09 2020-03-21 stsp getprogname());
1821 7848a0e1 2020-03-19 stsp exit(1);
1822 7848a0e1 2020-03-19 stsp }
1823 7848a0e1 2020-03-19 stsp
1824 7848a0e1 2020-03-19 stsp static const struct got_error *
1825 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1826 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1827 f21ec2f0 2020-03-21 stsp {
1828 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1829 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1830 3789fd73 2020-03-26 stsp char *id_str = NULL;
1831 3789fd73 2020-03-26 stsp
1832 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1833 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1834 3789fd73 2020-03-26 stsp if (err)
1835 3789fd73 2020-03-26 stsp return err;
1836 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1837 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1838 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1839 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1840 3789fd73 2020-03-26 stsp }
1841 3789fd73 2020-03-26 stsp } else {
1842 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1843 3789fd73 2020-03-26 stsp if (err)
1844 3789fd73 2020-03-26 stsp return err;
1845 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1846 3789fd73 2020-03-26 stsp if (err)
1847 3789fd73 2020-03-26 stsp goto done;
1848 3168e5da 2020-09-10 stsp
1849 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1850 3789fd73 2020-03-26 stsp if (err)
1851 3789fd73 2020-03-26 stsp goto done;
1852 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1853 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1854 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1855 3789fd73 2020-03-26 stsp }
1856 3789fd73 2020-03-26 stsp }
1857 3789fd73 2020-03-26 stsp done:
1858 3789fd73 2020-03-26 stsp free(id);
1859 3789fd73 2020-03-26 stsp free(id_str);
1860 3789fd73 2020-03-26 stsp return NULL;
1861 3789fd73 2020-03-26 stsp }
1862 3789fd73 2020-03-26 stsp
1863 3789fd73 2020-03-26 stsp static const struct got_error *
1864 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1865 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1866 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1867 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1868 3789fd73 2020-03-26 stsp {
1869 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1870 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1871 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1872 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1873 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1874 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1875 f21ec2f0 2020-03-21 stsp
1876 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1877 f21ec2f0 2020-03-21 stsp
1878 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1879 3789fd73 2020-03-26 stsp == -1)
1880 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1881 3789fd73 2020-03-26 stsp
1882 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1883 f21ec2f0 2020-03-21 stsp if (err)
1884 3789fd73 2020-03-26 stsp goto done;
1885 f21ec2f0 2020-03-21 stsp
1886 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1887 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1888 f21ec2f0 2020-03-21 stsp
1889 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1890 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1891 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1892 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1893 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1894 3789fd73 2020-03-26 stsp continue;
1895 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1896 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1897 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1898 3789fd73 2020-03-26 stsp goto done;
1899 3789fd73 2020-03-26 stsp }
1900 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1901 3789fd73 2020-03-26 stsp continue;
1902 3789fd73 2020-03-26 stsp }
1903 f21ec2f0 2020-03-21 stsp
1904 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1905 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1906 f21ec2f0 2020-03-21 stsp break;
1907 f21ec2f0 2020-03-21 stsp }
1908 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1909 f21ec2f0 2020-03-21 stsp continue;
1910 f21ec2f0 2020-03-21 stsp
1911 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1912 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1913 3789fd73 2020-03-26 stsp break;
1914 3789fd73 2020-03-26 stsp }
1915 3789fd73 2020-03-26 stsp if (pe != NULL)
1916 3789fd73 2020-03-26 stsp continue;
1917 f21ec2f0 2020-03-21 stsp
1918 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1919 f21ec2f0 2020-03-21 stsp if (err)
1920 f21ec2f0 2020-03-21 stsp break;
1921 3789fd73 2020-03-26 stsp
1922 3789fd73 2020-03-26 stsp if (local_refname) {
1923 3789fd73 2020-03-26 stsp struct got_reference *ref;
1924 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1925 3789fd73 2020-03-26 stsp if (err) {
1926 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1927 3789fd73 2020-03-26 stsp break;
1928 3789fd73 2020-03-26 stsp free(local_refname);
1929 3789fd73 2020-03-26 stsp local_refname = NULL;
1930 3789fd73 2020-03-26 stsp continue;
1931 3789fd73 2020-03-26 stsp }
1932 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1933 3789fd73 2020-03-26 stsp if (err)
1934 3789fd73 2020-03-26 stsp break;
1935 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1936 3789fd73 2020-03-26 stsp got_ref_close(ref);
1937 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1938 3789fd73 2020-03-26 stsp err = unlock_err;
1939 3789fd73 2020-03-26 stsp break;
1940 3789fd73 2020-03-26 stsp }
1941 3789fd73 2020-03-26 stsp
1942 3789fd73 2020-03-26 stsp free(local_refname);
1943 3789fd73 2020-03-26 stsp local_refname = NULL;
1944 6338a6a1 2020-03-21 stsp }
1945 f21ec2f0 2020-03-21 stsp }
1946 3789fd73 2020-03-26 stsp done:
1947 3789fd73 2020-03-26 stsp free(remote_namespace);
1948 3789fd73 2020-03-26 stsp free(local_refname);
1949 0e4002ca 2020-03-21 stsp return err;
1950 0e4002ca 2020-03-21 stsp }
1951 0e4002ca 2020-03-21 stsp
1952 0e4002ca 2020-03-21 stsp static const struct got_error *
1953 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1954 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1955 0e4002ca 2020-03-21 stsp {
1956 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1957 0e4002ca 2020-03-21 stsp char *remote_refname;
1958 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1959 0e4002ca 2020-03-21 stsp
1960 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1961 0e4002ca 2020-03-21 stsp refname += 5;
1962 0e4002ca 2020-03-21 stsp
1963 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1964 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1965 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1966 f21ec2f0 2020-03-21 stsp
1967 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1968 0e4002ca 2020-03-21 stsp if (err) {
1969 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1970 0e4002ca 2020-03-21 stsp goto done;
1971 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1972 0e4002ca 2020-03-21 stsp } else {
1973 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1974 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1975 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1976 9f142382 2020-03-21 stsp err = unlock_err;
1977 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1978 0e4002ca 2020-03-21 stsp }
1979 0e4002ca 2020-03-21 stsp done:
1980 0e4002ca 2020-03-21 stsp free(remote_refname);
1981 f21ec2f0 2020-03-21 stsp return err;
1982 f21ec2f0 2020-03-21 stsp }
1983 f21ec2f0 2020-03-21 stsp
1984 f21ec2f0 2020-03-21 stsp static const struct got_error *
1985 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1986 7848a0e1 2020-03-19 stsp {
1987 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1988 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1989 7848a0e1 2020-03-19 stsp const char *remote_name;
1990 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1991 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1992 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
1993 7848a0e1 2020-03-19 stsp int nremotes;
1994 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1995 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1996 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1997 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1998 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1999 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2000 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2001 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2002 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2003 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2004 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2005 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
2006 7848a0e1 2020-03-19 stsp
2007 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2008 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2009 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2010 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2011 7848a0e1 2020-03-19 stsp
2012 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2013 7848a0e1 2020-03-19 stsp switch (ch) {
2014 659e7fbd 2020-03-20 stsp case 'a':
2015 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2016 4ba14133 2020-03-20 stsp break;
2017 4ba14133 2020-03-20 stsp case 'b':
2018 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2019 4ba14133 2020-03-20 stsp optarg, NULL);
2020 4ba14133 2020-03-20 stsp if (error)
2021 4ba14133 2020-03-20 stsp return error;
2022 41b0de12 2020-03-21 stsp break;
2023 f21ec2f0 2020-03-21 stsp case 'd':
2024 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2025 f21ec2f0 2020-03-21 stsp break;
2026 41b0de12 2020-03-21 stsp case 'l':
2027 41b0de12 2020-03-21 stsp list_refs_only = 1;
2028 659e7fbd 2020-03-20 stsp break;
2029 7848a0e1 2020-03-19 stsp case 'r':
2030 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2031 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2032 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2033 7848a0e1 2020-03-19 stsp optarg);
2034 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2035 7848a0e1 2020-03-19 stsp break;
2036 db6d8ad8 2020-03-21 stsp case 't':
2037 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2038 db6d8ad8 2020-03-21 stsp break;
2039 7848a0e1 2020-03-19 stsp case 'v':
2040 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2041 7848a0e1 2020-03-19 stsp verbosity = 0;
2042 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2043 7848a0e1 2020-03-19 stsp verbosity++;
2044 7848a0e1 2020-03-19 stsp break;
2045 7848a0e1 2020-03-19 stsp case 'q':
2046 7848a0e1 2020-03-19 stsp verbosity = -1;
2047 7848a0e1 2020-03-19 stsp break;
2048 0e4002ca 2020-03-21 stsp case 'R':
2049 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2050 0e4002ca 2020-03-21 stsp optarg, NULL);
2051 0e4002ca 2020-03-21 stsp if (error)
2052 0e4002ca 2020-03-21 stsp return error;
2053 0e4002ca 2020-03-21 stsp break;
2054 7848a0e1 2020-03-19 stsp default:
2055 7848a0e1 2020-03-19 stsp usage_fetch();
2056 7848a0e1 2020-03-19 stsp break;
2057 7848a0e1 2020-03-19 stsp }
2058 7848a0e1 2020-03-19 stsp }
2059 7848a0e1 2020-03-19 stsp argc -= optind;
2060 7848a0e1 2020-03-19 stsp argv += optind;
2061 7848a0e1 2020-03-19 stsp
2062 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2063 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
2064 41b0de12 2020-03-21 stsp if (list_refs_only) {
2065 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2066 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
2067 41b0de12 2020-03-21 stsp if (fetch_all_branches)
2068 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
2069 f21ec2f0 2020-03-21 stsp if (delete_refs)
2070 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
2071 41b0de12 2020-03-21 stsp if (verbosity == -1)
2072 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
2073 41b0de12 2020-03-21 stsp }
2074 4ba14133 2020-03-20 stsp
2075 7848a0e1 2020-03-19 stsp if (argc == 0)
2076 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2077 7848a0e1 2020-03-19 stsp else if (argc == 1)
2078 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2079 7848a0e1 2020-03-19 stsp else
2080 7848a0e1 2020-03-19 stsp usage_fetch();
2081 7848a0e1 2020-03-19 stsp
2082 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2083 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2084 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2085 7848a0e1 2020-03-19 stsp goto done;
2086 7848a0e1 2020-03-19 stsp }
2087 7848a0e1 2020-03-19 stsp
2088 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2089 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2090 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2091 7848a0e1 2020-03-19 stsp goto done;
2092 7848a0e1 2020-03-19 stsp else
2093 7848a0e1 2020-03-19 stsp error = NULL;
2094 7848a0e1 2020-03-19 stsp if (worktree) {
2095 7848a0e1 2020-03-19 stsp repo_path =
2096 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2097 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2098 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2099 7848a0e1 2020-03-19 stsp if (error)
2100 7848a0e1 2020-03-19 stsp goto done;
2101 7848a0e1 2020-03-19 stsp } else {
2102 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2103 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2104 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2105 7848a0e1 2020-03-19 stsp goto done;
2106 7848a0e1 2020-03-19 stsp }
2107 7848a0e1 2020-03-19 stsp }
2108 7848a0e1 2020-03-19 stsp }
2109 7848a0e1 2020-03-19 stsp
2110 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2111 7848a0e1 2020-03-19 stsp if (error)
2112 7848a0e1 2020-03-19 stsp goto done;
2113 7848a0e1 2020-03-19 stsp
2114 50b0790e 2020-09-11 stsp if (worktree) {
2115 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2116 50b0790e 2020-09-11 stsp if (worktree_conf) {
2117 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2118 50b0790e 2020-09-11 stsp worktree_conf);
2119 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2120 50b0790e 2020-09-11 stsp remote = &remotes[i];
2121 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
2122 50b0790e 2020-09-11 stsp break;
2123 50b0790e 2020-09-11 stsp }
2124 50b0790e 2020-09-11 stsp }
2125 7848a0e1 2020-03-19 stsp }
2126 50b0790e 2020-09-11 stsp if (remote == NULL) {
2127 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2128 50b0790e 2020-09-11 stsp if (repo_conf) {
2129 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2130 50b0790e 2020-09-11 stsp repo_conf);
2131 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2132 50b0790e 2020-09-11 stsp remote = &remotes[i];
2133 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
2134 50b0790e 2020-09-11 stsp break;
2135 50b0790e 2020-09-11 stsp }
2136 50b0790e 2020-09-11 stsp }
2137 50b0790e 2020-09-11 stsp }
2138 50b0790e 2020-09-11 stsp if (remote == NULL) {
2139 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2140 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2141 257add31 2020-09-09 stsp remote = &remotes[i];
2142 257add31 2020-09-09 stsp if (strcmp(remote->name, remote_name) == 0)
2143 257add31 2020-09-09 stsp break;
2144 257add31 2020-09-09 stsp }
2145 7848a0e1 2020-03-19 stsp }
2146 50b0790e 2020-09-11 stsp if (remote == NULL) {
2147 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2148 50b0790e 2020-09-11 stsp goto done;
2149 b8adfa55 2020-09-25 stsp }
2150 b8adfa55 2020-09-25 stsp
2151 b8adfa55 2020-09-25 stsp if (TAILQ_EMPTY(&wanted_branches) && remote->nbranches > 0) {
2152 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2153 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2154 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2155 b8adfa55 2020-09-25 stsp }
2156 b8adfa55 2020-09-25 stsp
2157 50b0790e 2020-09-11 stsp }
2158 7848a0e1 2020-03-19 stsp
2159 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2160 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2161 7848a0e1 2020-03-19 stsp if (error)
2162 7848a0e1 2020-03-19 stsp goto done;
2163 7848a0e1 2020-03-19 stsp
2164 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2165 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2166 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2167 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2168 7848a0e1 2020-03-19 stsp err(1, "pledge");
2169 7848a0e1 2020-03-19 stsp #endif
2170 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2171 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2172 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2173 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2174 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2175 7848a0e1 2020-03-19 stsp err(1, "pledge");
2176 7848a0e1 2020-03-19 stsp #endif
2177 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2178 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2179 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2180 7848a0e1 2020-03-19 stsp goto done;
2181 7848a0e1 2020-03-19 stsp } else {
2182 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2183 7848a0e1 2020-03-19 stsp goto done;
2184 7848a0e1 2020-03-19 stsp }
2185 7848a0e1 2020-03-19 stsp
2186 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2187 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2188 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2189 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2190 7848a0e1 2020-03-19 stsp goto done;
2191 7848a0e1 2020-03-19 stsp }
2192 7848a0e1 2020-03-19 stsp }
2193 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2194 7848a0e1 2020-03-19 stsp if (error)
2195 7848a0e1 2020-03-19 stsp goto done;
2196 f79e6490 2020-04-19 stsp
2197 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2198 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2199 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2200 7848a0e1 2020-03-19 stsp
2201 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2202 9c52365f 2020-03-21 stsp server_path, verbosity);
2203 7848a0e1 2020-03-19 stsp if (error)
2204 7848a0e1 2020-03-19 stsp goto done;
2205 7848a0e1 2020-03-19 stsp
2206 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2207 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2208 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2209 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2210 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2211 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2212 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2213 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2214 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2215 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2216 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2217 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2218 7848a0e1 2020-03-19 stsp if (error)
2219 7848a0e1 2020-03-19 stsp goto done;
2220 7848a0e1 2020-03-19 stsp
2221 41b0de12 2020-03-21 stsp if (list_refs_only) {
2222 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2223 41b0de12 2020-03-21 stsp goto done;
2224 41b0de12 2020-03-21 stsp }
2225 41b0de12 2020-03-21 stsp
2226 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2227 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2228 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2229 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2230 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2231 984065c8 2020-03-19 stsp if (error)
2232 984065c8 2020-03-19 stsp goto done;
2233 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2234 984065c8 2020-03-19 stsp free(id_str);
2235 984065c8 2020-03-19 stsp id_str = NULL;
2236 984065c8 2020-03-19 stsp }
2237 7848a0e1 2020-03-19 stsp
2238 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2239 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2240 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2241 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2242 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2243 7848a0e1 2020-03-19 stsp char *remote_refname;
2244 7848a0e1 2020-03-19 stsp
2245 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2246 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2247 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2248 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2249 0e4002ca 2020-03-21 stsp if (error)
2250 0e4002ca 2020-03-21 stsp goto done;
2251 0e4002ca 2020-03-21 stsp continue;
2252 0e4002ca 2020-03-21 stsp }
2253 0e4002ca 2020-03-21 stsp
2254 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2255 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2256 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2257 7848a0e1 2020-03-19 stsp if (error) {
2258 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2259 7848a0e1 2020-03-19 stsp goto done;
2260 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2261 6338a6a1 2020-03-21 stsp repo);
2262 7848a0e1 2020-03-19 stsp if (error)
2263 7848a0e1 2020-03-19 stsp goto done;
2264 7848a0e1 2020-03-19 stsp } else {
2265 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2266 db6d8ad8 2020-03-21 stsp verbosity, repo);
2267 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2268 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2269 9f142382 2020-03-21 stsp error = unlock_err;
2270 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2271 7848a0e1 2020-03-19 stsp if (error)
2272 7848a0e1 2020-03-19 stsp goto done;
2273 7848a0e1 2020-03-19 stsp }
2274 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2275 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2276 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2277 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2278 7848a0e1 2020-03-19 stsp goto done;
2279 7848a0e1 2020-03-19 stsp }
2280 7848a0e1 2020-03-19 stsp
2281 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2282 7848a0e1 2020-03-19 stsp if (error) {
2283 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2284 7848a0e1 2020-03-19 stsp goto done;
2285 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2286 688f11b3 2020-03-21 stsp verbosity, repo);
2287 7848a0e1 2020-03-19 stsp if (error)
2288 7848a0e1 2020-03-19 stsp goto done;
2289 7848a0e1 2020-03-19 stsp } else {
2290 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2291 db6d8ad8 2020-03-21 stsp verbosity, repo);
2292 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2293 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2294 9f142382 2020-03-21 stsp error = unlock_err;
2295 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2296 7848a0e1 2020-03-19 stsp if (error)
2297 7848a0e1 2020-03-19 stsp goto done;
2298 7848a0e1 2020-03-19 stsp }
2299 2ec30c80 2020-03-20 stsp
2300 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2301 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2302 2ec30c80 2020-03-20 stsp if (error) {
2303 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2304 2ec30c80 2020-03-20 stsp goto done;
2305 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2306 6338a6a1 2020-03-21 stsp repo);
2307 2ec30c80 2020-03-20 stsp if (error)
2308 2ec30c80 2020-03-20 stsp goto done;
2309 9f142382 2020-03-21 stsp } else {
2310 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2311 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2312 9f142382 2020-03-21 stsp error = unlock_err;
2313 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2314 9f142382 2020-03-21 stsp }
2315 7848a0e1 2020-03-19 stsp }
2316 7848a0e1 2020-03-19 stsp }
2317 f1bcca34 2020-03-25 stsp if (delete_refs) {
2318 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2319 3789fd73 2020-03-26 stsp verbosity, repo);
2320 f1bcca34 2020-03-25 stsp if (error)
2321 f1bcca34 2020-03-25 stsp goto done;
2322 f1bcca34 2020-03-25 stsp }
2323 f1bcca34 2020-03-25 stsp
2324 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2325 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2326 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2327 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2328 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2329 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2330 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2331 f1bcca34 2020-03-25 stsp
2332 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2333 f1bcca34 2020-03-25 stsp continue;
2334 f1bcca34 2020-03-25 stsp
2335 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2336 f1bcca34 2020-03-25 stsp continue;
2337 f1bcca34 2020-03-25 stsp
2338 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2339 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2340 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2341 f1bcca34 2020-03-25 stsp goto done;
2342 f1bcca34 2020-03-25 stsp }
2343 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2344 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2345 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2346 f1bcca34 2020-03-25 stsp free(remote_refname);
2347 f1bcca34 2020-03-25 stsp goto done;
2348 f1bcca34 2020-03-25 stsp }
2349 f1bcca34 2020-03-25 stsp
2350 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2351 f1bcca34 2020-03-25 stsp 0);
2352 f1bcca34 2020-03-25 stsp if (error) {
2353 f1bcca34 2020-03-25 stsp free(remote_refname);
2354 f1bcca34 2020-03-25 stsp free(remote_target);
2355 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2356 f1bcca34 2020-03-25 stsp error = NULL;
2357 f1bcca34 2020-03-25 stsp continue;
2358 f1bcca34 2020-03-25 stsp }
2359 f1bcca34 2020-03-25 stsp goto done;
2360 f1bcca34 2020-03-25 stsp }
2361 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2362 f1bcca34 2020-03-25 stsp verbosity, repo);
2363 f1bcca34 2020-03-25 stsp free(remote_refname);
2364 f1bcca34 2020-03-25 stsp free(remote_target);
2365 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2366 f1bcca34 2020-03-25 stsp if (error)
2367 f1bcca34 2020-03-25 stsp goto done;
2368 f1bcca34 2020-03-25 stsp }
2369 f1bcca34 2020-03-25 stsp }
2370 7848a0e1 2020-03-19 stsp done:
2371 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2372 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2373 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2374 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2375 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2376 9c52365f 2020-03-21 stsp }
2377 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2378 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2379 7848a0e1 2020-03-19 stsp if (repo)
2380 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2381 7848a0e1 2020-03-19 stsp if (worktree)
2382 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2383 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2384 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2385 7848a0e1 2020-03-19 stsp free(pe->data);
2386 7848a0e1 2020-03-19 stsp }
2387 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2388 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2389 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2390 7848a0e1 2020-03-19 stsp free(pe->data);
2391 7848a0e1 2020-03-19 stsp }
2392 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2393 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2394 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2395 7848a0e1 2020-03-19 stsp free(id_str);
2396 7848a0e1 2020-03-19 stsp free(cwd);
2397 7848a0e1 2020-03-19 stsp free(repo_path);
2398 7848a0e1 2020-03-19 stsp free(pack_hash);
2399 7848a0e1 2020-03-19 stsp free(proto);
2400 7848a0e1 2020-03-19 stsp free(host);
2401 7848a0e1 2020-03-19 stsp free(port);
2402 7848a0e1 2020-03-19 stsp free(server_path);
2403 7848a0e1 2020-03-19 stsp free(repo_name);
2404 7848a0e1 2020-03-19 stsp return error;
2405 7848a0e1 2020-03-19 stsp }
2406 7848a0e1 2020-03-19 stsp
2407 7848a0e1 2020-03-19 stsp
2408 7848a0e1 2020-03-19 stsp __dead static void
2409 2ab43947 2020-03-18 stsp usage_checkout(void)
2410 2ab43947 2020-03-18 stsp {
2411 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2412 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2413 2ab43947 2020-03-18 stsp exit(1);
2414 2ab43947 2020-03-18 stsp }
2415 2ab43947 2020-03-18 stsp
2416 2ab43947 2020-03-18 stsp static void
2417 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2418 2ab43947 2020-03-18 stsp {
2419 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2420 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2421 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2422 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2423 2ab43947 2020-03-18 stsp getprogname());
2424 2ab43947 2020-03-18 stsp }
2425 2ab43947 2020-03-18 stsp
2426 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2427 2ab43947 2020-03-18 stsp const char *worktree_path;
2428 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2429 2ab43947 2020-03-18 stsp };
2430 2ab43947 2020-03-18 stsp
2431 2ab43947 2020-03-18 stsp static const struct got_error *
2432 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2433 2ab43947 2020-03-18 stsp {
2434 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2435 2ab43947 2020-03-18 stsp
2436 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2437 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2438 2ab43947 2020-03-18 stsp return NULL;
2439 2ab43947 2020-03-18 stsp
2440 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2441 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2442 2ab43947 2020-03-18 stsp return NULL;
2443 2ab43947 2020-03-18 stsp }
2444 2ab43947 2020-03-18 stsp
2445 2ab43947 2020-03-18 stsp while (path[0] == '/')
2446 2ab43947 2020-03-18 stsp path++;
2447 2ab43947 2020-03-18 stsp
2448 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2449 2ab43947 2020-03-18 stsp return NULL;
2450 2ab43947 2020-03-18 stsp }
2451 2ab43947 2020-03-18 stsp
2452 2ab43947 2020-03-18 stsp static const struct got_error *
2453 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2454 2ab43947 2020-03-18 stsp {
2455 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2456 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2457 2ab43947 2020-03-18 stsp return NULL;
2458 2ab43947 2020-03-18 stsp }
2459 2ab43947 2020-03-18 stsp
2460 2ab43947 2020-03-18 stsp static const struct got_error *
2461 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2462 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2463 2ab43947 2020-03-18 stsp struct got_repository *repo)
2464 2ab43947 2020-03-18 stsp {
2465 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2466 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2467 2ab43947 2020-03-18 stsp
2468 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2469 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2470 2ab43947 2020-03-18 stsp if (err)
2471 2ab43947 2020-03-18 stsp return err;
2472 2ab43947 2020-03-18 stsp
2473 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2474 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2475 2ab43947 2020-03-18 stsp
2476 2ab43947 2020-03-18 stsp /*
2477 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2478 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2479 2ab43947 2020-03-18 stsp *
2480 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2481 2ab43947 2020-03-18 stsp *
2482 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2483 2ab43947 2020-03-18 stsp * \ /
2484 2ab43947 2020-03-18 stsp * C E
2485 2ab43947 2020-03-18 stsp * \ /
2486 2ab43947 2020-03-18 stsp * B (yca)
2487 2ab43947 2020-03-18 stsp * |
2488 2ab43947 2020-03-18 stsp * A
2489 2ab43947 2020-03-18 stsp *
2490 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2491 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2492 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2493 2ab43947 2020-03-18 stsp */
2494 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2495 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2496 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2497 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2498 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2499 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2500 2ab43947 2020-03-18 stsp
2501 2ab43947 2020-03-18 stsp free(yca_id);
2502 2ab43947 2020-03-18 stsp return NULL;
2503 2ab43947 2020-03-18 stsp }
2504 2ab43947 2020-03-18 stsp
2505 2ab43947 2020-03-18 stsp static const struct got_error *
2506 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2507 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2508 2ab43947 2020-03-18 stsp struct got_repository *repo)
2509 2ab43947 2020-03-18 stsp {
2510 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2511 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2512 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2513 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2514 2ab43947 2020-03-18 stsp
2515 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2516 2ab43947 2020-03-18 stsp if (err)
2517 2ab43947 2020-03-18 stsp goto done;
2518 2ab43947 2020-03-18 stsp
2519 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2520 2ab43947 2020-03-18 stsp is_same_branch = 1;
2521 2ab43947 2020-03-18 stsp goto done;
2522 2ab43947 2020-03-18 stsp }
2523 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2524 2ab43947 2020-03-18 stsp is_same_branch = 1;
2525 2ab43947 2020-03-18 stsp goto done;
2526 2ab43947 2020-03-18 stsp }
2527 2ab43947 2020-03-18 stsp
2528 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2529 2ab43947 2020-03-18 stsp if (err)
2530 2ab43947 2020-03-18 stsp goto done;
2531 2ab43947 2020-03-18 stsp
2532 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2533 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2534 2ab43947 2020-03-18 stsp if (err)
2535 2ab43947 2020-03-18 stsp goto done;
2536 2ab43947 2020-03-18 stsp
2537 2ab43947 2020-03-18 stsp for (;;) {
2538 2ab43947 2020-03-18 stsp struct got_object_id *id;
2539 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2540 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2541 2ab43947 2020-03-18 stsp if (err) {
2542 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2543 2ab43947 2020-03-18 stsp err = NULL;
2544 2ab43947 2020-03-18 stsp break;
2545 2ab43947 2020-03-18 stsp }
2546 2ab43947 2020-03-18 stsp
2547 2ab43947 2020-03-18 stsp if (id) {
2548 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2549 2ab43947 2020-03-18 stsp break;
2550 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2551 2ab43947 2020-03-18 stsp is_same_branch = 1;
2552 2ab43947 2020-03-18 stsp break;
2553 2ab43947 2020-03-18 stsp }
2554 2ab43947 2020-03-18 stsp }
2555 2ab43947 2020-03-18 stsp }
2556 2ab43947 2020-03-18 stsp done:
2557 2ab43947 2020-03-18 stsp if (graph)
2558 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2559 2ab43947 2020-03-18 stsp free(head_commit_id);
2560 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2561 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2562 2ab43947 2020-03-18 stsp return err;
2563 a367ff0f 2019-05-14 stsp }
2564 8069f636 2019-01-12 stsp
2565 4ed7e80c 2018-05-20 stsp static const struct got_error *
2566 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2567 4b6c9460 2020-03-05 stsp {
2568 4b6c9460 2020-03-05 stsp static char msg[512];
2569 4b6c9460 2020-03-05 stsp const char *branch_name;
2570 4b6c9460 2020-03-05 stsp
2571 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2572 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2573 4b6c9460 2020-03-05 stsp else
2574 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2575 4b6c9460 2020-03-05 stsp
2576 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2577 4b6c9460 2020-03-05 stsp branch_name += 11;
2578 4b6c9460 2020-03-05 stsp
2579 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2580 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2581 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2582 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2583 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2584 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2585 4b6c9460 2020-03-05 stsp
2586 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2587 4b6c9460 2020-03-05 stsp }
2588 4b6c9460 2020-03-05 stsp
2589 4b6c9460 2020-03-05 stsp static const struct got_error *
2590 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2591 c09a553d 2018-03-12 stsp {
2592 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2593 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2594 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2595 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2596 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2597 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2598 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2599 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2600 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2601 c47340da 2020-09-20 stsp char *cwd = NULL, *path = NULL;
2602 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2603 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2604 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2605 f2ea84fa 2019-07-27 stsp
2606 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2607 c09a553d 2018-03-12 stsp
2608 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2609 0bb8a95e 2018-03-12 stsp switch (ch) {
2610 08573d5b 2019-05-14 stsp case 'b':
2611 08573d5b 2019-05-14 stsp branch_name = optarg;
2612 08573d5b 2019-05-14 stsp break;
2613 8069f636 2019-01-12 stsp case 'c':
2614 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2615 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2616 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2617 bb51a5b4 2020-01-13 stsp break;
2618 bb51a5b4 2020-01-13 stsp case 'E':
2619 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2620 8069f636 2019-01-12 stsp break;
2621 0bb8a95e 2018-03-12 stsp case 'p':
2622 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2623 0bb8a95e 2018-03-12 stsp break;
2624 0bb8a95e 2018-03-12 stsp default:
2625 2deda0b9 2019-03-07 stsp usage_checkout();
2626 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2627 0bb8a95e 2018-03-12 stsp }
2628 0bb8a95e 2018-03-12 stsp }
2629 0bb8a95e 2018-03-12 stsp
2630 0bb8a95e 2018-03-12 stsp argc -= optind;
2631 0bb8a95e 2018-03-12 stsp argv += optind;
2632 0bb8a95e 2018-03-12 stsp
2633 6715a751 2018-03-16 stsp #ifndef PROFILE
2634 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2635 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2636 c09a553d 2018-03-12 stsp err(1, "pledge");
2637 6715a751 2018-03-16 stsp #endif
2638 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2639 c47340da 2020-09-20 stsp char *base, *dotgit;
2640 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2641 76089277 2018-04-01 stsp if (repo_path == NULL)
2642 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2643 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2644 76089277 2018-04-01 stsp if (cwd == NULL) {
2645 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2646 76089277 2018-04-01 stsp goto done;
2647 76089277 2018-04-01 stsp }
2648 c47340da 2020-09-20 stsp if (path_prefix[0])
2649 c47340da 2020-09-20 stsp path = strdup(path_prefix);
2650 c47340da 2020-09-20 stsp else
2651 c47340da 2020-09-20 stsp path = strdup(repo_path);
2652 c47340da 2020-09-20 stsp if (path == NULL) {
2653 c47340da 2020-09-20 stsp error = got_error_from_errno("strdup");
2654 c47340da 2020-09-20 stsp goto done;
2655 c47340da 2020-09-20 stsp }
2656 c47340da 2020-09-20 stsp base = basename(path);
2657 c47340da 2020-09-20 stsp if (base == NULL) {
2658 c47340da 2020-09-20 stsp error = got_error_from_errno2("basename", path);
2659 c47340da 2020-09-20 stsp goto done;
2660 76089277 2018-04-01 stsp }
2661 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2662 c09a553d 2018-03-12 stsp if (dotgit)
2663 c09a553d 2018-03-12 stsp *dotgit = '\0';
2664 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2665 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2666 76089277 2018-04-01 stsp goto done;
2667 c09a553d 2018-03-12 stsp }
2668 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2669 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2670 76089277 2018-04-01 stsp if (repo_path == NULL) {
2671 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2672 76089277 2018-04-01 stsp goto done;
2673 76089277 2018-04-01 stsp }
2674 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2675 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2676 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2677 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2678 b4b3a7dd 2019-07-22 stsp argv[1]);
2679 b4b3a7dd 2019-07-22 stsp goto done;
2680 b4b3a7dd 2019-07-22 stsp }
2681 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2682 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2683 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2684 b4b3a7dd 2019-07-22 stsp goto done;
2685 b4b3a7dd 2019-07-22 stsp }
2686 76089277 2018-04-01 stsp }
2687 c09a553d 2018-03-12 stsp } else
2688 c09a553d 2018-03-12 stsp usage_checkout();
2689 c09a553d 2018-03-12 stsp
2690 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2691 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2692 13bfb272 2019-05-10 jcs
2693 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2694 c09a553d 2018-03-12 stsp if (error != NULL)
2695 c02c541e 2019-03-29 stsp goto done;
2696 c02c541e 2019-03-29 stsp
2697 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2698 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2699 c530dc23 2019-07-23 stsp if (error) {
2700 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2701 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2702 c530dc23 2019-07-23 stsp goto done;
2703 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2704 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2705 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2706 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2707 c530dc23 2019-07-23 stsp goto done;
2708 c530dc23 2019-07-23 stsp }
2709 c530dc23 2019-07-23 stsp }
2710 c530dc23 2019-07-23 stsp
2711 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2712 c02c541e 2019-03-29 stsp if (error)
2713 c09a553d 2018-03-12 stsp goto done;
2714 8069f636 2019-01-12 stsp
2715 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2716 c09a553d 2018-03-12 stsp if (error != NULL)
2717 c09a553d 2018-03-12 stsp goto done;
2718 c09a553d 2018-03-12 stsp
2719 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2720 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2721 c09a553d 2018-03-12 stsp goto done;
2722 c09a553d 2018-03-12 stsp
2723 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2724 c09a553d 2018-03-12 stsp if (error != NULL)
2725 c09a553d 2018-03-12 stsp goto done;
2726 c09a553d 2018-03-12 stsp
2727 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2728 e5dc7198 2018-12-29 stsp path_prefix);
2729 e5dc7198 2018-12-29 stsp if (error != NULL)
2730 e5dc7198 2018-12-29 stsp goto done;
2731 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2732 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2733 49520a32 2018-12-29 stsp goto done;
2734 49520a32 2018-12-29 stsp }
2735 49520a32 2018-12-29 stsp
2736 8069f636 2019-01-12 stsp if (commit_id_str) {
2737 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2738 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2739 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2740 30837e32 2019-07-25 stsp if (error)
2741 8069f636 2019-01-12 stsp goto done;
2742 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2743 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2744 8069f636 2019-01-12 stsp if (error != NULL) {
2745 8069f636 2019-01-12 stsp free(commit_id);
2746 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2747 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2748 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2749 4b6c9460 2020-03-05 stsp }
2750 8069f636 2019-01-12 stsp goto done;
2751 8069f636 2019-01-12 stsp }
2752 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2753 4b6c9460 2020-03-05 stsp if (error) {
2754 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2755 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2756 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2757 4b6c9460 2020-03-05 stsp }
2758 45d344f6 2019-05-14 stsp goto done;
2759 4b6c9460 2020-03-05 stsp }
2760 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2761 8069f636 2019-01-12 stsp commit_id);
2762 8069f636 2019-01-12 stsp free(commit_id);
2763 8069f636 2019-01-12 stsp if (error)
2764 8069f636 2019-01-12 stsp goto done;
2765 8069f636 2019-01-12 stsp }
2766 8069f636 2019-01-12 stsp
2767 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2768 f2ea84fa 2019-07-27 stsp if (error)
2769 f2ea84fa 2019-07-27 stsp goto done;
2770 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2771 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2772 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2773 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2774 c09a553d 2018-03-12 stsp if (error != NULL)
2775 c09a553d 2018-03-12 stsp goto done;
2776 c09a553d 2018-03-12 stsp
2777 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2778 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2779 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2780 c09a553d 2018-03-12 stsp done:
2781 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2782 8069f636 2019-01-12 stsp free(commit_id_str);
2783 76089277 2018-04-01 stsp free(repo_path);
2784 507dc3bb 2018-12-29 stsp free(worktree_path);
2785 c47340da 2020-09-20 stsp free(cwd);
2786 c47340da 2020-09-20 stsp free(path);
2787 507dc3bb 2018-12-29 stsp return error;
2788 9627c110 2020-04-18 stsp }
2789 9627c110 2020-04-18 stsp
2790 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2791 9627c110 2020-04-18 stsp int did_something;
2792 9627c110 2020-04-18 stsp int conflicts;
2793 9627c110 2020-04-18 stsp int obstructed;
2794 9627c110 2020-04-18 stsp int not_updated;
2795 9627c110 2020-04-18 stsp };
2796 9627c110 2020-04-18 stsp
2797 9627c110 2020-04-18 stsp void
2798 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2799 9627c110 2020-04-18 stsp {
2800 9627c110 2020-04-18 stsp if (!upa->did_something)
2801 9627c110 2020-04-18 stsp return;
2802 9627c110 2020-04-18 stsp
2803 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2804 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2805 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2806 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2807 9627c110 2020-04-18 stsp upa->obstructed);
2808 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2809 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2810 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2811 507dc3bb 2018-12-29 stsp }
2812 507dc3bb 2018-12-29 stsp
2813 507dc3bb 2018-12-29 stsp __dead static void
2814 507dc3bb 2018-12-29 stsp usage_update(void)
2815 507dc3bb 2018-12-29 stsp {
2816 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2817 507dc3bb 2018-12-29 stsp getprogname());
2818 507dc3bb 2018-12-29 stsp exit(1);
2819 507dc3bb 2018-12-29 stsp }
2820 507dc3bb 2018-12-29 stsp
2821 1ee397ad 2019-07-12 stsp static const struct got_error *
2822 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2823 507dc3bb 2018-12-29 stsp {
2824 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2825 784955db 2019-01-12 stsp
2826 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2827 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2828 1ee397ad 2019-07-12 stsp return NULL;
2829 507dc3bb 2018-12-29 stsp
2830 9627c110 2020-04-18 stsp upa->did_something = 1;
2831 a484d721 2019-06-10 stsp
2832 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2833 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2834 1ee397ad 2019-07-12 stsp return NULL;
2835 a484d721 2019-06-10 stsp
2836 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2837 9627c110 2020-04-18 stsp upa->conflicts++;
2838 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2839 9627c110 2020-04-18 stsp upa->obstructed++;
2840 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2841 9627c110 2020-04-18 stsp upa->not_updated++;
2842 9627c110 2020-04-18 stsp
2843 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2844 507dc3bb 2018-12-29 stsp path++;
2845 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2846 1ee397ad 2019-07-12 stsp return NULL;
2847 be7061eb 2018-12-30 stsp }
2848 be7061eb 2018-12-30 stsp
2849 be7061eb 2018-12-30 stsp static const struct got_error *
2850 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2851 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2852 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2853 a1fb16d8 2019-05-24 stsp {
2854 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2855 a1fb16d8 2019-05-24 stsp char *base_id_str;
2856 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2857 a1fb16d8 2019-05-24 stsp
2858 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2859 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2860 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2861 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2862 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2863 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2864 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2865 a1fb16d8 2019-05-24 stsp }
2866 a1fb16d8 2019-05-24 stsp
2867 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2868 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2869 a1fb16d8 2019-05-24 stsp if (err) {
2870 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2871 a1fb16d8 2019-05-24 stsp return err;
2872 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2873 a1fb16d8 2019-05-24 stsp }
2874 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2875 a1fb16d8 2019-05-24 stsp return NULL;
2876 a1fb16d8 2019-05-24 stsp
2877 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2878 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2879 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2880 a1fb16d8 2019-05-24 stsp if (err)
2881 a1fb16d8 2019-05-24 stsp return err;
2882 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2883 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2884 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2885 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2886 0ebf8283 2019-07-24 stsp return NULL;
2887 0ebf8283 2019-07-24 stsp }
2888 0ebf8283 2019-07-24 stsp
2889 0ebf8283 2019-07-24 stsp static const struct got_error *
2890 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2891 0ebf8283 2019-07-24 stsp {
2892 0ebf8283 2019-07-24 stsp const struct got_error *err;
2893 0ebf8283 2019-07-24 stsp int in_progress;
2894 0ebf8283 2019-07-24 stsp
2895 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2896 0ebf8283 2019-07-24 stsp if (err)
2897 0ebf8283 2019-07-24 stsp return err;
2898 0ebf8283 2019-07-24 stsp if (in_progress)
2899 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2900 0ebf8283 2019-07-24 stsp
2901 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2902 0ebf8283 2019-07-24 stsp if (err)
2903 0ebf8283 2019-07-24 stsp return err;
2904 0ebf8283 2019-07-24 stsp if (in_progress)
2905 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2906 0ebf8283 2019-07-24 stsp
2907 a1fb16d8 2019-05-24 stsp return NULL;
2908 a5edda0a 2019-07-27 stsp }
2909 a5edda0a 2019-07-27 stsp
2910 a5edda0a 2019-07-27 stsp static const struct got_error *
2911 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2912 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2913 a5edda0a 2019-07-27 stsp {
2914 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2915 a5edda0a 2019-07-27 stsp char *path;
2916 a5edda0a 2019-07-27 stsp int i;
2917 a5edda0a 2019-07-27 stsp
2918 a5edda0a 2019-07-27 stsp if (argc == 0) {
2919 a5edda0a 2019-07-27 stsp path = strdup("");
2920 a5edda0a 2019-07-27 stsp if (path == NULL)
2921 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2922 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2923 a5edda0a 2019-07-27 stsp }
2924 a5edda0a 2019-07-27 stsp
2925 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2926 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2927 a5edda0a 2019-07-27 stsp if (err)
2928 a5edda0a 2019-07-27 stsp break;
2929 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2930 a5edda0a 2019-07-27 stsp if (err) {
2931 a5edda0a 2019-07-27 stsp free(path);
2932 a5edda0a 2019-07-27 stsp break;
2933 a5edda0a 2019-07-27 stsp }
2934 a5edda0a 2019-07-27 stsp }
2935 a5edda0a 2019-07-27 stsp
2936 a5edda0a 2019-07-27 stsp return err;
2937 a1fb16d8 2019-05-24 stsp }
2938 a1fb16d8 2019-05-24 stsp
2939 a1fb16d8 2019-05-24 stsp static const struct got_error *
2940 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2941 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2942 fa51e947 2020-03-27 stsp {
2943 fa51e947 2020-03-27 stsp const struct got_error *err;
2944 fa51e947 2020-03-27 stsp struct got_repository *repo;
2945 fa51e947 2020-03-27 stsp static char msg[512];
2946 fa51e947 2020-03-27 stsp
2947 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2948 fa51e947 2020-03-27 stsp if (err)
2949 fa51e947 2020-03-27 stsp return orig_err;
2950 fa51e947 2020-03-27 stsp
2951 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2952 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2953 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2954 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2955 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2956 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2957 fa51e947 2020-03-27 stsp got_repo_close(repo);
2958 fa51e947 2020-03-27 stsp return err;
2959 fa51e947 2020-03-27 stsp }
2960 fa51e947 2020-03-27 stsp
2961 fa51e947 2020-03-27 stsp static const struct got_error *
2962 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2963 507dc3bb 2018-12-29 stsp {
2964 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2965 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2966 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2967 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2968 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2969 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2970 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2971 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2972 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2973 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2974 9627c110 2020-04-18 stsp int ch;
2975 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2976 507dc3bb 2018-12-29 stsp
2977 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2978 f2ea84fa 2019-07-27 stsp
2979 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2980 507dc3bb 2018-12-29 stsp switch (ch) {
2981 024e9686 2019-05-14 stsp case 'b':
2982 024e9686 2019-05-14 stsp branch_name = optarg;
2983 024e9686 2019-05-14 stsp break;
2984 507dc3bb 2018-12-29 stsp case 'c':
2985 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2986 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2987 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2988 507dc3bb 2018-12-29 stsp break;
2989 507dc3bb 2018-12-29 stsp default:
2990 2deda0b9 2019-03-07 stsp usage_update();
2991 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2992 507dc3bb 2018-12-29 stsp }
2993 507dc3bb 2018-12-29 stsp }
2994 507dc3bb 2018-12-29 stsp
2995 507dc3bb 2018-12-29 stsp argc -= optind;
2996 507dc3bb 2018-12-29 stsp argv += optind;
2997 507dc3bb 2018-12-29 stsp
2998 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2999 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3000 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3001 507dc3bb 2018-12-29 stsp err(1, "pledge");
3002 507dc3bb 2018-12-29 stsp #endif
3003 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3004 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3005 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3006 c4cdcb68 2019-04-03 stsp goto done;
3007 c4cdcb68 2019-04-03 stsp }
3008 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3009 fa51e947 2020-03-27 stsp if (error) {
3010 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3011 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3012 fa51e947 2020-03-27 stsp worktree_path);
3013 7d5807f4 2019-07-11 stsp goto done;
3014 fa51e947 2020-03-27 stsp }
3015 7d5807f4 2019-07-11 stsp
3016 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3017 f2ea84fa 2019-07-27 stsp if (error)
3018 f2ea84fa 2019-07-27 stsp goto done;
3019 507dc3bb 2018-12-29 stsp
3020 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3021 c9956ddf 2019-09-08 stsp NULL);
3022 507dc3bb 2018-12-29 stsp if (error != NULL)
3023 507dc3bb 2018-12-29 stsp goto done;
3024 507dc3bb 2018-12-29 stsp
3025 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3026 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3027 087fb88c 2019-08-04 stsp if (error)
3028 087fb88c 2019-08-04 stsp goto done;
3029 087fb88c 2019-08-04 stsp
3030 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3031 0266afb7 2019-01-04 stsp if (error)
3032 0266afb7 2019-01-04 stsp goto done;
3033 0266afb7 2019-01-04 stsp
3034 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3035 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3036 024e9686 2019-05-14 stsp if (error != NULL)
3037 024e9686 2019-05-14 stsp goto done;
3038 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3039 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3040 9c4b8182 2019-01-02 stsp if (error != NULL)
3041 9c4b8182 2019-01-02 stsp goto done;
3042 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3043 507dc3bb 2018-12-29 stsp if (error != NULL)
3044 507dc3bb 2018-12-29 stsp goto done;
3045 507dc3bb 2018-12-29 stsp } else {
3046 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3047 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3048 04f57cb3 2019-07-25 stsp free(commit_id_str);
3049 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3050 30837e32 2019-07-25 stsp if (error)
3051 a297e751 2019-07-11 stsp goto done;
3052 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3053 a297e751 2019-07-11 stsp if (error)
3054 507dc3bb 2018-12-29 stsp goto done;
3055 507dc3bb 2018-12-29 stsp }
3056 35c965b2 2018-12-29 stsp
3057 a1fb16d8 2019-05-24 stsp if (branch_name) {
3058 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3059 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3060 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3061 f2ea84fa 2019-07-27 stsp continue;
3062 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3063 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3064 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3065 024e9686 2019-05-14 stsp goto done;
3066 024e9686 2019-05-14 stsp }
3067 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3068 024e9686 2019-05-14 stsp if (error)
3069 024e9686 2019-05-14 stsp goto done;
3070 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3071 3aef623b 2019-10-15 stsp repo);
3072 a367ff0f 2019-05-14 stsp free(head_commit_id);
3073 024e9686 2019-05-14 stsp if (error != NULL)
3074 024e9686 2019-05-14 stsp goto done;
3075 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3076 a367ff0f 2019-05-14 stsp if (error)
3077 a367ff0f 2019-05-14 stsp goto done;
3078 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3079 024e9686 2019-05-14 stsp if (error)
3080 024e9686 2019-05-14 stsp goto done;
3081 024e9686 2019-05-14 stsp } else {
3082 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3083 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3084 a367ff0f 2019-05-14 stsp if (error != NULL) {
3085 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3086 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3087 024e9686 2019-05-14 stsp goto done;
3088 a367ff0f 2019-05-14 stsp }
3089 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3090 a367ff0f 2019-05-14 stsp if (error)
3091 a367ff0f 2019-05-14 stsp goto done;
3092 024e9686 2019-05-14 stsp }
3093 507dc3bb 2018-12-29 stsp
3094 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3095 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3096 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3097 507dc3bb 2018-12-29 stsp commit_id);
3098 507dc3bb 2018-12-29 stsp if (error)
3099 507dc3bb 2018-12-29 stsp goto done;
3100 507dc3bb 2018-12-29 stsp }
3101 507dc3bb 2018-12-29 stsp
3102 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3103 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3104 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3105 507dc3bb 2018-12-29 stsp if (error != NULL)
3106 507dc3bb 2018-12-29 stsp goto done;
3107 9c4b8182 2019-01-02 stsp
3108 9627c110 2020-04-18 stsp if (upa.did_something)
3109 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3110 784955db 2019-01-12 stsp else
3111 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3112 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3113 507dc3bb 2018-12-29 stsp done:
3114 c09a553d 2018-03-12 stsp free(worktree_path);
3115 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3116 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3117 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3118 507dc3bb 2018-12-29 stsp free(commit_id);
3119 9c4b8182 2019-01-02 stsp free(commit_id_str);
3120 c09a553d 2018-03-12 stsp return error;
3121 c09a553d 2018-03-12 stsp }
3122 c09a553d 2018-03-12 stsp
3123 f42b1b34 2018-03-12 stsp static const struct got_error *
3124 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3125 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3126 63035f9f 2019-10-06 stsp struct got_repository *repo)
3127 5c860e29 2018-03-12 stsp {
3128 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3129 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3130 79109fed 2018-03-27 stsp
3131 44392932 2019-08-25 stsp if (blob_id1) {
3132 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3133 44392932 2019-08-25 stsp if (err)
3134 44392932 2019-08-25 stsp goto done;
3135 44392932 2019-08-25 stsp }
3136 79109fed 2018-03-27 stsp
3137 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3138 44392932 2019-08-25 stsp if (err)
3139 44392932 2019-08-25 stsp goto done;
3140 79109fed 2018-03-27 stsp
3141 44392932 2019-08-25 stsp while (path[0] == '/')
3142 44392932 2019-08-25 stsp path++;
3143 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
3144 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
3145 44392932 2019-08-25 stsp done:
3146 44392932 2019-08-25 stsp if (blob1)
3147 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3148 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3149 44392932 2019-08-25 stsp return err;
3150 44392932 2019-08-25 stsp }
3151 44392932 2019-08-25 stsp
3152 44392932 2019-08-25 stsp static const struct got_error *
3153 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3154 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3155 63035f9f 2019-10-06 stsp struct got_repository *repo)
3156 44392932 2019-08-25 stsp {
3157 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3158 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3159 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3160 44392932 2019-08-25 stsp
3161 44392932 2019-08-25 stsp if (tree_id1) {
3162 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3163 79109fed 2018-03-27 stsp if (err)
3164 44392932 2019-08-25 stsp goto done;
3165 79109fed 2018-03-27 stsp }
3166 79109fed 2018-03-27 stsp
3167 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3168 0f2b3dca 2018-12-22 stsp if (err)
3169 0f2b3dca 2018-12-22 stsp goto done;
3170 0f2b3dca 2018-12-22 stsp
3171 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3172 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3173 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3174 44392932 2019-08-25 stsp while (path[0] == '/')
3175 44392932 2019-08-25 stsp path++;
3176 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3177 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3178 0f2b3dca 2018-12-22 stsp done:
3179 79109fed 2018-03-27 stsp if (tree1)
3180 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3181 366e0a5f 2019-10-10 stsp if (tree2)
3182 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3183 44392932 2019-08-25 stsp return err;
3184 44392932 2019-08-25 stsp }
3185 44392932 2019-08-25 stsp
3186 44392932 2019-08-25 stsp static const struct got_error *
3187 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3188 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3189 0208f208 2020-05-05 stsp {
3190 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3191 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3192 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3193 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3194 0208f208 2020-05-05 stsp
3195 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3196 0208f208 2020-05-05 stsp if (qid != NULL) {
3197 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3198 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3199 0208f208 2020-05-05 stsp qid->id);
3200 0208f208 2020-05-05 stsp if (err)
3201 0208f208 2020-05-05 stsp return err;
3202 0208f208 2020-05-05 stsp
3203 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3204 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3205 0208f208 2020-05-05 stsp
3206 0208f208 2020-05-05 stsp }
3207 0208f208 2020-05-05 stsp
3208 0208f208 2020-05-05 stsp if (tree_id1) {
3209 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3210 0208f208 2020-05-05 stsp if (err)
3211 0208f208 2020-05-05 stsp goto done;
3212 0208f208 2020-05-05 stsp }
3213 0208f208 2020-05-05 stsp
3214 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3215 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3216 0208f208 2020-05-05 stsp if (err)
3217 0208f208 2020-05-05 stsp goto done;
3218 0208f208 2020-05-05 stsp
3219 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3220 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3221 0208f208 2020-05-05 stsp done:
3222 0208f208 2020-05-05 stsp if (tree1)
3223 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3224 0208f208 2020-05-05 stsp if (tree2)
3225 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3226 0208f208 2020-05-05 stsp return err;
3227 0208f208 2020-05-05 stsp }
3228 0208f208 2020-05-05 stsp
3229 0208f208 2020-05-05 stsp static const struct got_error *
3230 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3231 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3232 44392932 2019-08-25 stsp {
3233 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3234 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3235 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3236 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3237 44392932 2019-08-25 stsp struct got_object_qid *qid;
3238 44392932 2019-08-25 stsp
3239 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3240 44392932 2019-08-25 stsp if (qid != NULL) {
3241 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3242 44392932 2019-08-25 stsp qid->id);
3243 44392932 2019-08-25 stsp if (err)
3244 44392932 2019-08-25 stsp return err;
3245 44392932 2019-08-25 stsp }
3246 44392932 2019-08-25 stsp
3247 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3248 44392932 2019-08-25 stsp int obj_type;
3249 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3250 44392932 2019-08-25 stsp if (err)
3251 44392932 2019-08-25 stsp goto done;
3252 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3253 44392932 2019-08-25 stsp if (err) {
3254 44392932 2019-08-25 stsp free(obj_id2);
3255 44392932 2019-08-25 stsp goto done;
3256 44392932 2019-08-25 stsp }
3257 44392932 2019-08-25 stsp if (pcommit) {
3258 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3259 44392932 2019-08-25 stsp qid->id, path);
3260 44392932 2019-08-25 stsp if (err) {
3261 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3262 2e8c69d1 2020-05-04 stsp free(obj_id2);
3263 2e8c69d1 2020-05-04 stsp goto done;
3264 2e8c69d1 2020-05-04 stsp }
3265 2e8c69d1 2020-05-04 stsp } else {
3266 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3267 2e8c69d1 2020-05-04 stsp if (err) {
3268 2e8c69d1 2020-05-04 stsp free(obj_id2);
3269 2e8c69d1 2020-05-04 stsp goto done;
3270 2e8c69d1 2020-05-04 stsp }
3271 44392932 2019-08-25 stsp }
3272 44392932 2019-08-25 stsp }
3273 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3274 44392932 2019-08-25 stsp if (err) {
3275 44392932 2019-08-25 stsp free(obj_id2);
3276 44392932 2019-08-25 stsp goto done;
3277 44392932 2019-08-25 stsp }
3278 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3279 44392932 2019-08-25 stsp switch (obj_type) {
3280 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3281 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3282 63035f9f 2019-10-06 stsp 0, repo);
3283 44392932 2019-08-25 stsp break;
3284 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3285 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3286 63035f9f 2019-10-06 stsp 0, repo);
3287 44392932 2019-08-25 stsp break;
3288 44392932 2019-08-25 stsp default:
3289 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3290 44392932 2019-08-25 stsp break;
3291 44392932 2019-08-25 stsp }
3292 44392932 2019-08-25 stsp free(obj_id1);
3293 44392932 2019-08-25 stsp free(obj_id2);
3294 44392932 2019-08-25 stsp } else {
3295 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3296 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3297 44392932 2019-08-25 stsp if (err)
3298 44392932 2019-08-25 stsp goto done;
3299 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3300 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
3301 44392932 2019-08-25 stsp if (err)
3302 44392932 2019-08-25 stsp goto done;
3303 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3304 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3305 44392932 2019-08-25 stsp }
3306 44392932 2019-08-25 stsp done:
3307 0f2b3dca 2018-12-22 stsp free(id_str1);
3308 0f2b3dca 2018-12-22 stsp free(id_str2);
3309 44392932 2019-08-25 stsp if (pcommit)
3310 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3311 79109fed 2018-03-27 stsp return err;
3312 79109fed 2018-03-27 stsp }
3313 79109fed 2018-03-27 stsp
3314 4bb494d5 2018-06-16 stsp static char *
3315 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3316 6c281f94 2018-06-11 stsp {
3317 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3318 09867e48 2019-08-13 stsp char *p, *s;
3319 09867e48 2019-08-13 stsp
3320 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3321 09867e48 2019-08-13 stsp if (tm == NULL)
3322 09867e48 2019-08-13 stsp return NULL;
3323 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3324 09867e48 2019-08-13 stsp if (s == NULL)
3325 09867e48 2019-08-13 stsp return NULL;
3326 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3327 6c281f94 2018-06-11 stsp if (p)
3328 6c281f94 2018-06-11 stsp *p = '\0';
3329 6c281f94 2018-06-11 stsp return s;
3330 6c281f94 2018-06-11 stsp }
3331 dc424a06 2019-08-07 stsp
3332 6841bf13 2019-11-29 kn static const struct got_error *
3333 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3334 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3335 6841bf13 2019-11-29 kn {
3336 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3337 6841bf13 2019-11-29 kn regmatch_t regmatch;
3338 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3339 6841bf13 2019-11-29 kn
3340 6841bf13 2019-11-29 kn *have_match = 0;
3341 6841bf13 2019-11-29 kn
3342 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3343 6841bf13 2019-11-29 kn if (err)
3344 6841bf13 2019-11-29 kn return err;
3345 6841bf13 2019-11-29 kn
3346 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3347 6841bf13 2019-11-29 kn if (err)
3348 6841bf13 2019-11-29 kn goto done;
3349 6841bf13 2019-11-29 kn
3350 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3351 6841bf13 2019-11-29 kn *have_match = 1;
3352 6841bf13 2019-11-29 kn done:
3353 6841bf13 2019-11-29 kn free(id_str);
3354 6841bf13 2019-11-29 kn free(logmsg);
3355 6841bf13 2019-11-29 kn return err;
3356 6841bf13 2019-11-29 kn }
3357 6841bf13 2019-11-29 kn
3358 0208f208 2020-05-05 stsp static void
3359 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3360 0208f208 2020-05-05 stsp regex_t *regex)
3361 0208f208 2020-05-05 stsp {
3362 0208f208 2020-05-05 stsp regmatch_t regmatch;
3363 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3364 0208f208 2020-05-05 stsp
3365 0208f208 2020-05-05 stsp *have_match = 0;
3366 0208f208 2020-05-05 stsp
3367 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3368 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3369 0208f208 2020-05-05 stsp *have_match = 1;
3370 0208f208 2020-05-05 stsp break;
3371 0208f208 2020-05-05 stsp }
3372 0208f208 2020-05-05 stsp }
3373 0208f208 2020-05-05 stsp }
3374 0208f208 2020-05-05 stsp
3375 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3376 6c281f94 2018-06-11 stsp
3377 79109fed 2018-03-27 stsp static const struct got_error *
3378 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3379 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3380 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3381 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3382 79109fed 2018-03-27 stsp {
3383 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3384 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3385 6c281f94 2018-06-11 stsp char datebuf[26];
3386 45d799e2 2018-12-23 stsp time_t committer_time;
3387 45d799e2 2018-12-23 stsp const char *author, *committer;
3388 199a4027 2019-02-02 stsp char *refs_str = NULL;
3389 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3390 5c860e29 2018-03-12 stsp
3391 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3392 199a4027 2019-02-02 stsp char *s;
3393 199a4027 2019-02-02 stsp const char *name;
3394 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3395 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3396 a436ad14 2019-08-13 stsp int cmp;
3397 a436ad14 2019-08-13 stsp
3398 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3399 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3400 d9498b20 2019-02-05 stsp continue;
3401 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3402 199a4027 2019-02-02 stsp name += 5;
3403 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3404 7143d404 2019-03-12 stsp continue;
3405 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3406 e34f9ed6 2019-02-02 stsp name += 6;
3407 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3408 141c2bff 2019-02-04 stsp name += 8;
3409 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3410 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3411 4343a07f 2020-04-24 stsp continue;
3412 4343a07f 2020-04-24 stsp }
3413 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3414 48cae60d 2020-09-22 stsp if (err)
3415 48cae60d 2020-09-22 stsp return err;
3416 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3417 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3418 5d844a1e 2019-08-13 stsp if (err) {
3419 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3420 48cae60d 2020-09-22 stsp free(ref_id);
3421 5d844a1e 2019-08-13 stsp return err;
3422 48cae60d 2020-09-22 stsp }
3423 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3424 5d844a1e 2019-08-13 stsp err = NULL;
3425 5d844a1e 2019-08-13 stsp tag = NULL;
3426 5d844a1e 2019-08-13 stsp }
3427 a436ad14 2019-08-13 stsp }
3428 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3429 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3430 48cae60d 2020-09-22 stsp free(ref_id);
3431 a436ad14 2019-08-13 stsp if (tag)
3432 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3433 a436ad14 2019-08-13 stsp if (cmp != 0)
3434 a436ad14 2019-08-13 stsp continue;
3435 199a4027 2019-02-02 stsp s = refs_str;
3436 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3437 199a4027 2019-02-02 stsp name) == -1) {
3438 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3439 199a4027 2019-02-02 stsp free(s);
3440 34ca4898 2019-08-13 stsp return err;
3441 199a4027 2019-02-02 stsp }
3442 199a4027 2019-02-02 stsp free(s);
3443 199a4027 2019-02-02 stsp }
3444 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3445 8bf5b3c9 2018-03-17 stsp if (err)
3446 8bf5b3c9 2018-03-17 stsp return err;
3447 788c352e 2018-06-16 stsp
3448 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3449 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3450 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3451 832c249c 2018-06-10 stsp free(id_str);
3452 d3d493d7 2019-02-21 stsp id_str = NULL;
3453 d3d493d7 2019-02-21 stsp free(refs_str);
3454 d3d493d7 2019-02-21 stsp refs_str = NULL;
3455 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3456 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3457 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3458 09867e48 2019-08-13 stsp if (datestr)
3459 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3460 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3461 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3462 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3463 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3464 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3465 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3466 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3467 3fe1abad 2018-06-10 stsp int n = 1;
3468 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3469 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3470 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3471 a0603db2 2018-06-10 stsp if (err)
3472 a0603db2 2018-06-10 stsp return err;
3473 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3474 a0603db2 2018-06-10 stsp free(id_str);
3475 a0603db2 2018-06-10 stsp }
3476 a0603db2 2018-06-10 stsp }
3477 832c249c 2018-06-10 stsp
3478 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3479 5943eee2 2019-08-13 stsp if (err)
3480 5943eee2 2019-08-13 stsp return err;
3481 8bf5b3c9 2018-03-17 stsp
3482 621015ac 2018-07-23 stsp logmsg = logmsg0;
3483 832c249c 2018-06-10 stsp do {
3484 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3485 832c249c 2018-06-10 stsp if (line)
3486 832c249c 2018-06-10 stsp printf(" %s\n", line);
3487 832c249c 2018-06-10 stsp } while (line);
3488 621015ac 2018-07-23 stsp free(logmsg0);
3489 832c249c 2018-06-10 stsp
3490 0208f208 2020-05-05 stsp if (changed_paths) {
3491 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3492 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3493 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3494 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3495 0208f208 2020-05-05 stsp }
3496 0208f208 2020-05-05 stsp printf("\n");
3497 0208f208 2020-05-05 stsp }
3498 971751ac 2018-03-27 stsp if (show_patch) {
3499 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3500 971751ac 2018-03-27 stsp if (err == 0)
3501 971751ac 2018-03-27 stsp printf("\n");
3502 971751ac 2018-03-27 stsp }
3503 07862c20 2018-09-15 stsp
3504 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3505 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3506 07862c20 2018-09-15 stsp return err;
3507 f42b1b34 2018-03-12 stsp }
3508 5c860e29 2018-03-12 stsp
3509 f42b1b34 2018-03-12 stsp static const struct got_error *
3510 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3511 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3512 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3513 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3514 f42b1b34 2018-03-12 stsp {
3515 f42b1b34 2018-03-12 stsp const struct got_error *err;
3516 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3517 6841bf13 2019-11-29 kn regex_t regex;
3518 6841bf13 2019-11-29 kn int have_match;
3519 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3520 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3521 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3522 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3523 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3524 dbec59df 2020-04-18 stsp
3525 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3526 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3527 372ccdbb 2018-06-10 stsp
3528 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3529 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3530 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3531 6841bf13 2019-11-29 kn
3532 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3533 f42b1b34 2018-03-12 stsp if (err)
3534 f42b1b34 2018-03-12 stsp return err;
3535 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3536 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3537 372ccdbb 2018-06-10 stsp if (err)
3538 fcc85cad 2018-07-22 stsp goto done;
3539 656b1f76 2019-05-11 jcs for (;;) {
3540 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3541 8bf5b3c9 2018-03-17 stsp
3542 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3543 84453469 2018-11-11 stsp break;
3544 84453469 2018-11-11 stsp
3545 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3546 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3547 372ccdbb 2018-06-10 stsp if (err) {
3548 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3549 9ba79e04 2018-06-11 stsp err = NULL;
3550 ee780d5c 2020-01-04 stsp break;
3551 7e665116 2018-04-02 stsp }
3552 b43fbaa0 2018-06-11 stsp if (id == NULL)
3553 7e665116 2018-04-02 stsp break;
3554 b43fbaa0 2018-06-11 stsp
3555 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3556 b43fbaa0 2018-06-11 stsp if (err)
3557 fcc85cad 2018-07-22 stsp break;
3558 6841bf13 2019-11-29 kn
3559 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3560 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3561 0208f208 2020-05-05 stsp if (err)
3562 0208f208 2020-05-05 stsp break;
3563 0208f208 2020-05-05 stsp }
3564 0208f208 2020-05-05 stsp
3565 6841bf13 2019-11-29 kn if (search_pattern) {
3566 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3567 6841bf13 2019-11-29 kn if (err) {
3568 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3569 6841bf13 2019-11-29 kn break;
3570 6841bf13 2019-11-29 kn }
3571 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3572 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3573 0208f208 2020-05-05 stsp &changed_paths, &regex);
3574 6841bf13 2019-11-29 kn if (have_match == 0) {
3575 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3576 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3577 0208f208 2020-05-05 stsp free((char *)pe->path);
3578 0208f208 2020-05-05 stsp free(pe->data);
3579 0208f208 2020-05-05 stsp }
3580 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3581 6841bf13 2019-11-29 kn continue;
3582 6841bf13 2019-11-29 kn }
3583 6841bf13 2019-11-29 kn }
3584 6841bf13 2019-11-29 kn
3585 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3586 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3587 dbec59df 2020-04-18 stsp if (err)
3588 dbec59df 2020-04-18 stsp break;
3589 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3590 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3591 dbec59df 2020-04-18 stsp } else {
3592 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3593 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3594 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3595 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3596 dbec59df 2020-04-18 stsp if (err)
3597 dbec59df 2020-04-18 stsp break;
3598 dbec59df 2020-04-18 stsp }
3599 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3600 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3601 7e665116 2018-04-02 stsp break;
3602 0208f208 2020-05-05 stsp
3603 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3604 0208f208 2020-05-05 stsp free((char *)pe->path);
3605 0208f208 2020-05-05 stsp free(pe->data);
3606 0208f208 2020-05-05 stsp }
3607 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3608 31cedeaf 2018-09-15 stsp }
3609 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3610 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3611 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3612 dbec59df 2020-04-18 stsp if (err)
3613 dbec59df 2020-04-18 stsp break;
3614 502b9684 2020-07-31 stsp if (show_changed_paths) {
3615 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3616 502b9684 2020-07-31 stsp commit, repo);
3617 502b9684 2020-07-31 stsp if (err)
3618 502b9684 2020-07-31 stsp break;
3619 502b9684 2020-07-31 stsp }
3620 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3621 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3622 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3623 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3624 dbec59df 2020-04-18 stsp if (err)
3625 dbec59df 2020-04-18 stsp break;
3626 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3627 502b9684 2020-07-31 stsp free((char *)pe->path);
3628 502b9684 2020-07-31 stsp free(pe->data);
3629 502b9684 2020-07-31 stsp }
3630 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3631 dbec59df 2020-04-18 stsp }
3632 dbec59df 2020-04-18 stsp }
3633 fcc85cad 2018-07-22 stsp done:
3634 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3635 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3636 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3637 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3638 dbec59df 2020-04-18 stsp }
3639 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3640 0208f208 2020-05-05 stsp free((char *)pe->path);
3641 0208f208 2020-05-05 stsp free(pe->data);
3642 0208f208 2020-05-05 stsp }
3643 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3644 6841bf13 2019-11-29 kn if (search_pattern)
3645 6841bf13 2019-11-29 kn regfree(&regex);
3646 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3647 f42b1b34 2018-03-12 stsp return err;
3648 f42b1b34 2018-03-12 stsp }
3649 5c860e29 2018-03-12 stsp
3650 4ed7e80c 2018-05-20 stsp __dead static void
3651 6f3d1eb0 2018-03-12 stsp usage_log(void)
3652 6f3d1eb0 2018-03-12 stsp {
3653 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3654 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3655 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3656 6f3d1eb0 2018-03-12 stsp exit(1);
3657 b1ebc001 2019-08-13 stsp }
3658 b1ebc001 2019-08-13 stsp
3659 b1ebc001 2019-08-13 stsp static int
3660 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3661 b1ebc001 2019-08-13 stsp {
3662 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3663 b1ebc001 2019-08-13 stsp long long n;
3664 b1ebc001 2019-08-13 stsp const char *errstr;
3665 b1ebc001 2019-08-13 stsp
3666 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3667 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3668 b1ebc001 2019-08-13 stsp return 0;
3669 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3670 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3671 b1ebc001 2019-08-13 stsp return 0;
3672 b1ebc001 2019-08-13 stsp return n;
3673 d1fe46f9 2020-04-18 stsp }
3674 d1fe46f9 2020-04-18 stsp
3675 d1fe46f9 2020-04-18 stsp static const struct got_error *
3676 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3677 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3678 d1fe46f9 2020-04-18 stsp {
3679 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3680 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3681 d1fe46f9 2020-04-18 stsp
3682 d1fe46f9 2020-04-18 stsp *id = NULL;
3683 d1fe46f9 2020-04-18 stsp
3684 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3685 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3686 d1fe46f9 2020-04-18 stsp int obj_type;
3687 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3688 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3689 d1fe46f9 2020-04-18 stsp if (err)
3690 d1fe46f9 2020-04-18 stsp return err;
3691 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3692 d1fe46f9 2020-04-18 stsp if (err)
3693 d1fe46f9 2020-04-18 stsp return err;
3694 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3695 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3696 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3697 d1fe46f9 2020-04-18 stsp if (err)
3698 d1fe46f9 2020-04-18 stsp return err;
3699 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3700 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3701 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3702 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3703 d1fe46f9 2020-04-18 stsp }
3704 d1fe46f9 2020-04-18 stsp free(*id);
3705 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3706 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3707 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3708 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3709 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3710 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3711 d1fe46f9 2020-04-18 stsp if (err)
3712 d1fe46f9 2020-04-18 stsp return err;
3713 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3714 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3715 d1fe46f9 2020-04-18 stsp } else {
3716 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3717 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3718 d1fe46f9 2020-04-18 stsp }
3719 d1fe46f9 2020-04-18 stsp
3720 d1fe46f9 2020-04-18 stsp return err;
3721 6f3d1eb0 2018-03-12 stsp }
3722 6f3d1eb0 2018-03-12 stsp
3723 4ed7e80c 2018-05-20 stsp static const struct got_error *
3724 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3725 f42b1b34 2018-03-12 stsp {
3726 f42b1b34 2018-03-12 stsp const struct got_error *error;
3727 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3728 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3729 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3730 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3731 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3732 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3733 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3734 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3735 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3736 64a96a6d 2018-04-01 stsp const char *errstr;
3737 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3738 1b3893a2 2019-03-18 stsp
3739 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3740 5c860e29 2018-03-12 stsp
3741 6715a751 2018-03-16 stsp #ifndef PROFILE
3742 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3743 6098196c 2019-01-04 stsp NULL)
3744 ad242220 2018-09-08 stsp == -1)
3745 f42b1b34 2018-03-12 stsp err(1, "pledge");
3746 6715a751 2018-03-16 stsp #endif
3747 79109fed 2018-03-27 stsp
3748 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3749 b1ebc001 2019-08-13 stsp
3750 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3751 79109fed 2018-03-27 stsp switch (ch) {
3752 79109fed 2018-03-27 stsp case 'p':
3753 79109fed 2018-03-27 stsp show_patch = 1;
3754 d142fc45 2018-04-01 stsp break;
3755 0208f208 2020-05-05 stsp case 'P':
3756 0208f208 2020-05-05 stsp show_changed_paths = 1;
3757 0208f208 2020-05-05 stsp break;
3758 d142fc45 2018-04-01 stsp case 'c':
3759 d142fc45 2018-04-01 stsp start_commit = optarg;
3760 64a96a6d 2018-04-01 stsp break;
3761 c0cc5c62 2018-10-18 stsp case 'C':
3762 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3763 4a8520aa 2018-10-18 stsp &errstr);
3764 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3765 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3766 c0cc5c62 2018-10-18 stsp break;
3767 64a96a6d 2018-04-01 stsp case 'l':
3768 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3769 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3770 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3771 cc54c501 2019-07-15 stsp break;
3772 48c8c60d 2020-01-27 stsp case 'b':
3773 48c8c60d 2020-01-27 stsp log_branches = 1;
3774 a0603db2 2018-06-10 stsp break;
3775 04ca23f4 2018-07-16 stsp case 'r':
3776 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3777 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3778 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3779 9ba1d308 2019-10-21 stsp optarg);
3780 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3781 04ca23f4 2018-07-16 stsp break;
3782 dbec59df 2020-04-18 stsp case 'R':
3783 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3784 dbec59df 2020-04-18 stsp break;
3785 6841bf13 2019-11-29 kn case 's':
3786 6841bf13 2019-11-29 kn search_pattern = optarg;
3787 6841bf13 2019-11-29 kn break;
3788 d1fe46f9 2020-04-18 stsp case 'x':
3789 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3790 d1fe46f9 2020-04-18 stsp break;
3791 79109fed 2018-03-27 stsp default:
3792 2deda0b9 2019-03-07 stsp usage_log();
3793 79109fed 2018-03-27 stsp /* NOTREACHED */
3794 79109fed 2018-03-27 stsp }
3795 79109fed 2018-03-27 stsp }
3796 79109fed 2018-03-27 stsp
3797 79109fed 2018-03-27 stsp argc -= optind;
3798 79109fed 2018-03-27 stsp argv += optind;
3799 f42b1b34 2018-03-12 stsp
3800 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3801 dc1edbfa 2019-11-29 kn diff_context = 3;
3802 dc1edbfa 2019-11-29 kn else if (!show_patch)
3803 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3804 dc1edbfa 2019-11-29 kn
3805 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3806 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3807 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3808 04ca23f4 2018-07-16 stsp goto done;
3809 04ca23f4 2018-07-16 stsp }
3810 cffc0aa4 2019-02-05 stsp
3811 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3812 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3813 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3814 50f2fada 2020-04-24 stsp goto done;
3815 50f2fada 2020-04-24 stsp error = NULL;
3816 50f2fada 2020-04-24 stsp }
3817 cffc0aa4 2019-02-05 stsp
3818 e7301579 2019-03-18 stsp if (argc == 0) {
3819 cbd1af7a 2019-03-18 stsp path = strdup("");
3820 e7301579 2019-03-18 stsp if (path == NULL) {
3821 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3822 cbd1af7a 2019-03-18 stsp goto done;
3823 e7301579 2019-03-18 stsp }
3824 e7301579 2019-03-18 stsp } else if (argc == 1) {
3825 e7301579 2019-03-18 stsp if (worktree) {
3826 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3827 e7301579 2019-03-18 stsp argv[0]);
3828 e7301579 2019-03-18 stsp if (error)
3829 e7301579 2019-03-18 stsp goto done;
3830 e7301579 2019-03-18 stsp } else {
3831 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3832 e7301579 2019-03-18 stsp if (path == NULL) {
3833 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3834 e7301579 2019-03-18 stsp goto done;
3835 e7301579 2019-03-18 stsp }
3836 e7301579 2019-03-18 stsp }
3837 cbd1af7a 2019-03-18 stsp } else
3838 cbd1af7a 2019-03-18 stsp usage_log();
3839 cbd1af7a 2019-03-18 stsp
3840 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3841 5486daa2 2019-05-11 stsp repo_path = worktree ?
3842 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3843 5486daa2 2019-05-11 stsp }
3844 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3845 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3846 cffc0aa4 2019-02-05 stsp goto done;
3847 04ca23f4 2018-07-16 stsp }
3848 6098196c 2019-01-04 stsp
3849 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3850 d7d4f210 2018-03-12 stsp if (error != NULL)
3851 04ca23f4 2018-07-16 stsp goto done;
3852 f42b1b34 2018-03-12 stsp
3853 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3854 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3855 c02c541e 2019-03-29 stsp if (error)
3856 c02c541e 2019-03-29 stsp goto done;
3857 c02c541e 2019-03-29 stsp
3858 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3859 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3860 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3861 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3862 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3863 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3864 3235492e 2018-04-01 stsp if (error != NULL)
3865 d1fe46f9 2020-04-18 stsp goto done;
3866 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3867 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3868 3235492e 2018-04-01 stsp if (error != NULL)
3869 d1fe46f9 2020-04-18 stsp goto done;
3870 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3871 d1fe46f9 2020-04-18 stsp start_id);
3872 d1fe46f9 2020-04-18 stsp if (error != NULL)
3873 d1fe46f9 2020-04-18 stsp goto done;
3874 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3875 3235492e 2018-04-01 stsp } else {
3876 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3877 d1fe46f9 2020-04-18 stsp if (error != NULL)
3878 d1fe46f9 2020-04-18 stsp goto done;
3879 3235492e 2018-04-01 stsp }
3880 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3881 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3882 d1fe46f9 2020-04-18 stsp if (error != NULL)
3883 d1fe46f9 2020-04-18 stsp goto done;
3884 d1fe46f9 2020-04-18 stsp }
3885 04ca23f4 2018-07-16 stsp
3886 deeabeae 2019-08-27 stsp if (worktree) {
3887 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3888 deeabeae 2019-08-27 stsp char *p;
3889 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3890 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3891 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3892 deeabeae 2019-08-27 stsp goto done;
3893 deeabeae 2019-08-27 stsp }
3894 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3895 deeabeae 2019-08-27 stsp free(p);
3896 deeabeae 2019-08-27 stsp } else
3897 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3898 04ca23f4 2018-07-16 stsp if (error != NULL)
3899 04ca23f4 2018-07-16 stsp goto done;
3900 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3901 04ca23f4 2018-07-16 stsp free(path);
3902 04ca23f4 2018-07-16 stsp path = in_repo_path;
3903 04ca23f4 2018-07-16 stsp }
3904 199a4027 2019-02-02 stsp
3905 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3906 199a4027 2019-02-02 stsp if (error)
3907 199a4027 2019-02-02 stsp goto done;
3908 04ca23f4 2018-07-16 stsp
3909 0208f208 2020-05-05 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3910 0208f208 2020-05-05 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3911 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3912 04ca23f4 2018-07-16 stsp done:
3913 04ca23f4 2018-07-16 stsp free(path);
3914 04ca23f4 2018-07-16 stsp free(repo_path);
3915 04ca23f4 2018-07-16 stsp free(cwd);
3916 cffc0aa4 2019-02-05 stsp if (worktree)
3917 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3918 ad242220 2018-09-08 stsp if (repo) {
3919 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3920 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3921 ad242220 2018-09-08 stsp if (error == NULL)
3922 ad242220 2018-09-08 stsp error = repo_error;
3923 ad242220 2018-09-08 stsp }
3924 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3925 8bf5b3c9 2018-03-17 stsp return error;
3926 5c860e29 2018-03-12 stsp }
3927 5c860e29 2018-03-12 stsp
3928 4ed7e80c 2018-05-20 stsp __dead static void
3929 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3930 3f8b7d6a 2018-04-01 stsp {
3931 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3932 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3933 3f8b7d6a 2018-04-01 stsp exit(1);
3934 b00d56cd 2018-04-01 stsp }
3935 b00d56cd 2018-04-01 stsp
3936 b72f483a 2019-02-05 stsp struct print_diff_arg {
3937 b72f483a 2019-02-05 stsp struct got_repository *repo;
3938 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3939 b72f483a 2019-02-05 stsp int diff_context;
3940 3fc0c068 2019-02-10 stsp const char *id_str;
3941 3fc0c068 2019-02-10 stsp int header_shown;
3942 98eaaa12 2019-08-03 stsp int diff_staged;
3943 63035f9f 2019-10-06 stsp int ignore_whitespace;
3944 b72f483a 2019-02-05 stsp };
3945 39449a05 2020-07-23 stsp
3946 39449a05 2020-07-23 stsp /*
3947 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3948 39449a05 2020-07-23 stsp * it as content to the diff engine.
3949 39449a05 2020-07-23 stsp */
3950 39449a05 2020-07-23 stsp static const struct got_error *
3951 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3952 39449a05 2020-07-23 stsp const char *abspath)
3953 39449a05 2020-07-23 stsp {
3954 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3955 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3956 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3957 39449a05 2020-07-23 stsp
3958 39449a05 2020-07-23 stsp *fd = -1;
3959 39449a05 2020-07-23 stsp
3960 39449a05 2020-07-23 stsp if (dirfd != -1) {
3961 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3962 39449a05 2020-07-23 stsp if (target_len == -1)
3963 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3964 39449a05 2020-07-23 stsp } else {
3965 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3966 39449a05 2020-07-23 stsp if (target_len == -1)
3967 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3968 39449a05 2020-07-23 stsp }
3969 39449a05 2020-07-23 stsp
3970 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3971 39449a05 2020-07-23 stsp if (*fd == -1)
3972 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3973 39449a05 2020-07-23 stsp
3974 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3975 39449a05 2020-07-23 stsp if (outlen == -1) {
3976 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3977 39449a05 2020-07-23 stsp goto done;
3978 39449a05 2020-07-23 stsp }
3979 39449a05 2020-07-23 stsp
3980 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3981 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3982 39449a05 2020-07-23 stsp goto done;
3983 39449a05 2020-07-23 stsp }
3984 39449a05 2020-07-23 stsp done:
3985 39449a05 2020-07-23 stsp if (err) {
3986 39449a05 2020-07-23 stsp close(*fd);
3987 39449a05 2020-07-23 stsp *fd = -1;
3988 39449a05 2020-07-23 stsp }
3989 39449a05 2020-07-23 stsp return err;
3990 39449a05 2020-07-23 stsp }
3991 b72f483a 2019-02-05 stsp
3992 4ed7e80c 2018-05-20 stsp static const struct got_error *
3993 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3994 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3995 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3996 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3997 b72f483a 2019-02-05 stsp {
3998 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3999 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
4000 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
4001 12463d8b 2019-12-13 stsp int fd = -1;
4002 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
4003 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
4004 b72f483a 2019-02-05 stsp struct stat sb;
4005 b72f483a 2019-02-05 stsp
4006 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4007 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
4008 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
4009 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
4010 98eaaa12 2019-08-03 stsp return NULL;
4011 98eaaa12 2019-08-03 stsp } else {
4012 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
4013 98eaaa12 2019-08-03 stsp return NULL;
4014 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4015 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
4016 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
4017 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
4018 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
4019 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
4020 98eaaa12 2019-08-03 stsp return NULL;
4021 98eaaa12 2019-08-03 stsp }
4022 3fc0c068 2019-02-10 stsp
4023 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
4024 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
4025 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
4026 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
4027 3fc0c068 2019-02-10 stsp a->header_shown = 1;
4028 3fc0c068 2019-02-10 stsp }
4029 b72f483a 2019-02-05 stsp
4030 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
4031 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
4032 98eaaa12 2019-08-03 stsp switch (staged_status) {
4033 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
4034 98eaaa12 2019-08-03 stsp label1 = path;
4035 98eaaa12 2019-08-03 stsp label2 = path;
4036 98eaaa12 2019-08-03 stsp break;
4037 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
4038 98eaaa12 2019-08-03 stsp label2 = path;
4039 98eaaa12 2019-08-03 stsp break;
4040 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4041 98eaaa12 2019-08-03 stsp label1 = path;
4042 98eaaa12 2019-08-03 stsp break;
4043 98eaaa12 2019-08-03 stsp default:
4044 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4045 98eaaa12 2019-08-03 stsp }
4046 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
4047 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
4048 63035f9f 2019-10-06 stsp a->repo, stdout);
4049 98eaaa12 2019-08-03 stsp }
4050 98eaaa12 2019-08-03 stsp
4051 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4052 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4053 4ce46740 2019-08-08 stsp char *id_str;
4054 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4055 408b4ebc 2019-08-03 stsp 8192);
4056 4ce46740 2019-08-08 stsp if (err)
4057 4ce46740 2019-08-08 stsp goto done;
4058 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4059 4ce46740 2019-08-08 stsp if (err)
4060 4ce46740 2019-08-08 stsp goto done;
4061 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4062 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4063 4ce46740 2019-08-08 stsp free(id_str);
4064 4ce46740 2019-08-08 stsp goto done;
4065 4ce46740 2019-08-08 stsp }
4066 4ce46740 2019-08-08 stsp free(id_str);
4067 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4068 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4069 4ce46740 2019-08-08 stsp if (err)
4070 4ce46740 2019-08-08 stsp goto done;
4071 4ce46740 2019-08-08 stsp }
4072 d00136be 2019-03-26 stsp
4073 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4074 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4075 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4076 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4077 2ec1f75b 2019-03-26 stsp goto done;
4078 2ec1f75b 2019-03-26 stsp }
4079 b72f483a 2019-02-05 stsp
4080 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4081 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4082 12463d8b 2019-12-13 stsp if (fd == -1) {
4083 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4084 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4085 39449a05 2020-07-23 stsp abspath);
4086 39449a05 2020-07-23 stsp goto done;
4087 39449a05 2020-07-23 stsp }
4088 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4089 39449a05 2020-07-23 stsp de_name, abspath);
4090 39449a05 2020-07-23 stsp if (err)
4091 39449a05 2020-07-23 stsp goto done;
4092 12463d8b 2019-12-13 stsp }
4093 12463d8b 2019-12-13 stsp } else {
4094 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4095 12463d8b 2019-12-13 stsp if (fd == -1) {
4096 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4097 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4098 39449a05 2020-07-23 stsp abspath);
4099 39449a05 2020-07-23 stsp goto done;
4100 39449a05 2020-07-23 stsp }
4101 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4102 39449a05 2020-07-23 stsp de_name, abspath);
4103 39449a05 2020-07-23 stsp if (err)
4104 39449a05 2020-07-23 stsp goto done;
4105 12463d8b 2019-12-13 stsp }
4106 12463d8b 2019-12-13 stsp }
4107 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4108 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4109 2ec1f75b 2019-03-26 stsp goto done;
4110 2ec1f75b 2019-03-26 stsp }
4111 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4112 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4113 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4114 2ec1f75b 2019-03-26 stsp goto done;
4115 2ec1f75b 2019-03-26 stsp }
4116 12463d8b 2019-12-13 stsp fd = -1;
4117 2ec1f75b 2019-03-26 stsp } else
4118 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4119 b72f483a 2019-02-05 stsp
4120 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4121 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
4122 b72f483a 2019-02-05 stsp done:
4123 b72f483a 2019-02-05 stsp if (blob1)
4124 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4125 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4126 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4127 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4128 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4129 b72f483a 2019-02-05 stsp free(abspath);
4130 927df6b7 2019-02-10 stsp return err;
4131 927df6b7 2019-02-10 stsp }
4132 927df6b7 2019-02-10 stsp
4133 927df6b7 2019-02-10 stsp static const struct got_error *
4134 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4135 b00d56cd 2018-04-01 stsp {
4136 b00d56cd 2018-04-01 stsp const struct got_error *error;
4137 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4138 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4139 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4140 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4141 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4142 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4143 15a94983 2018-12-23 stsp int type1, type2;
4144 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4145 c0cc5c62 2018-10-18 stsp const char *errstr;
4146 927df6b7 2019-02-10 stsp char *path = NULL;
4147 b00d56cd 2018-04-01 stsp
4148 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4149 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4150 25eccc22 2019-01-04 stsp NULL) == -1)
4151 b00d56cd 2018-04-01 stsp err(1, "pledge");
4152 b00d56cd 2018-04-01 stsp #endif
4153 b00d56cd 2018-04-01 stsp
4154 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
4155 b00d56cd 2018-04-01 stsp switch (ch) {
4156 c0cc5c62 2018-10-18 stsp case 'C':
4157 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4158 dfcab68b 2019-11-29 kn &errstr);
4159 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4160 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4161 c0cc5c62 2018-10-18 stsp break;
4162 b72f483a 2019-02-05 stsp case 'r':
4163 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4164 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4165 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4166 9ba1d308 2019-10-21 stsp optarg);
4167 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4168 b72f483a 2019-02-05 stsp break;
4169 98eaaa12 2019-08-03 stsp case 's':
4170 98eaaa12 2019-08-03 stsp diff_staged = 1;
4171 63035f9f 2019-10-06 stsp break;
4172 63035f9f 2019-10-06 stsp case 'w':
4173 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4174 98eaaa12 2019-08-03 stsp break;
4175 b00d56cd 2018-04-01 stsp default:
4176 2deda0b9 2019-03-07 stsp usage_diff();
4177 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4178 b00d56cd 2018-04-01 stsp }
4179 b00d56cd 2018-04-01 stsp }
4180 b00d56cd 2018-04-01 stsp
4181 b00d56cd 2018-04-01 stsp argc -= optind;
4182 b00d56cd 2018-04-01 stsp argv += optind;
4183 b00d56cd 2018-04-01 stsp
4184 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4185 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4186 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4187 b72f483a 2019-02-05 stsp goto done;
4188 b72f483a 2019-02-05 stsp }
4189 927df6b7 2019-02-10 stsp if (argc <= 1) {
4190 b72f483a 2019-02-05 stsp if (repo_path)
4191 b72f483a 2019-02-05 stsp errx(1,
4192 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4193 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4194 fa51e947 2020-03-27 stsp if (error) {
4195 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4196 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4197 fa51e947 2020-03-27 stsp cwd);
4198 1f03b8da 2020-03-20 stsp goto done;
4199 fa51e947 2020-03-27 stsp }
4200 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4201 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4202 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4203 927df6b7 2019-02-10 stsp goto done;
4204 927df6b7 2019-02-10 stsp }
4205 927df6b7 2019-02-10 stsp if (argc == 1) {
4206 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4207 cbd1af7a 2019-03-18 stsp argv[0]);
4208 927df6b7 2019-02-10 stsp if (error)
4209 927df6b7 2019-02-10 stsp goto done;
4210 927df6b7 2019-02-10 stsp } else {
4211 927df6b7 2019-02-10 stsp path = strdup("");
4212 927df6b7 2019-02-10 stsp if (path == NULL) {
4213 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4214 927df6b7 2019-02-10 stsp goto done;
4215 927df6b7 2019-02-10 stsp }
4216 927df6b7 2019-02-10 stsp }
4217 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4218 98eaaa12 2019-08-03 stsp if (diff_staged)
4219 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4220 98eaaa12 2019-08-03 stsp "objects in repository");
4221 15a94983 2018-12-23 stsp id_str1 = argv[0];
4222 15a94983 2018-12-23 stsp id_str2 = argv[1];
4223 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4224 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4225 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4226 30db809c 2019-06-05 stsp goto done;
4227 1f03b8da 2020-03-20 stsp if (worktree) {
4228 1f03b8da 2020-03-20 stsp repo_path = strdup(
4229 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4230 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4231 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4232 1f03b8da 2020-03-20 stsp goto done;
4233 1f03b8da 2020-03-20 stsp }
4234 1f03b8da 2020-03-20 stsp } else {
4235 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4236 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4237 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4238 1f03b8da 2020-03-20 stsp goto done;
4239 1f03b8da 2020-03-20 stsp }
4240 30db809c 2019-06-05 stsp }
4241 30db809c 2019-06-05 stsp }
4242 b00d56cd 2018-04-01 stsp } else
4243 b00d56cd 2018-04-01 stsp usage_diff();
4244 b00d56cd 2018-04-01 stsp
4245 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4246 76089277 2018-04-01 stsp free(repo_path);
4247 b00d56cd 2018-04-01 stsp if (error != NULL)
4248 b00d56cd 2018-04-01 stsp goto done;
4249 b00d56cd 2018-04-01 stsp
4250 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4251 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4252 c02c541e 2019-03-29 stsp if (error)
4253 c02c541e 2019-03-29 stsp goto done;
4254 c02c541e 2019-03-29 stsp
4255 30db809c 2019-06-05 stsp if (argc <= 1) {
4256 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4257 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4258 b72f483a 2019-02-05 stsp char *id_str;
4259 72ea6654 2019-07-27 stsp
4260 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4261 72ea6654 2019-07-27 stsp
4262 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4263 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4264 b72f483a 2019-02-05 stsp if (error)
4265 b72f483a 2019-02-05 stsp goto done;
4266 b72f483a 2019-02-05 stsp arg.repo = repo;
4267 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4268 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4269 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4270 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4271 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4272 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4273 b72f483a 2019-02-05 stsp
4274 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4275 72ea6654 2019-07-27 stsp if (error)
4276 72ea6654 2019-07-27 stsp goto done;
4277 72ea6654 2019-07-27 stsp
4278 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4279 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4280 b72f483a 2019-02-05 stsp free(id_str);
4281 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4282 b72f483a 2019-02-05 stsp goto done;
4283 b72f483a 2019-02-05 stsp }
4284 b72f483a 2019-02-05 stsp
4285 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4286 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4287 0adb7196 2019-08-11 stsp if (error)
4288 0adb7196 2019-08-11 stsp goto done;
4289 b00d56cd 2018-04-01 stsp
4290 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4291 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4292 0adb7196 2019-08-11 stsp if (error)
4293 0adb7196 2019-08-11 stsp goto done;
4294 b00d56cd 2018-04-01 stsp
4295 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4296 15a94983 2018-12-23 stsp if (error)
4297 15a94983 2018-12-23 stsp goto done;
4298 15a94983 2018-12-23 stsp
4299 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4300 15a94983 2018-12-23 stsp if (error)
4301 15a94983 2018-12-23 stsp goto done;
4302 15a94983 2018-12-23 stsp
4303 15a94983 2018-12-23 stsp if (type1 != type2) {
4304 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4305 b00d56cd 2018-04-01 stsp goto done;
4306 b00d56cd 2018-04-01 stsp }
4307 b00d56cd 2018-04-01 stsp
4308 15a94983 2018-12-23 stsp switch (type1) {
4309 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4310 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4311 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4312 b00d56cd 2018-04-01 stsp break;
4313 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4314 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4315 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4316 b00d56cd 2018-04-01 stsp break;
4317 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4318 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4319 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4320 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
4321 b00d56cd 2018-04-01 stsp break;
4322 b00d56cd 2018-04-01 stsp default:
4323 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4324 b00d56cd 2018-04-01 stsp }
4325 b00d56cd 2018-04-01 stsp done:
4326 5e70831e 2019-05-28 stsp free(label1);
4327 5e70831e 2019-05-28 stsp free(label2);
4328 15a94983 2018-12-23 stsp free(id1);
4329 15a94983 2018-12-23 stsp free(id2);
4330 927df6b7 2019-02-10 stsp free(path);
4331 b72f483a 2019-02-05 stsp if (worktree)
4332 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4333 ad242220 2018-09-08 stsp if (repo) {
4334 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4335 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4336 ad242220 2018-09-08 stsp if (error == NULL)
4337 ad242220 2018-09-08 stsp error = repo_error;
4338 ad242220 2018-09-08 stsp }
4339 b00d56cd 2018-04-01 stsp return error;
4340 404c43c4 2018-06-21 stsp }
4341 404c43c4 2018-06-21 stsp
4342 404c43c4 2018-06-21 stsp __dead static void
4343 404c43c4 2018-06-21 stsp usage_blame(void)
4344 404c43c4 2018-06-21 stsp {
4345 9270e621 2019-02-05 stsp fprintf(stderr,
4346 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4347 404c43c4 2018-06-21 stsp getprogname());
4348 404c43c4 2018-06-21 stsp exit(1);
4349 b00d56cd 2018-04-01 stsp }
4350 b00d56cd 2018-04-01 stsp
4351 28315671 2019-08-14 stsp struct blame_line {
4352 28315671 2019-08-14 stsp int annotated;
4353 28315671 2019-08-14 stsp char *id_str;
4354 82f6abb8 2019-08-14 stsp char *committer;
4355 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4356 28315671 2019-08-14 stsp };
4357 28315671 2019-08-14 stsp
4358 28315671 2019-08-14 stsp struct blame_cb_args {
4359 28315671 2019-08-14 stsp struct blame_line *lines;
4360 28315671 2019-08-14 stsp int nlines;
4361 7ef28ff8 2019-08-14 stsp int nlines_prec;
4362 28315671 2019-08-14 stsp int lineno_cur;
4363 28315671 2019-08-14 stsp off_t *line_offsets;
4364 28315671 2019-08-14 stsp FILE *f;
4365 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4366 28315671 2019-08-14 stsp };
4367 28315671 2019-08-14 stsp
4368 404c43c4 2018-06-21 stsp static const struct got_error *
4369 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4370 28315671 2019-08-14 stsp {
4371 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4372 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4373 28315671 2019-08-14 stsp struct blame_line *bline;
4374 82f6abb8 2019-08-14 stsp char *line = NULL;
4375 28315671 2019-08-14 stsp size_t linesize = 0;
4376 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4377 28315671 2019-08-14 stsp off_t offset;
4378 bcb49d15 2019-08-14 stsp struct tm tm;
4379 bcb49d15 2019-08-14 stsp time_t committer_time;
4380 28315671 2019-08-14 stsp
4381 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4382 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4383 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4384 28315671 2019-08-14 stsp
4385 28315671 2019-08-14 stsp if (sigint_received)
4386 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4387 28315671 2019-08-14 stsp
4388 2839f8b9 2019-08-18 stsp if (lineno == -1)
4389 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4390 2839f8b9 2019-08-18 stsp
4391 28315671 2019-08-14 stsp /* Annotate this line. */
4392 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4393 28315671 2019-08-14 stsp if (bline->annotated)
4394 28315671 2019-08-14 stsp return NULL;
4395 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4396 28315671 2019-08-14 stsp if (err)
4397 28315671 2019-08-14 stsp return err;
4398 82f6abb8 2019-08-14 stsp
4399 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4400 82f6abb8 2019-08-14 stsp if (err)
4401 82f6abb8 2019-08-14 stsp goto done;
4402 82f6abb8 2019-08-14 stsp
4403 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4404 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4405 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4406 bcb49d15 2019-08-14 stsp goto done;
4407 bcb49d15 2019-08-14 stsp }
4408 bcb49d15 2019-08-14 stsp
4409 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4410 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4411 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4412 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4413 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4414 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4415 82f6abb8 2019-08-14 stsp goto done;
4416 82f6abb8 2019-08-14 stsp }
4417 28315671 2019-08-14 stsp bline->annotated = 1;
4418 28315671 2019-08-14 stsp
4419 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4420 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4421 28315671 2019-08-14 stsp if (!bline->annotated)
4422 82f6abb8 2019-08-14 stsp goto done;
4423 28315671 2019-08-14 stsp
4424 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4425 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4426 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4427 82f6abb8 2019-08-14 stsp goto done;
4428 82f6abb8 2019-08-14 stsp }
4429 28315671 2019-08-14 stsp
4430 28315671 2019-08-14 stsp while (bline->annotated) {
4431 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4432 82f6abb8 2019-08-14 stsp size_t len;
4433 82f6abb8 2019-08-14 stsp
4434 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4435 28315671 2019-08-14 stsp if (ferror(a->f))
4436 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4437 28315671 2019-08-14 stsp break;
4438 28315671 2019-08-14 stsp }
4439 82f6abb8 2019-08-14 stsp
4440 82f6abb8 2019-08-14 stsp committer = bline->committer;
4441 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4442 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4443 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4444 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4445 82f6abb8 2019-08-14 stsp if (at)
4446 82f6abb8 2019-08-14 stsp *at = '\0';
4447 82f6abb8 2019-08-14 stsp len = strlen(committer);
4448 82f6abb8 2019-08-14 stsp if (len >= 9)
4449 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4450 28315671 2019-08-14 stsp
4451 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4452 28315671 2019-08-14 stsp if (nl)
4453 28315671 2019-08-14 stsp *nl = '\0';
4454 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4455 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4456 28315671 2019-08-14 stsp
4457 28315671 2019-08-14 stsp a->lineno_cur++;
4458 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4459 28315671 2019-08-14 stsp }
4460 82f6abb8 2019-08-14 stsp done:
4461 82f6abb8 2019-08-14 stsp if (commit)
4462 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4463 28315671 2019-08-14 stsp free(line);
4464 28315671 2019-08-14 stsp return err;
4465 28315671 2019-08-14 stsp }
4466 28315671 2019-08-14 stsp
4467 28315671 2019-08-14 stsp static const struct got_error *
4468 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4469 404c43c4 2018-06-21 stsp {
4470 404c43c4 2018-06-21 stsp const struct got_error *error;
4471 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4472 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4473 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4474 0587e10c 2020-07-23 stsp char *link_target = NULL;
4475 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4476 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4477 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4478 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4479 28315671 2019-08-14 stsp struct blame_cb_args bca;
4480 28315671 2019-08-14 stsp int ch, obj_type, i;
4481 b02560ec 2019-08-19 stsp size_t filesize;
4482 90f3c347 2019-08-19 stsp
4483 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4484 404c43c4 2018-06-21 stsp
4485 404c43c4 2018-06-21 stsp #ifndef PROFILE
4486 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4487 36e2fb66 2019-01-04 stsp NULL) == -1)
4488 404c43c4 2018-06-21 stsp err(1, "pledge");
4489 404c43c4 2018-06-21 stsp #endif
4490 404c43c4 2018-06-21 stsp
4491 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4492 404c43c4 2018-06-21 stsp switch (ch) {
4493 404c43c4 2018-06-21 stsp case 'c':
4494 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4495 404c43c4 2018-06-21 stsp break;
4496 66bea077 2018-08-02 stsp case 'r':
4497 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4498 66bea077 2018-08-02 stsp if (repo_path == NULL)
4499 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4500 9ba1d308 2019-10-21 stsp optarg);
4501 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4502 66bea077 2018-08-02 stsp break;
4503 404c43c4 2018-06-21 stsp default:
4504 2deda0b9 2019-03-07 stsp usage_blame();
4505 404c43c4 2018-06-21 stsp /* NOTREACHED */
4506 404c43c4 2018-06-21 stsp }
4507 404c43c4 2018-06-21 stsp }
4508 404c43c4 2018-06-21 stsp
4509 404c43c4 2018-06-21 stsp argc -= optind;
4510 404c43c4 2018-06-21 stsp argv += optind;
4511 404c43c4 2018-06-21 stsp
4512 a39318fd 2018-08-02 stsp if (argc == 1)
4513 404c43c4 2018-06-21 stsp path = argv[0];
4514 a39318fd 2018-08-02 stsp else
4515 404c43c4 2018-06-21 stsp usage_blame();
4516 404c43c4 2018-06-21 stsp
4517 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4518 66bea077 2018-08-02 stsp if (cwd == NULL) {
4519 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4520 66bea077 2018-08-02 stsp goto done;
4521 66bea077 2018-08-02 stsp }
4522 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4523 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4524 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4525 66bea077 2018-08-02 stsp goto done;
4526 0c06baac 2019-02-05 stsp else
4527 0c06baac 2019-02-05 stsp error = NULL;
4528 0c06baac 2019-02-05 stsp if (worktree) {
4529 0c06baac 2019-02-05 stsp repo_path =
4530 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4531 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4532 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4533 1f03b8da 2020-03-20 stsp if (error)
4534 1f03b8da 2020-03-20 stsp goto done;
4535 1f03b8da 2020-03-20 stsp }
4536 0c06baac 2019-02-05 stsp } else {
4537 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4538 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4539 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4540 0c06baac 2019-02-05 stsp goto done;
4541 0c06baac 2019-02-05 stsp }
4542 66bea077 2018-08-02 stsp }
4543 66bea077 2018-08-02 stsp }
4544 36e2fb66 2019-01-04 stsp
4545 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4546 c02c541e 2019-03-29 stsp if (error != NULL)
4547 36e2fb66 2019-01-04 stsp goto done;
4548 66bea077 2018-08-02 stsp
4549 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4550 c02c541e 2019-03-29 stsp if (error)
4551 404c43c4 2018-06-21 stsp goto done;
4552 404c43c4 2018-06-21 stsp
4553 0c06baac 2019-02-05 stsp if (worktree) {
4554 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4555 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4556 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4557 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4558 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4559 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4560 6efaaa2d 2019-02-05 stsp path) == -1) {
4561 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4562 0c06baac 2019-02-05 stsp goto done;
4563 0c06baac 2019-02-05 stsp }
4564 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4565 0c06baac 2019-02-05 stsp free(p);
4566 0c06baac 2019-02-05 stsp } else {
4567 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4568 0c06baac 2019-02-05 stsp }
4569 0c06baac 2019-02-05 stsp if (error)
4570 66bea077 2018-08-02 stsp goto done;
4571 66bea077 2018-08-02 stsp
4572 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4573 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4574 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4575 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4576 404c43c4 2018-06-21 stsp if (error != NULL)
4577 66bea077 2018-08-02 stsp goto done;
4578 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4579 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4580 404c43c4 2018-06-21 stsp if (error != NULL)
4581 66bea077 2018-08-02 stsp goto done;
4582 404c43c4 2018-06-21 stsp } else {
4583 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4584 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4585 30837e32 2019-07-25 stsp if (error)
4586 66bea077 2018-08-02 stsp goto done;
4587 404c43c4 2018-06-21 stsp }
4588 404c43c4 2018-06-21 stsp
4589 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4590 0587e10c 2020-07-23 stsp commit_id, repo);
4591 0587e10c 2020-07-23 stsp if (error)
4592 0587e10c 2020-07-23 stsp goto done;
4593 0587e10c 2020-07-23 stsp
4594 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4595 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4596 28315671 2019-08-14 stsp if (error)
4597 28315671 2019-08-14 stsp goto done;
4598 28315671 2019-08-14 stsp
4599 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4600 28315671 2019-08-14 stsp if (error)
4601 28315671 2019-08-14 stsp goto done;
4602 28315671 2019-08-14 stsp
4603 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4604 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4605 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4606 28315671 2019-08-14 stsp goto done;
4607 28315671 2019-08-14 stsp }
4608 28315671 2019-08-14 stsp
4609 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4610 28315671 2019-08-14 stsp if (error)
4611 28315671 2019-08-14 stsp goto done;
4612 28315671 2019-08-14 stsp bca.f = got_opentemp();
4613 28315671 2019-08-14 stsp if (bca.f == NULL) {
4614 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4615 28315671 2019-08-14 stsp goto done;
4616 28315671 2019-08-14 stsp }
4617 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4618 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4619 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4620 28315671 2019-08-14 stsp goto done;
4621 28315671 2019-08-14 stsp
4622 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4623 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4624 b02560ec 2019-08-19 stsp bca.nlines--;
4625 b02560ec 2019-08-19 stsp
4626 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4627 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4628 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4629 28315671 2019-08-14 stsp goto done;
4630 28315671 2019-08-14 stsp }
4631 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4632 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4633 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4634 7ef28ff8 2019-08-14 stsp while (i > 0) {
4635 7ef28ff8 2019-08-14 stsp i /= 10;
4636 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4637 7ef28ff8 2019-08-14 stsp }
4638 82f6abb8 2019-08-14 stsp bca.repo = repo;
4639 7ef28ff8 2019-08-14 stsp
4640 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4641 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4642 404c43c4 2018-06-21 stsp done:
4643 66bea077 2018-08-02 stsp free(in_repo_path);
4644 0587e10c 2020-07-23 stsp free(link_target);
4645 66bea077 2018-08-02 stsp free(repo_path);
4646 66bea077 2018-08-02 stsp free(cwd);
4647 404c43c4 2018-06-21 stsp free(commit_id);
4648 28315671 2019-08-14 stsp free(obj_id);
4649 28315671 2019-08-14 stsp if (blob)
4650 28315671 2019-08-14 stsp got_object_blob_close(blob);
4651 0c06baac 2019-02-05 stsp if (worktree)
4652 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4653 ad242220 2018-09-08 stsp if (repo) {
4654 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4655 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4656 ad242220 2018-09-08 stsp if (error == NULL)
4657 ad242220 2018-09-08 stsp error = repo_error;
4658 ad242220 2018-09-08 stsp }
4659 3affba96 2019-09-22 stsp if (bca.lines) {
4660 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4661 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4662 3affba96 2019-09-22 stsp free(bline->id_str);
4663 3affba96 2019-09-22 stsp free(bline->committer);
4664 3affba96 2019-09-22 stsp }
4665 3affba96 2019-09-22 stsp free(bca.lines);
4666 28315671 2019-08-14 stsp }
4667 28315671 2019-08-14 stsp free(bca.line_offsets);
4668 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4669 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4670 404c43c4 2018-06-21 stsp return error;
4671 5de5890b 2018-10-18 stsp }
4672 5de5890b 2018-10-18 stsp
4673 5de5890b 2018-10-18 stsp __dead static void
4674 5de5890b 2018-10-18 stsp usage_tree(void)
4675 5de5890b 2018-10-18 stsp {
4676 c1669e2e 2019-01-09 stsp fprintf(stderr,
4677 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4678 5de5890b 2018-10-18 stsp getprogname());
4679 5de5890b 2018-10-18 stsp exit(1);
4680 5de5890b 2018-10-18 stsp }
4681 5de5890b 2018-10-18 stsp
4682 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4683 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4684 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4685 c1669e2e 2019-01-09 stsp {
4686 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4687 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4688 848d6979 2019-08-12 stsp const char *modestr = "";
4689 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4690 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4691 5de5890b 2018-10-18 stsp
4692 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4693 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4694 c1669e2e 2019-01-09 stsp path++;
4695 c1669e2e 2019-01-09 stsp
4696 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4697 63c5ca5d 2019-08-24 stsp modestr = "$";
4698 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4699 0d6c6ee3 2020-05-20 stsp int i;
4700 0d6c6ee3 2020-05-20 stsp
4701 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4702 0d6c6ee3 2020-05-20 stsp if (err)
4703 0d6c6ee3 2020-05-20 stsp return err;
4704 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4705 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4706 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4707 0d6c6ee3 2020-05-20 stsp }
4708 0d6c6ee3 2020-05-20 stsp
4709 848d6979 2019-08-12 stsp modestr = "@";
4710 0d6c6ee3 2020-05-20 stsp }
4711 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4712 848d6979 2019-08-12 stsp modestr = "/";
4713 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4714 848d6979 2019-08-12 stsp modestr = "*";
4715 848d6979 2019-08-12 stsp
4716 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4717 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4718 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4719 0d6c6ee3 2020-05-20 stsp
4720 0d6c6ee3 2020-05-20 stsp free(link_target);
4721 0d6c6ee3 2020-05-20 stsp return NULL;
4722 c1669e2e 2019-01-09 stsp }
4723 c1669e2e 2019-01-09 stsp
4724 5de5890b 2018-10-18 stsp static const struct got_error *
4725 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4726 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4727 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4728 5de5890b 2018-10-18 stsp {
4729 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4730 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4731 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4732 56e0773d 2019-11-28 stsp int nentries, i;
4733 5de5890b 2018-10-18 stsp
4734 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4735 5de5890b 2018-10-18 stsp if (err)
4736 5de5890b 2018-10-18 stsp goto done;
4737 5de5890b 2018-10-18 stsp
4738 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4739 5de5890b 2018-10-18 stsp if (err)
4740 5de5890b 2018-10-18 stsp goto done;
4741 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4742 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4743 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4744 5de5890b 2018-10-18 stsp char *id = NULL;
4745 84453469 2018-11-11 stsp
4746 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4747 84453469 2018-11-11 stsp break;
4748 84453469 2018-11-11 stsp
4749 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4750 5de5890b 2018-10-18 stsp if (show_ids) {
4751 5de5890b 2018-10-18 stsp char *id_str;
4752 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4753 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4754 5de5890b 2018-10-18 stsp if (err)
4755 5de5890b 2018-10-18 stsp goto done;
4756 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4757 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4758 5de5890b 2018-10-18 stsp free(id_str);
4759 5de5890b 2018-10-18 stsp goto done;
4760 5de5890b 2018-10-18 stsp }
4761 5de5890b 2018-10-18 stsp free(id_str);
4762 5de5890b 2018-10-18 stsp }
4763 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4764 5de5890b 2018-10-18 stsp free(id);
4765 0d6c6ee3 2020-05-20 stsp if (err)
4766 0d6c6ee3 2020-05-20 stsp goto done;
4767 c1669e2e 2019-01-09 stsp
4768 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4769 c1669e2e 2019-01-09 stsp char *child_path;
4770 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4771 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4772 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4773 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4774 c1669e2e 2019-01-09 stsp goto done;
4775 c1669e2e 2019-01-09 stsp }
4776 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4777 c1669e2e 2019-01-09 stsp root_path, repo);
4778 c1669e2e 2019-01-09 stsp free(child_path);
4779 c1669e2e 2019-01-09 stsp if (err)
4780 c1669e2e 2019-01-09 stsp goto done;
4781 c1669e2e 2019-01-09 stsp }
4782 5de5890b 2018-10-18 stsp }
4783 5de5890b 2018-10-18 stsp done:
4784 5de5890b 2018-10-18 stsp if (tree)
4785 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4786 5de5890b 2018-10-18 stsp free(tree_id);
4787 5de5890b 2018-10-18 stsp return err;
4788 404c43c4 2018-06-21 stsp }
4789 404c43c4 2018-06-21 stsp
4790 5de5890b 2018-10-18 stsp static const struct got_error *
4791 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4792 5de5890b 2018-10-18 stsp {
4793 5de5890b 2018-10-18 stsp const struct got_error *error;
4794 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4795 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4796 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4797 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4798 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4799 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4800 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4801 5de5890b 2018-10-18 stsp int ch;
4802 5de5890b 2018-10-18 stsp
4803 5de5890b 2018-10-18 stsp #ifndef PROFILE
4804 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4805 0f8d269b 2019-01-04 stsp NULL) == -1)
4806 5de5890b 2018-10-18 stsp err(1, "pledge");
4807 5de5890b 2018-10-18 stsp #endif
4808 5de5890b 2018-10-18 stsp
4809 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4810 5de5890b 2018-10-18 stsp switch (ch) {
4811 5de5890b 2018-10-18 stsp case 'c':
4812 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4813 5de5890b 2018-10-18 stsp break;
4814 5de5890b 2018-10-18 stsp case 'r':
4815 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4816 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4817 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4818 9ba1d308 2019-10-21 stsp optarg);
4819 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4820 5de5890b 2018-10-18 stsp break;
4821 5de5890b 2018-10-18 stsp case 'i':
4822 5de5890b 2018-10-18 stsp show_ids = 1;
4823 5de5890b 2018-10-18 stsp break;
4824 c1669e2e 2019-01-09 stsp case 'R':
4825 c1669e2e 2019-01-09 stsp recurse = 1;
4826 c1669e2e 2019-01-09 stsp break;
4827 5de5890b 2018-10-18 stsp default:
4828 2deda0b9 2019-03-07 stsp usage_tree();
4829 5de5890b 2018-10-18 stsp /* NOTREACHED */
4830 5de5890b 2018-10-18 stsp }
4831 5de5890b 2018-10-18 stsp }
4832 5de5890b 2018-10-18 stsp
4833 5de5890b 2018-10-18 stsp argc -= optind;
4834 5de5890b 2018-10-18 stsp argv += optind;
4835 5de5890b 2018-10-18 stsp
4836 5de5890b 2018-10-18 stsp if (argc == 1)
4837 5de5890b 2018-10-18 stsp path = argv[0];
4838 5de5890b 2018-10-18 stsp else if (argc > 1)
4839 5de5890b 2018-10-18 stsp usage_tree();
4840 5de5890b 2018-10-18 stsp else
4841 7a2c19d6 2019-02-05 stsp path = NULL;
4842 7a2c19d6 2019-02-05 stsp
4843 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4844 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4845 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4846 9bf7a39b 2019-02-05 stsp goto done;
4847 9bf7a39b 2019-02-05 stsp }
4848 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4849 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4850 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4851 7a2c19d6 2019-02-05 stsp goto done;
4852 7a2c19d6 2019-02-05 stsp else
4853 7a2c19d6 2019-02-05 stsp error = NULL;
4854 7a2c19d6 2019-02-05 stsp if (worktree) {
4855 7a2c19d6 2019-02-05 stsp repo_path =
4856 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4857 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4858 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4859 7a2c19d6 2019-02-05 stsp if (error)
4860 7a2c19d6 2019-02-05 stsp goto done;
4861 7a2c19d6 2019-02-05 stsp } else {
4862 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4863 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4864 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4865 7a2c19d6 2019-02-05 stsp goto done;
4866 7a2c19d6 2019-02-05 stsp }
4867 5de5890b 2018-10-18 stsp }
4868 5de5890b 2018-10-18 stsp }
4869 5de5890b 2018-10-18 stsp
4870 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4871 5de5890b 2018-10-18 stsp if (error != NULL)
4872 c02c541e 2019-03-29 stsp goto done;
4873 c02c541e 2019-03-29 stsp
4874 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4875 c02c541e 2019-03-29 stsp if (error)
4876 5de5890b 2018-10-18 stsp goto done;
4877 5de5890b 2018-10-18 stsp
4878 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4879 9bf7a39b 2019-02-05 stsp if (worktree) {
4880 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4881 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4882 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4883 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4884 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4885 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4886 9bf7a39b 2019-02-05 stsp goto done;
4887 9bf7a39b 2019-02-05 stsp }
4888 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4889 9bf7a39b 2019-02-05 stsp free(p);
4890 9bf7a39b 2019-02-05 stsp if (error)
4891 9bf7a39b 2019-02-05 stsp goto done;
4892 9bf7a39b 2019-02-05 stsp } else
4893 9bf7a39b 2019-02-05 stsp path = "/";
4894 9bf7a39b 2019-02-05 stsp }
4895 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4896 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4897 9bf7a39b 2019-02-05 stsp if (error != NULL)
4898 9bf7a39b 2019-02-05 stsp goto done;
4899 9bf7a39b 2019-02-05 stsp }
4900 5de5890b 2018-10-18 stsp
4901 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4902 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4903 4e0a20a4 2020-03-23 tracey if (worktree)
4904 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4905 4e0a20a4 2020-03-23 tracey else
4906 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4907 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4908 5de5890b 2018-10-18 stsp if (error != NULL)
4909 5de5890b 2018-10-18 stsp goto done;
4910 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4911 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4912 5de5890b 2018-10-18 stsp if (error != NULL)
4913 5de5890b 2018-10-18 stsp goto done;
4914 5de5890b 2018-10-18 stsp } else {
4915 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4916 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4917 30837e32 2019-07-25 stsp if (error)
4918 5de5890b 2018-10-18 stsp goto done;
4919 5de5890b 2018-10-18 stsp }
4920 5de5890b 2018-10-18 stsp
4921 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4922 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4923 5de5890b 2018-10-18 stsp done:
4924 5de5890b 2018-10-18 stsp free(in_repo_path);
4925 5de5890b 2018-10-18 stsp free(repo_path);
4926 5de5890b 2018-10-18 stsp free(cwd);
4927 5de5890b 2018-10-18 stsp free(commit_id);
4928 7a2c19d6 2019-02-05 stsp if (worktree)
4929 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4930 5de5890b 2018-10-18 stsp if (repo) {
4931 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4932 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4933 5de5890b 2018-10-18 stsp if (error == NULL)
4934 5de5890b 2018-10-18 stsp error = repo_error;
4935 5de5890b 2018-10-18 stsp }
4936 5de5890b 2018-10-18 stsp return error;
4937 5de5890b 2018-10-18 stsp }
4938 5de5890b 2018-10-18 stsp
4939 6bad629b 2019-02-04 stsp __dead static void
4940 6bad629b 2019-02-04 stsp usage_status(void)
4941 6bad629b 2019-02-04 stsp {
4942 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4943 081470ac 2020-08-13 stsp getprogname());
4944 6bad629b 2019-02-04 stsp exit(1);
4945 6bad629b 2019-02-04 stsp }
4946 5c860e29 2018-03-12 stsp
4947 b72f483a 2019-02-05 stsp static const struct got_error *
4948 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4949 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4950 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4951 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4952 6bad629b 2019-02-04 stsp {
4953 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4954 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4955 081470ac 2020-08-13 stsp if (arg) {
4956 081470ac 2020-08-13 stsp char *status_codes = arg;
4957 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
4958 081470ac 2020-08-13 stsp int i;
4959 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4960 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
4961 081470ac 2020-08-13 stsp staged_status == status_codes[i])
4962 081470ac 2020-08-13 stsp break;
4963 081470ac 2020-08-13 stsp }
4964 081470ac 2020-08-13 stsp if (i == ncodes)
4965 081470ac 2020-08-13 stsp return NULL;
4966 081470ac 2020-08-13 stsp }
4967 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4968 b72f483a 2019-02-05 stsp return NULL;
4969 6bad629b 2019-02-04 stsp }
4970 5c860e29 2018-03-12 stsp
4971 6bad629b 2019-02-04 stsp static const struct got_error *
4972 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4973 6bad629b 2019-02-04 stsp {
4974 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4975 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4976 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4977 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
4978 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4979 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4980 081470ac 2020-08-13 stsp int ch, i;
4981 5c860e29 2018-03-12 stsp
4982 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4983 72ea6654 2019-07-27 stsp
4984 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4985 6bad629b 2019-02-04 stsp switch (ch) {
4986 081470ac 2020-08-13 stsp case 's':
4987 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
4988 081470ac 2020-08-13 stsp switch (optarg[i]) {
4989 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
4990 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
4991 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
4992 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
4993 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
4994 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
4995 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
4996 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
4997 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
4998 081470ac 2020-08-13 stsp break;
4999 081470ac 2020-08-13 stsp default:
5000 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
5001 081470ac 2020-08-13 stsp optarg[i]);
5002 081470ac 2020-08-13 stsp }
5003 081470ac 2020-08-13 stsp }
5004 081470ac 2020-08-13 stsp status_codes = optarg;
5005 081470ac 2020-08-13 stsp break;
5006 5c860e29 2018-03-12 stsp default:
5007 2deda0b9 2019-03-07 stsp usage_status();
5008 6bad629b 2019-02-04 stsp /* NOTREACHED */
5009 5c860e29 2018-03-12 stsp }
5010 5c860e29 2018-03-12 stsp }
5011 5c860e29 2018-03-12 stsp
5012 6bad629b 2019-02-04 stsp argc -= optind;
5013 6bad629b 2019-02-04 stsp argv += optind;
5014 5c860e29 2018-03-12 stsp
5015 6bad629b 2019-02-04 stsp #ifndef PROFILE
5016 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5017 6bad629b 2019-02-04 stsp NULL) == -1)
5018 6bad629b 2019-02-04 stsp err(1, "pledge");
5019 f42b1b34 2018-03-12 stsp #endif
5020 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
5021 927df6b7 2019-02-10 stsp if (cwd == NULL) {
5022 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5023 927df6b7 2019-02-10 stsp goto done;
5024 927df6b7 2019-02-10 stsp }
5025 927df6b7 2019-02-10 stsp
5026 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
5027 fa51e947 2020-03-27 stsp if (error) {
5028 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5029 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
5030 a5edda0a 2019-07-27 stsp goto done;
5031 fa51e947 2020-03-27 stsp }
5032 6bad629b 2019-02-04 stsp
5033 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5034 c9956ddf 2019-09-08 stsp NULL);
5035 6bad629b 2019-02-04 stsp if (error != NULL)
5036 6bad629b 2019-02-04 stsp goto done;
5037 6bad629b 2019-02-04 stsp
5038 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5039 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5040 087fb88c 2019-08-04 stsp if (error)
5041 087fb88c 2019-08-04 stsp goto done;
5042 087fb88c 2019-08-04 stsp
5043 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5044 6bad629b 2019-02-04 stsp if (error)
5045 6bad629b 2019-02-04 stsp goto done;
5046 6bad629b 2019-02-04 stsp
5047 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
5048 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
5049 6bad629b 2019-02-04 stsp done:
5050 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5051 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5052 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5053 927df6b7 2019-02-10 stsp free(cwd);
5054 d0eebce4 2019-03-11 stsp return error;
5055 d0eebce4 2019-03-11 stsp }
5056 d0eebce4 2019-03-11 stsp
5057 d0eebce4 2019-03-11 stsp __dead static void
5058 d0eebce4 2019-03-11 stsp usage_ref(void)
5059 d0eebce4 2019-03-11 stsp {
5060 d0eebce4 2019-03-11 stsp fprintf(stderr,
5061 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5062 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5063 d0eebce4 2019-03-11 stsp getprogname());
5064 d0eebce4 2019-03-11 stsp exit(1);
5065 d0eebce4 2019-03-11 stsp }
5066 d0eebce4 2019-03-11 stsp
5067 d0eebce4 2019-03-11 stsp static const struct got_error *
5068 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5069 d0eebce4 2019-03-11 stsp {
5070 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5071 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5072 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5073 d0eebce4 2019-03-11 stsp
5074 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
5075 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5076 d0eebce4 2019-03-11 stsp if (err)
5077 d0eebce4 2019-03-11 stsp return err;
5078 d0eebce4 2019-03-11 stsp
5079 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5080 d0eebce4 2019-03-11 stsp char *refstr;
5081 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5082 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5083 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5084 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5085 d0eebce4 2019-03-11 stsp free(refstr);
5086 d0eebce4 2019-03-11 stsp }
5087 d0eebce4 2019-03-11 stsp
5088 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5089 d0eebce4 2019-03-11 stsp return NULL;
5090 d0eebce4 2019-03-11 stsp }
5091 d0eebce4 2019-03-11 stsp
5092 d0eebce4 2019-03-11 stsp static const struct got_error *
5093 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
5094 d0eebce4 2019-03-11 stsp {
5095 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5096 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5097 d0eebce4 2019-03-11 stsp
5098 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5099 d0eebce4 2019-03-11 stsp if (err)
5100 d0eebce4 2019-03-11 stsp return err;
5101 d0eebce4 2019-03-11 stsp
5102 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
5103 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5104 d0eebce4 2019-03-11 stsp return err;
5105 d0eebce4 2019-03-11 stsp }
5106 d0eebce4 2019-03-11 stsp
5107 d0eebce4 2019-03-11 stsp static const struct got_error *
5108 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5109 d0eebce4 2019-03-11 stsp {
5110 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5111 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5112 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5113 d1644381 2019-07-14 stsp
5114 d1644381 2019-07-14 stsp /*
5115 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5116 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5117 d1644381 2019-07-14 stsp * an unintended typo.
5118 d1644381 2019-07-14 stsp */
5119 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5120 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5121 d0eebce4 2019-03-11 stsp
5122 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5123 dd88155e 2019-06-29 stsp repo);
5124 d83d9d5c 2019-05-13 stsp if (err) {
5125 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5126 d0eebce4 2019-03-11 stsp
5127 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5128 d83d9d5c 2019-05-13 stsp return err;
5129 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5130 d83d9d5c 2019-05-13 stsp if (err)
5131 d83d9d5c 2019-05-13 stsp return err;
5132 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5133 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5134 d83d9d5c 2019-05-13 stsp if (err)
5135 d83d9d5c 2019-05-13 stsp return err;
5136 d83d9d5c 2019-05-13 stsp }
5137 d83d9d5c 2019-05-13 stsp
5138 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5139 d0eebce4 2019-03-11 stsp if (err)
5140 d0eebce4 2019-03-11 stsp goto done;
5141 d0eebce4 2019-03-11 stsp
5142 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5143 d0eebce4 2019-03-11 stsp done:
5144 d0eebce4 2019-03-11 stsp if (ref)
5145 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5146 d0eebce4 2019-03-11 stsp free(id);
5147 d1c1ae5f 2019-08-12 stsp return err;
5148 d1c1ae5f 2019-08-12 stsp }
5149 d1c1ae5f 2019-08-12 stsp
5150 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5151 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5152 d1c1ae5f 2019-08-12 stsp {
5153 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5154 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5155 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5156 d1c1ae5f 2019-08-12 stsp
5157 d1c1ae5f 2019-08-12 stsp /*
5158 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5159 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5160 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5161 d1c1ae5f 2019-08-12 stsp */
5162 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5163 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5164 d1c1ae5f 2019-08-12 stsp
5165 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5166 d1c1ae5f 2019-08-12 stsp if (err)
5167 d1c1ae5f 2019-08-12 stsp return err;
5168 d1c1ae5f 2019-08-12 stsp
5169 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5170 d1c1ae5f 2019-08-12 stsp if (err)
5171 d1c1ae5f 2019-08-12 stsp goto done;
5172 d1c1ae5f 2019-08-12 stsp
5173 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5174 d1c1ae5f 2019-08-12 stsp done:
5175 d1c1ae5f 2019-08-12 stsp if (target_ref)
5176 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5177 d1c1ae5f 2019-08-12 stsp if (ref)
5178 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5179 d0eebce4 2019-03-11 stsp return err;
5180 d0eebce4 2019-03-11 stsp }
5181 d0eebce4 2019-03-11 stsp
5182 d0eebce4 2019-03-11 stsp static const struct got_error *
5183 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5184 d0eebce4 2019-03-11 stsp {
5185 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5186 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5187 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5188 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5189 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5190 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5191 b2070a3f 2020-03-22 stsp char *refname = NULL;
5192 d0eebce4 2019-03-11 stsp
5193 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5194 d0eebce4 2019-03-11 stsp switch (ch) {
5195 e31abbf2 2020-03-22 stsp case 'c':
5196 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5197 e31abbf2 2020-03-22 stsp break;
5198 d0eebce4 2019-03-11 stsp case 'd':
5199 e31abbf2 2020-03-22 stsp do_delete = 1;
5200 d0eebce4 2019-03-11 stsp break;
5201 d0eebce4 2019-03-11 stsp case 'r':
5202 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5203 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5204 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5205 9ba1d308 2019-10-21 stsp optarg);
5206 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5207 d0eebce4 2019-03-11 stsp break;
5208 d0eebce4 2019-03-11 stsp case 'l':
5209 d0eebce4 2019-03-11 stsp do_list = 1;
5210 d1c1ae5f 2019-08-12 stsp break;
5211 d1c1ae5f 2019-08-12 stsp case 's':
5212 e31abbf2 2020-03-22 stsp symref_target = optarg;
5213 d0eebce4 2019-03-11 stsp break;
5214 d0eebce4 2019-03-11 stsp default:
5215 d0eebce4 2019-03-11 stsp usage_ref();
5216 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5217 d0eebce4 2019-03-11 stsp }
5218 d0eebce4 2019-03-11 stsp }
5219 d0eebce4 2019-03-11 stsp
5220 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5221 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
5222 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
5223 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
5224 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5225 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
5226 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5227 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
5228 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5229 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
5230 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5231 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
5232 d0eebce4 2019-03-11 stsp
5233 d0eebce4 2019-03-11 stsp argc -= optind;
5234 d0eebce4 2019-03-11 stsp argv += optind;
5235 d0eebce4 2019-03-11 stsp
5236 e31abbf2 2020-03-22 stsp if (do_list) {
5237 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5238 d0eebce4 2019-03-11 stsp usage_ref();
5239 b2070a3f 2020-03-22 stsp if (argc == 1) {
5240 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5241 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5242 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5243 b2070a3f 2020-03-22 stsp goto done;
5244 b2070a3f 2020-03-22 stsp }
5245 b2070a3f 2020-03-22 stsp }
5246 e31abbf2 2020-03-22 stsp } else {
5247 e31abbf2 2020-03-22 stsp if (argc != 1)
5248 e31abbf2 2020-03-22 stsp usage_ref();
5249 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5250 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5251 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5252 b2070a3f 2020-03-22 stsp goto done;
5253 b2070a3f 2020-03-22 stsp }
5254 e31abbf2 2020-03-22 stsp }
5255 b2070a3f 2020-03-22 stsp
5256 b2070a3f 2020-03-22 stsp if (refname)
5257 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5258 d0eebce4 2019-03-11 stsp
5259 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5260 e0b57350 2019-03-12 stsp if (do_list) {
5261 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5262 e0b57350 2019-03-12 stsp NULL) == -1)
5263 e0b57350 2019-03-12 stsp err(1, "pledge");
5264 e0b57350 2019-03-12 stsp } else {
5265 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5266 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5267 e0b57350 2019-03-12 stsp err(1, "pledge");
5268 e0b57350 2019-03-12 stsp }
5269 d0eebce4 2019-03-11 stsp #endif
5270 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5271 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5272 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5273 d0eebce4 2019-03-11 stsp goto done;
5274 d0eebce4 2019-03-11 stsp }
5275 d0eebce4 2019-03-11 stsp
5276 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5277 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5278 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5279 d0eebce4 2019-03-11 stsp goto done;
5280 d0eebce4 2019-03-11 stsp else
5281 d0eebce4 2019-03-11 stsp error = NULL;
5282 d0eebce4 2019-03-11 stsp if (worktree) {
5283 d0eebce4 2019-03-11 stsp repo_path =
5284 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5285 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5286 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5287 d0eebce4 2019-03-11 stsp if (error)
5288 d0eebce4 2019-03-11 stsp goto done;
5289 d0eebce4 2019-03-11 stsp } else {
5290 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5291 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5292 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5293 d0eebce4 2019-03-11 stsp goto done;
5294 d0eebce4 2019-03-11 stsp }
5295 d0eebce4 2019-03-11 stsp }
5296 d0eebce4 2019-03-11 stsp }
5297 d0eebce4 2019-03-11 stsp
5298 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5299 d0eebce4 2019-03-11 stsp if (error != NULL)
5300 d0eebce4 2019-03-11 stsp goto done;
5301 d0eebce4 2019-03-11 stsp
5302 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5303 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5304 c02c541e 2019-03-29 stsp if (error)
5305 c02c541e 2019-03-29 stsp goto done;
5306 c02c541e 2019-03-29 stsp
5307 d0eebce4 2019-03-11 stsp if (do_list)
5308 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5309 e31abbf2 2020-03-22 stsp else if (do_delete)
5310 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5311 e31abbf2 2020-03-22 stsp else if (symref_target)
5312 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5313 e31abbf2 2020-03-22 stsp else {
5314 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5315 e31abbf2 2020-03-22 stsp usage_ref();
5316 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5317 e31abbf2 2020-03-22 stsp }
5318 4e759de4 2019-06-26 stsp done:
5319 b2070a3f 2020-03-22 stsp free(refname);
5320 4e759de4 2019-06-26 stsp if (repo)
5321 4e759de4 2019-06-26 stsp got_repo_close(repo);
5322 4e759de4 2019-06-26 stsp if (worktree)
5323 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5324 4e759de4 2019-06-26 stsp free(cwd);
5325 4e759de4 2019-06-26 stsp free(repo_path);
5326 4e759de4 2019-06-26 stsp return error;
5327 4e759de4 2019-06-26 stsp }
5328 4e759de4 2019-06-26 stsp
5329 4e759de4 2019-06-26 stsp __dead static void
5330 4e759de4 2019-06-26 stsp usage_branch(void)
5331 4e759de4 2019-06-26 stsp {
5332 4e759de4 2019-06-26 stsp fprintf(stderr,
5333 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5334 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5335 4e759de4 2019-06-26 stsp exit(1);
5336 4e759de4 2019-06-26 stsp }
5337 4e759de4 2019-06-26 stsp
5338 4e759de4 2019-06-26 stsp static const struct got_error *
5339 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5340 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5341 4e99b47e 2019-10-04 stsp {
5342 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5343 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5344 4e99b47e 2019-10-04 stsp char *refstr;
5345 4e99b47e 2019-10-04 stsp
5346 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5347 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5348 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5349 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5350 4e99b47e 2019-10-04 stsp
5351 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5352 4e99b47e 2019-10-04 stsp if (err)
5353 4e99b47e 2019-10-04 stsp return err;
5354 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5355 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5356 4e99b47e 2019-10-04 stsp marker = "* ";
5357 4e99b47e 2019-10-04 stsp else
5358 4e99b47e 2019-10-04 stsp marker = "~ ";
5359 4e99b47e 2019-10-04 stsp free(id);
5360 4e99b47e 2019-10-04 stsp }
5361 4e99b47e 2019-10-04 stsp
5362 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5363 4e99b47e 2019-10-04 stsp refname += 11;
5364 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5365 4e99b47e 2019-10-04 stsp refname += 18;
5366 4e99b47e 2019-10-04 stsp
5367 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5368 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5369 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5370 4e99b47e 2019-10-04 stsp
5371 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5372 4e99b47e 2019-10-04 stsp free(refstr);
5373 4e99b47e 2019-10-04 stsp return NULL;
5374 4e99b47e 2019-10-04 stsp }
5375 4e99b47e 2019-10-04 stsp
5376 4e99b47e 2019-10-04 stsp static const struct got_error *
5377 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5378 ad89fa31 2019-10-04 stsp {
5379 ad89fa31 2019-10-04 stsp const char *refname;
5380 ad89fa31 2019-10-04 stsp
5381 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5382 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5383 ad89fa31 2019-10-04 stsp
5384 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5385 ad89fa31 2019-10-04 stsp
5386 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5387 ad89fa31 2019-10-04 stsp refname += 11;
5388 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5389 ad89fa31 2019-10-04 stsp refname += 18;
5390 ad89fa31 2019-10-04 stsp
5391 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5392 ad89fa31 2019-10-04 stsp
5393 ad89fa31 2019-10-04 stsp return NULL;
5394 ad89fa31 2019-10-04 stsp }
5395 ad89fa31 2019-10-04 stsp
5396 ad89fa31 2019-10-04 stsp static const struct got_error *
5397 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5398 4e759de4 2019-06-26 stsp {
5399 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5400 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5401 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5402 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5403 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5404 4e759de4 2019-06-26 stsp
5405 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5406 ba882ee3 2019-07-11 stsp
5407 4e99b47e 2019-10-04 stsp if (worktree) {
5408 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5409 4e99b47e 2019-10-04 stsp worktree);
5410 4e99b47e 2019-10-04 stsp if (err)
5411 4e99b47e 2019-10-04 stsp return err;
5412 4e99b47e 2019-10-04 stsp
5413 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5414 4e99b47e 2019-10-04 stsp worktree);
5415 4e99b47e 2019-10-04 stsp if (err)
5416 4e99b47e 2019-10-04 stsp return err;
5417 4e99b47e 2019-10-04 stsp
5418 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5419 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5420 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5421 4e99b47e 2019-10-04 stsp if (err)
5422 4e99b47e 2019-10-04 stsp return err;
5423 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5424 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5425 4e99b47e 2019-10-04 stsp }
5426 4e99b47e 2019-10-04 stsp }
5427 4e99b47e 2019-10-04 stsp
5428 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5429 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5430 4e759de4 2019-06-26 stsp if (err)
5431 4e759de4 2019-06-26 stsp return err;
5432 4e759de4 2019-06-26 stsp
5433 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5434 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5435 4e759de4 2019-06-26 stsp
5436 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5437 4e759de4 2019-06-26 stsp return NULL;
5438 4e759de4 2019-06-26 stsp }
5439 4e759de4 2019-06-26 stsp
5440 4e759de4 2019-06-26 stsp static const struct got_error *
5441 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5442 45cd4e47 2019-08-25 stsp const char *branch_name)
5443 4e759de4 2019-06-26 stsp {
5444 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5445 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5446 4e759de4 2019-06-26 stsp char *refname;
5447 4e759de4 2019-06-26 stsp
5448 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5449 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5450 4e759de4 2019-06-26 stsp
5451 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5452 4e759de4 2019-06-26 stsp if (err)
5453 4e759de4 2019-06-26 stsp goto done;
5454 4e759de4 2019-06-26 stsp
5455 45cd4e47 2019-08-25 stsp if (worktree &&
5456 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5457 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5458 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5459 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5460 45cd4e47 2019-08-25 stsp goto done;
5461 45cd4e47 2019-08-25 stsp }
5462 45cd4e47 2019-08-25 stsp
5463 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5464 4e759de4 2019-06-26 stsp done:
5465 45cd4e47 2019-08-25 stsp if (ref)
5466 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5467 4e759de4 2019-06-26 stsp free(refname);
5468 4e759de4 2019-06-26 stsp return err;
5469 4e759de4 2019-06-26 stsp }
5470 4e759de4 2019-06-26 stsp
5471 4e759de4 2019-06-26 stsp static const struct got_error *
5472 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5473 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5474 4e759de4 2019-06-26 stsp {
5475 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5476 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5477 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5478 d3f84d51 2019-07-11 stsp
5479 d3f84d51 2019-07-11 stsp /*
5480 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5481 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5482 d3f84d51 2019-07-11 stsp * an unintended typo.
5483 d3f84d51 2019-07-11 stsp */
5484 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5485 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5486 4e759de4 2019-06-26 stsp
5487 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5488 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
5489 4e759de4 2019-06-26 stsp goto done;
5490 4e759de4 2019-06-26 stsp }
5491 4e759de4 2019-06-26 stsp
5492 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5493 4e759de4 2019-06-26 stsp if (err == NULL) {
5494 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5495 4e759de4 2019-06-26 stsp goto done;
5496 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5497 4e759de4 2019-06-26 stsp goto done;
5498 4e759de4 2019-06-26 stsp
5499 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5500 4e759de4 2019-06-26 stsp if (err)
5501 4e759de4 2019-06-26 stsp goto done;
5502 4e759de4 2019-06-26 stsp
5503 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5504 d0eebce4 2019-03-11 stsp done:
5505 4e759de4 2019-06-26 stsp if (ref)
5506 4e759de4 2019-06-26 stsp got_ref_close(ref);
5507 4e759de4 2019-06-26 stsp free(base_refname);
5508 4e759de4 2019-06-26 stsp free(refname);
5509 4e759de4 2019-06-26 stsp return err;
5510 4e759de4 2019-06-26 stsp }
5511 4e759de4 2019-06-26 stsp
5512 4e759de4 2019-06-26 stsp static const struct got_error *
5513 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5514 4e759de4 2019-06-26 stsp {
5515 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5516 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5517 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5518 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5519 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5520 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5521 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5522 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5523 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5524 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5525 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5526 4e759de4 2019-06-26 stsp
5527 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5528 da76fce2 2020-02-24 stsp
5529 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5530 4e759de4 2019-06-26 stsp switch (ch) {
5531 a74f7e83 2019-11-10 stsp case 'c':
5532 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5533 a74f7e83 2019-11-10 stsp break;
5534 4e759de4 2019-06-26 stsp case 'd':
5535 4e759de4 2019-06-26 stsp delref = optarg;
5536 4e759de4 2019-06-26 stsp break;
5537 4e759de4 2019-06-26 stsp case 'r':
5538 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5539 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5540 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5541 9ba1d308 2019-10-21 stsp optarg);
5542 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5543 4e759de4 2019-06-26 stsp break;
5544 4e759de4 2019-06-26 stsp case 'l':
5545 4e759de4 2019-06-26 stsp do_list = 1;
5546 da76fce2 2020-02-24 stsp break;
5547 da76fce2 2020-02-24 stsp case 'n':
5548 da76fce2 2020-02-24 stsp do_update = 0;
5549 4e759de4 2019-06-26 stsp break;
5550 4e759de4 2019-06-26 stsp default:
5551 4e759de4 2019-06-26 stsp usage_branch();
5552 4e759de4 2019-06-26 stsp /* NOTREACHED */
5553 4e759de4 2019-06-26 stsp }
5554 4e759de4 2019-06-26 stsp }
5555 4e759de4 2019-06-26 stsp
5556 4e759de4 2019-06-26 stsp if (do_list && delref)
5557 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5558 4e759de4 2019-06-26 stsp
5559 4e759de4 2019-06-26 stsp argc -= optind;
5560 4e759de4 2019-06-26 stsp argv += optind;
5561 ad89fa31 2019-10-04 stsp
5562 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5563 ad89fa31 2019-10-04 stsp do_show = 1;
5564 4e759de4 2019-06-26 stsp
5565 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5566 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5567 a74f7e83 2019-11-10 stsp
5568 4e759de4 2019-06-26 stsp if (do_list || delref) {
5569 4e759de4 2019-06-26 stsp if (argc > 0)
5570 4e759de4 2019-06-26 stsp usage_branch();
5571 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5572 4e759de4 2019-06-26 stsp usage_branch();
5573 4e759de4 2019-06-26 stsp
5574 4e759de4 2019-06-26 stsp #ifndef PROFILE
5575 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5576 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5577 4e759de4 2019-06-26 stsp NULL) == -1)
5578 4e759de4 2019-06-26 stsp err(1, "pledge");
5579 4e759de4 2019-06-26 stsp } else {
5580 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5581 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5582 4e759de4 2019-06-26 stsp err(1, "pledge");
5583 4e759de4 2019-06-26 stsp }
5584 4e759de4 2019-06-26 stsp #endif
5585 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5586 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5587 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5588 4e759de4 2019-06-26 stsp goto done;
5589 4e759de4 2019-06-26 stsp }
5590 4e759de4 2019-06-26 stsp
5591 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5592 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5593 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5594 4e759de4 2019-06-26 stsp goto done;
5595 4e759de4 2019-06-26 stsp else
5596 4e759de4 2019-06-26 stsp error = NULL;
5597 4e759de4 2019-06-26 stsp if (worktree) {
5598 4e759de4 2019-06-26 stsp repo_path =
5599 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5600 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5601 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5602 4e759de4 2019-06-26 stsp if (error)
5603 4e759de4 2019-06-26 stsp goto done;
5604 4e759de4 2019-06-26 stsp } else {
5605 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5606 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5607 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5608 4e759de4 2019-06-26 stsp goto done;
5609 4e759de4 2019-06-26 stsp }
5610 4e759de4 2019-06-26 stsp }
5611 4e759de4 2019-06-26 stsp }
5612 4e759de4 2019-06-26 stsp
5613 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5614 4e759de4 2019-06-26 stsp if (error != NULL)
5615 4e759de4 2019-06-26 stsp goto done;
5616 4e759de4 2019-06-26 stsp
5617 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5618 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5619 4e759de4 2019-06-26 stsp if (error)
5620 4e759de4 2019-06-26 stsp goto done;
5621 4e759de4 2019-06-26 stsp
5622 ad89fa31 2019-10-04 stsp if (do_show)
5623 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5624 ad89fa31 2019-10-04 stsp else if (do_list)
5625 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5626 4e759de4 2019-06-26 stsp else if (delref)
5627 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5628 4e759de4 2019-06-26 stsp else {
5629 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5630 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5631 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5632 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5633 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5634 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5635 a74f7e83 2019-11-10 stsp if (error)
5636 a74f7e83 2019-11-10 stsp goto done;
5637 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5638 da76fce2 2020-02-24 stsp if (error)
5639 da76fce2 2020-02-24 stsp goto done;
5640 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5641 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5642 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5643 da76fce2 2020-02-24 stsp
5644 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5645 da76fce2 2020-02-24 stsp if (error)
5646 da76fce2 2020-02-24 stsp goto done;
5647 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5648 da76fce2 2020-02-24 stsp worktree);
5649 da76fce2 2020-02-24 stsp if (error)
5650 da76fce2 2020-02-24 stsp goto done;
5651 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5652 da76fce2 2020-02-24 stsp == -1) {
5653 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5654 da76fce2 2020-02-24 stsp goto done;
5655 da76fce2 2020-02-24 stsp }
5656 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5657 da76fce2 2020-02-24 stsp free(branch_refname);
5658 da76fce2 2020-02-24 stsp if (error)
5659 da76fce2 2020-02-24 stsp goto done;
5660 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5661 da76fce2 2020-02-24 stsp repo);
5662 da76fce2 2020-02-24 stsp if (error)
5663 da76fce2 2020-02-24 stsp goto done;
5664 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5665 da76fce2 2020-02-24 stsp commit_id);
5666 da76fce2 2020-02-24 stsp if (error)
5667 da76fce2 2020-02-24 stsp goto done;
5668 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5669 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5670 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5671 9627c110 2020-04-18 stsp NULL);
5672 da76fce2 2020-02-24 stsp if (error)
5673 da76fce2 2020-02-24 stsp goto done;
5674 9627c110 2020-04-18 stsp if (upa.did_something)
5675 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5676 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5677 da76fce2 2020-02-24 stsp }
5678 4e759de4 2019-06-26 stsp }
5679 4e759de4 2019-06-26 stsp done:
5680 da76fce2 2020-02-24 stsp if (ref)
5681 da76fce2 2020-02-24 stsp got_ref_close(ref);
5682 d0eebce4 2019-03-11 stsp if (repo)
5683 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5684 d0eebce4 2019-03-11 stsp if (worktree)
5685 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5686 d0eebce4 2019-03-11 stsp free(cwd);
5687 d0eebce4 2019-03-11 stsp free(repo_path);
5688 da76fce2 2020-02-24 stsp free(commit_id);
5689 da76fce2 2020-02-24 stsp free(commit_id_str);
5690 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5691 da76fce2 2020-02-24 stsp free((char *)pe->path);
5692 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5693 d00136be 2019-03-26 stsp return error;
5694 d00136be 2019-03-26 stsp }
5695 d00136be 2019-03-26 stsp
5696 8e7bd50a 2019-08-22 stsp
5697 d00136be 2019-03-26 stsp __dead static void
5698 8e7bd50a 2019-08-22 stsp usage_tag(void)
5699 8e7bd50a 2019-08-22 stsp {
5700 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5701 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5702 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5703 8e7bd50a 2019-08-22 stsp exit(1);
5704 b8bad2ba 2019-08-23 stsp }
5705 b8bad2ba 2019-08-23 stsp
5706 b8bad2ba 2019-08-23 stsp #if 0
5707 b8bad2ba 2019-08-23 stsp static const struct got_error *
5708 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5709 b8bad2ba 2019-08-23 stsp {
5710 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5711 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5712 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5713 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5714 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5715 b8bad2ba 2019-08-23 stsp
5716 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5717 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5718 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5719 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5720 b8bad2ba 2019-08-23 stsp if (err)
5721 b8bad2ba 2019-08-23 stsp return err;
5722 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5723 b8bad2ba 2019-08-23 stsp continue;
5724 b8bad2ba 2019-08-23 stsp } else {
5725 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5726 b8bad2ba 2019-08-23 stsp if (err)
5727 b8bad2ba 2019-08-23 stsp break;
5728 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5729 b8bad2ba 2019-08-23 stsp free(re_id);
5730 b8bad2ba 2019-08-23 stsp if (err)
5731 b8bad2ba 2019-08-23 stsp break;
5732 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5733 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5734 b8bad2ba 2019-08-23 stsp }
5735 b8bad2ba 2019-08-23 stsp
5736 b8bad2ba 2019-08-23 stsp while (se) {
5737 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5738 b8bad2ba 2019-08-23 stsp if (err)
5739 b8bad2ba 2019-08-23 stsp break;
5740 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5741 b8bad2ba 2019-08-23 stsp free(se_id);
5742 b8bad2ba 2019-08-23 stsp if (err)
5743 b8bad2ba 2019-08-23 stsp break;
5744 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5745 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5746 b8bad2ba 2019-08-23 stsp
5747 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5748 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5749 b8bad2ba 2019-08-23 stsp if (err)
5750 b8bad2ba 2019-08-23 stsp return err;
5751 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5752 b8bad2ba 2019-08-23 stsp break;
5753 b8bad2ba 2019-08-23 stsp }
5754 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5755 b8bad2ba 2019-08-23 stsp continue;
5756 b8bad2ba 2019-08-23 stsp }
5757 b8bad2ba 2019-08-23 stsp }
5758 b8bad2ba 2019-08-23 stsp done:
5759 b8bad2ba 2019-08-23 stsp return err;
5760 8e7bd50a 2019-08-22 stsp }
5761 b8bad2ba 2019-08-23 stsp #endif
5762 b8bad2ba 2019-08-23 stsp
5763 b8bad2ba 2019-08-23 stsp static const struct got_error *
5764 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5765 8e7bd50a 2019-08-22 stsp {
5766 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5767 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5768 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5769 8e7bd50a 2019-08-22 stsp
5770 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5771 8e7bd50a 2019-08-22 stsp
5772 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5773 8e7bd50a 2019-08-22 stsp if (err)
5774 8e7bd50a 2019-08-22 stsp return err;
5775 8e7bd50a 2019-08-22 stsp
5776 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5777 8e7bd50a 2019-08-22 stsp const char *refname;
5778 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5779 8e7bd50a 2019-08-22 stsp char datebuf[26];
5780 d4efa91b 2020-01-14 stsp const char *tagger;
5781 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5782 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5783 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5784 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5785 8e7bd50a 2019-08-22 stsp
5786 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5787 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5788 8e7bd50a 2019-08-22 stsp continue;
5789 8e7bd50a 2019-08-22 stsp refname += 10;
5790 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5791 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5792 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5793 8e7bd50a 2019-08-22 stsp break;
5794 8e7bd50a 2019-08-22 stsp }
5795 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5796 8e7bd50a 2019-08-22 stsp free(refstr);
5797 8e7bd50a 2019-08-22 stsp
5798 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5799 8e7bd50a 2019-08-22 stsp if (err)
5800 8e7bd50a 2019-08-22 stsp break;
5801 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5802 d4efa91b 2020-01-14 stsp if (err) {
5803 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5804 d4efa91b 2020-01-14 stsp free(id);
5805 d4efa91b 2020-01-14 stsp break;
5806 d4efa91b 2020-01-14 stsp }
5807 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5808 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5809 d4efa91b 2020-01-14 stsp if (err) {
5810 d4efa91b 2020-01-14 stsp free(id);
5811 d4efa91b 2020-01-14 stsp break;
5812 d4efa91b 2020-01-14 stsp }
5813 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5814 d4efa91b 2020-01-14 stsp tagger_time =
5815 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5816 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5817 d4efa91b 2020-01-14 stsp free(id);
5818 d4efa91b 2020-01-14 stsp if (err)
5819 d4efa91b 2020-01-14 stsp break;
5820 d4efa91b 2020-01-14 stsp } else {
5821 d4efa91b 2020-01-14 stsp free(id);
5822 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5823 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5824 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5825 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5826 d4efa91b 2020-01-14 stsp if (err)
5827 d4efa91b 2020-01-14 stsp break;
5828 d4efa91b 2020-01-14 stsp }
5829 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5830 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5831 2417344c 2019-08-23 stsp if (datestr)
5832 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5833 d4efa91b 2020-01-14 stsp if (commit)
5834 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5835 d4efa91b 2020-01-14 stsp else {
5836 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5837 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5838 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5839 d4efa91b 2020-01-14 stsp id_str);
5840 d4efa91b 2020-01-14 stsp break;
5841 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5842 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5843 d4efa91b 2020-01-14 stsp id_str);
5844 d4efa91b 2020-01-14 stsp break;
5845 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5846 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5847 d4efa91b 2020-01-14 stsp id_str);
5848 d4efa91b 2020-01-14 stsp break;
5849 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5850 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5851 d4efa91b 2020-01-14 stsp id_str);
5852 d4efa91b 2020-01-14 stsp break;
5853 d4efa91b 2020-01-14 stsp default:
5854 d4efa91b 2020-01-14 stsp break;
5855 d4efa91b 2020-01-14 stsp }
5856 8e7bd50a 2019-08-22 stsp }
5857 8e7bd50a 2019-08-22 stsp free(id_str);
5858 d4efa91b 2020-01-14 stsp if (commit) {
5859 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5860 d4efa91b 2020-01-14 stsp if (err)
5861 d4efa91b 2020-01-14 stsp break;
5862 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5863 d4efa91b 2020-01-14 stsp } else {
5864 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5865 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5866 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5867 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5868 d4efa91b 2020-01-14 stsp break;
5869 d4efa91b 2020-01-14 stsp }
5870 8e7bd50a 2019-08-22 stsp }
5871 8e7bd50a 2019-08-22 stsp
5872 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5873 8e7bd50a 2019-08-22 stsp do {
5874 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5875 8e7bd50a 2019-08-22 stsp if (line)
5876 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5877 8e7bd50a 2019-08-22 stsp } while (line);
5878 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5879 8e7bd50a 2019-08-22 stsp }
5880 8e7bd50a 2019-08-22 stsp
5881 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5882 8e7bd50a 2019-08-22 stsp return NULL;
5883 8e7bd50a 2019-08-22 stsp }
5884 8e7bd50a 2019-08-22 stsp
5885 8e7bd50a 2019-08-22 stsp static const struct got_error *
5886 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5887 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5888 8e7bd50a 2019-08-22 stsp {
5889 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5890 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5891 f372d5cd 2019-10-21 stsp char *editor = NULL;
5892 1601cb9f 2020-09-11 naddy int initial_content_len;
5893 8e7bd50a 2019-08-22 stsp int fd = -1;
5894 8e7bd50a 2019-08-22 stsp
5895 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5896 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5897 8e7bd50a 2019-08-22 stsp goto done;
5898 8e7bd50a 2019-08-22 stsp }
5899 8e7bd50a 2019-08-22 stsp
5900 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5901 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5902 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5903 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5904 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5905 8e7bd50a 2019-08-22 stsp goto done;
5906 8e7bd50a 2019-08-22 stsp }
5907 8e7bd50a 2019-08-22 stsp
5908 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5909 8e7bd50a 2019-08-22 stsp if (err)
5910 8e7bd50a 2019-08-22 stsp goto done;
5911 8e7bd50a 2019-08-22 stsp
5912 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5913 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5914 97972933 2020-09-11 stsp goto done;
5915 97972933 2020-09-11 stsp }
5916 8e7bd50a 2019-08-22 stsp
5917 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5918 8e7bd50a 2019-08-22 stsp if (err)
5919 8e7bd50a 2019-08-22 stsp goto done;
5920 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
5921 bfa12d5e 2020-09-26 stsp initial_content_len);
5922 8e7bd50a 2019-08-22 stsp done:
5923 8e7bd50a 2019-08-22 stsp free(initial_content);
5924 8e7bd50a 2019-08-22 stsp free(template);
5925 8e7bd50a 2019-08-22 stsp free(editor);
5926 8e7bd50a 2019-08-22 stsp
5927 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5928 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5929 97972933 2020-09-11 stsp
5930 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5931 59f86c76 2020-09-11 stsp if (err == NULL)
5932 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5933 59f86c76 2020-09-11 stsp if (err) {
5934 59f86c76 2020-09-11 stsp free(*tagmsg);
5935 59f86c76 2020-09-11 stsp *tagmsg = NULL;
5936 8e7bd50a 2019-08-22 stsp }
5937 8e7bd50a 2019-08-22 stsp return err;
5938 8e7bd50a 2019-08-22 stsp }
5939 8e7bd50a 2019-08-22 stsp
5940 8e7bd50a 2019-08-22 stsp static const struct got_error *
5941 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5942 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5943 8e7bd50a 2019-08-22 stsp {
5944 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5945 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5946 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5947 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5948 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5949 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5950 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5951 8e7bd50a 2019-08-22 stsp
5952 8e7bd50a 2019-08-22 stsp /*
5953 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5954 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5955 8e7bd50a 2019-08-22 stsp * an unintended typo.
5956 8e7bd50a 2019-08-22 stsp */
5957 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5958 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5959 8e7bd50a 2019-08-22 stsp
5960 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
5961 8e7bd50a 2019-08-22 stsp if (err)
5962 8e7bd50a 2019-08-22 stsp return err;
5963 8e7bd50a 2019-08-22 stsp
5964 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5965 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5966 8e7bd50a 2019-08-22 stsp if (err)
5967 8e7bd50a 2019-08-22 stsp goto done;
5968 8e7bd50a 2019-08-22 stsp
5969 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5970 8e7bd50a 2019-08-22 stsp if (err)
5971 8e7bd50a 2019-08-22 stsp goto done;
5972 8e7bd50a 2019-08-22 stsp
5973 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5974 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5975 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5976 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5977 8e7bd50a 2019-08-22 stsp goto done;
5978 8e7bd50a 2019-08-22 stsp }
5979 8e7bd50a 2019-08-22 stsp tag_name += 10;
5980 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5981 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5982 8e7bd50a 2019-08-22 stsp goto done;
5983 8e7bd50a 2019-08-22 stsp }
5984 8e7bd50a 2019-08-22 stsp
5985 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5986 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5987 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5988 8e7bd50a 2019-08-22 stsp goto done;
5989 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5990 8e7bd50a 2019-08-22 stsp goto done;
5991 8e7bd50a 2019-08-22 stsp
5992 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5993 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5994 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5995 f372d5cd 2019-10-21 stsp if (err) {
5996 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5997 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5998 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5999 8e7bd50a 2019-08-22 stsp goto done;
6000 f372d5cd 2019-10-21 stsp }
6001 8e7bd50a 2019-08-22 stsp }
6002 8e7bd50a 2019-08-22 stsp
6003 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
6004 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
6005 f372d5cd 2019-10-21 stsp if (err) {
6006 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6007 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6008 8e7bd50a 2019-08-22 stsp goto done;
6009 f372d5cd 2019-10-21 stsp }
6010 8e7bd50a 2019-08-22 stsp
6011 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
6012 f372d5cd 2019-10-21 stsp if (err) {
6013 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6014 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6015 8e7bd50a 2019-08-22 stsp goto done;
6016 f372d5cd 2019-10-21 stsp }
6017 8e7bd50a 2019-08-22 stsp
6018 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
6019 f372d5cd 2019-10-21 stsp if (err) {
6020 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6021 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6022 f372d5cd 2019-10-21 stsp goto done;
6023 f372d5cd 2019-10-21 stsp }
6024 8e7bd50a 2019-08-22 stsp
6025 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
6026 f372d5cd 2019-10-21 stsp if (err) {
6027 f372d5cd 2019-10-21 stsp if (tagmsg_path)
6028 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
6029 f372d5cd 2019-10-21 stsp goto done;
6030 8e7bd50a 2019-08-22 stsp }
6031 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
6032 8e7bd50a 2019-08-22 stsp done:
6033 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6034 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6035 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6036 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6037 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6038 f372d5cd 2019-10-21 stsp free(tag_id_str);
6039 8e7bd50a 2019-08-22 stsp if (ref)
6040 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6041 8e7bd50a 2019-08-22 stsp free(commit_id);
6042 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6043 8e7bd50a 2019-08-22 stsp free(refname);
6044 8e7bd50a 2019-08-22 stsp free(tagmsg);
6045 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6046 aba9c984 2019-09-08 stsp free(tagger);
6047 8e7bd50a 2019-08-22 stsp return err;
6048 8e7bd50a 2019-08-22 stsp }
6049 8e7bd50a 2019-08-22 stsp
6050 8e7bd50a 2019-08-22 stsp static const struct got_error *
6051 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6052 8e7bd50a 2019-08-22 stsp {
6053 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6054 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6055 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6056 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6057 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6058 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6059 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6060 8e7bd50a 2019-08-22 stsp
6061 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6062 8e7bd50a 2019-08-22 stsp switch (ch) {
6063 80106605 2020-02-24 stsp case 'c':
6064 80106605 2020-02-24 stsp commit_id_arg = optarg;
6065 80106605 2020-02-24 stsp break;
6066 8e7bd50a 2019-08-22 stsp case 'm':
6067 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6068 8e7bd50a 2019-08-22 stsp break;
6069 8e7bd50a 2019-08-22 stsp case 'r':
6070 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6071 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6072 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6073 9ba1d308 2019-10-21 stsp optarg);
6074 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6075 8e7bd50a 2019-08-22 stsp break;
6076 8e7bd50a 2019-08-22 stsp case 'l':
6077 8e7bd50a 2019-08-22 stsp do_list = 1;
6078 8e7bd50a 2019-08-22 stsp break;
6079 8e7bd50a 2019-08-22 stsp default:
6080 8e7bd50a 2019-08-22 stsp usage_tag();
6081 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6082 8e7bd50a 2019-08-22 stsp }
6083 8e7bd50a 2019-08-22 stsp }
6084 8e7bd50a 2019-08-22 stsp
6085 8e7bd50a 2019-08-22 stsp argc -= optind;
6086 8e7bd50a 2019-08-22 stsp argv += optind;
6087 8e7bd50a 2019-08-22 stsp
6088 8e7bd50a 2019-08-22 stsp if (do_list) {
6089 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6090 775ce909 2020-03-22 stsp errx(1,
6091 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6092 8e7bd50a 2019-08-22 stsp if (tagmsg)
6093 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
6094 8e7bd50a 2019-08-22 stsp if (argc > 0)
6095 8e7bd50a 2019-08-22 stsp usage_tag();
6096 80106605 2020-02-24 stsp } else if (argc != 1)
6097 8e7bd50a 2019-08-22 stsp usage_tag();
6098 80106605 2020-02-24 stsp
6099 a2887370 2019-08-23 stsp tag_name = argv[0];
6100 8e7bd50a 2019-08-22 stsp
6101 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6102 8e7bd50a 2019-08-22 stsp if (do_list) {
6103 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6104 8e7bd50a 2019-08-22 stsp NULL) == -1)
6105 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6106 8e7bd50a 2019-08-22 stsp } else {
6107 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6108 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6109 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6110 8e7bd50a 2019-08-22 stsp }
6111 8e7bd50a 2019-08-22 stsp #endif
6112 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6113 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6114 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6115 8e7bd50a 2019-08-22 stsp goto done;
6116 8e7bd50a 2019-08-22 stsp }
6117 8e7bd50a 2019-08-22 stsp
6118 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6119 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6120 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6121 8e7bd50a 2019-08-22 stsp goto done;
6122 8e7bd50a 2019-08-22 stsp else
6123 8e7bd50a 2019-08-22 stsp error = NULL;
6124 8e7bd50a 2019-08-22 stsp if (worktree) {
6125 8e7bd50a 2019-08-22 stsp repo_path =
6126 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6127 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6128 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6129 8e7bd50a 2019-08-22 stsp if (error)
6130 8e7bd50a 2019-08-22 stsp goto done;
6131 8e7bd50a 2019-08-22 stsp } else {
6132 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6133 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6134 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6135 8e7bd50a 2019-08-22 stsp goto done;
6136 8e7bd50a 2019-08-22 stsp }
6137 8e7bd50a 2019-08-22 stsp }
6138 8e7bd50a 2019-08-22 stsp }
6139 8e7bd50a 2019-08-22 stsp
6140 8e7bd50a 2019-08-22 stsp if (do_list) {
6141 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6142 c9956ddf 2019-09-08 stsp if (error != NULL)
6143 c9956ddf 2019-09-08 stsp goto done;
6144 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6145 8e7bd50a 2019-08-22 stsp if (error)
6146 8e7bd50a 2019-08-22 stsp goto done;
6147 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6148 8e7bd50a 2019-08-22 stsp } else {
6149 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6150 c9956ddf 2019-09-08 stsp if (error)
6151 c9956ddf 2019-09-08 stsp goto done;
6152 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6153 c9956ddf 2019-09-08 stsp if (error != NULL)
6154 c9956ddf 2019-09-08 stsp goto done;
6155 c9956ddf 2019-09-08 stsp
6156 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6157 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6158 8e7bd50a 2019-08-22 stsp if (error)
6159 8e7bd50a 2019-08-22 stsp goto done;
6160 8e7bd50a 2019-08-22 stsp }
6161 8e7bd50a 2019-08-22 stsp
6162 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6163 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6164 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6165 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6166 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6167 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6168 8e7bd50a 2019-08-22 stsp if (error)
6169 8e7bd50a 2019-08-22 stsp goto done;
6170 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6171 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6172 8e7bd50a 2019-08-22 stsp if (error)
6173 8e7bd50a 2019-08-22 stsp goto done;
6174 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6175 8e7bd50a 2019-08-22 stsp free(commit_id);
6176 8e7bd50a 2019-08-22 stsp if (error)
6177 8e7bd50a 2019-08-22 stsp goto done;
6178 8e7bd50a 2019-08-22 stsp }
6179 8e7bd50a 2019-08-22 stsp
6180 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6181 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6182 8e7bd50a 2019-08-22 stsp }
6183 8e7bd50a 2019-08-22 stsp done:
6184 8e7bd50a 2019-08-22 stsp if (repo)
6185 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
6186 8e7bd50a 2019-08-22 stsp if (worktree)
6187 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6188 8e7bd50a 2019-08-22 stsp free(cwd);
6189 8e7bd50a 2019-08-22 stsp free(repo_path);
6190 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6191 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6192 8e7bd50a 2019-08-22 stsp return error;
6193 8e7bd50a 2019-08-22 stsp }
6194 8e7bd50a 2019-08-22 stsp
6195 8e7bd50a 2019-08-22 stsp __dead static void
6196 d00136be 2019-03-26 stsp usage_add(void)
6197 d00136be 2019-03-26 stsp {
6198 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6199 022fae89 2019-12-06 tracey getprogname());
6200 d00136be 2019-03-26 stsp exit(1);
6201 d00136be 2019-03-26 stsp }
6202 d00136be 2019-03-26 stsp
6203 d00136be 2019-03-26 stsp static const struct got_error *
6204 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6205 4e68cba3 2019-11-23 stsp {
6206 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6207 4e68cba3 2019-11-23 stsp path++;
6208 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6209 4e68cba3 2019-11-23 stsp return NULL;
6210 4e68cba3 2019-11-23 stsp }
6211 4e68cba3 2019-11-23 stsp
6212 4e68cba3 2019-11-23 stsp static const struct got_error *
6213 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6214 d00136be 2019-03-26 stsp {
6215 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6216 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6217 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6218 1dd54920 2019-05-11 stsp char *cwd = NULL;
6219 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6220 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6221 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6222 1dd54920 2019-05-11 stsp
6223 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6224 d00136be 2019-03-26 stsp
6225 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6226 d00136be 2019-03-26 stsp switch (ch) {
6227 022fae89 2019-12-06 tracey case 'I':
6228 022fae89 2019-12-06 tracey no_ignores = 1;
6229 022fae89 2019-12-06 tracey break;
6230 4e68cba3 2019-11-23 stsp case 'R':
6231 4e68cba3 2019-11-23 stsp can_recurse = 1;
6232 4e68cba3 2019-11-23 stsp break;
6233 d00136be 2019-03-26 stsp default:
6234 d00136be 2019-03-26 stsp usage_add();
6235 d00136be 2019-03-26 stsp /* NOTREACHED */
6236 d00136be 2019-03-26 stsp }
6237 d00136be 2019-03-26 stsp }
6238 d00136be 2019-03-26 stsp
6239 d00136be 2019-03-26 stsp argc -= optind;
6240 d00136be 2019-03-26 stsp argv += optind;
6241 d00136be 2019-03-26 stsp
6242 43012d58 2019-07-14 stsp #ifndef PROFILE
6243 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6244 43012d58 2019-07-14 stsp NULL) == -1)
6245 43012d58 2019-07-14 stsp err(1, "pledge");
6246 43012d58 2019-07-14 stsp #endif
6247 723c305c 2019-05-11 jcs if (argc < 1)
6248 d00136be 2019-03-26 stsp usage_add();
6249 d00136be 2019-03-26 stsp
6250 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6251 d00136be 2019-03-26 stsp if (cwd == NULL) {
6252 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6253 d00136be 2019-03-26 stsp goto done;
6254 d00136be 2019-03-26 stsp }
6255 723c305c 2019-05-11 jcs
6256 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6257 fa51e947 2020-03-27 stsp if (error) {
6258 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6259 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6260 d00136be 2019-03-26 stsp goto done;
6261 fa51e947 2020-03-27 stsp }
6262 d00136be 2019-03-26 stsp
6263 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6264 c9956ddf 2019-09-08 stsp NULL);
6265 031a5338 2019-03-26 stsp if (error != NULL)
6266 031a5338 2019-03-26 stsp goto done;
6267 031a5338 2019-03-26 stsp
6268 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6269 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6270 d00136be 2019-03-26 stsp if (error)
6271 d00136be 2019-03-26 stsp goto done;
6272 d00136be 2019-03-26 stsp
6273 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6274 6d022e97 2019-08-04 stsp if (error)
6275 022fae89 2019-12-06 tracey goto done;
6276 022fae89 2019-12-06 tracey
6277 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6278 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6279 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6280 6d022e97 2019-08-04 stsp goto done;
6281 723c305c 2019-05-11 jcs
6282 022fae89 2019-12-06 tracey }
6283 022fae89 2019-12-06 tracey
6284 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6285 4e68cba3 2019-11-23 stsp char *ondisk_path;
6286 4e68cba3 2019-11-23 stsp struct stat sb;
6287 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6288 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6289 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6290 4e68cba3 2019-11-23 stsp pe->path) == -1) {
6291 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6292 4e68cba3 2019-11-23 stsp goto done;
6293 4e68cba3 2019-11-23 stsp }
6294 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6295 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6296 4e68cba3 2019-11-23 stsp free(ondisk_path);
6297 4e68cba3 2019-11-23 stsp continue;
6298 4e68cba3 2019-11-23 stsp }
6299 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6300 4e68cba3 2019-11-23 stsp ondisk_path);
6301 4e68cba3 2019-11-23 stsp free(ondisk_path);
6302 4e68cba3 2019-11-23 stsp goto done;
6303 4e68cba3 2019-11-23 stsp }
6304 4e68cba3 2019-11-23 stsp free(ondisk_path);
6305 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6306 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6307 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6308 4e68cba3 2019-11-23 stsp goto done;
6309 4e68cba3 2019-11-23 stsp }
6310 4e68cba3 2019-11-23 stsp }
6311 4e68cba3 2019-11-23 stsp }
6312 022fae89 2019-12-06 tracey
6313 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6314 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6315 d00136be 2019-03-26 stsp done:
6316 031a5338 2019-03-26 stsp if (repo)
6317 031a5338 2019-03-26 stsp got_repo_close(repo);
6318 d00136be 2019-03-26 stsp if (worktree)
6319 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6320 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6321 1dd54920 2019-05-11 stsp free((char *)pe->path);
6322 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6323 2ec1f75b 2019-03-26 stsp free(cwd);
6324 2ec1f75b 2019-03-26 stsp return error;
6325 2ec1f75b 2019-03-26 stsp }
6326 2ec1f75b 2019-03-26 stsp
6327 2ec1f75b 2019-03-26 stsp __dead static void
6328 648e4ef7 2019-07-09 stsp usage_remove(void)
6329 2ec1f75b 2019-03-26 stsp {
6330 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6331 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6332 2ec1f75b 2019-03-26 stsp exit(1);
6333 2a06fe5f 2019-08-24 stsp }
6334 2a06fe5f 2019-08-24 stsp
6335 2a06fe5f 2019-08-24 stsp static const struct got_error *
6336 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6337 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6338 2a06fe5f 2019-08-24 stsp {
6339 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6340 f2a9dc41 2019-12-13 tracey path++;
6341 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6342 2a06fe5f 2019-08-24 stsp return NULL;
6343 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6344 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6345 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6346 2a06fe5f 2019-08-24 stsp return NULL;
6347 2ec1f75b 2019-03-26 stsp }
6348 2ec1f75b 2019-03-26 stsp
6349 2ec1f75b 2019-03-26 stsp static const struct got_error *
6350 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6351 2ec1f75b 2019-03-26 stsp {
6352 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6353 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6354 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6355 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6356 17ed4618 2019-06-02 stsp char *cwd = NULL;
6357 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6358 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6359 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6360 2ec1f75b 2019-03-26 stsp
6361 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6362 17ed4618 2019-06-02 stsp
6363 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6364 2ec1f75b 2019-03-26 stsp switch (ch) {
6365 2ec1f75b 2019-03-26 stsp case 'f':
6366 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6367 2ec1f75b 2019-03-26 stsp break;
6368 70e3e7f5 2019-12-13 tracey case 'k':
6369 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6370 70e3e7f5 2019-12-13 tracey break;
6371 f2a9dc41 2019-12-13 tracey case 'R':
6372 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6373 f2a9dc41 2019-12-13 tracey break;
6374 766841c2 2020-08-13 stsp case 's':
6375 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6376 766841c2 2020-08-13 stsp switch (optarg[i]) {
6377 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6378 766841c2 2020-08-13 stsp delete_local_mods = 1;
6379 766841c2 2020-08-13 stsp break;
6380 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6381 766841c2 2020-08-13 stsp break;
6382 766841c2 2020-08-13 stsp default:
6383 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6384 766841c2 2020-08-13 stsp optarg[i]);
6385 766841c2 2020-08-13 stsp }
6386 766841c2 2020-08-13 stsp }
6387 766841c2 2020-08-13 stsp status_codes = optarg;
6388 766841c2 2020-08-13 stsp break;
6389 2ec1f75b 2019-03-26 stsp default:
6390 f2a9dc41 2019-12-13 tracey usage_remove();
6391 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6392 2ec1f75b 2019-03-26 stsp }
6393 2ec1f75b 2019-03-26 stsp }
6394 2ec1f75b 2019-03-26 stsp
6395 2ec1f75b 2019-03-26 stsp argc -= optind;
6396 2ec1f75b 2019-03-26 stsp argv += optind;
6397 2ec1f75b 2019-03-26 stsp
6398 43012d58 2019-07-14 stsp #ifndef PROFILE
6399 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6400 43012d58 2019-07-14 stsp NULL) == -1)
6401 43012d58 2019-07-14 stsp err(1, "pledge");
6402 43012d58 2019-07-14 stsp #endif
6403 17ed4618 2019-06-02 stsp if (argc < 1)
6404 648e4ef7 2019-07-09 stsp usage_remove();
6405 2ec1f75b 2019-03-26 stsp
6406 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6407 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6408 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6409 2ec1f75b 2019-03-26 stsp goto done;
6410 2ec1f75b 2019-03-26 stsp }
6411 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6412 fa51e947 2020-03-27 stsp if (error) {
6413 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6414 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6415 2ec1f75b 2019-03-26 stsp goto done;
6416 fa51e947 2020-03-27 stsp }
6417 2ec1f75b 2019-03-26 stsp
6418 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6419 c9956ddf 2019-09-08 stsp NULL);
6420 2af4a041 2019-05-11 jcs if (error)
6421 2ec1f75b 2019-03-26 stsp goto done;
6422 2ec1f75b 2019-03-26 stsp
6423 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6424 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6425 2ec1f75b 2019-03-26 stsp if (error)
6426 2ec1f75b 2019-03-26 stsp goto done;
6427 2ec1f75b 2019-03-26 stsp
6428 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6429 6d022e97 2019-08-04 stsp if (error)
6430 6d022e97 2019-08-04 stsp goto done;
6431 17ed4618 2019-06-02 stsp
6432 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6433 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6434 f2a9dc41 2019-12-13 tracey struct stat sb;
6435 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6436 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6437 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6438 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
6439 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6440 f2a9dc41 2019-12-13 tracey goto done;
6441 f2a9dc41 2019-12-13 tracey }
6442 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6443 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6444 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6445 f2a9dc41 2019-12-13 tracey continue;
6446 f2a9dc41 2019-12-13 tracey }
6447 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6448 f2a9dc41 2019-12-13 tracey ondisk_path);
6449 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6450 f2a9dc41 2019-12-13 tracey goto done;
6451 f2a9dc41 2019-12-13 tracey }
6452 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6453 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6454 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6455 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6456 f2a9dc41 2019-12-13 tracey goto done;
6457 f2a9dc41 2019-12-13 tracey }
6458 f2a9dc41 2019-12-13 tracey }
6459 f2a9dc41 2019-12-13 tracey }
6460 f2a9dc41 2019-12-13 tracey
6461 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6462 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6463 766841c2 2020-08-13 stsp repo, keep_on_disk);
6464 a129376b 2019-03-28 stsp done:
6465 a129376b 2019-03-28 stsp if (repo)
6466 a129376b 2019-03-28 stsp got_repo_close(repo);
6467 a129376b 2019-03-28 stsp if (worktree)
6468 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6469 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6470 17ed4618 2019-06-02 stsp free((char *)pe->path);
6471 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6472 a129376b 2019-03-28 stsp free(cwd);
6473 a129376b 2019-03-28 stsp return error;
6474 a129376b 2019-03-28 stsp }
6475 a129376b 2019-03-28 stsp
6476 a129376b 2019-03-28 stsp __dead static void
6477 a129376b 2019-03-28 stsp usage_revert(void)
6478 a129376b 2019-03-28 stsp {
6479 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6480 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6481 a129376b 2019-03-28 stsp exit(1);
6482 a129376b 2019-03-28 stsp }
6483 a129376b 2019-03-28 stsp
6484 1ee397ad 2019-07-12 stsp static const struct got_error *
6485 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6486 a129376b 2019-03-28 stsp {
6487 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6488 fb9704af 2020-01-27 stsp return NULL;
6489 fb9704af 2020-01-27 stsp
6490 a129376b 2019-03-28 stsp while (path[0] == '/')
6491 a129376b 2019-03-28 stsp path++;
6492 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6493 33aa809d 2019-08-08 stsp return NULL;
6494 33aa809d 2019-08-08 stsp }
6495 33aa809d 2019-08-08 stsp
6496 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6497 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6498 33aa809d 2019-08-08 stsp const char *action;
6499 33aa809d 2019-08-08 stsp };
6500 33aa809d 2019-08-08 stsp
6501 33aa809d 2019-08-08 stsp static const struct got_error *
6502 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6503 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6504 33aa809d 2019-08-08 stsp {
6505 33aa809d 2019-08-08 stsp char *line = NULL;
6506 33aa809d 2019-08-08 stsp size_t linesize = 0;
6507 33aa809d 2019-08-08 stsp ssize_t linelen;
6508 33aa809d 2019-08-08 stsp
6509 33aa809d 2019-08-08 stsp switch (status) {
6510 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6511 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6512 33aa809d 2019-08-08 stsp break;
6513 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6514 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6515 33aa809d 2019-08-08 stsp break;
6516 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6517 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6518 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6519 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6520 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6521 33aa809d 2019-08-08 stsp printf("%s", line);
6522 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6523 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6524 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6525 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6526 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6527 33aa809d 2019-08-08 stsp break;
6528 33aa809d 2019-08-08 stsp default:
6529 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6530 33aa809d 2019-08-08 stsp }
6531 33aa809d 2019-08-08 stsp
6532 33aa809d 2019-08-08 stsp return NULL;
6533 33aa809d 2019-08-08 stsp }
6534 33aa809d 2019-08-08 stsp
6535 33aa809d 2019-08-08 stsp static const struct got_error *
6536 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6537 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6538 33aa809d 2019-08-08 stsp {
6539 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6540 33aa809d 2019-08-08 stsp char *line = NULL;
6541 33aa809d 2019-08-08 stsp size_t linesize = 0;
6542 33aa809d 2019-08-08 stsp ssize_t linelen;
6543 33aa809d 2019-08-08 stsp int resp = ' ';
6544 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6545 33aa809d 2019-08-08 stsp
6546 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6547 33aa809d 2019-08-08 stsp
6548 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6549 33aa809d 2019-08-08 stsp char *nl;
6550 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6551 33aa809d 2019-08-08 stsp a->action);
6552 33aa809d 2019-08-08 stsp if (err)
6553 33aa809d 2019-08-08 stsp return err;
6554 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6555 33aa809d 2019-08-08 stsp if (linelen == -1) {
6556 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6557 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6558 33aa809d 2019-08-08 stsp return NULL;
6559 33aa809d 2019-08-08 stsp }
6560 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6561 33aa809d 2019-08-08 stsp if (nl)
6562 33aa809d 2019-08-08 stsp *nl = '\0';
6563 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6564 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6565 33aa809d 2019-08-08 stsp printf("y\n");
6566 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6567 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6568 33aa809d 2019-08-08 stsp printf("n\n");
6569 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6570 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6571 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6572 33aa809d 2019-08-08 stsp printf("q\n");
6573 33aa809d 2019-08-08 stsp } else
6574 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6575 33aa809d 2019-08-08 stsp free(line);
6576 33aa809d 2019-08-08 stsp return NULL;
6577 33aa809d 2019-08-08 stsp }
6578 33aa809d 2019-08-08 stsp
6579 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6580 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6581 33aa809d 2019-08-08 stsp a->action);
6582 33aa809d 2019-08-08 stsp if (err)
6583 33aa809d 2019-08-08 stsp return err;
6584 33aa809d 2019-08-08 stsp resp = getchar();
6585 33aa809d 2019-08-08 stsp if (resp == '\n')
6586 33aa809d 2019-08-08 stsp resp = getchar();
6587 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6588 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6589 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6590 33aa809d 2019-08-08 stsp resp = ' ';
6591 33aa809d 2019-08-08 stsp }
6592 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6593 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6594 33aa809d 2019-08-08 stsp resp = ' ';
6595 33aa809d 2019-08-08 stsp }
6596 33aa809d 2019-08-08 stsp }
6597 33aa809d 2019-08-08 stsp
6598 33aa809d 2019-08-08 stsp if (resp == 'y')
6599 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6600 33aa809d 2019-08-08 stsp else if (resp == 'n')
6601 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6602 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6603 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6604 33aa809d 2019-08-08 stsp
6605 1ee397ad 2019-07-12 stsp return NULL;
6606 a129376b 2019-03-28 stsp }
6607 a129376b 2019-03-28 stsp
6608 33aa809d 2019-08-08 stsp
6609 a129376b 2019-03-28 stsp static const struct got_error *
6610 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6611 a129376b 2019-03-28 stsp {
6612 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6613 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6614 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6615 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6616 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6617 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6618 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6619 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6620 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6621 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6622 a129376b 2019-03-28 stsp
6623 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6624 e20a8b6f 2019-06-04 stsp
6625 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6626 a129376b 2019-03-28 stsp switch (ch) {
6627 33aa809d 2019-08-08 stsp case 'p':
6628 33aa809d 2019-08-08 stsp pflag = 1;
6629 33aa809d 2019-08-08 stsp break;
6630 33aa809d 2019-08-08 stsp case 'F':
6631 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6632 33aa809d 2019-08-08 stsp break;
6633 0f6d7415 2019-08-08 stsp case 'R':
6634 0f6d7415 2019-08-08 stsp can_recurse = 1;
6635 0f6d7415 2019-08-08 stsp break;
6636 a129376b 2019-03-28 stsp default:
6637 a129376b 2019-03-28 stsp usage_revert();
6638 a129376b 2019-03-28 stsp /* NOTREACHED */
6639 a129376b 2019-03-28 stsp }
6640 a129376b 2019-03-28 stsp }
6641 a129376b 2019-03-28 stsp
6642 a129376b 2019-03-28 stsp argc -= optind;
6643 a129376b 2019-03-28 stsp argv += optind;
6644 a129376b 2019-03-28 stsp
6645 43012d58 2019-07-14 stsp #ifndef PROFILE
6646 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6647 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6648 43012d58 2019-07-14 stsp err(1, "pledge");
6649 43012d58 2019-07-14 stsp #endif
6650 e20a8b6f 2019-06-04 stsp if (argc < 1)
6651 a129376b 2019-03-28 stsp usage_revert();
6652 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6653 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6654 a129376b 2019-03-28 stsp
6655 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6656 a129376b 2019-03-28 stsp if (cwd == NULL) {
6657 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6658 a129376b 2019-03-28 stsp goto done;
6659 a129376b 2019-03-28 stsp }
6660 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6661 fa51e947 2020-03-27 stsp if (error) {
6662 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6663 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6664 2ec1f75b 2019-03-26 stsp goto done;
6665 fa51e947 2020-03-27 stsp }
6666 a129376b 2019-03-28 stsp
6667 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6668 c9956ddf 2019-09-08 stsp NULL);
6669 a129376b 2019-03-28 stsp if (error != NULL)
6670 a129376b 2019-03-28 stsp goto done;
6671 a129376b 2019-03-28 stsp
6672 33aa809d 2019-08-08 stsp if (patch_script_path) {
6673 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6674 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6675 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6676 33aa809d 2019-08-08 stsp patch_script_path);
6677 33aa809d 2019-08-08 stsp goto done;
6678 33aa809d 2019-08-08 stsp }
6679 33aa809d 2019-08-08 stsp }
6680 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6681 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6682 a129376b 2019-03-28 stsp if (error)
6683 a129376b 2019-03-28 stsp goto done;
6684 a129376b 2019-03-28 stsp
6685 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6686 6d022e97 2019-08-04 stsp if (error)
6687 6d022e97 2019-08-04 stsp goto done;
6688 00db391e 2019-08-03 stsp
6689 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6690 0f6d7415 2019-08-08 stsp char *ondisk_path;
6691 0f6d7415 2019-08-08 stsp struct stat sb;
6692 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6693 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6694 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6695 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6696 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6697 0f6d7415 2019-08-08 stsp goto done;
6698 0f6d7415 2019-08-08 stsp }
6699 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6700 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6701 0f6d7415 2019-08-08 stsp free(ondisk_path);
6702 0f6d7415 2019-08-08 stsp continue;
6703 0f6d7415 2019-08-08 stsp }
6704 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6705 0f6d7415 2019-08-08 stsp ondisk_path);
6706 0f6d7415 2019-08-08 stsp free(ondisk_path);
6707 0f6d7415 2019-08-08 stsp goto done;
6708 0f6d7415 2019-08-08 stsp }
6709 0f6d7415 2019-08-08 stsp free(ondisk_path);
6710 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6711 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6712 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6713 0f6d7415 2019-08-08 stsp goto done;
6714 0f6d7415 2019-08-08 stsp }
6715 0f6d7415 2019-08-08 stsp }
6716 0f6d7415 2019-08-08 stsp }
6717 0f6d7415 2019-08-08 stsp
6718 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6719 33aa809d 2019-08-08 stsp cpa.action = "revert";
6720 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6721 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6722 2ec1f75b 2019-03-26 stsp done:
6723 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6724 33aa809d 2019-08-08 stsp error == NULL)
6725 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6726 2ec1f75b 2019-03-26 stsp if (repo)
6727 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6728 2ec1f75b 2019-03-26 stsp if (worktree)
6729 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6730 2ec1f75b 2019-03-26 stsp free(path);
6731 d00136be 2019-03-26 stsp free(cwd);
6732 6bad629b 2019-02-04 stsp return error;
6733 c4296144 2019-05-09 stsp }
6734 c4296144 2019-05-09 stsp
6735 c4296144 2019-05-09 stsp __dead static void
6736 c4296144 2019-05-09 stsp usage_commit(void)
6737 c4296144 2019-05-09 stsp {
6738 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6739 5c1e53bc 2019-07-28 stsp getprogname());
6740 c4296144 2019-05-09 stsp exit(1);
6741 33ad4cbe 2019-05-12 jcs }
6742 33ad4cbe 2019-05-12 jcs
6743 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6744 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6745 e2ba3d07 2019-05-13 stsp const char *editor;
6746 e0870e44 2019-05-13 stsp const char *worktree_path;
6747 76d98825 2019-06-03 stsp const char *branch_name;
6748 314a6357 2019-05-13 stsp const char *repo_path;
6749 e0870e44 2019-05-13 stsp char *logmsg_path;
6750 e2ba3d07 2019-05-13 stsp
6751 e2ba3d07 2019-05-13 stsp };
6752 e0870e44 2019-05-13 stsp
6753 33ad4cbe 2019-05-12 jcs static const struct got_error *
6754 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6755 33ad4cbe 2019-05-12 jcs void *arg)
6756 33ad4cbe 2019-05-12 jcs {
6757 76d98825 2019-06-03 stsp char *initial_content = NULL;
6758 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6759 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6760 e0870e44 2019-05-13 stsp char *template = NULL;
6761 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6762 1601cb9f 2020-09-11 naddy int initial_content_len;
6763 97972933 2020-09-11 stsp int fd = -1;
6764 33ad4cbe 2019-05-12 jcs size_t len;
6765 33ad4cbe 2019-05-12 jcs
6766 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6767 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6768 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6769 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6770 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6771 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6772 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6773 33ad4cbe 2019-05-12 jcs return NULL;
6774 33ad4cbe 2019-05-12 jcs }
6775 33ad4cbe 2019-05-12 jcs
6776 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6777 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6778 76d98825 2019-06-03 stsp
6779 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6780 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6781 1601cb9f 2020-09-11 naddy a->branch_name);
6782 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6783 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6784 e0870e44 2019-05-13 stsp
6785 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6786 793c30b5 2019-05-13 stsp if (err)
6787 e0870e44 2019-05-13 stsp goto done;
6788 33ad4cbe 2019-05-12 jcs
6789 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6790 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6791 97972933 2020-09-11 stsp goto done;
6792 97972933 2020-09-11 stsp }
6793 33ad4cbe 2019-05-12 jcs
6794 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6795 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6796 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6797 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6798 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6799 33ad4cbe 2019-05-12 jcs }
6800 33ad4cbe 2019-05-12 jcs
6801 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6802 bfa12d5e 2020-09-26 stsp initial_content_len);
6803 33ad4cbe 2019-05-12 jcs done:
6804 76d98825 2019-06-03 stsp free(initial_content);
6805 e0870e44 2019-05-13 stsp free(template);
6806 314a6357 2019-05-13 stsp
6807 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6808 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6809 97972933 2020-09-11 stsp
6810 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6811 59f86c76 2020-09-11 stsp if (err == NULL)
6812 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6813 59f86c76 2020-09-11 stsp if (err) {
6814 59f86c76 2020-09-11 stsp free(*logmsg);
6815 59f86c76 2020-09-11 stsp *logmsg = NULL;
6816 3ce1b845 2019-07-15 stsp }
6817 33ad4cbe 2019-05-12 jcs return err;
6818 6bad629b 2019-02-04 stsp }
6819 c4296144 2019-05-09 stsp
6820 c4296144 2019-05-09 stsp static const struct got_error *
6821 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6822 c4296144 2019-05-09 stsp {
6823 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6824 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6825 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6826 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6827 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6828 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6829 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6830 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6831 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6832 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6833 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6834 5c1e53bc 2019-07-28 stsp
6835 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6836 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6837 c4296144 2019-05-09 stsp
6838 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6839 c4296144 2019-05-09 stsp switch (ch) {
6840 c4296144 2019-05-09 stsp case 'm':
6841 c4296144 2019-05-09 stsp logmsg = optarg;
6842 c4296144 2019-05-09 stsp break;
6843 35213c7c 2020-07-23 stsp case 'S':
6844 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6845 35213c7c 2020-07-23 stsp break;
6846 c4296144 2019-05-09 stsp default:
6847 c4296144 2019-05-09 stsp usage_commit();
6848 c4296144 2019-05-09 stsp /* NOTREACHED */
6849 c4296144 2019-05-09 stsp }
6850 c4296144 2019-05-09 stsp }
6851 c4296144 2019-05-09 stsp
6852 c4296144 2019-05-09 stsp argc -= optind;
6853 c4296144 2019-05-09 stsp argv += optind;
6854 c4296144 2019-05-09 stsp
6855 43012d58 2019-07-14 stsp #ifndef PROFILE
6856 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6857 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6858 43012d58 2019-07-14 stsp err(1, "pledge");
6859 43012d58 2019-07-14 stsp #endif
6860 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6861 c4296144 2019-05-09 stsp if (cwd == NULL) {
6862 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6863 c4296144 2019-05-09 stsp goto done;
6864 c4296144 2019-05-09 stsp }
6865 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6866 fa51e947 2020-03-27 stsp if (error) {
6867 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6868 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6869 7d5807f4 2019-07-11 stsp goto done;
6870 fa51e947 2020-03-27 stsp }
6871 7d5807f4 2019-07-11 stsp
6872 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6873 c4296144 2019-05-09 stsp if (error)
6874 a698f62e 2019-07-25 stsp goto done;
6875 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6876 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6877 c4296144 2019-05-09 stsp goto done;
6878 a698f62e 2019-07-25 stsp }
6879 5c1e53bc 2019-07-28 stsp
6880 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6881 916f288c 2019-07-30 stsp worktree);
6882 5c1e53bc 2019-07-28 stsp if (error)
6883 5c1e53bc 2019-07-28 stsp goto done;
6884 c4296144 2019-05-09 stsp
6885 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6886 c9956ddf 2019-09-08 stsp if (error)
6887 c9956ddf 2019-09-08 stsp goto done;
6888 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6889 c9956ddf 2019-09-08 stsp gitconfig_path);
6890 c4296144 2019-05-09 stsp if (error != NULL)
6891 c4296144 2019-05-09 stsp goto done;
6892 c4296144 2019-05-09 stsp
6893 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6894 aba9c984 2019-09-08 stsp if (error)
6895 aba9c984 2019-09-08 stsp return error;
6896 aba9c984 2019-09-08 stsp
6897 314a6357 2019-05-13 stsp /*
6898 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6899 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6900 314a6357 2019-05-13 stsp */
6901 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6902 314a6357 2019-05-13 stsp error = get_editor(&editor);
6903 314a6357 2019-05-13 stsp else
6904 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6905 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6906 c4296144 2019-05-09 stsp if (error)
6907 c4296144 2019-05-09 stsp goto done;
6908 c4296144 2019-05-09 stsp
6909 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6910 087fb88c 2019-08-04 stsp if (error)
6911 087fb88c 2019-08-04 stsp goto done;
6912 087fb88c 2019-08-04 stsp
6913 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6914 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6915 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6916 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6917 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6918 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6919 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6920 916f288c 2019-07-30 stsp goto done;
6921 916f288c 2019-07-30 stsp }
6922 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6923 916f288c 2019-07-30 stsp }
6924 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6925 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6926 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6927 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6928 e0870e44 2019-05-13 stsp if (error) {
6929 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6930 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6931 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6932 c4296144 2019-05-09 stsp goto done;
6933 e0870e44 2019-05-13 stsp }
6934 c4296144 2019-05-09 stsp
6935 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6936 c4296144 2019-05-09 stsp if (error)
6937 c4296144 2019-05-09 stsp goto done;
6938 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6939 c4296144 2019-05-09 stsp done:
6940 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6941 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6942 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6943 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6944 7266f21f 2019-10-21 stsp error == NULL)
6945 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6946 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6947 c4296144 2019-05-09 stsp if (repo)
6948 c4296144 2019-05-09 stsp got_repo_close(repo);
6949 c4296144 2019-05-09 stsp if (worktree)
6950 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6951 c4296144 2019-05-09 stsp free(cwd);
6952 c4296144 2019-05-09 stsp free(id_str);
6953 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6954 e2ba3d07 2019-05-13 stsp free(editor);
6955 aba9c984 2019-09-08 stsp free(author);
6956 234035bc 2019-06-01 stsp return error;
6957 234035bc 2019-06-01 stsp }
6958 234035bc 2019-06-01 stsp
6959 234035bc 2019-06-01 stsp __dead static void
6960 234035bc 2019-06-01 stsp usage_cherrypick(void)
6961 234035bc 2019-06-01 stsp {
6962 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6963 234035bc 2019-06-01 stsp exit(1);
6964 234035bc 2019-06-01 stsp }
6965 234035bc 2019-06-01 stsp
6966 234035bc 2019-06-01 stsp static const struct got_error *
6967 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6968 234035bc 2019-06-01 stsp {
6969 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6970 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6971 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6972 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6973 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6974 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6975 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6976 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6977 9627c110 2020-04-18 stsp int ch;
6978 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6979 234035bc 2019-06-01 stsp
6980 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6981 234035bc 2019-06-01 stsp switch (ch) {
6982 234035bc 2019-06-01 stsp default:
6983 234035bc 2019-06-01 stsp usage_cherrypick();
6984 234035bc 2019-06-01 stsp /* NOTREACHED */
6985 234035bc 2019-06-01 stsp }
6986 234035bc 2019-06-01 stsp }
6987 234035bc 2019-06-01 stsp
6988 234035bc 2019-06-01 stsp argc -= optind;
6989 234035bc 2019-06-01 stsp argv += optind;
6990 234035bc 2019-06-01 stsp
6991 43012d58 2019-07-14 stsp #ifndef PROFILE
6992 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6993 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6994 43012d58 2019-07-14 stsp err(1, "pledge");
6995 43012d58 2019-07-14 stsp #endif
6996 234035bc 2019-06-01 stsp if (argc != 1)
6997 234035bc 2019-06-01 stsp usage_cherrypick();
6998 234035bc 2019-06-01 stsp
6999 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
7000 234035bc 2019-06-01 stsp if (cwd == NULL) {
7001 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
7002 234035bc 2019-06-01 stsp goto done;
7003 234035bc 2019-06-01 stsp }
7004 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
7005 fa51e947 2020-03-27 stsp if (error) {
7006 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7007 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
7008 fa51e947 2020-03-27 stsp cwd);
7009 234035bc 2019-06-01 stsp goto done;
7010 fa51e947 2020-03-27 stsp }
7011 234035bc 2019-06-01 stsp
7012 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7013 c9956ddf 2019-09-08 stsp NULL);
7014 234035bc 2019-06-01 stsp if (error != NULL)
7015 234035bc 2019-06-01 stsp goto done;
7016 234035bc 2019-06-01 stsp
7017 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7018 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7019 234035bc 2019-06-01 stsp if (error)
7020 234035bc 2019-06-01 stsp goto done;
7021 234035bc 2019-06-01 stsp
7022 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7023 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7024 234035bc 2019-06-01 stsp if (error != NULL) {
7025 234035bc 2019-06-01 stsp struct got_reference *ref;
7026 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7027 234035bc 2019-06-01 stsp goto done;
7028 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7029 234035bc 2019-06-01 stsp if (error != NULL)
7030 234035bc 2019-06-01 stsp goto done;
7031 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
7032 234035bc 2019-06-01 stsp got_ref_close(ref);
7033 234035bc 2019-06-01 stsp if (error != NULL)
7034 234035bc 2019-06-01 stsp goto done;
7035 234035bc 2019-06-01 stsp }
7036 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7037 234035bc 2019-06-01 stsp if (error)
7038 234035bc 2019-06-01 stsp goto done;
7039 234035bc 2019-06-01 stsp
7040 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
7041 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
7042 234035bc 2019-06-01 stsp if (error != NULL)
7043 234035bc 2019-06-01 stsp goto done;
7044 234035bc 2019-06-01 stsp
7045 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7046 234035bc 2019-06-01 stsp if (error) {
7047 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
7048 234035bc 2019-06-01 stsp goto done;
7049 234035bc 2019-06-01 stsp error = NULL;
7050 234035bc 2019-06-01 stsp } else {
7051 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
7052 234035bc 2019-06-01 stsp goto done;
7053 234035bc 2019-06-01 stsp }
7054 234035bc 2019-06-01 stsp
7055 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7056 234035bc 2019-06-01 stsp if (error)
7057 234035bc 2019-06-01 stsp goto done;
7058 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7059 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7060 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7061 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7062 03415a1a 2019-06-02 stsp NULL);
7063 234035bc 2019-06-01 stsp if (error != NULL)
7064 234035bc 2019-06-01 stsp goto done;
7065 234035bc 2019-06-01 stsp
7066 9627c110 2020-04-18 stsp if (upa.did_something)
7067 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7068 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7069 234035bc 2019-06-01 stsp done:
7070 234035bc 2019-06-01 stsp if (commit)
7071 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7072 234035bc 2019-06-01 stsp free(commit_id_str);
7073 234035bc 2019-06-01 stsp if (head_ref)
7074 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7075 234035bc 2019-06-01 stsp if (worktree)
7076 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7077 234035bc 2019-06-01 stsp if (repo)
7078 234035bc 2019-06-01 stsp got_repo_close(repo);
7079 c4296144 2019-05-09 stsp return error;
7080 c4296144 2019-05-09 stsp }
7081 5ef14e63 2019-06-02 stsp
7082 5ef14e63 2019-06-02 stsp __dead static void
7083 5ef14e63 2019-06-02 stsp usage_backout(void)
7084 5ef14e63 2019-06-02 stsp {
7085 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7086 5ef14e63 2019-06-02 stsp exit(1);
7087 5ef14e63 2019-06-02 stsp }
7088 5ef14e63 2019-06-02 stsp
7089 5ef14e63 2019-06-02 stsp static const struct got_error *
7090 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7091 5ef14e63 2019-06-02 stsp {
7092 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7093 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7094 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7095 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7096 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7097 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7098 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7099 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7100 9627c110 2020-04-18 stsp int ch;
7101 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7102 5ef14e63 2019-06-02 stsp
7103 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7104 5ef14e63 2019-06-02 stsp switch (ch) {
7105 5ef14e63 2019-06-02 stsp default:
7106 5ef14e63 2019-06-02 stsp usage_backout();
7107 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7108 5ef14e63 2019-06-02 stsp }
7109 5ef14e63 2019-06-02 stsp }
7110 5ef14e63 2019-06-02 stsp
7111 5ef14e63 2019-06-02 stsp argc -= optind;
7112 5ef14e63 2019-06-02 stsp argv += optind;
7113 5ef14e63 2019-06-02 stsp
7114 43012d58 2019-07-14 stsp #ifndef PROFILE
7115 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7116 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7117 43012d58 2019-07-14 stsp err(1, "pledge");
7118 43012d58 2019-07-14 stsp #endif
7119 5ef14e63 2019-06-02 stsp if (argc != 1)
7120 5ef14e63 2019-06-02 stsp usage_backout();
7121 5ef14e63 2019-06-02 stsp
7122 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7123 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7124 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7125 5ef14e63 2019-06-02 stsp goto done;
7126 5ef14e63 2019-06-02 stsp }
7127 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7128 fa51e947 2020-03-27 stsp if (error) {
7129 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7130 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7131 5ef14e63 2019-06-02 stsp goto done;
7132 fa51e947 2020-03-27 stsp }
7133 5ef14e63 2019-06-02 stsp
7134 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7135 c9956ddf 2019-09-08 stsp NULL);
7136 5ef14e63 2019-06-02 stsp if (error != NULL)
7137 5ef14e63 2019-06-02 stsp goto done;
7138 5ef14e63 2019-06-02 stsp
7139 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7140 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7141 5ef14e63 2019-06-02 stsp if (error)
7142 5ef14e63 2019-06-02 stsp goto done;
7143 5ef14e63 2019-06-02 stsp
7144 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7145 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7146 5ef14e63 2019-06-02 stsp if (error != NULL) {
7147 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7148 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7149 5ef14e63 2019-06-02 stsp goto done;
7150 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7151 5ef14e63 2019-06-02 stsp if (error != NULL)
7152 5ef14e63 2019-06-02 stsp goto done;
7153 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7154 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7155 5ef14e63 2019-06-02 stsp if (error != NULL)
7156 5ef14e63 2019-06-02 stsp goto done;
7157 5ef14e63 2019-06-02 stsp }
7158 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7159 5ef14e63 2019-06-02 stsp if (error)
7160 5ef14e63 2019-06-02 stsp goto done;
7161 5ef14e63 2019-06-02 stsp
7162 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7163 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7164 5ef14e63 2019-06-02 stsp if (error != NULL)
7165 5ef14e63 2019-06-02 stsp goto done;
7166 5ef14e63 2019-06-02 stsp
7167 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7168 5ef14e63 2019-06-02 stsp if (error)
7169 5ef14e63 2019-06-02 stsp goto done;
7170 5ef14e63 2019-06-02 stsp
7171 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7172 5ef14e63 2019-06-02 stsp if (error)
7173 5ef14e63 2019-06-02 stsp goto done;
7174 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7175 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7176 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7177 5ef14e63 2019-06-02 stsp goto done;
7178 5ef14e63 2019-06-02 stsp }
7179 5ef14e63 2019-06-02 stsp
7180 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7181 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7182 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7183 5ef14e63 2019-06-02 stsp if (error != NULL)
7184 5ef14e63 2019-06-02 stsp goto done;
7185 5ef14e63 2019-06-02 stsp
7186 9627c110 2020-04-18 stsp if (upa.did_something)
7187 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7188 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7189 5ef14e63 2019-06-02 stsp done:
7190 5ef14e63 2019-06-02 stsp if (commit)
7191 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7192 5ef14e63 2019-06-02 stsp free(commit_id_str);
7193 5ef14e63 2019-06-02 stsp if (head_ref)
7194 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7195 818c7501 2019-07-11 stsp if (worktree)
7196 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7197 818c7501 2019-07-11 stsp if (repo)
7198 818c7501 2019-07-11 stsp got_repo_close(repo);
7199 818c7501 2019-07-11 stsp return error;
7200 818c7501 2019-07-11 stsp }
7201 818c7501 2019-07-11 stsp
7202 818c7501 2019-07-11 stsp __dead static void
7203 818c7501 2019-07-11 stsp usage_rebase(void)
7204 818c7501 2019-07-11 stsp {
7205 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7206 af54c8f8 2019-07-11 stsp getprogname());
7207 818c7501 2019-07-11 stsp exit(1);
7208 0ebf8283 2019-07-24 stsp }
7209 0ebf8283 2019-07-24 stsp
7210 0ebf8283 2019-07-24 stsp void
7211 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7212 0ebf8283 2019-07-24 stsp {
7213 0ebf8283 2019-07-24 stsp char *nl;
7214 0ebf8283 2019-07-24 stsp size_t len;
7215 0ebf8283 2019-07-24 stsp
7216 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7217 0ebf8283 2019-07-24 stsp if (len > limit)
7218 0ebf8283 2019-07-24 stsp len = limit;
7219 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7220 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7221 0ebf8283 2019-07-24 stsp if (nl)
7222 0ebf8283 2019-07-24 stsp *nl = '\0';
7223 0ebf8283 2019-07-24 stsp }
7224 0ebf8283 2019-07-24 stsp
7225 0ebf8283 2019-07-24 stsp static const struct got_error *
7226 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7227 0ebf8283 2019-07-24 stsp {
7228 5943eee2 2019-08-13 stsp const struct got_error *err;
7229 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7230 5943eee2 2019-08-13 stsp const char *s;
7231 0ebf8283 2019-07-24 stsp
7232 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7233 5943eee2 2019-08-13 stsp if (err)
7234 5943eee2 2019-08-13 stsp return err;
7235 0ebf8283 2019-07-24 stsp
7236 5943eee2 2019-08-13 stsp s = logmsg0;
7237 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7238 5943eee2 2019-08-13 stsp s++;
7239 5943eee2 2019-08-13 stsp
7240 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7241 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7242 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7243 5943eee2 2019-08-13 stsp goto done;
7244 5943eee2 2019-08-13 stsp }
7245 0ebf8283 2019-07-24 stsp
7246 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7247 5943eee2 2019-08-13 stsp done:
7248 5943eee2 2019-08-13 stsp free(logmsg0);
7249 a0ea4fc0 2020-02-28 stsp return err;
7250 a0ea4fc0 2020-02-28 stsp }
7251 a0ea4fc0 2020-02-28 stsp
7252 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7253 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7254 a0ea4fc0 2020-02-28 stsp {
7255 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7256 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7257 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7258 a0ea4fc0 2020-02-28 stsp
7259 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7260 a0ea4fc0 2020-02-28 stsp if (err)
7261 a0ea4fc0 2020-02-28 stsp return err;
7262 a0ea4fc0 2020-02-28 stsp
7263 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7264 a0ea4fc0 2020-02-28 stsp if (err)
7265 a0ea4fc0 2020-02-28 stsp goto done;
7266 a0ea4fc0 2020-02-28 stsp
7267 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7268 a0ea4fc0 2020-02-28 stsp
7269 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7270 a0ea4fc0 2020-02-28 stsp if (err)
7271 a0ea4fc0 2020-02-28 stsp goto done;
7272 a0ea4fc0 2020-02-28 stsp
7273 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7274 a0ea4fc0 2020-02-28 stsp done:
7275 a0ea4fc0 2020-02-28 stsp free(id_str);
7276 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7277 a0ea4fc0 2020-02-28 stsp free(logmsg);
7278 5943eee2 2019-08-13 stsp return err;
7279 818c7501 2019-07-11 stsp }
7280 818c7501 2019-07-11 stsp
7281 818c7501 2019-07-11 stsp static const struct got_error *
7282 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7283 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7284 818c7501 2019-07-11 stsp {
7285 818c7501 2019-07-11 stsp const struct got_error *err;
7286 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7287 818c7501 2019-07-11 stsp
7288 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7289 818c7501 2019-07-11 stsp if (err)
7290 818c7501 2019-07-11 stsp goto done;
7291 818c7501 2019-07-11 stsp
7292 ff0d2220 2019-07-11 stsp if (new_id) {
7293 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7294 ff0d2220 2019-07-11 stsp if (err)
7295 ff0d2220 2019-07-11 stsp goto done;
7296 ff0d2220 2019-07-11 stsp }
7297 818c7501 2019-07-11 stsp
7298 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7299 ff0d2220 2019-07-11 stsp if (new_id_str)
7300 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7301 0ebf8283 2019-07-24 stsp
7302 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7303 0ebf8283 2019-07-24 stsp if (err)
7304 0ebf8283 2019-07-24 stsp goto done;
7305 0ebf8283 2019-07-24 stsp
7306 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7307 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7308 818c7501 2019-07-11 stsp done:
7309 818c7501 2019-07-11 stsp free(old_id_str);
7310 818c7501 2019-07-11 stsp free(new_id_str);
7311 272a1371 2020-02-28 stsp free(logmsg);
7312 818c7501 2019-07-11 stsp return err;
7313 818c7501 2019-07-11 stsp }
7314 818c7501 2019-07-11 stsp
7315 1ee397ad 2019-07-12 stsp static const struct got_error *
7316 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7317 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7318 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7319 818c7501 2019-07-11 stsp {
7320 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7321 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7322 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7323 818c7501 2019-07-11 stsp }
7324 818c7501 2019-07-11 stsp
7325 818c7501 2019-07-11 stsp static const struct got_error *
7326 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7327 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7328 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7329 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7330 ff0d2220 2019-07-11 stsp {
7331 ff0d2220 2019-07-11 stsp const struct got_error *error;
7332 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7333 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7334 ff0d2220 2019-07-11 stsp
7335 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7336 ff0d2220 2019-07-11 stsp if (error)
7337 ff0d2220 2019-07-11 stsp return error;
7338 ff0d2220 2019-07-11 stsp
7339 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7340 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7341 ff0d2220 2019-07-11 stsp if (error) {
7342 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7343 ff0d2220 2019-07-11 stsp goto done;
7344 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7345 ff0d2220 2019-07-11 stsp } else {
7346 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7347 ff0d2220 2019-07-11 stsp free(new_commit_id);
7348 ff0d2220 2019-07-11 stsp }
7349 ff0d2220 2019-07-11 stsp done:
7350 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7351 ff0d2220 2019-07-11 stsp return error;
7352 64c6d990 2019-07-11 stsp }
7353 64c6d990 2019-07-11 stsp
7354 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7355 64c6d990 2019-07-11 stsp const char *path_prefix;
7356 64c6d990 2019-07-11 stsp size_t len;
7357 8ca9bd68 2019-07-25 stsp int errcode;
7358 64c6d990 2019-07-11 stsp };
7359 64c6d990 2019-07-11 stsp
7360 64c6d990 2019-07-11 stsp static const struct got_error *
7361 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7362 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7363 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7364 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7365 64c6d990 2019-07-11 stsp {
7366 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7367 64c6d990 2019-07-11 stsp
7368 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7369 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7370 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7371 64c6d990 2019-07-11 stsp
7372 64c6d990 2019-07-11 stsp return NULL;
7373 64c6d990 2019-07-11 stsp }
7374 64c6d990 2019-07-11 stsp
7375 64c6d990 2019-07-11 stsp static const struct got_error *
7376 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7377 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7378 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7379 64c6d990 2019-07-11 stsp {
7380 64c6d990 2019-07-11 stsp const struct got_error *err;
7381 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7382 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7383 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7384 64c6d990 2019-07-11 stsp
7385 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7386 64c6d990 2019-07-11 stsp return NULL;
7387 64c6d990 2019-07-11 stsp
7388 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7389 64c6d990 2019-07-11 stsp if (err)
7390 64c6d990 2019-07-11 stsp goto done;
7391 64c6d990 2019-07-11 stsp
7392 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7393 64c6d990 2019-07-11 stsp if (err)
7394 64c6d990 2019-07-11 stsp goto done;
7395 64c6d990 2019-07-11 stsp
7396 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7397 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7398 64c6d990 2019-07-11 stsp if (err)
7399 64c6d990 2019-07-11 stsp goto done;
7400 64c6d990 2019-07-11 stsp
7401 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7402 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7403 64c6d990 2019-07-11 stsp if (err)
7404 64c6d990 2019-07-11 stsp goto done;
7405 64c6d990 2019-07-11 stsp
7406 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7407 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7408 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7409 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7410 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7411 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7412 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7413 64c6d990 2019-07-11 stsp done:
7414 64c6d990 2019-07-11 stsp if (tree1)
7415 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7416 64c6d990 2019-07-11 stsp if (tree2)
7417 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7418 64c6d990 2019-07-11 stsp if (commit)
7419 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7420 64c6d990 2019-07-11 stsp if (parent_commit)
7421 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7422 64c6d990 2019-07-11 stsp return err;
7423 ff0d2220 2019-07-11 stsp }
7424 ff0d2220 2019-07-11 stsp
7425 ff0d2220 2019-07-11 stsp static const struct got_error *
7426 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7427 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7428 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7429 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7430 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7431 af61c510 2019-07-19 stsp {
7432 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7433 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7434 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7435 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7436 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7437 af61c510 2019-07-19 stsp
7438 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7439 af61c510 2019-07-19 stsp if (err)
7440 af61c510 2019-07-19 stsp return err;
7441 af61c510 2019-07-19 stsp
7442 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7443 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7444 af61c510 2019-07-19 stsp if (err)
7445 af61c510 2019-07-19 stsp goto done;
7446 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7447 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7448 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7449 af61c510 2019-07-19 stsp if (err) {
7450 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7451 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7452 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7453 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7454 af61c510 2019-07-19 stsp "been reached?!?");
7455 ee780d5c 2020-01-04 stsp }
7456 ee780d5c 2020-01-04 stsp goto done;
7457 af61c510 2019-07-19 stsp } else {
7458 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7459 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7460 af61c510 2019-07-19 stsp if (err)
7461 af61c510 2019-07-19 stsp goto done;
7462 af61c510 2019-07-19 stsp
7463 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7464 af61c510 2019-07-19 stsp if (err)
7465 af61c510 2019-07-19 stsp goto done;
7466 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7467 af61c510 2019-07-19 stsp commit_id = parent_id;
7468 af61c510 2019-07-19 stsp }
7469 af61c510 2019-07-19 stsp }
7470 af61c510 2019-07-19 stsp done:
7471 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7472 af61c510 2019-07-19 stsp return err;
7473 af61c510 2019-07-19 stsp }
7474 af61c510 2019-07-19 stsp
7475 af61c510 2019-07-19 stsp static const struct got_error *
7476 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7477 818c7501 2019-07-11 stsp {
7478 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7479 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7480 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7481 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7482 818c7501 2019-07-11 stsp char *cwd = NULL;
7483 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7484 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7485 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7486 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7487 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7488 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7489 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7490 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7491 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7492 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7493 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7494 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7495 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7496 818c7501 2019-07-11 stsp
7497 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7498 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7499 818c7501 2019-07-11 stsp
7500 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7501 818c7501 2019-07-11 stsp switch (ch) {
7502 818c7501 2019-07-11 stsp case 'a':
7503 818c7501 2019-07-11 stsp abort_rebase = 1;
7504 818c7501 2019-07-11 stsp break;
7505 818c7501 2019-07-11 stsp case 'c':
7506 818c7501 2019-07-11 stsp continue_rebase = 1;
7507 818c7501 2019-07-11 stsp break;
7508 818c7501 2019-07-11 stsp default:
7509 818c7501 2019-07-11 stsp usage_rebase();
7510 818c7501 2019-07-11 stsp /* NOTREACHED */
7511 818c7501 2019-07-11 stsp }
7512 818c7501 2019-07-11 stsp }
7513 818c7501 2019-07-11 stsp
7514 818c7501 2019-07-11 stsp argc -= optind;
7515 818c7501 2019-07-11 stsp argv += optind;
7516 818c7501 2019-07-11 stsp
7517 43012d58 2019-07-14 stsp #ifndef PROFILE
7518 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7519 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7520 43012d58 2019-07-14 stsp err(1, "pledge");
7521 43012d58 2019-07-14 stsp #endif
7522 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7523 818c7501 2019-07-11 stsp usage_rebase();
7524 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7525 818c7501 2019-07-11 stsp if (argc != 0)
7526 818c7501 2019-07-11 stsp usage_rebase();
7527 818c7501 2019-07-11 stsp } else if (argc != 1)
7528 818c7501 2019-07-11 stsp usage_rebase();
7529 818c7501 2019-07-11 stsp
7530 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7531 818c7501 2019-07-11 stsp if (cwd == NULL) {
7532 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7533 818c7501 2019-07-11 stsp goto done;
7534 818c7501 2019-07-11 stsp }
7535 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7536 fa51e947 2020-03-27 stsp if (error) {
7537 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7538 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7539 818c7501 2019-07-11 stsp goto done;
7540 fa51e947 2020-03-27 stsp }
7541 818c7501 2019-07-11 stsp
7542 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7543 c9956ddf 2019-09-08 stsp NULL);
7544 818c7501 2019-07-11 stsp if (error != NULL)
7545 818c7501 2019-07-11 stsp goto done;
7546 818c7501 2019-07-11 stsp
7547 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7548 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7549 7ef62c4e 2020-02-24 stsp if (error)
7550 7ef62c4e 2020-02-24 stsp goto done;
7551 7ef62c4e 2020-02-24 stsp
7552 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7553 7ef62c4e 2020-02-24 stsp worktree);
7554 818c7501 2019-07-11 stsp if (error)
7555 7ef62c4e 2020-02-24 stsp goto done;
7556 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7557 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7558 818c7501 2019-07-11 stsp goto done;
7559 7ef62c4e 2020-02-24 stsp }
7560 818c7501 2019-07-11 stsp
7561 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7562 818c7501 2019-07-11 stsp if (error)
7563 818c7501 2019-07-11 stsp goto done;
7564 818c7501 2019-07-11 stsp
7565 f6794adc 2019-07-23 stsp if (abort_rebase) {
7566 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7567 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7568 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7569 f6794adc 2019-07-23 stsp goto done;
7570 f6794adc 2019-07-23 stsp }
7571 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7572 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7573 3e3a69f1 2019-07-25 stsp worktree, repo);
7574 818c7501 2019-07-11 stsp if (error)
7575 818c7501 2019-07-11 stsp goto done;
7576 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7577 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7578 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7579 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7580 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7581 818c7501 2019-07-11 stsp if (error)
7582 818c7501 2019-07-11 stsp goto done;
7583 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7584 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7585 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7586 818c7501 2019-07-11 stsp }
7587 818c7501 2019-07-11 stsp
7588 818c7501 2019-07-11 stsp if (continue_rebase) {
7589 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7590 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7591 f6794adc 2019-07-23 stsp goto done;
7592 f6794adc 2019-07-23 stsp }
7593 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7594 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7595 3e3a69f1 2019-07-25 stsp worktree, repo);
7596 818c7501 2019-07-11 stsp if (error)
7597 818c7501 2019-07-11 stsp goto done;
7598 818c7501 2019-07-11 stsp
7599 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7600 01757395 2019-07-12 stsp resume_commit_id, repo);
7601 818c7501 2019-07-11 stsp if (error)
7602 818c7501 2019-07-11 stsp goto done;
7603 818c7501 2019-07-11 stsp
7604 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7605 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7606 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7607 818c7501 2019-07-11 stsp goto done;
7608 818c7501 2019-07-11 stsp }
7609 818c7501 2019-07-11 stsp } else {
7610 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7611 818c7501 2019-07-11 stsp if (error != NULL)
7612 818c7501 2019-07-11 stsp goto done;
7613 ff0d2220 2019-07-11 stsp }
7614 818c7501 2019-07-11 stsp
7615 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7616 ff0d2220 2019-07-11 stsp if (error)
7617 ff0d2220 2019-07-11 stsp goto done;
7618 ff0d2220 2019-07-11 stsp
7619 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7620 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7621 a51a74b3 2019-07-27 stsp
7622 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7623 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7624 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7625 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7626 818c7501 2019-07-11 stsp if (error)
7627 818c7501 2019-07-11 stsp goto done;
7628 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7629 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7630 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7631 818c7501 2019-07-11 stsp "with work tree's branch");
7632 818c7501 2019-07-11 stsp goto done;
7633 818c7501 2019-07-11 stsp }
7634 818c7501 2019-07-11 stsp
7635 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7636 a51a74b3 2019-07-27 stsp if (error) {
7637 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7638 a51a74b3 2019-07-27 stsp goto done;
7639 a51a74b3 2019-07-27 stsp error = NULL;
7640 a51a74b3 2019-07-27 stsp } else {
7641 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7642 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7643 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7644 a51a74b3 2019-07-27 stsp goto done;
7645 a51a74b3 2019-07-27 stsp }
7646 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7647 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7648 818c7501 2019-07-11 stsp if (error)
7649 818c7501 2019-07-11 stsp goto done;
7650 818c7501 2019-07-11 stsp }
7651 818c7501 2019-07-11 stsp
7652 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7653 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7654 818c7501 2019-07-11 stsp if (error)
7655 818c7501 2019-07-11 stsp goto done;
7656 818c7501 2019-07-11 stsp
7657 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7658 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7659 fc66b545 2019-08-12 stsp if (pid == NULL) {
7660 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7661 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7662 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7663 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7664 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7665 fc66b545 2019-08-12 stsp if (error)
7666 fc66b545 2019-08-12 stsp goto done;
7667 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7668 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7669 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7670 9627c110 2020-04-18 stsp
7671 fc66b545 2019-08-12 stsp }
7672 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7673 fc66b545 2019-08-12 stsp goto done;
7674 fc66b545 2019-08-12 stsp }
7675 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7676 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7677 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7678 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7679 818c7501 2019-07-11 stsp commit = NULL;
7680 818c7501 2019-07-11 stsp if (error)
7681 818c7501 2019-07-11 stsp goto done;
7682 64c6d990 2019-07-11 stsp
7683 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7684 38b0338b 2019-11-29 stsp if (continue_rebase) {
7685 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7686 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7687 38b0338b 2019-11-29 stsp goto done;
7688 38b0338b 2019-11-29 stsp } else {
7689 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7690 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7691 38b0338b 2019-11-29 stsp char *id_str;
7692 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7693 38b0338b 2019-11-29 stsp new_base_branch);
7694 38b0338b 2019-11-29 stsp if (error)
7695 38b0338b 2019-11-29 stsp goto done;
7696 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7697 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7698 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7699 38b0338b 2019-11-29 stsp free(id_str);
7700 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7701 38b0338b 2019-11-29 stsp new_head_commit_id);
7702 38b0338b 2019-11-29 stsp if (error)
7703 38b0338b 2019-11-29 stsp goto done;
7704 38b0338b 2019-11-29 stsp }
7705 818c7501 2019-07-11 stsp }
7706 818c7501 2019-07-11 stsp
7707 818c7501 2019-07-11 stsp pid = NULL;
7708 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7709 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7710 9627c110 2020-04-18 stsp
7711 818c7501 2019-07-11 stsp commit_id = qid->id;
7712 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7713 818c7501 2019-07-11 stsp pid = qid;
7714 818c7501 2019-07-11 stsp
7715 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7716 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7717 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7718 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7719 818c7501 2019-07-11 stsp if (error)
7720 818c7501 2019-07-11 stsp goto done;
7721 9627c110 2020-04-18 stsp
7722 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7723 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7724 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7725 818c7501 2019-07-11 stsp
7726 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7727 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7728 a0ea4fc0 2020-02-28 stsp if (error)
7729 a0ea4fc0 2020-02-28 stsp goto done;
7730 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7731 818c7501 2019-07-11 stsp break;
7732 01757395 2019-07-12 stsp }
7733 818c7501 2019-07-11 stsp
7734 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7735 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7736 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7737 818c7501 2019-07-11 stsp if (error)
7738 818c7501 2019-07-11 stsp goto done;
7739 818c7501 2019-07-11 stsp }
7740 818c7501 2019-07-11 stsp
7741 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7742 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7743 818c7501 2019-07-11 stsp if (error)
7744 818c7501 2019-07-11 stsp goto done;
7745 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7746 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7747 818c7501 2019-07-11 stsp } else
7748 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7749 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7750 818c7501 2019-07-11 stsp done:
7751 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7752 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7753 818c7501 2019-07-11 stsp free(resume_commit_id);
7754 818c7501 2019-07-11 stsp free(yca_id);
7755 818c7501 2019-07-11 stsp if (commit)
7756 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7757 818c7501 2019-07-11 stsp if (branch)
7758 818c7501 2019-07-11 stsp got_ref_close(branch);
7759 818c7501 2019-07-11 stsp if (new_base_branch)
7760 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7761 818c7501 2019-07-11 stsp if (tmp_branch)
7762 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7763 5ef14e63 2019-06-02 stsp if (worktree)
7764 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7765 5ef14e63 2019-06-02 stsp if (repo)
7766 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7767 5ef14e63 2019-06-02 stsp return error;
7768 0ebf8283 2019-07-24 stsp }
7769 0ebf8283 2019-07-24 stsp
7770 0ebf8283 2019-07-24 stsp __dead static void
7771 0ebf8283 2019-07-24 stsp usage_histedit(void)
7772 0ebf8283 2019-07-24 stsp {
7773 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7774 0ebf8283 2019-07-24 stsp getprogname());
7775 0ebf8283 2019-07-24 stsp exit(1);
7776 0ebf8283 2019-07-24 stsp }
7777 0ebf8283 2019-07-24 stsp
7778 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7779 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7780 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7781 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7782 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7783 0ebf8283 2019-07-24 stsp
7784 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7785 0ebf8283 2019-07-24 stsp unsigned char code;
7786 0ebf8283 2019-07-24 stsp const char *name;
7787 0ebf8283 2019-07-24 stsp const char *desc;
7788 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7789 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7790 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7791 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7792 82997472 2020-01-29 stsp "be used" },
7793 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7794 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7795 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7796 0ebf8283 2019-07-24 stsp };
7797 0ebf8283 2019-07-24 stsp
7798 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7799 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7800 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7801 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7802 0ebf8283 2019-07-24 stsp char *logmsg;
7803 0ebf8283 2019-07-24 stsp };
7804 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7805 0ebf8283 2019-07-24 stsp
7806 0ebf8283 2019-07-24 stsp static const struct got_error *
7807 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7808 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7809 0ebf8283 2019-07-24 stsp {
7810 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7811 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7812 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7813 8138f3e1 2019-08-11 stsp int n;
7814 0ebf8283 2019-07-24 stsp
7815 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7816 0ebf8283 2019-07-24 stsp if (err)
7817 0ebf8283 2019-07-24 stsp goto done;
7818 0ebf8283 2019-07-24 stsp
7819 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7820 0ebf8283 2019-07-24 stsp if (err)
7821 0ebf8283 2019-07-24 stsp goto done;
7822 0ebf8283 2019-07-24 stsp
7823 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7824 0ebf8283 2019-07-24 stsp if (err)
7825 0ebf8283 2019-07-24 stsp goto done;
7826 0ebf8283 2019-07-24 stsp
7827 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7828 0ebf8283 2019-07-24 stsp if (n < 0)
7829 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7830 0ebf8283 2019-07-24 stsp done:
7831 0ebf8283 2019-07-24 stsp if (commit)
7832 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7833 0ebf8283 2019-07-24 stsp free(id_str);
7834 0ebf8283 2019-07-24 stsp free(logmsg);
7835 0ebf8283 2019-07-24 stsp return err;
7836 0ebf8283 2019-07-24 stsp }
7837 0ebf8283 2019-07-24 stsp
7838 0ebf8283 2019-07-24 stsp static const struct got_error *
7839 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7840 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7841 0ebf8283 2019-07-24 stsp {
7842 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7843 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7844 0ebf8283 2019-07-24 stsp
7845 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7846 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7847 0ebf8283 2019-07-24 stsp
7848 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7849 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7850 0ebf8283 2019-07-24 stsp f, repo);
7851 0ebf8283 2019-07-24 stsp if (err)
7852 0ebf8283 2019-07-24 stsp break;
7853 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7854 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7855 083957f4 2020-02-24 stsp if (n < 0) {
7856 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7857 083957f4 2020-02-24 stsp break;
7858 083957f4 2020-02-24 stsp }
7859 083957f4 2020-02-24 stsp }
7860 0ebf8283 2019-07-24 stsp }
7861 0ebf8283 2019-07-24 stsp
7862 0ebf8283 2019-07-24 stsp return err;
7863 0ebf8283 2019-07-24 stsp }
7864 0ebf8283 2019-07-24 stsp
7865 0ebf8283 2019-07-24 stsp static const struct got_error *
7866 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7867 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7868 0ebf8283 2019-07-24 stsp {
7869 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7870 0ebf8283 2019-07-24 stsp int n, i;
7871 514f2ffe 2020-01-29 stsp char *id_str;
7872 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7873 514f2ffe 2020-01-29 stsp
7874 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7875 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7876 514f2ffe 2020-01-29 stsp if (err)
7877 514f2ffe 2020-01-29 stsp return err;
7878 514f2ffe 2020-01-29 stsp
7879 514f2ffe 2020-01-29 stsp n = fprintf(f,
7880 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7881 514f2ffe 2020-01-29 stsp "# commit %s\n"
7882 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7883 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7884 514f2ffe 2020-01-29 stsp if (n < 0) {
7885 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7886 514f2ffe 2020-01-29 stsp goto done;
7887 514f2ffe 2020-01-29 stsp }
7888 0ebf8283 2019-07-24 stsp
7889 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7890 514f2ffe 2020-01-29 stsp if (n < 0) {
7891 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7892 514f2ffe 2020-01-29 stsp goto done;
7893 514f2ffe 2020-01-29 stsp }
7894 0ebf8283 2019-07-24 stsp
7895 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7896 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7897 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7898 0ebf8283 2019-07-24 stsp cmd->desc);
7899 0ebf8283 2019-07-24 stsp if (n < 0) {
7900 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7901 0ebf8283 2019-07-24 stsp break;
7902 0ebf8283 2019-07-24 stsp }
7903 0ebf8283 2019-07-24 stsp }
7904 514f2ffe 2020-01-29 stsp done:
7905 514f2ffe 2020-01-29 stsp free(id_str);
7906 0ebf8283 2019-07-24 stsp return err;
7907 0ebf8283 2019-07-24 stsp }
7908 0ebf8283 2019-07-24 stsp
7909 0ebf8283 2019-07-24 stsp static const struct got_error *
7910 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7911 0ebf8283 2019-07-24 stsp {
7912 0ebf8283 2019-07-24 stsp static char msg[42];
7913 0ebf8283 2019-07-24 stsp int ret;
7914 0ebf8283 2019-07-24 stsp
7915 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7916 0ebf8283 2019-07-24 stsp lineno);
7917 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7918 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7919 0ebf8283 2019-07-24 stsp
7920 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7921 0ebf8283 2019-07-24 stsp }
7922 0ebf8283 2019-07-24 stsp
7923 0ebf8283 2019-07-24 stsp static const struct got_error *
7924 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7925 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7926 0ebf8283 2019-07-24 stsp {
7927 0ebf8283 2019-07-24 stsp const struct got_error *err;
7928 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7929 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7930 0ebf8283 2019-07-24 stsp
7931 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7932 0ebf8283 2019-07-24 stsp if (err)
7933 0ebf8283 2019-07-24 stsp return err;
7934 0ebf8283 2019-07-24 stsp
7935 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7936 0ebf8283 2019-07-24 stsp if (err)
7937 0ebf8283 2019-07-24 stsp goto done;
7938 0ebf8283 2019-07-24 stsp
7939 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7940 5943eee2 2019-08-13 stsp if (err)
7941 5943eee2 2019-08-13 stsp goto done;
7942 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7943 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7944 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7945 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7946 0ebf8283 2019-07-24 stsp }
7947 0ebf8283 2019-07-24 stsp done:
7948 0ebf8283 2019-07-24 stsp if (folded_commit)
7949 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7950 0ebf8283 2019-07-24 stsp free(id_str);
7951 5943eee2 2019-08-13 stsp free(folded_logmsg);
7952 0ebf8283 2019-07-24 stsp return err;
7953 0ebf8283 2019-07-24 stsp }
7954 0ebf8283 2019-07-24 stsp
7955 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7956 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7957 0ebf8283 2019-07-24 stsp {
7958 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7959 0ebf8283 2019-07-24 stsp
7960 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7961 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7962 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7963 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7964 3f9de99f 2019-07-24 stsp folded = prev;
7965 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7966 0ebf8283 2019-07-24 stsp }
7967 0ebf8283 2019-07-24 stsp
7968 0ebf8283 2019-07-24 stsp return folded;
7969 0ebf8283 2019-07-24 stsp }
7970 0ebf8283 2019-07-24 stsp
7971 0ebf8283 2019-07-24 stsp static const struct got_error *
7972 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7973 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7974 0ebf8283 2019-07-24 stsp {
7975 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7976 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7977 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7978 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7979 1601cb9f 2020-09-11 naddy int logmsg_len;
7980 0ebf8283 2019-07-24 stsp int fd;
7981 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7982 0ebf8283 2019-07-24 stsp
7983 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7984 0ebf8283 2019-07-24 stsp if (err)
7985 0ebf8283 2019-07-24 stsp return err;
7986 0ebf8283 2019-07-24 stsp
7987 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7988 0ebf8283 2019-07-24 stsp if (folded) {
7989 0ebf8283 2019-07-24 stsp while (folded != hle) {
7990 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7991 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7992 3f9de99f 2019-07-24 stsp continue;
7993 3f9de99f 2019-07-24 stsp }
7994 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7995 0ebf8283 2019-07-24 stsp logmsg, repo);
7996 0ebf8283 2019-07-24 stsp if (err)
7997 0ebf8283 2019-07-24 stsp goto done;
7998 0ebf8283 2019-07-24 stsp free(logmsg);
7999 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8000 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
8001 0ebf8283 2019-07-24 stsp }
8002 0ebf8283 2019-07-24 stsp }
8003 0ebf8283 2019-07-24 stsp
8004 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8005 0ebf8283 2019-07-24 stsp if (err)
8006 0ebf8283 2019-07-24 stsp goto done;
8007 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
8008 5943eee2 2019-08-13 stsp if (err)
8009 5943eee2 2019-08-13 stsp goto done;
8010 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
8011 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
8012 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
8013 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
8014 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
8015 0ebf8283 2019-07-24 stsp goto done;
8016 0ebf8283 2019-07-24 stsp }
8017 0ebf8283 2019-07-24 stsp free(logmsg);
8018 0ebf8283 2019-07-24 stsp logmsg = new_msg;
8019 0ebf8283 2019-07-24 stsp
8020 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
8021 0ebf8283 2019-07-24 stsp if (err)
8022 0ebf8283 2019-07-24 stsp goto done;
8023 0ebf8283 2019-07-24 stsp
8024 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
8025 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
8026 0ebf8283 2019-07-24 stsp if (err)
8027 0ebf8283 2019-07-24 stsp goto done;
8028 0ebf8283 2019-07-24 stsp
8029 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
8030 0ebf8283 2019-07-24 stsp close(fd);
8031 0ebf8283 2019-07-24 stsp
8032 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8033 0ebf8283 2019-07-24 stsp if (err)
8034 0ebf8283 2019-07-24 stsp goto done;
8035 0ebf8283 2019-07-24 stsp
8036 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8037 bfa12d5e 2020-09-26 stsp logmsg_len);
8038 0ebf8283 2019-07-24 stsp if (err) {
8039 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8040 0ebf8283 2019-07-24 stsp goto done;
8041 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
8042 0ebf8283 2019-07-24 stsp }
8043 0ebf8283 2019-07-24 stsp done:
8044 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8045 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
8046 0ebf8283 2019-07-24 stsp free(logmsg_path);
8047 0ebf8283 2019-07-24 stsp free(logmsg);
8048 5943eee2 2019-08-13 stsp free(orig_logmsg);
8049 0ebf8283 2019-07-24 stsp free(editor);
8050 0ebf8283 2019-07-24 stsp if (commit)
8051 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8052 0ebf8283 2019-07-24 stsp return err;
8053 5ef14e63 2019-06-02 stsp }
8054 0ebf8283 2019-07-24 stsp
8055 0ebf8283 2019-07-24 stsp static const struct got_error *
8056 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
8057 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8058 0ebf8283 2019-07-24 stsp {
8059 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8060 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
8061 0ebf8283 2019-07-24 stsp size_t size;
8062 0ebf8283 2019-07-24 stsp ssize_t len;
8063 0ebf8283 2019-07-24 stsp int lineno = 0, i;
8064 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8065 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8066 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8067 0ebf8283 2019-07-24 stsp
8068 0ebf8283 2019-07-24 stsp for (;;) {
8069 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8070 0ebf8283 2019-07-24 stsp if (len == -1) {
8071 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8072 0ebf8283 2019-07-24 stsp if (feof(f))
8073 0ebf8283 2019-07-24 stsp break;
8074 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8075 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8076 0ebf8283 2019-07-24 stsp break;
8077 0ebf8283 2019-07-24 stsp }
8078 0ebf8283 2019-07-24 stsp lineno++;
8079 0ebf8283 2019-07-24 stsp p = line;
8080 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8081 0ebf8283 2019-07-24 stsp p++;
8082 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8083 0ebf8283 2019-07-24 stsp free(line);
8084 0ebf8283 2019-07-24 stsp line = NULL;
8085 0ebf8283 2019-07-24 stsp continue;
8086 0ebf8283 2019-07-24 stsp }
8087 0ebf8283 2019-07-24 stsp cmd = NULL;
8088 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8089 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8090 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8091 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8092 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8093 0ebf8283 2019-07-24 stsp break;
8094 0ebf8283 2019-07-24 stsp }
8095 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8096 0ebf8283 2019-07-24 stsp p++;
8097 0ebf8283 2019-07-24 stsp break;
8098 0ebf8283 2019-07-24 stsp }
8099 0ebf8283 2019-07-24 stsp }
8100 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8101 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8102 0ebf8283 2019-07-24 stsp break;
8103 0ebf8283 2019-07-24 stsp }
8104 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8105 0ebf8283 2019-07-24 stsp p++;
8106 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8107 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8108 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8109 0ebf8283 2019-07-24 stsp break;
8110 0ebf8283 2019-07-24 stsp }
8111 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8112 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8113 0ebf8283 2019-07-24 stsp if (err)
8114 0ebf8283 2019-07-24 stsp break;
8115 0ebf8283 2019-07-24 stsp } else {
8116 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8117 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8118 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8119 0ebf8283 2019-07-24 stsp break;
8120 0ebf8283 2019-07-24 stsp }
8121 0ebf8283 2019-07-24 stsp }
8122 0ebf8283 2019-07-24 stsp free(line);
8123 0ebf8283 2019-07-24 stsp line = NULL;
8124 0ebf8283 2019-07-24 stsp continue;
8125 0ebf8283 2019-07-24 stsp } else {
8126 0ebf8283 2019-07-24 stsp end = p;
8127 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8128 0ebf8283 2019-07-24 stsp end++;
8129 0ebf8283 2019-07-24 stsp *end = '\0';
8130 0ebf8283 2019-07-24 stsp
8131 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8132 0ebf8283 2019-07-24 stsp if (err) {
8133 0ebf8283 2019-07-24 stsp /* override error code */
8134 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8135 0ebf8283 2019-07-24 stsp break;
8136 0ebf8283 2019-07-24 stsp }
8137 0ebf8283 2019-07-24 stsp }
8138 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8139 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8140 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8141 0ebf8283 2019-07-24 stsp break;
8142 0ebf8283 2019-07-24 stsp }
8143 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8144 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8145 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8146 0ebf8283 2019-07-24 stsp commit_id = NULL;
8147 0ebf8283 2019-07-24 stsp free(line);
8148 0ebf8283 2019-07-24 stsp line = NULL;
8149 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8150 0ebf8283 2019-07-24 stsp }
8151 0ebf8283 2019-07-24 stsp
8152 0ebf8283 2019-07-24 stsp free(line);
8153 0ebf8283 2019-07-24 stsp free(commit_id);
8154 0ebf8283 2019-07-24 stsp return err;
8155 0ebf8283 2019-07-24 stsp }
8156 0ebf8283 2019-07-24 stsp
8157 0ebf8283 2019-07-24 stsp static const struct got_error *
8158 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8159 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8160 bfce7f83 2019-07-27 stsp {
8161 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8162 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8163 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8164 5b87815e 2020-03-05 stsp static char msg[92];
8165 bfce7f83 2019-07-27 stsp char *id_str;
8166 bfce7f83 2019-07-27 stsp
8167 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8168 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8169 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8170 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
8171 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8172 5b87815e 2020-03-05 stsp
8173 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8174 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8175 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8176 5b87815e 2020-03-05 stsp if (hle == hle2)
8177 5b87815e 2020-03-05 stsp continue;
8178 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8179 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8180 5b87815e 2020-03-05 stsp continue;
8181 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8182 5b87815e 2020-03-05 stsp if (err)
8183 5b87815e 2020-03-05 stsp return err;
8184 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8185 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8186 5b87815e 2020-03-05 stsp free(id_str);
8187 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8188 5b87815e 2020-03-05 stsp }
8189 5b87815e 2020-03-05 stsp }
8190 bfce7f83 2019-07-27 stsp
8191 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8192 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8193 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8194 bfce7f83 2019-07-27 stsp break;
8195 bfce7f83 2019-07-27 stsp }
8196 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8197 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8198 bfce7f83 2019-07-27 stsp if (err)
8199 bfce7f83 2019-07-27 stsp return err;
8200 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8201 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8202 bfce7f83 2019-07-27 stsp free(id_str);
8203 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8204 bfce7f83 2019-07-27 stsp }
8205 bfce7f83 2019-07-27 stsp }
8206 bfce7f83 2019-07-27 stsp
8207 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8208 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8209 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8210 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8211 bfce7f83 2019-07-27 stsp
8212 bfce7f83 2019-07-27 stsp return NULL;
8213 bfce7f83 2019-07-27 stsp }
8214 bfce7f83 2019-07-27 stsp
8215 bfce7f83 2019-07-27 stsp static const struct got_error *
8216 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8217 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8218 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8219 0ebf8283 2019-07-24 stsp {
8220 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8221 0ebf8283 2019-07-24 stsp char *editor;
8222 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8223 0ebf8283 2019-07-24 stsp
8224 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8225 0ebf8283 2019-07-24 stsp if (err)
8226 0ebf8283 2019-07-24 stsp return err;
8227 0ebf8283 2019-07-24 stsp
8228 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8229 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8230 0ebf8283 2019-07-24 stsp goto done;
8231 0ebf8283 2019-07-24 stsp }
8232 0ebf8283 2019-07-24 stsp
8233 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8234 0ebf8283 2019-07-24 stsp if (f == NULL) {
8235 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8236 0ebf8283 2019-07-24 stsp goto done;
8237 0ebf8283 2019-07-24 stsp }
8238 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8239 bfce7f83 2019-07-27 stsp if (err)
8240 bfce7f83 2019-07-27 stsp goto done;
8241 bfce7f83 2019-07-27 stsp
8242 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8243 0ebf8283 2019-07-24 stsp done:
8244 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8245 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8246 0ebf8283 2019-07-24 stsp free(editor);
8247 0ebf8283 2019-07-24 stsp return err;
8248 0ebf8283 2019-07-24 stsp }
8249 0ebf8283 2019-07-24 stsp
8250 0ebf8283 2019-07-24 stsp static const struct got_error *
8251 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8252 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8253 514f2ffe 2020-01-29 stsp struct got_repository *);
8254 0ebf8283 2019-07-24 stsp
8255 0ebf8283 2019-07-24 stsp static const struct got_error *
8256 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8257 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8258 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
8259 0ebf8283 2019-07-24 stsp {
8260 0ebf8283 2019-07-24 stsp const struct got_error *err;
8261 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8262 0ebf8283 2019-07-24 stsp char *path = NULL;
8263 0ebf8283 2019-07-24 stsp
8264 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8265 0ebf8283 2019-07-24 stsp if (err)
8266 0ebf8283 2019-07-24 stsp return err;
8267 0ebf8283 2019-07-24 stsp
8268 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8269 0ebf8283 2019-07-24 stsp if (err)
8270 0ebf8283 2019-07-24 stsp goto done;
8271 0ebf8283 2019-07-24 stsp
8272 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8273 0ebf8283 2019-07-24 stsp if (err)
8274 0ebf8283 2019-07-24 stsp goto done;
8275 0ebf8283 2019-07-24 stsp
8276 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8277 083957f4 2020-02-24 stsp rewind(f);
8278 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8279 083957f4 2020-02-24 stsp } else {
8280 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8281 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8282 0ebf8283 2019-07-24 stsp goto done;
8283 083957f4 2020-02-24 stsp }
8284 083957f4 2020-02-24 stsp f = NULL;
8285 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8286 083957f4 2020-02-24 stsp if (err) {
8287 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8288 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8289 083957f4 2020-02-24 stsp goto done;
8290 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8291 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8292 083957f4 2020-02-24 stsp }
8293 0ebf8283 2019-07-24 stsp }
8294 0ebf8283 2019-07-24 stsp done:
8295 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8296 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8297 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8298 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8299 0ebf8283 2019-07-24 stsp free(path);
8300 0ebf8283 2019-07-24 stsp return err;
8301 0ebf8283 2019-07-24 stsp }
8302 0ebf8283 2019-07-24 stsp
8303 0ebf8283 2019-07-24 stsp static const struct got_error *
8304 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8305 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8306 0ebf8283 2019-07-24 stsp {
8307 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8308 0ebf8283 2019-07-24 stsp char *path = NULL;
8309 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8310 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8311 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8312 0ebf8283 2019-07-24 stsp
8313 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8314 0ebf8283 2019-07-24 stsp if (err)
8315 0ebf8283 2019-07-24 stsp return err;
8316 0ebf8283 2019-07-24 stsp
8317 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8318 0ebf8283 2019-07-24 stsp if (f == NULL) {
8319 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8320 0ebf8283 2019-07-24 stsp goto done;
8321 0ebf8283 2019-07-24 stsp }
8322 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8323 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8324 0ebf8283 2019-07-24 stsp repo);
8325 0ebf8283 2019-07-24 stsp if (err)
8326 0ebf8283 2019-07-24 stsp break;
8327 0ebf8283 2019-07-24 stsp
8328 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8329 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8330 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8331 0ebf8283 2019-07-24 stsp if (n < 0) {
8332 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8333 0ebf8283 2019-07-24 stsp break;
8334 0ebf8283 2019-07-24 stsp }
8335 0ebf8283 2019-07-24 stsp }
8336 0ebf8283 2019-07-24 stsp }
8337 0ebf8283 2019-07-24 stsp done:
8338 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8339 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8340 0ebf8283 2019-07-24 stsp free(path);
8341 0ebf8283 2019-07-24 stsp if (commit)
8342 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8343 0ebf8283 2019-07-24 stsp return err;
8344 0ebf8283 2019-07-24 stsp }
8345 0ebf8283 2019-07-24 stsp
8346 bfce7f83 2019-07-27 stsp void
8347 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8348 bfce7f83 2019-07-27 stsp {
8349 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8350 bfce7f83 2019-07-27 stsp
8351 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8352 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8353 bfce7f83 2019-07-27 stsp free(hle);
8354 bfce7f83 2019-07-27 stsp }
8355 bfce7f83 2019-07-27 stsp }
8356 bfce7f83 2019-07-27 stsp
8357 0ebf8283 2019-07-24 stsp static const struct got_error *
8358 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8359 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8360 0ebf8283 2019-07-24 stsp {
8361 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8362 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8363 0ebf8283 2019-07-24 stsp
8364 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8365 0ebf8283 2019-07-24 stsp if (f == NULL) {
8366 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8367 0ebf8283 2019-07-24 stsp goto done;
8368 0ebf8283 2019-07-24 stsp }
8369 0ebf8283 2019-07-24 stsp
8370 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8371 0ebf8283 2019-07-24 stsp done:
8372 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8373 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8374 0ebf8283 2019-07-24 stsp return err;
8375 0ebf8283 2019-07-24 stsp }
8376 0ebf8283 2019-07-24 stsp
8377 0ebf8283 2019-07-24 stsp static const struct got_error *
8378 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8379 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8380 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8381 0ebf8283 2019-07-24 stsp {
8382 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8383 0ebf8283 2019-07-24 stsp int resp = ' ';
8384 0ebf8283 2019-07-24 stsp
8385 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8386 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8387 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8388 0ebf8283 2019-07-24 stsp resp = getchar();
8389 a61a4414 2019-08-07 stsp if (resp == '\n')
8390 a61a4414 2019-08-07 stsp resp = getchar();
8391 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8392 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8393 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8394 bfce7f83 2019-07-27 stsp repo);
8395 426ebf2e 2019-07-25 stsp if (err) {
8396 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8397 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8398 426ebf2e 2019-07-25 stsp break;
8399 bfce7f83 2019-07-27 stsp prev_err = err;
8400 426ebf2e 2019-07-25 stsp resp = ' ';
8401 426ebf2e 2019-07-25 stsp continue;
8402 426ebf2e 2019-07-25 stsp }
8403 bfce7f83 2019-07-27 stsp break;
8404 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8405 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8406 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8407 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8408 426ebf2e 2019-07-25 stsp if (err) {
8409 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8410 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8411 426ebf2e 2019-07-25 stsp break;
8412 bfce7f83 2019-07-27 stsp prev_err = err;
8413 426ebf2e 2019-07-25 stsp resp = ' ';
8414 426ebf2e 2019-07-25 stsp continue;
8415 426ebf2e 2019-07-25 stsp }
8416 bfce7f83 2019-07-27 stsp break;
8417 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8418 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8419 0ebf8283 2019-07-24 stsp break;
8420 426ebf2e 2019-07-25 stsp } else
8421 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8422 0ebf8283 2019-07-24 stsp }
8423 0ebf8283 2019-07-24 stsp
8424 0ebf8283 2019-07-24 stsp return err;
8425 0ebf8283 2019-07-24 stsp }
8426 0ebf8283 2019-07-24 stsp
8427 0ebf8283 2019-07-24 stsp static const struct got_error *
8428 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8429 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8430 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8431 0ebf8283 2019-07-24 stsp {
8432 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8433 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8434 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8435 3e3a69f1 2019-07-25 stsp branch, repo);
8436 0ebf8283 2019-07-24 stsp }
8437 0ebf8283 2019-07-24 stsp
8438 0ebf8283 2019-07-24 stsp static const struct got_error *
8439 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8440 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8441 0ebf8283 2019-07-24 stsp {
8442 0ebf8283 2019-07-24 stsp const struct got_error *err;
8443 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8444 0ebf8283 2019-07-24 stsp
8445 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8446 0ebf8283 2019-07-24 stsp if (err)
8447 0ebf8283 2019-07-24 stsp goto done;
8448 0ebf8283 2019-07-24 stsp
8449 0ebf8283 2019-07-24 stsp if (new_id) {
8450 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8451 0ebf8283 2019-07-24 stsp if (err)
8452 0ebf8283 2019-07-24 stsp goto done;
8453 0ebf8283 2019-07-24 stsp }
8454 0ebf8283 2019-07-24 stsp
8455 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8456 0ebf8283 2019-07-24 stsp if (new_id_str)
8457 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8458 0ebf8283 2019-07-24 stsp
8459 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8460 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8461 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8462 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8463 0ebf8283 2019-07-24 stsp goto done;
8464 0ebf8283 2019-07-24 stsp }
8465 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8466 0ebf8283 2019-07-24 stsp } else {
8467 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8468 0ebf8283 2019-07-24 stsp if (err)
8469 0ebf8283 2019-07-24 stsp goto done;
8470 0ebf8283 2019-07-24 stsp }
8471 0ebf8283 2019-07-24 stsp
8472 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8473 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8474 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8475 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8476 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8477 0ebf8283 2019-07-24 stsp break;
8478 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8479 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8480 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8481 0ebf8283 2019-07-24 stsp logmsg);
8482 0ebf8283 2019-07-24 stsp break;
8483 0ebf8283 2019-07-24 stsp default:
8484 0ebf8283 2019-07-24 stsp break;
8485 0ebf8283 2019-07-24 stsp }
8486 0ebf8283 2019-07-24 stsp done:
8487 0ebf8283 2019-07-24 stsp free(old_id_str);
8488 0ebf8283 2019-07-24 stsp free(new_id_str);
8489 0ebf8283 2019-07-24 stsp return err;
8490 0ebf8283 2019-07-24 stsp }
8491 0ebf8283 2019-07-24 stsp
8492 0ebf8283 2019-07-24 stsp static const struct got_error *
8493 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8494 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8495 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8496 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8497 0ebf8283 2019-07-24 stsp {
8498 0ebf8283 2019-07-24 stsp const struct got_error *err;
8499 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8500 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8501 0ebf8283 2019-07-24 stsp
8502 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8503 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8504 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8505 0ebf8283 2019-07-24 stsp if (err)
8506 0ebf8283 2019-07-24 stsp return err;
8507 0ebf8283 2019-07-24 stsp }
8508 0ebf8283 2019-07-24 stsp
8509 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8510 0ebf8283 2019-07-24 stsp if (err)
8511 0ebf8283 2019-07-24 stsp return err;
8512 0ebf8283 2019-07-24 stsp
8513 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8514 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8515 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8516 0ebf8283 2019-07-24 stsp if (err) {
8517 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8518 0ebf8283 2019-07-24 stsp goto done;
8519 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8520 0ebf8283 2019-07-24 stsp } else {
8521 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8522 0ebf8283 2019-07-24 stsp free(new_commit_id);
8523 0ebf8283 2019-07-24 stsp }
8524 0ebf8283 2019-07-24 stsp done:
8525 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8526 0ebf8283 2019-07-24 stsp return err;
8527 0ebf8283 2019-07-24 stsp }
8528 0ebf8283 2019-07-24 stsp
8529 0ebf8283 2019-07-24 stsp static const struct got_error *
8530 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8531 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8532 0ebf8283 2019-07-24 stsp {
8533 0ebf8283 2019-07-24 stsp const struct got_error *error;
8534 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8535 0ebf8283 2019-07-24 stsp
8536 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8537 0ebf8283 2019-07-24 stsp repo);
8538 0ebf8283 2019-07-24 stsp if (error)
8539 0ebf8283 2019-07-24 stsp return error;
8540 0ebf8283 2019-07-24 stsp
8541 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8542 0ebf8283 2019-07-24 stsp if (error)
8543 0ebf8283 2019-07-24 stsp return error;
8544 0ebf8283 2019-07-24 stsp
8545 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8546 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8547 0ebf8283 2019-07-24 stsp return error;
8548 ab20a43a 2020-01-29 stsp }
8549 ab20a43a 2020-01-29 stsp
8550 ab20a43a 2020-01-29 stsp static const struct got_error *
8551 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8552 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8553 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8554 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8555 ab20a43a 2020-01-29 stsp {
8556 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8557 ab20a43a 2020-01-29 stsp
8558 ab20a43a 2020-01-29 stsp switch (status) {
8559 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8560 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8561 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8562 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8563 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8564 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8565 ab20a43a 2020-01-29 stsp default:
8566 ab20a43a 2020-01-29 stsp break;
8567 ab20a43a 2020-01-29 stsp }
8568 ab20a43a 2020-01-29 stsp
8569 ab20a43a 2020-01-29 stsp switch (staged_status) {
8570 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8571 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8572 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8573 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8574 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8575 ab20a43a 2020-01-29 stsp default:
8576 ab20a43a 2020-01-29 stsp break;
8577 ab20a43a 2020-01-29 stsp }
8578 ab20a43a 2020-01-29 stsp
8579 ab20a43a 2020-01-29 stsp return NULL;
8580 0ebf8283 2019-07-24 stsp }
8581 0ebf8283 2019-07-24 stsp
8582 0ebf8283 2019-07-24 stsp static const struct got_error *
8583 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8584 0ebf8283 2019-07-24 stsp {
8585 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8586 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8587 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8588 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8589 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8590 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8591 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8592 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8593 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8594 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8595 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8596 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8597 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8598 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8599 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8600 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8601 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8602 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8603 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8604 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8605 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8606 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8607 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8608 0ebf8283 2019-07-24 stsp
8609 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8610 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8611 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8612 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8613 0ebf8283 2019-07-24 stsp
8614 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8615 0ebf8283 2019-07-24 stsp switch (ch) {
8616 0ebf8283 2019-07-24 stsp case 'a':
8617 0ebf8283 2019-07-24 stsp abort_edit = 1;
8618 0ebf8283 2019-07-24 stsp break;
8619 0ebf8283 2019-07-24 stsp case 'c':
8620 0ebf8283 2019-07-24 stsp continue_edit = 1;
8621 0ebf8283 2019-07-24 stsp break;
8622 0ebf8283 2019-07-24 stsp case 'F':
8623 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8624 0ebf8283 2019-07-24 stsp break;
8625 083957f4 2020-02-24 stsp case 'm':
8626 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8627 083957f4 2020-02-24 stsp break;
8628 0ebf8283 2019-07-24 stsp default:
8629 0ebf8283 2019-07-24 stsp usage_histedit();
8630 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8631 0ebf8283 2019-07-24 stsp }
8632 0ebf8283 2019-07-24 stsp }
8633 0ebf8283 2019-07-24 stsp
8634 0ebf8283 2019-07-24 stsp argc -= optind;
8635 0ebf8283 2019-07-24 stsp argv += optind;
8636 0ebf8283 2019-07-24 stsp
8637 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8638 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8639 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8640 0ebf8283 2019-07-24 stsp err(1, "pledge");
8641 0ebf8283 2019-07-24 stsp #endif
8642 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8643 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8644 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8645 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8646 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8647 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8648 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8649 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8650 0ebf8283 2019-07-24 stsp if (argc != 0)
8651 0ebf8283 2019-07-24 stsp usage_histedit();
8652 0ebf8283 2019-07-24 stsp
8653 0ebf8283 2019-07-24 stsp /*
8654 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8655 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8656 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8657 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8658 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8659 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8660 0ebf8283 2019-07-24 stsp */
8661 0ebf8283 2019-07-24 stsp
8662 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8663 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8664 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8665 0ebf8283 2019-07-24 stsp goto done;
8666 0ebf8283 2019-07-24 stsp }
8667 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8668 fa51e947 2020-03-27 stsp if (error) {
8669 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8670 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8671 0ebf8283 2019-07-24 stsp goto done;
8672 fa51e947 2020-03-27 stsp }
8673 0ebf8283 2019-07-24 stsp
8674 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8675 c9956ddf 2019-09-08 stsp NULL);
8676 0ebf8283 2019-07-24 stsp if (error != NULL)
8677 0ebf8283 2019-07-24 stsp goto done;
8678 0ebf8283 2019-07-24 stsp
8679 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8680 0ebf8283 2019-07-24 stsp if (error)
8681 0ebf8283 2019-07-24 stsp goto done;
8682 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8683 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8684 0ebf8283 2019-07-24 stsp goto done;
8685 0ebf8283 2019-07-24 stsp }
8686 0ebf8283 2019-07-24 stsp
8687 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8688 0ebf8283 2019-07-24 stsp if (error)
8689 0ebf8283 2019-07-24 stsp goto done;
8690 0ebf8283 2019-07-24 stsp
8691 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8692 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8693 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8694 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8695 083957f4 2020-02-24 stsp "before the -m option can be used");
8696 083957f4 2020-02-24 stsp goto done;
8697 083957f4 2020-02-24 stsp }
8698 083957f4 2020-02-24 stsp
8699 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8700 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8701 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8702 3e3a69f1 2019-07-25 stsp worktree, repo);
8703 0ebf8283 2019-07-24 stsp if (error)
8704 0ebf8283 2019-07-24 stsp goto done;
8705 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8706 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8707 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8708 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8709 0ebf8283 2019-07-24 stsp if (error)
8710 0ebf8283 2019-07-24 stsp goto done;
8711 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8712 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8713 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8714 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8715 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8716 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8717 0ebf8283 2019-07-24 stsp goto done;
8718 0ebf8283 2019-07-24 stsp }
8719 0ebf8283 2019-07-24 stsp
8720 0ebf8283 2019-07-24 stsp if (continue_edit) {
8721 0ebf8283 2019-07-24 stsp char *path;
8722 0ebf8283 2019-07-24 stsp
8723 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8724 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8725 0ebf8283 2019-07-24 stsp goto done;
8726 0ebf8283 2019-07-24 stsp }
8727 0ebf8283 2019-07-24 stsp
8728 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8729 0ebf8283 2019-07-24 stsp if (error)
8730 0ebf8283 2019-07-24 stsp goto done;
8731 0ebf8283 2019-07-24 stsp
8732 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8733 0ebf8283 2019-07-24 stsp free(path);
8734 0ebf8283 2019-07-24 stsp if (error)
8735 0ebf8283 2019-07-24 stsp goto done;
8736 0ebf8283 2019-07-24 stsp
8737 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8738 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8739 3e3a69f1 2019-07-25 stsp worktree, repo);
8740 0ebf8283 2019-07-24 stsp if (error)
8741 0ebf8283 2019-07-24 stsp goto done;
8742 0ebf8283 2019-07-24 stsp
8743 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8744 0ebf8283 2019-07-24 stsp if (error)
8745 0ebf8283 2019-07-24 stsp goto done;
8746 0ebf8283 2019-07-24 stsp
8747 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8748 0ebf8283 2019-07-24 stsp head_commit_id);
8749 0ebf8283 2019-07-24 stsp if (error)
8750 0ebf8283 2019-07-24 stsp goto done;
8751 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8752 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8753 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8754 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8755 3aac7cf7 2019-07-25 stsp goto done;
8756 3aac7cf7 2019-07-25 stsp }
8757 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8758 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8759 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8760 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8761 0ebf8283 2019-07-24 stsp commit = NULL;
8762 0ebf8283 2019-07-24 stsp if (error)
8763 0ebf8283 2019-07-24 stsp goto done;
8764 0ebf8283 2019-07-24 stsp } else {
8765 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8766 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8767 0ebf8283 2019-07-24 stsp goto done;
8768 0ebf8283 2019-07-24 stsp }
8769 0ebf8283 2019-07-24 stsp
8770 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8771 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8772 0ebf8283 2019-07-24 stsp if (error != NULL)
8773 0ebf8283 2019-07-24 stsp goto done;
8774 0ebf8283 2019-07-24 stsp
8775 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8776 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8777 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8778 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8779 c7d20a3f 2019-07-30 stsp goto done;
8780 c7d20a3f 2019-07-30 stsp }
8781 c7d20a3f 2019-07-30 stsp
8782 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8783 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8784 bfce7f83 2019-07-27 stsp branch = NULL;
8785 0ebf8283 2019-07-24 stsp if (error)
8786 0ebf8283 2019-07-24 stsp goto done;
8787 0ebf8283 2019-07-24 stsp
8788 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8789 0ebf8283 2019-07-24 stsp head_commit_id);
8790 0ebf8283 2019-07-24 stsp if (error)
8791 0ebf8283 2019-07-24 stsp goto done;
8792 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8793 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8794 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8795 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8796 3aac7cf7 2019-07-25 stsp goto done;
8797 3aac7cf7 2019-07-25 stsp }
8798 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8799 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8800 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8801 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8802 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8803 0ebf8283 2019-07-24 stsp commit = NULL;
8804 0ebf8283 2019-07-24 stsp if (error)
8805 0ebf8283 2019-07-24 stsp goto done;
8806 0ebf8283 2019-07-24 stsp
8807 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8808 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8809 c5996fff 2020-01-29 stsp goto done;
8810 c5996fff 2020-01-29 stsp }
8811 c5996fff 2020-01-29 stsp
8812 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8813 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8814 bfce7f83 2019-07-27 stsp if (error)
8815 bfce7f83 2019-07-27 stsp goto done;
8816 bfce7f83 2019-07-27 stsp
8817 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8818 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8819 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8820 6ba2bea2 2019-07-27 stsp if (error) {
8821 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8822 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8823 9627c110 2020-04-18 stsp update_progress, &upa);
8824 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8825 0ebf8283 2019-07-24 stsp goto done;
8826 6ba2bea2 2019-07-27 stsp }
8827 0ebf8283 2019-07-24 stsp } else {
8828 514f2ffe 2020-01-29 stsp const char *branch_name;
8829 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8830 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8831 514f2ffe 2020-01-29 stsp branch_name += 11;
8832 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8833 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8834 6ba2bea2 2019-07-27 stsp if (error) {
8835 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8836 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8837 9627c110 2020-04-18 stsp update_progress, &upa);
8838 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8839 0ebf8283 2019-07-24 stsp goto done;
8840 6ba2bea2 2019-07-27 stsp }
8841 0ebf8283 2019-07-24 stsp
8842 0ebf8283 2019-07-24 stsp }
8843 0ebf8283 2019-07-24 stsp
8844 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8845 0ebf8283 2019-07-24 stsp repo);
8846 6ba2bea2 2019-07-27 stsp if (error) {
8847 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8848 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8849 9627c110 2020-04-18 stsp update_progress, &upa);
8850 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8851 0ebf8283 2019-07-24 stsp goto done;
8852 6ba2bea2 2019-07-27 stsp }
8853 0ebf8283 2019-07-24 stsp
8854 0ebf8283 2019-07-24 stsp }
8855 0ebf8283 2019-07-24 stsp
8856 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8857 4ec14e60 2019-08-28 hiltjo if (error)
8858 0ebf8283 2019-07-24 stsp goto done;
8859 0ebf8283 2019-07-24 stsp
8860 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8861 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8862 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8863 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8864 0ebf8283 2019-07-24 stsp continue;
8865 0ebf8283 2019-07-24 stsp
8866 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8867 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8868 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8869 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8870 0ebf8283 2019-07-24 stsp repo);
8871 ab20a43a 2020-01-29 stsp if (error)
8872 ab20a43a 2020-01-29 stsp goto done;
8873 0ebf8283 2019-07-24 stsp } else {
8874 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8875 ab20a43a 2020-01-29 stsp int have_changes = 0;
8876 ab20a43a 2020-01-29 stsp
8877 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8878 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8879 ab20a43a 2020-01-29 stsp if (error)
8880 ab20a43a 2020-01-29 stsp goto done;
8881 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8882 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8883 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8884 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8885 ab20a43a 2020-01-29 stsp if (error) {
8886 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8887 ab20a43a 2020-01-29 stsp goto done;
8888 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8889 ab20a43a 2020-01-29 stsp goto done;
8890 ab20a43a 2020-01-29 stsp }
8891 ab20a43a 2020-01-29 stsp if (have_changes) {
8892 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8893 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8894 ab20a43a 2020-01-29 stsp if (error)
8895 ab20a43a 2020-01-29 stsp goto done;
8896 ab20a43a 2020-01-29 stsp } else {
8897 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8898 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8899 ab20a43a 2020-01-29 stsp if (error)
8900 ab20a43a 2020-01-29 stsp goto done;
8901 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8902 ab20a43a 2020-01-29 stsp hle, NULL);
8903 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8904 ab20a43a 2020-01-29 stsp commit = NULL;
8905 ab20a43a 2020-01-29 stsp if (error)
8906 ab20a43a 2020-01-29 stsp goto done;
8907 ab20a43a 2020-01-29 stsp }
8908 0ebf8283 2019-07-24 stsp }
8909 0ebf8283 2019-07-24 stsp continue;
8910 0ebf8283 2019-07-24 stsp }
8911 0ebf8283 2019-07-24 stsp
8912 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8913 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8914 0ebf8283 2019-07-24 stsp if (error)
8915 0ebf8283 2019-07-24 stsp goto done;
8916 0ebf8283 2019-07-24 stsp continue;
8917 0ebf8283 2019-07-24 stsp }
8918 0ebf8283 2019-07-24 stsp
8919 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8920 0ebf8283 2019-07-24 stsp hle->commit_id);
8921 0ebf8283 2019-07-24 stsp if (error)
8922 0ebf8283 2019-07-24 stsp goto done;
8923 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8924 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8925 0ebf8283 2019-07-24 stsp
8926 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8927 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8928 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8929 0ebf8283 2019-07-24 stsp if (error)
8930 0ebf8283 2019-07-24 stsp goto done;
8931 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8932 0ebf8283 2019-07-24 stsp commit = NULL;
8933 0ebf8283 2019-07-24 stsp
8934 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8935 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8936 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8937 9627c110 2020-04-18 stsp
8938 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8939 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8940 a0ea4fc0 2020-02-28 stsp repo);
8941 a0ea4fc0 2020-02-28 stsp if (error)
8942 a0ea4fc0 2020-02-28 stsp goto done;
8943 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8944 0ebf8283 2019-07-24 stsp break;
8945 0ebf8283 2019-07-24 stsp }
8946 0ebf8283 2019-07-24 stsp
8947 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8948 0ebf8283 2019-07-24 stsp char *id_str;
8949 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8950 0ebf8283 2019-07-24 stsp if (error)
8951 0ebf8283 2019-07-24 stsp goto done;
8952 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8953 0ebf8283 2019-07-24 stsp id_str);
8954 0ebf8283 2019-07-24 stsp free(id_str);
8955 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8956 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8957 3e3a69f1 2019-07-25 stsp fileindex);
8958 0ebf8283 2019-07-24 stsp goto done;
8959 0ebf8283 2019-07-24 stsp }
8960 0ebf8283 2019-07-24 stsp
8961 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8962 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8963 0ebf8283 2019-07-24 stsp if (error)
8964 0ebf8283 2019-07-24 stsp goto done;
8965 0ebf8283 2019-07-24 stsp continue;
8966 0ebf8283 2019-07-24 stsp }
8967 0ebf8283 2019-07-24 stsp
8968 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8969 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8970 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8971 0ebf8283 2019-07-24 stsp if (error)
8972 0ebf8283 2019-07-24 stsp goto done;
8973 0ebf8283 2019-07-24 stsp }
8974 0ebf8283 2019-07-24 stsp
8975 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8976 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8977 0ebf8283 2019-07-24 stsp if (error)
8978 0ebf8283 2019-07-24 stsp goto done;
8979 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8980 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8981 0ebf8283 2019-07-24 stsp } else
8982 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8983 3e3a69f1 2019-07-25 stsp branch, repo);
8984 0ebf8283 2019-07-24 stsp done:
8985 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8986 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8987 0ebf8283 2019-07-24 stsp free(head_commit_id);
8988 0ebf8283 2019-07-24 stsp free(base_commit_id);
8989 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8990 0ebf8283 2019-07-24 stsp if (commit)
8991 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8992 0ebf8283 2019-07-24 stsp if (branch)
8993 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8994 0ebf8283 2019-07-24 stsp if (tmp_branch)
8995 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8996 0ebf8283 2019-07-24 stsp if (worktree)
8997 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8998 2822a352 2019-10-15 stsp if (repo)
8999 2822a352 2019-10-15 stsp got_repo_close(repo);
9000 2822a352 2019-10-15 stsp return error;
9001 2822a352 2019-10-15 stsp }
9002 2822a352 2019-10-15 stsp
9003 2822a352 2019-10-15 stsp __dead static void
9004 2822a352 2019-10-15 stsp usage_integrate(void)
9005 2822a352 2019-10-15 stsp {
9006 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
9007 2822a352 2019-10-15 stsp exit(1);
9008 2822a352 2019-10-15 stsp }
9009 2822a352 2019-10-15 stsp
9010 2822a352 2019-10-15 stsp static const struct got_error *
9011 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
9012 2822a352 2019-10-15 stsp {
9013 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
9014 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
9015 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
9016 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
9017 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
9018 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
9019 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
9020 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
9021 9627c110 2020-04-18 stsp int ch;
9022 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9023 2822a352 2019-10-15 stsp
9024 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9025 2822a352 2019-10-15 stsp switch (ch) {
9026 2822a352 2019-10-15 stsp default:
9027 2822a352 2019-10-15 stsp usage_integrate();
9028 2822a352 2019-10-15 stsp /* NOTREACHED */
9029 2822a352 2019-10-15 stsp }
9030 2822a352 2019-10-15 stsp }
9031 2822a352 2019-10-15 stsp
9032 2822a352 2019-10-15 stsp argc -= optind;
9033 2822a352 2019-10-15 stsp argv += optind;
9034 2822a352 2019-10-15 stsp
9035 2822a352 2019-10-15 stsp if (argc != 1)
9036 2822a352 2019-10-15 stsp usage_integrate();
9037 2822a352 2019-10-15 stsp branch_arg = argv[0];
9038 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
9039 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9040 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
9041 2822a352 2019-10-15 stsp err(1, "pledge");
9042 b08a0ccd 2020-09-24 stsp #endif
9043 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
9044 2822a352 2019-10-15 stsp if (cwd == NULL) {
9045 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
9046 2822a352 2019-10-15 stsp goto done;
9047 2822a352 2019-10-15 stsp }
9048 2822a352 2019-10-15 stsp
9049 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
9050 fa51e947 2020-03-27 stsp if (error) {
9051 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9052 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
9053 fa51e947 2020-03-27 stsp cwd);
9054 2822a352 2019-10-15 stsp goto done;
9055 fa51e947 2020-03-27 stsp }
9056 2822a352 2019-10-15 stsp
9057 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
9058 2822a352 2019-10-15 stsp if (error)
9059 2822a352 2019-10-15 stsp goto done;
9060 2822a352 2019-10-15 stsp
9061 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9062 2822a352 2019-10-15 stsp NULL);
9063 2822a352 2019-10-15 stsp if (error != NULL)
9064 2822a352 2019-10-15 stsp goto done;
9065 2822a352 2019-10-15 stsp
9066 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9067 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9068 2822a352 2019-10-15 stsp if (error)
9069 2822a352 2019-10-15 stsp goto done;
9070 2822a352 2019-10-15 stsp
9071 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9072 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9073 2822a352 2019-10-15 stsp goto done;
9074 2822a352 2019-10-15 stsp }
9075 2822a352 2019-10-15 stsp
9076 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9077 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9078 2822a352 2019-10-15 stsp if (error)
9079 2822a352 2019-10-15 stsp goto done;
9080 2822a352 2019-10-15 stsp
9081 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9082 2822a352 2019-10-15 stsp if (refname == NULL) {
9083 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9084 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9085 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9086 2822a352 2019-10-15 stsp goto done;
9087 2822a352 2019-10-15 stsp }
9088 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9089 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9090 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9091 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9092 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9093 2822a352 2019-10-15 stsp goto done;
9094 2822a352 2019-10-15 stsp }
9095 2822a352 2019-10-15 stsp
9096 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9097 2822a352 2019-10-15 stsp if (error)
9098 2822a352 2019-10-15 stsp goto done;
9099 2822a352 2019-10-15 stsp
9100 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9101 2822a352 2019-10-15 stsp if (error)
9102 2822a352 2019-10-15 stsp goto done;
9103 2822a352 2019-10-15 stsp
9104 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9105 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9106 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9107 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9108 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9109 2822a352 2019-10-15 stsp goto done;
9110 2822a352 2019-10-15 stsp }
9111 2822a352 2019-10-15 stsp
9112 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9113 2822a352 2019-10-15 stsp if (error) {
9114 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9115 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9116 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9117 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9118 2822a352 2019-10-15 stsp goto done;
9119 2822a352 2019-10-15 stsp }
9120 2822a352 2019-10-15 stsp
9121 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9122 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9123 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9124 2822a352 2019-10-15 stsp check_cancelled, NULL);
9125 2822a352 2019-10-15 stsp if (error)
9126 2822a352 2019-10-15 stsp goto done;
9127 2822a352 2019-10-15 stsp
9128 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9129 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9130 2822a352 2019-10-15 stsp done:
9131 715dc77e 2019-08-03 stsp if (repo)
9132 715dc77e 2019-08-03 stsp got_repo_close(repo);
9133 2822a352 2019-10-15 stsp if (worktree)
9134 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9135 2822a352 2019-10-15 stsp free(cwd);
9136 2822a352 2019-10-15 stsp free(base_commit_id);
9137 2822a352 2019-10-15 stsp free(commit_id);
9138 2822a352 2019-10-15 stsp free(refname);
9139 2822a352 2019-10-15 stsp free(base_refname);
9140 715dc77e 2019-08-03 stsp return error;
9141 715dc77e 2019-08-03 stsp }
9142 715dc77e 2019-08-03 stsp
9143 715dc77e 2019-08-03 stsp __dead static void
9144 715dc77e 2019-08-03 stsp usage_stage(void)
9145 715dc77e 2019-08-03 stsp {
9146 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9147 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9148 715dc77e 2019-08-03 stsp getprogname());
9149 715dc77e 2019-08-03 stsp exit(1);
9150 715dc77e 2019-08-03 stsp }
9151 715dc77e 2019-08-03 stsp
9152 715dc77e 2019-08-03 stsp static const struct got_error *
9153 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9154 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9155 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9156 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9157 408b4ebc 2019-08-03 stsp {
9158 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9159 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9160 408b4ebc 2019-08-03 stsp
9161 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9162 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9163 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9164 408b4ebc 2019-08-03 stsp return NULL;
9165 408b4ebc 2019-08-03 stsp
9166 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9167 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9168 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9169 408b4ebc 2019-08-03 stsp else
9170 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9171 408b4ebc 2019-08-03 stsp if (err)
9172 408b4ebc 2019-08-03 stsp return err;
9173 408b4ebc 2019-08-03 stsp
9174 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9175 408b4ebc 2019-08-03 stsp free(id_str);
9176 dc424a06 2019-08-07 stsp return NULL;
9177 dc424a06 2019-08-07 stsp }
9178 2e1f37b0 2019-08-08 stsp
9179 dc424a06 2019-08-07 stsp static const struct got_error *
9180 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9181 715dc77e 2019-08-03 stsp {
9182 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9183 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9184 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9185 715dc77e 2019-08-03 stsp char *cwd = NULL;
9186 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
9187 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
9188 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9189 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
9190 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9191 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9192 715dc77e 2019-08-03 stsp
9193 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
9194 715dc77e 2019-08-03 stsp
9195 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9196 715dc77e 2019-08-03 stsp switch (ch) {
9197 408b4ebc 2019-08-03 stsp case 'l':
9198 408b4ebc 2019-08-03 stsp list_stage = 1;
9199 408b4ebc 2019-08-03 stsp break;
9200 dc424a06 2019-08-07 stsp case 'p':
9201 dc424a06 2019-08-07 stsp pflag = 1;
9202 dc424a06 2019-08-07 stsp break;
9203 dc424a06 2019-08-07 stsp case 'F':
9204 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9205 dc424a06 2019-08-07 stsp break;
9206 35213c7c 2020-07-23 stsp case 'S':
9207 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9208 35213c7c 2020-07-23 stsp break;
9209 715dc77e 2019-08-03 stsp default:
9210 715dc77e 2019-08-03 stsp usage_stage();
9211 715dc77e 2019-08-03 stsp /* NOTREACHED */
9212 715dc77e 2019-08-03 stsp }
9213 715dc77e 2019-08-03 stsp }
9214 715dc77e 2019-08-03 stsp
9215 715dc77e 2019-08-03 stsp argc -= optind;
9216 715dc77e 2019-08-03 stsp argv += optind;
9217 715dc77e 2019-08-03 stsp
9218 715dc77e 2019-08-03 stsp #ifndef PROFILE
9219 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9220 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9221 715dc77e 2019-08-03 stsp err(1, "pledge");
9222 715dc77e 2019-08-03 stsp #endif
9223 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9224 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9225 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9226 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9227 715dc77e 2019-08-03 stsp
9228 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9229 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9230 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9231 715dc77e 2019-08-03 stsp goto done;
9232 715dc77e 2019-08-03 stsp }
9233 715dc77e 2019-08-03 stsp
9234 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9235 fa51e947 2020-03-27 stsp if (error) {
9236 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9237 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9238 715dc77e 2019-08-03 stsp goto done;
9239 fa51e947 2020-03-27 stsp }
9240 715dc77e 2019-08-03 stsp
9241 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9242 c9956ddf 2019-09-08 stsp NULL);
9243 715dc77e 2019-08-03 stsp if (error != NULL)
9244 715dc77e 2019-08-03 stsp goto done;
9245 715dc77e 2019-08-03 stsp
9246 dc424a06 2019-08-07 stsp if (patch_script_path) {
9247 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9248 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9249 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9250 dc424a06 2019-08-07 stsp patch_script_path);
9251 dc424a06 2019-08-07 stsp goto done;
9252 dc424a06 2019-08-07 stsp }
9253 dc424a06 2019-08-07 stsp }
9254 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9255 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9256 715dc77e 2019-08-03 stsp if (error)
9257 715dc77e 2019-08-03 stsp goto done;
9258 715dc77e 2019-08-03 stsp
9259 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9260 6d022e97 2019-08-04 stsp if (error)
9261 6d022e97 2019-08-04 stsp goto done;
9262 715dc77e 2019-08-03 stsp
9263 6d022e97 2019-08-04 stsp if (list_stage)
9264 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9265 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9266 2e1f37b0 2019-08-08 stsp else {
9267 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9268 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9269 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9270 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9271 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9272 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9273 2e1f37b0 2019-08-08 stsp }
9274 ad493afc 2019-08-03 stsp done:
9275 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9276 2e1f37b0 2019-08-08 stsp error == NULL)
9277 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9278 ad493afc 2019-08-03 stsp if (repo)
9279 ad493afc 2019-08-03 stsp got_repo_close(repo);
9280 ad493afc 2019-08-03 stsp if (worktree)
9281 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9282 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9283 ad493afc 2019-08-03 stsp free((char *)pe->path);
9284 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9285 ad493afc 2019-08-03 stsp free(cwd);
9286 ad493afc 2019-08-03 stsp return error;
9287 ad493afc 2019-08-03 stsp }
9288 ad493afc 2019-08-03 stsp
9289 ad493afc 2019-08-03 stsp __dead static void
9290 ad493afc 2019-08-03 stsp usage_unstage(void)
9291 ad493afc 2019-08-03 stsp {
9292 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9293 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9294 ad493afc 2019-08-03 stsp getprogname());
9295 ad493afc 2019-08-03 stsp exit(1);
9296 ad493afc 2019-08-03 stsp }
9297 ad493afc 2019-08-03 stsp
9298 ad493afc 2019-08-03 stsp
9299 ad493afc 2019-08-03 stsp static const struct got_error *
9300 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9301 ad493afc 2019-08-03 stsp {
9302 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9303 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9304 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9305 ad493afc 2019-08-03 stsp char *cwd = NULL;
9306 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9307 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9308 9627c110 2020-04-18 stsp int ch, pflag = 0;
9309 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9310 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9311 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9312 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9313 ad493afc 2019-08-03 stsp
9314 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9315 ad493afc 2019-08-03 stsp
9316 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9317 ad493afc 2019-08-03 stsp switch (ch) {
9318 2e1f37b0 2019-08-08 stsp case 'p':
9319 2e1f37b0 2019-08-08 stsp pflag = 1;
9320 2e1f37b0 2019-08-08 stsp break;
9321 2e1f37b0 2019-08-08 stsp case 'F':
9322 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9323 2e1f37b0 2019-08-08 stsp break;
9324 ad493afc 2019-08-03 stsp default:
9325 ad493afc 2019-08-03 stsp usage_unstage();
9326 ad493afc 2019-08-03 stsp /* NOTREACHED */
9327 ad493afc 2019-08-03 stsp }
9328 ad493afc 2019-08-03 stsp }
9329 ad493afc 2019-08-03 stsp
9330 ad493afc 2019-08-03 stsp argc -= optind;
9331 ad493afc 2019-08-03 stsp argv += optind;
9332 ad493afc 2019-08-03 stsp
9333 ad493afc 2019-08-03 stsp #ifndef PROFILE
9334 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9335 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9336 ad493afc 2019-08-03 stsp err(1, "pledge");
9337 ad493afc 2019-08-03 stsp #endif
9338 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9339 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9340 2e1f37b0 2019-08-08 stsp
9341 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9342 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9343 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9344 ad493afc 2019-08-03 stsp goto done;
9345 ad493afc 2019-08-03 stsp }
9346 ad493afc 2019-08-03 stsp
9347 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9348 fa51e947 2020-03-27 stsp if (error) {
9349 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9350 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9351 ad493afc 2019-08-03 stsp goto done;
9352 fa51e947 2020-03-27 stsp }
9353 ad493afc 2019-08-03 stsp
9354 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9355 c9956ddf 2019-09-08 stsp NULL);
9356 ad493afc 2019-08-03 stsp if (error != NULL)
9357 ad493afc 2019-08-03 stsp goto done;
9358 ad493afc 2019-08-03 stsp
9359 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9360 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9361 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9362 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9363 2e1f37b0 2019-08-08 stsp patch_script_path);
9364 2e1f37b0 2019-08-08 stsp goto done;
9365 2e1f37b0 2019-08-08 stsp }
9366 2e1f37b0 2019-08-08 stsp }
9367 2e1f37b0 2019-08-08 stsp
9368 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9369 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9370 ad493afc 2019-08-03 stsp if (error)
9371 ad493afc 2019-08-03 stsp goto done;
9372 ad493afc 2019-08-03 stsp
9373 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9374 ad493afc 2019-08-03 stsp if (error)
9375 ad493afc 2019-08-03 stsp goto done;
9376 ad493afc 2019-08-03 stsp
9377 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9378 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9379 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9380 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9381 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9382 9627c110 2020-04-18 stsp if (!error)
9383 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9384 715dc77e 2019-08-03 stsp done:
9385 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9386 2e1f37b0 2019-08-08 stsp error == NULL)
9387 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9388 0ebf8283 2019-07-24 stsp if (repo)
9389 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9390 715dc77e 2019-08-03 stsp if (worktree)
9391 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9392 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9393 715dc77e 2019-08-03 stsp free((char *)pe->path);
9394 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9395 715dc77e 2019-08-03 stsp free(cwd);
9396 01073a5d 2019-08-22 stsp return error;
9397 01073a5d 2019-08-22 stsp }
9398 01073a5d 2019-08-22 stsp
9399 01073a5d 2019-08-22 stsp __dead static void
9400 01073a5d 2019-08-22 stsp usage_cat(void)
9401 01073a5d 2019-08-22 stsp {
9402 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9403 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9404 01073a5d 2019-08-22 stsp exit(1);
9405 01073a5d 2019-08-22 stsp }
9406 01073a5d 2019-08-22 stsp
9407 01073a5d 2019-08-22 stsp static const struct got_error *
9408 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9409 01073a5d 2019-08-22 stsp {
9410 01073a5d 2019-08-22 stsp const struct got_error *err;
9411 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9412 01073a5d 2019-08-22 stsp
9413 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9414 01073a5d 2019-08-22 stsp if (err)
9415 01073a5d 2019-08-22 stsp return err;
9416 01073a5d 2019-08-22 stsp
9417 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9418 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9419 01073a5d 2019-08-22 stsp return err;
9420 01073a5d 2019-08-22 stsp }
9421 01073a5d 2019-08-22 stsp
9422 01073a5d 2019-08-22 stsp static const struct got_error *
9423 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9424 01073a5d 2019-08-22 stsp {
9425 01073a5d 2019-08-22 stsp const struct got_error *err;
9426 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9427 56e0773d 2019-11-28 stsp int nentries, i;
9428 01073a5d 2019-08-22 stsp
9429 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9430 01073a5d 2019-08-22 stsp if (err)
9431 01073a5d 2019-08-22 stsp return err;
9432 01073a5d 2019-08-22 stsp
9433 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9434 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9435 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9436 01073a5d 2019-08-22 stsp char *id_str;
9437 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9438 01073a5d 2019-08-22 stsp break;
9439 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9440 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9441 01073a5d 2019-08-22 stsp if (err)
9442 01073a5d 2019-08-22 stsp break;
9443 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9444 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9445 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9446 01073a5d 2019-08-22 stsp free(id_str);
9447 01073a5d 2019-08-22 stsp }
9448 01073a5d 2019-08-22 stsp
9449 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9450 01073a5d 2019-08-22 stsp return err;
9451 01073a5d 2019-08-22 stsp }
9452 01073a5d 2019-08-22 stsp
9453 01073a5d 2019-08-22 stsp static const struct got_error *
9454 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9455 01073a5d 2019-08-22 stsp {
9456 01073a5d 2019-08-22 stsp const struct got_error *err;
9457 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9458 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9459 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9460 01073a5d 2019-08-22 stsp char *id_str = NULL;
9461 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9462 01073a5d 2019-08-22 stsp
9463 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9464 01073a5d 2019-08-22 stsp if (err)
9465 01073a5d 2019-08-22 stsp return err;
9466 01073a5d 2019-08-22 stsp
9467 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9468 01073a5d 2019-08-22 stsp if (err)
9469 01073a5d 2019-08-22 stsp goto done;
9470 01073a5d 2019-08-22 stsp
9471 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9472 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9473 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9474 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9475 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9476 01073a5d 2019-08-22 stsp char *pid_str;
9477 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9478 01073a5d 2019-08-22 stsp if (err)
9479 01073a5d 2019-08-22 stsp goto done;
9480 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9481 01073a5d 2019-08-22 stsp free(pid_str);
9482 01073a5d 2019-08-22 stsp }
9483 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9484 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9485 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
9486 01073a5d 2019-08-22 stsp
9487 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9488 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9489 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
9490 01073a5d 2019-08-22 stsp
9491 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9492 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9493 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9494 01073a5d 2019-08-22 stsp done:
9495 01073a5d 2019-08-22 stsp free(id_str);
9496 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9497 01073a5d 2019-08-22 stsp return err;
9498 01073a5d 2019-08-22 stsp }
9499 01073a5d 2019-08-22 stsp
9500 01073a5d 2019-08-22 stsp static const struct got_error *
9501 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9502 01073a5d 2019-08-22 stsp {
9503 01073a5d 2019-08-22 stsp const struct got_error *err;
9504 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9505 01073a5d 2019-08-22 stsp char *id_str = NULL;
9506 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9507 01073a5d 2019-08-22 stsp
9508 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9509 01073a5d 2019-08-22 stsp if (err)
9510 01073a5d 2019-08-22 stsp return err;
9511 01073a5d 2019-08-22 stsp
9512 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9513 01073a5d 2019-08-22 stsp if (err)
9514 01073a5d 2019-08-22 stsp goto done;
9515 01073a5d 2019-08-22 stsp
9516 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9517 e15d5241 2019-08-22 stsp
9518 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9519 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9520 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9521 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9522 01073a5d 2019-08-22 stsp break;
9523 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9524 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9525 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9526 01073a5d 2019-08-22 stsp break;
9527 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9528 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9529 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9530 01073a5d 2019-08-22 stsp break;
9531 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9532 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9533 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9534 01073a5d 2019-08-22 stsp break;
9535 01073a5d 2019-08-22 stsp default:
9536 01073a5d 2019-08-22 stsp break;
9537 01073a5d 2019-08-22 stsp }
9538 01073a5d 2019-08-22 stsp
9539 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9540 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9541 e15d5241 2019-08-22 stsp
9542 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9543 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9544 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
9545 01073a5d 2019-08-22 stsp
9546 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9547 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9548 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9549 01073a5d 2019-08-22 stsp done:
9550 01073a5d 2019-08-22 stsp free(id_str);
9551 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9552 01073a5d 2019-08-22 stsp return err;
9553 01073a5d 2019-08-22 stsp }
9554 01073a5d 2019-08-22 stsp
9555 01073a5d 2019-08-22 stsp static const struct got_error *
9556 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9557 01073a5d 2019-08-22 stsp {
9558 01073a5d 2019-08-22 stsp const struct got_error *error;
9559 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9560 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9561 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9562 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9563 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9564 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9565 01073a5d 2019-08-22 stsp
9566 01073a5d 2019-08-22 stsp #ifndef PROFILE
9567 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9568 01073a5d 2019-08-22 stsp NULL) == -1)
9569 01073a5d 2019-08-22 stsp err(1, "pledge");
9570 01073a5d 2019-08-22 stsp #endif
9571 01073a5d 2019-08-22 stsp
9572 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9573 01073a5d 2019-08-22 stsp switch (ch) {
9574 896e9b6f 2019-08-26 stsp case 'c':
9575 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9576 896e9b6f 2019-08-26 stsp break;
9577 01073a5d 2019-08-22 stsp case 'r':
9578 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9579 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9580 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9581 9ba1d308 2019-10-21 stsp optarg);
9582 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9583 01073a5d 2019-08-22 stsp break;
9584 896e9b6f 2019-08-26 stsp case 'P':
9585 896e9b6f 2019-08-26 stsp force_path = 1;
9586 896e9b6f 2019-08-26 stsp break;
9587 01073a5d 2019-08-22 stsp default:
9588 01073a5d 2019-08-22 stsp usage_cat();
9589 01073a5d 2019-08-22 stsp /* NOTREACHED */
9590 01073a5d 2019-08-22 stsp }
9591 01073a5d 2019-08-22 stsp }
9592 01073a5d 2019-08-22 stsp
9593 01073a5d 2019-08-22 stsp argc -= optind;
9594 01073a5d 2019-08-22 stsp argv += optind;
9595 01073a5d 2019-08-22 stsp
9596 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9597 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9598 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9599 01073a5d 2019-08-22 stsp goto done;
9600 01073a5d 2019-08-22 stsp }
9601 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9602 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9603 01073a5d 2019-08-22 stsp goto done;
9604 01073a5d 2019-08-22 stsp if (worktree) {
9605 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9606 01073a5d 2019-08-22 stsp repo_path = strdup(
9607 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9608 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9609 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9610 01073a5d 2019-08-22 stsp goto done;
9611 01073a5d 2019-08-22 stsp }
9612 01073a5d 2019-08-22 stsp }
9613 01073a5d 2019-08-22 stsp }
9614 01073a5d 2019-08-22 stsp
9615 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9616 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9617 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9618 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9619 01073a5d 2019-08-22 stsp }
9620 01073a5d 2019-08-22 stsp
9621 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9622 01073a5d 2019-08-22 stsp free(repo_path);
9623 01073a5d 2019-08-22 stsp if (error != NULL)
9624 01073a5d 2019-08-22 stsp goto done;
9625 01073a5d 2019-08-22 stsp
9626 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9627 896e9b6f 2019-08-26 stsp if (error)
9628 896e9b6f 2019-08-26 stsp goto done;
9629 896e9b6f 2019-08-26 stsp
9630 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9631 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9632 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9633 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9634 01073a5d 2019-08-22 stsp if (error)
9635 01073a5d 2019-08-22 stsp goto done;
9636 01073a5d 2019-08-22 stsp
9637 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9638 896e9b6f 2019-08-26 stsp if (force_path) {
9639 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9640 896e9b6f 2019-08-26 stsp argv[i]);
9641 896e9b6f 2019-08-26 stsp if (error)
9642 896e9b6f 2019-08-26 stsp break;
9643 896e9b6f 2019-08-26 stsp } else {
9644 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9645 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9646 896e9b6f 2019-08-26 stsp if (error) {
9647 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9648 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9649 896e9b6f 2019-08-26 stsp break;
9650 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9651 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9652 896e9b6f 2019-08-26 stsp if (error)
9653 896e9b6f 2019-08-26 stsp break;
9654 896e9b6f 2019-08-26 stsp }
9655 896e9b6f 2019-08-26 stsp }
9656 01073a5d 2019-08-22 stsp
9657 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9658 01073a5d 2019-08-22 stsp if (error)
9659 01073a5d 2019-08-22 stsp break;
9660 01073a5d 2019-08-22 stsp
9661 01073a5d 2019-08-22 stsp switch (obj_type) {
9662 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9663 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9664 01073a5d 2019-08-22 stsp break;
9665 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9666 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9667 01073a5d 2019-08-22 stsp break;
9668 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9669 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9670 01073a5d 2019-08-22 stsp break;
9671 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9672 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9673 01073a5d 2019-08-22 stsp break;
9674 01073a5d 2019-08-22 stsp default:
9675 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9676 01073a5d 2019-08-22 stsp break;
9677 01073a5d 2019-08-22 stsp }
9678 01073a5d 2019-08-22 stsp if (error)
9679 01073a5d 2019-08-22 stsp break;
9680 01073a5d 2019-08-22 stsp free(label);
9681 01073a5d 2019-08-22 stsp label = NULL;
9682 01073a5d 2019-08-22 stsp free(id);
9683 01073a5d 2019-08-22 stsp id = NULL;
9684 01073a5d 2019-08-22 stsp }
9685 01073a5d 2019-08-22 stsp done:
9686 01073a5d 2019-08-22 stsp free(label);
9687 01073a5d 2019-08-22 stsp free(id);
9688 896e9b6f 2019-08-26 stsp free(commit_id);
9689 01073a5d 2019-08-22 stsp if (worktree)
9690 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9691 01073a5d 2019-08-22 stsp if (repo) {
9692 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9693 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9694 01073a5d 2019-08-22 stsp if (error == NULL)
9695 01073a5d 2019-08-22 stsp error = repo_error;
9696 b2118c49 2020-07-28 stsp }
9697 b2118c49 2020-07-28 stsp return error;
9698 b2118c49 2020-07-28 stsp }
9699 b2118c49 2020-07-28 stsp
9700 b2118c49 2020-07-28 stsp __dead static void
9701 b2118c49 2020-07-28 stsp usage_info(void)
9702 b2118c49 2020-07-28 stsp {
9703 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9704 b2118c49 2020-07-28 stsp getprogname());
9705 b2118c49 2020-07-28 stsp exit(1);
9706 b2118c49 2020-07-28 stsp }
9707 b2118c49 2020-07-28 stsp
9708 b2118c49 2020-07-28 stsp static const struct got_error *
9709 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9710 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9711 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9712 b2118c49 2020-07-28 stsp {
9713 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9714 b2118c49 2020-07-28 stsp char *id_str = NULL;
9715 b2118c49 2020-07-28 stsp char datebuf[128];
9716 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9717 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9718 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9719 b2118c49 2020-07-28 stsp
9720 b2118c49 2020-07-28 stsp /*
9721 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9722 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9723 b2118c49 2020-07-28 stsp */
9724 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9725 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9726 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9727 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9728 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9729 b2118c49 2020-07-28 stsp }
9730 b2118c49 2020-07-28 stsp
9731 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9732 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9733 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9734 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9735 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9736 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9737 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9738 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9739 b2118c49 2020-07-28 stsp else
9740 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9741 b2118c49 2020-07-28 stsp
9742 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9743 b2118c49 2020-07-28 stsp if (tm == NULL)
9744 b2118c49 2020-07-28 stsp return NULL;
9745 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9746 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9747 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9748 b2118c49 2020-07-28 stsp
9749 b2118c49 2020-07-28 stsp if (blob_id) {
9750 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9751 b2118c49 2020-07-28 stsp if (err)
9752 b2118c49 2020-07-28 stsp return err;
9753 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9754 b2118c49 2020-07-28 stsp free(id_str);
9755 b2118c49 2020-07-28 stsp }
9756 b2118c49 2020-07-28 stsp
9757 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9758 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9759 b2118c49 2020-07-28 stsp if (err)
9760 b2118c49 2020-07-28 stsp return err;
9761 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9762 b2118c49 2020-07-28 stsp free(id_str);
9763 b2118c49 2020-07-28 stsp }
9764 b2118c49 2020-07-28 stsp
9765 b2118c49 2020-07-28 stsp if (commit_id) {
9766 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9767 b2118c49 2020-07-28 stsp if (err)
9768 b2118c49 2020-07-28 stsp return err;
9769 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9770 b2118c49 2020-07-28 stsp free(id_str);
9771 b2118c49 2020-07-28 stsp }
9772 b2118c49 2020-07-28 stsp
9773 b2118c49 2020-07-28 stsp return NULL;
9774 b2118c49 2020-07-28 stsp }
9775 b2118c49 2020-07-28 stsp
9776 b2118c49 2020-07-28 stsp static const struct got_error *
9777 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9778 b2118c49 2020-07-28 stsp {
9779 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9780 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9781 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9782 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9783 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9784 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9785 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9786 b2118c49 2020-07-28 stsp
9787 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9788 b2118c49 2020-07-28 stsp
9789 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9790 b2118c49 2020-07-28 stsp switch (ch) {
9791 b2118c49 2020-07-28 stsp default:
9792 b2118c49 2020-07-28 stsp usage_info();
9793 b2118c49 2020-07-28 stsp /* NOTREACHED */
9794 b2118c49 2020-07-28 stsp }
9795 01073a5d 2019-08-22 stsp }
9796 b2118c49 2020-07-28 stsp
9797 b2118c49 2020-07-28 stsp argc -= optind;
9798 b2118c49 2020-07-28 stsp argv += optind;
9799 b2118c49 2020-07-28 stsp
9800 b2118c49 2020-07-28 stsp #ifndef PROFILE
9801 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9802 b2118c49 2020-07-28 stsp NULL) == -1)
9803 b2118c49 2020-07-28 stsp err(1, "pledge");
9804 b2118c49 2020-07-28 stsp #endif
9805 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9806 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9807 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9808 b2118c49 2020-07-28 stsp goto done;
9809 b2118c49 2020-07-28 stsp }
9810 b2118c49 2020-07-28 stsp
9811 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9812 b2118c49 2020-07-28 stsp if (error) {
9813 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9814 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9815 b2118c49 2020-07-28 stsp goto done;
9816 b2118c49 2020-07-28 stsp }
9817 b2118c49 2020-07-28 stsp
9818 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9819 b2118c49 2020-07-28 stsp if (error)
9820 b2118c49 2020-07-28 stsp goto done;
9821 b2118c49 2020-07-28 stsp
9822 b2118c49 2020-07-28 stsp if (argc >= 1) {
9823 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9824 b2118c49 2020-07-28 stsp worktree);
9825 b2118c49 2020-07-28 stsp if (error)
9826 b2118c49 2020-07-28 stsp goto done;
9827 b2118c49 2020-07-28 stsp show_files = 1;
9828 b2118c49 2020-07-28 stsp }
9829 b2118c49 2020-07-28 stsp
9830 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9831 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9832 b2118c49 2020-07-28 stsp if (error)
9833 b2118c49 2020-07-28 stsp goto done;
9834 b2118c49 2020-07-28 stsp
9835 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9836 b2118c49 2020-07-28 stsp if (error)
9837 b2118c49 2020-07-28 stsp goto done;
9838 b2118c49 2020-07-28 stsp
9839 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9840 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9841 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9842 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9843 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9844 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9845 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9846 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9847 b2118c49 2020-07-28 stsp
9848 b2118c49 2020-07-28 stsp if (show_files) {
9849 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9850 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9851 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9852 b2118c49 2020-07-28 stsp continue;
9853 b2118c49 2020-07-28 stsp /*
9854 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9855 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9856 b2118c49 2020-07-28 stsp */
9857 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9858 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9859 b2118c49 2020-07-28 stsp }
9860 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9861 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9862 b2118c49 2020-07-28 stsp if (error)
9863 b2118c49 2020-07-28 stsp goto done;
9864 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9865 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9866 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9867 b2118c49 2020-07-28 stsp break;
9868 b2118c49 2020-07-28 stsp }
9869 b2118c49 2020-07-28 stsp }
9870 b2118c49 2020-07-28 stsp }
9871 b2118c49 2020-07-28 stsp done:
9872 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9873 b2118c49 2020-07-28 stsp free((char *)pe->path);
9874 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9875 b2118c49 2020-07-28 stsp free(cwd);
9876 b2118c49 2020-07-28 stsp free(id_str);
9877 b2118c49 2020-07-28 stsp free(uuidstr);
9878 0ebf8283 2019-07-24 stsp return error;
9879 0ebf8283 2019-07-24 stsp }