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 5c860e29 2018-03-12 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 83cd27f8 2020-01-13 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 1e70621d 2018-03-27 stsp optind = 0;
217 53ccebc2 2019-07-30 stsp
218 53ccebc2 2019-07-30 stsp if (Vflag) {
219 53ccebc2 2019-07-30 stsp got_version_print_str();
220 53ccebc2 2019-07-30 stsp return 1;
221 53ccebc2 2019-07-30 stsp }
222 5c860e29 2018-03-12 stsp
223 5c860e29 2018-03-12 stsp if (argc <= 0)
224 ce5b7c56 2019-07-09 stsp usage(hflag);
225 5c860e29 2018-03-12 stsp
226 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
227 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
228 99437157 2018-11-11 stsp
229 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
230 d7d4f210 2018-03-12 stsp const struct got_error *error;
231 d7d4f210 2018-03-12 stsp
232 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
233 5c860e29 2018-03-12 stsp
234 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
235 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
236 5c860e29 2018-03-12 stsp continue;
237 5c860e29 2018-03-12 stsp
238 1b6b95a8 2018-03-12 stsp if (hflag)
239 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
240 1b6b95a8 2018-03-12 stsp
241 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
242 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
243 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
244 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
245 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
246 70015d7a 2019-11-08 stsp !(sigint_received &&
247 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
248 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
249 d7d4f210 2018-03-12 stsp return 1;
250 d7d4f210 2018-03-12 stsp }
251 d7d4f210 2018-03-12 stsp
252 d7d4f210 2018-03-12 stsp return 0;
253 5c860e29 2018-03-12 stsp }
254 5c860e29 2018-03-12 stsp
255 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
256 ce5b7c56 2019-07-09 stsp list_commands();
257 5c860e29 2018-03-12 stsp return 1;
258 5c860e29 2018-03-12 stsp }
259 5c860e29 2018-03-12 stsp
260 4ed7e80c 2018-05-20 stsp __dead static void
261 ce5b7c56 2019-07-09 stsp usage(int hflag)
262 5c860e29 2018-03-12 stsp {
263 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
264 53ccebc2 2019-07-30 stsp getprogname());
265 ce5b7c56 2019-07-09 stsp if (hflag)
266 ce5b7c56 2019-07-09 stsp list_commands();
267 5c860e29 2018-03-12 stsp exit(1);
268 5c860e29 2018-03-12 stsp }
269 5c860e29 2018-03-12 stsp
270 0266afb7 2019-01-04 stsp static const struct got_error *
271 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
272 e2ba3d07 2019-05-13 stsp {
273 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
274 e2ba3d07 2019-05-13 stsp const char *editor;
275 8920fa04 2019-08-18 stsp
276 8920fa04 2019-08-18 stsp *abspath = NULL;
277 e2ba3d07 2019-05-13 stsp
278 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
279 e2ba3d07 2019-05-13 stsp if (editor == NULL)
280 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
281 e2ba3d07 2019-05-13 stsp
282 0ee7065d 2019-05-13 stsp if (editor) {
283 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
284 0ee7065d 2019-05-13 stsp if (err)
285 0ee7065d 2019-05-13 stsp return err;
286 0ee7065d 2019-05-13 stsp }
287 e2ba3d07 2019-05-13 stsp
288 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
289 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
290 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
291 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
292 0ee7065d 2019-05-13 stsp }
293 0ee7065d 2019-05-13 stsp
294 e2ba3d07 2019-05-13 stsp return NULL;
295 e2ba3d07 2019-05-13 stsp }
296 e2ba3d07 2019-05-13 stsp
297 e2ba3d07 2019-05-13 stsp static const struct got_error *
298 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
299 c530dc23 2019-07-23 stsp const char *worktree_path)
300 0266afb7 2019-01-04 stsp {
301 163ce85a 2019-05-13 stsp const struct got_error *err;
302 0266afb7 2019-01-04 stsp
303 37c06ea4 2019-07-15 stsp #ifdef PROFILE
304 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
305 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
306 37c06ea4 2019-07-15 stsp #endif
307 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
308 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
309 0266afb7 2019-01-04 stsp
310 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
311 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
312 0266afb7 2019-01-04 stsp
313 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
314 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
315 0266afb7 2019-01-04 stsp
316 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
317 163ce85a 2019-05-13 stsp if (err != NULL)
318 163ce85a 2019-05-13 stsp return err;
319 0266afb7 2019-01-04 stsp
320 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
321 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
322 0266afb7 2019-01-04 stsp
323 0266afb7 2019-01-04 stsp return NULL;
324 2c7829a4 2019-06-17 stsp }
325 2c7829a4 2019-06-17 stsp
326 2c7829a4 2019-06-17 stsp __dead static void
327 2c7829a4 2019-06-17 stsp usage_init(void)
328 2c7829a4 2019-06-17 stsp {
329 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
330 2c7829a4 2019-06-17 stsp exit(1);
331 2c7829a4 2019-06-17 stsp }
332 2c7829a4 2019-06-17 stsp
333 2c7829a4 2019-06-17 stsp static const struct got_error *
334 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
335 2c7829a4 2019-06-17 stsp {
336 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
337 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
338 2c7829a4 2019-06-17 stsp int ch;
339 2c7829a4 2019-06-17 stsp
340 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
341 2c7829a4 2019-06-17 stsp switch (ch) {
342 2c7829a4 2019-06-17 stsp default:
343 2c7829a4 2019-06-17 stsp usage_init();
344 2c7829a4 2019-06-17 stsp /* NOTREACHED */
345 2c7829a4 2019-06-17 stsp }
346 2c7829a4 2019-06-17 stsp }
347 2c7829a4 2019-06-17 stsp
348 2c7829a4 2019-06-17 stsp argc -= optind;
349 2c7829a4 2019-06-17 stsp argv += optind;
350 2c7829a4 2019-06-17 stsp
351 2c7829a4 2019-06-17 stsp #ifndef PROFILE
352 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
353 2c7829a4 2019-06-17 stsp err(1, "pledge");
354 2c7829a4 2019-06-17 stsp #endif
355 2c7829a4 2019-06-17 stsp if (argc != 1)
356 bc20e173 2019-06-17 stsp usage_init();
357 2c7829a4 2019-06-17 stsp
358 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
359 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
360 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
361 2c7829a4 2019-06-17 stsp
362 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
363 2c7829a4 2019-06-17 stsp
364 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
365 2c7829a4 2019-06-17 stsp if (error &&
366 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
367 2c7829a4 2019-06-17 stsp goto done;
368 2c7829a4 2019-06-17 stsp
369 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
370 2c7829a4 2019-06-17 stsp if (error)
371 2c7829a4 2019-06-17 stsp goto done;
372 2c7829a4 2019-06-17 stsp
373 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
374 3ce1b845 2019-07-15 stsp done:
375 3ce1b845 2019-07-15 stsp free(repo_path);
376 3ce1b845 2019-07-15 stsp return error;
377 3ce1b845 2019-07-15 stsp }
378 3ce1b845 2019-07-15 stsp
379 3ce1b845 2019-07-15 stsp __dead static void
380 3ce1b845 2019-07-15 stsp usage_import(void)
381 3ce1b845 2019-07-15 stsp {
382 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
383 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
384 3ce1b845 2019-07-15 stsp exit(1);
385 3ce1b845 2019-07-15 stsp }
386 3ce1b845 2019-07-15 stsp
387 3ce1b845 2019-07-15 stsp int
388 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
389 3ce1b845 2019-07-15 stsp {
390 3ce1b845 2019-07-15 stsp pid_t pid;
391 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
392 3ce1b845 2019-07-15 stsp int st = -1;
393 3ce1b845 2019-07-15 stsp
394 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
395 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
396 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
397 3ce1b845 2019-07-15 stsp
398 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
399 3ce1b845 2019-07-15 stsp case -1:
400 3ce1b845 2019-07-15 stsp goto doneediting;
401 3ce1b845 2019-07-15 stsp case 0:
402 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
403 3ce1b845 2019-07-15 stsp _exit(127);
404 3ce1b845 2019-07-15 stsp }
405 3ce1b845 2019-07-15 stsp
406 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
407 3ce1b845 2019-07-15 stsp if (errno != EINTR)
408 3ce1b845 2019-07-15 stsp break;
409 3ce1b845 2019-07-15 stsp
410 3ce1b845 2019-07-15 stsp doneediting:
411 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
412 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
413 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
416 3ce1b845 2019-07-15 stsp errno = EINTR;
417 3ce1b845 2019-07-15 stsp return -1;
418 3ce1b845 2019-07-15 stsp }
419 3ce1b845 2019-07-15 stsp
420 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
421 3ce1b845 2019-07-15 stsp }
422 3ce1b845 2019-07-15 stsp
423 3ce1b845 2019-07-15 stsp static const struct got_error *
424 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
425 3ce1b845 2019-07-15 stsp const char *initial_content)
426 3ce1b845 2019-07-15 stsp {
427 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
428 3ce1b845 2019-07-15 stsp char buf[1024];
429 3ce1b845 2019-07-15 stsp struct stat st, st2;
430 3ce1b845 2019-07-15 stsp FILE *fp;
431 3ce1b845 2019-07-15 stsp int content_changed = 0;
432 3ce1b845 2019-07-15 stsp size_t len;
433 3ce1b845 2019-07-15 stsp
434 3ce1b845 2019-07-15 stsp *logmsg = NULL;
435 3ce1b845 2019-07-15 stsp
436 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
437 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
438 3ce1b845 2019-07-15 stsp
439 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
440 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
441 3ce1b845 2019-07-15 stsp
442 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
446 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
447 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
448 3ce1b845 2019-07-15 stsp
449 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
450 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
451 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
452 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
453 3ce1b845 2019-07-15 stsp len = 0;
454 3ce1b845 2019-07-15 stsp
455 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
456 3ce1b845 2019-07-15 stsp if (fp == NULL) {
457 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
458 3ce1b845 2019-07-15 stsp goto done;
459 3ce1b845 2019-07-15 stsp }
460 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
461 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
462 3ce1b845 2019-07-15 stsp content_changed = 1;
463 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
464 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
465 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
466 3ce1b845 2019-07-15 stsp }
467 3ce1b845 2019-07-15 stsp fclose(fp);
468 3ce1b845 2019-07-15 stsp
469 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
470 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
471 3ce1b845 2019-07-15 stsp len--;
472 3ce1b845 2019-07-15 stsp }
473 3ce1b845 2019-07-15 stsp
474 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
475 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
476 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
477 3ce1b845 2019-07-15 stsp done:
478 3ce1b845 2019-07-15 stsp if (err) {
479 3ce1b845 2019-07-15 stsp free(*logmsg);
480 3ce1b845 2019-07-15 stsp *logmsg = NULL;
481 3ce1b845 2019-07-15 stsp }
482 3ce1b845 2019-07-15 stsp return err;
483 3ce1b845 2019-07-15 stsp }
484 3ce1b845 2019-07-15 stsp
485 3ce1b845 2019-07-15 stsp static const struct got_error *
486 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
487 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
488 3ce1b845 2019-07-15 stsp {
489 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
490 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
491 1601cb9f 2020-09-11 naddy int initial_content_len;
492 97972933 2020-09-11 stsp int fd = -1;
493 3ce1b845 2019-07-15 stsp
494 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
495 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
496 1601cb9f 2020-09-11 naddy branch_name);
497 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
498 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
499 3ce1b845 2019-07-15 stsp
500 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
501 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
502 3ce1b845 2019-07-15 stsp if (err)
503 3ce1b845 2019-07-15 stsp goto done;
504 3ce1b845 2019-07-15 stsp
505 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
506 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
507 97972933 2020-09-11 stsp goto done;
508 97972933 2020-09-11 stsp }
509 3ce1b845 2019-07-15 stsp
510 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
511 3ce1b845 2019-07-15 stsp done:
512 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
513 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
514 3ce1b845 2019-07-15 stsp free(initial_content);
515 59f86c76 2020-09-11 stsp if (err) {
516 59f86c76 2020-09-11 stsp free(*logmsg_path);
517 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
518 59f86c76 2020-09-11 stsp }
519 3ce1b845 2019-07-15 stsp return err;
520 3ce1b845 2019-07-15 stsp }
521 3ce1b845 2019-07-15 stsp
522 3ce1b845 2019-07-15 stsp static const struct got_error *
523 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
524 3ce1b845 2019-07-15 stsp {
525 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
526 3ce1b845 2019-07-15 stsp return NULL;
527 3ce1b845 2019-07-15 stsp }
528 3ce1b845 2019-07-15 stsp
529 3ce1b845 2019-07-15 stsp static const struct got_error *
530 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
531 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
532 84792843 2019-08-09 stsp {
533 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
534 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
535 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
536 84792843 2019-08-09 stsp
537 84792843 2019-08-09 stsp *author = NULL;
538 aba9c984 2019-09-08 stsp
539 50b0790e 2020-09-11 stsp if (worktree)
540 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
541 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
542 50b0790e 2020-09-11 stsp
543 50b0790e 2020-09-11 stsp /*
544 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
545 50b0790e 2020-09-11 stsp * significant to least significant:
546 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
547 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
548 50b0790e 2020-09-11 stsp * 3) repository's git config file
549 50b0790e 2020-09-11 stsp * 4) environment variables
550 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
551 50b0790e 2020-09-11 stsp */
552 50b0790e 2020-09-11 stsp
553 50b0790e 2020-09-11 stsp if (worktree_conf)
554 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
555 50b0790e 2020-09-11 stsp if (got_author == NULL)
556 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
557 84792843 2019-08-09 stsp if (got_author == NULL) {
558 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
559 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
560 c9956ddf 2019-09-08 stsp if (name && email) {
561 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
562 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
563 c9956ddf 2019-09-08 stsp return NULL;
564 c9956ddf 2019-09-08 stsp }
565 257add31 2020-09-09 stsp
566 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
567 257add31 2020-09-09 stsp if (got_author == NULL) {
568 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
569 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
570 257add31 2020-09-09 stsp repo);
571 257add31 2020-09-09 stsp if (name && email) {
572 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
573 257add31 2020-09-09 stsp == -1)
574 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
575 257add31 2020-09-09 stsp return NULL;
576 257add31 2020-09-09 stsp }
577 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
578 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
579 257add31 2020-09-09 stsp }
580 84792843 2019-08-09 stsp }
581 84792843 2019-08-09 stsp
582 aba9c984 2019-09-08 stsp *author = strdup(got_author);
583 aba9c984 2019-09-08 stsp if (*author == NULL)
584 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
585 84792843 2019-08-09 stsp
586 84792843 2019-08-09 stsp /*
587 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
588 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
589 84792843 2019-08-09 stsp */
590 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
591 84792843 2019-08-09 stsp got_author++;
592 aba9c984 2019-09-08 stsp if (*got_author != '<') {
593 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
594 aba9c984 2019-09-08 stsp goto done;
595 aba9c984 2019-09-08 stsp }
596 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
597 84792843 2019-08-09 stsp got_author++;
598 aba9c984 2019-09-08 stsp if (*got_author != '@') {
599 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
600 aba9c984 2019-09-08 stsp goto done;
601 aba9c984 2019-09-08 stsp }
602 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
603 84792843 2019-08-09 stsp got_author++;
604 84792843 2019-08-09 stsp if (*got_author != '>')
605 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
606 aba9c984 2019-09-08 stsp done:
607 aba9c984 2019-09-08 stsp if (err) {
608 aba9c984 2019-09-08 stsp free(*author);
609 aba9c984 2019-09-08 stsp *author = NULL;
610 aba9c984 2019-09-08 stsp }
611 aba9c984 2019-09-08 stsp return err;
612 c9956ddf 2019-09-08 stsp }
613 c9956ddf 2019-09-08 stsp
614 c9956ddf 2019-09-08 stsp static const struct got_error *
615 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
616 c9956ddf 2019-09-08 stsp {
617 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
618 c9956ddf 2019-09-08 stsp
619 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
620 c9956ddf 2019-09-08 stsp if (homedir) {
621 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
622 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
623 c9956ddf 2019-09-08 stsp
624 c9956ddf 2019-09-08 stsp }
625 c9956ddf 2019-09-08 stsp return NULL;
626 84792843 2019-08-09 stsp }
627 84792843 2019-08-09 stsp
628 84792843 2019-08-09 stsp static const struct got_error *
629 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
630 3ce1b845 2019-07-15 stsp {
631 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
632 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
633 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
634 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
635 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
636 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
637 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
638 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
639 3ce1b845 2019-07-15 stsp int ch;
640 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
641 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
642 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
643 3ce1b845 2019-07-15 stsp
644 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
645 3ce1b845 2019-07-15 stsp
646 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
647 3ce1b845 2019-07-15 stsp switch (ch) {
648 3ce1b845 2019-07-15 stsp case 'b':
649 3ce1b845 2019-07-15 stsp branch_name = optarg;
650 3ce1b845 2019-07-15 stsp break;
651 3ce1b845 2019-07-15 stsp case 'm':
652 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
653 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
654 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
655 3ce1b845 2019-07-15 stsp goto done;
656 3ce1b845 2019-07-15 stsp }
657 3ce1b845 2019-07-15 stsp break;
658 3ce1b845 2019-07-15 stsp case 'r':
659 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
660 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
661 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
662 9ba1d308 2019-10-21 stsp optarg);
663 3ce1b845 2019-07-15 stsp goto done;
664 3ce1b845 2019-07-15 stsp }
665 3ce1b845 2019-07-15 stsp break;
666 3ce1b845 2019-07-15 stsp case 'I':
667 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
668 3ce1b845 2019-07-15 stsp break;
669 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
670 3ce1b845 2019-07-15 stsp NULL);
671 3ce1b845 2019-07-15 stsp if (error)
672 3ce1b845 2019-07-15 stsp goto done;
673 3ce1b845 2019-07-15 stsp break;
674 3ce1b845 2019-07-15 stsp default:
675 b2b65d18 2019-08-22 stsp usage_import();
676 3ce1b845 2019-07-15 stsp /* NOTREACHED */
677 3ce1b845 2019-07-15 stsp }
678 3ce1b845 2019-07-15 stsp }
679 3ce1b845 2019-07-15 stsp
680 3ce1b845 2019-07-15 stsp argc -= optind;
681 3ce1b845 2019-07-15 stsp argv += optind;
682 3ce1b845 2019-07-15 stsp
683 3ce1b845 2019-07-15 stsp #ifndef PROFILE
684 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
685 aba9c984 2019-09-08 stsp "unveil",
686 3ce1b845 2019-07-15 stsp NULL) == -1)
687 3ce1b845 2019-07-15 stsp err(1, "pledge");
688 3ce1b845 2019-07-15 stsp #endif
689 3ce1b845 2019-07-15 stsp if (argc != 1)
690 3ce1b845 2019-07-15 stsp usage_import();
691 2c7829a4 2019-06-17 stsp
692 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
693 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
694 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
695 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
696 3ce1b845 2019-07-15 stsp }
697 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
698 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
699 c9956ddf 2019-09-08 stsp if (error)
700 c9956ddf 2019-09-08 stsp goto done;
701 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
702 3ce1b845 2019-07-15 stsp if (error)
703 3ce1b845 2019-07-15 stsp goto done;
704 aba9c984 2019-09-08 stsp
705 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
706 aba9c984 2019-09-08 stsp if (error)
707 aba9c984 2019-09-08 stsp return error;
708 e560b7e0 2019-11-28 stsp
709 e560b7e0 2019-11-28 stsp /*
710 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
711 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
712 e560b7e0 2019-11-28 stsp * an unintended typo.
713 e560b7e0 2019-11-28 stsp */
714 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
715 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
716 3ce1b845 2019-07-15 stsp
717 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
718 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
719 3ce1b845 2019-07-15 stsp goto done;
720 3ce1b845 2019-07-15 stsp }
721 3ce1b845 2019-07-15 stsp
722 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
723 3ce1b845 2019-07-15 stsp if (error) {
724 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
725 3ce1b845 2019-07-15 stsp goto done;
726 3ce1b845 2019-07-15 stsp } else {
727 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
728 3ce1b845 2019-07-15 stsp "import target branch already exists");
729 3ce1b845 2019-07-15 stsp goto done;
730 3ce1b845 2019-07-15 stsp }
731 3ce1b845 2019-07-15 stsp
732 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
733 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
734 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
735 3ce1b845 2019-07-15 stsp goto done;
736 3ce1b845 2019-07-15 stsp }
737 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
738 3ce1b845 2019-07-15 stsp
739 3ce1b845 2019-07-15 stsp /*
740 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
741 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
742 3ce1b845 2019-07-15 stsp */
743 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
744 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
745 3ce1b845 2019-07-15 stsp if (error)
746 3ce1b845 2019-07-15 stsp goto done;
747 8e158b01 2019-09-22 stsp free(logmsg);
748 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
749 ef293bdd 2019-10-21 stsp path_dir, refname);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
752 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
753 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
754 3ce1b845 2019-07-15 stsp goto done;
755 ef293bdd 2019-10-21 stsp }
756 3ce1b845 2019-07-15 stsp }
757 3ce1b845 2019-07-15 stsp
758 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
759 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
760 ef293bdd 2019-10-21 stsp if (logmsg_path)
761 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
762 3ce1b845 2019-07-15 stsp goto done;
763 ef293bdd 2019-10-21 stsp }
764 3ce1b845 2019-07-15 stsp
765 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
766 ef293bdd 2019-10-21 stsp if (error) {
767 ef293bdd 2019-10-21 stsp if (logmsg_path)
768 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
769 ef293bdd 2019-10-21 stsp goto done;
770 ef293bdd 2019-10-21 stsp }
771 ef293bdd 2019-10-21 stsp
772 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
773 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
774 ef293bdd 2019-10-21 stsp if (error) {
775 ef293bdd 2019-10-21 stsp if (logmsg_path)
776 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
777 3ce1b845 2019-07-15 stsp goto done;
778 ef293bdd 2019-10-21 stsp }
779 3ce1b845 2019-07-15 stsp
780 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
781 ef293bdd 2019-10-21 stsp if (error) {
782 ef293bdd 2019-10-21 stsp if (logmsg_path)
783 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
784 3ce1b845 2019-07-15 stsp goto done;
785 ef293bdd 2019-10-21 stsp }
786 3ce1b845 2019-07-15 stsp
787 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
788 ef293bdd 2019-10-21 stsp if (error) {
789 ef293bdd 2019-10-21 stsp if (logmsg_path)
790 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
791 3ce1b845 2019-07-15 stsp goto done;
792 ef293bdd 2019-10-21 stsp }
793 3ce1b845 2019-07-15 stsp
794 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
795 ef293bdd 2019-10-21 stsp if (error) {
796 ef293bdd 2019-10-21 stsp if (logmsg_path)
797 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
798 3ce1b845 2019-07-15 stsp goto done;
799 ef293bdd 2019-10-21 stsp }
800 3ce1b845 2019-07-15 stsp
801 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
802 3ce1b845 2019-07-15 stsp if (error) {
803 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
804 ef293bdd 2019-10-21 stsp if (logmsg_path)
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 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
810 3ce1b845 2019-07-15 stsp branch_ref);
811 ef293bdd 2019-10-21 stsp if (error) {
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 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
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 3ce1b845 2019-07-15 stsp goto done;
822 ef293bdd 2019-10-21 stsp }
823 3ce1b845 2019-07-15 stsp }
824 3ce1b845 2019-07-15 stsp
825 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
826 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
827 2c7829a4 2019-06-17 stsp done:
828 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
829 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
830 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
831 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
832 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
833 8e158b01 2019-09-22 stsp free(logmsg);
834 ef293bdd 2019-10-21 stsp free(logmsg_path);
835 2c7829a4 2019-06-17 stsp free(repo_path);
836 3ce1b845 2019-07-15 stsp free(editor);
837 3ce1b845 2019-07-15 stsp free(refname);
838 3ce1b845 2019-07-15 stsp free(new_commit_id);
839 3ce1b845 2019-07-15 stsp free(id_str);
840 aba9c984 2019-09-08 stsp free(author);
841 c9956ddf 2019-09-08 stsp free(gitconfig_path);
842 3ce1b845 2019-07-15 stsp if (branch_ref)
843 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
844 3ce1b845 2019-07-15 stsp if (head_ref)
845 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
846 2c7829a4 2019-06-17 stsp return error;
847 93658fb9 2020-03-18 stsp }
848 93658fb9 2020-03-18 stsp
849 93658fb9 2020-03-18 stsp __dead static void
850 93658fb9 2020-03-18 stsp usage_clone(void)
851 93658fb9 2020-03-18 stsp {
852 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
853 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
854 93658fb9 2020-03-18 stsp exit(1);
855 93658fb9 2020-03-18 stsp }
856 892ac3b6 2020-03-18 stsp
857 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
858 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
859 892ac3b6 2020-03-18 stsp int last_p_indexed;
860 892ac3b6 2020-03-18 stsp int last_p_resolved;
861 68999b92 2020-03-18 stsp int verbosity;
862 04d9a9ec 2020-09-24 stsp
863 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
864 04d9a9ec 2020-09-24 stsp
865 04d9a9ec 2020-09-24 stsp int create_configs;
866 04d9a9ec 2020-09-24 stsp int configs_created;
867 04d9a9ec 2020-09-24 stsp struct {
868 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
869 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
870 04d9a9ec 2020-09-24 stsp const char *proto;
871 04d9a9ec 2020-09-24 stsp const char *host;
872 04d9a9ec 2020-09-24 stsp const char *port;
873 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
874 04d9a9ec 2020-09-24 stsp const char *git_url;
875 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
876 04d9a9ec 2020-09-24 stsp int mirror_references;
877 04d9a9ec 2020-09-24 stsp } config_info;
878 892ac3b6 2020-03-18 stsp };
879 93658fb9 2020-03-18 stsp
880 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
881 93658fb9 2020-03-18 stsp static const struct got_error *
882 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
883 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
884 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
885 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo);
886 04d9a9ec 2020-09-24 stsp
887 04d9a9ec 2020-09-24 stsp static const struct got_error *
888 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
889 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
890 531c3985 2020-03-18 stsp {
891 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
892 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
893 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
894 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
895 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
896 04d9a9ec 2020-09-24 stsp
897 04d9a9ec 2020-09-24 stsp /*
898 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
899 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
900 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
901 04d9a9ec 2020-09-24 stsp * we have all required information.
902 04d9a9ec 2020-09-24 stsp */
903 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
904 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
905 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
906 04d9a9ec 2020-09-24 stsp a->config_info.host, a->config_info.port,
907 04d9a9ec 2020-09-24 stsp a->config_info.remote_repo_path,
908 04d9a9ec 2020-09-24 stsp a->config_info.git_url,
909 04d9a9ec 2020-09-24 stsp a->config_info.fetch_all_branches,
910 04d9a9ec 2020-09-24 stsp a->config_info.mirror_references,
911 04d9a9ec 2020-09-24 stsp a->config_info.symrefs,
912 04d9a9ec 2020-09-24 stsp a->config_info.wanted_branches, a->repo);
913 04d9a9ec 2020-09-24 stsp if (err)
914 04d9a9ec 2020-09-24 stsp return err;
915 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
916 04d9a9ec 2020-09-24 stsp }
917 b2409d58 2020-03-18 stsp
918 68999b92 2020-03-18 stsp if (a->verbosity < 0)
919 68999b92 2020-03-18 stsp return NULL;
920 68999b92 2020-03-18 stsp
921 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
922 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
923 892ac3b6 2020-03-18 stsp fflush(stdout);
924 12d1281e 2020-03-19 stsp return NULL;
925 b2409d58 2020-03-18 stsp }
926 b2409d58 2020-03-18 stsp
927 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
928 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
929 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
930 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
931 892ac3b6 2020-03-18 stsp print_size = 1;
932 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
933 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
934 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
935 892ac3b6 2020-03-18 stsp }
936 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
937 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
938 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
939 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
940 892ac3b6 2020-03-18 stsp print_indexed = 1;
941 892ac3b6 2020-03-18 stsp print_size = 1;
942 892ac3b6 2020-03-18 stsp }
943 61cc1a7a 2020-03-18 stsp }
944 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
945 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
946 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
947 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
948 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
949 892ac3b6 2020-03-18 stsp print_resolved = 1;
950 892ac3b6 2020-03-18 stsp print_indexed = 1;
951 892ac3b6 2020-03-18 stsp print_size = 1;
952 892ac3b6 2020-03-18 stsp }
953 61cc1a7a 2020-03-18 stsp }
954 3168e5da 2020-09-10 stsp
955 d2cdc636 2020-03-18 stsp }
956 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
957 892ac3b6 2020-03-18 stsp printf("\r");
958 892ac3b6 2020-03-18 stsp if (print_size)
959 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
960 d715f13e 2020-03-19 stsp if (print_indexed)
961 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
962 d715f13e 2020-03-19 stsp if (print_resolved)
963 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
964 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
965 892ac3b6 2020-03-18 stsp fflush(stdout);
966 892ac3b6 2020-03-18 stsp
967 531c3985 2020-03-18 stsp return NULL;
968 531c3985 2020-03-18 stsp }
969 531c3985 2020-03-18 stsp
970 531c3985 2020-03-18 stsp static const struct got_error *
971 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
972 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
973 4ba14133 2020-03-20 stsp {
974 4ba14133 2020-03-20 stsp const struct got_error *err;
975 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
976 4ba14133 2020-03-20 stsp
977 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
978 4ba14133 2020-03-20 stsp if (err)
979 4ba14133 2020-03-20 stsp return err;
980 4ba14133 2020-03-20 stsp
981 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
982 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
983 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
984 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
985 6338a6a1 2020-03-21 stsp }
986 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
987 4ba14133 2020-03-20 stsp return err;
988 4ba14133 2020-03-20 stsp }
989 4ba14133 2020-03-20 stsp
990 4ba14133 2020-03-20 stsp static const struct got_error *
991 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
992 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
993 41b0de12 2020-03-21 stsp {
994 41b0de12 2020-03-21 stsp const struct got_error *err;
995 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
996 41b0de12 2020-03-21 stsp
997 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
998 41b0de12 2020-03-21 stsp const char *refname = pe->path;
999 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1000 41b0de12 2020-03-21 stsp
1001 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1002 41b0de12 2020-03-21 stsp }
1003 41b0de12 2020-03-21 stsp
1004 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1005 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1006 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1007 41b0de12 2020-03-21 stsp char *id_str;
1008 41b0de12 2020-03-21 stsp
1009 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1010 41b0de12 2020-03-21 stsp if (err)
1011 41b0de12 2020-03-21 stsp return err;
1012 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1013 41b0de12 2020-03-21 stsp free(id_str);
1014 41b0de12 2020-03-21 stsp }
1015 41b0de12 2020-03-21 stsp
1016 41b0de12 2020-03-21 stsp return NULL;
1017 6338a6a1 2020-03-21 stsp }
1018 6338a6a1 2020-03-21 stsp
1019 6338a6a1 2020-03-21 stsp static const struct got_error *
1020 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1021 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1022 6338a6a1 2020-03-21 stsp {
1023 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1024 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1025 6338a6a1 2020-03-21 stsp char *id_str;
1026 6338a6a1 2020-03-21 stsp
1027 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1028 6338a6a1 2020-03-21 stsp if (err)
1029 6338a6a1 2020-03-21 stsp return err;
1030 6338a6a1 2020-03-21 stsp
1031 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1032 6338a6a1 2020-03-21 stsp if (err)
1033 6338a6a1 2020-03-21 stsp goto done;
1034 6338a6a1 2020-03-21 stsp
1035 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1036 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1037 6338a6a1 2020-03-21 stsp
1038 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1039 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1040 6338a6a1 2020-03-21 stsp done:
1041 6338a6a1 2020-03-21 stsp free(id_str);
1042 6338a6a1 2020-03-21 stsp return err;
1043 0e4002ca 2020-03-21 stsp }
1044 0e4002ca 2020-03-21 stsp
1045 0e4002ca 2020-03-21 stsp static int
1046 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1047 0e4002ca 2020-03-21 stsp {
1048 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1049 0e4002ca 2020-03-21 stsp return 0;
1050 0e4002ca 2020-03-21 stsp refname += 5;
1051 0e4002ca 2020-03-21 stsp
1052 0e4002ca 2020-03-21 stsp /*
1053 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1054 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1055 0e4002ca 2020-03-21 stsp */
1056 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1057 0e4002ca 2020-03-21 stsp return 0;
1058 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1059 0e4002ca 2020-03-21 stsp return 0;
1060 0e4002ca 2020-03-21 stsp
1061 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1062 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1063 0e4002ca 2020-03-21 stsp
1064 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1065 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1066 0e4002ca 2020-03-21 stsp return 1;
1067 0e4002ca 2020-03-21 stsp
1068 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1069 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1070 41b0de12 2020-03-21 stsp }
1071 41b0de12 2020-03-21 stsp
1072 0e4002ca 2020-03-21 stsp static int
1073 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1074 0e4002ca 2020-03-21 stsp {
1075 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1076 0e4002ca 2020-03-21 stsp
1077 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1078 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1079 0e4002ca 2020-03-21 stsp return 1;
1080 0e4002ca 2020-03-21 stsp }
1081 0e4002ca 2020-03-21 stsp
1082 0e4002ca 2020-03-21 stsp return 0;
1083 0e4002ca 2020-03-21 stsp }
1084 0e4002ca 2020-03-21 stsp
1085 41b0de12 2020-03-21 stsp static const struct got_error *
1086 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1087 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1088 0e4002ca 2020-03-21 stsp {
1089 0e4002ca 2020-03-21 stsp const struct got_error *err;
1090 0e4002ca 2020-03-21 stsp char *remote_refname;
1091 0e4002ca 2020-03-21 stsp
1092 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1093 0e4002ca 2020-03-21 stsp refname += 5;
1094 0e4002ca 2020-03-21 stsp
1095 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1096 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1097 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1098 0e4002ca 2020-03-21 stsp
1099 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1100 0e4002ca 2020-03-21 stsp free(remote_refname);
1101 7c0b7f42 2020-09-24 stsp return err;
1102 7c0b7f42 2020-09-24 stsp }
1103 7c0b7f42 2020-09-24 stsp
1104 7c0b7f42 2020-09-24 stsp static const struct got_error *
1105 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1106 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, int fetch_all_branches, int mirror_references,
1107 7c0b7f42 2020-09-24 stsp struct got_repository *repo)
1108 7c0b7f42 2020-09-24 stsp {
1109 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1110 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1111 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1112 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1113 7c0b7f42 2020-09-24 stsp ssize_t n;
1114 7c0b7f42 2020-09-24 stsp
1115 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1116 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1117 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1118 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1119 7c0b7f42 2020-09-24 stsp goto done;
1120 7c0b7f42 2020-09-24 stsp }
1121 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1122 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1123 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1124 7c0b7f42 2020-09-24 stsp goto done;
1125 7c0b7f42 2020-09-24 stsp }
1126 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1127 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1128 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1129 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1130 7c0b7f42 2020-09-24 stsp "%s%s%s"
1131 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1132 7c0b7f42 2020-09-24 stsp "%s"
1133 7c0b7f42 2020-09-24 stsp "}\n",
1134 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1135 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1136 7c0b7f42 2020-09-24 stsp remote_repo_path,
1137 7c0b7f42 2020-09-24 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1138 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1139 7c0b7f42 2020-09-24 stsp goto done;
1140 7c0b7f42 2020-09-24 stsp }
1141 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1142 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1143 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1144 7c0b7f42 2020-09-24 stsp goto done;
1145 7c0b7f42 2020-09-24 stsp }
1146 7c0b7f42 2020-09-24 stsp
1147 7c0b7f42 2020-09-24 stsp done:
1148 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1149 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1150 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1151 7c0b7f42 2020-09-24 stsp return err;
1152 7c0b7f42 2020-09-24 stsp }
1153 7c0b7f42 2020-09-24 stsp
1154 7c0b7f42 2020-09-24 stsp static const struct got_error *
1155 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1156 7c0b7f42 2020-09-24 stsp int fetch_all_branches, int mirror_references, struct got_repository *repo)
1157 7c0b7f42 2020-09-24 stsp {
1158 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1159 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1160 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1161 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1162 7c0b7f42 2020-09-24 stsp ssize_t n;
1163 7c0b7f42 2020-09-24 stsp
1164 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1165 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1166 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1167 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1168 7c0b7f42 2020-09-24 stsp goto done;
1169 7c0b7f42 2020-09-24 stsp }
1170 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1171 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1172 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1173 7c0b7f42 2020-09-24 stsp goto done;
1174 7c0b7f42 2020-09-24 stsp }
1175 7c0b7f42 2020-09-24 stsp if (mirror_references) {
1176 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1177 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1178 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1179 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/*:refs/*\n"
1180 7c0b7f42 2020-09-24 stsp "\tmirror = true\n",
1181 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1182 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1183 7c0b7f42 2020-09-24 stsp goto done;
1184 7c0b7f42 2020-09-24 stsp }
1185 7c0b7f42 2020-09-24 stsp } else if (fetch_all_branches) {
1186 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1187 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1188 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1189 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1190 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1191 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1192 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1193 7c0b7f42 2020-09-24 stsp goto done;
1194 7c0b7f42 2020-09-24 stsp }
1195 7c0b7f42 2020-09-24 stsp } else {
1196 7c0b7f42 2020-09-24 stsp const char *branchname;
1197 7c0b7f42 2020-09-24 stsp
1198 7c0b7f42 2020-09-24 stsp /*
1199 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1200 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1201 7c0b7f42 2020-09-24 stsp */
1202 04d9a9ec 2020-09-24 stsp if (default_branch) {
1203 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1204 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1205 7c0b7f42 2020-09-24 stsp branchname += 11;
1206 7c0b7f42 2020-09-24 stsp } else
1207 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1208 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1209 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1210 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1211 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1212 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1213 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1214 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1215 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1216 7c0b7f42 2020-09-24 stsp goto done;
1217 7c0b7f42 2020-09-24 stsp }
1218 7c0b7f42 2020-09-24 stsp }
1219 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1220 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1221 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1222 7c0b7f42 2020-09-24 stsp goto done;
1223 7c0b7f42 2020-09-24 stsp }
1224 7c0b7f42 2020-09-24 stsp done:
1225 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1226 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1227 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1228 0e4002ca 2020-03-21 stsp return err;
1229 0e4002ca 2020-03-21 stsp }
1230 0e4002ca 2020-03-21 stsp
1231 0e4002ca 2020-03-21 stsp static const struct got_error *
1232 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1233 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1234 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1235 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo)
1236 04d9a9ec 2020-09-24 stsp {
1237 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1238 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1239 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1240 04d9a9ec 2020-09-24 stsp
1241 04d9a9ec 2020-09-24 stsp /*
1242 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1243 04d9a9ec 2020-09-24 stsp * one of those.
1244 04d9a9ec 2020-09-24 stsp */
1245 04d9a9ec 2020-09-24 stsp if (!TAILQ_EMPTY(wanted_branches)) {
1246 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1247 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1248 04d9a9ec 2020-09-24 stsp } else {
1249 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1250 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1251 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1252 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1253 04d9a9ec 2020-09-24 stsp
1254 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1255 04d9a9ec 2020-09-24 stsp continue;
1256 04d9a9ec 2020-09-24 stsp
1257 04d9a9ec 2020-09-24 stsp default_branch = target;
1258 04d9a9ec 2020-09-24 stsp break;
1259 04d9a9ec 2020-09-24 stsp }
1260 04d9a9ec 2020-09-24 stsp }
1261 04d9a9ec 2020-09-24 stsp
1262 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1263 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1264 04d9a9ec 2020-09-24 stsp fetch_all_branches, mirror_references, repo);
1265 04d9a9ec 2020-09-24 stsp if (err)
1266 04d9a9ec 2020-09-24 stsp return err;
1267 04d9a9ec 2020-09-24 stsp
1268 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1269 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1270 04d9a9ec 2020-09-24 stsp mirror_references, repo);
1271 04d9a9ec 2020-09-24 stsp }
1272 04d9a9ec 2020-09-24 stsp
1273 04d9a9ec 2020-09-24 stsp static const struct got_error *
1274 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1275 93658fb9 2020-03-18 stsp {
1276 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1277 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1278 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1279 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1280 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1281 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1282 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1283 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1284 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1285 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1286 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1287 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1288 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1289 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1290 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1291 93658fb9 2020-03-18 stsp
1292 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1293 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1294 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1295 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1296 d9b4d0c0 2020-03-18 stsp
1297 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1298 93658fb9 2020-03-18 stsp switch (ch) {
1299 659e7fbd 2020-03-20 stsp case 'a':
1300 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1301 4ba14133 2020-03-20 stsp break;
1302 4ba14133 2020-03-20 stsp case 'b':
1303 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1304 4ba14133 2020-03-20 stsp optarg, NULL);
1305 4ba14133 2020-03-20 stsp if (error)
1306 4ba14133 2020-03-20 stsp return error;
1307 659e7fbd 2020-03-20 stsp break;
1308 41b0de12 2020-03-21 stsp case 'l':
1309 41b0de12 2020-03-21 stsp list_refs_only = 1;
1310 41b0de12 2020-03-21 stsp break;
1311 469dd726 2020-03-20 stsp case 'm':
1312 469dd726 2020-03-20 stsp mirror_references = 1;
1313 469dd726 2020-03-20 stsp break;
1314 68999b92 2020-03-18 stsp case 'v':
1315 68999b92 2020-03-18 stsp if (verbosity < 0)
1316 68999b92 2020-03-18 stsp verbosity = 0;
1317 68999b92 2020-03-18 stsp else if (verbosity < 3)
1318 68999b92 2020-03-18 stsp verbosity++;
1319 68999b92 2020-03-18 stsp break;
1320 68999b92 2020-03-18 stsp case 'q':
1321 68999b92 2020-03-18 stsp verbosity = -1;
1322 68999b92 2020-03-18 stsp break;
1323 0e4002ca 2020-03-21 stsp case 'R':
1324 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1325 0e4002ca 2020-03-21 stsp optarg, NULL);
1326 0e4002ca 2020-03-21 stsp if (error)
1327 0e4002ca 2020-03-21 stsp return error;
1328 0e4002ca 2020-03-21 stsp break;
1329 93658fb9 2020-03-18 stsp default:
1330 93658fb9 2020-03-18 stsp usage_clone();
1331 93658fb9 2020-03-18 stsp break;
1332 93658fb9 2020-03-18 stsp }
1333 93658fb9 2020-03-18 stsp }
1334 93658fb9 2020-03-18 stsp argc -= optind;
1335 93658fb9 2020-03-18 stsp argv += optind;
1336 39c64a6a 2020-03-18 stsp
1337 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1338 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1339 41b0de12 2020-03-21 stsp if (list_refs_only) {
1340 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1341 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1342 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1343 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1344 41b0de12 2020-03-21 stsp if (mirror_references)
1345 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1346 41b0de12 2020-03-21 stsp if (verbosity == -1)
1347 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1348 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1349 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1350 41b0de12 2020-03-21 stsp }
1351 4ba14133 2020-03-20 stsp
1352 93658fb9 2020-03-18 stsp uri = argv[0];
1353 9df6f38b 2020-03-18 stsp
1354 9df6f38b 2020-03-18 stsp if (argc == 1)
1355 93658fb9 2020-03-18 stsp dirname = NULL;
1356 9df6f38b 2020-03-18 stsp else if (argc == 2)
1357 93658fb9 2020-03-18 stsp dirname = argv[1];
1358 93658fb9 2020-03-18 stsp else
1359 93658fb9 2020-03-18 stsp usage_clone();
1360 09838ffc 2020-03-18 stsp
1361 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1362 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1363 39c64a6a 2020-03-18 stsp if (error)
1364 09f63084 2020-03-20 stsp goto done;
1365 09f63084 2020-03-20 stsp
1366 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1367 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1368 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1369 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1370 09838ffc 2020-03-18 stsp goto done;
1371 09f63084 2020-03-20 stsp }
1372 09838ffc 2020-03-18 stsp
1373 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1374 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1375 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1376 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1377 39c64a6a 2020-03-18 stsp err(1, "pledge");
1378 b46f3e71 2020-03-18 stsp #endif
1379 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1380 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1381 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1382 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1383 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1384 39c64a6a 2020-03-18 stsp err(1, "pledge");
1385 b46f3e71 2020-03-18 stsp #endif
1386 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1387 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1388 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1389 39c64a6a 2020-03-18 stsp goto done;
1390 39c64a6a 2020-03-18 stsp } else {
1391 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1392 39c64a6a 2020-03-18 stsp goto done;
1393 39c64a6a 2020-03-18 stsp }
1394 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1395 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1396 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1397 bb64b798 2020-03-18 stsp goto done;
1398 bb64b798 2020-03-18 stsp }
1399 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1400 bb64b798 2020-03-18 stsp } else
1401 bb64b798 2020-03-18 stsp repo_path = dirname;
1402 bb64b798 2020-03-18 stsp
1403 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1404 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1405 2751fe64 2020-09-24 stsp if (error &&
1406 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1407 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1408 41b0de12 2020-03-21 stsp goto done;
1409 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1410 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1411 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1412 41b0de12 2020-03-21 stsp goto done;
1413 2751fe64 2020-09-24 stsp }
1414 41b0de12 2020-03-21 stsp }
1415 bb64b798 2020-03-18 stsp
1416 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1417 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1418 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1419 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1420 ee448f5f 2020-03-18 stsp goto done;
1421 ee448f5f 2020-03-18 stsp }
1422 ee448f5f 2020-03-18 stsp }
1423 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1424 ee448f5f 2020-03-18 stsp if (error)
1425 ee448f5f 2020-03-18 stsp goto done;
1426 f79e6490 2020-04-19 stsp
1427 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1428 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1429 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1430 ee448f5f 2020-03-18 stsp
1431 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1432 9c52365f 2020-03-21 stsp server_path, verbosity);
1433 39c64a6a 2020-03-18 stsp if (error)
1434 bb64b798 2020-03-18 stsp goto done;
1435 bb64b798 2020-03-18 stsp
1436 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1437 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1438 2751fe64 2020-09-24 stsp if (error)
1439 2751fe64 2020-09-24 stsp goto done;
1440 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1441 2751fe64 2020-09-24 stsp if (error)
1442 2751fe64 2020-09-24 stsp goto done;
1443 2751fe64 2020-09-24 stsp }
1444 2751fe64 2020-09-24 stsp
1445 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1446 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1447 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1448 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1449 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1450 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1451 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1452 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1453 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1454 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1455 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1456 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1457 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1458 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1459 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1460 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1461 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1462 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1463 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1464 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1465 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1466 39c64a6a 2020-03-18 stsp if (error)
1467 d9b4d0c0 2020-03-18 stsp goto done;
1468 d9b4d0c0 2020-03-18 stsp
1469 41b0de12 2020-03-21 stsp if (list_refs_only) {
1470 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1471 41b0de12 2020-03-21 stsp goto done;
1472 41b0de12 2020-03-21 stsp }
1473 41b0de12 2020-03-21 stsp
1474 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1475 39c64a6a 2020-03-18 stsp if (error)
1476 d9b4d0c0 2020-03-18 stsp goto done;
1477 68999b92 2020-03-18 stsp if (verbosity >= 0)
1478 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1479 d9b4d0c0 2020-03-18 stsp free(id_str);
1480 d9b4d0c0 2020-03-18 stsp
1481 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1482 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1483 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1484 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1485 7ebc0570 2020-03-18 stsp char *remote_refname;
1486 0e4002ca 2020-03-21 stsp
1487 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1488 0e4002ca 2020-03-21 stsp !mirror_references) {
1489 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1490 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1491 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1492 0e4002ca 2020-03-21 stsp if (error)
1493 0e4002ca 2020-03-21 stsp goto done;
1494 0e4002ca 2020-03-21 stsp continue;
1495 0e4002ca 2020-03-21 stsp }
1496 668a20f6 2020-03-18 stsp
1497 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1498 39c64a6a 2020-03-18 stsp if (error)
1499 d9b4d0c0 2020-03-18 stsp goto done;
1500 d9b4d0c0 2020-03-18 stsp
1501 469dd726 2020-03-20 stsp if (mirror_references)
1502 469dd726 2020-03-20 stsp continue;
1503 469dd726 2020-03-20 stsp
1504 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1505 7ebc0570 2020-03-18 stsp continue;
1506 7ebc0570 2020-03-18 stsp
1507 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1508 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1509 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1510 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1511 7ebc0570 2020-03-18 stsp goto done;
1512 7ebc0570 2020-03-18 stsp }
1513 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1514 f298ae0f 2020-03-25 stsp free(remote_refname);
1515 39c64a6a 2020-03-18 stsp if (error)
1516 d9b4d0c0 2020-03-18 stsp goto done;
1517 d9b4d0c0 2020-03-18 stsp }
1518 d9b4d0c0 2020-03-18 stsp
1519 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1520 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1521 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1522 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1523 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1524 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1525 d9b4d0c0 2020-03-18 stsp
1526 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1527 d9b4d0c0 2020-03-18 stsp continue;
1528 d9b4d0c0 2020-03-18 stsp
1529 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1530 39c64a6a 2020-03-18 stsp if (error) {
1531 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1532 55330abe 2020-03-20 stsp error = NULL;
1533 d9b4d0c0 2020-03-18 stsp continue;
1534 55330abe 2020-03-20 stsp }
1535 d9b4d0c0 2020-03-18 stsp goto done;
1536 d9b4d0c0 2020-03-18 stsp }
1537 d9b4d0c0 2020-03-18 stsp
1538 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1539 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1540 f298ae0f 2020-03-25 stsp if (error)
1541 f298ae0f 2020-03-25 stsp goto done;
1542 f298ae0f 2020-03-25 stsp
1543 f298ae0f 2020-03-25 stsp if (mirror_references)
1544 f298ae0f 2020-03-25 stsp continue;
1545 f298ae0f 2020-03-25 stsp
1546 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1547 f298ae0f 2020-03-25 stsp continue;
1548 f298ae0f 2020-03-25 stsp
1549 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1550 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1551 f298ae0f 2020-03-25 stsp refname) == -1) {
1552 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1553 f298ae0f 2020-03-25 stsp goto done;
1554 f298ae0f 2020-03-25 stsp }
1555 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1556 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1557 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1558 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1559 f298ae0f 2020-03-25 stsp free(remote_refname);
1560 f298ae0f 2020-03-25 stsp goto done;
1561 f298ae0f 2020-03-25 stsp }
1562 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1563 f298ae0f 2020-03-25 stsp if (error) {
1564 f298ae0f 2020-03-25 stsp free(remote_refname);
1565 f298ae0f 2020-03-25 stsp free(remote_target);
1566 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1567 f298ae0f 2020-03-25 stsp error = NULL;
1568 f298ae0f 2020-03-25 stsp continue;
1569 f298ae0f 2020-03-25 stsp }
1570 f298ae0f 2020-03-25 stsp goto done;
1571 f298ae0f 2020-03-25 stsp }
1572 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1573 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1574 f298ae0f 2020-03-25 stsp free(remote_refname);
1575 f298ae0f 2020-03-25 stsp free(remote_target);
1576 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1577 39c64a6a 2020-03-18 stsp if (error)
1578 d9b4d0c0 2020-03-18 stsp goto done;
1579 4ba14133 2020-03-20 stsp }
1580 4ba14133 2020-03-20 stsp if (pe == NULL) {
1581 4ba14133 2020-03-20 stsp /*
1582 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1583 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1584 4ba14133 2020-03-20 stsp * which could be fetched instead.
1585 4ba14133 2020-03-20 stsp */
1586 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1587 4ba14133 2020-03-20 stsp const char *target = pe->path;
1588 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1589 d9b4d0c0 2020-03-18 stsp
1590 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1591 4ba14133 2020-03-20 stsp if (error) {
1592 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1593 4ba14133 2020-03-20 stsp error = NULL;
1594 4ba14133 2020-03-20 stsp continue;
1595 4ba14133 2020-03-20 stsp }
1596 4ba14133 2020-03-20 stsp goto done;
1597 4ba14133 2020-03-20 stsp }
1598 4ba14133 2020-03-20 stsp
1599 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1600 04d9a9ec 2020-09-24 stsp verbosity, repo);
1601 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1602 4ba14133 2020-03-20 stsp if (error)
1603 4ba14133 2020-03-20 stsp goto done;
1604 4ba14133 2020-03-20 stsp break;
1605 4ba14133 2020-03-20 stsp }
1606 659e7fbd 2020-03-20 stsp }
1607 659e7fbd 2020-03-20 stsp
1608 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1609 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1610 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1611 09838ffc 2020-03-18 stsp done:
1612 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1613 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1614 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1615 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1616 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1617 9c52365f 2020-03-21 stsp }
1618 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1619 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1620 bb64b798 2020-03-18 stsp if (repo)
1621 bb64b798 2020-03-18 stsp got_repo_close(repo);
1622 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1623 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1624 d9b4d0c0 2020-03-18 stsp free(pe->data);
1625 d9b4d0c0 2020-03-18 stsp }
1626 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1627 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1628 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1629 d9b4d0c0 2020-03-18 stsp free(pe->data);
1630 d9b4d0c0 2020-03-18 stsp }
1631 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1632 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1633 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1634 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1635 09838ffc 2020-03-18 stsp free(proto);
1636 09838ffc 2020-03-18 stsp free(host);
1637 09838ffc 2020-03-18 stsp free(port);
1638 09838ffc 2020-03-18 stsp free(server_path);
1639 09838ffc 2020-03-18 stsp free(repo_name);
1640 bb64b798 2020-03-18 stsp free(default_destdir);
1641 b46f3e71 2020-03-18 stsp free(git_url);
1642 39c64a6a 2020-03-18 stsp return error;
1643 7848a0e1 2020-03-19 stsp }
1644 7848a0e1 2020-03-19 stsp
1645 7848a0e1 2020-03-19 stsp static const struct got_error *
1646 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1647 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1648 7848a0e1 2020-03-19 stsp {
1649 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1650 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1651 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1652 7848a0e1 2020-03-19 stsp
1653 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1654 7848a0e1 2020-03-19 stsp if (err)
1655 7848a0e1 2020-03-19 stsp goto done;
1656 7848a0e1 2020-03-19 stsp
1657 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1658 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1659 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1660 88609724 2020-03-21 stsp if (err)
1661 88609724 2020-03-21 stsp goto done;
1662 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1663 88609724 2020-03-21 stsp goto done;
1664 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1665 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1666 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1667 db6d8ad8 2020-03-21 stsp }
1668 db6d8ad8 2020-03-21 stsp goto done;
1669 db6d8ad8 2020-03-21 stsp }
1670 db6d8ad8 2020-03-21 stsp
1671 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1672 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1673 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1674 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1675 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1676 688f11b3 2020-03-21 stsp }
1677 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1678 7848a0e1 2020-03-19 stsp if (err)
1679 7848a0e1 2020-03-19 stsp goto done;
1680 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1681 e8a967e0 2020-03-21 stsp if (err)
1682 e8a967e0 2020-03-21 stsp goto done;
1683 7848a0e1 2020-03-19 stsp } else {
1684 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1685 7848a0e1 2020-03-19 stsp if (err)
1686 7848a0e1 2020-03-19 stsp goto done;
1687 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1688 6338a6a1 2020-03-21 stsp goto done;
1689 6338a6a1 2020-03-21 stsp
1690 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1691 6338a6a1 2020-03-21 stsp if (err)
1692 6338a6a1 2020-03-21 stsp goto done;
1693 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1694 6338a6a1 2020-03-21 stsp if (err)
1695 6338a6a1 2020-03-21 stsp goto done;
1696 7848a0e1 2020-03-19 stsp }
1697 6338a6a1 2020-03-21 stsp
1698 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1699 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1700 6338a6a1 2020-03-21 stsp new_id_str);
1701 7848a0e1 2020-03-19 stsp done:
1702 7848a0e1 2020-03-19 stsp free(old_id);
1703 7848a0e1 2020-03-19 stsp free(new_id_str);
1704 7848a0e1 2020-03-19 stsp return err;
1705 2ab43947 2020-03-18 stsp }
1706 f1bcca34 2020-03-25 stsp
1707 f1bcca34 2020-03-25 stsp static const struct got_error *
1708 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1709 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1710 f1bcca34 2020-03-25 stsp {
1711 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1712 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1713 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1714 f1bcca34 2020-03-25 stsp
1715 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1716 bcf34b0e 2020-03-26 stsp if (err) {
1717 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1718 bcf34b0e 2020-03-26 stsp return err;
1719 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1720 bcf34b0e 2020-03-26 stsp if (err)
1721 bcf34b0e 2020-03-26 stsp goto done;
1722 2ab43947 2020-03-18 stsp
1723 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1724 bcf34b0e 2020-03-26 stsp if (err)
1725 bcf34b0e 2020-03-26 stsp goto done;
1726 f1bcca34 2020-03-25 stsp
1727 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1728 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1729 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1730 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1731 bcf34b0e 2020-03-26 stsp } else {
1732 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1733 f1bcca34 2020-03-25 stsp
1734 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1735 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1736 bcf34b0e 2020-03-26 stsp goto done;
1737 bcf34b0e 2020-03-26 stsp
1738 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1739 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1740 bcf34b0e 2020-03-26 stsp if (err)
1741 bcf34b0e 2020-03-26 stsp goto done;
1742 bcf34b0e 2020-03-26 stsp
1743 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1744 bcf34b0e 2020-03-26 stsp if (err)
1745 bcf34b0e 2020-03-26 stsp goto done;
1746 bcf34b0e 2020-03-26 stsp
1747 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1748 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1749 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1750 bcf34b0e 2020-03-26 stsp
1751 bcf34b0e 2020-03-26 stsp }
1752 f1bcca34 2020-03-25 stsp done:
1753 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1754 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1755 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1756 bcf34b0e 2020-03-26 stsp err = unlock_err;
1757 bcf34b0e 2020-03-26 stsp }
1758 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1759 f1bcca34 2020-03-25 stsp return err;
1760 f1bcca34 2020-03-25 stsp }
1761 f1bcca34 2020-03-25 stsp
1762 2ab43947 2020-03-18 stsp __dead static void
1763 7848a0e1 2020-03-19 stsp usage_fetch(void)
1764 7848a0e1 2020-03-19 stsp {
1765 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1766 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1767 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1768 13f12b09 2020-03-21 stsp getprogname());
1769 7848a0e1 2020-03-19 stsp exit(1);
1770 7848a0e1 2020-03-19 stsp }
1771 7848a0e1 2020-03-19 stsp
1772 7848a0e1 2020-03-19 stsp static const struct got_error *
1773 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1774 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1775 f21ec2f0 2020-03-21 stsp {
1776 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1777 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1778 3789fd73 2020-03-26 stsp char *id_str = NULL;
1779 3789fd73 2020-03-26 stsp
1780 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1781 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1782 3789fd73 2020-03-26 stsp if (err)
1783 3789fd73 2020-03-26 stsp return err;
1784 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1785 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1786 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1787 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1788 3789fd73 2020-03-26 stsp }
1789 3789fd73 2020-03-26 stsp } else {
1790 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1791 3789fd73 2020-03-26 stsp if (err)
1792 3789fd73 2020-03-26 stsp return err;
1793 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1794 3789fd73 2020-03-26 stsp if (err)
1795 3789fd73 2020-03-26 stsp goto done;
1796 3168e5da 2020-09-10 stsp
1797 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1798 3789fd73 2020-03-26 stsp if (err)
1799 3789fd73 2020-03-26 stsp goto done;
1800 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1801 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1802 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1803 3789fd73 2020-03-26 stsp }
1804 3789fd73 2020-03-26 stsp }
1805 3789fd73 2020-03-26 stsp done:
1806 3789fd73 2020-03-26 stsp free(id);
1807 3789fd73 2020-03-26 stsp free(id_str);
1808 3789fd73 2020-03-26 stsp return NULL;
1809 3789fd73 2020-03-26 stsp }
1810 3789fd73 2020-03-26 stsp
1811 3789fd73 2020-03-26 stsp static const struct got_error *
1812 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1813 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1814 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1815 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1816 3789fd73 2020-03-26 stsp {
1817 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1818 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1819 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1820 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1821 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1822 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1823 f21ec2f0 2020-03-21 stsp
1824 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1825 f21ec2f0 2020-03-21 stsp
1826 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1827 3789fd73 2020-03-26 stsp == -1)
1828 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1829 3789fd73 2020-03-26 stsp
1830 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1831 f21ec2f0 2020-03-21 stsp if (err)
1832 3789fd73 2020-03-26 stsp goto done;
1833 f21ec2f0 2020-03-21 stsp
1834 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1835 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1836 f21ec2f0 2020-03-21 stsp
1837 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1838 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1839 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1840 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1841 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1842 3789fd73 2020-03-26 stsp continue;
1843 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1844 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1845 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1846 3789fd73 2020-03-26 stsp goto done;
1847 3789fd73 2020-03-26 stsp }
1848 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1849 3789fd73 2020-03-26 stsp continue;
1850 3789fd73 2020-03-26 stsp }
1851 f21ec2f0 2020-03-21 stsp
1852 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1853 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1854 f21ec2f0 2020-03-21 stsp break;
1855 f21ec2f0 2020-03-21 stsp }
1856 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1857 f21ec2f0 2020-03-21 stsp continue;
1858 f21ec2f0 2020-03-21 stsp
1859 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1860 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1861 3789fd73 2020-03-26 stsp break;
1862 3789fd73 2020-03-26 stsp }
1863 3789fd73 2020-03-26 stsp if (pe != NULL)
1864 3789fd73 2020-03-26 stsp continue;
1865 f21ec2f0 2020-03-21 stsp
1866 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1867 f21ec2f0 2020-03-21 stsp if (err)
1868 f21ec2f0 2020-03-21 stsp break;
1869 3789fd73 2020-03-26 stsp
1870 3789fd73 2020-03-26 stsp if (local_refname) {
1871 3789fd73 2020-03-26 stsp struct got_reference *ref;
1872 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1873 3789fd73 2020-03-26 stsp if (err) {
1874 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1875 3789fd73 2020-03-26 stsp break;
1876 3789fd73 2020-03-26 stsp free(local_refname);
1877 3789fd73 2020-03-26 stsp local_refname = NULL;
1878 3789fd73 2020-03-26 stsp continue;
1879 3789fd73 2020-03-26 stsp }
1880 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1881 3789fd73 2020-03-26 stsp if (err)
1882 3789fd73 2020-03-26 stsp break;
1883 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1884 3789fd73 2020-03-26 stsp got_ref_close(ref);
1885 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1886 3789fd73 2020-03-26 stsp err = unlock_err;
1887 3789fd73 2020-03-26 stsp break;
1888 3789fd73 2020-03-26 stsp }
1889 3789fd73 2020-03-26 stsp
1890 3789fd73 2020-03-26 stsp free(local_refname);
1891 3789fd73 2020-03-26 stsp local_refname = NULL;
1892 6338a6a1 2020-03-21 stsp }
1893 f21ec2f0 2020-03-21 stsp }
1894 3789fd73 2020-03-26 stsp done:
1895 3789fd73 2020-03-26 stsp free(remote_namespace);
1896 3789fd73 2020-03-26 stsp free(local_refname);
1897 0e4002ca 2020-03-21 stsp return err;
1898 0e4002ca 2020-03-21 stsp }
1899 0e4002ca 2020-03-21 stsp
1900 0e4002ca 2020-03-21 stsp static const struct got_error *
1901 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1902 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1903 0e4002ca 2020-03-21 stsp {
1904 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1905 0e4002ca 2020-03-21 stsp char *remote_refname;
1906 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1907 0e4002ca 2020-03-21 stsp
1908 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1909 0e4002ca 2020-03-21 stsp refname += 5;
1910 0e4002ca 2020-03-21 stsp
1911 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1912 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1913 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1914 f21ec2f0 2020-03-21 stsp
1915 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1916 0e4002ca 2020-03-21 stsp if (err) {
1917 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1918 0e4002ca 2020-03-21 stsp goto done;
1919 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1920 0e4002ca 2020-03-21 stsp } else {
1921 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1922 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1923 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1924 9f142382 2020-03-21 stsp err = unlock_err;
1925 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1926 0e4002ca 2020-03-21 stsp }
1927 0e4002ca 2020-03-21 stsp done:
1928 0e4002ca 2020-03-21 stsp free(remote_refname);
1929 f21ec2f0 2020-03-21 stsp return err;
1930 f21ec2f0 2020-03-21 stsp }
1931 f21ec2f0 2020-03-21 stsp
1932 f21ec2f0 2020-03-21 stsp static const struct got_error *
1933 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1934 7848a0e1 2020-03-19 stsp {
1935 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1936 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1937 7848a0e1 2020-03-19 stsp const char *remote_name;
1938 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1939 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1940 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
1941 7848a0e1 2020-03-19 stsp int nremotes;
1942 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1943 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1944 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1945 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1946 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1947 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1948 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1949 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1950 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1951 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1952 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1953 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1954 7848a0e1 2020-03-19 stsp
1955 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1956 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1957 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1958 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1959 7848a0e1 2020-03-19 stsp
1960 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1961 7848a0e1 2020-03-19 stsp switch (ch) {
1962 659e7fbd 2020-03-20 stsp case 'a':
1963 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1964 4ba14133 2020-03-20 stsp break;
1965 4ba14133 2020-03-20 stsp case 'b':
1966 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1967 4ba14133 2020-03-20 stsp optarg, NULL);
1968 4ba14133 2020-03-20 stsp if (error)
1969 4ba14133 2020-03-20 stsp return error;
1970 41b0de12 2020-03-21 stsp break;
1971 f21ec2f0 2020-03-21 stsp case 'd':
1972 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1973 f21ec2f0 2020-03-21 stsp break;
1974 41b0de12 2020-03-21 stsp case 'l':
1975 41b0de12 2020-03-21 stsp list_refs_only = 1;
1976 659e7fbd 2020-03-20 stsp break;
1977 7848a0e1 2020-03-19 stsp case 'r':
1978 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1979 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1980 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1981 7848a0e1 2020-03-19 stsp optarg);
1982 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1983 7848a0e1 2020-03-19 stsp break;
1984 db6d8ad8 2020-03-21 stsp case 't':
1985 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1986 db6d8ad8 2020-03-21 stsp break;
1987 7848a0e1 2020-03-19 stsp case 'v':
1988 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1989 7848a0e1 2020-03-19 stsp verbosity = 0;
1990 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1991 7848a0e1 2020-03-19 stsp verbosity++;
1992 7848a0e1 2020-03-19 stsp break;
1993 7848a0e1 2020-03-19 stsp case 'q':
1994 7848a0e1 2020-03-19 stsp verbosity = -1;
1995 7848a0e1 2020-03-19 stsp break;
1996 0e4002ca 2020-03-21 stsp case 'R':
1997 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1998 0e4002ca 2020-03-21 stsp optarg, NULL);
1999 0e4002ca 2020-03-21 stsp if (error)
2000 0e4002ca 2020-03-21 stsp return error;
2001 0e4002ca 2020-03-21 stsp break;
2002 7848a0e1 2020-03-19 stsp default:
2003 7848a0e1 2020-03-19 stsp usage_fetch();
2004 7848a0e1 2020-03-19 stsp break;
2005 7848a0e1 2020-03-19 stsp }
2006 7848a0e1 2020-03-19 stsp }
2007 7848a0e1 2020-03-19 stsp argc -= optind;
2008 7848a0e1 2020-03-19 stsp argv += optind;
2009 7848a0e1 2020-03-19 stsp
2010 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2011 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
2012 41b0de12 2020-03-21 stsp if (list_refs_only) {
2013 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2014 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
2015 41b0de12 2020-03-21 stsp if (fetch_all_branches)
2016 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
2017 f21ec2f0 2020-03-21 stsp if (delete_refs)
2018 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
2019 41b0de12 2020-03-21 stsp if (verbosity == -1)
2020 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
2021 41b0de12 2020-03-21 stsp }
2022 4ba14133 2020-03-20 stsp
2023 7848a0e1 2020-03-19 stsp if (argc == 0)
2024 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2025 7848a0e1 2020-03-19 stsp else if (argc == 1)
2026 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2027 7848a0e1 2020-03-19 stsp else
2028 7848a0e1 2020-03-19 stsp usage_fetch();
2029 7848a0e1 2020-03-19 stsp
2030 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2031 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2032 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2033 7848a0e1 2020-03-19 stsp goto done;
2034 7848a0e1 2020-03-19 stsp }
2035 7848a0e1 2020-03-19 stsp
2036 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2037 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2038 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2039 7848a0e1 2020-03-19 stsp goto done;
2040 7848a0e1 2020-03-19 stsp else
2041 7848a0e1 2020-03-19 stsp error = NULL;
2042 7848a0e1 2020-03-19 stsp if (worktree) {
2043 7848a0e1 2020-03-19 stsp repo_path =
2044 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2045 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2046 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2047 7848a0e1 2020-03-19 stsp if (error)
2048 7848a0e1 2020-03-19 stsp goto done;
2049 7848a0e1 2020-03-19 stsp } else {
2050 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2051 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2052 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2053 7848a0e1 2020-03-19 stsp goto done;
2054 7848a0e1 2020-03-19 stsp }
2055 7848a0e1 2020-03-19 stsp }
2056 7848a0e1 2020-03-19 stsp }
2057 7848a0e1 2020-03-19 stsp
2058 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2059 7848a0e1 2020-03-19 stsp if (error)
2060 7848a0e1 2020-03-19 stsp goto done;
2061 7848a0e1 2020-03-19 stsp
2062 50b0790e 2020-09-11 stsp if (worktree) {
2063 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2064 50b0790e 2020-09-11 stsp if (worktree_conf) {
2065 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2066 50b0790e 2020-09-11 stsp worktree_conf);
2067 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2068 50b0790e 2020-09-11 stsp remote = &remotes[i];
2069 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
2070 50b0790e 2020-09-11 stsp break;
2071 50b0790e 2020-09-11 stsp }
2072 50b0790e 2020-09-11 stsp }
2073 7848a0e1 2020-03-19 stsp }
2074 50b0790e 2020-09-11 stsp if (remote == NULL) {
2075 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2076 50b0790e 2020-09-11 stsp if (repo_conf) {
2077 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2078 50b0790e 2020-09-11 stsp repo_conf);
2079 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2080 50b0790e 2020-09-11 stsp remote = &remotes[i];
2081 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
2082 50b0790e 2020-09-11 stsp break;
2083 50b0790e 2020-09-11 stsp }
2084 50b0790e 2020-09-11 stsp }
2085 50b0790e 2020-09-11 stsp }
2086 50b0790e 2020-09-11 stsp if (remote == NULL) {
2087 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2088 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2089 257add31 2020-09-09 stsp remote = &remotes[i];
2090 257add31 2020-09-09 stsp if (strcmp(remote->name, remote_name) == 0)
2091 257add31 2020-09-09 stsp break;
2092 257add31 2020-09-09 stsp }
2093 7848a0e1 2020-03-19 stsp }
2094 50b0790e 2020-09-11 stsp if (remote == NULL) {
2095 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2096 50b0790e 2020-09-11 stsp goto done;
2097 b8adfa55 2020-09-25 stsp }
2098 b8adfa55 2020-09-25 stsp
2099 b8adfa55 2020-09-25 stsp if (TAILQ_EMPTY(&wanted_branches) && remote->nbranches > 0) {
2100 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2101 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2102 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2103 b8adfa55 2020-09-25 stsp }
2104 b8adfa55 2020-09-25 stsp
2105 50b0790e 2020-09-11 stsp }
2106 7848a0e1 2020-03-19 stsp
2107 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2108 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2109 7848a0e1 2020-03-19 stsp if (error)
2110 7848a0e1 2020-03-19 stsp goto done;
2111 7848a0e1 2020-03-19 stsp
2112 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2113 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2114 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2115 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2116 7848a0e1 2020-03-19 stsp err(1, "pledge");
2117 7848a0e1 2020-03-19 stsp #endif
2118 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2119 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2120 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2121 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2122 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2123 7848a0e1 2020-03-19 stsp err(1, "pledge");
2124 7848a0e1 2020-03-19 stsp #endif
2125 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2126 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2127 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2128 7848a0e1 2020-03-19 stsp goto done;
2129 7848a0e1 2020-03-19 stsp } else {
2130 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2131 7848a0e1 2020-03-19 stsp goto done;
2132 7848a0e1 2020-03-19 stsp }
2133 7848a0e1 2020-03-19 stsp
2134 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2135 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2136 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2137 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2138 7848a0e1 2020-03-19 stsp goto done;
2139 7848a0e1 2020-03-19 stsp }
2140 7848a0e1 2020-03-19 stsp }
2141 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2142 7848a0e1 2020-03-19 stsp if (error)
2143 7848a0e1 2020-03-19 stsp goto done;
2144 f79e6490 2020-04-19 stsp
2145 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2146 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2147 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2148 7848a0e1 2020-03-19 stsp
2149 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2150 9c52365f 2020-03-21 stsp server_path, verbosity);
2151 7848a0e1 2020-03-19 stsp if (error)
2152 7848a0e1 2020-03-19 stsp goto done;
2153 7848a0e1 2020-03-19 stsp
2154 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2155 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2156 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2157 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2158 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2159 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2160 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2161 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2162 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2163 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2164 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2165 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2166 7848a0e1 2020-03-19 stsp if (error)
2167 7848a0e1 2020-03-19 stsp goto done;
2168 7848a0e1 2020-03-19 stsp
2169 41b0de12 2020-03-21 stsp if (list_refs_only) {
2170 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2171 41b0de12 2020-03-21 stsp goto done;
2172 41b0de12 2020-03-21 stsp }
2173 41b0de12 2020-03-21 stsp
2174 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2175 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2176 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2177 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2178 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2179 984065c8 2020-03-19 stsp if (error)
2180 984065c8 2020-03-19 stsp goto done;
2181 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2182 984065c8 2020-03-19 stsp free(id_str);
2183 984065c8 2020-03-19 stsp id_str = NULL;
2184 984065c8 2020-03-19 stsp }
2185 7848a0e1 2020-03-19 stsp
2186 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2187 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2188 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2189 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2190 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2191 7848a0e1 2020-03-19 stsp char *remote_refname;
2192 7848a0e1 2020-03-19 stsp
2193 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2194 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2195 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2196 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2197 0e4002ca 2020-03-21 stsp if (error)
2198 0e4002ca 2020-03-21 stsp goto done;
2199 0e4002ca 2020-03-21 stsp continue;
2200 0e4002ca 2020-03-21 stsp }
2201 0e4002ca 2020-03-21 stsp
2202 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2203 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2204 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2205 7848a0e1 2020-03-19 stsp if (error) {
2206 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2207 7848a0e1 2020-03-19 stsp goto done;
2208 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2209 6338a6a1 2020-03-21 stsp repo);
2210 7848a0e1 2020-03-19 stsp if (error)
2211 7848a0e1 2020-03-19 stsp goto done;
2212 7848a0e1 2020-03-19 stsp } else {
2213 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2214 db6d8ad8 2020-03-21 stsp verbosity, repo);
2215 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2216 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2217 9f142382 2020-03-21 stsp error = unlock_err;
2218 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2219 7848a0e1 2020-03-19 stsp if (error)
2220 7848a0e1 2020-03-19 stsp goto done;
2221 7848a0e1 2020-03-19 stsp }
2222 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2223 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2224 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2225 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2226 7848a0e1 2020-03-19 stsp goto done;
2227 7848a0e1 2020-03-19 stsp }
2228 7848a0e1 2020-03-19 stsp
2229 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2230 7848a0e1 2020-03-19 stsp if (error) {
2231 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2232 7848a0e1 2020-03-19 stsp goto done;
2233 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2234 688f11b3 2020-03-21 stsp verbosity, repo);
2235 7848a0e1 2020-03-19 stsp if (error)
2236 7848a0e1 2020-03-19 stsp goto done;
2237 7848a0e1 2020-03-19 stsp } else {
2238 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2239 db6d8ad8 2020-03-21 stsp verbosity, repo);
2240 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2241 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2242 9f142382 2020-03-21 stsp error = unlock_err;
2243 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2244 7848a0e1 2020-03-19 stsp if (error)
2245 7848a0e1 2020-03-19 stsp goto done;
2246 7848a0e1 2020-03-19 stsp }
2247 2ec30c80 2020-03-20 stsp
2248 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2249 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2250 2ec30c80 2020-03-20 stsp if (error) {
2251 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2252 2ec30c80 2020-03-20 stsp goto done;
2253 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2254 6338a6a1 2020-03-21 stsp repo);
2255 2ec30c80 2020-03-20 stsp if (error)
2256 2ec30c80 2020-03-20 stsp goto done;
2257 9f142382 2020-03-21 stsp } else {
2258 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2259 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2260 9f142382 2020-03-21 stsp error = unlock_err;
2261 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2262 9f142382 2020-03-21 stsp }
2263 7848a0e1 2020-03-19 stsp }
2264 7848a0e1 2020-03-19 stsp }
2265 f1bcca34 2020-03-25 stsp if (delete_refs) {
2266 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2267 3789fd73 2020-03-26 stsp verbosity, repo);
2268 f1bcca34 2020-03-25 stsp if (error)
2269 f1bcca34 2020-03-25 stsp goto done;
2270 f1bcca34 2020-03-25 stsp }
2271 f1bcca34 2020-03-25 stsp
2272 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2273 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2274 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2275 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2276 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2277 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2278 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2279 f1bcca34 2020-03-25 stsp
2280 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2281 f1bcca34 2020-03-25 stsp continue;
2282 f1bcca34 2020-03-25 stsp
2283 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2284 f1bcca34 2020-03-25 stsp continue;
2285 f1bcca34 2020-03-25 stsp
2286 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2287 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2288 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2289 f1bcca34 2020-03-25 stsp goto done;
2290 f1bcca34 2020-03-25 stsp }
2291 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2292 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2293 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2294 f1bcca34 2020-03-25 stsp free(remote_refname);
2295 f1bcca34 2020-03-25 stsp goto done;
2296 f1bcca34 2020-03-25 stsp }
2297 f1bcca34 2020-03-25 stsp
2298 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2299 f1bcca34 2020-03-25 stsp 0);
2300 f1bcca34 2020-03-25 stsp if (error) {
2301 f1bcca34 2020-03-25 stsp free(remote_refname);
2302 f1bcca34 2020-03-25 stsp free(remote_target);
2303 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2304 f1bcca34 2020-03-25 stsp error = NULL;
2305 f1bcca34 2020-03-25 stsp continue;
2306 f1bcca34 2020-03-25 stsp }
2307 f1bcca34 2020-03-25 stsp goto done;
2308 f1bcca34 2020-03-25 stsp }
2309 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2310 f1bcca34 2020-03-25 stsp verbosity, repo);
2311 f1bcca34 2020-03-25 stsp free(remote_refname);
2312 f1bcca34 2020-03-25 stsp free(remote_target);
2313 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2314 f1bcca34 2020-03-25 stsp if (error)
2315 f1bcca34 2020-03-25 stsp goto done;
2316 f1bcca34 2020-03-25 stsp }
2317 f1bcca34 2020-03-25 stsp }
2318 7848a0e1 2020-03-19 stsp done:
2319 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2320 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2321 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2322 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2323 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2324 9c52365f 2020-03-21 stsp }
2325 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2326 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2327 7848a0e1 2020-03-19 stsp if (repo)
2328 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2329 7848a0e1 2020-03-19 stsp if (worktree)
2330 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2331 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2332 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2333 7848a0e1 2020-03-19 stsp free(pe->data);
2334 7848a0e1 2020-03-19 stsp }
2335 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2336 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2337 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2338 7848a0e1 2020-03-19 stsp free(pe->data);
2339 7848a0e1 2020-03-19 stsp }
2340 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2341 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2342 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2343 7848a0e1 2020-03-19 stsp free(id_str);
2344 7848a0e1 2020-03-19 stsp free(cwd);
2345 7848a0e1 2020-03-19 stsp free(repo_path);
2346 7848a0e1 2020-03-19 stsp free(pack_hash);
2347 7848a0e1 2020-03-19 stsp free(proto);
2348 7848a0e1 2020-03-19 stsp free(host);
2349 7848a0e1 2020-03-19 stsp free(port);
2350 7848a0e1 2020-03-19 stsp free(server_path);
2351 7848a0e1 2020-03-19 stsp free(repo_name);
2352 7848a0e1 2020-03-19 stsp return error;
2353 7848a0e1 2020-03-19 stsp }
2354 7848a0e1 2020-03-19 stsp
2355 7848a0e1 2020-03-19 stsp
2356 7848a0e1 2020-03-19 stsp __dead static void
2357 2ab43947 2020-03-18 stsp usage_checkout(void)
2358 2ab43947 2020-03-18 stsp {
2359 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2360 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2361 2ab43947 2020-03-18 stsp exit(1);
2362 2ab43947 2020-03-18 stsp }
2363 2ab43947 2020-03-18 stsp
2364 2ab43947 2020-03-18 stsp static void
2365 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2366 2ab43947 2020-03-18 stsp {
2367 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2368 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2369 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2370 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2371 2ab43947 2020-03-18 stsp getprogname());
2372 2ab43947 2020-03-18 stsp }
2373 2ab43947 2020-03-18 stsp
2374 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2375 2ab43947 2020-03-18 stsp const char *worktree_path;
2376 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2377 2ab43947 2020-03-18 stsp };
2378 2ab43947 2020-03-18 stsp
2379 2ab43947 2020-03-18 stsp static const struct got_error *
2380 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2381 2ab43947 2020-03-18 stsp {
2382 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2383 2ab43947 2020-03-18 stsp
2384 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2385 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2386 2ab43947 2020-03-18 stsp return NULL;
2387 2ab43947 2020-03-18 stsp
2388 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2389 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2390 2ab43947 2020-03-18 stsp return NULL;
2391 2ab43947 2020-03-18 stsp }
2392 2ab43947 2020-03-18 stsp
2393 2ab43947 2020-03-18 stsp while (path[0] == '/')
2394 2ab43947 2020-03-18 stsp path++;
2395 2ab43947 2020-03-18 stsp
2396 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2397 2ab43947 2020-03-18 stsp return NULL;
2398 2ab43947 2020-03-18 stsp }
2399 2ab43947 2020-03-18 stsp
2400 2ab43947 2020-03-18 stsp static const struct got_error *
2401 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2402 2ab43947 2020-03-18 stsp {
2403 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2404 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2405 2ab43947 2020-03-18 stsp return NULL;
2406 2ab43947 2020-03-18 stsp }
2407 2ab43947 2020-03-18 stsp
2408 2ab43947 2020-03-18 stsp static const struct got_error *
2409 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2410 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2411 2ab43947 2020-03-18 stsp struct got_repository *repo)
2412 2ab43947 2020-03-18 stsp {
2413 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2414 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2415 2ab43947 2020-03-18 stsp
2416 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2417 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2418 2ab43947 2020-03-18 stsp if (err)
2419 2ab43947 2020-03-18 stsp return err;
2420 2ab43947 2020-03-18 stsp
2421 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2422 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2423 2ab43947 2020-03-18 stsp
2424 2ab43947 2020-03-18 stsp /*
2425 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2426 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2427 2ab43947 2020-03-18 stsp *
2428 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2429 2ab43947 2020-03-18 stsp *
2430 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2431 2ab43947 2020-03-18 stsp * \ /
2432 2ab43947 2020-03-18 stsp * C E
2433 2ab43947 2020-03-18 stsp * \ /
2434 2ab43947 2020-03-18 stsp * B (yca)
2435 2ab43947 2020-03-18 stsp * |
2436 2ab43947 2020-03-18 stsp * A
2437 2ab43947 2020-03-18 stsp *
2438 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2439 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2440 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2441 2ab43947 2020-03-18 stsp */
2442 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2443 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2444 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2445 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2446 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2447 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2448 2ab43947 2020-03-18 stsp
2449 2ab43947 2020-03-18 stsp free(yca_id);
2450 2ab43947 2020-03-18 stsp return NULL;
2451 2ab43947 2020-03-18 stsp }
2452 2ab43947 2020-03-18 stsp
2453 2ab43947 2020-03-18 stsp static const struct got_error *
2454 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2455 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2456 2ab43947 2020-03-18 stsp struct got_repository *repo)
2457 2ab43947 2020-03-18 stsp {
2458 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2459 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2460 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2461 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2462 2ab43947 2020-03-18 stsp
2463 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2464 2ab43947 2020-03-18 stsp if (err)
2465 2ab43947 2020-03-18 stsp goto done;
2466 2ab43947 2020-03-18 stsp
2467 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2468 2ab43947 2020-03-18 stsp is_same_branch = 1;
2469 2ab43947 2020-03-18 stsp goto done;
2470 2ab43947 2020-03-18 stsp }
2471 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2472 2ab43947 2020-03-18 stsp is_same_branch = 1;
2473 2ab43947 2020-03-18 stsp goto done;
2474 2ab43947 2020-03-18 stsp }
2475 2ab43947 2020-03-18 stsp
2476 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2477 2ab43947 2020-03-18 stsp if (err)
2478 2ab43947 2020-03-18 stsp goto done;
2479 2ab43947 2020-03-18 stsp
2480 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2481 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2482 2ab43947 2020-03-18 stsp if (err)
2483 2ab43947 2020-03-18 stsp goto done;
2484 2ab43947 2020-03-18 stsp
2485 2ab43947 2020-03-18 stsp for (;;) {
2486 2ab43947 2020-03-18 stsp struct got_object_id *id;
2487 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2488 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2489 2ab43947 2020-03-18 stsp if (err) {
2490 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2491 2ab43947 2020-03-18 stsp err = NULL;
2492 2ab43947 2020-03-18 stsp break;
2493 2ab43947 2020-03-18 stsp }
2494 2ab43947 2020-03-18 stsp
2495 2ab43947 2020-03-18 stsp if (id) {
2496 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2497 2ab43947 2020-03-18 stsp break;
2498 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2499 2ab43947 2020-03-18 stsp is_same_branch = 1;
2500 2ab43947 2020-03-18 stsp break;
2501 2ab43947 2020-03-18 stsp }
2502 2ab43947 2020-03-18 stsp }
2503 2ab43947 2020-03-18 stsp }
2504 2ab43947 2020-03-18 stsp done:
2505 2ab43947 2020-03-18 stsp if (graph)
2506 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2507 2ab43947 2020-03-18 stsp free(head_commit_id);
2508 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2509 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2510 2ab43947 2020-03-18 stsp return err;
2511 a367ff0f 2019-05-14 stsp }
2512 8069f636 2019-01-12 stsp
2513 4ed7e80c 2018-05-20 stsp static const struct got_error *
2514 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2515 4b6c9460 2020-03-05 stsp {
2516 4b6c9460 2020-03-05 stsp static char msg[512];
2517 4b6c9460 2020-03-05 stsp const char *branch_name;
2518 4b6c9460 2020-03-05 stsp
2519 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2520 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2521 4b6c9460 2020-03-05 stsp else
2522 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2523 4b6c9460 2020-03-05 stsp
2524 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2525 4b6c9460 2020-03-05 stsp branch_name += 11;
2526 4b6c9460 2020-03-05 stsp
2527 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2528 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2529 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2530 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2531 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2532 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2533 4b6c9460 2020-03-05 stsp
2534 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2535 4b6c9460 2020-03-05 stsp }
2536 4b6c9460 2020-03-05 stsp
2537 4b6c9460 2020-03-05 stsp static const struct got_error *
2538 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2539 c09a553d 2018-03-12 stsp {
2540 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2541 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2542 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2543 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2544 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2545 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2546 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2547 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2548 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2549 c47340da 2020-09-20 stsp char *cwd = NULL, *path = NULL;
2550 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2551 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2552 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2553 f2ea84fa 2019-07-27 stsp
2554 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2555 c09a553d 2018-03-12 stsp
2556 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2557 0bb8a95e 2018-03-12 stsp switch (ch) {
2558 08573d5b 2019-05-14 stsp case 'b':
2559 08573d5b 2019-05-14 stsp branch_name = optarg;
2560 08573d5b 2019-05-14 stsp break;
2561 8069f636 2019-01-12 stsp case 'c':
2562 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2563 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2564 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2565 bb51a5b4 2020-01-13 stsp break;
2566 bb51a5b4 2020-01-13 stsp case 'E':
2567 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2568 8069f636 2019-01-12 stsp break;
2569 0bb8a95e 2018-03-12 stsp case 'p':
2570 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2571 0bb8a95e 2018-03-12 stsp break;
2572 0bb8a95e 2018-03-12 stsp default:
2573 2deda0b9 2019-03-07 stsp usage_checkout();
2574 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2575 0bb8a95e 2018-03-12 stsp }
2576 0bb8a95e 2018-03-12 stsp }
2577 0bb8a95e 2018-03-12 stsp
2578 0bb8a95e 2018-03-12 stsp argc -= optind;
2579 0bb8a95e 2018-03-12 stsp argv += optind;
2580 0bb8a95e 2018-03-12 stsp
2581 6715a751 2018-03-16 stsp #ifndef PROFILE
2582 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2583 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2584 c09a553d 2018-03-12 stsp err(1, "pledge");
2585 6715a751 2018-03-16 stsp #endif
2586 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2587 c47340da 2020-09-20 stsp char *base, *dotgit;
2588 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2589 76089277 2018-04-01 stsp if (repo_path == NULL)
2590 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2591 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2592 76089277 2018-04-01 stsp if (cwd == NULL) {
2593 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2594 76089277 2018-04-01 stsp goto done;
2595 76089277 2018-04-01 stsp }
2596 c47340da 2020-09-20 stsp if (path_prefix[0])
2597 c47340da 2020-09-20 stsp path = strdup(path_prefix);
2598 c47340da 2020-09-20 stsp else
2599 c47340da 2020-09-20 stsp path = strdup(repo_path);
2600 c47340da 2020-09-20 stsp if (path == NULL) {
2601 c47340da 2020-09-20 stsp error = got_error_from_errno("strdup");
2602 c47340da 2020-09-20 stsp goto done;
2603 c47340da 2020-09-20 stsp }
2604 c47340da 2020-09-20 stsp base = basename(path);
2605 c47340da 2020-09-20 stsp if (base == NULL) {
2606 c47340da 2020-09-20 stsp error = got_error_from_errno2("basename", path);
2607 c47340da 2020-09-20 stsp goto done;
2608 76089277 2018-04-01 stsp }
2609 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2610 c09a553d 2018-03-12 stsp if (dotgit)
2611 c09a553d 2018-03-12 stsp *dotgit = '\0';
2612 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2613 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2614 76089277 2018-04-01 stsp goto done;
2615 c09a553d 2018-03-12 stsp }
2616 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2617 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2618 76089277 2018-04-01 stsp if (repo_path == NULL) {
2619 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2620 76089277 2018-04-01 stsp goto done;
2621 76089277 2018-04-01 stsp }
2622 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2623 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2624 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2625 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2626 b4b3a7dd 2019-07-22 stsp argv[1]);
2627 b4b3a7dd 2019-07-22 stsp goto done;
2628 b4b3a7dd 2019-07-22 stsp }
2629 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2630 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2631 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2632 b4b3a7dd 2019-07-22 stsp goto done;
2633 b4b3a7dd 2019-07-22 stsp }
2634 76089277 2018-04-01 stsp }
2635 c09a553d 2018-03-12 stsp } else
2636 c09a553d 2018-03-12 stsp usage_checkout();
2637 c09a553d 2018-03-12 stsp
2638 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2639 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2640 13bfb272 2019-05-10 jcs
2641 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2642 c09a553d 2018-03-12 stsp if (error != NULL)
2643 c02c541e 2019-03-29 stsp goto done;
2644 c02c541e 2019-03-29 stsp
2645 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2646 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2647 c530dc23 2019-07-23 stsp if (error) {
2648 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2649 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2650 c530dc23 2019-07-23 stsp goto done;
2651 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2652 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2653 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2654 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2655 c530dc23 2019-07-23 stsp goto done;
2656 c530dc23 2019-07-23 stsp }
2657 c530dc23 2019-07-23 stsp }
2658 c530dc23 2019-07-23 stsp
2659 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2660 c02c541e 2019-03-29 stsp if (error)
2661 c09a553d 2018-03-12 stsp goto done;
2662 8069f636 2019-01-12 stsp
2663 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2664 c09a553d 2018-03-12 stsp if (error != NULL)
2665 c09a553d 2018-03-12 stsp goto done;
2666 c09a553d 2018-03-12 stsp
2667 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2668 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2669 c09a553d 2018-03-12 stsp goto done;
2670 c09a553d 2018-03-12 stsp
2671 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2672 c09a553d 2018-03-12 stsp if (error != NULL)
2673 c09a553d 2018-03-12 stsp goto done;
2674 c09a553d 2018-03-12 stsp
2675 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2676 e5dc7198 2018-12-29 stsp path_prefix);
2677 e5dc7198 2018-12-29 stsp if (error != NULL)
2678 e5dc7198 2018-12-29 stsp goto done;
2679 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2680 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2681 49520a32 2018-12-29 stsp goto done;
2682 49520a32 2018-12-29 stsp }
2683 49520a32 2018-12-29 stsp
2684 8069f636 2019-01-12 stsp if (commit_id_str) {
2685 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2686 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2687 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2688 30837e32 2019-07-25 stsp if (error)
2689 8069f636 2019-01-12 stsp goto done;
2690 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2691 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2692 8069f636 2019-01-12 stsp if (error != NULL) {
2693 8069f636 2019-01-12 stsp free(commit_id);
2694 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2695 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2696 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2697 4b6c9460 2020-03-05 stsp }
2698 8069f636 2019-01-12 stsp goto done;
2699 8069f636 2019-01-12 stsp }
2700 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2701 4b6c9460 2020-03-05 stsp if (error) {
2702 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2703 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2704 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2705 4b6c9460 2020-03-05 stsp }
2706 45d344f6 2019-05-14 stsp goto done;
2707 4b6c9460 2020-03-05 stsp }
2708 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2709 8069f636 2019-01-12 stsp commit_id);
2710 8069f636 2019-01-12 stsp free(commit_id);
2711 8069f636 2019-01-12 stsp if (error)
2712 8069f636 2019-01-12 stsp goto done;
2713 8069f636 2019-01-12 stsp }
2714 8069f636 2019-01-12 stsp
2715 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2716 f2ea84fa 2019-07-27 stsp if (error)
2717 f2ea84fa 2019-07-27 stsp goto done;
2718 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2719 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2720 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2721 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2722 c09a553d 2018-03-12 stsp if (error != NULL)
2723 c09a553d 2018-03-12 stsp goto done;
2724 c09a553d 2018-03-12 stsp
2725 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2726 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2727 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2728 c09a553d 2018-03-12 stsp done:
2729 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2730 8069f636 2019-01-12 stsp free(commit_id_str);
2731 76089277 2018-04-01 stsp free(repo_path);
2732 507dc3bb 2018-12-29 stsp free(worktree_path);
2733 c47340da 2020-09-20 stsp free(cwd);
2734 c47340da 2020-09-20 stsp free(path);
2735 507dc3bb 2018-12-29 stsp return error;
2736 9627c110 2020-04-18 stsp }
2737 9627c110 2020-04-18 stsp
2738 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2739 9627c110 2020-04-18 stsp int did_something;
2740 9627c110 2020-04-18 stsp int conflicts;
2741 9627c110 2020-04-18 stsp int obstructed;
2742 9627c110 2020-04-18 stsp int not_updated;
2743 9627c110 2020-04-18 stsp };
2744 9627c110 2020-04-18 stsp
2745 9627c110 2020-04-18 stsp void
2746 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2747 9627c110 2020-04-18 stsp {
2748 9627c110 2020-04-18 stsp if (!upa->did_something)
2749 9627c110 2020-04-18 stsp return;
2750 9627c110 2020-04-18 stsp
2751 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2752 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2753 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2754 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2755 9627c110 2020-04-18 stsp upa->obstructed);
2756 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2757 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2758 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2759 507dc3bb 2018-12-29 stsp }
2760 507dc3bb 2018-12-29 stsp
2761 507dc3bb 2018-12-29 stsp __dead static void
2762 507dc3bb 2018-12-29 stsp usage_update(void)
2763 507dc3bb 2018-12-29 stsp {
2764 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2765 507dc3bb 2018-12-29 stsp getprogname());
2766 507dc3bb 2018-12-29 stsp exit(1);
2767 507dc3bb 2018-12-29 stsp }
2768 507dc3bb 2018-12-29 stsp
2769 1ee397ad 2019-07-12 stsp static const struct got_error *
2770 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2771 507dc3bb 2018-12-29 stsp {
2772 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2773 784955db 2019-01-12 stsp
2774 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2775 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2776 1ee397ad 2019-07-12 stsp return NULL;
2777 507dc3bb 2018-12-29 stsp
2778 9627c110 2020-04-18 stsp upa->did_something = 1;
2779 a484d721 2019-06-10 stsp
2780 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2781 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2782 1ee397ad 2019-07-12 stsp return NULL;
2783 a484d721 2019-06-10 stsp
2784 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2785 9627c110 2020-04-18 stsp upa->conflicts++;
2786 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2787 9627c110 2020-04-18 stsp upa->obstructed++;
2788 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2789 9627c110 2020-04-18 stsp upa->not_updated++;
2790 9627c110 2020-04-18 stsp
2791 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2792 507dc3bb 2018-12-29 stsp path++;
2793 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2794 1ee397ad 2019-07-12 stsp return NULL;
2795 be7061eb 2018-12-30 stsp }
2796 be7061eb 2018-12-30 stsp
2797 be7061eb 2018-12-30 stsp static const struct got_error *
2798 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2799 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2800 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2801 a1fb16d8 2019-05-24 stsp {
2802 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2803 a1fb16d8 2019-05-24 stsp char *base_id_str;
2804 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2805 a1fb16d8 2019-05-24 stsp
2806 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2807 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2808 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2809 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2810 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2811 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2812 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2813 a1fb16d8 2019-05-24 stsp }
2814 a1fb16d8 2019-05-24 stsp
2815 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2816 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2817 a1fb16d8 2019-05-24 stsp if (err) {
2818 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2819 a1fb16d8 2019-05-24 stsp return err;
2820 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2821 a1fb16d8 2019-05-24 stsp }
2822 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2823 a1fb16d8 2019-05-24 stsp return NULL;
2824 a1fb16d8 2019-05-24 stsp
2825 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2826 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2827 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2828 a1fb16d8 2019-05-24 stsp if (err)
2829 a1fb16d8 2019-05-24 stsp return err;
2830 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2831 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2832 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2833 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2834 0ebf8283 2019-07-24 stsp return NULL;
2835 0ebf8283 2019-07-24 stsp }
2836 0ebf8283 2019-07-24 stsp
2837 0ebf8283 2019-07-24 stsp static const struct got_error *
2838 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2839 0ebf8283 2019-07-24 stsp {
2840 0ebf8283 2019-07-24 stsp const struct got_error *err;
2841 0ebf8283 2019-07-24 stsp int in_progress;
2842 0ebf8283 2019-07-24 stsp
2843 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2844 0ebf8283 2019-07-24 stsp if (err)
2845 0ebf8283 2019-07-24 stsp return err;
2846 0ebf8283 2019-07-24 stsp if (in_progress)
2847 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2848 0ebf8283 2019-07-24 stsp
2849 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2850 0ebf8283 2019-07-24 stsp if (err)
2851 0ebf8283 2019-07-24 stsp return err;
2852 0ebf8283 2019-07-24 stsp if (in_progress)
2853 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2854 0ebf8283 2019-07-24 stsp
2855 a1fb16d8 2019-05-24 stsp return NULL;
2856 a5edda0a 2019-07-27 stsp }
2857 a5edda0a 2019-07-27 stsp
2858 a5edda0a 2019-07-27 stsp static const struct got_error *
2859 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2860 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2861 a5edda0a 2019-07-27 stsp {
2862 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2863 a5edda0a 2019-07-27 stsp char *path;
2864 a5edda0a 2019-07-27 stsp int i;
2865 a5edda0a 2019-07-27 stsp
2866 a5edda0a 2019-07-27 stsp if (argc == 0) {
2867 a5edda0a 2019-07-27 stsp path = strdup("");
2868 a5edda0a 2019-07-27 stsp if (path == NULL)
2869 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2870 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2871 a5edda0a 2019-07-27 stsp }
2872 a5edda0a 2019-07-27 stsp
2873 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2874 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2875 a5edda0a 2019-07-27 stsp if (err)
2876 a5edda0a 2019-07-27 stsp break;
2877 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2878 a5edda0a 2019-07-27 stsp if (err) {
2879 a5edda0a 2019-07-27 stsp free(path);
2880 a5edda0a 2019-07-27 stsp break;
2881 a5edda0a 2019-07-27 stsp }
2882 a5edda0a 2019-07-27 stsp }
2883 a5edda0a 2019-07-27 stsp
2884 a5edda0a 2019-07-27 stsp return err;
2885 a1fb16d8 2019-05-24 stsp }
2886 a1fb16d8 2019-05-24 stsp
2887 a1fb16d8 2019-05-24 stsp static const struct got_error *
2888 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2889 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2890 fa51e947 2020-03-27 stsp {
2891 fa51e947 2020-03-27 stsp const struct got_error *err;
2892 fa51e947 2020-03-27 stsp struct got_repository *repo;
2893 fa51e947 2020-03-27 stsp static char msg[512];
2894 fa51e947 2020-03-27 stsp
2895 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2896 fa51e947 2020-03-27 stsp if (err)
2897 fa51e947 2020-03-27 stsp return orig_err;
2898 fa51e947 2020-03-27 stsp
2899 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2900 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2901 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2902 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2903 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2904 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2905 fa51e947 2020-03-27 stsp got_repo_close(repo);
2906 fa51e947 2020-03-27 stsp return err;
2907 fa51e947 2020-03-27 stsp }
2908 fa51e947 2020-03-27 stsp
2909 fa51e947 2020-03-27 stsp static const struct got_error *
2910 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2911 507dc3bb 2018-12-29 stsp {
2912 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2913 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2914 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2915 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2916 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2917 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2918 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2919 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2920 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2921 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2922 9627c110 2020-04-18 stsp int ch;
2923 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2924 507dc3bb 2018-12-29 stsp
2925 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2926 f2ea84fa 2019-07-27 stsp
2927 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2928 507dc3bb 2018-12-29 stsp switch (ch) {
2929 024e9686 2019-05-14 stsp case 'b':
2930 024e9686 2019-05-14 stsp branch_name = optarg;
2931 024e9686 2019-05-14 stsp break;
2932 507dc3bb 2018-12-29 stsp case 'c':
2933 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2934 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2935 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2936 507dc3bb 2018-12-29 stsp break;
2937 507dc3bb 2018-12-29 stsp default:
2938 2deda0b9 2019-03-07 stsp usage_update();
2939 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2940 507dc3bb 2018-12-29 stsp }
2941 507dc3bb 2018-12-29 stsp }
2942 507dc3bb 2018-12-29 stsp
2943 507dc3bb 2018-12-29 stsp argc -= optind;
2944 507dc3bb 2018-12-29 stsp argv += optind;
2945 507dc3bb 2018-12-29 stsp
2946 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2947 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2948 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2949 507dc3bb 2018-12-29 stsp err(1, "pledge");
2950 507dc3bb 2018-12-29 stsp #endif
2951 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2952 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2953 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2954 c4cdcb68 2019-04-03 stsp goto done;
2955 c4cdcb68 2019-04-03 stsp }
2956 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2957 fa51e947 2020-03-27 stsp if (error) {
2958 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2959 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
2960 fa51e947 2020-03-27 stsp worktree_path);
2961 7d5807f4 2019-07-11 stsp goto done;
2962 fa51e947 2020-03-27 stsp }
2963 7d5807f4 2019-07-11 stsp
2964 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2965 f2ea84fa 2019-07-27 stsp if (error)
2966 f2ea84fa 2019-07-27 stsp goto done;
2967 507dc3bb 2018-12-29 stsp
2968 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2969 c9956ddf 2019-09-08 stsp NULL);
2970 507dc3bb 2018-12-29 stsp if (error != NULL)
2971 507dc3bb 2018-12-29 stsp goto done;
2972 507dc3bb 2018-12-29 stsp
2973 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2974 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2975 087fb88c 2019-08-04 stsp if (error)
2976 087fb88c 2019-08-04 stsp goto done;
2977 087fb88c 2019-08-04 stsp
2978 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2979 0266afb7 2019-01-04 stsp if (error)
2980 0266afb7 2019-01-04 stsp goto done;
2981 0266afb7 2019-01-04 stsp
2982 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2983 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2984 024e9686 2019-05-14 stsp if (error != NULL)
2985 024e9686 2019-05-14 stsp goto done;
2986 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2987 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2988 9c4b8182 2019-01-02 stsp if (error != NULL)
2989 9c4b8182 2019-01-02 stsp goto done;
2990 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2991 507dc3bb 2018-12-29 stsp if (error != NULL)
2992 507dc3bb 2018-12-29 stsp goto done;
2993 507dc3bb 2018-12-29 stsp } else {
2994 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2995 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2996 04f57cb3 2019-07-25 stsp free(commit_id_str);
2997 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2998 30837e32 2019-07-25 stsp if (error)
2999 a297e751 2019-07-11 stsp goto done;
3000 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3001 a297e751 2019-07-11 stsp if (error)
3002 507dc3bb 2018-12-29 stsp goto done;
3003 507dc3bb 2018-12-29 stsp }
3004 35c965b2 2018-12-29 stsp
3005 a1fb16d8 2019-05-24 stsp if (branch_name) {
3006 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3007 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3008 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3009 f2ea84fa 2019-07-27 stsp continue;
3010 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3011 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3012 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3013 024e9686 2019-05-14 stsp goto done;
3014 024e9686 2019-05-14 stsp }
3015 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3016 024e9686 2019-05-14 stsp if (error)
3017 024e9686 2019-05-14 stsp goto done;
3018 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3019 3aef623b 2019-10-15 stsp repo);
3020 a367ff0f 2019-05-14 stsp free(head_commit_id);
3021 024e9686 2019-05-14 stsp if (error != NULL)
3022 024e9686 2019-05-14 stsp goto done;
3023 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3024 a367ff0f 2019-05-14 stsp if (error)
3025 a367ff0f 2019-05-14 stsp goto done;
3026 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3027 024e9686 2019-05-14 stsp if (error)
3028 024e9686 2019-05-14 stsp goto done;
3029 024e9686 2019-05-14 stsp } else {
3030 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3031 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3032 a367ff0f 2019-05-14 stsp if (error != NULL) {
3033 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3034 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3035 024e9686 2019-05-14 stsp goto done;
3036 a367ff0f 2019-05-14 stsp }
3037 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3038 a367ff0f 2019-05-14 stsp if (error)
3039 a367ff0f 2019-05-14 stsp goto done;
3040 024e9686 2019-05-14 stsp }
3041 507dc3bb 2018-12-29 stsp
3042 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3043 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3044 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3045 507dc3bb 2018-12-29 stsp commit_id);
3046 507dc3bb 2018-12-29 stsp if (error)
3047 507dc3bb 2018-12-29 stsp goto done;
3048 507dc3bb 2018-12-29 stsp }
3049 507dc3bb 2018-12-29 stsp
3050 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3051 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3052 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3053 507dc3bb 2018-12-29 stsp if (error != NULL)
3054 507dc3bb 2018-12-29 stsp goto done;
3055 9c4b8182 2019-01-02 stsp
3056 9627c110 2020-04-18 stsp if (upa.did_something)
3057 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3058 784955db 2019-01-12 stsp else
3059 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3060 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3061 507dc3bb 2018-12-29 stsp done:
3062 c09a553d 2018-03-12 stsp free(worktree_path);
3063 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3064 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3065 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3066 507dc3bb 2018-12-29 stsp free(commit_id);
3067 9c4b8182 2019-01-02 stsp free(commit_id_str);
3068 c09a553d 2018-03-12 stsp return error;
3069 c09a553d 2018-03-12 stsp }
3070 c09a553d 2018-03-12 stsp
3071 f42b1b34 2018-03-12 stsp static const struct got_error *
3072 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3073 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3074 63035f9f 2019-10-06 stsp struct got_repository *repo)
3075 5c860e29 2018-03-12 stsp {
3076 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3077 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3078 79109fed 2018-03-27 stsp
3079 44392932 2019-08-25 stsp if (blob_id1) {
3080 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3081 44392932 2019-08-25 stsp if (err)
3082 44392932 2019-08-25 stsp goto done;
3083 44392932 2019-08-25 stsp }
3084 79109fed 2018-03-27 stsp
3085 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3086 44392932 2019-08-25 stsp if (err)
3087 44392932 2019-08-25 stsp goto done;
3088 79109fed 2018-03-27 stsp
3089 44392932 2019-08-25 stsp while (path[0] == '/')
3090 44392932 2019-08-25 stsp path++;
3091 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
3092 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
3093 44392932 2019-08-25 stsp done:
3094 44392932 2019-08-25 stsp if (blob1)
3095 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3096 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3097 44392932 2019-08-25 stsp return err;
3098 44392932 2019-08-25 stsp }
3099 44392932 2019-08-25 stsp
3100 44392932 2019-08-25 stsp static const struct got_error *
3101 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3102 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3103 63035f9f 2019-10-06 stsp struct got_repository *repo)
3104 44392932 2019-08-25 stsp {
3105 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3106 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3107 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3108 44392932 2019-08-25 stsp
3109 44392932 2019-08-25 stsp if (tree_id1) {
3110 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3111 79109fed 2018-03-27 stsp if (err)
3112 44392932 2019-08-25 stsp goto done;
3113 79109fed 2018-03-27 stsp }
3114 79109fed 2018-03-27 stsp
3115 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3116 0f2b3dca 2018-12-22 stsp if (err)
3117 0f2b3dca 2018-12-22 stsp goto done;
3118 0f2b3dca 2018-12-22 stsp
3119 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3120 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3121 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3122 44392932 2019-08-25 stsp while (path[0] == '/')
3123 44392932 2019-08-25 stsp path++;
3124 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3125 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3126 0f2b3dca 2018-12-22 stsp done:
3127 79109fed 2018-03-27 stsp if (tree1)
3128 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3129 366e0a5f 2019-10-10 stsp if (tree2)
3130 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3131 44392932 2019-08-25 stsp return err;
3132 44392932 2019-08-25 stsp }
3133 44392932 2019-08-25 stsp
3134 44392932 2019-08-25 stsp static const struct got_error *
3135 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3136 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3137 0208f208 2020-05-05 stsp {
3138 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3139 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3140 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3141 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3142 0208f208 2020-05-05 stsp
3143 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3144 0208f208 2020-05-05 stsp if (qid != NULL) {
3145 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3146 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3147 0208f208 2020-05-05 stsp qid->id);
3148 0208f208 2020-05-05 stsp if (err)
3149 0208f208 2020-05-05 stsp return err;
3150 0208f208 2020-05-05 stsp
3151 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3152 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3153 0208f208 2020-05-05 stsp
3154 0208f208 2020-05-05 stsp }
3155 0208f208 2020-05-05 stsp
3156 0208f208 2020-05-05 stsp if (tree_id1) {
3157 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3158 0208f208 2020-05-05 stsp if (err)
3159 0208f208 2020-05-05 stsp goto done;
3160 0208f208 2020-05-05 stsp }
3161 0208f208 2020-05-05 stsp
3162 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3163 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3164 0208f208 2020-05-05 stsp if (err)
3165 0208f208 2020-05-05 stsp goto done;
3166 0208f208 2020-05-05 stsp
3167 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3168 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3169 0208f208 2020-05-05 stsp done:
3170 0208f208 2020-05-05 stsp if (tree1)
3171 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3172 0208f208 2020-05-05 stsp if (tree2)
3173 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3174 0208f208 2020-05-05 stsp return err;
3175 0208f208 2020-05-05 stsp }
3176 0208f208 2020-05-05 stsp
3177 0208f208 2020-05-05 stsp static const struct got_error *
3178 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3179 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3180 44392932 2019-08-25 stsp {
3181 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3182 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3183 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3184 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3185 44392932 2019-08-25 stsp struct got_object_qid *qid;
3186 44392932 2019-08-25 stsp
3187 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3188 44392932 2019-08-25 stsp if (qid != NULL) {
3189 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3190 44392932 2019-08-25 stsp qid->id);
3191 44392932 2019-08-25 stsp if (err)
3192 44392932 2019-08-25 stsp return err;
3193 44392932 2019-08-25 stsp }
3194 44392932 2019-08-25 stsp
3195 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3196 44392932 2019-08-25 stsp int obj_type;
3197 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3198 44392932 2019-08-25 stsp if (err)
3199 44392932 2019-08-25 stsp goto done;
3200 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3201 44392932 2019-08-25 stsp if (err) {
3202 44392932 2019-08-25 stsp free(obj_id2);
3203 44392932 2019-08-25 stsp goto done;
3204 44392932 2019-08-25 stsp }
3205 44392932 2019-08-25 stsp if (pcommit) {
3206 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3207 44392932 2019-08-25 stsp qid->id, path);
3208 44392932 2019-08-25 stsp if (err) {
3209 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3210 2e8c69d1 2020-05-04 stsp free(obj_id2);
3211 2e8c69d1 2020-05-04 stsp goto done;
3212 2e8c69d1 2020-05-04 stsp }
3213 2e8c69d1 2020-05-04 stsp } else {
3214 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3215 2e8c69d1 2020-05-04 stsp if (err) {
3216 2e8c69d1 2020-05-04 stsp free(obj_id2);
3217 2e8c69d1 2020-05-04 stsp goto done;
3218 2e8c69d1 2020-05-04 stsp }
3219 44392932 2019-08-25 stsp }
3220 44392932 2019-08-25 stsp }
3221 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3222 44392932 2019-08-25 stsp if (err) {
3223 44392932 2019-08-25 stsp free(obj_id2);
3224 44392932 2019-08-25 stsp goto done;
3225 44392932 2019-08-25 stsp }
3226 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3227 44392932 2019-08-25 stsp switch (obj_type) {
3228 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3229 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3230 63035f9f 2019-10-06 stsp 0, repo);
3231 44392932 2019-08-25 stsp break;
3232 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3233 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3234 63035f9f 2019-10-06 stsp 0, repo);
3235 44392932 2019-08-25 stsp break;
3236 44392932 2019-08-25 stsp default:
3237 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3238 44392932 2019-08-25 stsp break;
3239 44392932 2019-08-25 stsp }
3240 44392932 2019-08-25 stsp free(obj_id1);
3241 44392932 2019-08-25 stsp free(obj_id2);
3242 44392932 2019-08-25 stsp } else {
3243 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3244 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3245 44392932 2019-08-25 stsp if (err)
3246 44392932 2019-08-25 stsp goto done;
3247 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3248 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
3249 44392932 2019-08-25 stsp if (err)
3250 44392932 2019-08-25 stsp goto done;
3251 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3252 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3253 44392932 2019-08-25 stsp }
3254 44392932 2019-08-25 stsp done:
3255 0f2b3dca 2018-12-22 stsp free(id_str1);
3256 0f2b3dca 2018-12-22 stsp free(id_str2);
3257 44392932 2019-08-25 stsp if (pcommit)
3258 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3259 79109fed 2018-03-27 stsp return err;
3260 79109fed 2018-03-27 stsp }
3261 79109fed 2018-03-27 stsp
3262 4bb494d5 2018-06-16 stsp static char *
3263 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3264 6c281f94 2018-06-11 stsp {
3265 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3266 09867e48 2019-08-13 stsp char *p, *s;
3267 09867e48 2019-08-13 stsp
3268 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3269 09867e48 2019-08-13 stsp if (tm == NULL)
3270 09867e48 2019-08-13 stsp return NULL;
3271 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3272 09867e48 2019-08-13 stsp if (s == NULL)
3273 09867e48 2019-08-13 stsp return NULL;
3274 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3275 6c281f94 2018-06-11 stsp if (p)
3276 6c281f94 2018-06-11 stsp *p = '\0';
3277 6c281f94 2018-06-11 stsp return s;
3278 6c281f94 2018-06-11 stsp }
3279 dc424a06 2019-08-07 stsp
3280 6841bf13 2019-11-29 kn static const struct got_error *
3281 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3282 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3283 6841bf13 2019-11-29 kn {
3284 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3285 6841bf13 2019-11-29 kn regmatch_t regmatch;
3286 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3287 6841bf13 2019-11-29 kn
3288 6841bf13 2019-11-29 kn *have_match = 0;
3289 6841bf13 2019-11-29 kn
3290 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3291 6841bf13 2019-11-29 kn if (err)
3292 6841bf13 2019-11-29 kn return err;
3293 6841bf13 2019-11-29 kn
3294 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3295 6841bf13 2019-11-29 kn if (err)
3296 6841bf13 2019-11-29 kn goto done;
3297 6841bf13 2019-11-29 kn
3298 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3299 6841bf13 2019-11-29 kn *have_match = 1;
3300 6841bf13 2019-11-29 kn done:
3301 6841bf13 2019-11-29 kn free(id_str);
3302 6841bf13 2019-11-29 kn free(logmsg);
3303 6841bf13 2019-11-29 kn return err;
3304 6841bf13 2019-11-29 kn }
3305 6841bf13 2019-11-29 kn
3306 0208f208 2020-05-05 stsp static void
3307 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3308 0208f208 2020-05-05 stsp regex_t *regex)
3309 0208f208 2020-05-05 stsp {
3310 0208f208 2020-05-05 stsp regmatch_t regmatch;
3311 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3312 0208f208 2020-05-05 stsp
3313 0208f208 2020-05-05 stsp *have_match = 0;
3314 0208f208 2020-05-05 stsp
3315 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3316 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3317 0208f208 2020-05-05 stsp *have_match = 1;
3318 0208f208 2020-05-05 stsp break;
3319 0208f208 2020-05-05 stsp }
3320 0208f208 2020-05-05 stsp }
3321 0208f208 2020-05-05 stsp }
3322 0208f208 2020-05-05 stsp
3323 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3324 6c281f94 2018-06-11 stsp
3325 79109fed 2018-03-27 stsp static const struct got_error *
3326 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3327 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3328 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3329 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3330 79109fed 2018-03-27 stsp {
3331 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3332 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3333 6c281f94 2018-06-11 stsp char datebuf[26];
3334 45d799e2 2018-12-23 stsp time_t committer_time;
3335 45d799e2 2018-12-23 stsp const char *author, *committer;
3336 199a4027 2019-02-02 stsp char *refs_str = NULL;
3337 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3338 5c860e29 2018-03-12 stsp
3339 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3340 199a4027 2019-02-02 stsp char *s;
3341 199a4027 2019-02-02 stsp const char *name;
3342 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3343 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3344 a436ad14 2019-08-13 stsp int cmp;
3345 a436ad14 2019-08-13 stsp
3346 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3347 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3348 d9498b20 2019-02-05 stsp continue;
3349 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3350 199a4027 2019-02-02 stsp name += 5;
3351 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3352 7143d404 2019-03-12 stsp continue;
3353 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3354 e34f9ed6 2019-02-02 stsp name += 6;
3355 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3356 141c2bff 2019-02-04 stsp name += 8;
3357 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3358 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3359 4343a07f 2020-04-24 stsp continue;
3360 4343a07f 2020-04-24 stsp }
3361 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3362 48cae60d 2020-09-22 stsp if (err)
3363 48cae60d 2020-09-22 stsp return err;
3364 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3365 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3366 5d844a1e 2019-08-13 stsp if (err) {
3367 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3368 48cae60d 2020-09-22 stsp free(ref_id);
3369 5d844a1e 2019-08-13 stsp return err;
3370 48cae60d 2020-09-22 stsp }
3371 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3372 5d844a1e 2019-08-13 stsp err = NULL;
3373 5d844a1e 2019-08-13 stsp tag = NULL;
3374 5d844a1e 2019-08-13 stsp }
3375 a436ad14 2019-08-13 stsp }
3376 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3377 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3378 48cae60d 2020-09-22 stsp free(ref_id);
3379 a436ad14 2019-08-13 stsp if (tag)
3380 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3381 a436ad14 2019-08-13 stsp if (cmp != 0)
3382 a436ad14 2019-08-13 stsp continue;
3383 199a4027 2019-02-02 stsp s = refs_str;
3384 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3385 199a4027 2019-02-02 stsp name) == -1) {
3386 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3387 199a4027 2019-02-02 stsp free(s);
3388 34ca4898 2019-08-13 stsp return err;
3389 199a4027 2019-02-02 stsp }
3390 199a4027 2019-02-02 stsp free(s);
3391 199a4027 2019-02-02 stsp }
3392 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3393 8bf5b3c9 2018-03-17 stsp if (err)
3394 8bf5b3c9 2018-03-17 stsp return err;
3395 788c352e 2018-06-16 stsp
3396 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3397 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3398 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3399 832c249c 2018-06-10 stsp free(id_str);
3400 d3d493d7 2019-02-21 stsp id_str = NULL;
3401 d3d493d7 2019-02-21 stsp free(refs_str);
3402 d3d493d7 2019-02-21 stsp refs_str = NULL;
3403 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3404 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3405 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3406 09867e48 2019-08-13 stsp if (datestr)
3407 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3408 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3409 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3410 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3411 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3412 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3413 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3414 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3415 3fe1abad 2018-06-10 stsp int n = 1;
3416 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3417 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3418 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3419 a0603db2 2018-06-10 stsp if (err)
3420 a0603db2 2018-06-10 stsp return err;
3421 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3422 a0603db2 2018-06-10 stsp free(id_str);
3423 a0603db2 2018-06-10 stsp }
3424 a0603db2 2018-06-10 stsp }
3425 832c249c 2018-06-10 stsp
3426 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3427 5943eee2 2019-08-13 stsp if (err)
3428 5943eee2 2019-08-13 stsp return err;
3429 8bf5b3c9 2018-03-17 stsp
3430 621015ac 2018-07-23 stsp logmsg = logmsg0;
3431 832c249c 2018-06-10 stsp do {
3432 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3433 832c249c 2018-06-10 stsp if (line)
3434 832c249c 2018-06-10 stsp printf(" %s\n", line);
3435 832c249c 2018-06-10 stsp } while (line);
3436 621015ac 2018-07-23 stsp free(logmsg0);
3437 832c249c 2018-06-10 stsp
3438 0208f208 2020-05-05 stsp if (changed_paths) {
3439 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3440 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3441 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3442 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3443 0208f208 2020-05-05 stsp }
3444 0208f208 2020-05-05 stsp printf("\n");
3445 0208f208 2020-05-05 stsp }
3446 971751ac 2018-03-27 stsp if (show_patch) {
3447 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3448 971751ac 2018-03-27 stsp if (err == 0)
3449 971751ac 2018-03-27 stsp printf("\n");
3450 971751ac 2018-03-27 stsp }
3451 07862c20 2018-09-15 stsp
3452 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3453 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3454 07862c20 2018-09-15 stsp return err;
3455 f42b1b34 2018-03-12 stsp }
3456 5c860e29 2018-03-12 stsp
3457 f42b1b34 2018-03-12 stsp static const struct got_error *
3458 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3459 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3460 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3461 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3462 f42b1b34 2018-03-12 stsp {
3463 f42b1b34 2018-03-12 stsp const struct got_error *err;
3464 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3465 6841bf13 2019-11-29 kn regex_t regex;
3466 6841bf13 2019-11-29 kn int have_match;
3467 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3468 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3469 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3470 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3471 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3472 dbec59df 2020-04-18 stsp
3473 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3474 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3475 372ccdbb 2018-06-10 stsp
3476 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3477 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3478 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3479 6841bf13 2019-11-29 kn
3480 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3481 f42b1b34 2018-03-12 stsp if (err)
3482 f42b1b34 2018-03-12 stsp return err;
3483 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3484 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3485 372ccdbb 2018-06-10 stsp if (err)
3486 fcc85cad 2018-07-22 stsp goto done;
3487 656b1f76 2019-05-11 jcs for (;;) {
3488 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3489 8bf5b3c9 2018-03-17 stsp
3490 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3491 84453469 2018-11-11 stsp break;
3492 84453469 2018-11-11 stsp
3493 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3494 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3495 372ccdbb 2018-06-10 stsp if (err) {
3496 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3497 9ba79e04 2018-06-11 stsp err = NULL;
3498 ee780d5c 2020-01-04 stsp break;
3499 7e665116 2018-04-02 stsp }
3500 b43fbaa0 2018-06-11 stsp if (id == NULL)
3501 7e665116 2018-04-02 stsp break;
3502 b43fbaa0 2018-06-11 stsp
3503 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3504 b43fbaa0 2018-06-11 stsp if (err)
3505 fcc85cad 2018-07-22 stsp break;
3506 6841bf13 2019-11-29 kn
3507 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3508 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3509 0208f208 2020-05-05 stsp if (err)
3510 0208f208 2020-05-05 stsp break;
3511 0208f208 2020-05-05 stsp }
3512 0208f208 2020-05-05 stsp
3513 6841bf13 2019-11-29 kn if (search_pattern) {
3514 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3515 6841bf13 2019-11-29 kn if (err) {
3516 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3517 6841bf13 2019-11-29 kn break;
3518 6841bf13 2019-11-29 kn }
3519 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3520 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3521 0208f208 2020-05-05 stsp &changed_paths, &regex);
3522 6841bf13 2019-11-29 kn if (have_match == 0) {
3523 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3524 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3525 0208f208 2020-05-05 stsp free((char *)pe->path);
3526 0208f208 2020-05-05 stsp free(pe->data);
3527 0208f208 2020-05-05 stsp }
3528 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3529 6841bf13 2019-11-29 kn continue;
3530 6841bf13 2019-11-29 kn }
3531 6841bf13 2019-11-29 kn }
3532 6841bf13 2019-11-29 kn
3533 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3534 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3535 dbec59df 2020-04-18 stsp if (err)
3536 dbec59df 2020-04-18 stsp break;
3537 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3538 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3539 dbec59df 2020-04-18 stsp } else {
3540 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3541 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3542 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3543 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3544 dbec59df 2020-04-18 stsp if (err)
3545 dbec59df 2020-04-18 stsp break;
3546 dbec59df 2020-04-18 stsp }
3547 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3548 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3549 7e665116 2018-04-02 stsp break;
3550 0208f208 2020-05-05 stsp
3551 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3552 0208f208 2020-05-05 stsp free((char *)pe->path);
3553 0208f208 2020-05-05 stsp free(pe->data);
3554 0208f208 2020-05-05 stsp }
3555 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3556 31cedeaf 2018-09-15 stsp }
3557 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3558 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3559 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3560 dbec59df 2020-04-18 stsp if (err)
3561 dbec59df 2020-04-18 stsp break;
3562 502b9684 2020-07-31 stsp if (show_changed_paths) {
3563 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3564 502b9684 2020-07-31 stsp commit, repo);
3565 502b9684 2020-07-31 stsp if (err)
3566 502b9684 2020-07-31 stsp break;
3567 502b9684 2020-07-31 stsp }
3568 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3569 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3570 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3571 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3572 dbec59df 2020-04-18 stsp if (err)
3573 dbec59df 2020-04-18 stsp break;
3574 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3575 502b9684 2020-07-31 stsp free((char *)pe->path);
3576 502b9684 2020-07-31 stsp free(pe->data);
3577 502b9684 2020-07-31 stsp }
3578 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3579 dbec59df 2020-04-18 stsp }
3580 dbec59df 2020-04-18 stsp }
3581 fcc85cad 2018-07-22 stsp done:
3582 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3583 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3584 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3585 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3586 dbec59df 2020-04-18 stsp }
3587 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3588 0208f208 2020-05-05 stsp free((char *)pe->path);
3589 0208f208 2020-05-05 stsp free(pe->data);
3590 0208f208 2020-05-05 stsp }
3591 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3592 6841bf13 2019-11-29 kn if (search_pattern)
3593 6841bf13 2019-11-29 kn regfree(&regex);
3594 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3595 f42b1b34 2018-03-12 stsp return err;
3596 f42b1b34 2018-03-12 stsp }
3597 5c860e29 2018-03-12 stsp
3598 4ed7e80c 2018-05-20 stsp __dead static void
3599 6f3d1eb0 2018-03-12 stsp usage_log(void)
3600 6f3d1eb0 2018-03-12 stsp {
3601 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3602 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3603 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3604 6f3d1eb0 2018-03-12 stsp exit(1);
3605 b1ebc001 2019-08-13 stsp }
3606 b1ebc001 2019-08-13 stsp
3607 b1ebc001 2019-08-13 stsp static int
3608 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3609 b1ebc001 2019-08-13 stsp {
3610 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3611 b1ebc001 2019-08-13 stsp long long n;
3612 b1ebc001 2019-08-13 stsp const char *errstr;
3613 b1ebc001 2019-08-13 stsp
3614 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3615 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3616 b1ebc001 2019-08-13 stsp return 0;
3617 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3618 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3619 b1ebc001 2019-08-13 stsp return 0;
3620 b1ebc001 2019-08-13 stsp return n;
3621 d1fe46f9 2020-04-18 stsp }
3622 d1fe46f9 2020-04-18 stsp
3623 d1fe46f9 2020-04-18 stsp static const struct got_error *
3624 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3625 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3626 d1fe46f9 2020-04-18 stsp {
3627 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3628 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3629 d1fe46f9 2020-04-18 stsp
3630 d1fe46f9 2020-04-18 stsp *id = NULL;
3631 d1fe46f9 2020-04-18 stsp
3632 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3633 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3634 d1fe46f9 2020-04-18 stsp int obj_type;
3635 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3636 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3637 d1fe46f9 2020-04-18 stsp if (err)
3638 d1fe46f9 2020-04-18 stsp return err;
3639 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3640 d1fe46f9 2020-04-18 stsp if (err)
3641 d1fe46f9 2020-04-18 stsp return err;
3642 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3643 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3644 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3645 d1fe46f9 2020-04-18 stsp if (err)
3646 d1fe46f9 2020-04-18 stsp return err;
3647 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3648 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3649 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3650 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3651 d1fe46f9 2020-04-18 stsp }
3652 d1fe46f9 2020-04-18 stsp free(*id);
3653 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3654 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3655 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3656 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3657 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3658 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3659 d1fe46f9 2020-04-18 stsp if (err)
3660 d1fe46f9 2020-04-18 stsp return err;
3661 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3662 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3663 d1fe46f9 2020-04-18 stsp } else {
3664 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3665 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3666 d1fe46f9 2020-04-18 stsp }
3667 d1fe46f9 2020-04-18 stsp
3668 d1fe46f9 2020-04-18 stsp return err;
3669 6f3d1eb0 2018-03-12 stsp }
3670 6f3d1eb0 2018-03-12 stsp
3671 4ed7e80c 2018-05-20 stsp static const struct got_error *
3672 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3673 f42b1b34 2018-03-12 stsp {
3674 f42b1b34 2018-03-12 stsp const struct got_error *error;
3675 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3676 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3677 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3678 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3679 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3680 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3681 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3682 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3683 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3684 64a96a6d 2018-04-01 stsp const char *errstr;
3685 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3686 1b3893a2 2019-03-18 stsp
3687 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3688 5c860e29 2018-03-12 stsp
3689 6715a751 2018-03-16 stsp #ifndef PROFILE
3690 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3691 6098196c 2019-01-04 stsp NULL)
3692 ad242220 2018-09-08 stsp == -1)
3693 f42b1b34 2018-03-12 stsp err(1, "pledge");
3694 6715a751 2018-03-16 stsp #endif
3695 79109fed 2018-03-27 stsp
3696 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3697 b1ebc001 2019-08-13 stsp
3698 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3699 79109fed 2018-03-27 stsp switch (ch) {
3700 79109fed 2018-03-27 stsp case 'p':
3701 79109fed 2018-03-27 stsp show_patch = 1;
3702 d142fc45 2018-04-01 stsp break;
3703 0208f208 2020-05-05 stsp case 'P':
3704 0208f208 2020-05-05 stsp show_changed_paths = 1;
3705 0208f208 2020-05-05 stsp break;
3706 d142fc45 2018-04-01 stsp case 'c':
3707 d142fc45 2018-04-01 stsp start_commit = optarg;
3708 64a96a6d 2018-04-01 stsp break;
3709 c0cc5c62 2018-10-18 stsp case 'C':
3710 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3711 4a8520aa 2018-10-18 stsp &errstr);
3712 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3713 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3714 c0cc5c62 2018-10-18 stsp break;
3715 64a96a6d 2018-04-01 stsp case 'l':
3716 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3717 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3718 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3719 cc54c501 2019-07-15 stsp break;
3720 48c8c60d 2020-01-27 stsp case 'b':
3721 48c8c60d 2020-01-27 stsp log_branches = 1;
3722 a0603db2 2018-06-10 stsp break;
3723 04ca23f4 2018-07-16 stsp case 'r':
3724 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3725 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3726 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3727 9ba1d308 2019-10-21 stsp optarg);
3728 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3729 04ca23f4 2018-07-16 stsp break;
3730 dbec59df 2020-04-18 stsp case 'R':
3731 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3732 dbec59df 2020-04-18 stsp break;
3733 6841bf13 2019-11-29 kn case 's':
3734 6841bf13 2019-11-29 kn search_pattern = optarg;
3735 6841bf13 2019-11-29 kn break;
3736 d1fe46f9 2020-04-18 stsp case 'x':
3737 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3738 d1fe46f9 2020-04-18 stsp break;
3739 79109fed 2018-03-27 stsp default:
3740 2deda0b9 2019-03-07 stsp usage_log();
3741 79109fed 2018-03-27 stsp /* NOTREACHED */
3742 79109fed 2018-03-27 stsp }
3743 79109fed 2018-03-27 stsp }
3744 79109fed 2018-03-27 stsp
3745 79109fed 2018-03-27 stsp argc -= optind;
3746 79109fed 2018-03-27 stsp argv += optind;
3747 f42b1b34 2018-03-12 stsp
3748 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3749 dc1edbfa 2019-11-29 kn diff_context = 3;
3750 dc1edbfa 2019-11-29 kn else if (!show_patch)
3751 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3752 dc1edbfa 2019-11-29 kn
3753 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3754 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3755 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3756 04ca23f4 2018-07-16 stsp goto done;
3757 04ca23f4 2018-07-16 stsp }
3758 cffc0aa4 2019-02-05 stsp
3759 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3760 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3761 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3762 50f2fada 2020-04-24 stsp goto done;
3763 50f2fada 2020-04-24 stsp error = NULL;
3764 50f2fada 2020-04-24 stsp }
3765 cffc0aa4 2019-02-05 stsp
3766 e7301579 2019-03-18 stsp if (argc == 0) {
3767 cbd1af7a 2019-03-18 stsp path = strdup("");
3768 e7301579 2019-03-18 stsp if (path == NULL) {
3769 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3770 cbd1af7a 2019-03-18 stsp goto done;
3771 e7301579 2019-03-18 stsp }
3772 e7301579 2019-03-18 stsp } else if (argc == 1) {
3773 e7301579 2019-03-18 stsp if (worktree) {
3774 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3775 e7301579 2019-03-18 stsp argv[0]);
3776 e7301579 2019-03-18 stsp if (error)
3777 e7301579 2019-03-18 stsp goto done;
3778 e7301579 2019-03-18 stsp } else {
3779 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3780 e7301579 2019-03-18 stsp if (path == NULL) {
3781 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3782 e7301579 2019-03-18 stsp goto done;
3783 e7301579 2019-03-18 stsp }
3784 e7301579 2019-03-18 stsp }
3785 cbd1af7a 2019-03-18 stsp } else
3786 cbd1af7a 2019-03-18 stsp usage_log();
3787 cbd1af7a 2019-03-18 stsp
3788 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3789 5486daa2 2019-05-11 stsp repo_path = worktree ?
3790 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3791 5486daa2 2019-05-11 stsp }
3792 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3793 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3794 cffc0aa4 2019-02-05 stsp goto done;
3795 04ca23f4 2018-07-16 stsp }
3796 6098196c 2019-01-04 stsp
3797 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3798 d7d4f210 2018-03-12 stsp if (error != NULL)
3799 04ca23f4 2018-07-16 stsp goto done;
3800 f42b1b34 2018-03-12 stsp
3801 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3802 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3803 c02c541e 2019-03-29 stsp if (error)
3804 c02c541e 2019-03-29 stsp goto done;
3805 c02c541e 2019-03-29 stsp
3806 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3807 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3808 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3809 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3810 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3811 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3812 3235492e 2018-04-01 stsp if (error != NULL)
3813 d1fe46f9 2020-04-18 stsp goto done;
3814 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3815 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3816 3235492e 2018-04-01 stsp if (error != NULL)
3817 d1fe46f9 2020-04-18 stsp goto done;
3818 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3819 d1fe46f9 2020-04-18 stsp start_id);
3820 d1fe46f9 2020-04-18 stsp if (error != NULL)
3821 d1fe46f9 2020-04-18 stsp goto done;
3822 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3823 3235492e 2018-04-01 stsp } else {
3824 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3825 d1fe46f9 2020-04-18 stsp if (error != NULL)
3826 d1fe46f9 2020-04-18 stsp goto done;
3827 3235492e 2018-04-01 stsp }
3828 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3829 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3830 d1fe46f9 2020-04-18 stsp if (error != NULL)
3831 d1fe46f9 2020-04-18 stsp goto done;
3832 d1fe46f9 2020-04-18 stsp }
3833 04ca23f4 2018-07-16 stsp
3834 deeabeae 2019-08-27 stsp if (worktree) {
3835 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3836 deeabeae 2019-08-27 stsp char *p;
3837 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3838 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3839 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3840 deeabeae 2019-08-27 stsp goto done;
3841 deeabeae 2019-08-27 stsp }
3842 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3843 deeabeae 2019-08-27 stsp free(p);
3844 deeabeae 2019-08-27 stsp } else
3845 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3846 04ca23f4 2018-07-16 stsp if (error != NULL)
3847 04ca23f4 2018-07-16 stsp goto done;
3848 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3849 04ca23f4 2018-07-16 stsp free(path);
3850 04ca23f4 2018-07-16 stsp path = in_repo_path;
3851 04ca23f4 2018-07-16 stsp }
3852 199a4027 2019-02-02 stsp
3853 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3854 199a4027 2019-02-02 stsp if (error)
3855 199a4027 2019-02-02 stsp goto done;
3856 04ca23f4 2018-07-16 stsp
3857 0208f208 2020-05-05 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3858 0208f208 2020-05-05 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3859 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3860 04ca23f4 2018-07-16 stsp done:
3861 04ca23f4 2018-07-16 stsp free(path);
3862 04ca23f4 2018-07-16 stsp free(repo_path);
3863 04ca23f4 2018-07-16 stsp free(cwd);
3864 cffc0aa4 2019-02-05 stsp if (worktree)
3865 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3866 ad242220 2018-09-08 stsp if (repo) {
3867 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3868 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3869 ad242220 2018-09-08 stsp if (error == NULL)
3870 ad242220 2018-09-08 stsp error = repo_error;
3871 ad242220 2018-09-08 stsp }
3872 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3873 8bf5b3c9 2018-03-17 stsp return error;
3874 5c860e29 2018-03-12 stsp }
3875 5c860e29 2018-03-12 stsp
3876 4ed7e80c 2018-05-20 stsp __dead static void
3877 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3878 3f8b7d6a 2018-04-01 stsp {
3879 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3880 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3881 3f8b7d6a 2018-04-01 stsp exit(1);
3882 b00d56cd 2018-04-01 stsp }
3883 b00d56cd 2018-04-01 stsp
3884 b72f483a 2019-02-05 stsp struct print_diff_arg {
3885 b72f483a 2019-02-05 stsp struct got_repository *repo;
3886 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3887 b72f483a 2019-02-05 stsp int diff_context;
3888 3fc0c068 2019-02-10 stsp const char *id_str;
3889 3fc0c068 2019-02-10 stsp int header_shown;
3890 98eaaa12 2019-08-03 stsp int diff_staged;
3891 63035f9f 2019-10-06 stsp int ignore_whitespace;
3892 b72f483a 2019-02-05 stsp };
3893 39449a05 2020-07-23 stsp
3894 39449a05 2020-07-23 stsp /*
3895 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3896 39449a05 2020-07-23 stsp * it as content to the diff engine.
3897 39449a05 2020-07-23 stsp */
3898 39449a05 2020-07-23 stsp static const struct got_error *
3899 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3900 39449a05 2020-07-23 stsp const char *abspath)
3901 39449a05 2020-07-23 stsp {
3902 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3903 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3904 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3905 39449a05 2020-07-23 stsp
3906 39449a05 2020-07-23 stsp *fd = -1;
3907 39449a05 2020-07-23 stsp
3908 39449a05 2020-07-23 stsp if (dirfd != -1) {
3909 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3910 39449a05 2020-07-23 stsp if (target_len == -1)
3911 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3912 39449a05 2020-07-23 stsp } else {
3913 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3914 39449a05 2020-07-23 stsp if (target_len == -1)
3915 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3916 39449a05 2020-07-23 stsp }
3917 39449a05 2020-07-23 stsp
3918 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3919 39449a05 2020-07-23 stsp if (*fd == -1)
3920 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3921 39449a05 2020-07-23 stsp
3922 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3923 39449a05 2020-07-23 stsp if (outlen == -1) {
3924 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3925 39449a05 2020-07-23 stsp goto done;
3926 39449a05 2020-07-23 stsp }
3927 39449a05 2020-07-23 stsp
3928 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3929 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3930 39449a05 2020-07-23 stsp goto done;
3931 39449a05 2020-07-23 stsp }
3932 39449a05 2020-07-23 stsp done:
3933 39449a05 2020-07-23 stsp if (err) {
3934 39449a05 2020-07-23 stsp close(*fd);
3935 39449a05 2020-07-23 stsp *fd = -1;
3936 39449a05 2020-07-23 stsp }
3937 39449a05 2020-07-23 stsp return err;
3938 39449a05 2020-07-23 stsp }
3939 b72f483a 2019-02-05 stsp
3940 4ed7e80c 2018-05-20 stsp static const struct got_error *
3941 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3942 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3943 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3944 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3945 b72f483a 2019-02-05 stsp {
3946 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3947 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3948 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3949 12463d8b 2019-12-13 stsp int fd = -1;
3950 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3951 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3952 b72f483a 2019-02-05 stsp struct stat sb;
3953 b72f483a 2019-02-05 stsp
3954 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3955 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3956 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3957 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3958 98eaaa12 2019-08-03 stsp return NULL;
3959 98eaaa12 2019-08-03 stsp } else {
3960 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3961 98eaaa12 2019-08-03 stsp return NULL;
3962 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3963 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3964 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3965 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3966 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3967 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3968 98eaaa12 2019-08-03 stsp return NULL;
3969 98eaaa12 2019-08-03 stsp }
3970 3fc0c068 2019-02-10 stsp
3971 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3972 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3973 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3974 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3975 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3976 3fc0c068 2019-02-10 stsp }
3977 b72f483a 2019-02-05 stsp
3978 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3979 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3980 98eaaa12 2019-08-03 stsp switch (staged_status) {
3981 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3982 98eaaa12 2019-08-03 stsp label1 = path;
3983 98eaaa12 2019-08-03 stsp label2 = path;
3984 98eaaa12 2019-08-03 stsp break;
3985 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3986 98eaaa12 2019-08-03 stsp label2 = path;
3987 98eaaa12 2019-08-03 stsp break;
3988 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3989 98eaaa12 2019-08-03 stsp label1 = path;
3990 98eaaa12 2019-08-03 stsp break;
3991 98eaaa12 2019-08-03 stsp default:
3992 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3993 98eaaa12 2019-08-03 stsp }
3994 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3995 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3996 63035f9f 2019-10-06 stsp a->repo, stdout);
3997 98eaaa12 2019-08-03 stsp }
3998 98eaaa12 2019-08-03 stsp
3999 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4000 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4001 4ce46740 2019-08-08 stsp char *id_str;
4002 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4003 408b4ebc 2019-08-03 stsp 8192);
4004 4ce46740 2019-08-08 stsp if (err)
4005 4ce46740 2019-08-08 stsp goto done;
4006 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4007 4ce46740 2019-08-08 stsp if (err)
4008 4ce46740 2019-08-08 stsp goto done;
4009 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4010 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4011 4ce46740 2019-08-08 stsp free(id_str);
4012 4ce46740 2019-08-08 stsp goto done;
4013 4ce46740 2019-08-08 stsp }
4014 4ce46740 2019-08-08 stsp free(id_str);
4015 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4016 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4017 4ce46740 2019-08-08 stsp if (err)
4018 4ce46740 2019-08-08 stsp goto done;
4019 4ce46740 2019-08-08 stsp }
4020 d00136be 2019-03-26 stsp
4021 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4022 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4023 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4024 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4025 2ec1f75b 2019-03-26 stsp goto done;
4026 2ec1f75b 2019-03-26 stsp }
4027 b72f483a 2019-02-05 stsp
4028 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4029 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4030 12463d8b 2019-12-13 stsp if (fd == -1) {
4031 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4032 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4033 39449a05 2020-07-23 stsp abspath);
4034 39449a05 2020-07-23 stsp goto done;
4035 39449a05 2020-07-23 stsp }
4036 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4037 39449a05 2020-07-23 stsp de_name, abspath);
4038 39449a05 2020-07-23 stsp if (err)
4039 39449a05 2020-07-23 stsp goto done;
4040 12463d8b 2019-12-13 stsp }
4041 12463d8b 2019-12-13 stsp } else {
4042 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4043 12463d8b 2019-12-13 stsp if (fd == -1) {
4044 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4045 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4046 39449a05 2020-07-23 stsp abspath);
4047 39449a05 2020-07-23 stsp goto done;
4048 39449a05 2020-07-23 stsp }
4049 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4050 39449a05 2020-07-23 stsp de_name, abspath);
4051 39449a05 2020-07-23 stsp if (err)
4052 39449a05 2020-07-23 stsp goto done;
4053 12463d8b 2019-12-13 stsp }
4054 12463d8b 2019-12-13 stsp }
4055 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4056 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4057 2ec1f75b 2019-03-26 stsp goto done;
4058 2ec1f75b 2019-03-26 stsp }
4059 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4060 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4061 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4062 2ec1f75b 2019-03-26 stsp goto done;
4063 2ec1f75b 2019-03-26 stsp }
4064 12463d8b 2019-12-13 stsp fd = -1;
4065 2ec1f75b 2019-03-26 stsp } else
4066 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4067 b72f483a 2019-02-05 stsp
4068 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4069 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
4070 b72f483a 2019-02-05 stsp done:
4071 b72f483a 2019-02-05 stsp if (blob1)
4072 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4073 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4074 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4075 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4076 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4077 b72f483a 2019-02-05 stsp free(abspath);
4078 927df6b7 2019-02-10 stsp return err;
4079 927df6b7 2019-02-10 stsp }
4080 927df6b7 2019-02-10 stsp
4081 927df6b7 2019-02-10 stsp static const struct got_error *
4082 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4083 b00d56cd 2018-04-01 stsp {
4084 b00d56cd 2018-04-01 stsp const struct got_error *error;
4085 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4086 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4087 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4088 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4089 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4090 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4091 15a94983 2018-12-23 stsp int type1, type2;
4092 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4093 c0cc5c62 2018-10-18 stsp const char *errstr;
4094 927df6b7 2019-02-10 stsp char *path = NULL;
4095 b00d56cd 2018-04-01 stsp
4096 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4097 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4098 25eccc22 2019-01-04 stsp NULL) == -1)
4099 b00d56cd 2018-04-01 stsp err(1, "pledge");
4100 b00d56cd 2018-04-01 stsp #endif
4101 b00d56cd 2018-04-01 stsp
4102 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
4103 b00d56cd 2018-04-01 stsp switch (ch) {
4104 c0cc5c62 2018-10-18 stsp case 'C':
4105 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4106 dfcab68b 2019-11-29 kn &errstr);
4107 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4108 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4109 c0cc5c62 2018-10-18 stsp break;
4110 b72f483a 2019-02-05 stsp case 'r':
4111 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4112 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4113 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4114 9ba1d308 2019-10-21 stsp optarg);
4115 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4116 b72f483a 2019-02-05 stsp break;
4117 98eaaa12 2019-08-03 stsp case 's':
4118 98eaaa12 2019-08-03 stsp diff_staged = 1;
4119 63035f9f 2019-10-06 stsp break;
4120 63035f9f 2019-10-06 stsp case 'w':
4121 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4122 98eaaa12 2019-08-03 stsp break;
4123 b00d56cd 2018-04-01 stsp default:
4124 2deda0b9 2019-03-07 stsp usage_diff();
4125 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4126 b00d56cd 2018-04-01 stsp }
4127 b00d56cd 2018-04-01 stsp }
4128 b00d56cd 2018-04-01 stsp
4129 b00d56cd 2018-04-01 stsp argc -= optind;
4130 b00d56cd 2018-04-01 stsp argv += optind;
4131 b00d56cd 2018-04-01 stsp
4132 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4133 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4134 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4135 b72f483a 2019-02-05 stsp goto done;
4136 b72f483a 2019-02-05 stsp }
4137 927df6b7 2019-02-10 stsp if (argc <= 1) {
4138 b72f483a 2019-02-05 stsp if (repo_path)
4139 b72f483a 2019-02-05 stsp errx(1,
4140 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4141 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4142 fa51e947 2020-03-27 stsp if (error) {
4143 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4144 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4145 fa51e947 2020-03-27 stsp cwd);
4146 1f03b8da 2020-03-20 stsp goto done;
4147 fa51e947 2020-03-27 stsp }
4148 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4149 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4150 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4151 927df6b7 2019-02-10 stsp goto done;
4152 927df6b7 2019-02-10 stsp }
4153 927df6b7 2019-02-10 stsp if (argc == 1) {
4154 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4155 cbd1af7a 2019-03-18 stsp argv[0]);
4156 927df6b7 2019-02-10 stsp if (error)
4157 927df6b7 2019-02-10 stsp goto done;
4158 927df6b7 2019-02-10 stsp } else {
4159 927df6b7 2019-02-10 stsp path = strdup("");
4160 927df6b7 2019-02-10 stsp if (path == NULL) {
4161 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4162 927df6b7 2019-02-10 stsp goto done;
4163 927df6b7 2019-02-10 stsp }
4164 927df6b7 2019-02-10 stsp }
4165 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4166 98eaaa12 2019-08-03 stsp if (diff_staged)
4167 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4168 98eaaa12 2019-08-03 stsp "objects in repository");
4169 15a94983 2018-12-23 stsp id_str1 = argv[0];
4170 15a94983 2018-12-23 stsp id_str2 = argv[1];
4171 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4172 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4173 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4174 30db809c 2019-06-05 stsp goto done;
4175 1f03b8da 2020-03-20 stsp if (worktree) {
4176 1f03b8da 2020-03-20 stsp repo_path = strdup(
4177 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4178 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4179 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4180 1f03b8da 2020-03-20 stsp goto done;
4181 1f03b8da 2020-03-20 stsp }
4182 1f03b8da 2020-03-20 stsp } else {
4183 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4184 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4185 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4186 1f03b8da 2020-03-20 stsp goto done;
4187 1f03b8da 2020-03-20 stsp }
4188 30db809c 2019-06-05 stsp }
4189 30db809c 2019-06-05 stsp }
4190 b00d56cd 2018-04-01 stsp } else
4191 b00d56cd 2018-04-01 stsp usage_diff();
4192 b00d56cd 2018-04-01 stsp
4193 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4194 76089277 2018-04-01 stsp free(repo_path);
4195 b00d56cd 2018-04-01 stsp if (error != NULL)
4196 b00d56cd 2018-04-01 stsp goto done;
4197 b00d56cd 2018-04-01 stsp
4198 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4199 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4200 c02c541e 2019-03-29 stsp if (error)
4201 c02c541e 2019-03-29 stsp goto done;
4202 c02c541e 2019-03-29 stsp
4203 30db809c 2019-06-05 stsp if (argc <= 1) {
4204 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4205 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4206 b72f483a 2019-02-05 stsp char *id_str;
4207 72ea6654 2019-07-27 stsp
4208 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4209 72ea6654 2019-07-27 stsp
4210 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4211 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4212 b72f483a 2019-02-05 stsp if (error)
4213 b72f483a 2019-02-05 stsp goto done;
4214 b72f483a 2019-02-05 stsp arg.repo = repo;
4215 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4216 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4217 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4218 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4219 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4220 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4221 b72f483a 2019-02-05 stsp
4222 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4223 72ea6654 2019-07-27 stsp if (error)
4224 72ea6654 2019-07-27 stsp goto done;
4225 72ea6654 2019-07-27 stsp
4226 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4227 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4228 b72f483a 2019-02-05 stsp free(id_str);
4229 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4230 b72f483a 2019-02-05 stsp goto done;
4231 b72f483a 2019-02-05 stsp }
4232 b72f483a 2019-02-05 stsp
4233 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4234 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4235 0adb7196 2019-08-11 stsp if (error)
4236 0adb7196 2019-08-11 stsp goto done;
4237 b00d56cd 2018-04-01 stsp
4238 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4239 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4240 0adb7196 2019-08-11 stsp if (error)
4241 0adb7196 2019-08-11 stsp goto done;
4242 b00d56cd 2018-04-01 stsp
4243 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4244 15a94983 2018-12-23 stsp if (error)
4245 15a94983 2018-12-23 stsp goto done;
4246 15a94983 2018-12-23 stsp
4247 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4248 15a94983 2018-12-23 stsp if (error)
4249 15a94983 2018-12-23 stsp goto done;
4250 15a94983 2018-12-23 stsp
4251 15a94983 2018-12-23 stsp if (type1 != type2) {
4252 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4253 b00d56cd 2018-04-01 stsp goto done;
4254 b00d56cd 2018-04-01 stsp }
4255 b00d56cd 2018-04-01 stsp
4256 15a94983 2018-12-23 stsp switch (type1) {
4257 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4258 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4259 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4260 b00d56cd 2018-04-01 stsp break;
4261 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4262 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4263 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4264 b00d56cd 2018-04-01 stsp break;
4265 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4266 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4267 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4268 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
4269 b00d56cd 2018-04-01 stsp break;
4270 b00d56cd 2018-04-01 stsp default:
4271 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4272 b00d56cd 2018-04-01 stsp }
4273 b00d56cd 2018-04-01 stsp done:
4274 5e70831e 2019-05-28 stsp free(label1);
4275 5e70831e 2019-05-28 stsp free(label2);
4276 15a94983 2018-12-23 stsp free(id1);
4277 15a94983 2018-12-23 stsp free(id2);
4278 927df6b7 2019-02-10 stsp free(path);
4279 b72f483a 2019-02-05 stsp if (worktree)
4280 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4281 ad242220 2018-09-08 stsp if (repo) {
4282 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4283 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4284 ad242220 2018-09-08 stsp if (error == NULL)
4285 ad242220 2018-09-08 stsp error = repo_error;
4286 ad242220 2018-09-08 stsp }
4287 b00d56cd 2018-04-01 stsp return error;
4288 404c43c4 2018-06-21 stsp }
4289 404c43c4 2018-06-21 stsp
4290 404c43c4 2018-06-21 stsp __dead static void
4291 404c43c4 2018-06-21 stsp usage_blame(void)
4292 404c43c4 2018-06-21 stsp {
4293 9270e621 2019-02-05 stsp fprintf(stderr,
4294 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4295 404c43c4 2018-06-21 stsp getprogname());
4296 404c43c4 2018-06-21 stsp exit(1);
4297 b00d56cd 2018-04-01 stsp }
4298 b00d56cd 2018-04-01 stsp
4299 28315671 2019-08-14 stsp struct blame_line {
4300 28315671 2019-08-14 stsp int annotated;
4301 28315671 2019-08-14 stsp char *id_str;
4302 82f6abb8 2019-08-14 stsp char *committer;
4303 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4304 28315671 2019-08-14 stsp };
4305 28315671 2019-08-14 stsp
4306 28315671 2019-08-14 stsp struct blame_cb_args {
4307 28315671 2019-08-14 stsp struct blame_line *lines;
4308 28315671 2019-08-14 stsp int nlines;
4309 7ef28ff8 2019-08-14 stsp int nlines_prec;
4310 28315671 2019-08-14 stsp int lineno_cur;
4311 28315671 2019-08-14 stsp off_t *line_offsets;
4312 28315671 2019-08-14 stsp FILE *f;
4313 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4314 28315671 2019-08-14 stsp };
4315 28315671 2019-08-14 stsp
4316 404c43c4 2018-06-21 stsp static const struct got_error *
4317 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4318 28315671 2019-08-14 stsp {
4319 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4320 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4321 28315671 2019-08-14 stsp struct blame_line *bline;
4322 82f6abb8 2019-08-14 stsp char *line = NULL;
4323 28315671 2019-08-14 stsp size_t linesize = 0;
4324 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4325 28315671 2019-08-14 stsp off_t offset;
4326 bcb49d15 2019-08-14 stsp struct tm tm;
4327 bcb49d15 2019-08-14 stsp time_t committer_time;
4328 28315671 2019-08-14 stsp
4329 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4330 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4331 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4332 28315671 2019-08-14 stsp
4333 28315671 2019-08-14 stsp if (sigint_received)
4334 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4335 28315671 2019-08-14 stsp
4336 2839f8b9 2019-08-18 stsp if (lineno == -1)
4337 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4338 2839f8b9 2019-08-18 stsp
4339 28315671 2019-08-14 stsp /* Annotate this line. */
4340 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4341 28315671 2019-08-14 stsp if (bline->annotated)
4342 28315671 2019-08-14 stsp return NULL;
4343 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4344 28315671 2019-08-14 stsp if (err)
4345 28315671 2019-08-14 stsp return err;
4346 82f6abb8 2019-08-14 stsp
4347 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4348 82f6abb8 2019-08-14 stsp if (err)
4349 82f6abb8 2019-08-14 stsp goto done;
4350 82f6abb8 2019-08-14 stsp
4351 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4352 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4353 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4354 bcb49d15 2019-08-14 stsp goto done;
4355 bcb49d15 2019-08-14 stsp }
4356 bcb49d15 2019-08-14 stsp
4357 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4358 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4359 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4360 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4361 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4362 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4363 82f6abb8 2019-08-14 stsp goto done;
4364 82f6abb8 2019-08-14 stsp }
4365 28315671 2019-08-14 stsp bline->annotated = 1;
4366 28315671 2019-08-14 stsp
4367 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4368 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4369 28315671 2019-08-14 stsp if (!bline->annotated)
4370 82f6abb8 2019-08-14 stsp goto done;
4371 28315671 2019-08-14 stsp
4372 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4373 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4374 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4375 82f6abb8 2019-08-14 stsp goto done;
4376 82f6abb8 2019-08-14 stsp }
4377 28315671 2019-08-14 stsp
4378 28315671 2019-08-14 stsp while (bline->annotated) {
4379 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4380 82f6abb8 2019-08-14 stsp size_t len;
4381 82f6abb8 2019-08-14 stsp
4382 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4383 28315671 2019-08-14 stsp if (ferror(a->f))
4384 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4385 28315671 2019-08-14 stsp break;
4386 28315671 2019-08-14 stsp }
4387 82f6abb8 2019-08-14 stsp
4388 82f6abb8 2019-08-14 stsp committer = bline->committer;
4389 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4390 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4391 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4392 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4393 82f6abb8 2019-08-14 stsp if (at)
4394 82f6abb8 2019-08-14 stsp *at = '\0';
4395 82f6abb8 2019-08-14 stsp len = strlen(committer);
4396 82f6abb8 2019-08-14 stsp if (len >= 9)
4397 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4398 28315671 2019-08-14 stsp
4399 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4400 28315671 2019-08-14 stsp if (nl)
4401 28315671 2019-08-14 stsp *nl = '\0';
4402 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4403 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4404 28315671 2019-08-14 stsp
4405 28315671 2019-08-14 stsp a->lineno_cur++;
4406 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4407 28315671 2019-08-14 stsp }
4408 82f6abb8 2019-08-14 stsp done:
4409 82f6abb8 2019-08-14 stsp if (commit)
4410 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4411 28315671 2019-08-14 stsp free(line);
4412 28315671 2019-08-14 stsp return err;
4413 28315671 2019-08-14 stsp }
4414 28315671 2019-08-14 stsp
4415 28315671 2019-08-14 stsp static const struct got_error *
4416 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4417 404c43c4 2018-06-21 stsp {
4418 404c43c4 2018-06-21 stsp const struct got_error *error;
4419 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4420 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4421 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4422 0587e10c 2020-07-23 stsp char *link_target = NULL;
4423 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4424 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4425 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4426 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4427 28315671 2019-08-14 stsp struct blame_cb_args bca;
4428 28315671 2019-08-14 stsp int ch, obj_type, i;
4429 b02560ec 2019-08-19 stsp size_t filesize;
4430 90f3c347 2019-08-19 stsp
4431 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4432 404c43c4 2018-06-21 stsp
4433 404c43c4 2018-06-21 stsp #ifndef PROFILE
4434 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4435 36e2fb66 2019-01-04 stsp NULL) == -1)
4436 404c43c4 2018-06-21 stsp err(1, "pledge");
4437 404c43c4 2018-06-21 stsp #endif
4438 404c43c4 2018-06-21 stsp
4439 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4440 404c43c4 2018-06-21 stsp switch (ch) {
4441 404c43c4 2018-06-21 stsp case 'c':
4442 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4443 404c43c4 2018-06-21 stsp break;
4444 66bea077 2018-08-02 stsp case 'r':
4445 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4446 66bea077 2018-08-02 stsp if (repo_path == NULL)
4447 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4448 9ba1d308 2019-10-21 stsp optarg);
4449 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4450 66bea077 2018-08-02 stsp break;
4451 404c43c4 2018-06-21 stsp default:
4452 2deda0b9 2019-03-07 stsp usage_blame();
4453 404c43c4 2018-06-21 stsp /* NOTREACHED */
4454 404c43c4 2018-06-21 stsp }
4455 404c43c4 2018-06-21 stsp }
4456 404c43c4 2018-06-21 stsp
4457 404c43c4 2018-06-21 stsp argc -= optind;
4458 404c43c4 2018-06-21 stsp argv += optind;
4459 404c43c4 2018-06-21 stsp
4460 a39318fd 2018-08-02 stsp if (argc == 1)
4461 404c43c4 2018-06-21 stsp path = argv[0];
4462 a39318fd 2018-08-02 stsp else
4463 404c43c4 2018-06-21 stsp usage_blame();
4464 404c43c4 2018-06-21 stsp
4465 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4466 66bea077 2018-08-02 stsp if (cwd == NULL) {
4467 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4468 66bea077 2018-08-02 stsp goto done;
4469 66bea077 2018-08-02 stsp }
4470 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4471 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4472 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4473 66bea077 2018-08-02 stsp goto done;
4474 0c06baac 2019-02-05 stsp else
4475 0c06baac 2019-02-05 stsp error = NULL;
4476 0c06baac 2019-02-05 stsp if (worktree) {
4477 0c06baac 2019-02-05 stsp repo_path =
4478 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4479 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4480 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4481 1f03b8da 2020-03-20 stsp if (error)
4482 1f03b8da 2020-03-20 stsp goto done;
4483 1f03b8da 2020-03-20 stsp }
4484 0c06baac 2019-02-05 stsp } else {
4485 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4486 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4487 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4488 0c06baac 2019-02-05 stsp goto done;
4489 0c06baac 2019-02-05 stsp }
4490 66bea077 2018-08-02 stsp }
4491 66bea077 2018-08-02 stsp }
4492 36e2fb66 2019-01-04 stsp
4493 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4494 c02c541e 2019-03-29 stsp if (error != NULL)
4495 36e2fb66 2019-01-04 stsp goto done;
4496 66bea077 2018-08-02 stsp
4497 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4498 c02c541e 2019-03-29 stsp if (error)
4499 404c43c4 2018-06-21 stsp goto done;
4500 404c43c4 2018-06-21 stsp
4501 0c06baac 2019-02-05 stsp if (worktree) {
4502 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4503 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4504 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4505 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4506 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4507 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4508 6efaaa2d 2019-02-05 stsp path) == -1) {
4509 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4510 0c06baac 2019-02-05 stsp goto done;
4511 0c06baac 2019-02-05 stsp }
4512 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4513 0c06baac 2019-02-05 stsp free(p);
4514 0c06baac 2019-02-05 stsp } else {
4515 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4516 0c06baac 2019-02-05 stsp }
4517 0c06baac 2019-02-05 stsp if (error)
4518 66bea077 2018-08-02 stsp goto done;
4519 66bea077 2018-08-02 stsp
4520 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4521 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4522 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4523 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4524 404c43c4 2018-06-21 stsp if (error != NULL)
4525 66bea077 2018-08-02 stsp goto done;
4526 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4527 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4528 404c43c4 2018-06-21 stsp if (error != NULL)
4529 66bea077 2018-08-02 stsp goto done;
4530 404c43c4 2018-06-21 stsp } else {
4531 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4532 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4533 30837e32 2019-07-25 stsp if (error)
4534 66bea077 2018-08-02 stsp goto done;
4535 404c43c4 2018-06-21 stsp }
4536 404c43c4 2018-06-21 stsp
4537 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4538 0587e10c 2020-07-23 stsp commit_id, repo);
4539 0587e10c 2020-07-23 stsp if (error)
4540 0587e10c 2020-07-23 stsp goto done;
4541 0587e10c 2020-07-23 stsp
4542 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4543 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4544 28315671 2019-08-14 stsp if (error)
4545 28315671 2019-08-14 stsp goto done;
4546 28315671 2019-08-14 stsp
4547 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4548 28315671 2019-08-14 stsp if (error)
4549 28315671 2019-08-14 stsp goto done;
4550 28315671 2019-08-14 stsp
4551 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4552 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4553 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4554 28315671 2019-08-14 stsp goto done;
4555 28315671 2019-08-14 stsp }
4556 28315671 2019-08-14 stsp
4557 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4558 28315671 2019-08-14 stsp if (error)
4559 28315671 2019-08-14 stsp goto done;
4560 28315671 2019-08-14 stsp bca.f = got_opentemp();
4561 28315671 2019-08-14 stsp if (bca.f == NULL) {
4562 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4563 28315671 2019-08-14 stsp goto done;
4564 28315671 2019-08-14 stsp }
4565 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4566 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4567 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4568 28315671 2019-08-14 stsp goto done;
4569 28315671 2019-08-14 stsp
4570 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4571 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4572 b02560ec 2019-08-19 stsp bca.nlines--;
4573 b02560ec 2019-08-19 stsp
4574 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4575 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4576 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4577 28315671 2019-08-14 stsp goto done;
4578 28315671 2019-08-14 stsp }
4579 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4580 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4581 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4582 7ef28ff8 2019-08-14 stsp while (i > 0) {
4583 7ef28ff8 2019-08-14 stsp i /= 10;
4584 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4585 7ef28ff8 2019-08-14 stsp }
4586 82f6abb8 2019-08-14 stsp bca.repo = repo;
4587 7ef28ff8 2019-08-14 stsp
4588 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4589 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4590 404c43c4 2018-06-21 stsp done:
4591 66bea077 2018-08-02 stsp free(in_repo_path);
4592 0587e10c 2020-07-23 stsp free(link_target);
4593 66bea077 2018-08-02 stsp free(repo_path);
4594 66bea077 2018-08-02 stsp free(cwd);
4595 404c43c4 2018-06-21 stsp free(commit_id);
4596 28315671 2019-08-14 stsp free(obj_id);
4597 28315671 2019-08-14 stsp if (blob)
4598 28315671 2019-08-14 stsp got_object_blob_close(blob);
4599 0c06baac 2019-02-05 stsp if (worktree)
4600 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4601 ad242220 2018-09-08 stsp if (repo) {
4602 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4603 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4604 ad242220 2018-09-08 stsp if (error == NULL)
4605 ad242220 2018-09-08 stsp error = repo_error;
4606 ad242220 2018-09-08 stsp }
4607 3affba96 2019-09-22 stsp if (bca.lines) {
4608 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4609 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4610 3affba96 2019-09-22 stsp free(bline->id_str);
4611 3affba96 2019-09-22 stsp free(bline->committer);
4612 3affba96 2019-09-22 stsp }
4613 3affba96 2019-09-22 stsp free(bca.lines);
4614 28315671 2019-08-14 stsp }
4615 28315671 2019-08-14 stsp free(bca.line_offsets);
4616 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4617 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4618 404c43c4 2018-06-21 stsp return error;
4619 5de5890b 2018-10-18 stsp }
4620 5de5890b 2018-10-18 stsp
4621 5de5890b 2018-10-18 stsp __dead static void
4622 5de5890b 2018-10-18 stsp usage_tree(void)
4623 5de5890b 2018-10-18 stsp {
4624 c1669e2e 2019-01-09 stsp fprintf(stderr,
4625 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4626 5de5890b 2018-10-18 stsp getprogname());
4627 5de5890b 2018-10-18 stsp exit(1);
4628 5de5890b 2018-10-18 stsp }
4629 5de5890b 2018-10-18 stsp
4630 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4631 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4632 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4633 c1669e2e 2019-01-09 stsp {
4634 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4635 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4636 848d6979 2019-08-12 stsp const char *modestr = "";
4637 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4638 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4639 5de5890b 2018-10-18 stsp
4640 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4641 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4642 c1669e2e 2019-01-09 stsp path++;
4643 c1669e2e 2019-01-09 stsp
4644 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4645 63c5ca5d 2019-08-24 stsp modestr = "$";
4646 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4647 0d6c6ee3 2020-05-20 stsp int i;
4648 0d6c6ee3 2020-05-20 stsp
4649 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4650 0d6c6ee3 2020-05-20 stsp if (err)
4651 0d6c6ee3 2020-05-20 stsp return err;
4652 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4653 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4654 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4655 0d6c6ee3 2020-05-20 stsp }
4656 0d6c6ee3 2020-05-20 stsp
4657 848d6979 2019-08-12 stsp modestr = "@";
4658 0d6c6ee3 2020-05-20 stsp }
4659 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4660 848d6979 2019-08-12 stsp modestr = "/";
4661 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4662 848d6979 2019-08-12 stsp modestr = "*";
4663 848d6979 2019-08-12 stsp
4664 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4665 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4666 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4667 0d6c6ee3 2020-05-20 stsp
4668 0d6c6ee3 2020-05-20 stsp free(link_target);
4669 0d6c6ee3 2020-05-20 stsp return NULL;
4670 c1669e2e 2019-01-09 stsp }
4671 c1669e2e 2019-01-09 stsp
4672 5de5890b 2018-10-18 stsp static const struct got_error *
4673 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4674 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4675 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4676 5de5890b 2018-10-18 stsp {
4677 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4678 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4679 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4680 56e0773d 2019-11-28 stsp int nentries, i;
4681 5de5890b 2018-10-18 stsp
4682 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4683 5de5890b 2018-10-18 stsp if (err)
4684 5de5890b 2018-10-18 stsp goto done;
4685 5de5890b 2018-10-18 stsp
4686 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4687 5de5890b 2018-10-18 stsp if (err)
4688 5de5890b 2018-10-18 stsp goto done;
4689 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4690 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4691 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4692 5de5890b 2018-10-18 stsp char *id = NULL;
4693 84453469 2018-11-11 stsp
4694 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4695 84453469 2018-11-11 stsp break;
4696 84453469 2018-11-11 stsp
4697 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4698 5de5890b 2018-10-18 stsp if (show_ids) {
4699 5de5890b 2018-10-18 stsp char *id_str;
4700 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4701 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4702 5de5890b 2018-10-18 stsp if (err)
4703 5de5890b 2018-10-18 stsp goto done;
4704 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4705 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4706 5de5890b 2018-10-18 stsp free(id_str);
4707 5de5890b 2018-10-18 stsp goto done;
4708 5de5890b 2018-10-18 stsp }
4709 5de5890b 2018-10-18 stsp free(id_str);
4710 5de5890b 2018-10-18 stsp }
4711 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4712 5de5890b 2018-10-18 stsp free(id);
4713 0d6c6ee3 2020-05-20 stsp if (err)
4714 0d6c6ee3 2020-05-20 stsp goto done;
4715 c1669e2e 2019-01-09 stsp
4716 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4717 c1669e2e 2019-01-09 stsp char *child_path;
4718 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4719 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4720 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4721 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4722 c1669e2e 2019-01-09 stsp goto done;
4723 c1669e2e 2019-01-09 stsp }
4724 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4725 c1669e2e 2019-01-09 stsp root_path, repo);
4726 c1669e2e 2019-01-09 stsp free(child_path);
4727 c1669e2e 2019-01-09 stsp if (err)
4728 c1669e2e 2019-01-09 stsp goto done;
4729 c1669e2e 2019-01-09 stsp }
4730 5de5890b 2018-10-18 stsp }
4731 5de5890b 2018-10-18 stsp done:
4732 5de5890b 2018-10-18 stsp if (tree)
4733 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4734 5de5890b 2018-10-18 stsp free(tree_id);
4735 5de5890b 2018-10-18 stsp return err;
4736 404c43c4 2018-06-21 stsp }
4737 404c43c4 2018-06-21 stsp
4738 5de5890b 2018-10-18 stsp static const struct got_error *
4739 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4740 5de5890b 2018-10-18 stsp {
4741 5de5890b 2018-10-18 stsp const struct got_error *error;
4742 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4743 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4744 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4745 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4746 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4747 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4748 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4749 5de5890b 2018-10-18 stsp int ch;
4750 5de5890b 2018-10-18 stsp
4751 5de5890b 2018-10-18 stsp #ifndef PROFILE
4752 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4753 0f8d269b 2019-01-04 stsp NULL) == -1)
4754 5de5890b 2018-10-18 stsp err(1, "pledge");
4755 5de5890b 2018-10-18 stsp #endif
4756 5de5890b 2018-10-18 stsp
4757 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4758 5de5890b 2018-10-18 stsp switch (ch) {
4759 5de5890b 2018-10-18 stsp case 'c':
4760 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4761 5de5890b 2018-10-18 stsp break;
4762 5de5890b 2018-10-18 stsp case 'r':
4763 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4764 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4765 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4766 9ba1d308 2019-10-21 stsp optarg);
4767 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4768 5de5890b 2018-10-18 stsp break;
4769 5de5890b 2018-10-18 stsp case 'i':
4770 5de5890b 2018-10-18 stsp show_ids = 1;
4771 5de5890b 2018-10-18 stsp break;
4772 c1669e2e 2019-01-09 stsp case 'R':
4773 c1669e2e 2019-01-09 stsp recurse = 1;
4774 c1669e2e 2019-01-09 stsp break;
4775 5de5890b 2018-10-18 stsp default:
4776 2deda0b9 2019-03-07 stsp usage_tree();
4777 5de5890b 2018-10-18 stsp /* NOTREACHED */
4778 5de5890b 2018-10-18 stsp }
4779 5de5890b 2018-10-18 stsp }
4780 5de5890b 2018-10-18 stsp
4781 5de5890b 2018-10-18 stsp argc -= optind;
4782 5de5890b 2018-10-18 stsp argv += optind;
4783 5de5890b 2018-10-18 stsp
4784 5de5890b 2018-10-18 stsp if (argc == 1)
4785 5de5890b 2018-10-18 stsp path = argv[0];
4786 5de5890b 2018-10-18 stsp else if (argc > 1)
4787 5de5890b 2018-10-18 stsp usage_tree();
4788 5de5890b 2018-10-18 stsp else
4789 7a2c19d6 2019-02-05 stsp path = NULL;
4790 7a2c19d6 2019-02-05 stsp
4791 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4792 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4793 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4794 9bf7a39b 2019-02-05 stsp goto done;
4795 9bf7a39b 2019-02-05 stsp }
4796 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4797 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4798 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4799 7a2c19d6 2019-02-05 stsp goto done;
4800 7a2c19d6 2019-02-05 stsp else
4801 7a2c19d6 2019-02-05 stsp error = NULL;
4802 7a2c19d6 2019-02-05 stsp if (worktree) {
4803 7a2c19d6 2019-02-05 stsp repo_path =
4804 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4805 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4806 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4807 7a2c19d6 2019-02-05 stsp if (error)
4808 7a2c19d6 2019-02-05 stsp goto done;
4809 7a2c19d6 2019-02-05 stsp } else {
4810 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4811 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4812 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4813 7a2c19d6 2019-02-05 stsp goto done;
4814 7a2c19d6 2019-02-05 stsp }
4815 5de5890b 2018-10-18 stsp }
4816 5de5890b 2018-10-18 stsp }
4817 5de5890b 2018-10-18 stsp
4818 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4819 5de5890b 2018-10-18 stsp if (error != NULL)
4820 c02c541e 2019-03-29 stsp goto done;
4821 c02c541e 2019-03-29 stsp
4822 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4823 c02c541e 2019-03-29 stsp if (error)
4824 5de5890b 2018-10-18 stsp goto done;
4825 5de5890b 2018-10-18 stsp
4826 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4827 9bf7a39b 2019-02-05 stsp if (worktree) {
4828 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4829 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4830 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4831 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4832 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4833 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4834 9bf7a39b 2019-02-05 stsp goto done;
4835 9bf7a39b 2019-02-05 stsp }
4836 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4837 9bf7a39b 2019-02-05 stsp free(p);
4838 9bf7a39b 2019-02-05 stsp if (error)
4839 9bf7a39b 2019-02-05 stsp goto done;
4840 9bf7a39b 2019-02-05 stsp } else
4841 9bf7a39b 2019-02-05 stsp path = "/";
4842 9bf7a39b 2019-02-05 stsp }
4843 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4844 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4845 9bf7a39b 2019-02-05 stsp if (error != NULL)
4846 9bf7a39b 2019-02-05 stsp goto done;
4847 9bf7a39b 2019-02-05 stsp }
4848 5de5890b 2018-10-18 stsp
4849 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4850 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4851 4e0a20a4 2020-03-23 tracey if (worktree)
4852 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4853 4e0a20a4 2020-03-23 tracey else
4854 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4855 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4856 5de5890b 2018-10-18 stsp if (error != NULL)
4857 5de5890b 2018-10-18 stsp goto done;
4858 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4859 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4860 5de5890b 2018-10-18 stsp if (error != NULL)
4861 5de5890b 2018-10-18 stsp goto done;
4862 5de5890b 2018-10-18 stsp } else {
4863 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4864 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4865 30837e32 2019-07-25 stsp if (error)
4866 5de5890b 2018-10-18 stsp goto done;
4867 5de5890b 2018-10-18 stsp }
4868 5de5890b 2018-10-18 stsp
4869 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4870 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4871 5de5890b 2018-10-18 stsp done:
4872 5de5890b 2018-10-18 stsp free(in_repo_path);
4873 5de5890b 2018-10-18 stsp free(repo_path);
4874 5de5890b 2018-10-18 stsp free(cwd);
4875 5de5890b 2018-10-18 stsp free(commit_id);
4876 7a2c19d6 2019-02-05 stsp if (worktree)
4877 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4878 5de5890b 2018-10-18 stsp if (repo) {
4879 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4880 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4881 5de5890b 2018-10-18 stsp if (error == NULL)
4882 5de5890b 2018-10-18 stsp error = repo_error;
4883 5de5890b 2018-10-18 stsp }
4884 5de5890b 2018-10-18 stsp return error;
4885 5de5890b 2018-10-18 stsp }
4886 5de5890b 2018-10-18 stsp
4887 6bad629b 2019-02-04 stsp __dead static void
4888 6bad629b 2019-02-04 stsp usage_status(void)
4889 6bad629b 2019-02-04 stsp {
4890 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4891 081470ac 2020-08-13 stsp getprogname());
4892 6bad629b 2019-02-04 stsp exit(1);
4893 6bad629b 2019-02-04 stsp }
4894 5c860e29 2018-03-12 stsp
4895 b72f483a 2019-02-05 stsp static const struct got_error *
4896 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4897 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4898 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4899 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4900 6bad629b 2019-02-04 stsp {
4901 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4902 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4903 081470ac 2020-08-13 stsp if (arg) {
4904 081470ac 2020-08-13 stsp char *status_codes = arg;
4905 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
4906 081470ac 2020-08-13 stsp int i;
4907 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4908 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
4909 081470ac 2020-08-13 stsp staged_status == status_codes[i])
4910 081470ac 2020-08-13 stsp break;
4911 081470ac 2020-08-13 stsp }
4912 081470ac 2020-08-13 stsp if (i == ncodes)
4913 081470ac 2020-08-13 stsp return NULL;
4914 081470ac 2020-08-13 stsp }
4915 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4916 b72f483a 2019-02-05 stsp return NULL;
4917 6bad629b 2019-02-04 stsp }
4918 5c860e29 2018-03-12 stsp
4919 6bad629b 2019-02-04 stsp static const struct got_error *
4920 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4921 6bad629b 2019-02-04 stsp {
4922 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4923 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4924 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4925 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
4926 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4927 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4928 081470ac 2020-08-13 stsp int ch, i;
4929 5c860e29 2018-03-12 stsp
4930 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4931 72ea6654 2019-07-27 stsp
4932 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4933 6bad629b 2019-02-04 stsp switch (ch) {
4934 081470ac 2020-08-13 stsp case 's':
4935 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
4936 081470ac 2020-08-13 stsp switch (optarg[i]) {
4937 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
4938 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
4939 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
4940 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
4941 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
4942 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
4943 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
4944 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
4945 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
4946 081470ac 2020-08-13 stsp break;
4947 081470ac 2020-08-13 stsp default:
4948 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
4949 081470ac 2020-08-13 stsp optarg[i]);
4950 081470ac 2020-08-13 stsp }
4951 081470ac 2020-08-13 stsp }
4952 081470ac 2020-08-13 stsp status_codes = optarg;
4953 081470ac 2020-08-13 stsp break;
4954 5c860e29 2018-03-12 stsp default:
4955 2deda0b9 2019-03-07 stsp usage_status();
4956 6bad629b 2019-02-04 stsp /* NOTREACHED */
4957 5c860e29 2018-03-12 stsp }
4958 5c860e29 2018-03-12 stsp }
4959 5c860e29 2018-03-12 stsp
4960 6bad629b 2019-02-04 stsp argc -= optind;
4961 6bad629b 2019-02-04 stsp argv += optind;
4962 5c860e29 2018-03-12 stsp
4963 6bad629b 2019-02-04 stsp #ifndef PROFILE
4964 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4965 6bad629b 2019-02-04 stsp NULL) == -1)
4966 6bad629b 2019-02-04 stsp err(1, "pledge");
4967 f42b1b34 2018-03-12 stsp #endif
4968 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4969 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4970 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4971 927df6b7 2019-02-10 stsp goto done;
4972 927df6b7 2019-02-10 stsp }
4973 927df6b7 2019-02-10 stsp
4974 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4975 fa51e947 2020-03-27 stsp if (error) {
4976 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4977 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4978 a5edda0a 2019-07-27 stsp goto done;
4979 fa51e947 2020-03-27 stsp }
4980 6bad629b 2019-02-04 stsp
4981 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4982 c9956ddf 2019-09-08 stsp NULL);
4983 6bad629b 2019-02-04 stsp if (error != NULL)
4984 6bad629b 2019-02-04 stsp goto done;
4985 6bad629b 2019-02-04 stsp
4986 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4987 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4988 087fb88c 2019-08-04 stsp if (error)
4989 087fb88c 2019-08-04 stsp goto done;
4990 087fb88c 2019-08-04 stsp
4991 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4992 6bad629b 2019-02-04 stsp if (error)
4993 6bad629b 2019-02-04 stsp goto done;
4994 6bad629b 2019-02-04 stsp
4995 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4996 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
4997 6bad629b 2019-02-04 stsp done:
4998 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4999 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5000 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5001 927df6b7 2019-02-10 stsp free(cwd);
5002 d0eebce4 2019-03-11 stsp return error;
5003 d0eebce4 2019-03-11 stsp }
5004 d0eebce4 2019-03-11 stsp
5005 d0eebce4 2019-03-11 stsp __dead static void
5006 d0eebce4 2019-03-11 stsp usage_ref(void)
5007 d0eebce4 2019-03-11 stsp {
5008 d0eebce4 2019-03-11 stsp fprintf(stderr,
5009 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5010 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5011 d0eebce4 2019-03-11 stsp getprogname());
5012 d0eebce4 2019-03-11 stsp exit(1);
5013 d0eebce4 2019-03-11 stsp }
5014 d0eebce4 2019-03-11 stsp
5015 d0eebce4 2019-03-11 stsp static const struct got_error *
5016 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5017 d0eebce4 2019-03-11 stsp {
5018 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5019 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5020 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5021 d0eebce4 2019-03-11 stsp
5022 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
5023 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5024 d0eebce4 2019-03-11 stsp if (err)
5025 d0eebce4 2019-03-11 stsp return err;
5026 d0eebce4 2019-03-11 stsp
5027 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5028 d0eebce4 2019-03-11 stsp char *refstr;
5029 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5030 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5031 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5032 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5033 d0eebce4 2019-03-11 stsp free(refstr);
5034 d0eebce4 2019-03-11 stsp }
5035 d0eebce4 2019-03-11 stsp
5036 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5037 d0eebce4 2019-03-11 stsp return NULL;
5038 d0eebce4 2019-03-11 stsp }
5039 d0eebce4 2019-03-11 stsp
5040 d0eebce4 2019-03-11 stsp static const struct got_error *
5041 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
5042 d0eebce4 2019-03-11 stsp {
5043 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5044 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5045 d0eebce4 2019-03-11 stsp
5046 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5047 d0eebce4 2019-03-11 stsp if (err)
5048 d0eebce4 2019-03-11 stsp return err;
5049 d0eebce4 2019-03-11 stsp
5050 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
5051 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5052 d0eebce4 2019-03-11 stsp return err;
5053 d0eebce4 2019-03-11 stsp }
5054 d0eebce4 2019-03-11 stsp
5055 d0eebce4 2019-03-11 stsp static const struct got_error *
5056 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5057 d0eebce4 2019-03-11 stsp {
5058 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5059 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5060 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5061 d1644381 2019-07-14 stsp
5062 d1644381 2019-07-14 stsp /*
5063 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5064 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5065 d1644381 2019-07-14 stsp * an unintended typo.
5066 d1644381 2019-07-14 stsp */
5067 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5068 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5069 d0eebce4 2019-03-11 stsp
5070 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5071 dd88155e 2019-06-29 stsp repo);
5072 d83d9d5c 2019-05-13 stsp if (err) {
5073 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5074 d0eebce4 2019-03-11 stsp
5075 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5076 d83d9d5c 2019-05-13 stsp return err;
5077 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5078 d83d9d5c 2019-05-13 stsp if (err)
5079 d83d9d5c 2019-05-13 stsp return err;
5080 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5081 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5082 d83d9d5c 2019-05-13 stsp if (err)
5083 d83d9d5c 2019-05-13 stsp return err;
5084 d83d9d5c 2019-05-13 stsp }
5085 d83d9d5c 2019-05-13 stsp
5086 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5087 d0eebce4 2019-03-11 stsp if (err)
5088 d0eebce4 2019-03-11 stsp goto done;
5089 d0eebce4 2019-03-11 stsp
5090 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5091 d0eebce4 2019-03-11 stsp done:
5092 d0eebce4 2019-03-11 stsp if (ref)
5093 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5094 d0eebce4 2019-03-11 stsp free(id);
5095 d1c1ae5f 2019-08-12 stsp return err;
5096 d1c1ae5f 2019-08-12 stsp }
5097 d1c1ae5f 2019-08-12 stsp
5098 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5099 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5100 d1c1ae5f 2019-08-12 stsp {
5101 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5102 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5103 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5104 d1c1ae5f 2019-08-12 stsp
5105 d1c1ae5f 2019-08-12 stsp /*
5106 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5107 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5108 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5109 d1c1ae5f 2019-08-12 stsp */
5110 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5111 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5112 d1c1ae5f 2019-08-12 stsp
5113 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5114 d1c1ae5f 2019-08-12 stsp if (err)
5115 d1c1ae5f 2019-08-12 stsp return err;
5116 d1c1ae5f 2019-08-12 stsp
5117 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5118 d1c1ae5f 2019-08-12 stsp if (err)
5119 d1c1ae5f 2019-08-12 stsp goto done;
5120 d1c1ae5f 2019-08-12 stsp
5121 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5122 d1c1ae5f 2019-08-12 stsp done:
5123 d1c1ae5f 2019-08-12 stsp if (target_ref)
5124 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5125 d1c1ae5f 2019-08-12 stsp if (ref)
5126 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5127 d0eebce4 2019-03-11 stsp return err;
5128 d0eebce4 2019-03-11 stsp }
5129 d0eebce4 2019-03-11 stsp
5130 d0eebce4 2019-03-11 stsp static const struct got_error *
5131 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5132 d0eebce4 2019-03-11 stsp {
5133 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5134 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5135 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5136 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5137 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5138 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5139 b2070a3f 2020-03-22 stsp char *refname = NULL;
5140 d0eebce4 2019-03-11 stsp
5141 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5142 d0eebce4 2019-03-11 stsp switch (ch) {
5143 e31abbf2 2020-03-22 stsp case 'c':
5144 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5145 e31abbf2 2020-03-22 stsp break;
5146 d0eebce4 2019-03-11 stsp case 'd':
5147 e31abbf2 2020-03-22 stsp do_delete = 1;
5148 d0eebce4 2019-03-11 stsp break;
5149 d0eebce4 2019-03-11 stsp case 'r':
5150 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5151 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5152 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5153 9ba1d308 2019-10-21 stsp optarg);
5154 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5155 d0eebce4 2019-03-11 stsp break;
5156 d0eebce4 2019-03-11 stsp case 'l':
5157 d0eebce4 2019-03-11 stsp do_list = 1;
5158 d1c1ae5f 2019-08-12 stsp break;
5159 d1c1ae5f 2019-08-12 stsp case 's':
5160 e31abbf2 2020-03-22 stsp symref_target = optarg;
5161 d0eebce4 2019-03-11 stsp break;
5162 d0eebce4 2019-03-11 stsp default:
5163 d0eebce4 2019-03-11 stsp usage_ref();
5164 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5165 d0eebce4 2019-03-11 stsp }
5166 d0eebce4 2019-03-11 stsp }
5167 d0eebce4 2019-03-11 stsp
5168 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5169 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
5170 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
5171 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
5172 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5173 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
5174 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5175 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
5176 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5177 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
5178 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5179 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
5180 d0eebce4 2019-03-11 stsp
5181 d0eebce4 2019-03-11 stsp argc -= optind;
5182 d0eebce4 2019-03-11 stsp argv += optind;
5183 d0eebce4 2019-03-11 stsp
5184 e31abbf2 2020-03-22 stsp if (do_list) {
5185 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5186 d0eebce4 2019-03-11 stsp usage_ref();
5187 b2070a3f 2020-03-22 stsp if (argc == 1) {
5188 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5189 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5190 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5191 b2070a3f 2020-03-22 stsp goto done;
5192 b2070a3f 2020-03-22 stsp }
5193 b2070a3f 2020-03-22 stsp }
5194 e31abbf2 2020-03-22 stsp } else {
5195 e31abbf2 2020-03-22 stsp if (argc != 1)
5196 e31abbf2 2020-03-22 stsp usage_ref();
5197 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5198 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5199 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5200 b2070a3f 2020-03-22 stsp goto done;
5201 b2070a3f 2020-03-22 stsp }
5202 e31abbf2 2020-03-22 stsp }
5203 b2070a3f 2020-03-22 stsp
5204 b2070a3f 2020-03-22 stsp if (refname)
5205 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5206 d0eebce4 2019-03-11 stsp
5207 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5208 e0b57350 2019-03-12 stsp if (do_list) {
5209 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5210 e0b57350 2019-03-12 stsp NULL) == -1)
5211 e0b57350 2019-03-12 stsp err(1, "pledge");
5212 e0b57350 2019-03-12 stsp } else {
5213 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5214 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5215 e0b57350 2019-03-12 stsp err(1, "pledge");
5216 e0b57350 2019-03-12 stsp }
5217 d0eebce4 2019-03-11 stsp #endif
5218 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5219 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5220 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5221 d0eebce4 2019-03-11 stsp goto done;
5222 d0eebce4 2019-03-11 stsp }
5223 d0eebce4 2019-03-11 stsp
5224 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5225 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5226 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5227 d0eebce4 2019-03-11 stsp goto done;
5228 d0eebce4 2019-03-11 stsp else
5229 d0eebce4 2019-03-11 stsp error = NULL;
5230 d0eebce4 2019-03-11 stsp if (worktree) {
5231 d0eebce4 2019-03-11 stsp repo_path =
5232 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5233 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5234 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5235 d0eebce4 2019-03-11 stsp if (error)
5236 d0eebce4 2019-03-11 stsp goto done;
5237 d0eebce4 2019-03-11 stsp } else {
5238 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5239 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5240 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5241 d0eebce4 2019-03-11 stsp goto done;
5242 d0eebce4 2019-03-11 stsp }
5243 d0eebce4 2019-03-11 stsp }
5244 d0eebce4 2019-03-11 stsp }
5245 d0eebce4 2019-03-11 stsp
5246 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5247 d0eebce4 2019-03-11 stsp if (error != NULL)
5248 d0eebce4 2019-03-11 stsp goto done;
5249 d0eebce4 2019-03-11 stsp
5250 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5251 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5252 c02c541e 2019-03-29 stsp if (error)
5253 c02c541e 2019-03-29 stsp goto done;
5254 c02c541e 2019-03-29 stsp
5255 d0eebce4 2019-03-11 stsp if (do_list)
5256 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5257 e31abbf2 2020-03-22 stsp else if (do_delete)
5258 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5259 e31abbf2 2020-03-22 stsp else if (symref_target)
5260 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5261 e31abbf2 2020-03-22 stsp else {
5262 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5263 e31abbf2 2020-03-22 stsp usage_ref();
5264 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5265 e31abbf2 2020-03-22 stsp }
5266 4e759de4 2019-06-26 stsp done:
5267 b2070a3f 2020-03-22 stsp free(refname);
5268 4e759de4 2019-06-26 stsp if (repo)
5269 4e759de4 2019-06-26 stsp got_repo_close(repo);
5270 4e759de4 2019-06-26 stsp if (worktree)
5271 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5272 4e759de4 2019-06-26 stsp free(cwd);
5273 4e759de4 2019-06-26 stsp free(repo_path);
5274 4e759de4 2019-06-26 stsp return error;
5275 4e759de4 2019-06-26 stsp }
5276 4e759de4 2019-06-26 stsp
5277 4e759de4 2019-06-26 stsp __dead static void
5278 4e759de4 2019-06-26 stsp usage_branch(void)
5279 4e759de4 2019-06-26 stsp {
5280 4e759de4 2019-06-26 stsp fprintf(stderr,
5281 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5282 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5283 4e759de4 2019-06-26 stsp exit(1);
5284 4e759de4 2019-06-26 stsp }
5285 4e759de4 2019-06-26 stsp
5286 4e759de4 2019-06-26 stsp static const struct got_error *
5287 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5288 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5289 4e99b47e 2019-10-04 stsp {
5290 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5291 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5292 4e99b47e 2019-10-04 stsp char *refstr;
5293 4e99b47e 2019-10-04 stsp
5294 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5295 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5296 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5297 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5298 4e99b47e 2019-10-04 stsp
5299 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5300 4e99b47e 2019-10-04 stsp if (err)
5301 4e99b47e 2019-10-04 stsp return err;
5302 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5303 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5304 4e99b47e 2019-10-04 stsp marker = "* ";
5305 4e99b47e 2019-10-04 stsp else
5306 4e99b47e 2019-10-04 stsp marker = "~ ";
5307 4e99b47e 2019-10-04 stsp free(id);
5308 4e99b47e 2019-10-04 stsp }
5309 4e99b47e 2019-10-04 stsp
5310 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5311 4e99b47e 2019-10-04 stsp refname += 11;
5312 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5313 4e99b47e 2019-10-04 stsp refname += 18;
5314 4e99b47e 2019-10-04 stsp
5315 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5316 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5317 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5318 4e99b47e 2019-10-04 stsp
5319 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5320 4e99b47e 2019-10-04 stsp free(refstr);
5321 4e99b47e 2019-10-04 stsp return NULL;
5322 4e99b47e 2019-10-04 stsp }
5323 4e99b47e 2019-10-04 stsp
5324 4e99b47e 2019-10-04 stsp static const struct got_error *
5325 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5326 ad89fa31 2019-10-04 stsp {
5327 ad89fa31 2019-10-04 stsp const char *refname;
5328 ad89fa31 2019-10-04 stsp
5329 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5330 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5331 ad89fa31 2019-10-04 stsp
5332 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5333 ad89fa31 2019-10-04 stsp
5334 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5335 ad89fa31 2019-10-04 stsp refname += 11;
5336 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5337 ad89fa31 2019-10-04 stsp refname += 18;
5338 ad89fa31 2019-10-04 stsp
5339 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5340 ad89fa31 2019-10-04 stsp
5341 ad89fa31 2019-10-04 stsp return NULL;
5342 ad89fa31 2019-10-04 stsp }
5343 ad89fa31 2019-10-04 stsp
5344 ad89fa31 2019-10-04 stsp static const struct got_error *
5345 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5346 4e759de4 2019-06-26 stsp {
5347 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5348 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5349 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5350 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5351 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5352 4e759de4 2019-06-26 stsp
5353 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5354 ba882ee3 2019-07-11 stsp
5355 4e99b47e 2019-10-04 stsp if (worktree) {
5356 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5357 4e99b47e 2019-10-04 stsp worktree);
5358 4e99b47e 2019-10-04 stsp if (err)
5359 4e99b47e 2019-10-04 stsp return err;
5360 4e99b47e 2019-10-04 stsp
5361 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5362 4e99b47e 2019-10-04 stsp worktree);
5363 4e99b47e 2019-10-04 stsp if (err)
5364 4e99b47e 2019-10-04 stsp return err;
5365 4e99b47e 2019-10-04 stsp
5366 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5367 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5368 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5369 4e99b47e 2019-10-04 stsp if (err)
5370 4e99b47e 2019-10-04 stsp return err;
5371 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5372 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5373 4e99b47e 2019-10-04 stsp }
5374 4e99b47e 2019-10-04 stsp }
5375 4e99b47e 2019-10-04 stsp
5376 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5377 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5378 4e759de4 2019-06-26 stsp if (err)
5379 4e759de4 2019-06-26 stsp return err;
5380 4e759de4 2019-06-26 stsp
5381 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5382 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5383 4e759de4 2019-06-26 stsp
5384 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5385 4e759de4 2019-06-26 stsp return NULL;
5386 4e759de4 2019-06-26 stsp }
5387 4e759de4 2019-06-26 stsp
5388 4e759de4 2019-06-26 stsp static const struct got_error *
5389 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5390 45cd4e47 2019-08-25 stsp const char *branch_name)
5391 4e759de4 2019-06-26 stsp {
5392 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5393 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5394 4e759de4 2019-06-26 stsp char *refname;
5395 4e759de4 2019-06-26 stsp
5396 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5397 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5398 4e759de4 2019-06-26 stsp
5399 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5400 4e759de4 2019-06-26 stsp if (err)
5401 4e759de4 2019-06-26 stsp goto done;
5402 4e759de4 2019-06-26 stsp
5403 45cd4e47 2019-08-25 stsp if (worktree &&
5404 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5405 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5406 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5407 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5408 45cd4e47 2019-08-25 stsp goto done;
5409 45cd4e47 2019-08-25 stsp }
5410 45cd4e47 2019-08-25 stsp
5411 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5412 4e759de4 2019-06-26 stsp done:
5413 45cd4e47 2019-08-25 stsp if (ref)
5414 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5415 4e759de4 2019-06-26 stsp free(refname);
5416 4e759de4 2019-06-26 stsp return err;
5417 4e759de4 2019-06-26 stsp }
5418 4e759de4 2019-06-26 stsp
5419 4e759de4 2019-06-26 stsp static const struct got_error *
5420 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5421 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5422 4e759de4 2019-06-26 stsp {
5423 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5424 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5425 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5426 d3f84d51 2019-07-11 stsp
5427 d3f84d51 2019-07-11 stsp /*
5428 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5429 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5430 d3f84d51 2019-07-11 stsp * an unintended typo.
5431 d3f84d51 2019-07-11 stsp */
5432 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5433 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5434 4e759de4 2019-06-26 stsp
5435 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5436 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
5437 4e759de4 2019-06-26 stsp goto done;
5438 4e759de4 2019-06-26 stsp }
5439 4e759de4 2019-06-26 stsp
5440 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5441 4e759de4 2019-06-26 stsp if (err == NULL) {
5442 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5443 4e759de4 2019-06-26 stsp goto done;
5444 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5445 4e759de4 2019-06-26 stsp goto done;
5446 4e759de4 2019-06-26 stsp
5447 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5448 4e759de4 2019-06-26 stsp if (err)
5449 4e759de4 2019-06-26 stsp goto done;
5450 4e759de4 2019-06-26 stsp
5451 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5452 d0eebce4 2019-03-11 stsp done:
5453 4e759de4 2019-06-26 stsp if (ref)
5454 4e759de4 2019-06-26 stsp got_ref_close(ref);
5455 4e759de4 2019-06-26 stsp free(base_refname);
5456 4e759de4 2019-06-26 stsp free(refname);
5457 4e759de4 2019-06-26 stsp return err;
5458 4e759de4 2019-06-26 stsp }
5459 4e759de4 2019-06-26 stsp
5460 4e759de4 2019-06-26 stsp static const struct got_error *
5461 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5462 4e759de4 2019-06-26 stsp {
5463 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5464 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5465 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5466 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5467 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5468 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5469 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5470 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5471 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5472 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5473 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5474 4e759de4 2019-06-26 stsp
5475 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5476 da76fce2 2020-02-24 stsp
5477 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5478 4e759de4 2019-06-26 stsp switch (ch) {
5479 a74f7e83 2019-11-10 stsp case 'c':
5480 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5481 a74f7e83 2019-11-10 stsp break;
5482 4e759de4 2019-06-26 stsp case 'd':
5483 4e759de4 2019-06-26 stsp delref = optarg;
5484 4e759de4 2019-06-26 stsp break;
5485 4e759de4 2019-06-26 stsp case 'r':
5486 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5487 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5488 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5489 9ba1d308 2019-10-21 stsp optarg);
5490 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5491 4e759de4 2019-06-26 stsp break;
5492 4e759de4 2019-06-26 stsp case 'l':
5493 4e759de4 2019-06-26 stsp do_list = 1;
5494 da76fce2 2020-02-24 stsp break;
5495 da76fce2 2020-02-24 stsp case 'n':
5496 da76fce2 2020-02-24 stsp do_update = 0;
5497 4e759de4 2019-06-26 stsp break;
5498 4e759de4 2019-06-26 stsp default:
5499 4e759de4 2019-06-26 stsp usage_branch();
5500 4e759de4 2019-06-26 stsp /* NOTREACHED */
5501 4e759de4 2019-06-26 stsp }
5502 4e759de4 2019-06-26 stsp }
5503 4e759de4 2019-06-26 stsp
5504 4e759de4 2019-06-26 stsp if (do_list && delref)
5505 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5506 4e759de4 2019-06-26 stsp
5507 4e759de4 2019-06-26 stsp argc -= optind;
5508 4e759de4 2019-06-26 stsp argv += optind;
5509 ad89fa31 2019-10-04 stsp
5510 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5511 ad89fa31 2019-10-04 stsp do_show = 1;
5512 4e759de4 2019-06-26 stsp
5513 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5514 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5515 a74f7e83 2019-11-10 stsp
5516 4e759de4 2019-06-26 stsp if (do_list || delref) {
5517 4e759de4 2019-06-26 stsp if (argc > 0)
5518 4e759de4 2019-06-26 stsp usage_branch();
5519 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5520 4e759de4 2019-06-26 stsp usage_branch();
5521 4e759de4 2019-06-26 stsp
5522 4e759de4 2019-06-26 stsp #ifndef PROFILE
5523 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5524 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5525 4e759de4 2019-06-26 stsp NULL) == -1)
5526 4e759de4 2019-06-26 stsp err(1, "pledge");
5527 4e759de4 2019-06-26 stsp } else {
5528 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5529 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5530 4e759de4 2019-06-26 stsp err(1, "pledge");
5531 4e759de4 2019-06-26 stsp }
5532 4e759de4 2019-06-26 stsp #endif
5533 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5534 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5535 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5536 4e759de4 2019-06-26 stsp goto done;
5537 4e759de4 2019-06-26 stsp }
5538 4e759de4 2019-06-26 stsp
5539 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5540 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5541 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5542 4e759de4 2019-06-26 stsp goto done;
5543 4e759de4 2019-06-26 stsp else
5544 4e759de4 2019-06-26 stsp error = NULL;
5545 4e759de4 2019-06-26 stsp if (worktree) {
5546 4e759de4 2019-06-26 stsp repo_path =
5547 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5548 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5549 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5550 4e759de4 2019-06-26 stsp if (error)
5551 4e759de4 2019-06-26 stsp goto done;
5552 4e759de4 2019-06-26 stsp } else {
5553 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5554 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5555 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5556 4e759de4 2019-06-26 stsp goto done;
5557 4e759de4 2019-06-26 stsp }
5558 4e759de4 2019-06-26 stsp }
5559 4e759de4 2019-06-26 stsp }
5560 4e759de4 2019-06-26 stsp
5561 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5562 4e759de4 2019-06-26 stsp if (error != NULL)
5563 4e759de4 2019-06-26 stsp goto done;
5564 4e759de4 2019-06-26 stsp
5565 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5566 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5567 4e759de4 2019-06-26 stsp if (error)
5568 4e759de4 2019-06-26 stsp goto done;
5569 4e759de4 2019-06-26 stsp
5570 ad89fa31 2019-10-04 stsp if (do_show)
5571 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5572 ad89fa31 2019-10-04 stsp else if (do_list)
5573 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5574 4e759de4 2019-06-26 stsp else if (delref)
5575 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5576 4e759de4 2019-06-26 stsp else {
5577 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5578 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5579 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5580 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5581 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5582 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5583 a74f7e83 2019-11-10 stsp if (error)
5584 a74f7e83 2019-11-10 stsp goto done;
5585 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5586 da76fce2 2020-02-24 stsp if (error)
5587 da76fce2 2020-02-24 stsp goto done;
5588 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5589 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5590 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5591 da76fce2 2020-02-24 stsp
5592 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5593 da76fce2 2020-02-24 stsp if (error)
5594 da76fce2 2020-02-24 stsp goto done;
5595 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5596 da76fce2 2020-02-24 stsp worktree);
5597 da76fce2 2020-02-24 stsp if (error)
5598 da76fce2 2020-02-24 stsp goto done;
5599 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5600 da76fce2 2020-02-24 stsp == -1) {
5601 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5602 da76fce2 2020-02-24 stsp goto done;
5603 da76fce2 2020-02-24 stsp }
5604 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5605 da76fce2 2020-02-24 stsp free(branch_refname);
5606 da76fce2 2020-02-24 stsp if (error)
5607 da76fce2 2020-02-24 stsp goto done;
5608 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5609 da76fce2 2020-02-24 stsp repo);
5610 da76fce2 2020-02-24 stsp if (error)
5611 da76fce2 2020-02-24 stsp goto done;
5612 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5613 da76fce2 2020-02-24 stsp commit_id);
5614 da76fce2 2020-02-24 stsp if (error)
5615 da76fce2 2020-02-24 stsp goto done;
5616 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5617 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5618 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5619 9627c110 2020-04-18 stsp NULL);
5620 da76fce2 2020-02-24 stsp if (error)
5621 da76fce2 2020-02-24 stsp goto done;
5622 9627c110 2020-04-18 stsp if (upa.did_something)
5623 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5624 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5625 da76fce2 2020-02-24 stsp }
5626 4e759de4 2019-06-26 stsp }
5627 4e759de4 2019-06-26 stsp done:
5628 da76fce2 2020-02-24 stsp if (ref)
5629 da76fce2 2020-02-24 stsp got_ref_close(ref);
5630 d0eebce4 2019-03-11 stsp if (repo)
5631 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5632 d0eebce4 2019-03-11 stsp if (worktree)
5633 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5634 d0eebce4 2019-03-11 stsp free(cwd);
5635 d0eebce4 2019-03-11 stsp free(repo_path);
5636 da76fce2 2020-02-24 stsp free(commit_id);
5637 da76fce2 2020-02-24 stsp free(commit_id_str);
5638 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5639 da76fce2 2020-02-24 stsp free((char *)pe->path);
5640 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5641 d00136be 2019-03-26 stsp return error;
5642 d00136be 2019-03-26 stsp }
5643 d00136be 2019-03-26 stsp
5644 8e7bd50a 2019-08-22 stsp
5645 d00136be 2019-03-26 stsp __dead static void
5646 8e7bd50a 2019-08-22 stsp usage_tag(void)
5647 8e7bd50a 2019-08-22 stsp {
5648 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5649 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5650 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5651 8e7bd50a 2019-08-22 stsp exit(1);
5652 b8bad2ba 2019-08-23 stsp }
5653 b8bad2ba 2019-08-23 stsp
5654 b8bad2ba 2019-08-23 stsp #if 0
5655 b8bad2ba 2019-08-23 stsp static const struct got_error *
5656 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5657 b8bad2ba 2019-08-23 stsp {
5658 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5659 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5660 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5661 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5662 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5663 b8bad2ba 2019-08-23 stsp
5664 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5665 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5666 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5667 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5668 b8bad2ba 2019-08-23 stsp if (err)
5669 b8bad2ba 2019-08-23 stsp return err;
5670 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5671 b8bad2ba 2019-08-23 stsp continue;
5672 b8bad2ba 2019-08-23 stsp } else {
5673 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5674 b8bad2ba 2019-08-23 stsp if (err)
5675 b8bad2ba 2019-08-23 stsp break;
5676 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5677 b8bad2ba 2019-08-23 stsp free(re_id);
5678 b8bad2ba 2019-08-23 stsp if (err)
5679 b8bad2ba 2019-08-23 stsp break;
5680 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5681 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5682 b8bad2ba 2019-08-23 stsp }
5683 b8bad2ba 2019-08-23 stsp
5684 b8bad2ba 2019-08-23 stsp while (se) {
5685 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5686 b8bad2ba 2019-08-23 stsp if (err)
5687 b8bad2ba 2019-08-23 stsp break;
5688 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5689 b8bad2ba 2019-08-23 stsp free(se_id);
5690 b8bad2ba 2019-08-23 stsp if (err)
5691 b8bad2ba 2019-08-23 stsp break;
5692 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5693 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5694 b8bad2ba 2019-08-23 stsp
5695 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5696 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5697 b8bad2ba 2019-08-23 stsp if (err)
5698 b8bad2ba 2019-08-23 stsp return err;
5699 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5700 b8bad2ba 2019-08-23 stsp break;
5701 b8bad2ba 2019-08-23 stsp }
5702 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5703 b8bad2ba 2019-08-23 stsp continue;
5704 b8bad2ba 2019-08-23 stsp }
5705 b8bad2ba 2019-08-23 stsp }
5706 b8bad2ba 2019-08-23 stsp done:
5707 b8bad2ba 2019-08-23 stsp return err;
5708 8e7bd50a 2019-08-22 stsp }
5709 b8bad2ba 2019-08-23 stsp #endif
5710 b8bad2ba 2019-08-23 stsp
5711 b8bad2ba 2019-08-23 stsp static const struct got_error *
5712 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5713 8e7bd50a 2019-08-22 stsp {
5714 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5715 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5716 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5717 8e7bd50a 2019-08-22 stsp
5718 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5719 8e7bd50a 2019-08-22 stsp
5720 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5721 8e7bd50a 2019-08-22 stsp if (err)
5722 8e7bd50a 2019-08-22 stsp return err;
5723 8e7bd50a 2019-08-22 stsp
5724 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5725 8e7bd50a 2019-08-22 stsp const char *refname;
5726 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5727 8e7bd50a 2019-08-22 stsp char datebuf[26];
5728 d4efa91b 2020-01-14 stsp const char *tagger;
5729 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5730 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5731 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5732 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5733 8e7bd50a 2019-08-22 stsp
5734 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5735 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5736 8e7bd50a 2019-08-22 stsp continue;
5737 8e7bd50a 2019-08-22 stsp refname += 10;
5738 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5739 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5740 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5741 8e7bd50a 2019-08-22 stsp break;
5742 8e7bd50a 2019-08-22 stsp }
5743 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5744 8e7bd50a 2019-08-22 stsp free(refstr);
5745 8e7bd50a 2019-08-22 stsp
5746 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5747 8e7bd50a 2019-08-22 stsp if (err)
5748 8e7bd50a 2019-08-22 stsp break;
5749 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5750 d4efa91b 2020-01-14 stsp if (err) {
5751 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5752 d4efa91b 2020-01-14 stsp free(id);
5753 d4efa91b 2020-01-14 stsp break;
5754 d4efa91b 2020-01-14 stsp }
5755 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5756 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5757 d4efa91b 2020-01-14 stsp if (err) {
5758 d4efa91b 2020-01-14 stsp free(id);
5759 d4efa91b 2020-01-14 stsp break;
5760 d4efa91b 2020-01-14 stsp }
5761 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5762 d4efa91b 2020-01-14 stsp tagger_time =
5763 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5764 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5765 d4efa91b 2020-01-14 stsp free(id);
5766 d4efa91b 2020-01-14 stsp if (err)
5767 d4efa91b 2020-01-14 stsp break;
5768 d4efa91b 2020-01-14 stsp } else {
5769 d4efa91b 2020-01-14 stsp free(id);
5770 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5771 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5772 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5773 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5774 d4efa91b 2020-01-14 stsp if (err)
5775 d4efa91b 2020-01-14 stsp break;
5776 d4efa91b 2020-01-14 stsp }
5777 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5778 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5779 2417344c 2019-08-23 stsp if (datestr)
5780 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5781 d4efa91b 2020-01-14 stsp if (commit)
5782 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5783 d4efa91b 2020-01-14 stsp else {
5784 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5785 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5786 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5787 d4efa91b 2020-01-14 stsp id_str);
5788 d4efa91b 2020-01-14 stsp break;
5789 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5790 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5791 d4efa91b 2020-01-14 stsp id_str);
5792 d4efa91b 2020-01-14 stsp break;
5793 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5794 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5795 d4efa91b 2020-01-14 stsp id_str);
5796 d4efa91b 2020-01-14 stsp break;
5797 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5798 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5799 d4efa91b 2020-01-14 stsp id_str);
5800 d4efa91b 2020-01-14 stsp break;
5801 d4efa91b 2020-01-14 stsp default:
5802 d4efa91b 2020-01-14 stsp break;
5803 d4efa91b 2020-01-14 stsp }
5804 8e7bd50a 2019-08-22 stsp }
5805 8e7bd50a 2019-08-22 stsp free(id_str);
5806 d4efa91b 2020-01-14 stsp if (commit) {
5807 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5808 d4efa91b 2020-01-14 stsp if (err)
5809 d4efa91b 2020-01-14 stsp break;
5810 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5811 d4efa91b 2020-01-14 stsp } else {
5812 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5813 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5814 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5815 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5816 d4efa91b 2020-01-14 stsp break;
5817 d4efa91b 2020-01-14 stsp }
5818 8e7bd50a 2019-08-22 stsp }
5819 8e7bd50a 2019-08-22 stsp
5820 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5821 8e7bd50a 2019-08-22 stsp do {
5822 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5823 8e7bd50a 2019-08-22 stsp if (line)
5824 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5825 8e7bd50a 2019-08-22 stsp } while (line);
5826 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5827 8e7bd50a 2019-08-22 stsp }
5828 8e7bd50a 2019-08-22 stsp
5829 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5830 8e7bd50a 2019-08-22 stsp return NULL;
5831 8e7bd50a 2019-08-22 stsp }
5832 8e7bd50a 2019-08-22 stsp
5833 8e7bd50a 2019-08-22 stsp static const struct got_error *
5834 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5835 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5836 8e7bd50a 2019-08-22 stsp {
5837 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5838 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5839 f372d5cd 2019-10-21 stsp char *editor = NULL;
5840 1601cb9f 2020-09-11 naddy int initial_content_len;
5841 8e7bd50a 2019-08-22 stsp int fd = -1;
5842 8e7bd50a 2019-08-22 stsp
5843 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5844 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5845 8e7bd50a 2019-08-22 stsp goto done;
5846 8e7bd50a 2019-08-22 stsp }
5847 8e7bd50a 2019-08-22 stsp
5848 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5849 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5850 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5851 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5852 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5853 8e7bd50a 2019-08-22 stsp goto done;
5854 8e7bd50a 2019-08-22 stsp }
5855 8e7bd50a 2019-08-22 stsp
5856 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5857 8e7bd50a 2019-08-22 stsp if (err)
5858 8e7bd50a 2019-08-22 stsp goto done;
5859 8e7bd50a 2019-08-22 stsp
5860 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5861 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5862 97972933 2020-09-11 stsp goto done;
5863 97972933 2020-09-11 stsp }
5864 8e7bd50a 2019-08-22 stsp
5865 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5866 8e7bd50a 2019-08-22 stsp if (err)
5867 8e7bd50a 2019-08-22 stsp goto done;
5868 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5869 8e7bd50a 2019-08-22 stsp done:
5870 8e7bd50a 2019-08-22 stsp free(initial_content);
5871 8e7bd50a 2019-08-22 stsp free(template);
5872 8e7bd50a 2019-08-22 stsp free(editor);
5873 8e7bd50a 2019-08-22 stsp
5874 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5875 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5876 97972933 2020-09-11 stsp
5877 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5878 59f86c76 2020-09-11 stsp if (err == NULL)
5879 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5880 59f86c76 2020-09-11 stsp if (err) {
5881 59f86c76 2020-09-11 stsp free(*tagmsg);
5882 59f86c76 2020-09-11 stsp *tagmsg = NULL;
5883 8e7bd50a 2019-08-22 stsp }
5884 8e7bd50a 2019-08-22 stsp return err;
5885 8e7bd50a 2019-08-22 stsp }
5886 8e7bd50a 2019-08-22 stsp
5887 8e7bd50a 2019-08-22 stsp static const struct got_error *
5888 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5889 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5890 8e7bd50a 2019-08-22 stsp {
5891 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5892 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5893 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5894 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5895 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5896 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5897 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5898 8e7bd50a 2019-08-22 stsp
5899 8e7bd50a 2019-08-22 stsp /*
5900 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5901 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5902 8e7bd50a 2019-08-22 stsp * an unintended typo.
5903 8e7bd50a 2019-08-22 stsp */
5904 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5905 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5906 8e7bd50a 2019-08-22 stsp
5907 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
5908 8e7bd50a 2019-08-22 stsp if (err)
5909 8e7bd50a 2019-08-22 stsp return err;
5910 8e7bd50a 2019-08-22 stsp
5911 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5912 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5913 8e7bd50a 2019-08-22 stsp if (err)
5914 8e7bd50a 2019-08-22 stsp goto done;
5915 8e7bd50a 2019-08-22 stsp
5916 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5917 8e7bd50a 2019-08-22 stsp if (err)
5918 8e7bd50a 2019-08-22 stsp goto done;
5919 8e7bd50a 2019-08-22 stsp
5920 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5921 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5922 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5923 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5924 8e7bd50a 2019-08-22 stsp goto done;
5925 8e7bd50a 2019-08-22 stsp }
5926 8e7bd50a 2019-08-22 stsp tag_name += 10;
5927 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5928 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5929 8e7bd50a 2019-08-22 stsp goto done;
5930 8e7bd50a 2019-08-22 stsp }
5931 8e7bd50a 2019-08-22 stsp
5932 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5933 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5934 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5935 8e7bd50a 2019-08-22 stsp goto done;
5936 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5937 8e7bd50a 2019-08-22 stsp goto done;
5938 8e7bd50a 2019-08-22 stsp
5939 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5940 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5941 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5942 f372d5cd 2019-10-21 stsp if (err) {
5943 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5944 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5945 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5946 8e7bd50a 2019-08-22 stsp goto done;
5947 f372d5cd 2019-10-21 stsp }
5948 8e7bd50a 2019-08-22 stsp }
5949 8e7bd50a 2019-08-22 stsp
5950 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5951 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5952 f372d5cd 2019-10-21 stsp if (err) {
5953 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5954 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5955 8e7bd50a 2019-08-22 stsp goto done;
5956 f372d5cd 2019-10-21 stsp }
5957 8e7bd50a 2019-08-22 stsp
5958 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5959 f372d5cd 2019-10-21 stsp if (err) {
5960 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5961 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5962 8e7bd50a 2019-08-22 stsp goto done;
5963 f372d5cd 2019-10-21 stsp }
5964 8e7bd50a 2019-08-22 stsp
5965 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5966 f372d5cd 2019-10-21 stsp if (err) {
5967 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5968 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5969 f372d5cd 2019-10-21 stsp goto done;
5970 f372d5cd 2019-10-21 stsp }
5971 8e7bd50a 2019-08-22 stsp
5972 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5973 f372d5cd 2019-10-21 stsp if (err) {
5974 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5975 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5976 f372d5cd 2019-10-21 stsp goto done;
5977 8e7bd50a 2019-08-22 stsp }
5978 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5979 8e7bd50a 2019-08-22 stsp done:
5980 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5981 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5982 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5983 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5984 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5985 f372d5cd 2019-10-21 stsp free(tag_id_str);
5986 8e7bd50a 2019-08-22 stsp if (ref)
5987 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5988 8e7bd50a 2019-08-22 stsp free(commit_id);
5989 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5990 8e7bd50a 2019-08-22 stsp free(refname);
5991 8e7bd50a 2019-08-22 stsp free(tagmsg);
5992 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5993 aba9c984 2019-09-08 stsp free(tagger);
5994 8e7bd50a 2019-08-22 stsp return err;
5995 8e7bd50a 2019-08-22 stsp }
5996 8e7bd50a 2019-08-22 stsp
5997 8e7bd50a 2019-08-22 stsp static const struct got_error *
5998 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5999 8e7bd50a 2019-08-22 stsp {
6000 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6001 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6002 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6003 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6004 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6005 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6006 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6007 8e7bd50a 2019-08-22 stsp
6008 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6009 8e7bd50a 2019-08-22 stsp switch (ch) {
6010 80106605 2020-02-24 stsp case 'c':
6011 80106605 2020-02-24 stsp commit_id_arg = optarg;
6012 80106605 2020-02-24 stsp break;
6013 8e7bd50a 2019-08-22 stsp case 'm':
6014 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6015 8e7bd50a 2019-08-22 stsp break;
6016 8e7bd50a 2019-08-22 stsp case 'r':
6017 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6018 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6019 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6020 9ba1d308 2019-10-21 stsp optarg);
6021 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6022 8e7bd50a 2019-08-22 stsp break;
6023 8e7bd50a 2019-08-22 stsp case 'l':
6024 8e7bd50a 2019-08-22 stsp do_list = 1;
6025 8e7bd50a 2019-08-22 stsp break;
6026 8e7bd50a 2019-08-22 stsp default:
6027 8e7bd50a 2019-08-22 stsp usage_tag();
6028 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6029 8e7bd50a 2019-08-22 stsp }
6030 8e7bd50a 2019-08-22 stsp }
6031 8e7bd50a 2019-08-22 stsp
6032 8e7bd50a 2019-08-22 stsp argc -= optind;
6033 8e7bd50a 2019-08-22 stsp argv += optind;
6034 8e7bd50a 2019-08-22 stsp
6035 8e7bd50a 2019-08-22 stsp if (do_list) {
6036 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6037 775ce909 2020-03-22 stsp errx(1,
6038 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6039 8e7bd50a 2019-08-22 stsp if (tagmsg)
6040 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
6041 8e7bd50a 2019-08-22 stsp if (argc > 0)
6042 8e7bd50a 2019-08-22 stsp usage_tag();
6043 80106605 2020-02-24 stsp } else if (argc != 1)
6044 8e7bd50a 2019-08-22 stsp usage_tag();
6045 80106605 2020-02-24 stsp
6046 a2887370 2019-08-23 stsp tag_name = argv[0];
6047 8e7bd50a 2019-08-22 stsp
6048 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6049 8e7bd50a 2019-08-22 stsp if (do_list) {
6050 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6051 8e7bd50a 2019-08-22 stsp NULL) == -1)
6052 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6053 8e7bd50a 2019-08-22 stsp } else {
6054 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6055 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6056 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6057 8e7bd50a 2019-08-22 stsp }
6058 8e7bd50a 2019-08-22 stsp #endif
6059 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6060 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6061 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6062 8e7bd50a 2019-08-22 stsp goto done;
6063 8e7bd50a 2019-08-22 stsp }
6064 8e7bd50a 2019-08-22 stsp
6065 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6066 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6067 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6068 8e7bd50a 2019-08-22 stsp goto done;
6069 8e7bd50a 2019-08-22 stsp else
6070 8e7bd50a 2019-08-22 stsp error = NULL;
6071 8e7bd50a 2019-08-22 stsp if (worktree) {
6072 8e7bd50a 2019-08-22 stsp repo_path =
6073 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6074 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6075 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6076 8e7bd50a 2019-08-22 stsp if (error)
6077 8e7bd50a 2019-08-22 stsp goto done;
6078 8e7bd50a 2019-08-22 stsp } else {
6079 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6080 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6081 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6082 8e7bd50a 2019-08-22 stsp goto done;
6083 8e7bd50a 2019-08-22 stsp }
6084 8e7bd50a 2019-08-22 stsp }
6085 8e7bd50a 2019-08-22 stsp }
6086 8e7bd50a 2019-08-22 stsp
6087 8e7bd50a 2019-08-22 stsp if (do_list) {
6088 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6089 c9956ddf 2019-09-08 stsp if (error != NULL)
6090 c9956ddf 2019-09-08 stsp goto done;
6091 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6092 8e7bd50a 2019-08-22 stsp if (error)
6093 8e7bd50a 2019-08-22 stsp goto done;
6094 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6095 8e7bd50a 2019-08-22 stsp } else {
6096 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6097 c9956ddf 2019-09-08 stsp if (error)
6098 c9956ddf 2019-09-08 stsp goto done;
6099 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6100 c9956ddf 2019-09-08 stsp if (error != NULL)
6101 c9956ddf 2019-09-08 stsp goto done;
6102 c9956ddf 2019-09-08 stsp
6103 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6104 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6105 8e7bd50a 2019-08-22 stsp if (error)
6106 8e7bd50a 2019-08-22 stsp goto done;
6107 8e7bd50a 2019-08-22 stsp }
6108 8e7bd50a 2019-08-22 stsp
6109 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6110 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6111 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6112 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6113 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6114 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6115 8e7bd50a 2019-08-22 stsp if (error)
6116 8e7bd50a 2019-08-22 stsp goto done;
6117 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6118 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6119 8e7bd50a 2019-08-22 stsp if (error)
6120 8e7bd50a 2019-08-22 stsp goto done;
6121 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6122 8e7bd50a 2019-08-22 stsp free(commit_id);
6123 8e7bd50a 2019-08-22 stsp if (error)
6124 8e7bd50a 2019-08-22 stsp goto done;
6125 8e7bd50a 2019-08-22 stsp }
6126 8e7bd50a 2019-08-22 stsp
6127 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6128 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6129 8e7bd50a 2019-08-22 stsp }
6130 8e7bd50a 2019-08-22 stsp done:
6131 8e7bd50a 2019-08-22 stsp if (repo)
6132 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
6133 8e7bd50a 2019-08-22 stsp if (worktree)
6134 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6135 8e7bd50a 2019-08-22 stsp free(cwd);
6136 8e7bd50a 2019-08-22 stsp free(repo_path);
6137 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6138 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6139 8e7bd50a 2019-08-22 stsp return error;
6140 8e7bd50a 2019-08-22 stsp }
6141 8e7bd50a 2019-08-22 stsp
6142 8e7bd50a 2019-08-22 stsp __dead static void
6143 d00136be 2019-03-26 stsp usage_add(void)
6144 d00136be 2019-03-26 stsp {
6145 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6146 022fae89 2019-12-06 tracey getprogname());
6147 d00136be 2019-03-26 stsp exit(1);
6148 d00136be 2019-03-26 stsp }
6149 d00136be 2019-03-26 stsp
6150 d00136be 2019-03-26 stsp static const struct got_error *
6151 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6152 4e68cba3 2019-11-23 stsp {
6153 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6154 4e68cba3 2019-11-23 stsp path++;
6155 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6156 4e68cba3 2019-11-23 stsp return NULL;
6157 4e68cba3 2019-11-23 stsp }
6158 4e68cba3 2019-11-23 stsp
6159 4e68cba3 2019-11-23 stsp static const struct got_error *
6160 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6161 d00136be 2019-03-26 stsp {
6162 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6163 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6164 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6165 1dd54920 2019-05-11 stsp char *cwd = NULL;
6166 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6167 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6168 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6169 1dd54920 2019-05-11 stsp
6170 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6171 d00136be 2019-03-26 stsp
6172 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6173 d00136be 2019-03-26 stsp switch (ch) {
6174 022fae89 2019-12-06 tracey case 'I':
6175 022fae89 2019-12-06 tracey no_ignores = 1;
6176 022fae89 2019-12-06 tracey break;
6177 4e68cba3 2019-11-23 stsp case 'R':
6178 4e68cba3 2019-11-23 stsp can_recurse = 1;
6179 4e68cba3 2019-11-23 stsp break;
6180 d00136be 2019-03-26 stsp default:
6181 d00136be 2019-03-26 stsp usage_add();
6182 d00136be 2019-03-26 stsp /* NOTREACHED */
6183 d00136be 2019-03-26 stsp }
6184 d00136be 2019-03-26 stsp }
6185 d00136be 2019-03-26 stsp
6186 d00136be 2019-03-26 stsp argc -= optind;
6187 d00136be 2019-03-26 stsp argv += optind;
6188 d00136be 2019-03-26 stsp
6189 43012d58 2019-07-14 stsp #ifndef PROFILE
6190 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6191 43012d58 2019-07-14 stsp NULL) == -1)
6192 43012d58 2019-07-14 stsp err(1, "pledge");
6193 43012d58 2019-07-14 stsp #endif
6194 723c305c 2019-05-11 jcs if (argc < 1)
6195 d00136be 2019-03-26 stsp usage_add();
6196 d00136be 2019-03-26 stsp
6197 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6198 d00136be 2019-03-26 stsp if (cwd == NULL) {
6199 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6200 d00136be 2019-03-26 stsp goto done;
6201 d00136be 2019-03-26 stsp }
6202 723c305c 2019-05-11 jcs
6203 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6204 fa51e947 2020-03-27 stsp if (error) {
6205 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6206 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6207 d00136be 2019-03-26 stsp goto done;
6208 fa51e947 2020-03-27 stsp }
6209 d00136be 2019-03-26 stsp
6210 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6211 c9956ddf 2019-09-08 stsp NULL);
6212 031a5338 2019-03-26 stsp if (error != NULL)
6213 031a5338 2019-03-26 stsp goto done;
6214 031a5338 2019-03-26 stsp
6215 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6216 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6217 d00136be 2019-03-26 stsp if (error)
6218 d00136be 2019-03-26 stsp goto done;
6219 d00136be 2019-03-26 stsp
6220 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6221 6d022e97 2019-08-04 stsp if (error)
6222 022fae89 2019-12-06 tracey goto done;
6223 022fae89 2019-12-06 tracey
6224 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6225 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6226 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6227 6d022e97 2019-08-04 stsp goto done;
6228 723c305c 2019-05-11 jcs
6229 022fae89 2019-12-06 tracey }
6230 022fae89 2019-12-06 tracey
6231 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6232 4e68cba3 2019-11-23 stsp char *ondisk_path;
6233 4e68cba3 2019-11-23 stsp struct stat sb;
6234 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6235 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6236 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6237 4e68cba3 2019-11-23 stsp pe->path) == -1) {
6238 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6239 4e68cba3 2019-11-23 stsp goto done;
6240 4e68cba3 2019-11-23 stsp }
6241 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6242 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6243 4e68cba3 2019-11-23 stsp free(ondisk_path);
6244 4e68cba3 2019-11-23 stsp continue;
6245 4e68cba3 2019-11-23 stsp }
6246 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6247 4e68cba3 2019-11-23 stsp ondisk_path);
6248 4e68cba3 2019-11-23 stsp free(ondisk_path);
6249 4e68cba3 2019-11-23 stsp goto done;
6250 4e68cba3 2019-11-23 stsp }
6251 4e68cba3 2019-11-23 stsp free(ondisk_path);
6252 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6253 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6254 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6255 4e68cba3 2019-11-23 stsp goto done;
6256 4e68cba3 2019-11-23 stsp }
6257 4e68cba3 2019-11-23 stsp }
6258 4e68cba3 2019-11-23 stsp }
6259 022fae89 2019-12-06 tracey
6260 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6261 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6262 d00136be 2019-03-26 stsp done:
6263 031a5338 2019-03-26 stsp if (repo)
6264 031a5338 2019-03-26 stsp got_repo_close(repo);
6265 d00136be 2019-03-26 stsp if (worktree)
6266 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6267 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6268 1dd54920 2019-05-11 stsp free((char *)pe->path);
6269 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6270 2ec1f75b 2019-03-26 stsp free(cwd);
6271 2ec1f75b 2019-03-26 stsp return error;
6272 2ec1f75b 2019-03-26 stsp }
6273 2ec1f75b 2019-03-26 stsp
6274 2ec1f75b 2019-03-26 stsp __dead static void
6275 648e4ef7 2019-07-09 stsp usage_remove(void)
6276 2ec1f75b 2019-03-26 stsp {
6277 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6278 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6279 2ec1f75b 2019-03-26 stsp exit(1);
6280 2a06fe5f 2019-08-24 stsp }
6281 2a06fe5f 2019-08-24 stsp
6282 2a06fe5f 2019-08-24 stsp static const struct got_error *
6283 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6284 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6285 2a06fe5f 2019-08-24 stsp {
6286 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6287 f2a9dc41 2019-12-13 tracey path++;
6288 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6289 2a06fe5f 2019-08-24 stsp return NULL;
6290 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6291 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6292 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6293 2a06fe5f 2019-08-24 stsp return NULL;
6294 2ec1f75b 2019-03-26 stsp }
6295 2ec1f75b 2019-03-26 stsp
6296 2ec1f75b 2019-03-26 stsp static const struct got_error *
6297 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6298 2ec1f75b 2019-03-26 stsp {
6299 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6300 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6301 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6302 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6303 17ed4618 2019-06-02 stsp char *cwd = NULL;
6304 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6305 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6306 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6307 2ec1f75b 2019-03-26 stsp
6308 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6309 17ed4618 2019-06-02 stsp
6310 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6311 2ec1f75b 2019-03-26 stsp switch (ch) {
6312 2ec1f75b 2019-03-26 stsp case 'f':
6313 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6314 2ec1f75b 2019-03-26 stsp break;
6315 70e3e7f5 2019-12-13 tracey case 'k':
6316 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6317 70e3e7f5 2019-12-13 tracey break;
6318 f2a9dc41 2019-12-13 tracey case 'R':
6319 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6320 f2a9dc41 2019-12-13 tracey break;
6321 766841c2 2020-08-13 stsp case 's':
6322 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6323 766841c2 2020-08-13 stsp switch (optarg[i]) {
6324 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6325 766841c2 2020-08-13 stsp delete_local_mods = 1;
6326 766841c2 2020-08-13 stsp break;
6327 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6328 766841c2 2020-08-13 stsp break;
6329 766841c2 2020-08-13 stsp default:
6330 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6331 766841c2 2020-08-13 stsp optarg[i]);
6332 766841c2 2020-08-13 stsp }
6333 766841c2 2020-08-13 stsp }
6334 766841c2 2020-08-13 stsp status_codes = optarg;
6335 766841c2 2020-08-13 stsp break;
6336 2ec1f75b 2019-03-26 stsp default:
6337 f2a9dc41 2019-12-13 tracey usage_remove();
6338 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6339 2ec1f75b 2019-03-26 stsp }
6340 2ec1f75b 2019-03-26 stsp }
6341 2ec1f75b 2019-03-26 stsp
6342 2ec1f75b 2019-03-26 stsp argc -= optind;
6343 2ec1f75b 2019-03-26 stsp argv += optind;
6344 2ec1f75b 2019-03-26 stsp
6345 43012d58 2019-07-14 stsp #ifndef PROFILE
6346 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6347 43012d58 2019-07-14 stsp NULL) == -1)
6348 43012d58 2019-07-14 stsp err(1, "pledge");
6349 43012d58 2019-07-14 stsp #endif
6350 17ed4618 2019-06-02 stsp if (argc < 1)
6351 648e4ef7 2019-07-09 stsp usage_remove();
6352 2ec1f75b 2019-03-26 stsp
6353 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6354 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6355 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6356 2ec1f75b 2019-03-26 stsp goto done;
6357 2ec1f75b 2019-03-26 stsp }
6358 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6359 fa51e947 2020-03-27 stsp if (error) {
6360 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6361 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6362 2ec1f75b 2019-03-26 stsp goto done;
6363 fa51e947 2020-03-27 stsp }
6364 2ec1f75b 2019-03-26 stsp
6365 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6366 c9956ddf 2019-09-08 stsp NULL);
6367 2af4a041 2019-05-11 jcs if (error)
6368 2ec1f75b 2019-03-26 stsp goto done;
6369 2ec1f75b 2019-03-26 stsp
6370 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6371 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6372 2ec1f75b 2019-03-26 stsp if (error)
6373 2ec1f75b 2019-03-26 stsp goto done;
6374 2ec1f75b 2019-03-26 stsp
6375 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6376 6d022e97 2019-08-04 stsp if (error)
6377 6d022e97 2019-08-04 stsp goto done;
6378 17ed4618 2019-06-02 stsp
6379 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6380 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6381 f2a9dc41 2019-12-13 tracey struct stat sb;
6382 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6383 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6384 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6385 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
6386 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6387 f2a9dc41 2019-12-13 tracey goto done;
6388 f2a9dc41 2019-12-13 tracey }
6389 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6390 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6391 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6392 f2a9dc41 2019-12-13 tracey continue;
6393 f2a9dc41 2019-12-13 tracey }
6394 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6395 f2a9dc41 2019-12-13 tracey ondisk_path);
6396 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6397 f2a9dc41 2019-12-13 tracey goto done;
6398 f2a9dc41 2019-12-13 tracey }
6399 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6400 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6401 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6402 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6403 f2a9dc41 2019-12-13 tracey goto done;
6404 f2a9dc41 2019-12-13 tracey }
6405 f2a9dc41 2019-12-13 tracey }
6406 f2a9dc41 2019-12-13 tracey }
6407 f2a9dc41 2019-12-13 tracey
6408 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6409 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6410 766841c2 2020-08-13 stsp repo, keep_on_disk);
6411 a129376b 2019-03-28 stsp done:
6412 a129376b 2019-03-28 stsp if (repo)
6413 a129376b 2019-03-28 stsp got_repo_close(repo);
6414 a129376b 2019-03-28 stsp if (worktree)
6415 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6416 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6417 17ed4618 2019-06-02 stsp free((char *)pe->path);
6418 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6419 a129376b 2019-03-28 stsp free(cwd);
6420 a129376b 2019-03-28 stsp return error;
6421 a129376b 2019-03-28 stsp }
6422 a129376b 2019-03-28 stsp
6423 a129376b 2019-03-28 stsp __dead static void
6424 a129376b 2019-03-28 stsp usage_revert(void)
6425 a129376b 2019-03-28 stsp {
6426 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6427 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6428 a129376b 2019-03-28 stsp exit(1);
6429 a129376b 2019-03-28 stsp }
6430 a129376b 2019-03-28 stsp
6431 1ee397ad 2019-07-12 stsp static const struct got_error *
6432 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6433 a129376b 2019-03-28 stsp {
6434 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6435 fb9704af 2020-01-27 stsp return NULL;
6436 fb9704af 2020-01-27 stsp
6437 a129376b 2019-03-28 stsp while (path[0] == '/')
6438 a129376b 2019-03-28 stsp path++;
6439 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6440 33aa809d 2019-08-08 stsp return NULL;
6441 33aa809d 2019-08-08 stsp }
6442 33aa809d 2019-08-08 stsp
6443 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6444 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6445 33aa809d 2019-08-08 stsp const char *action;
6446 33aa809d 2019-08-08 stsp };
6447 33aa809d 2019-08-08 stsp
6448 33aa809d 2019-08-08 stsp static const struct got_error *
6449 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6450 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6451 33aa809d 2019-08-08 stsp {
6452 33aa809d 2019-08-08 stsp char *line = NULL;
6453 33aa809d 2019-08-08 stsp size_t linesize = 0;
6454 33aa809d 2019-08-08 stsp ssize_t linelen;
6455 33aa809d 2019-08-08 stsp
6456 33aa809d 2019-08-08 stsp switch (status) {
6457 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6458 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6459 33aa809d 2019-08-08 stsp break;
6460 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6461 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6462 33aa809d 2019-08-08 stsp break;
6463 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6464 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6465 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6466 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6467 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6468 33aa809d 2019-08-08 stsp printf("%s", line);
6469 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6470 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6471 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6472 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6473 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6474 33aa809d 2019-08-08 stsp break;
6475 33aa809d 2019-08-08 stsp default:
6476 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6477 33aa809d 2019-08-08 stsp }
6478 33aa809d 2019-08-08 stsp
6479 33aa809d 2019-08-08 stsp return NULL;
6480 33aa809d 2019-08-08 stsp }
6481 33aa809d 2019-08-08 stsp
6482 33aa809d 2019-08-08 stsp static const struct got_error *
6483 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6484 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6485 33aa809d 2019-08-08 stsp {
6486 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6487 33aa809d 2019-08-08 stsp char *line = NULL;
6488 33aa809d 2019-08-08 stsp size_t linesize = 0;
6489 33aa809d 2019-08-08 stsp ssize_t linelen;
6490 33aa809d 2019-08-08 stsp int resp = ' ';
6491 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6492 33aa809d 2019-08-08 stsp
6493 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6494 33aa809d 2019-08-08 stsp
6495 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6496 33aa809d 2019-08-08 stsp char *nl;
6497 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6498 33aa809d 2019-08-08 stsp a->action);
6499 33aa809d 2019-08-08 stsp if (err)
6500 33aa809d 2019-08-08 stsp return err;
6501 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6502 33aa809d 2019-08-08 stsp if (linelen == -1) {
6503 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6504 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6505 33aa809d 2019-08-08 stsp return NULL;
6506 33aa809d 2019-08-08 stsp }
6507 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6508 33aa809d 2019-08-08 stsp if (nl)
6509 33aa809d 2019-08-08 stsp *nl = '\0';
6510 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6511 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6512 33aa809d 2019-08-08 stsp printf("y\n");
6513 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6514 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6515 33aa809d 2019-08-08 stsp printf("n\n");
6516 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6517 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6518 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6519 33aa809d 2019-08-08 stsp printf("q\n");
6520 33aa809d 2019-08-08 stsp } else
6521 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6522 33aa809d 2019-08-08 stsp free(line);
6523 33aa809d 2019-08-08 stsp return NULL;
6524 33aa809d 2019-08-08 stsp }
6525 33aa809d 2019-08-08 stsp
6526 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6527 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6528 33aa809d 2019-08-08 stsp a->action);
6529 33aa809d 2019-08-08 stsp if (err)
6530 33aa809d 2019-08-08 stsp return err;
6531 33aa809d 2019-08-08 stsp resp = getchar();
6532 33aa809d 2019-08-08 stsp if (resp == '\n')
6533 33aa809d 2019-08-08 stsp resp = getchar();
6534 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6535 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6536 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6537 33aa809d 2019-08-08 stsp resp = ' ';
6538 33aa809d 2019-08-08 stsp }
6539 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6540 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6541 33aa809d 2019-08-08 stsp resp = ' ';
6542 33aa809d 2019-08-08 stsp }
6543 33aa809d 2019-08-08 stsp }
6544 33aa809d 2019-08-08 stsp
6545 33aa809d 2019-08-08 stsp if (resp == 'y')
6546 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6547 33aa809d 2019-08-08 stsp else if (resp == 'n')
6548 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6549 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6550 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6551 33aa809d 2019-08-08 stsp
6552 1ee397ad 2019-07-12 stsp return NULL;
6553 a129376b 2019-03-28 stsp }
6554 a129376b 2019-03-28 stsp
6555 33aa809d 2019-08-08 stsp
6556 a129376b 2019-03-28 stsp static const struct got_error *
6557 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6558 a129376b 2019-03-28 stsp {
6559 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6560 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6561 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6562 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6563 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6564 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6565 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6566 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6567 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6568 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6569 a129376b 2019-03-28 stsp
6570 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6571 e20a8b6f 2019-06-04 stsp
6572 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6573 a129376b 2019-03-28 stsp switch (ch) {
6574 33aa809d 2019-08-08 stsp case 'p':
6575 33aa809d 2019-08-08 stsp pflag = 1;
6576 33aa809d 2019-08-08 stsp break;
6577 33aa809d 2019-08-08 stsp case 'F':
6578 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6579 33aa809d 2019-08-08 stsp break;
6580 0f6d7415 2019-08-08 stsp case 'R':
6581 0f6d7415 2019-08-08 stsp can_recurse = 1;
6582 0f6d7415 2019-08-08 stsp break;
6583 a129376b 2019-03-28 stsp default:
6584 a129376b 2019-03-28 stsp usage_revert();
6585 a129376b 2019-03-28 stsp /* NOTREACHED */
6586 a129376b 2019-03-28 stsp }
6587 a129376b 2019-03-28 stsp }
6588 a129376b 2019-03-28 stsp
6589 a129376b 2019-03-28 stsp argc -= optind;
6590 a129376b 2019-03-28 stsp argv += optind;
6591 a129376b 2019-03-28 stsp
6592 43012d58 2019-07-14 stsp #ifndef PROFILE
6593 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6594 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6595 43012d58 2019-07-14 stsp err(1, "pledge");
6596 43012d58 2019-07-14 stsp #endif
6597 e20a8b6f 2019-06-04 stsp if (argc < 1)
6598 a129376b 2019-03-28 stsp usage_revert();
6599 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6600 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6601 a129376b 2019-03-28 stsp
6602 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6603 a129376b 2019-03-28 stsp if (cwd == NULL) {
6604 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6605 a129376b 2019-03-28 stsp goto done;
6606 a129376b 2019-03-28 stsp }
6607 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6608 fa51e947 2020-03-27 stsp if (error) {
6609 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6610 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6611 2ec1f75b 2019-03-26 stsp goto done;
6612 fa51e947 2020-03-27 stsp }
6613 a129376b 2019-03-28 stsp
6614 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6615 c9956ddf 2019-09-08 stsp NULL);
6616 a129376b 2019-03-28 stsp if (error != NULL)
6617 a129376b 2019-03-28 stsp goto done;
6618 a129376b 2019-03-28 stsp
6619 33aa809d 2019-08-08 stsp if (patch_script_path) {
6620 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6621 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6622 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6623 33aa809d 2019-08-08 stsp patch_script_path);
6624 33aa809d 2019-08-08 stsp goto done;
6625 33aa809d 2019-08-08 stsp }
6626 33aa809d 2019-08-08 stsp }
6627 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6628 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6629 a129376b 2019-03-28 stsp if (error)
6630 a129376b 2019-03-28 stsp goto done;
6631 a129376b 2019-03-28 stsp
6632 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6633 6d022e97 2019-08-04 stsp if (error)
6634 6d022e97 2019-08-04 stsp goto done;
6635 00db391e 2019-08-03 stsp
6636 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6637 0f6d7415 2019-08-08 stsp char *ondisk_path;
6638 0f6d7415 2019-08-08 stsp struct stat sb;
6639 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6640 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6641 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6642 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6643 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6644 0f6d7415 2019-08-08 stsp goto done;
6645 0f6d7415 2019-08-08 stsp }
6646 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6647 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6648 0f6d7415 2019-08-08 stsp free(ondisk_path);
6649 0f6d7415 2019-08-08 stsp continue;
6650 0f6d7415 2019-08-08 stsp }
6651 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6652 0f6d7415 2019-08-08 stsp ondisk_path);
6653 0f6d7415 2019-08-08 stsp free(ondisk_path);
6654 0f6d7415 2019-08-08 stsp goto done;
6655 0f6d7415 2019-08-08 stsp }
6656 0f6d7415 2019-08-08 stsp free(ondisk_path);
6657 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6658 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6659 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6660 0f6d7415 2019-08-08 stsp goto done;
6661 0f6d7415 2019-08-08 stsp }
6662 0f6d7415 2019-08-08 stsp }
6663 0f6d7415 2019-08-08 stsp }
6664 0f6d7415 2019-08-08 stsp
6665 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6666 33aa809d 2019-08-08 stsp cpa.action = "revert";
6667 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6668 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6669 2ec1f75b 2019-03-26 stsp done:
6670 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6671 33aa809d 2019-08-08 stsp error == NULL)
6672 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6673 2ec1f75b 2019-03-26 stsp if (repo)
6674 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6675 2ec1f75b 2019-03-26 stsp if (worktree)
6676 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6677 2ec1f75b 2019-03-26 stsp free(path);
6678 d00136be 2019-03-26 stsp free(cwd);
6679 6bad629b 2019-02-04 stsp return error;
6680 c4296144 2019-05-09 stsp }
6681 c4296144 2019-05-09 stsp
6682 c4296144 2019-05-09 stsp __dead static void
6683 c4296144 2019-05-09 stsp usage_commit(void)
6684 c4296144 2019-05-09 stsp {
6685 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6686 5c1e53bc 2019-07-28 stsp getprogname());
6687 c4296144 2019-05-09 stsp exit(1);
6688 33ad4cbe 2019-05-12 jcs }
6689 33ad4cbe 2019-05-12 jcs
6690 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6691 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6692 e2ba3d07 2019-05-13 stsp const char *editor;
6693 e0870e44 2019-05-13 stsp const char *worktree_path;
6694 76d98825 2019-06-03 stsp const char *branch_name;
6695 314a6357 2019-05-13 stsp const char *repo_path;
6696 e0870e44 2019-05-13 stsp char *logmsg_path;
6697 e2ba3d07 2019-05-13 stsp
6698 e2ba3d07 2019-05-13 stsp };
6699 e0870e44 2019-05-13 stsp
6700 33ad4cbe 2019-05-12 jcs static const struct got_error *
6701 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6702 33ad4cbe 2019-05-12 jcs void *arg)
6703 33ad4cbe 2019-05-12 jcs {
6704 76d98825 2019-06-03 stsp char *initial_content = NULL;
6705 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6706 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6707 e0870e44 2019-05-13 stsp char *template = NULL;
6708 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6709 1601cb9f 2020-09-11 naddy int initial_content_len;
6710 97972933 2020-09-11 stsp int fd = -1;
6711 33ad4cbe 2019-05-12 jcs size_t len;
6712 33ad4cbe 2019-05-12 jcs
6713 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6714 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6715 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6716 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6717 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6718 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6719 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6720 33ad4cbe 2019-05-12 jcs return NULL;
6721 33ad4cbe 2019-05-12 jcs }
6722 33ad4cbe 2019-05-12 jcs
6723 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6724 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6725 76d98825 2019-06-03 stsp
6726 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6727 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6728 1601cb9f 2020-09-11 naddy a->branch_name);
6729 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6730 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6731 e0870e44 2019-05-13 stsp
6732 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6733 793c30b5 2019-05-13 stsp if (err)
6734 e0870e44 2019-05-13 stsp goto done;
6735 33ad4cbe 2019-05-12 jcs
6736 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6737 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6738 97972933 2020-09-11 stsp goto done;
6739 97972933 2020-09-11 stsp }
6740 33ad4cbe 2019-05-12 jcs
6741 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6742 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6743 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6744 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6745 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6746 33ad4cbe 2019-05-12 jcs }
6747 33ad4cbe 2019-05-12 jcs
6748 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6749 33ad4cbe 2019-05-12 jcs done:
6750 76d98825 2019-06-03 stsp free(initial_content);
6751 e0870e44 2019-05-13 stsp free(template);
6752 314a6357 2019-05-13 stsp
6753 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6754 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6755 97972933 2020-09-11 stsp
6756 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6757 59f86c76 2020-09-11 stsp if (err == NULL)
6758 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6759 59f86c76 2020-09-11 stsp if (err) {
6760 59f86c76 2020-09-11 stsp free(*logmsg);
6761 59f86c76 2020-09-11 stsp *logmsg = NULL;
6762 3ce1b845 2019-07-15 stsp }
6763 33ad4cbe 2019-05-12 jcs return err;
6764 6bad629b 2019-02-04 stsp }
6765 c4296144 2019-05-09 stsp
6766 c4296144 2019-05-09 stsp static const struct got_error *
6767 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6768 c4296144 2019-05-09 stsp {
6769 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6770 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6771 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6772 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6773 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6774 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6775 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6776 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6777 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6778 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6779 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6780 5c1e53bc 2019-07-28 stsp
6781 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6782 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6783 c4296144 2019-05-09 stsp
6784 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6785 c4296144 2019-05-09 stsp switch (ch) {
6786 c4296144 2019-05-09 stsp case 'm':
6787 c4296144 2019-05-09 stsp logmsg = optarg;
6788 c4296144 2019-05-09 stsp break;
6789 35213c7c 2020-07-23 stsp case 'S':
6790 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6791 35213c7c 2020-07-23 stsp break;
6792 c4296144 2019-05-09 stsp default:
6793 c4296144 2019-05-09 stsp usage_commit();
6794 c4296144 2019-05-09 stsp /* NOTREACHED */
6795 c4296144 2019-05-09 stsp }
6796 c4296144 2019-05-09 stsp }
6797 c4296144 2019-05-09 stsp
6798 c4296144 2019-05-09 stsp argc -= optind;
6799 c4296144 2019-05-09 stsp argv += optind;
6800 c4296144 2019-05-09 stsp
6801 43012d58 2019-07-14 stsp #ifndef PROFILE
6802 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6803 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6804 43012d58 2019-07-14 stsp err(1, "pledge");
6805 43012d58 2019-07-14 stsp #endif
6806 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6807 c4296144 2019-05-09 stsp if (cwd == NULL) {
6808 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6809 c4296144 2019-05-09 stsp goto done;
6810 c4296144 2019-05-09 stsp }
6811 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6812 fa51e947 2020-03-27 stsp if (error) {
6813 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6814 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6815 7d5807f4 2019-07-11 stsp goto done;
6816 fa51e947 2020-03-27 stsp }
6817 7d5807f4 2019-07-11 stsp
6818 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6819 c4296144 2019-05-09 stsp if (error)
6820 a698f62e 2019-07-25 stsp goto done;
6821 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6822 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6823 c4296144 2019-05-09 stsp goto done;
6824 a698f62e 2019-07-25 stsp }
6825 5c1e53bc 2019-07-28 stsp
6826 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6827 916f288c 2019-07-30 stsp worktree);
6828 5c1e53bc 2019-07-28 stsp if (error)
6829 5c1e53bc 2019-07-28 stsp goto done;
6830 c4296144 2019-05-09 stsp
6831 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6832 c9956ddf 2019-09-08 stsp if (error)
6833 c9956ddf 2019-09-08 stsp goto done;
6834 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6835 c9956ddf 2019-09-08 stsp gitconfig_path);
6836 c4296144 2019-05-09 stsp if (error != NULL)
6837 c4296144 2019-05-09 stsp goto done;
6838 c4296144 2019-05-09 stsp
6839 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6840 aba9c984 2019-09-08 stsp if (error)
6841 aba9c984 2019-09-08 stsp return error;
6842 aba9c984 2019-09-08 stsp
6843 314a6357 2019-05-13 stsp /*
6844 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6845 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6846 314a6357 2019-05-13 stsp */
6847 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6848 314a6357 2019-05-13 stsp error = get_editor(&editor);
6849 314a6357 2019-05-13 stsp else
6850 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6851 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6852 c4296144 2019-05-09 stsp if (error)
6853 c4296144 2019-05-09 stsp goto done;
6854 c4296144 2019-05-09 stsp
6855 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6856 087fb88c 2019-08-04 stsp if (error)
6857 087fb88c 2019-08-04 stsp goto done;
6858 087fb88c 2019-08-04 stsp
6859 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6860 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6861 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6862 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6863 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6864 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6865 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6866 916f288c 2019-07-30 stsp goto done;
6867 916f288c 2019-07-30 stsp }
6868 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6869 916f288c 2019-07-30 stsp }
6870 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6871 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6872 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6873 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6874 e0870e44 2019-05-13 stsp if (error) {
6875 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6876 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6877 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6878 c4296144 2019-05-09 stsp goto done;
6879 e0870e44 2019-05-13 stsp }
6880 c4296144 2019-05-09 stsp
6881 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6882 c4296144 2019-05-09 stsp if (error)
6883 c4296144 2019-05-09 stsp goto done;
6884 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6885 c4296144 2019-05-09 stsp done:
6886 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6887 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6888 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6889 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6890 7266f21f 2019-10-21 stsp error == NULL)
6891 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6892 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6893 c4296144 2019-05-09 stsp if (repo)
6894 c4296144 2019-05-09 stsp got_repo_close(repo);
6895 c4296144 2019-05-09 stsp if (worktree)
6896 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6897 c4296144 2019-05-09 stsp free(cwd);
6898 c4296144 2019-05-09 stsp free(id_str);
6899 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6900 e2ba3d07 2019-05-13 stsp free(editor);
6901 aba9c984 2019-09-08 stsp free(author);
6902 234035bc 2019-06-01 stsp return error;
6903 234035bc 2019-06-01 stsp }
6904 234035bc 2019-06-01 stsp
6905 234035bc 2019-06-01 stsp __dead static void
6906 234035bc 2019-06-01 stsp usage_cherrypick(void)
6907 234035bc 2019-06-01 stsp {
6908 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6909 234035bc 2019-06-01 stsp exit(1);
6910 234035bc 2019-06-01 stsp }
6911 234035bc 2019-06-01 stsp
6912 234035bc 2019-06-01 stsp static const struct got_error *
6913 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6914 234035bc 2019-06-01 stsp {
6915 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6916 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6917 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6918 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6919 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6920 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6921 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6922 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6923 9627c110 2020-04-18 stsp int ch;
6924 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6925 234035bc 2019-06-01 stsp
6926 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6927 234035bc 2019-06-01 stsp switch (ch) {
6928 234035bc 2019-06-01 stsp default:
6929 234035bc 2019-06-01 stsp usage_cherrypick();
6930 234035bc 2019-06-01 stsp /* NOTREACHED */
6931 234035bc 2019-06-01 stsp }
6932 234035bc 2019-06-01 stsp }
6933 234035bc 2019-06-01 stsp
6934 234035bc 2019-06-01 stsp argc -= optind;
6935 234035bc 2019-06-01 stsp argv += optind;
6936 234035bc 2019-06-01 stsp
6937 43012d58 2019-07-14 stsp #ifndef PROFILE
6938 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6939 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6940 43012d58 2019-07-14 stsp err(1, "pledge");
6941 43012d58 2019-07-14 stsp #endif
6942 234035bc 2019-06-01 stsp if (argc != 1)
6943 234035bc 2019-06-01 stsp usage_cherrypick();
6944 234035bc 2019-06-01 stsp
6945 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6946 234035bc 2019-06-01 stsp if (cwd == NULL) {
6947 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6948 234035bc 2019-06-01 stsp goto done;
6949 234035bc 2019-06-01 stsp }
6950 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6951 fa51e947 2020-03-27 stsp if (error) {
6952 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6953 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6954 fa51e947 2020-03-27 stsp cwd);
6955 234035bc 2019-06-01 stsp goto done;
6956 fa51e947 2020-03-27 stsp }
6957 234035bc 2019-06-01 stsp
6958 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6959 c9956ddf 2019-09-08 stsp NULL);
6960 234035bc 2019-06-01 stsp if (error != NULL)
6961 234035bc 2019-06-01 stsp goto done;
6962 234035bc 2019-06-01 stsp
6963 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6964 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6965 234035bc 2019-06-01 stsp if (error)
6966 234035bc 2019-06-01 stsp goto done;
6967 234035bc 2019-06-01 stsp
6968 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6969 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6970 234035bc 2019-06-01 stsp if (error != NULL) {
6971 234035bc 2019-06-01 stsp struct got_reference *ref;
6972 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6973 234035bc 2019-06-01 stsp goto done;
6974 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6975 234035bc 2019-06-01 stsp if (error != NULL)
6976 234035bc 2019-06-01 stsp goto done;
6977 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6978 234035bc 2019-06-01 stsp got_ref_close(ref);
6979 234035bc 2019-06-01 stsp if (error != NULL)
6980 234035bc 2019-06-01 stsp goto done;
6981 234035bc 2019-06-01 stsp }
6982 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6983 234035bc 2019-06-01 stsp if (error)
6984 234035bc 2019-06-01 stsp goto done;
6985 234035bc 2019-06-01 stsp
6986 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6987 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6988 234035bc 2019-06-01 stsp if (error != NULL)
6989 234035bc 2019-06-01 stsp goto done;
6990 234035bc 2019-06-01 stsp
6991 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6992 234035bc 2019-06-01 stsp if (error) {
6993 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6994 234035bc 2019-06-01 stsp goto done;
6995 234035bc 2019-06-01 stsp error = NULL;
6996 234035bc 2019-06-01 stsp } else {
6997 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6998 234035bc 2019-06-01 stsp goto done;
6999 234035bc 2019-06-01 stsp }
7000 234035bc 2019-06-01 stsp
7001 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7002 234035bc 2019-06-01 stsp if (error)
7003 234035bc 2019-06-01 stsp goto done;
7004 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7005 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7006 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7007 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7008 03415a1a 2019-06-02 stsp NULL);
7009 234035bc 2019-06-01 stsp if (error != NULL)
7010 234035bc 2019-06-01 stsp goto done;
7011 234035bc 2019-06-01 stsp
7012 9627c110 2020-04-18 stsp if (upa.did_something)
7013 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7014 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7015 234035bc 2019-06-01 stsp done:
7016 234035bc 2019-06-01 stsp if (commit)
7017 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7018 234035bc 2019-06-01 stsp free(commit_id_str);
7019 234035bc 2019-06-01 stsp if (head_ref)
7020 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7021 234035bc 2019-06-01 stsp if (worktree)
7022 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7023 234035bc 2019-06-01 stsp if (repo)
7024 234035bc 2019-06-01 stsp got_repo_close(repo);
7025 c4296144 2019-05-09 stsp return error;
7026 c4296144 2019-05-09 stsp }
7027 5ef14e63 2019-06-02 stsp
7028 5ef14e63 2019-06-02 stsp __dead static void
7029 5ef14e63 2019-06-02 stsp usage_backout(void)
7030 5ef14e63 2019-06-02 stsp {
7031 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7032 5ef14e63 2019-06-02 stsp exit(1);
7033 5ef14e63 2019-06-02 stsp }
7034 5ef14e63 2019-06-02 stsp
7035 5ef14e63 2019-06-02 stsp static const struct got_error *
7036 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7037 5ef14e63 2019-06-02 stsp {
7038 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7039 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7040 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7041 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7042 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7043 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7044 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7045 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7046 9627c110 2020-04-18 stsp int ch;
7047 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7048 5ef14e63 2019-06-02 stsp
7049 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7050 5ef14e63 2019-06-02 stsp switch (ch) {
7051 5ef14e63 2019-06-02 stsp default:
7052 5ef14e63 2019-06-02 stsp usage_backout();
7053 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7054 5ef14e63 2019-06-02 stsp }
7055 5ef14e63 2019-06-02 stsp }
7056 5ef14e63 2019-06-02 stsp
7057 5ef14e63 2019-06-02 stsp argc -= optind;
7058 5ef14e63 2019-06-02 stsp argv += optind;
7059 5ef14e63 2019-06-02 stsp
7060 43012d58 2019-07-14 stsp #ifndef PROFILE
7061 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7062 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7063 43012d58 2019-07-14 stsp err(1, "pledge");
7064 43012d58 2019-07-14 stsp #endif
7065 5ef14e63 2019-06-02 stsp if (argc != 1)
7066 5ef14e63 2019-06-02 stsp usage_backout();
7067 5ef14e63 2019-06-02 stsp
7068 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7069 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7070 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7071 5ef14e63 2019-06-02 stsp goto done;
7072 5ef14e63 2019-06-02 stsp }
7073 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7074 fa51e947 2020-03-27 stsp if (error) {
7075 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7076 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7077 5ef14e63 2019-06-02 stsp goto done;
7078 fa51e947 2020-03-27 stsp }
7079 5ef14e63 2019-06-02 stsp
7080 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7081 c9956ddf 2019-09-08 stsp NULL);
7082 5ef14e63 2019-06-02 stsp if (error != NULL)
7083 5ef14e63 2019-06-02 stsp goto done;
7084 5ef14e63 2019-06-02 stsp
7085 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7086 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7087 5ef14e63 2019-06-02 stsp if (error)
7088 5ef14e63 2019-06-02 stsp goto done;
7089 5ef14e63 2019-06-02 stsp
7090 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7091 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7092 5ef14e63 2019-06-02 stsp if (error != NULL) {
7093 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7094 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7095 5ef14e63 2019-06-02 stsp goto done;
7096 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7097 5ef14e63 2019-06-02 stsp if (error != NULL)
7098 5ef14e63 2019-06-02 stsp goto done;
7099 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7100 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7101 5ef14e63 2019-06-02 stsp if (error != NULL)
7102 5ef14e63 2019-06-02 stsp goto done;
7103 5ef14e63 2019-06-02 stsp }
7104 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7105 5ef14e63 2019-06-02 stsp if (error)
7106 5ef14e63 2019-06-02 stsp goto done;
7107 5ef14e63 2019-06-02 stsp
7108 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7109 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7110 5ef14e63 2019-06-02 stsp if (error != NULL)
7111 5ef14e63 2019-06-02 stsp goto done;
7112 5ef14e63 2019-06-02 stsp
7113 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7114 5ef14e63 2019-06-02 stsp if (error)
7115 5ef14e63 2019-06-02 stsp goto done;
7116 5ef14e63 2019-06-02 stsp
7117 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7118 5ef14e63 2019-06-02 stsp if (error)
7119 5ef14e63 2019-06-02 stsp goto done;
7120 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7121 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7122 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7123 5ef14e63 2019-06-02 stsp goto done;
7124 5ef14e63 2019-06-02 stsp }
7125 5ef14e63 2019-06-02 stsp
7126 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7127 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7128 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7129 5ef14e63 2019-06-02 stsp if (error != NULL)
7130 5ef14e63 2019-06-02 stsp goto done;
7131 5ef14e63 2019-06-02 stsp
7132 9627c110 2020-04-18 stsp if (upa.did_something)
7133 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7134 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7135 5ef14e63 2019-06-02 stsp done:
7136 5ef14e63 2019-06-02 stsp if (commit)
7137 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7138 5ef14e63 2019-06-02 stsp free(commit_id_str);
7139 5ef14e63 2019-06-02 stsp if (head_ref)
7140 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7141 818c7501 2019-07-11 stsp if (worktree)
7142 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7143 818c7501 2019-07-11 stsp if (repo)
7144 818c7501 2019-07-11 stsp got_repo_close(repo);
7145 818c7501 2019-07-11 stsp return error;
7146 818c7501 2019-07-11 stsp }
7147 818c7501 2019-07-11 stsp
7148 818c7501 2019-07-11 stsp __dead static void
7149 818c7501 2019-07-11 stsp usage_rebase(void)
7150 818c7501 2019-07-11 stsp {
7151 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7152 af54c8f8 2019-07-11 stsp getprogname());
7153 818c7501 2019-07-11 stsp exit(1);
7154 0ebf8283 2019-07-24 stsp }
7155 0ebf8283 2019-07-24 stsp
7156 0ebf8283 2019-07-24 stsp void
7157 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7158 0ebf8283 2019-07-24 stsp {
7159 0ebf8283 2019-07-24 stsp char *nl;
7160 0ebf8283 2019-07-24 stsp size_t len;
7161 0ebf8283 2019-07-24 stsp
7162 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7163 0ebf8283 2019-07-24 stsp if (len > limit)
7164 0ebf8283 2019-07-24 stsp len = limit;
7165 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7166 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7167 0ebf8283 2019-07-24 stsp if (nl)
7168 0ebf8283 2019-07-24 stsp *nl = '\0';
7169 0ebf8283 2019-07-24 stsp }
7170 0ebf8283 2019-07-24 stsp
7171 0ebf8283 2019-07-24 stsp static const struct got_error *
7172 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7173 0ebf8283 2019-07-24 stsp {
7174 5943eee2 2019-08-13 stsp const struct got_error *err;
7175 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7176 5943eee2 2019-08-13 stsp const char *s;
7177 0ebf8283 2019-07-24 stsp
7178 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7179 5943eee2 2019-08-13 stsp if (err)
7180 5943eee2 2019-08-13 stsp return err;
7181 0ebf8283 2019-07-24 stsp
7182 5943eee2 2019-08-13 stsp s = logmsg0;
7183 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7184 5943eee2 2019-08-13 stsp s++;
7185 5943eee2 2019-08-13 stsp
7186 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7187 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7188 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7189 5943eee2 2019-08-13 stsp goto done;
7190 5943eee2 2019-08-13 stsp }
7191 0ebf8283 2019-07-24 stsp
7192 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7193 5943eee2 2019-08-13 stsp done:
7194 5943eee2 2019-08-13 stsp free(logmsg0);
7195 a0ea4fc0 2020-02-28 stsp return err;
7196 a0ea4fc0 2020-02-28 stsp }
7197 a0ea4fc0 2020-02-28 stsp
7198 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7199 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7200 a0ea4fc0 2020-02-28 stsp {
7201 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7202 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7203 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7204 a0ea4fc0 2020-02-28 stsp
7205 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7206 a0ea4fc0 2020-02-28 stsp if (err)
7207 a0ea4fc0 2020-02-28 stsp return err;
7208 a0ea4fc0 2020-02-28 stsp
7209 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7210 a0ea4fc0 2020-02-28 stsp if (err)
7211 a0ea4fc0 2020-02-28 stsp goto done;
7212 a0ea4fc0 2020-02-28 stsp
7213 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7214 a0ea4fc0 2020-02-28 stsp
7215 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7216 a0ea4fc0 2020-02-28 stsp if (err)
7217 a0ea4fc0 2020-02-28 stsp goto done;
7218 a0ea4fc0 2020-02-28 stsp
7219 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7220 a0ea4fc0 2020-02-28 stsp done:
7221 a0ea4fc0 2020-02-28 stsp free(id_str);
7222 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7223 a0ea4fc0 2020-02-28 stsp free(logmsg);
7224 5943eee2 2019-08-13 stsp return err;
7225 818c7501 2019-07-11 stsp }
7226 818c7501 2019-07-11 stsp
7227 818c7501 2019-07-11 stsp static const struct got_error *
7228 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7229 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7230 818c7501 2019-07-11 stsp {
7231 818c7501 2019-07-11 stsp const struct got_error *err;
7232 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7233 818c7501 2019-07-11 stsp
7234 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7235 818c7501 2019-07-11 stsp if (err)
7236 818c7501 2019-07-11 stsp goto done;
7237 818c7501 2019-07-11 stsp
7238 ff0d2220 2019-07-11 stsp if (new_id) {
7239 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7240 ff0d2220 2019-07-11 stsp if (err)
7241 ff0d2220 2019-07-11 stsp goto done;
7242 ff0d2220 2019-07-11 stsp }
7243 818c7501 2019-07-11 stsp
7244 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7245 ff0d2220 2019-07-11 stsp if (new_id_str)
7246 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7247 0ebf8283 2019-07-24 stsp
7248 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7249 0ebf8283 2019-07-24 stsp if (err)
7250 0ebf8283 2019-07-24 stsp goto done;
7251 0ebf8283 2019-07-24 stsp
7252 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7253 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7254 818c7501 2019-07-11 stsp done:
7255 818c7501 2019-07-11 stsp free(old_id_str);
7256 818c7501 2019-07-11 stsp free(new_id_str);
7257 272a1371 2020-02-28 stsp free(logmsg);
7258 818c7501 2019-07-11 stsp return err;
7259 818c7501 2019-07-11 stsp }
7260 818c7501 2019-07-11 stsp
7261 1ee397ad 2019-07-12 stsp static const struct got_error *
7262 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7263 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7264 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7265 818c7501 2019-07-11 stsp {
7266 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7267 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7268 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7269 818c7501 2019-07-11 stsp }
7270 818c7501 2019-07-11 stsp
7271 818c7501 2019-07-11 stsp static const struct got_error *
7272 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7273 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7274 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7275 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7276 ff0d2220 2019-07-11 stsp {
7277 ff0d2220 2019-07-11 stsp const struct got_error *error;
7278 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7279 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7280 ff0d2220 2019-07-11 stsp
7281 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7282 ff0d2220 2019-07-11 stsp if (error)
7283 ff0d2220 2019-07-11 stsp return error;
7284 ff0d2220 2019-07-11 stsp
7285 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7286 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7287 ff0d2220 2019-07-11 stsp if (error) {
7288 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7289 ff0d2220 2019-07-11 stsp goto done;
7290 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7291 ff0d2220 2019-07-11 stsp } else {
7292 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7293 ff0d2220 2019-07-11 stsp free(new_commit_id);
7294 ff0d2220 2019-07-11 stsp }
7295 ff0d2220 2019-07-11 stsp done:
7296 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7297 ff0d2220 2019-07-11 stsp return error;
7298 64c6d990 2019-07-11 stsp }
7299 64c6d990 2019-07-11 stsp
7300 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7301 64c6d990 2019-07-11 stsp const char *path_prefix;
7302 64c6d990 2019-07-11 stsp size_t len;
7303 8ca9bd68 2019-07-25 stsp int errcode;
7304 64c6d990 2019-07-11 stsp };
7305 64c6d990 2019-07-11 stsp
7306 64c6d990 2019-07-11 stsp static const struct got_error *
7307 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7308 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7309 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7310 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7311 64c6d990 2019-07-11 stsp {
7312 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7313 64c6d990 2019-07-11 stsp
7314 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7315 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7316 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7317 64c6d990 2019-07-11 stsp
7318 64c6d990 2019-07-11 stsp return NULL;
7319 64c6d990 2019-07-11 stsp }
7320 64c6d990 2019-07-11 stsp
7321 64c6d990 2019-07-11 stsp static const struct got_error *
7322 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7323 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7324 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7325 64c6d990 2019-07-11 stsp {
7326 64c6d990 2019-07-11 stsp const struct got_error *err;
7327 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7328 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7329 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7330 64c6d990 2019-07-11 stsp
7331 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7332 64c6d990 2019-07-11 stsp return NULL;
7333 64c6d990 2019-07-11 stsp
7334 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7335 64c6d990 2019-07-11 stsp if (err)
7336 64c6d990 2019-07-11 stsp goto done;
7337 64c6d990 2019-07-11 stsp
7338 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7339 64c6d990 2019-07-11 stsp if (err)
7340 64c6d990 2019-07-11 stsp goto done;
7341 64c6d990 2019-07-11 stsp
7342 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7343 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7344 64c6d990 2019-07-11 stsp if (err)
7345 64c6d990 2019-07-11 stsp goto done;
7346 64c6d990 2019-07-11 stsp
7347 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7348 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7349 64c6d990 2019-07-11 stsp if (err)
7350 64c6d990 2019-07-11 stsp goto done;
7351 64c6d990 2019-07-11 stsp
7352 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7353 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7354 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7355 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7356 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7357 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7358 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7359 64c6d990 2019-07-11 stsp done:
7360 64c6d990 2019-07-11 stsp if (tree1)
7361 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7362 64c6d990 2019-07-11 stsp if (tree2)
7363 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7364 64c6d990 2019-07-11 stsp if (commit)
7365 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7366 64c6d990 2019-07-11 stsp if (parent_commit)
7367 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7368 64c6d990 2019-07-11 stsp return err;
7369 ff0d2220 2019-07-11 stsp }
7370 ff0d2220 2019-07-11 stsp
7371 ff0d2220 2019-07-11 stsp static const struct got_error *
7372 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7373 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7374 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7375 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7376 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7377 af61c510 2019-07-19 stsp {
7378 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7379 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7380 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7381 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7382 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7383 af61c510 2019-07-19 stsp
7384 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7385 af61c510 2019-07-19 stsp if (err)
7386 af61c510 2019-07-19 stsp return err;
7387 af61c510 2019-07-19 stsp
7388 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7389 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7390 af61c510 2019-07-19 stsp if (err)
7391 af61c510 2019-07-19 stsp goto done;
7392 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7393 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7394 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7395 af61c510 2019-07-19 stsp if (err) {
7396 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7397 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7398 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7399 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7400 af61c510 2019-07-19 stsp "been reached?!?");
7401 ee780d5c 2020-01-04 stsp }
7402 ee780d5c 2020-01-04 stsp goto done;
7403 af61c510 2019-07-19 stsp } else {
7404 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7405 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7406 af61c510 2019-07-19 stsp if (err)
7407 af61c510 2019-07-19 stsp goto done;
7408 af61c510 2019-07-19 stsp
7409 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7410 af61c510 2019-07-19 stsp if (err)
7411 af61c510 2019-07-19 stsp goto done;
7412 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7413 af61c510 2019-07-19 stsp commit_id = parent_id;
7414 af61c510 2019-07-19 stsp }
7415 af61c510 2019-07-19 stsp }
7416 af61c510 2019-07-19 stsp done:
7417 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7418 af61c510 2019-07-19 stsp return err;
7419 af61c510 2019-07-19 stsp }
7420 af61c510 2019-07-19 stsp
7421 af61c510 2019-07-19 stsp static const struct got_error *
7422 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7423 818c7501 2019-07-11 stsp {
7424 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7425 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7426 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7427 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7428 818c7501 2019-07-11 stsp char *cwd = NULL;
7429 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7430 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7431 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7432 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7433 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7434 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7435 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7436 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7437 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7438 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7439 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7440 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7441 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7442 818c7501 2019-07-11 stsp
7443 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7444 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7445 818c7501 2019-07-11 stsp
7446 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7447 818c7501 2019-07-11 stsp switch (ch) {
7448 818c7501 2019-07-11 stsp case 'a':
7449 818c7501 2019-07-11 stsp abort_rebase = 1;
7450 818c7501 2019-07-11 stsp break;
7451 818c7501 2019-07-11 stsp case 'c':
7452 818c7501 2019-07-11 stsp continue_rebase = 1;
7453 818c7501 2019-07-11 stsp break;
7454 818c7501 2019-07-11 stsp default:
7455 818c7501 2019-07-11 stsp usage_rebase();
7456 818c7501 2019-07-11 stsp /* NOTREACHED */
7457 818c7501 2019-07-11 stsp }
7458 818c7501 2019-07-11 stsp }
7459 818c7501 2019-07-11 stsp
7460 818c7501 2019-07-11 stsp argc -= optind;
7461 818c7501 2019-07-11 stsp argv += optind;
7462 818c7501 2019-07-11 stsp
7463 43012d58 2019-07-14 stsp #ifndef PROFILE
7464 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7465 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7466 43012d58 2019-07-14 stsp err(1, "pledge");
7467 43012d58 2019-07-14 stsp #endif
7468 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7469 818c7501 2019-07-11 stsp usage_rebase();
7470 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7471 818c7501 2019-07-11 stsp if (argc != 0)
7472 818c7501 2019-07-11 stsp usage_rebase();
7473 818c7501 2019-07-11 stsp } else if (argc != 1)
7474 818c7501 2019-07-11 stsp usage_rebase();
7475 818c7501 2019-07-11 stsp
7476 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7477 818c7501 2019-07-11 stsp if (cwd == NULL) {
7478 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7479 818c7501 2019-07-11 stsp goto done;
7480 818c7501 2019-07-11 stsp }
7481 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7482 fa51e947 2020-03-27 stsp if (error) {
7483 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7484 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7485 818c7501 2019-07-11 stsp goto done;
7486 fa51e947 2020-03-27 stsp }
7487 818c7501 2019-07-11 stsp
7488 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7489 c9956ddf 2019-09-08 stsp NULL);
7490 818c7501 2019-07-11 stsp if (error != NULL)
7491 818c7501 2019-07-11 stsp goto done;
7492 818c7501 2019-07-11 stsp
7493 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7494 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7495 7ef62c4e 2020-02-24 stsp if (error)
7496 7ef62c4e 2020-02-24 stsp goto done;
7497 7ef62c4e 2020-02-24 stsp
7498 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7499 7ef62c4e 2020-02-24 stsp worktree);
7500 818c7501 2019-07-11 stsp if (error)
7501 7ef62c4e 2020-02-24 stsp goto done;
7502 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7503 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7504 818c7501 2019-07-11 stsp goto done;
7505 7ef62c4e 2020-02-24 stsp }
7506 818c7501 2019-07-11 stsp
7507 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7508 818c7501 2019-07-11 stsp if (error)
7509 818c7501 2019-07-11 stsp goto done;
7510 818c7501 2019-07-11 stsp
7511 f6794adc 2019-07-23 stsp if (abort_rebase) {
7512 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7513 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7514 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7515 f6794adc 2019-07-23 stsp goto done;
7516 f6794adc 2019-07-23 stsp }
7517 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7518 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7519 3e3a69f1 2019-07-25 stsp worktree, repo);
7520 818c7501 2019-07-11 stsp if (error)
7521 818c7501 2019-07-11 stsp goto done;
7522 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7523 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7524 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7525 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7526 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7527 818c7501 2019-07-11 stsp if (error)
7528 818c7501 2019-07-11 stsp goto done;
7529 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7530 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7531 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7532 818c7501 2019-07-11 stsp }
7533 818c7501 2019-07-11 stsp
7534 818c7501 2019-07-11 stsp if (continue_rebase) {
7535 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7536 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7537 f6794adc 2019-07-23 stsp goto done;
7538 f6794adc 2019-07-23 stsp }
7539 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7540 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7541 3e3a69f1 2019-07-25 stsp worktree, repo);
7542 818c7501 2019-07-11 stsp if (error)
7543 818c7501 2019-07-11 stsp goto done;
7544 818c7501 2019-07-11 stsp
7545 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7546 01757395 2019-07-12 stsp resume_commit_id, repo);
7547 818c7501 2019-07-11 stsp if (error)
7548 818c7501 2019-07-11 stsp goto done;
7549 818c7501 2019-07-11 stsp
7550 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7551 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7552 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7553 818c7501 2019-07-11 stsp goto done;
7554 818c7501 2019-07-11 stsp }
7555 818c7501 2019-07-11 stsp } else {
7556 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7557 818c7501 2019-07-11 stsp if (error != NULL)
7558 818c7501 2019-07-11 stsp goto done;
7559 ff0d2220 2019-07-11 stsp }
7560 818c7501 2019-07-11 stsp
7561 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7562 ff0d2220 2019-07-11 stsp if (error)
7563 ff0d2220 2019-07-11 stsp goto done;
7564 ff0d2220 2019-07-11 stsp
7565 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7566 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7567 a51a74b3 2019-07-27 stsp
7568 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7569 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7570 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7571 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7572 818c7501 2019-07-11 stsp if (error)
7573 818c7501 2019-07-11 stsp goto done;
7574 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7575 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7576 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7577 818c7501 2019-07-11 stsp "with work tree's branch");
7578 818c7501 2019-07-11 stsp goto done;
7579 818c7501 2019-07-11 stsp }
7580 818c7501 2019-07-11 stsp
7581 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7582 a51a74b3 2019-07-27 stsp if (error) {
7583 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7584 a51a74b3 2019-07-27 stsp goto done;
7585 a51a74b3 2019-07-27 stsp error = NULL;
7586 a51a74b3 2019-07-27 stsp } else {
7587 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7588 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7589 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7590 a51a74b3 2019-07-27 stsp goto done;
7591 a51a74b3 2019-07-27 stsp }
7592 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7593 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7594 818c7501 2019-07-11 stsp if (error)
7595 818c7501 2019-07-11 stsp goto done;
7596 818c7501 2019-07-11 stsp }
7597 818c7501 2019-07-11 stsp
7598 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7599 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7600 818c7501 2019-07-11 stsp if (error)
7601 818c7501 2019-07-11 stsp goto done;
7602 818c7501 2019-07-11 stsp
7603 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7604 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7605 fc66b545 2019-08-12 stsp if (pid == NULL) {
7606 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7607 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7608 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7609 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7610 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7611 fc66b545 2019-08-12 stsp if (error)
7612 fc66b545 2019-08-12 stsp goto done;
7613 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7614 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7615 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7616 9627c110 2020-04-18 stsp
7617 fc66b545 2019-08-12 stsp }
7618 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7619 fc66b545 2019-08-12 stsp goto done;
7620 fc66b545 2019-08-12 stsp }
7621 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7622 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7623 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7624 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7625 818c7501 2019-07-11 stsp commit = NULL;
7626 818c7501 2019-07-11 stsp if (error)
7627 818c7501 2019-07-11 stsp goto done;
7628 64c6d990 2019-07-11 stsp
7629 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7630 38b0338b 2019-11-29 stsp if (continue_rebase) {
7631 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7632 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7633 38b0338b 2019-11-29 stsp goto done;
7634 38b0338b 2019-11-29 stsp } else {
7635 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7636 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7637 38b0338b 2019-11-29 stsp char *id_str;
7638 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7639 38b0338b 2019-11-29 stsp new_base_branch);
7640 38b0338b 2019-11-29 stsp if (error)
7641 38b0338b 2019-11-29 stsp goto done;
7642 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7643 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7644 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7645 38b0338b 2019-11-29 stsp free(id_str);
7646 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7647 38b0338b 2019-11-29 stsp new_head_commit_id);
7648 38b0338b 2019-11-29 stsp if (error)
7649 38b0338b 2019-11-29 stsp goto done;
7650 38b0338b 2019-11-29 stsp }
7651 818c7501 2019-07-11 stsp }
7652 818c7501 2019-07-11 stsp
7653 818c7501 2019-07-11 stsp pid = NULL;
7654 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7655 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7656 9627c110 2020-04-18 stsp
7657 818c7501 2019-07-11 stsp commit_id = qid->id;
7658 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7659 818c7501 2019-07-11 stsp pid = qid;
7660 818c7501 2019-07-11 stsp
7661 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7662 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7663 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7664 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7665 818c7501 2019-07-11 stsp if (error)
7666 818c7501 2019-07-11 stsp goto done;
7667 9627c110 2020-04-18 stsp
7668 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7669 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7670 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7671 818c7501 2019-07-11 stsp
7672 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7673 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7674 a0ea4fc0 2020-02-28 stsp if (error)
7675 a0ea4fc0 2020-02-28 stsp goto done;
7676 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7677 818c7501 2019-07-11 stsp break;
7678 01757395 2019-07-12 stsp }
7679 818c7501 2019-07-11 stsp
7680 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7681 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7682 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7683 818c7501 2019-07-11 stsp if (error)
7684 818c7501 2019-07-11 stsp goto done;
7685 818c7501 2019-07-11 stsp }
7686 818c7501 2019-07-11 stsp
7687 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7688 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7689 818c7501 2019-07-11 stsp if (error)
7690 818c7501 2019-07-11 stsp goto done;
7691 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7692 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7693 818c7501 2019-07-11 stsp } else
7694 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7695 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7696 818c7501 2019-07-11 stsp done:
7697 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7698 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7699 818c7501 2019-07-11 stsp free(resume_commit_id);
7700 818c7501 2019-07-11 stsp free(yca_id);
7701 818c7501 2019-07-11 stsp if (commit)
7702 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7703 818c7501 2019-07-11 stsp if (branch)
7704 818c7501 2019-07-11 stsp got_ref_close(branch);
7705 818c7501 2019-07-11 stsp if (new_base_branch)
7706 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7707 818c7501 2019-07-11 stsp if (tmp_branch)
7708 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7709 5ef14e63 2019-06-02 stsp if (worktree)
7710 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7711 5ef14e63 2019-06-02 stsp if (repo)
7712 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7713 5ef14e63 2019-06-02 stsp return error;
7714 0ebf8283 2019-07-24 stsp }
7715 0ebf8283 2019-07-24 stsp
7716 0ebf8283 2019-07-24 stsp __dead static void
7717 0ebf8283 2019-07-24 stsp usage_histedit(void)
7718 0ebf8283 2019-07-24 stsp {
7719 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7720 0ebf8283 2019-07-24 stsp getprogname());
7721 0ebf8283 2019-07-24 stsp exit(1);
7722 0ebf8283 2019-07-24 stsp }
7723 0ebf8283 2019-07-24 stsp
7724 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7725 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7726 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7727 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7728 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7729 0ebf8283 2019-07-24 stsp
7730 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7731 0ebf8283 2019-07-24 stsp unsigned char code;
7732 0ebf8283 2019-07-24 stsp const char *name;
7733 0ebf8283 2019-07-24 stsp const char *desc;
7734 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7735 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7736 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7737 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7738 82997472 2020-01-29 stsp "be used" },
7739 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7740 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7741 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7742 0ebf8283 2019-07-24 stsp };
7743 0ebf8283 2019-07-24 stsp
7744 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7745 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7746 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7747 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7748 0ebf8283 2019-07-24 stsp char *logmsg;
7749 0ebf8283 2019-07-24 stsp };
7750 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7751 0ebf8283 2019-07-24 stsp
7752 0ebf8283 2019-07-24 stsp static const struct got_error *
7753 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7754 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7755 0ebf8283 2019-07-24 stsp {
7756 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7757 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7758 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7759 8138f3e1 2019-08-11 stsp int n;
7760 0ebf8283 2019-07-24 stsp
7761 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7762 0ebf8283 2019-07-24 stsp if (err)
7763 0ebf8283 2019-07-24 stsp goto done;
7764 0ebf8283 2019-07-24 stsp
7765 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7766 0ebf8283 2019-07-24 stsp if (err)
7767 0ebf8283 2019-07-24 stsp goto done;
7768 0ebf8283 2019-07-24 stsp
7769 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7770 0ebf8283 2019-07-24 stsp if (err)
7771 0ebf8283 2019-07-24 stsp goto done;
7772 0ebf8283 2019-07-24 stsp
7773 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7774 0ebf8283 2019-07-24 stsp if (n < 0)
7775 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7776 0ebf8283 2019-07-24 stsp done:
7777 0ebf8283 2019-07-24 stsp if (commit)
7778 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7779 0ebf8283 2019-07-24 stsp free(id_str);
7780 0ebf8283 2019-07-24 stsp free(logmsg);
7781 0ebf8283 2019-07-24 stsp return err;
7782 0ebf8283 2019-07-24 stsp }
7783 0ebf8283 2019-07-24 stsp
7784 0ebf8283 2019-07-24 stsp static const struct got_error *
7785 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7786 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7787 0ebf8283 2019-07-24 stsp {
7788 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7789 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7790 0ebf8283 2019-07-24 stsp
7791 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7792 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7793 0ebf8283 2019-07-24 stsp
7794 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7795 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7796 0ebf8283 2019-07-24 stsp f, repo);
7797 0ebf8283 2019-07-24 stsp if (err)
7798 0ebf8283 2019-07-24 stsp break;
7799 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7800 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7801 083957f4 2020-02-24 stsp if (n < 0) {
7802 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7803 083957f4 2020-02-24 stsp break;
7804 083957f4 2020-02-24 stsp }
7805 083957f4 2020-02-24 stsp }
7806 0ebf8283 2019-07-24 stsp }
7807 0ebf8283 2019-07-24 stsp
7808 0ebf8283 2019-07-24 stsp return err;
7809 0ebf8283 2019-07-24 stsp }
7810 0ebf8283 2019-07-24 stsp
7811 0ebf8283 2019-07-24 stsp static const struct got_error *
7812 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7813 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7814 0ebf8283 2019-07-24 stsp {
7815 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7816 0ebf8283 2019-07-24 stsp int n, i;
7817 514f2ffe 2020-01-29 stsp char *id_str;
7818 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7819 514f2ffe 2020-01-29 stsp
7820 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7821 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7822 514f2ffe 2020-01-29 stsp if (err)
7823 514f2ffe 2020-01-29 stsp return err;
7824 514f2ffe 2020-01-29 stsp
7825 514f2ffe 2020-01-29 stsp n = fprintf(f,
7826 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7827 514f2ffe 2020-01-29 stsp "# commit %s\n"
7828 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7829 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7830 514f2ffe 2020-01-29 stsp if (n < 0) {
7831 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7832 514f2ffe 2020-01-29 stsp goto done;
7833 514f2ffe 2020-01-29 stsp }
7834 0ebf8283 2019-07-24 stsp
7835 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7836 514f2ffe 2020-01-29 stsp if (n < 0) {
7837 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7838 514f2ffe 2020-01-29 stsp goto done;
7839 514f2ffe 2020-01-29 stsp }
7840 0ebf8283 2019-07-24 stsp
7841 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7842 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7843 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7844 0ebf8283 2019-07-24 stsp cmd->desc);
7845 0ebf8283 2019-07-24 stsp if (n < 0) {
7846 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7847 0ebf8283 2019-07-24 stsp break;
7848 0ebf8283 2019-07-24 stsp }
7849 0ebf8283 2019-07-24 stsp }
7850 514f2ffe 2020-01-29 stsp done:
7851 514f2ffe 2020-01-29 stsp free(id_str);
7852 0ebf8283 2019-07-24 stsp return err;
7853 0ebf8283 2019-07-24 stsp }
7854 0ebf8283 2019-07-24 stsp
7855 0ebf8283 2019-07-24 stsp static const struct got_error *
7856 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7857 0ebf8283 2019-07-24 stsp {
7858 0ebf8283 2019-07-24 stsp static char msg[42];
7859 0ebf8283 2019-07-24 stsp int ret;
7860 0ebf8283 2019-07-24 stsp
7861 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7862 0ebf8283 2019-07-24 stsp lineno);
7863 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7864 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7865 0ebf8283 2019-07-24 stsp
7866 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7867 0ebf8283 2019-07-24 stsp }
7868 0ebf8283 2019-07-24 stsp
7869 0ebf8283 2019-07-24 stsp static const struct got_error *
7870 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7871 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7872 0ebf8283 2019-07-24 stsp {
7873 0ebf8283 2019-07-24 stsp const struct got_error *err;
7874 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7875 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7876 0ebf8283 2019-07-24 stsp
7877 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7878 0ebf8283 2019-07-24 stsp if (err)
7879 0ebf8283 2019-07-24 stsp return err;
7880 0ebf8283 2019-07-24 stsp
7881 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7882 0ebf8283 2019-07-24 stsp if (err)
7883 0ebf8283 2019-07-24 stsp goto done;
7884 0ebf8283 2019-07-24 stsp
7885 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7886 5943eee2 2019-08-13 stsp if (err)
7887 5943eee2 2019-08-13 stsp goto done;
7888 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7889 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7890 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7891 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7892 0ebf8283 2019-07-24 stsp }
7893 0ebf8283 2019-07-24 stsp done:
7894 0ebf8283 2019-07-24 stsp if (folded_commit)
7895 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7896 0ebf8283 2019-07-24 stsp free(id_str);
7897 5943eee2 2019-08-13 stsp free(folded_logmsg);
7898 0ebf8283 2019-07-24 stsp return err;
7899 0ebf8283 2019-07-24 stsp }
7900 0ebf8283 2019-07-24 stsp
7901 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7902 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7903 0ebf8283 2019-07-24 stsp {
7904 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7905 0ebf8283 2019-07-24 stsp
7906 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7907 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7908 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7909 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7910 3f9de99f 2019-07-24 stsp folded = prev;
7911 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7912 0ebf8283 2019-07-24 stsp }
7913 0ebf8283 2019-07-24 stsp
7914 0ebf8283 2019-07-24 stsp return folded;
7915 0ebf8283 2019-07-24 stsp }
7916 0ebf8283 2019-07-24 stsp
7917 0ebf8283 2019-07-24 stsp static const struct got_error *
7918 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7919 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7920 0ebf8283 2019-07-24 stsp {
7921 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7922 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7923 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7924 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7925 1601cb9f 2020-09-11 naddy int logmsg_len;
7926 0ebf8283 2019-07-24 stsp int fd;
7927 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7928 0ebf8283 2019-07-24 stsp
7929 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7930 0ebf8283 2019-07-24 stsp if (err)
7931 0ebf8283 2019-07-24 stsp return err;
7932 0ebf8283 2019-07-24 stsp
7933 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7934 0ebf8283 2019-07-24 stsp if (folded) {
7935 0ebf8283 2019-07-24 stsp while (folded != hle) {
7936 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7937 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7938 3f9de99f 2019-07-24 stsp continue;
7939 3f9de99f 2019-07-24 stsp }
7940 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7941 0ebf8283 2019-07-24 stsp logmsg, repo);
7942 0ebf8283 2019-07-24 stsp if (err)
7943 0ebf8283 2019-07-24 stsp goto done;
7944 0ebf8283 2019-07-24 stsp free(logmsg);
7945 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7946 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7947 0ebf8283 2019-07-24 stsp }
7948 0ebf8283 2019-07-24 stsp }
7949 0ebf8283 2019-07-24 stsp
7950 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7951 0ebf8283 2019-07-24 stsp if (err)
7952 0ebf8283 2019-07-24 stsp goto done;
7953 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7954 5943eee2 2019-08-13 stsp if (err)
7955 5943eee2 2019-08-13 stsp goto done;
7956 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
7957 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7958 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
7959 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
7960 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7961 0ebf8283 2019-07-24 stsp goto done;
7962 0ebf8283 2019-07-24 stsp }
7963 0ebf8283 2019-07-24 stsp free(logmsg);
7964 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7965 0ebf8283 2019-07-24 stsp
7966 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7967 0ebf8283 2019-07-24 stsp if (err)
7968 0ebf8283 2019-07-24 stsp goto done;
7969 0ebf8283 2019-07-24 stsp
7970 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7971 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7972 0ebf8283 2019-07-24 stsp if (err)
7973 0ebf8283 2019-07-24 stsp goto done;
7974 0ebf8283 2019-07-24 stsp
7975 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
7976 0ebf8283 2019-07-24 stsp close(fd);
7977 0ebf8283 2019-07-24 stsp
7978 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7979 0ebf8283 2019-07-24 stsp if (err)
7980 0ebf8283 2019-07-24 stsp goto done;
7981 0ebf8283 2019-07-24 stsp
7982 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7983 0ebf8283 2019-07-24 stsp if (err) {
7984 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7985 0ebf8283 2019-07-24 stsp goto done;
7986 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7987 0ebf8283 2019-07-24 stsp }
7988 0ebf8283 2019-07-24 stsp done:
7989 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7990 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7991 0ebf8283 2019-07-24 stsp free(logmsg_path);
7992 0ebf8283 2019-07-24 stsp free(logmsg);
7993 5943eee2 2019-08-13 stsp free(orig_logmsg);
7994 0ebf8283 2019-07-24 stsp free(editor);
7995 0ebf8283 2019-07-24 stsp if (commit)
7996 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7997 0ebf8283 2019-07-24 stsp return err;
7998 5ef14e63 2019-06-02 stsp }
7999 0ebf8283 2019-07-24 stsp
8000 0ebf8283 2019-07-24 stsp static const struct got_error *
8001 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
8002 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8003 0ebf8283 2019-07-24 stsp {
8004 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8005 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
8006 0ebf8283 2019-07-24 stsp size_t size;
8007 0ebf8283 2019-07-24 stsp ssize_t len;
8008 0ebf8283 2019-07-24 stsp int lineno = 0, i;
8009 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8010 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8011 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8012 0ebf8283 2019-07-24 stsp
8013 0ebf8283 2019-07-24 stsp for (;;) {
8014 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8015 0ebf8283 2019-07-24 stsp if (len == -1) {
8016 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8017 0ebf8283 2019-07-24 stsp if (feof(f))
8018 0ebf8283 2019-07-24 stsp break;
8019 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8020 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8021 0ebf8283 2019-07-24 stsp break;
8022 0ebf8283 2019-07-24 stsp }
8023 0ebf8283 2019-07-24 stsp lineno++;
8024 0ebf8283 2019-07-24 stsp p = line;
8025 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8026 0ebf8283 2019-07-24 stsp p++;
8027 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8028 0ebf8283 2019-07-24 stsp free(line);
8029 0ebf8283 2019-07-24 stsp line = NULL;
8030 0ebf8283 2019-07-24 stsp continue;
8031 0ebf8283 2019-07-24 stsp }
8032 0ebf8283 2019-07-24 stsp cmd = NULL;
8033 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8034 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8035 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8036 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8037 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8038 0ebf8283 2019-07-24 stsp break;
8039 0ebf8283 2019-07-24 stsp }
8040 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8041 0ebf8283 2019-07-24 stsp p++;
8042 0ebf8283 2019-07-24 stsp break;
8043 0ebf8283 2019-07-24 stsp }
8044 0ebf8283 2019-07-24 stsp }
8045 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8046 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8047 0ebf8283 2019-07-24 stsp break;
8048 0ebf8283 2019-07-24 stsp }
8049 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8050 0ebf8283 2019-07-24 stsp p++;
8051 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8052 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8053 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8054 0ebf8283 2019-07-24 stsp break;
8055 0ebf8283 2019-07-24 stsp }
8056 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8057 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8058 0ebf8283 2019-07-24 stsp if (err)
8059 0ebf8283 2019-07-24 stsp break;
8060 0ebf8283 2019-07-24 stsp } else {
8061 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8062 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8063 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8064 0ebf8283 2019-07-24 stsp break;
8065 0ebf8283 2019-07-24 stsp }
8066 0ebf8283 2019-07-24 stsp }
8067 0ebf8283 2019-07-24 stsp free(line);
8068 0ebf8283 2019-07-24 stsp line = NULL;
8069 0ebf8283 2019-07-24 stsp continue;
8070 0ebf8283 2019-07-24 stsp } else {
8071 0ebf8283 2019-07-24 stsp end = p;
8072 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8073 0ebf8283 2019-07-24 stsp end++;
8074 0ebf8283 2019-07-24 stsp *end = '\0';
8075 0ebf8283 2019-07-24 stsp
8076 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8077 0ebf8283 2019-07-24 stsp if (err) {
8078 0ebf8283 2019-07-24 stsp /* override error code */
8079 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8080 0ebf8283 2019-07-24 stsp break;
8081 0ebf8283 2019-07-24 stsp }
8082 0ebf8283 2019-07-24 stsp }
8083 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8084 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8085 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8086 0ebf8283 2019-07-24 stsp break;
8087 0ebf8283 2019-07-24 stsp }
8088 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8089 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8090 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8091 0ebf8283 2019-07-24 stsp commit_id = NULL;
8092 0ebf8283 2019-07-24 stsp free(line);
8093 0ebf8283 2019-07-24 stsp line = NULL;
8094 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8095 0ebf8283 2019-07-24 stsp }
8096 0ebf8283 2019-07-24 stsp
8097 0ebf8283 2019-07-24 stsp free(line);
8098 0ebf8283 2019-07-24 stsp free(commit_id);
8099 0ebf8283 2019-07-24 stsp return err;
8100 0ebf8283 2019-07-24 stsp }
8101 0ebf8283 2019-07-24 stsp
8102 0ebf8283 2019-07-24 stsp static const struct got_error *
8103 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8104 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8105 bfce7f83 2019-07-27 stsp {
8106 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8107 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8108 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8109 5b87815e 2020-03-05 stsp static char msg[92];
8110 bfce7f83 2019-07-27 stsp char *id_str;
8111 bfce7f83 2019-07-27 stsp
8112 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8113 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8114 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8115 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
8116 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8117 5b87815e 2020-03-05 stsp
8118 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8119 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8120 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8121 5b87815e 2020-03-05 stsp if (hle == hle2)
8122 5b87815e 2020-03-05 stsp continue;
8123 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8124 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8125 5b87815e 2020-03-05 stsp continue;
8126 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8127 5b87815e 2020-03-05 stsp if (err)
8128 5b87815e 2020-03-05 stsp return err;
8129 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8130 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8131 5b87815e 2020-03-05 stsp free(id_str);
8132 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8133 5b87815e 2020-03-05 stsp }
8134 5b87815e 2020-03-05 stsp }
8135 bfce7f83 2019-07-27 stsp
8136 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8137 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8138 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8139 bfce7f83 2019-07-27 stsp break;
8140 bfce7f83 2019-07-27 stsp }
8141 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8142 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8143 bfce7f83 2019-07-27 stsp if (err)
8144 bfce7f83 2019-07-27 stsp return err;
8145 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8146 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8147 bfce7f83 2019-07-27 stsp free(id_str);
8148 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8149 bfce7f83 2019-07-27 stsp }
8150 bfce7f83 2019-07-27 stsp }
8151 bfce7f83 2019-07-27 stsp
8152 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8153 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8154 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8155 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8156 bfce7f83 2019-07-27 stsp
8157 bfce7f83 2019-07-27 stsp return NULL;
8158 bfce7f83 2019-07-27 stsp }
8159 bfce7f83 2019-07-27 stsp
8160 bfce7f83 2019-07-27 stsp static const struct got_error *
8161 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8162 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8163 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8164 0ebf8283 2019-07-24 stsp {
8165 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8166 0ebf8283 2019-07-24 stsp char *editor;
8167 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8168 0ebf8283 2019-07-24 stsp
8169 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8170 0ebf8283 2019-07-24 stsp if (err)
8171 0ebf8283 2019-07-24 stsp return err;
8172 0ebf8283 2019-07-24 stsp
8173 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8174 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8175 0ebf8283 2019-07-24 stsp goto done;
8176 0ebf8283 2019-07-24 stsp }
8177 0ebf8283 2019-07-24 stsp
8178 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8179 0ebf8283 2019-07-24 stsp if (f == NULL) {
8180 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8181 0ebf8283 2019-07-24 stsp goto done;
8182 0ebf8283 2019-07-24 stsp }
8183 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8184 bfce7f83 2019-07-27 stsp if (err)
8185 bfce7f83 2019-07-27 stsp goto done;
8186 bfce7f83 2019-07-27 stsp
8187 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8188 0ebf8283 2019-07-24 stsp done:
8189 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8190 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8191 0ebf8283 2019-07-24 stsp free(editor);
8192 0ebf8283 2019-07-24 stsp return err;
8193 0ebf8283 2019-07-24 stsp }
8194 0ebf8283 2019-07-24 stsp
8195 0ebf8283 2019-07-24 stsp static const struct got_error *
8196 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8197 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8198 514f2ffe 2020-01-29 stsp struct got_repository *);
8199 0ebf8283 2019-07-24 stsp
8200 0ebf8283 2019-07-24 stsp static const struct got_error *
8201 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8202 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8203 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
8204 0ebf8283 2019-07-24 stsp {
8205 0ebf8283 2019-07-24 stsp const struct got_error *err;
8206 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8207 0ebf8283 2019-07-24 stsp char *path = NULL;
8208 0ebf8283 2019-07-24 stsp
8209 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8210 0ebf8283 2019-07-24 stsp if (err)
8211 0ebf8283 2019-07-24 stsp return err;
8212 0ebf8283 2019-07-24 stsp
8213 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8214 0ebf8283 2019-07-24 stsp if (err)
8215 0ebf8283 2019-07-24 stsp goto done;
8216 0ebf8283 2019-07-24 stsp
8217 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8218 0ebf8283 2019-07-24 stsp if (err)
8219 0ebf8283 2019-07-24 stsp goto done;
8220 0ebf8283 2019-07-24 stsp
8221 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8222 083957f4 2020-02-24 stsp rewind(f);
8223 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8224 083957f4 2020-02-24 stsp } else {
8225 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8226 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8227 0ebf8283 2019-07-24 stsp goto done;
8228 083957f4 2020-02-24 stsp }
8229 083957f4 2020-02-24 stsp f = NULL;
8230 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8231 083957f4 2020-02-24 stsp if (err) {
8232 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8233 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8234 083957f4 2020-02-24 stsp goto done;
8235 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8236 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8237 083957f4 2020-02-24 stsp }
8238 0ebf8283 2019-07-24 stsp }
8239 0ebf8283 2019-07-24 stsp done:
8240 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8241 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8242 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8243 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8244 0ebf8283 2019-07-24 stsp free(path);
8245 0ebf8283 2019-07-24 stsp return err;
8246 0ebf8283 2019-07-24 stsp }
8247 0ebf8283 2019-07-24 stsp
8248 0ebf8283 2019-07-24 stsp static const struct got_error *
8249 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8250 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8251 0ebf8283 2019-07-24 stsp {
8252 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8253 0ebf8283 2019-07-24 stsp char *path = NULL;
8254 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8255 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8256 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8257 0ebf8283 2019-07-24 stsp
8258 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8259 0ebf8283 2019-07-24 stsp if (err)
8260 0ebf8283 2019-07-24 stsp return err;
8261 0ebf8283 2019-07-24 stsp
8262 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8263 0ebf8283 2019-07-24 stsp if (f == NULL) {
8264 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8265 0ebf8283 2019-07-24 stsp goto done;
8266 0ebf8283 2019-07-24 stsp }
8267 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8268 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8269 0ebf8283 2019-07-24 stsp repo);
8270 0ebf8283 2019-07-24 stsp if (err)
8271 0ebf8283 2019-07-24 stsp break;
8272 0ebf8283 2019-07-24 stsp
8273 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8274 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8275 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8276 0ebf8283 2019-07-24 stsp if (n < 0) {
8277 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8278 0ebf8283 2019-07-24 stsp break;
8279 0ebf8283 2019-07-24 stsp }
8280 0ebf8283 2019-07-24 stsp }
8281 0ebf8283 2019-07-24 stsp }
8282 0ebf8283 2019-07-24 stsp done:
8283 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8284 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8285 0ebf8283 2019-07-24 stsp free(path);
8286 0ebf8283 2019-07-24 stsp if (commit)
8287 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8288 0ebf8283 2019-07-24 stsp return err;
8289 0ebf8283 2019-07-24 stsp }
8290 0ebf8283 2019-07-24 stsp
8291 bfce7f83 2019-07-27 stsp void
8292 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8293 bfce7f83 2019-07-27 stsp {
8294 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8295 bfce7f83 2019-07-27 stsp
8296 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8297 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8298 bfce7f83 2019-07-27 stsp free(hle);
8299 bfce7f83 2019-07-27 stsp }
8300 bfce7f83 2019-07-27 stsp }
8301 bfce7f83 2019-07-27 stsp
8302 0ebf8283 2019-07-24 stsp static const struct got_error *
8303 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8304 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8305 0ebf8283 2019-07-24 stsp {
8306 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8307 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8308 0ebf8283 2019-07-24 stsp
8309 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8310 0ebf8283 2019-07-24 stsp if (f == NULL) {
8311 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8312 0ebf8283 2019-07-24 stsp goto done;
8313 0ebf8283 2019-07-24 stsp }
8314 0ebf8283 2019-07-24 stsp
8315 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8316 0ebf8283 2019-07-24 stsp done:
8317 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8318 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8319 0ebf8283 2019-07-24 stsp return err;
8320 0ebf8283 2019-07-24 stsp }
8321 0ebf8283 2019-07-24 stsp
8322 0ebf8283 2019-07-24 stsp static const struct got_error *
8323 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8324 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8325 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8326 0ebf8283 2019-07-24 stsp {
8327 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8328 0ebf8283 2019-07-24 stsp int resp = ' ';
8329 0ebf8283 2019-07-24 stsp
8330 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8331 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8332 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8333 0ebf8283 2019-07-24 stsp resp = getchar();
8334 a61a4414 2019-08-07 stsp if (resp == '\n')
8335 a61a4414 2019-08-07 stsp resp = getchar();
8336 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8337 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8338 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8339 bfce7f83 2019-07-27 stsp repo);
8340 426ebf2e 2019-07-25 stsp if (err) {
8341 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8342 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8343 426ebf2e 2019-07-25 stsp break;
8344 bfce7f83 2019-07-27 stsp prev_err = err;
8345 426ebf2e 2019-07-25 stsp resp = ' ';
8346 426ebf2e 2019-07-25 stsp continue;
8347 426ebf2e 2019-07-25 stsp }
8348 bfce7f83 2019-07-27 stsp break;
8349 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8350 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8351 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8352 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8353 426ebf2e 2019-07-25 stsp if (err) {
8354 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8355 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8356 426ebf2e 2019-07-25 stsp break;
8357 bfce7f83 2019-07-27 stsp prev_err = err;
8358 426ebf2e 2019-07-25 stsp resp = ' ';
8359 426ebf2e 2019-07-25 stsp continue;
8360 426ebf2e 2019-07-25 stsp }
8361 bfce7f83 2019-07-27 stsp break;
8362 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8363 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8364 0ebf8283 2019-07-24 stsp break;
8365 426ebf2e 2019-07-25 stsp } else
8366 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8367 0ebf8283 2019-07-24 stsp }
8368 0ebf8283 2019-07-24 stsp
8369 0ebf8283 2019-07-24 stsp return err;
8370 0ebf8283 2019-07-24 stsp }
8371 0ebf8283 2019-07-24 stsp
8372 0ebf8283 2019-07-24 stsp static const struct got_error *
8373 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8374 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8375 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8376 0ebf8283 2019-07-24 stsp {
8377 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8378 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8379 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8380 3e3a69f1 2019-07-25 stsp branch, repo);
8381 0ebf8283 2019-07-24 stsp }
8382 0ebf8283 2019-07-24 stsp
8383 0ebf8283 2019-07-24 stsp static const struct got_error *
8384 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8385 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8386 0ebf8283 2019-07-24 stsp {
8387 0ebf8283 2019-07-24 stsp const struct got_error *err;
8388 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8389 0ebf8283 2019-07-24 stsp
8390 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8391 0ebf8283 2019-07-24 stsp if (err)
8392 0ebf8283 2019-07-24 stsp goto done;
8393 0ebf8283 2019-07-24 stsp
8394 0ebf8283 2019-07-24 stsp if (new_id) {
8395 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8396 0ebf8283 2019-07-24 stsp if (err)
8397 0ebf8283 2019-07-24 stsp goto done;
8398 0ebf8283 2019-07-24 stsp }
8399 0ebf8283 2019-07-24 stsp
8400 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8401 0ebf8283 2019-07-24 stsp if (new_id_str)
8402 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8403 0ebf8283 2019-07-24 stsp
8404 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8405 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8406 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8407 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8408 0ebf8283 2019-07-24 stsp goto done;
8409 0ebf8283 2019-07-24 stsp }
8410 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8411 0ebf8283 2019-07-24 stsp } else {
8412 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8413 0ebf8283 2019-07-24 stsp if (err)
8414 0ebf8283 2019-07-24 stsp goto done;
8415 0ebf8283 2019-07-24 stsp }
8416 0ebf8283 2019-07-24 stsp
8417 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8418 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8419 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8420 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8421 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8422 0ebf8283 2019-07-24 stsp break;
8423 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8424 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8425 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8426 0ebf8283 2019-07-24 stsp logmsg);
8427 0ebf8283 2019-07-24 stsp break;
8428 0ebf8283 2019-07-24 stsp default:
8429 0ebf8283 2019-07-24 stsp break;
8430 0ebf8283 2019-07-24 stsp }
8431 0ebf8283 2019-07-24 stsp done:
8432 0ebf8283 2019-07-24 stsp free(old_id_str);
8433 0ebf8283 2019-07-24 stsp free(new_id_str);
8434 0ebf8283 2019-07-24 stsp return err;
8435 0ebf8283 2019-07-24 stsp }
8436 0ebf8283 2019-07-24 stsp
8437 0ebf8283 2019-07-24 stsp static const struct got_error *
8438 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8439 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8440 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8441 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8442 0ebf8283 2019-07-24 stsp {
8443 0ebf8283 2019-07-24 stsp const struct got_error *err;
8444 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8445 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8446 0ebf8283 2019-07-24 stsp
8447 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8448 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8449 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8450 0ebf8283 2019-07-24 stsp if (err)
8451 0ebf8283 2019-07-24 stsp return err;
8452 0ebf8283 2019-07-24 stsp }
8453 0ebf8283 2019-07-24 stsp
8454 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8455 0ebf8283 2019-07-24 stsp if (err)
8456 0ebf8283 2019-07-24 stsp return err;
8457 0ebf8283 2019-07-24 stsp
8458 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8459 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8460 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8461 0ebf8283 2019-07-24 stsp if (err) {
8462 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8463 0ebf8283 2019-07-24 stsp goto done;
8464 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8465 0ebf8283 2019-07-24 stsp } else {
8466 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8467 0ebf8283 2019-07-24 stsp free(new_commit_id);
8468 0ebf8283 2019-07-24 stsp }
8469 0ebf8283 2019-07-24 stsp done:
8470 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8471 0ebf8283 2019-07-24 stsp return err;
8472 0ebf8283 2019-07-24 stsp }
8473 0ebf8283 2019-07-24 stsp
8474 0ebf8283 2019-07-24 stsp static const struct got_error *
8475 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8476 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8477 0ebf8283 2019-07-24 stsp {
8478 0ebf8283 2019-07-24 stsp const struct got_error *error;
8479 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8480 0ebf8283 2019-07-24 stsp
8481 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8482 0ebf8283 2019-07-24 stsp repo);
8483 0ebf8283 2019-07-24 stsp if (error)
8484 0ebf8283 2019-07-24 stsp return error;
8485 0ebf8283 2019-07-24 stsp
8486 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8487 0ebf8283 2019-07-24 stsp if (error)
8488 0ebf8283 2019-07-24 stsp return error;
8489 0ebf8283 2019-07-24 stsp
8490 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8491 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8492 0ebf8283 2019-07-24 stsp return error;
8493 ab20a43a 2020-01-29 stsp }
8494 ab20a43a 2020-01-29 stsp
8495 ab20a43a 2020-01-29 stsp static const struct got_error *
8496 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8497 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8498 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8499 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8500 ab20a43a 2020-01-29 stsp {
8501 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8502 ab20a43a 2020-01-29 stsp
8503 ab20a43a 2020-01-29 stsp switch (status) {
8504 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8505 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8506 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8507 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8508 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8509 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8510 ab20a43a 2020-01-29 stsp default:
8511 ab20a43a 2020-01-29 stsp break;
8512 ab20a43a 2020-01-29 stsp }
8513 ab20a43a 2020-01-29 stsp
8514 ab20a43a 2020-01-29 stsp switch (staged_status) {
8515 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8516 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8517 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8518 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8519 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8520 ab20a43a 2020-01-29 stsp default:
8521 ab20a43a 2020-01-29 stsp break;
8522 ab20a43a 2020-01-29 stsp }
8523 ab20a43a 2020-01-29 stsp
8524 ab20a43a 2020-01-29 stsp return NULL;
8525 0ebf8283 2019-07-24 stsp }
8526 0ebf8283 2019-07-24 stsp
8527 0ebf8283 2019-07-24 stsp static const struct got_error *
8528 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8529 0ebf8283 2019-07-24 stsp {
8530 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8531 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8532 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8533 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8534 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8535 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8536 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8537 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8538 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8539 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8540 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8541 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8542 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8543 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8544 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8545 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8546 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8547 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8548 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8549 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8550 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8551 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8552 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8553 0ebf8283 2019-07-24 stsp
8554 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8555 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8556 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8557 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8558 0ebf8283 2019-07-24 stsp
8559 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8560 0ebf8283 2019-07-24 stsp switch (ch) {
8561 0ebf8283 2019-07-24 stsp case 'a':
8562 0ebf8283 2019-07-24 stsp abort_edit = 1;
8563 0ebf8283 2019-07-24 stsp break;
8564 0ebf8283 2019-07-24 stsp case 'c':
8565 0ebf8283 2019-07-24 stsp continue_edit = 1;
8566 0ebf8283 2019-07-24 stsp break;
8567 0ebf8283 2019-07-24 stsp case 'F':
8568 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8569 0ebf8283 2019-07-24 stsp break;
8570 083957f4 2020-02-24 stsp case 'm':
8571 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8572 083957f4 2020-02-24 stsp break;
8573 0ebf8283 2019-07-24 stsp default:
8574 0ebf8283 2019-07-24 stsp usage_histedit();
8575 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8576 0ebf8283 2019-07-24 stsp }
8577 0ebf8283 2019-07-24 stsp }
8578 0ebf8283 2019-07-24 stsp
8579 0ebf8283 2019-07-24 stsp argc -= optind;
8580 0ebf8283 2019-07-24 stsp argv += optind;
8581 0ebf8283 2019-07-24 stsp
8582 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8583 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8584 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8585 0ebf8283 2019-07-24 stsp err(1, "pledge");
8586 0ebf8283 2019-07-24 stsp #endif
8587 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8588 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8589 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8590 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8591 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8592 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8593 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8594 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8595 0ebf8283 2019-07-24 stsp if (argc != 0)
8596 0ebf8283 2019-07-24 stsp usage_histedit();
8597 0ebf8283 2019-07-24 stsp
8598 0ebf8283 2019-07-24 stsp /*
8599 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8600 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8601 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8602 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8603 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8604 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8605 0ebf8283 2019-07-24 stsp */
8606 0ebf8283 2019-07-24 stsp
8607 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8608 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8609 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8610 0ebf8283 2019-07-24 stsp goto done;
8611 0ebf8283 2019-07-24 stsp }
8612 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8613 fa51e947 2020-03-27 stsp if (error) {
8614 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8615 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8616 0ebf8283 2019-07-24 stsp goto done;
8617 fa51e947 2020-03-27 stsp }
8618 0ebf8283 2019-07-24 stsp
8619 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8620 c9956ddf 2019-09-08 stsp NULL);
8621 0ebf8283 2019-07-24 stsp if (error != NULL)
8622 0ebf8283 2019-07-24 stsp goto done;
8623 0ebf8283 2019-07-24 stsp
8624 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8625 0ebf8283 2019-07-24 stsp if (error)
8626 0ebf8283 2019-07-24 stsp goto done;
8627 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8628 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8629 0ebf8283 2019-07-24 stsp goto done;
8630 0ebf8283 2019-07-24 stsp }
8631 0ebf8283 2019-07-24 stsp
8632 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8633 0ebf8283 2019-07-24 stsp if (error)
8634 0ebf8283 2019-07-24 stsp goto done;
8635 0ebf8283 2019-07-24 stsp
8636 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8637 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8638 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8639 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8640 083957f4 2020-02-24 stsp "before the -m option can be used");
8641 083957f4 2020-02-24 stsp goto done;
8642 083957f4 2020-02-24 stsp }
8643 083957f4 2020-02-24 stsp
8644 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8645 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8646 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8647 3e3a69f1 2019-07-25 stsp worktree, repo);
8648 0ebf8283 2019-07-24 stsp if (error)
8649 0ebf8283 2019-07-24 stsp goto done;
8650 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8651 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8652 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8653 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8654 0ebf8283 2019-07-24 stsp if (error)
8655 0ebf8283 2019-07-24 stsp goto done;
8656 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8657 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8658 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8659 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8660 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8661 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8662 0ebf8283 2019-07-24 stsp goto done;
8663 0ebf8283 2019-07-24 stsp }
8664 0ebf8283 2019-07-24 stsp
8665 0ebf8283 2019-07-24 stsp if (continue_edit) {
8666 0ebf8283 2019-07-24 stsp char *path;
8667 0ebf8283 2019-07-24 stsp
8668 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8669 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8670 0ebf8283 2019-07-24 stsp goto done;
8671 0ebf8283 2019-07-24 stsp }
8672 0ebf8283 2019-07-24 stsp
8673 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8674 0ebf8283 2019-07-24 stsp if (error)
8675 0ebf8283 2019-07-24 stsp goto done;
8676 0ebf8283 2019-07-24 stsp
8677 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8678 0ebf8283 2019-07-24 stsp free(path);
8679 0ebf8283 2019-07-24 stsp if (error)
8680 0ebf8283 2019-07-24 stsp goto done;
8681 0ebf8283 2019-07-24 stsp
8682 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8683 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8684 3e3a69f1 2019-07-25 stsp worktree, repo);
8685 0ebf8283 2019-07-24 stsp if (error)
8686 0ebf8283 2019-07-24 stsp goto done;
8687 0ebf8283 2019-07-24 stsp
8688 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8689 0ebf8283 2019-07-24 stsp if (error)
8690 0ebf8283 2019-07-24 stsp goto done;
8691 0ebf8283 2019-07-24 stsp
8692 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8693 0ebf8283 2019-07-24 stsp head_commit_id);
8694 0ebf8283 2019-07-24 stsp if (error)
8695 0ebf8283 2019-07-24 stsp goto done;
8696 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8697 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8698 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8699 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8700 3aac7cf7 2019-07-25 stsp goto done;
8701 3aac7cf7 2019-07-25 stsp }
8702 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8703 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8704 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8705 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8706 0ebf8283 2019-07-24 stsp commit = NULL;
8707 0ebf8283 2019-07-24 stsp if (error)
8708 0ebf8283 2019-07-24 stsp goto done;
8709 0ebf8283 2019-07-24 stsp } else {
8710 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8711 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8712 0ebf8283 2019-07-24 stsp goto done;
8713 0ebf8283 2019-07-24 stsp }
8714 0ebf8283 2019-07-24 stsp
8715 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8716 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8717 0ebf8283 2019-07-24 stsp if (error != NULL)
8718 0ebf8283 2019-07-24 stsp goto done;
8719 0ebf8283 2019-07-24 stsp
8720 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8721 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8722 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8723 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8724 c7d20a3f 2019-07-30 stsp goto done;
8725 c7d20a3f 2019-07-30 stsp }
8726 c7d20a3f 2019-07-30 stsp
8727 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8728 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8729 bfce7f83 2019-07-27 stsp branch = NULL;
8730 0ebf8283 2019-07-24 stsp if (error)
8731 0ebf8283 2019-07-24 stsp goto done;
8732 0ebf8283 2019-07-24 stsp
8733 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8734 0ebf8283 2019-07-24 stsp head_commit_id);
8735 0ebf8283 2019-07-24 stsp if (error)
8736 0ebf8283 2019-07-24 stsp goto done;
8737 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8738 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8739 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8740 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8741 3aac7cf7 2019-07-25 stsp goto done;
8742 3aac7cf7 2019-07-25 stsp }
8743 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8744 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8745 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8746 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8747 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8748 0ebf8283 2019-07-24 stsp commit = NULL;
8749 0ebf8283 2019-07-24 stsp if (error)
8750 0ebf8283 2019-07-24 stsp goto done;
8751 0ebf8283 2019-07-24 stsp
8752 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8753 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8754 c5996fff 2020-01-29 stsp goto done;
8755 c5996fff 2020-01-29 stsp }
8756 c5996fff 2020-01-29 stsp
8757 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8758 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8759 bfce7f83 2019-07-27 stsp if (error)
8760 bfce7f83 2019-07-27 stsp goto done;
8761 bfce7f83 2019-07-27 stsp
8762 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8763 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8764 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8765 6ba2bea2 2019-07-27 stsp if (error) {
8766 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8767 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8768 9627c110 2020-04-18 stsp update_progress, &upa);
8769 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8770 0ebf8283 2019-07-24 stsp goto done;
8771 6ba2bea2 2019-07-27 stsp }
8772 0ebf8283 2019-07-24 stsp } else {
8773 514f2ffe 2020-01-29 stsp const char *branch_name;
8774 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8775 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8776 514f2ffe 2020-01-29 stsp branch_name += 11;
8777 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8778 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8779 6ba2bea2 2019-07-27 stsp if (error) {
8780 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8781 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8782 9627c110 2020-04-18 stsp update_progress, &upa);
8783 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8784 0ebf8283 2019-07-24 stsp goto done;
8785 6ba2bea2 2019-07-27 stsp }
8786 0ebf8283 2019-07-24 stsp
8787 0ebf8283 2019-07-24 stsp }
8788 0ebf8283 2019-07-24 stsp
8789 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8790 0ebf8283 2019-07-24 stsp repo);
8791 6ba2bea2 2019-07-27 stsp if (error) {
8792 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8793 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8794 9627c110 2020-04-18 stsp update_progress, &upa);
8795 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8796 0ebf8283 2019-07-24 stsp goto done;
8797 6ba2bea2 2019-07-27 stsp }
8798 0ebf8283 2019-07-24 stsp
8799 0ebf8283 2019-07-24 stsp }
8800 0ebf8283 2019-07-24 stsp
8801 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8802 4ec14e60 2019-08-28 hiltjo if (error)
8803 0ebf8283 2019-07-24 stsp goto done;
8804 0ebf8283 2019-07-24 stsp
8805 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8806 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8807 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8808 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8809 0ebf8283 2019-07-24 stsp continue;
8810 0ebf8283 2019-07-24 stsp
8811 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8812 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8813 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8814 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8815 0ebf8283 2019-07-24 stsp repo);
8816 ab20a43a 2020-01-29 stsp if (error)
8817 ab20a43a 2020-01-29 stsp goto done;
8818 0ebf8283 2019-07-24 stsp } else {
8819 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8820 ab20a43a 2020-01-29 stsp int have_changes = 0;
8821 ab20a43a 2020-01-29 stsp
8822 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8823 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8824 ab20a43a 2020-01-29 stsp if (error)
8825 ab20a43a 2020-01-29 stsp goto done;
8826 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8827 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8828 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8829 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8830 ab20a43a 2020-01-29 stsp if (error) {
8831 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8832 ab20a43a 2020-01-29 stsp goto done;
8833 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8834 ab20a43a 2020-01-29 stsp goto done;
8835 ab20a43a 2020-01-29 stsp }
8836 ab20a43a 2020-01-29 stsp if (have_changes) {
8837 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8838 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8839 ab20a43a 2020-01-29 stsp if (error)
8840 ab20a43a 2020-01-29 stsp goto done;
8841 ab20a43a 2020-01-29 stsp } else {
8842 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8843 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8844 ab20a43a 2020-01-29 stsp if (error)
8845 ab20a43a 2020-01-29 stsp goto done;
8846 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8847 ab20a43a 2020-01-29 stsp hle, NULL);
8848 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8849 ab20a43a 2020-01-29 stsp commit = NULL;
8850 ab20a43a 2020-01-29 stsp if (error)
8851 ab20a43a 2020-01-29 stsp goto done;
8852 ab20a43a 2020-01-29 stsp }
8853 0ebf8283 2019-07-24 stsp }
8854 0ebf8283 2019-07-24 stsp continue;
8855 0ebf8283 2019-07-24 stsp }
8856 0ebf8283 2019-07-24 stsp
8857 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8858 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8859 0ebf8283 2019-07-24 stsp if (error)
8860 0ebf8283 2019-07-24 stsp goto done;
8861 0ebf8283 2019-07-24 stsp continue;
8862 0ebf8283 2019-07-24 stsp }
8863 0ebf8283 2019-07-24 stsp
8864 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8865 0ebf8283 2019-07-24 stsp hle->commit_id);
8866 0ebf8283 2019-07-24 stsp if (error)
8867 0ebf8283 2019-07-24 stsp goto done;
8868 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8869 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8870 0ebf8283 2019-07-24 stsp
8871 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8872 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8873 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8874 0ebf8283 2019-07-24 stsp if (error)
8875 0ebf8283 2019-07-24 stsp goto done;
8876 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8877 0ebf8283 2019-07-24 stsp commit = NULL;
8878 0ebf8283 2019-07-24 stsp
8879 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8880 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8881 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8882 9627c110 2020-04-18 stsp
8883 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8884 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8885 a0ea4fc0 2020-02-28 stsp repo);
8886 a0ea4fc0 2020-02-28 stsp if (error)
8887 a0ea4fc0 2020-02-28 stsp goto done;
8888 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8889 0ebf8283 2019-07-24 stsp break;
8890 0ebf8283 2019-07-24 stsp }
8891 0ebf8283 2019-07-24 stsp
8892 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8893 0ebf8283 2019-07-24 stsp char *id_str;
8894 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8895 0ebf8283 2019-07-24 stsp if (error)
8896 0ebf8283 2019-07-24 stsp goto done;
8897 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8898 0ebf8283 2019-07-24 stsp id_str);
8899 0ebf8283 2019-07-24 stsp free(id_str);
8900 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8901 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8902 3e3a69f1 2019-07-25 stsp fileindex);
8903 0ebf8283 2019-07-24 stsp goto done;
8904 0ebf8283 2019-07-24 stsp }
8905 0ebf8283 2019-07-24 stsp
8906 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8907 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8908 0ebf8283 2019-07-24 stsp if (error)
8909 0ebf8283 2019-07-24 stsp goto done;
8910 0ebf8283 2019-07-24 stsp continue;
8911 0ebf8283 2019-07-24 stsp }
8912 0ebf8283 2019-07-24 stsp
8913 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8914 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8915 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8916 0ebf8283 2019-07-24 stsp if (error)
8917 0ebf8283 2019-07-24 stsp goto done;
8918 0ebf8283 2019-07-24 stsp }
8919 0ebf8283 2019-07-24 stsp
8920 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8921 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8922 0ebf8283 2019-07-24 stsp if (error)
8923 0ebf8283 2019-07-24 stsp goto done;
8924 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8925 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8926 0ebf8283 2019-07-24 stsp } else
8927 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8928 3e3a69f1 2019-07-25 stsp branch, repo);
8929 0ebf8283 2019-07-24 stsp done:
8930 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8931 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8932 0ebf8283 2019-07-24 stsp free(head_commit_id);
8933 0ebf8283 2019-07-24 stsp free(base_commit_id);
8934 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8935 0ebf8283 2019-07-24 stsp if (commit)
8936 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8937 0ebf8283 2019-07-24 stsp if (branch)
8938 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8939 0ebf8283 2019-07-24 stsp if (tmp_branch)
8940 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8941 0ebf8283 2019-07-24 stsp if (worktree)
8942 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8943 2822a352 2019-10-15 stsp if (repo)
8944 2822a352 2019-10-15 stsp got_repo_close(repo);
8945 2822a352 2019-10-15 stsp return error;
8946 2822a352 2019-10-15 stsp }
8947 2822a352 2019-10-15 stsp
8948 2822a352 2019-10-15 stsp __dead static void
8949 2822a352 2019-10-15 stsp usage_integrate(void)
8950 2822a352 2019-10-15 stsp {
8951 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8952 2822a352 2019-10-15 stsp exit(1);
8953 2822a352 2019-10-15 stsp }
8954 2822a352 2019-10-15 stsp
8955 2822a352 2019-10-15 stsp static const struct got_error *
8956 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8957 2822a352 2019-10-15 stsp {
8958 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8959 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8960 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8961 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8962 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8963 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8964 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8965 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8966 9627c110 2020-04-18 stsp int ch;
8967 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8968 2822a352 2019-10-15 stsp
8969 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8970 2822a352 2019-10-15 stsp switch (ch) {
8971 2822a352 2019-10-15 stsp default:
8972 2822a352 2019-10-15 stsp usage_integrate();
8973 2822a352 2019-10-15 stsp /* NOTREACHED */
8974 2822a352 2019-10-15 stsp }
8975 2822a352 2019-10-15 stsp }
8976 2822a352 2019-10-15 stsp
8977 2822a352 2019-10-15 stsp argc -= optind;
8978 2822a352 2019-10-15 stsp argv += optind;
8979 2822a352 2019-10-15 stsp
8980 2822a352 2019-10-15 stsp if (argc != 1)
8981 2822a352 2019-10-15 stsp usage_integrate();
8982 2822a352 2019-10-15 stsp branch_arg = argv[0];
8983 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
8984 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8985 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8986 2822a352 2019-10-15 stsp err(1, "pledge");
8987 b08a0ccd 2020-09-24 stsp #endif
8988 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8989 2822a352 2019-10-15 stsp if (cwd == NULL) {
8990 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8991 2822a352 2019-10-15 stsp goto done;
8992 2822a352 2019-10-15 stsp }
8993 2822a352 2019-10-15 stsp
8994 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8995 fa51e947 2020-03-27 stsp if (error) {
8996 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8997 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
8998 fa51e947 2020-03-27 stsp cwd);
8999 2822a352 2019-10-15 stsp goto done;
9000 fa51e947 2020-03-27 stsp }
9001 2822a352 2019-10-15 stsp
9002 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
9003 2822a352 2019-10-15 stsp if (error)
9004 2822a352 2019-10-15 stsp goto done;
9005 2822a352 2019-10-15 stsp
9006 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9007 2822a352 2019-10-15 stsp NULL);
9008 2822a352 2019-10-15 stsp if (error != NULL)
9009 2822a352 2019-10-15 stsp goto done;
9010 2822a352 2019-10-15 stsp
9011 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9012 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9013 2822a352 2019-10-15 stsp if (error)
9014 2822a352 2019-10-15 stsp goto done;
9015 2822a352 2019-10-15 stsp
9016 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9017 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9018 2822a352 2019-10-15 stsp goto done;
9019 2822a352 2019-10-15 stsp }
9020 2822a352 2019-10-15 stsp
9021 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9022 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9023 2822a352 2019-10-15 stsp if (error)
9024 2822a352 2019-10-15 stsp goto done;
9025 2822a352 2019-10-15 stsp
9026 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9027 2822a352 2019-10-15 stsp if (refname == NULL) {
9028 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9029 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9030 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9031 2822a352 2019-10-15 stsp goto done;
9032 2822a352 2019-10-15 stsp }
9033 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9034 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9035 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9036 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9037 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9038 2822a352 2019-10-15 stsp goto done;
9039 2822a352 2019-10-15 stsp }
9040 2822a352 2019-10-15 stsp
9041 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9042 2822a352 2019-10-15 stsp if (error)
9043 2822a352 2019-10-15 stsp goto done;
9044 2822a352 2019-10-15 stsp
9045 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9046 2822a352 2019-10-15 stsp if (error)
9047 2822a352 2019-10-15 stsp goto done;
9048 2822a352 2019-10-15 stsp
9049 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9050 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9051 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9052 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9053 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9054 2822a352 2019-10-15 stsp goto done;
9055 2822a352 2019-10-15 stsp }
9056 2822a352 2019-10-15 stsp
9057 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9058 2822a352 2019-10-15 stsp if (error) {
9059 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9060 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9061 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9062 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9063 2822a352 2019-10-15 stsp goto done;
9064 2822a352 2019-10-15 stsp }
9065 2822a352 2019-10-15 stsp
9066 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9067 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9068 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9069 2822a352 2019-10-15 stsp check_cancelled, NULL);
9070 2822a352 2019-10-15 stsp if (error)
9071 2822a352 2019-10-15 stsp goto done;
9072 2822a352 2019-10-15 stsp
9073 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9074 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9075 2822a352 2019-10-15 stsp done:
9076 715dc77e 2019-08-03 stsp if (repo)
9077 715dc77e 2019-08-03 stsp got_repo_close(repo);
9078 2822a352 2019-10-15 stsp if (worktree)
9079 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9080 2822a352 2019-10-15 stsp free(cwd);
9081 2822a352 2019-10-15 stsp free(base_commit_id);
9082 2822a352 2019-10-15 stsp free(commit_id);
9083 2822a352 2019-10-15 stsp free(refname);
9084 2822a352 2019-10-15 stsp free(base_refname);
9085 715dc77e 2019-08-03 stsp return error;
9086 715dc77e 2019-08-03 stsp }
9087 715dc77e 2019-08-03 stsp
9088 715dc77e 2019-08-03 stsp __dead static void
9089 715dc77e 2019-08-03 stsp usage_stage(void)
9090 715dc77e 2019-08-03 stsp {
9091 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9092 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9093 715dc77e 2019-08-03 stsp getprogname());
9094 715dc77e 2019-08-03 stsp exit(1);
9095 715dc77e 2019-08-03 stsp }
9096 715dc77e 2019-08-03 stsp
9097 715dc77e 2019-08-03 stsp static const struct got_error *
9098 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9099 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9100 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9101 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9102 408b4ebc 2019-08-03 stsp {
9103 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9104 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9105 408b4ebc 2019-08-03 stsp
9106 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9107 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9108 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9109 408b4ebc 2019-08-03 stsp return NULL;
9110 408b4ebc 2019-08-03 stsp
9111 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9112 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9113 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9114 408b4ebc 2019-08-03 stsp else
9115 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9116 408b4ebc 2019-08-03 stsp if (err)
9117 408b4ebc 2019-08-03 stsp return err;
9118 408b4ebc 2019-08-03 stsp
9119 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9120 408b4ebc 2019-08-03 stsp free(id_str);
9121 dc424a06 2019-08-07 stsp return NULL;
9122 dc424a06 2019-08-07 stsp }
9123 2e1f37b0 2019-08-08 stsp
9124 dc424a06 2019-08-07 stsp static const struct got_error *
9125 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9126 715dc77e 2019-08-03 stsp {
9127 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9128 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9129 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9130 715dc77e 2019-08-03 stsp char *cwd = NULL;
9131 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
9132 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
9133 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9134 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
9135 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9136 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9137 715dc77e 2019-08-03 stsp
9138 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
9139 715dc77e 2019-08-03 stsp
9140 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9141 715dc77e 2019-08-03 stsp switch (ch) {
9142 408b4ebc 2019-08-03 stsp case 'l':
9143 408b4ebc 2019-08-03 stsp list_stage = 1;
9144 408b4ebc 2019-08-03 stsp break;
9145 dc424a06 2019-08-07 stsp case 'p':
9146 dc424a06 2019-08-07 stsp pflag = 1;
9147 dc424a06 2019-08-07 stsp break;
9148 dc424a06 2019-08-07 stsp case 'F':
9149 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9150 dc424a06 2019-08-07 stsp break;
9151 35213c7c 2020-07-23 stsp case 'S':
9152 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9153 35213c7c 2020-07-23 stsp break;
9154 715dc77e 2019-08-03 stsp default:
9155 715dc77e 2019-08-03 stsp usage_stage();
9156 715dc77e 2019-08-03 stsp /* NOTREACHED */
9157 715dc77e 2019-08-03 stsp }
9158 715dc77e 2019-08-03 stsp }
9159 715dc77e 2019-08-03 stsp
9160 715dc77e 2019-08-03 stsp argc -= optind;
9161 715dc77e 2019-08-03 stsp argv += optind;
9162 715dc77e 2019-08-03 stsp
9163 715dc77e 2019-08-03 stsp #ifndef PROFILE
9164 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9165 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9166 715dc77e 2019-08-03 stsp err(1, "pledge");
9167 715dc77e 2019-08-03 stsp #endif
9168 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9169 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9170 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9171 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9172 715dc77e 2019-08-03 stsp
9173 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9174 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9175 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9176 715dc77e 2019-08-03 stsp goto done;
9177 715dc77e 2019-08-03 stsp }
9178 715dc77e 2019-08-03 stsp
9179 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9180 fa51e947 2020-03-27 stsp if (error) {
9181 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9182 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9183 715dc77e 2019-08-03 stsp goto done;
9184 fa51e947 2020-03-27 stsp }
9185 715dc77e 2019-08-03 stsp
9186 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9187 c9956ddf 2019-09-08 stsp NULL);
9188 715dc77e 2019-08-03 stsp if (error != NULL)
9189 715dc77e 2019-08-03 stsp goto done;
9190 715dc77e 2019-08-03 stsp
9191 dc424a06 2019-08-07 stsp if (patch_script_path) {
9192 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9193 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9194 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9195 dc424a06 2019-08-07 stsp patch_script_path);
9196 dc424a06 2019-08-07 stsp goto done;
9197 dc424a06 2019-08-07 stsp }
9198 dc424a06 2019-08-07 stsp }
9199 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9200 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9201 715dc77e 2019-08-03 stsp if (error)
9202 715dc77e 2019-08-03 stsp goto done;
9203 715dc77e 2019-08-03 stsp
9204 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9205 6d022e97 2019-08-04 stsp if (error)
9206 6d022e97 2019-08-04 stsp goto done;
9207 715dc77e 2019-08-03 stsp
9208 6d022e97 2019-08-04 stsp if (list_stage)
9209 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9210 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9211 2e1f37b0 2019-08-08 stsp else {
9212 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9213 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9214 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9215 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9216 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9217 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9218 2e1f37b0 2019-08-08 stsp }
9219 ad493afc 2019-08-03 stsp done:
9220 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9221 2e1f37b0 2019-08-08 stsp error == NULL)
9222 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9223 ad493afc 2019-08-03 stsp if (repo)
9224 ad493afc 2019-08-03 stsp got_repo_close(repo);
9225 ad493afc 2019-08-03 stsp if (worktree)
9226 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9227 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9228 ad493afc 2019-08-03 stsp free((char *)pe->path);
9229 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9230 ad493afc 2019-08-03 stsp free(cwd);
9231 ad493afc 2019-08-03 stsp return error;
9232 ad493afc 2019-08-03 stsp }
9233 ad493afc 2019-08-03 stsp
9234 ad493afc 2019-08-03 stsp __dead static void
9235 ad493afc 2019-08-03 stsp usage_unstage(void)
9236 ad493afc 2019-08-03 stsp {
9237 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9238 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9239 ad493afc 2019-08-03 stsp getprogname());
9240 ad493afc 2019-08-03 stsp exit(1);
9241 ad493afc 2019-08-03 stsp }
9242 ad493afc 2019-08-03 stsp
9243 ad493afc 2019-08-03 stsp
9244 ad493afc 2019-08-03 stsp static const struct got_error *
9245 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9246 ad493afc 2019-08-03 stsp {
9247 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9248 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9249 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9250 ad493afc 2019-08-03 stsp char *cwd = NULL;
9251 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9252 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9253 9627c110 2020-04-18 stsp int ch, pflag = 0;
9254 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9255 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9256 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9257 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9258 ad493afc 2019-08-03 stsp
9259 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9260 ad493afc 2019-08-03 stsp
9261 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9262 ad493afc 2019-08-03 stsp switch (ch) {
9263 2e1f37b0 2019-08-08 stsp case 'p':
9264 2e1f37b0 2019-08-08 stsp pflag = 1;
9265 2e1f37b0 2019-08-08 stsp break;
9266 2e1f37b0 2019-08-08 stsp case 'F':
9267 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9268 2e1f37b0 2019-08-08 stsp break;
9269 ad493afc 2019-08-03 stsp default:
9270 ad493afc 2019-08-03 stsp usage_unstage();
9271 ad493afc 2019-08-03 stsp /* NOTREACHED */
9272 ad493afc 2019-08-03 stsp }
9273 ad493afc 2019-08-03 stsp }
9274 ad493afc 2019-08-03 stsp
9275 ad493afc 2019-08-03 stsp argc -= optind;
9276 ad493afc 2019-08-03 stsp argv += optind;
9277 ad493afc 2019-08-03 stsp
9278 ad493afc 2019-08-03 stsp #ifndef PROFILE
9279 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9280 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9281 ad493afc 2019-08-03 stsp err(1, "pledge");
9282 ad493afc 2019-08-03 stsp #endif
9283 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9284 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9285 2e1f37b0 2019-08-08 stsp
9286 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9287 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9288 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9289 ad493afc 2019-08-03 stsp goto done;
9290 ad493afc 2019-08-03 stsp }
9291 ad493afc 2019-08-03 stsp
9292 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9293 fa51e947 2020-03-27 stsp if (error) {
9294 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9295 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9296 ad493afc 2019-08-03 stsp goto done;
9297 fa51e947 2020-03-27 stsp }
9298 ad493afc 2019-08-03 stsp
9299 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9300 c9956ddf 2019-09-08 stsp NULL);
9301 ad493afc 2019-08-03 stsp if (error != NULL)
9302 ad493afc 2019-08-03 stsp goto done;
9303 ad493afc 2019-08-03 stsp
9304 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9305 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9306 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9307 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9308 2e1f37b0 2019-08-08 stsp patch_script_path);
9309 2e1f37b0 2019-08-08 stsp goto done;
9310 2e1f37b0 2019-08-08 stsp }
9311 2e1f37b0 2019-08-08 stsp }
9312 2e1f37b0 2019-08-08 stsp
9313 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9314 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9315 ad493afc 2019-08-03 stsp if (error)
9316 ad493afc 2019-08-03 stsp goto done;
9317 ad493afc 2019-08-03 stsp
9318 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9319 ad493afc 2019-08-03 stsp if (error)
9320 ad493afc 2019-08-03 stsp goto done;
9321 ad493afc 2019-08-03 stsp
9322 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9323 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9324 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9325 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9326 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9327 9627c110 2020-04-18 stsp if (!error)
9328 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9329 715dc77e 2019-08-03 stsp done:
9330 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9331 2e1f37b0 2019-08-08 stsp error == NULL)
9332 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9333 0ebf8283 2019-07-24 stsp if (repo)
9334 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9335 715dc77e 2019-08-03 stsp if (worktree)
9336 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9337 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9338 715dc77e 2019-08-03 stsp free((char *)pe->path);
9339 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9340 715dc77e 2019-08-03 stsp free(cwd);
9341 01073a5d 2019-08-22 stsp return error;
9342 01073a5d 2019-08-22 stsp }
9343 01073a5d 2019-08-22 stsp
9344 01073a5d 2019-08-22 stsp __dead static void
9345 01073a5d 2019-08-22 stsp usage_cat(void)
9346 01073a5d 2019-08-22 stsp {
9347 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9348 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9349 01073a5d 2019-08-22 stsp exit(1);
9350 01073a5d 2019-08-22 stsp }
9351 01073a5d 2019-08-22 stsp
9352 01073a5d 2019-08-22 stsp static const struct got_error *
9353 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9354 01073a5d 2019-08-22 stsp {
9355 01073a5d 2019-08-22 stsp const struct got_error *err;
9356 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9357 01073a5d 2019-08-22 stsp
9358 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9359 01073a5d 2019-08-22 stsp if (err)
9360 01073a5d 2019-08-22 stsp return err;
9361 01073a5d 2019-08-22 stsp
9362 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9363 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9364 01073a5d 2019-08-22 stsp return err;
9365 01073a5d 2019-08-22 stsp }
9366 01073a5d 2019-08-22 stsp
9367 01073a5d 2019-08-22 stsp static const struct got_error *
9368 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9369 01073a5d 2019-08-22 stsp {
9370 01073a5d 2019-08-22 stsp const struct got_error *err;
9371 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9372 56e0773d 2019-11-28 stsp int nentries, i;
9373 01073a5d 2019-08-22 stsp
9374 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9375 01073a5d 2019-08-22 stsp if (err)
9376 01073a5d 2019-08-22 stsp return err;
9377 01073a5d 2019-08-22 stsp
9378 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9379 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9380 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9381 01073a5d 2019-08-22 stsp char *id_str;
9382 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9383 01073a5d 2019-08-22 stsp break;
9384 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9385 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9386 01073a5d 2019-08-22 stsp if (err)
9387 01073a5d 2019-08-22 stsp break;
9388 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9389 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9390 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9391 01073a5d 2019-08-22 stsp free(id_str);
9392 01073a5d 2019-08-22 stsp }
9393 01073a5d 2019-08-22 stsp
9394 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9395 01073a5d 2019-08-22 stsp return err;
9396 01073a5d 2019-08-22 stsp }
9397 01073a5d 2019-08-22 stsp
9398 01073a5d 2019-08-22 stsp static const struct got_error *
9399 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9400 01073a5d 2019-08-22 stsp {
9401 01073a5d 2019-08-22 stsp const struct got_error *err;
9402 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9403 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9404 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9405 01073a5d 2019-08-22 stsp char *id_str = NULL;
9406 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9407 01073a5d 2019-08-22 stsp
9408 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9409 01073a5d 2019-08-22 stsp if (err)
9410 01073a5d 2019-08-22 stsp return err;
9411 01073a5d 2019-08-22 stsp
9412 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9413 01073a5d 2019-08-22 stsp if (err)
9414 01073a5d 2019-08-22 stsp goto done;
9415 01073a5d 2019-08-22 stsp
9416 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9417 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9418 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9419 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9420 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9421 01073a5d 2019-08-22 stsp char *pid_str;
9422 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9423 01073a5d 2019-08-22 stsp if (err)
9424 01073a5d 2019-08-22 stsp goto done;
9425 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9426 01073a5d 2019-08-22 stsp free(pid_str);
9427 01073a5d 2019-08-22 stsp }
9428 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9429 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9430 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
9431 01073a5d 2019-08-22 stsp
9432 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9433 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9434 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
9435 01073a5d 2019-08-22 stsp
9436 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9437 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9438 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9439 01073a5d 2019-08-22 stsp done:
9440 01073a5d 2019-08-22 stsp free(id_str);
9441 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9442 01073a5d 2019-08-22 stsp return err;
9443 01073a5d 2019-08-22 stsp }
9444 01073a5d 2019-08-22 stsp
9445 01073a5d 2019-08-22 stsp static const struct got_error *
9446 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9447 01073a5d 2019-08-22 stsp {
9448 01073a5d 2019-08-22 stsp const struct got_error *err;
9449 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9450 01073a5d 2019-08-22 stsp char *id_str = NULL;
9451 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9452 01073a5d 2019-08-22 stsp
9453 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9454 01073a5d 2019-08-22 stsp if (err)
9455 01073a5d 2019-08-22 stsp return err;
9456 01073a5d 2019-08-22 stsp
9457 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9458 01073a5d 2019-08-22 stsp if (err)
9459 01073a5d 2019-08-22 stsp goto done;
9460 01073a5d 2019-08-22 stsp
9461 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9462 e15d5241 2019-08-22 stsp
9463 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9464 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9465 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9466 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9467 01073a5d 2019-08-22 stsp break;
9468 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9469 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9470 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9471 01073a5d 2019-08-22 stsp break;
9472 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9473 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9474 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9475 01073a5d 2019-08-22 stsp break;
9476 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9477 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9478 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9479 01073a5d 2019-08-22 stsp break;
9480 01073a5d 2019-08-22 stsp default:
9481 01073a5d 2019-08-22 stsp break;
9482 01073a5d 2019-08-22 stsp }
9483 01073a5d 2019-08-22 stsp
9484 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9485 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9486 e15d5241 2019-08-22 stsp
9487 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9488 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9489 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
9490 01073a5d 2019-08-22 stsp
9491 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9492 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9493 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9494 01073a5d 2019-08-22 stsp done:
9495 01073a5d 2019-08-22 stsp free(id_str);
9496 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
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 cmd_cat(int argc, char *argv[])
9502 01073a5d 2019-08-22 stsp {
9503 01073a5d 2019-08-22 stsp const struct got_error *error;
9504 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9505 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9506 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9507 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9508 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9509 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9510 01073a5d 2019-08-22 stsp
9511 01073a5d 2019-08-22 stsp #ifndef PROFILE
9512 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9513 01073a5d 2019-08-22 stsp NULL) == -1)
9514 01073a5d 2019-08-22 stsp err(1, "pledge");
9515 01073a5d 2019-08-22 stsp #endif
9516 01073a5d 2019-08-22 stsp
9517 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9518 01073a5d 2019-08-22 stsp switch (ch) {
9519 896e9b6f 2019-08-26 stsp case 'c':
9520 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9521 896e9b6f 2019-08-26 stsp break;
9522 01073a5d 2019-08-22 stsp case 'r':
9523 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9524 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9525 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9526 9ba1d308 2019-10-21 stsp optarg);
9527 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9528 01073a5d 2019-08-22 stsp break;
9529 896e9b6f 2019-08-26 stsp case 'P':
9530 896e9b6f 2019-08-26 stsp force_path = 1;
9531 896e9b6f 2019-08-26 stsp break;
9532 01073a5d 2019-08-22 stsp default:
9533 01073a5d 2019-08-22 stsp usage_cat();
9534 01073a5d 2019-08-22 stsp /* NOTREACHED */
9535 01073a5d 2019-08-22 stsp }
9536 01073a5d 2019-08-22 stsp }
9537 01073a5d 2019-08-22 stsp
9538 01073a5d 2019-08-22 stsp argc -= optind;
9539 01073a5d 2019-08-22 stsp argv += optind;
9540 01073a5d 2019-08-22 stsp
9541 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9542 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9543 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9544 01073a5d 2019-08-22 stsp goto done;
9545 01073a5d 2019-08-22 stsp }
9546 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9547 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9548 01073a5d 2019-08-22 stsp goto done;
9549 01073a5d 2019-08-22 stsp if (worktree) {
9550 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9551 01073a5d 2019-08-22 stsp repo_path = strdup(
9552 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9553 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9554 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9555 01073a5d 2019-08-22 stsp goto done;
9556 01073a5d 2019-08-22 stsp }
9557 01073a5d 2019-08-22 stsp }
9558 01073a5d 2019-08-22 stsp }
9559 01073a5d 2019-08-22 stsp
9560 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9561 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9562 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9563 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9564 01073a5d 2019-08-22 stsp }
9565 01073a5d 2019-08-22 stsp
9566 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9567 01073a5d 2019-08-22 stsp free(repo_path);
9568 01073a5d 2019-08-22 stsp if (error != NULL)
9569 01073a5d 2019-08-22 stsp goto done;
9570 01073a5d 2019-08-22 stsp
9571 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9572 896e9b6f 2019-08-26 stsp if (error)
9573 896e9b6f 2019-08-26 stsp goto done;
9574 896e9b6f 2019-08-26 stsp
9575 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9576 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9577 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9578 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9579 01073a5d 2019-08-22 stsp if (error)
9580 01073a5d 2019-08-22 stsp goto done;
9581 01073a5d 2019-08-22 stsp
9582 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9583 896e9b6f 2019-08-26 stsp if (force_path) {
9584 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9585 896e9b6f 2019-08-26 stsp argv[i]);
9586 896e9b6f 2019-08-26 stsp if (error)
9587 896e9b6f 2019-08-26 stsp break;
9588 896e9b6f 2019-08-26 stsp } else {
9589 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9590 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9591 896e9b6f 2019-08-26 stsp if (error) {
9592 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9593 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9594 896e9b6f 2019-08-26 stsp break;
9595 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9596 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9597 896e9b6f 2019-08-26 stsp if (error)
9598 896e9b6f 2019-08-26 stsp break;
9599 896e9b6f 2019-08-26 stsp }
9600 896e9b6f 2019-08-26 stsp }
9601 01073a5d 2019-08-22 stsp
9602 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9603 01073a5d 2019-08-22 stsp if (error)
9604 01073a5d 2019-08-22 stsp break;
9605 01073a5d 2019-08-22 stsp
9606 01073a5d 2019-08-22 stsp switch (obj_type) {
9607 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9608 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9609 01073a5d 2019-08-22 stsp break;
9610 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9611 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9612 01073a5d 2019-08-22 stsp break;
9613 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9614 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9615 01073a5d 2019-08-22 stsp break;
9616 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9617 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9618 01073a5d 2019-08-22 stsp break;
9619 01073a5d 2019-08-22 stsp default:
9620 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9621 01073a5d 2019-08-22 stsp break;
9622 01073a5d 2019-08-22 stsp }
9623 01073a5d 2019-08-22 stsp if (error)
9624 01073a5d 2019-08-22 stsp break;
9625 01073a5d 2019-08-22 stsp free(label);
9626 01073a5d 2019-08-22 stsp label = NULL;
9627 01073a5d 2019-08-22 stsp free(id);
9628 01073a5d 2019-08-22 stsp id = NULL;
9629 01073a5d 2019-08-22 stsp }
9630 01073a5d 2019-08-22 stsp done:
9631 01073a5d 2019-08-22 stsp free(label);
9632 01073a5d 2019-08-22 stsp free(id);
9633 896e9b6f 2019-08-26 stsp free(commit_id);
9634 01073a5d 2019-08-22 stsp if (worktree)
9635 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9636 01073a5d 2019-08-22 stsp if (repo) {
9637 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9638 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9639 01073a5d 2019-08-22 stsp if (error == NULL)
9640 01073a5d 2019-08-22 stsp error = repo_error;
9641 b2118c49 2020-07-28 stsp }
9642 b2118c49 2020-07-28 stsp return error;
9643 b2118c49 2020-07-28 stsp }
9644 b2118c49 2020-07-28 stsp
9645 b2118c49 2020-07-28 stsp __dead static void
9646 b2118c49 2020-07-28 stsp usage_info(void)
9647 b2118c49 2020-07-28 stsp {
9648 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9649 b2118c49 2020-07-28 stsp getprogname());
9650 b2118c49 2020-07-28 stsp exit(1);
9651 b2118c49 2020-07-28 stsp }
9652 b2118c49 2020-07-28 stsp
9653 b2118c49 2020-07-28 stsp static const struct got_error *
9654 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9655 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9656 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9657 b2118c49 2020-07-28 stsp {
9658 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9659 b2118c49 2020-07-28 stsp char *id_str = NULL;
9660 b2118c49 2020-07-28 stsp char datebuf[128];
9661 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9662 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9663 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9664 b2118c49 2020-07-28 stsp
9665 b2118c49 2020-07-28 stsp /*
9666 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9667 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9668 b2118c49 2020-07-28 stsp */
9669 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9670 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9671 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9672 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9673 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9674 b2118c49 2020-07-28 stsp }
9675 b2118c49 2020-07-28 stsp
9676 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9677 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9678 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9679 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9680 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9681 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9682 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9683 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9684 b2118c49 2020-07-28 stsp else
9685 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9686 b2118c49 2020-07-28 stsp
9687 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9688 b2118c49 2020-07-28 stsp if (tm == NULL)
9689 b2118c49 2020-07-28 stsp return NULL;
9690 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9691 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9692 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9693 b2118c49 2020-07-28 stsp
9694 b2118c49 2020-07-28 stsp if (blob_id) {
9695 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9696 b2118c49 2020-07-28 stsp if (err)
9697 b2118c49 2020-07-28 stsp return err;
9698 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9699 b2118c49 2020-07-28 stsp free(id_str);
9700 b2118c49 2020-07-28 stsp }
9701 b2118c49 2020-07-28 stsp
9702 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9703 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9704 b2118c49 2020-07-28 stsp if (err)
9705 b2118c49 2020-07-28 stsp return err;
9706 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9707 b2118c49 2020-07-28 stsp free(id_str);
9708 b2118c49 2020-07-28 stsp }
9709 b2118c49 2020-07-28 stsp
9710 b2118c49 2020-07-28 stsp if (commit_id) {
9711 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9712 b2118c49 2020-07-28 stsp if (err)
9713 b2118c49 2020-07-28 stsp return err;
9714 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9715 b2118c49 2020-07-28 stsp free(id_str);
9716 b2118c49 2020-07-28 stsp }
9717 b2118c49 2020-07-28 stsp
9718 b2118c49 2020-07-28 stsp return NULL;
9719 b2118c49 2020-07-28 stsp }
9720 b2118c49 2020-07-28 stsp
9721 b2118c49 2020-07-28 stsp static const struct got_error *
9722 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9723 b2118c49 2020-07-28 stsp {
9724 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9725 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9726 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9727 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9728 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9729 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9730 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9731 b2118c49 2020-07-28 stsp
9732 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9733 b2118c49 2020-07-28 stsp
9734 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9735 b2118c49 2020-07-28 stsp switch (ch) {
9736 b2118c49 2020-07-28 stsp default:
9737 b2118c49 2020-07-28 stsp usage_info();
9738 b2118c49 2020-07-28 stsp /* NOTREACHED */
9739 b2118c49 2020-07-28 stsp }
9740 01073a5d 2019-08-22 stsp }
9741 b2118c49 2020-07-28 stsp
9742 b2118c49 2020-07-28 stsp argc -= optind;
9743 b2118c49 2020-07-28 stsp argv += optind;
9744 b2118c49 2020-07-28 stsp
9745 b2118c49 2020-07-28 stsp #ifndef PROFILE
9746 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9747 b2118c49 2020-07-28 stsp NULL) == -1)
9748 b2118c49 2020-07-28 stsp err(1, "pledge");
9749 b2118c49 2020-07-28 stsp #endif
9750 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9751 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9752 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9753 b2118c49 2020-07-28 stsp goto done;
9754 b2118c49 2020-07-28 stsp }
9755 b2118c49 2020-07-28 stsp
9756 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9757 b2118c49 2020-07-28 stsp if (error) {
9758 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9759 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9760 b2118c49 2020-07-28 stsp goto done;
9761 b2118c49 2020-07-28 stsp }
9762 b2118c49 2020-07-28 stsp
9763 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9764 b2118c49 2020-07-28 stsp if (error)
9765 b2118c49 2020-07-28 stsp goto done;
9766 b2118c49 2020-07-28 stsp
9767 b2118c49 2020-07-28 stsp if (argc >= 1) {
9768 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9769 b2118c49 2020-07-28 stsp worktree);
9770 b2118c49 2020-07-28 stsp if (error)
9771 b2118c49 2020-07-28 stsp goto done;
9772 b2118c49 2020-07-28 stsp show_files = 1;
9773 b2118c49 2020-07-28 stsp }
9774 b2118c49 2020-07-28 stsp
9775 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9776 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9777 b2118c49 2020-07-28 stsp if (error)
9778 b2118c49 2020-07-28 stsp goto done;
9779 b2118c49 2020-07-28 stsp
9780 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9781 b2118c49 2020-07-28 stsp if (error)
9782 b2118c49 2020-07-28 stsp goto done;
9783 b2118c49 2020-07-28 stsp
9784 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9785 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9786 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9787 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9788 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9789 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9790 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9791 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9792 b2118c49 2020-07-28 stsp
9793 b2118c49 2020-07-28 stsp if (show_files) {
9794 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9795 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9796 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9797 b2118c49 2020-07-28 stsp continue;
9798 b2118c49 2020-07-28 stsp /*
9799 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9800 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9801 b2118c49 2020-07-28 stsp */
9802 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9803 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9804 b2118c49 2020-07-28 stsp }
9805 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9806 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9807 b2118c49 2020-07-28 stsp if (error)
9808 b2118c49 2020-07-28 stsp goto done;
9809 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9810 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9811 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9812 b2118c49 2020-07-28 stsp break;
9813 b2118c49 2020-07-28 stsp }
9814 b2118c49 2020-07-28 stsp }
9815 b2118c49 2020-07-28 stsp }
9816 b2118c49 2020-07-28 stsp done:
9817 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9818 b2118c49 2020-07-28 stsp free((char *)pe->path);
9819 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9820 b2118c49 2020-07-28 stsp free(cwd);
9821 b2118c49 2020-07-28 stsp free(id_str);
9822 b2118c49 2020-07-28 stsp free(uuidstr);
9823 0ebf8283 2019-07-24 stsp return error;
9824 0ebf8283 2019-07-24 stsp }