Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 5c860e29 2018-03-12 stsp *
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp *
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5c860e29 2018-03-12 stsp */
18 5c860e29 2018-03-12 stsp
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 f42b1b34 2018-03-12 stsp
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 d2cdc636 2020-03-18 stsp #include <util.h>
42 5c860e29 2018-03-12 stsp
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
58 5c860e29 2018-03-12 stsp
59 5c860e29 2018-03-12 stsp #ifndef nitems
60 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 5c860e29 2018-03-12 stsp #endif
62 99437157 2018-11-11 stsp
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
65 99437157 2018-11-11 stsp
66 99437157 2018-11-11 stsp static void
67 99437157 2018-11-11 stsp catch_sigint(int signo)
68 99437157 2018-11-11 stsp {
69 99437157 2018-11-11 stsp sigint_received = 1;
70 99437157 2018-11-11 stsp }
71 99437157 2018-11-11 stsp
72 99437157 2018-11-11 stsp static void
73 99437157 2018-11-11 stsp catch_sigpipe(int signo)
74 99437157 2018-11-11 stsp {
75 99437157 2018-11-11 stsp sigpipe_received = 1;
76 99437157 2018-11-11 stsp }
77 5c860e29 2018-03-12 stsp
78 99437157 2018-11-11 stsp
79 8cfb4057 2019-07-09 stsp struct got_cmd {
80 820059fa 2020-09-25 stsp const char *cmd_name;
81 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
83 97b3a7be 2019-07-09 stsp const char *cmd_alias;
84 5c860e29 2018-03-12 stsp };
85 5c860e29 2018-03-12 stsp
86 6879ba42 2020-10-01 naddy __dead static void usage(int, int);
87 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
88 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
89 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
90 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
91 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
92 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
95 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
96 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
97 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
98 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
99 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
100 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
101 d00136be 2019-03-26 stsp __dead static void usage_add(void);
102 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
103 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
104 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
105 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
106 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
107 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
108 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
109 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
110 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
111 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
112 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
113 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
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 6879ba42 2020-10-01 naddy list_commands(FILE *fp)
175 ce5b7c56 2019-07-09 stsp {
176 ce5b7c56 2019-07-09 stsp int i;
177 ce5b7c56 2019-07-09 stsp
178 6879ba42 2020-10-01 naddy fprintf(fp, "commands:");
179 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
180 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
181 6879ba42 2020-10-01 naddy fprintf(fp, " %s", cmd->cmd_name);
182 ce5b7c56 2019-07-09 stsp }
183 6879ba42 2020-10-01 naddy fputc('\n', fp);
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 62d463ca 2020-10-20 naddy { "version", no_argument, NULL, 'V' },
195 62d463ca 2020-10-20 naddy { 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 6879ba42 2020-10-01 naddy usage(hflag, 1);
210 5c860e29 2018-03-12 stsp /* NOTREACHED */
211 5c860e29 2018-03-12 stsp }
212 5c860e29 2018-03-12 stsp }
213 5c860e29 2018-03-12 stsp
214 5c860e29 2018-03-12 stsp argc -= optind;
215 5c860e29 2018-03-12 stsp argv += optind;
216 9814e6a3 2020-09-27 naddy optind = 1;
217 9814e6a3 2020-09-27 naddy optreset = 1;
218 53ccebc2 2019-07-30 stsp
219 53ccebc2 2019-07-30 stsp if (Vflag) {
220 53ccebc2 2019-07-30 stsp got_version_print_str();
221 6879ba42 2020-10-01 naddy return 0;
222 53ccebc2 2019-07-30 stsp }
223 5c860e29 2018-03-12 stsp
224 5c860e29 2018-03-12 stsp if (argc <= 0)
225 6879ba42 2020-10-01 naddy usage(hflag, hflag ? 0 : 1);
226 5c860e29 2018-03-12 stsp
227 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
228 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
229 99437157 2018-11-11 stsp
230 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
231 d7d4f210 2018-03-12 stsp const struct got_error *error;
232 d7d4f210 2018-03-12 stsp
233 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
234 5c860e29 2018-03-12 stsp
235 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
236 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
237 5c860e29 2018-03-12 stsp continue;
238 5c860e29 2018-03-12 stsp
239 1b6b95a8 2018-03-12 stsp if (hflag)
240 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
241 1b6b95a8 2018-03-12 stsp
242 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
243 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
244 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
245 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
246 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
247 70015d7a 2019-11-08 stsp !(sigint_received &&
248 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
249 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
250 d7d4f210 2018-03-12 stsp return 1;
251 d7d4f210 2018-03-12 stsp }
252 d7d4f210 2018-03-12 stsp
253 d7d4f210 2018-03-12 stsp return 0;
254 5c860e29 2018-03-12 stsp }
255 5c860e29 2018-03-12 stsp
256 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
257 6879ba42 2020-10-01 naddy list_commands(stderr);
258 5c860e29 2018-03-12 stsp return 1;
259 5c860e29 2018-03-12 stsp }
260 5c860e29 2018-03-12 stsp
261 4ed7e80c 2018-05-20 stsp __dead static void
262 6879ba42 2020-10-01 naddy usage(int hflag, int status)
263 5c860e29 2018-03-12 stsp {
264 6879ba42 2020-10-01 naddy FILE *fp = (status == 0) ? stdout : stderr;
265 6879ba42 2020-10-01 naddy
266 6879ba42 2020-10-01 naddy fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
267 53ccebc2 2019-07-30 stsp getprogname());
268 ce5b7c56 2019-07-09 stsp if (hflag)
269 6879ba42 2020-10-01 naddy list_commands(fp);
270 6879ba42 2020-10-01 naddy exit(status);
271 5c860e29 2018-03-12 stsp }
272 5c860e29 2018-03-12 stsp
273 0266afb7 2019-01-04 stsp static const struct got_error *
274 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
275 e2ba3d07 2019-05-13 stsp {
276 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
277 e2ba3d07 2019-05-13 stsp const char *editor;
278 8920fa04 2019-08-18 stsp
279 8920fa04 2019-08-18 stsp *abspath = NULL;
280 e2ba3d07 2019-05-13 stsp
281 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
282 e2ba3d07 2019-05-13 stsp if (editor == NULL)
283 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
284 e2ba3d07 2019-05-13 stsp
285 0ee7065d 2019-05-13 stsp if (editor) {
286 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
287 0ee7065d 2019-05-13 stsp if (err)
288 0ee7065d 2019-05-13 stsp return err;
289 0ee7065d 2019-05-13 stsp }
290 e2ba3d07 2019-05-13 stsp
291 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
292 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
293 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
294 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
295 0ee7065d 2019-05-13 stsp }
296 0ee7065d 2019-05-13 stsp
297 e2ba3d07 2019-05-13 stsp return NULL;
298 e2ba3d07 2019-05-13 stsp }
299 e2ba3d07 2019-05-13 stsp
300 e2ba3d07 2019-05-13 stsp static const struct got_error *
301 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
302 c530dc23 2019-07-23 stsp const char *worktree_path)
303 0266afb7 2019-01-04 stsp {
304 163ce85a 2019-05-13 stsp const struct got_error *err;
305 0266afb7 2019-01-04 stsp
306 37c06ea4 2019-07-15 stsp #ifdef PROFILE
307 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
308 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
309 37c06ea4 2019-07-15 stsp #endif
310 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
311 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
312 0266afb7 2019-01-04 stsp
313 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
314 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
315 0266afb7 2019-01-04 stsp
316 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
317 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
318 0266afb7 2019-01-04 stsp
319 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
320 163ce85a 2019-05-13 stsp if (err != NULL)
321 163ce85a 2019-05-13 stsp return err;
322 0266afb7 2019-01-04 stsp
323 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
324 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
325 0266afb7 2019-01-04 stsp
326 0266afb7 2019-01-04 stsp return NULL;
327 2c7829a4 2019-06-17 stsp }
328 2c7829a4 2019-06-17 stsp
329 2c7829a4 2019-06-17 stsp __dead static void
330 2c7829a4 2019-06-17 stsp usage_init(void)
331 2c7829a4 2019-06-17 stsp {
332 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
333 2c7829a4 2019-06-17 stsp exit(1);
334 2c7829a4 2019-06-17 stsp }
335 2c7829a4 2019-06-17 stsp
336 2c7829a4 2019-06-17 stsp static const struct got_error *
337 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
338 2c7829a4 2019-06-17 stsp {
339 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
340 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
341 2c7829a4 2019-06-17 stsp int ch;
342 2c7829a4 2019-06-17 stsp
343 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
344 2c7829a4 2019-06-17 stsp switch (ch) {
345 2c7829a4 2019-06-17 stsp default:
346 2c7829a4 2019-06-17 stsp usage_init();
347 2c7829a4 2019-06-17 stsp /* NOTREACHED */
348 2c7829a4 2019-06-17 stsp }
349 2c7829a4 2019-06-17 stsp }
350 2c7829a4 2019-06-17 stsp
351 2c7829a4 2019-06-17 stsp argc -= optind;
352 2c7829a4 2019-06-17 stsp argv += optind;
353 2c7829a4 2019-06-17 stsp
354 2c7829a4 2019-06-17 stsp #ifndef PROFILE
355 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
356 2c7829a4 2019-06-17 stsp err(1, "pledge");
357 2c7829a4 2019-06-17 stsp #endif
358 2c7829a4 2019-06-17 stsp if (argc != 1)
359 bc20e173 2019-06-17 stsp usage_init();
360 2c7829a4 2019-06-17 stsp
361 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
362 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
363 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
364 2c7829a4 2019-06-17 stsp
365 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
366 2c7829a4 2019-06-17 stsp
367 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
368 2c7829a4 2019-06-17 stsp if (error &&
369 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
370 2c7829a4 2019-06-17 stsp goto done;
371 2c7829a4 2019-06-17 stsp
372 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
373 2c7829a4 2019-06-17 stsp if (error)
374 2c7829a4 2019-06-17 stsp goto done;
375 2c7829a4 2019-06-17 stsp
376 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
377 3ce1b845 2019-07-15 stsp done:
378 3ce1b845 2019-07-15 stsp free(repo_path);
379 3ce1b845 2019-07-15 stsp return error;
380 3ce1b845 2019-07-15 stsp }
381 3ce1b845 2019-07-15 stsp
382 3ce1b845 2019-07-15 stsp __dead static void
383 3ce1b845 2019-07-15 stsp usage_import(void)
384 3ce1b845 2019-07-15 stsp {
385 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
386 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
387 3ce1b845 2019-07-15 stsp exit(1);
388 3ce1b845 2019-07-15 stsp }
389 3ce1b845 2019-07-15 stsp
390 3ce1b845 2019-07-15 stsp int
391 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
392 3ce1b845 2019-07-15 stsp {
393 3ce1b845 2019-07-15 stsp pid_t pid;
394 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
395 3ce1b845 2019-07-15 stsp int st = -1;
396 3ce1b845 2019-07-15 stsp
397 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
398 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
399 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
400 3ce1b845 2019-07-15 stsp
401 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
402 3ce1b845 2019-07-15 stsp case -1:
403 3ce1b845 2019-07-15 stsp goto doneediting;
404 3ce1b845 2019-07-15 stsp case 0:
405 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
406 3ce1b845 2019-07-15 stsp _exit(127);
407 3ce1b845 2019-07-15 stsp }
408 3ce1b845 2019-07-15 stsp
409 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
410 3ce1b845 2019-07-15 stsp if (errno != EINTR)
411 3ce1b845 2019-07-15 stsp break;
412 3ce1b845 2019-07-15 stsp
413 3ce1b845 2019-07-15 stsp doneediting:
414 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
415 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
416 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
417 3ce1b845 2019-07-15 stsp
418 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
419 3ce1b845 2019-07-15 stsp errno = EINTR;
420 3ce1b845 2019-07-15 stsp return -1;
421 3ce1b845 2019-07-15 stsp }
422 3ce1b845 2019-07-15 stsp
423 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
424 3ce1b845 2019-07-15 stsp }
425 3ce1b845 2019-07-15 stsp
426 3ce1b845 2019-07-15 stsp static const struct got_error *
427 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
428 bfa12d5e 2020-09-26 stsp const char *initial_content, size_t initial_content_len)
429 3ce1b845 2019-07-15 stsp {
430 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
431 bfa12d5e 2020-09-26 stsp char *line = NULL;
432 bfa12d5e 2020-09-26 stsp size_t linesize = 0;
433 bfa12d5e 2020-09-26 stsp ssize_t linelen;
434 3ce1b845 2019-07-15 stsp struct stat st, st2;
435 bfa12d5e 2020-09-26 stsp FILE *fp = NULL;
436 bfa12d5e 2020-09-26 stsp size_t len, logmsg_len;
437 bfa12d5e 2020-09-26 stsp char *initial_content_stripped = NULL, *buf = NULL, *s;
438 3ce1b845 2019-07-15 stsp
439 3ce1b845 2019-07-15 stsp *logmsg = NULL;
440 3ce1b845 2019-07-15 stsp
441 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
442 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
443 3ce1b845 2019-07-15 stsp
444 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
445 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
446 3ce1b845 2019-07-15 stsp
447 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
448 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
449 3ce1b845 2019-07-15 stsp
450 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
451 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
452 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
453 3ce1b845 2019-07-15 stsp
454 bfa12d5e 2020-09-26 stsp /*
455 bfa12d5e 2020-09-26 stsp * Set up a stripped version of the initial content without comments
456 bfa12d5e 2020-09-26 stsp * and blank lines. We need this in order to check if the message
457 bfa12d5e 2020-09-26 stsp * has in fact been edited.
458 bfa12d5e 2020-09-26 stsp */
459 bfa12d5e 2020-09-26 stsp initial_content_stripped = malloc(initial_content_len + 1);
460 bfa12d5e 2020-09-26 stsp if (initial_content_stripped == NULL)
461 bfa12d5e 2020-09-26 stsp return got_error_from_errno("malloc");
462 bfa12d5e 2020-09-26 stsp initial_content_stripped[0] = '\0';
463 bfa12d5e 2020-09-26 stsp
464 bfa12d5e 2020-09-26 stsp buf = strdup(initial_content);
465 bfa12d5e 2020-09-26 stsp if (buf == NULL) {
466 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("strdup");
467 bfa12d5e 2020-09-26 stsp goto done;
468 bfa12d5e 2020-09-26 stsp }
469 bfa12d5e 2020-09-26 stsp s = buf;
470 bfa12d5e 2020-09-26 stsp len = 0;
471 bfa12d5e 2020-09-26 stsp while ((line = strsep(&s, "\n")) != NULL) {
472 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
473 bfa12d5e 2020-09-26 stsp continue; /* remove comments and leading empty lines */
474 bfa12d5e 2020-09-26 stsp len = strlcat(initial_content_stripped, line,
475 bfa12d5e 2020-09-26 stsp initial_content_len + 1);
476 bfa12d5e 2020-09-26 stsp if (len >= initial_content_len + 1) {
477 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
478 bfa12d5e 2020-09-26 stsp goto done;
479 bfa12d5e 2020-09-26 stsp }
480 bfa12d5e 2020-09-26 stsp }
481 bfa12d5e 2020-09-26 stsp while (len > 0 && initial_content_stripped[len - 1] == '\n') {
482 bfa12d5e 2020-09-26 stsp initial_content_stripped[len - 1] = '\0';
483 bfa12d5e 2020-09-26 stsp len--;
484 bfa12d5e 2020-09-26 stsp }
485 bfa12d5e 2020-09-26 stsp
486 bfa12d5e 2020-09-26 stsp logmsg_len = st2.st_size;
487 bfa12d5e 2020-09-26 stsp *logmsg = malloc(logmsg_len + 1);
488 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
489 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
490 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
491 3ce1b845 2019-07-15 stsp
492 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
493 3ce1b845 2019-07-15 stsp if (fp == NULL) {
494 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
495 3ce1b845 2019-07-15 stsp goto done;
496 3ce1b845 2019-07-15 stsp }
497 bfa12d5e 2020-09-26 stsp
498 bfa12d5e 2020-09-26 stsp len = 0;
499 bfa12d5e 2020-09-26 stsp while ((linelen = getline(&line, &linesize, fp)) != -1) {
500 bfa12d5e 2020-09-26 stsp if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
501 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
502 bfa12d5e 2020-09-26 stsp len = strlcat(*logmsg, line, logmsg_len + 1);
503 bfa12d5e 2020-09-26 stsp if (len >= logmsg_len + 1) {
504 bfa12d5e 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
505 bfa12d5e 2020-09-26 stsp goto done;
506 bfa12d5e 2020-09-26 stsp }
507 3ce1b845 2019-07-15 stsp }
508 bfa12d5e 2020-09-26 stsp free(line);
509 bfa12d5e 2020-09-26 stsp if (ferror(fp)) {
510 bfa12d5e 2020-09-26 stsp err = got_ferror(fp, GOT_ERR_IO);
511 bfa12d5e 2020-09-26 stsp goto done;
512 bfa12d5e 2020-09-26 stsp }
513 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
514 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
515 3ce1b845 2019-07-15 stsp len--;
516 3ce1b845 2019-07-15 stsp }
517 3ce1b845 2019-07-15 stsp
518 bfa12d5e 2020-09-26 stsp if (len == 0) {
519 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
520 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
521 bfa12d5e 2020-09-26 stsp goto done;
522 bfa12d5e 2020-09-26 stsp }
523 bfa12d5e 2020-09-26 stsp if (strcmp(*logmsg, initial_content_stripped) == 0)
524 bfa12d5e 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
525 bfa12d5e 2020-09-26 stsp "no changes made to commit message, aborting");
526 3ce1b845 2019-07-15 stsp done:
527 bfa12d5e 2020-09-26 stsp free(initial_content_stripped);
528 bfa12d5e 2020-09-26 stsp free(buf);
529 bfa12d5e 2020-09-26 stsp if (fp && fclose(fp) == EOF && err == NULL)
530 bfa12d5e 2020-09-26 stsp err = got_error_from_errno("fclose");
531 3ce1b845 2019-07-15 stsp if (err) {
532 3ce1b845 2019-07-15 stsp free(*logmsg);
533 3ce1b845 2019-07-15 stsp *logmsg = NULL;
534 3ce1b845 2019-07-15 stsp }
535 3ce1b845 2019-07-15 stsp return err;
536 3ce1b845 2019-07-15 stsp }
537 3ce1b845 2019-07-15 stsp
538 3ce1b845 2019-07-15 stsp static const struct got_error *
539 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
540 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
541 3ce1b845 2019-07-15 stsp {
542 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
543 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
544 1601cb9f 2020-09-11 naddy int initial_content_len;
545 97972933 2020-09-11 stsp int fd = -1;
546 3ce1b845 2019-07-15 stsp
547 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
548 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
549 1601cb9f 2020-09-11 naddy branch_name);
550 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
551 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
552 3ce1b845 2019-07-15 stsp
553 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
554 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
555 3ce1b845 2019-07-15 stsp if (err)
556 3ce1b845 2019-07-15 stsp goto done;
557 3ce1b845 2019-07-15 stsp
558 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
559 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
560 97972933 2020-09-11 stsp goto done;
561 97972933 2020-09-11 stsp }
562 3ce1b845 2019-07-15 stsp
563 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
564 62d463ca 2020-10-20 naddy initial_content_len);
565 3ce1b845 2019-07-15 stsp done:
566 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
567 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
568 3ce1b845 2019-07-15 stsp free(initial_content);
569 59f86c76 2020-09-11 stsp if (err) {
570 59f86c76 2020-09-11 stsp free(*logmsg_path);
571 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
572 59f86c76 2020-09-11 stsp }
573 3ce1b845 2019-07-15 stsp return err;
574 3ce1b845 2019-07-15 stsp }
575 3ce1b845 2019-07-15 stsp
576 3ce1b845 2019-07-15 stsp static const struct got_error *
577 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
578 3ce1b845 2019-07-15 stsp {
579 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
580 3ce1b845 2019-07-15 stsp return NULL;
581 3ce1b845 2019-07-15 stsp }
582 3ce1b845 2019-07-15 stsp
583 3ce1b845 2019-07-15 stsp static const struct got_error *
584 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
585 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
586 84792843 2019-08-09 stsp {
587 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
588 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
589 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
590 84792843 2019-08-09 stsp
591 84792843 2019-08-09 stsp *author = NULL;
592 aba9c984 2019-09-08 stsp
593 50b0790e 2020-09-11 stsp if (worktree)
594 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
595 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
596 50b0790e 2020-09-11 stsp
597 50b0790e 2020-09-11 stsp /*
598 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
599 50b0790e 2020-09-11 stsp * significant to least significant:
600 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
601 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
602 50b0790e 2020-09-11 stsp * 3) repository's git config file
603 50b0790e 2020-09-11 stsp * 4) environment variables
604 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
605 50b0790e 2020-09-11 stsp */
606 50b0790e 2020-09-11 stsp
607 50b0790e 2020-09-11 stsp if (worktree_conf)
608 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
609 50b0790e 2020-09-11 stsp if (got_author == NULL)
610 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
611 84792843 2019-08-09 stsp if (got_author == NULL) {
612 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
613 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
614 c9956ddf 2019-09-08 stsp if (name && email) {
615 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
616 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
617 c9956ddf 2019-09-08 stsp return NULL;
618 c9956ddf 2019-09-08 stsp }
619 257add31 2020-09-09 stsp
620 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
621 257add31 2020-09-09 stsp if (got_author == NULL) {
622 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
623 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
624 257add31 2020-09-09 stsp repo);
625 257add31 2020-09-09 stsp if (name && email) {
626 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
627 257add31 2020-09-09 stsp == -1)
628 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
629 257add31 2020-09-09 stsp return NULL;
630 257add31 2020-09-09 stsp }
631 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
632 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
633 257add31 2020-09-09 stsp }
634 84792843 2019-08-09 stsp }
635 84792843 2019-08-09 stsp
636 aba9c984 2019-09-08 stsp *author = strdup(got_author);
637 aba9c984 2019-09-08 stsp if (*author == NULL)
638 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
639 84792843 2019-08-09 stsp
640 84792843 2019-08-09 stsp /*
641 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
642 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
643 84792843 2019-08-09 stsp */
644 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
645 84792843 2019-08-09 stsp got_author++;
646 aba9c984 2019-09-08 stsp if (*got_author != '<') {
647 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
648 aba9c984 2019-09-08 stsp goto done;
649 aba9c984 2019-09-08 stsp }
650 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
651 84792843 2019-08-09 stsp got_author++;
652 aba9c984 2019-09-08 stsp if (*got_author != '@') {
653 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
654 aba9c984 2019-09-08 stsp goto done;
655 aba9c984 2019-09-08 stsp }
656 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
657 84792843 2019-08-09 stsp got_author++;
658 84792843 2019-08-09 stsp if (*got_author != '>')
659 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
660 aba9c984 2019-09-08 stsp done:
661 aba9c984 2019-09-08 stsp if (err) {
662 aba9c984 2019-09-08 stsp free(*author);
663 aba9c984 2019-09-08 stsp *author = NULL;
664 aba9c984 2019-09-08 stsp }
665 aba9c984 2019-09-08 stsp return err;
666 c9956ddf 2019-09-08 stsp }
667 c9956ddf 2019-09-08 stsp
668 c9956ddf 2019-09-08 stsp static const struct got_error *
669 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
670 c9956ddf 2019-09-08 stsp {
671 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
672 c9956ddf 2019-09-08 stsp
673 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
674 c9956ddf 2019-09-08 stsp if (homedir) {
675 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
676 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
677 c9956ddf 2019-09-08 stsp
678 c9956ddf 2019-09-08 stsp }
679 c9956ddf 2019-09-08 stsp return NULL;
680 84792843 2019-08-09 stsp }
681 84792843 2019-08-09 stsp
682 84792843 2019-08-09 stsp static const struct got_error *
683 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
684 3ce1b845 2019-07-15 stsp {
685 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
686 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
687 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
688 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
689 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
690 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
691 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
692 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
693 3ce1b845 2019-07-15 stsp int ch;
694 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
695 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
696 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
697 3ce1b845 2019-07-15 stsp
698 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
699 3ce1b845 2019-07-15 stsp
700 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
701 3ce1b845 2019-07-15 stsp switch (ch) {
702 3ce1b845 2019-07-15 stsp case 'b':
703 3ce1b845 2019-07-15 stsp branch_name = optarg;
704 3ce1b845 2019-07-15 stsp break;
705 3ce1b845 2019-07-15 stsp case 'm':
706 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
707 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
708 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
709 3ce1b845 2019-07-15 stsp goto done;
710 3ce1b845 2019-07-15 stsp }
711 3ce1b845 2019-07-15 stsp break;
712 3ce1b845 2019-07-15 stsp case 'r':
713 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
714 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
715 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
716 9ba1d308 2019-10-21 stsp optarg);
717 3ce1b845 2019-07-15 stsp goto done;
718 3ce1b845 2019-07-15 stsp }
719 3ce1b845 2019-07-15 stsp break;
720 3ce1b845 2019-07-15 stsp case 'I':
721 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
722 3ce1b845 2019-07-15 stsp break;
723 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
724 3ce1b845 2019-07-15 stsp NULL);
725 3ce1b845 2019-07-15 stsp if (error)
726 3ce1b845 2019-07-15 stsp goto done;
727 3ce1b845 2019-07-15 stsp break;
728 3ce1b845 2019-07-15 stsp default:
729 b2b65d18 2019-08-22 stsp usage_import();
730 3ce1b845 2019-07-15 stsp /* NOTREACHED */
731 3ce1b845 2019-07-15 stsp }
732 3ce1b845 2019-07-15 stsp }
733 3ce1b845 2019-07-15 stsp
734 3ce1b845 2019-07-15 stsp argc -= optind;
735 3ce1b845 2019-07-15 stsp argv += optind;
736 3ce1b845 2019-07-15 stsp
737 3ce1b845 2019-07-15 stsp #ifndef PROFILE
738 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
739 aba9c984 2019-09-08 stsp "unveil",
740 3ce1b845 2019-07-15 stsp NULL) == -1)
741 3ce1b845 2019-07-15 stsp err(1, "pledge");
742 3ce1b845 2019-07-15 stsp #endif
743 3ce1b845 2019-07-15 stsp if (argc != 1)
744 3ce1b845 2019-07-15 stsp usage_import();
745 2c7829a4 2019-06-17 stsp
746 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
747 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
748 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
749 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
750 3ce1b845 2019-07-15 stsp }
751 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
752 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
753 c9956ddf 2019-09-08 stsp if (error)
754 c9956ddf 2019-09-08 stsp goto done;
755 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
756 3ce1b845 2019-07-15 stsp if (error)
757 3ce1b845 2019-07-15 stsp goto done;
758 aba9c984 2019-09-08 stsp
759 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
760 aba9c984 2019-09-08 stsp if (error)
761 aba9c984 2019-09-08 stsp return error;
762 e560b7e0 2019-11-28 stsp
763 e560b7e0 2019-11-28 stsp /*
764 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
765 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
766 e560b7e0 2019-11-28 stsp * an unintended typo.
767 e560b7e0 2019-11-28 stsp */
768 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
769 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
770 3ce1b845 2019-07-15 stsp
771 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
772 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
773 3ce1b845 2019-07-15 stsp goto done;
774 3ce1b845 2019-07-15 stsp }
775 3ce1b845 2019-07-15 stsp
776 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
777 3ce1b845 2019-07-15 stsp if (error) {
778 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
779 3ce1b845 2019-07-15 stsp goto done;
780 3ce1b845 2019-07-15 stsp } else {
781 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
782 3ce1b845 2019-07-15 stsp "import target branch already exists");
783 3ce1b845 2019-07-15 stsp goto done;
784 3ce1b845 2019-07-15 stsp }
785 3ce1b845 2019-07-15 stsp
786 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
787 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
788 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
789 3ce1b845 2019-07-15 stsp goto done;
790 3ce1b845 2019-07-15 stsp }
791 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
792 3ce1b845 2019-07-15 stsp
793 3ce1b845 2019-07-15 stsp /*
794 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
795 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
796 3ce1b845 2019-07-15 stsp */
797 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
798 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
799 3ce1b845 2019-07-15 stsp if (error)
800 3ce1b845 2019-07-15 stsp goto done;
801 8e158b01 2019-09-22 stsp free(logmsg);
802 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
803 ef293bdd 2019-10-21 stsp path_dir, refname);
804 ef293bdd 2019-10-21 stsp if (error) {
805 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
806 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
807 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
808 3ce1b845 2019-07-15 stsp goto done;
809 ef293bdd 2019-10-21 stsp }
810 3ce1b845 2019-07-15 stsp }
811 3ce1b845 2019-07-15 stsp
812 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
813 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
814 ef293bdd 2019-10-21 stsp if (logmsg_path)
815 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
816 3ce1b845 2019-07-15 stsp goto done;
817 ef293bdd 2019-10-21 stsp }
818 3ce1b845 2019-07-15 stsp
819 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
820 ef293bdd 2019-10-21 stsp if (error) {
821 ef293bdd 2019-10-21 stsp if (logmsg_path)
822 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
823 ef293bdd 2019-10-21 stsp goto done;
824 ef293bdd 2019-10-21 stsp }
825 ef293bdd 2019-10-21 stsp
826 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
827 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
828 ef293bdd 2019-10-21 stsp if (error) {
829 ef293bdd 2019-10-21 stsp if (logmsg_path)
830 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
831 3ce1b845 2019-07-15 stsp goto done;
832 ef293bdd 2019-10-21 stsp }
833 3ce1b845 2019-07-15 stsp
834 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
835 ef293bdd 2019-10-21 stsp if (error) {
836 ef293bdd 2019-10-21 stsp if (logmsg_path)
837 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
838 3ce1b845 2019-07-15 stsp goto done;
839 ef293bdd 2019-10-21 stsp }
840 3ce1b845 2019-07-15 stsp
841 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
842 ef293bdd 2019-10-21 stsp if (error) {
843 ef293bdd 2019-10-21 stsp if (logmsg_path)
844 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
845 3ce1b845 2019-07-15 stsp goto done;
846 ef293bdd 2019-10-21 stsp }
847 3ce1b845 2019-07-15 stsp
848 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
849 ef293bdd 2019-10-21 stsp if (error) {
850 ef293bdd 2019-10-21 stsp if (logmsg_path)
851 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
852 3ce1b845 2019-07-15 stsp goto done;
853 ef293bdd 2019-10-21 stsp }
854 3ce1b845 2019-07-15 stsp
855 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
856 3ce1b845 2019-07-15 stsp if (error) {
857 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
858 ef293bdd 2019-10-21 stsp if (logmsg_path)
859 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
860 3ce1b845 2019-07-15 stsp goto done;
861 ef293bdd 2019-10-21 stsp }
862 3ce1b845 2019-07-15 stsp
863 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
864 3ce1b845 2019-07-15 stsp branch_ref);
865 ef293bdd 2019-10-21 stsp if (error) {
866 ef293bdd 2019-10-21 stsp if (logmsg_path)
867 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
868 3ce1b845 2019-07-15 stsp goto done;
869 ef293bdd 2019-10-21 stsp }
870 3ce1b845 2019-07-15 stsp
871 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
872 ef293bdd 2019-10-21 stsp if (error) {
873 ef293bdd 2019-10-21 stsp if (logmsg_path)
874 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
875 3ce1b845 2019-07-15 stsp goto done;
876 ef293bdd 2019-10-21 stsp }
877 3ce1b845 2019-07-15 stsp }
878 3ce1b845 2019-07-15 stsp
879 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
880 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
881 2c7829a4 2019-06-17 stsp done:
882 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
883 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
884 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
885 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
886 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
887 8e158b01 2019-09-22 stsp free(logmsg);
888 ef293bdd 2019-10-21 stsp free(logmsg_path);
889 2c7829a4 2019-06-17 stsp free(repo_path);
890 3ce1b845 2019-07-15 stsp free(editor);
891 3ce1b845 2019-07-15 stsp free(refname);
892 3ce1b845 2019-07-15 stsp free(new_commit_id);
893 3ce1b845 2019-07-15 stsp free(id_str);
894 aba9c984 2019-09-08 stsp free(author);
895 c9956ddf 2019-09-08 stsp free(gitconfig_path);
896 3ce1b845 2019-07-15 stsp if (branch_ref)
897 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
898 3ce1b845 2019-07-15 stsp if (head_ref)
899 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
900 2c7829a4 2019-06-17 stsp return error;
901 93658fb9 2020-03-18 stsp }
902 93658fb9 2020-03-18 stsp
903 93658fb9 2020-03-18 stsp __dead static void
904 93658fb9 2020-03-18 stsp usage_clone(void)
905 93658fb9 2020-03-18 stsp {
906 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
907 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
908 93658fb9 2020-03-18 stsp exit(1);
909 93658fb9 2020-03-18 stsp }
910 892ac3b6 2020-03-18 stsp
911 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
912 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
913 892ac3b6 2020-03-18 stsp int last_p_indexed;
914 892ac3b6 2020-03-18 stsp int last_p_resolved;
915 68999b92 2020-03-18 stsp int verbosity;
916 04d9a9ec 2020-09-24 stsp
917 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
918 04d9a9ec 2020-09-24 stsp
919 04d9a9ec 2020-09-24 stsp int create_configs;
920 04d9a9ec 2020-09-24 stsp int configs_created;
921 04d9a9ec 2020-09-24 stsp struct {
922 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
923 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
924 04d9a9ec 2020-09-24 stsp const char *proto;
925 04d9a9ec 2020-09-24 stsp const char *host;
926 04d9a9ec 2020-09-24 stsp const char *port;
927 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
928 04d9a9ec 2020-09-24 stsp const char *git_url;
929 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
930 04d9a9ec 2020-09-24 stsp int mirror_references;
931 04d9a9ec 2020-09-24 stsp } config_info;
932 892ac3b6 2020-03-18 stsp };
933 93658fb9 2020-03-18 stsp
934 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
935 93658fb9 2020-03-18 stsp static const struct got_error *
936 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
937 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
938 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
939 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo);
940 04d9a9ec 2020-09-24 stsp
941 04d9a9ec 2020-09-24 stsp static const struct got_error *
942 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
943 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
944 531c3985 2020-03-18 stsp {
945 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
946 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
947 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
948 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
949 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
950 04d9a9ec 2020-09-24 stsp
951 04d9a9ec 2020-09-24 stsp /*
952 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
953 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
954 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
955 04d9a9ec 2020-09-24 stsp * we have all required information.
956 04d9a9ec 2020-09-24 stsp */
957 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
958 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
959 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
960 62d463ca 2020-10-20 naddy a->config_info.host, a->config_info.port,
961 62d463ca 2020-10-20 naddy a->config_info.remote_repo_path,
962 62d463ca 2020-10-20 naddy a->config_info.git_url,
963 62d463ca 2020-10-20 naddy a->config_info.fetch_all_branches,
964 62d463ca 2020-10-20 naddy a->config_info.mirror_references,
965 62d463ca 2020-10-20 naddy a->config_info.symrefs,
966 62d463ca 2020-10-20 naddy a->config_info.wanted_branches, a->repo);
967 04d9a9ec 2020-09-24 stsp if (err)
968 04d9a9ec 2020-09-24 stsp return err;
969 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
970 04d9a9ec 2020-09-24 stsp }
971 b2409d58 2020-03-18 stsp
972 68999b92 2020-03-18 stsp if (a->verbosity < 0)
973 68999b92 2020-03-18 stsp return NULL;
974 68999b92 2020-03-18 stsp
975 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
976 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
977 892ac3b6 2020-03-18 stsp fflush(stdout);
978 12d1281e 2020-03-19 stsp return NULL;
979 b2409d58 2020-03-18 stsp }
980 b2409d58 2020-03-18 stsp
981 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
982 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
983 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
984 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
985 892ac3b6 2020-03-18 stsp print_size = 1;
986 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
987 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
988 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
989 892ac3b6 2020-03-18 stsp }
990 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
991 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
992 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
993 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
994 892ac3b6 2020-03-18 stsp print_indexed = 1;
995 892ac3b6 2020-03-18 stsp print_size = 1;
996 892ac3b6 2020-03-18 stsp }
997 61cc1a7a 2020-03-18 stsp }
998 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
999 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
1000 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
1001 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
1002 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
1003 892ac3b6 2020-03-18 stsp print_resolved = 1;
1004 892ac3b6 2020-03-18 stsp print_indexed = 1;
1005 892ac3b6 2020-03-18 stsp print_size = 1;
1006 892ac3b6 2020-03-18 stsp }
1007 61cc1a7a 2020-03-18 stsp }
1008 3168e5da 2020-09-10 stsp
1009 d2cdc636 2020-03-18 stsp }
1010 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1011 892ac3b6 2020-03-18 stsp printf("\r");
1012 892ac3b6 2020-03-18 stsp if (print_size)
1013 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
1014 d715f13e 2020-03-19 stsp if (print_indexed)
1015 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
1016 d715f13e 2020-03-19 stsp if (print_resolved)
1017 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
1018 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
1019 892ac3b6 2020-03-18 stsp fflush(stdout);
1020 892ac3b6 2020-03-18 stsp
1021 531c3985 2020-03-18 stsp return NULL;
1022 531c3985 2020-03-18 stsp }
1023 531c3985 2020-03-18 stsp
1024 531c3985 2020-03-18 stsp static const struct got_error *
1025 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
1026 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
1027 4ba14133 2020-03-20 stsp {
1028 4ba14133 2020-03-20 stsp const struct got_error *err;
1029 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
1030 4ba14133 2020-03-20 stsp
1031 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1032 4ba14133 2020-03-20 stsp if (err)
1033 4ba14133 2020-03-20 stsp return err;
1034 4ba14133 2020-03-20 stsp
1035 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
1036 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
1037 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
1038 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
1039 6338a6a1 2020-03-21 stsp }
1040 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
1041 4ba14133 2020-03-20 stsp return err;
1042 4ba14133 2020-03-20 stsp }
1043 4ba14133 2020-03-20 stsp
1044 4ba14133 2020-03-20 stsp static const struct got_error *
1045 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
1046 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
1047 41b0de12 2020-03-21 stsp {
1048 41b0de12 2020-03-21 stsp const struct got_error *err;
1049 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
1050 41b0de12 2020-03-21 stsp
1051 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1052 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1053 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1054 41b0de12 2020-03-21 stsp
1055 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1056 41b0de12 2020-03-21 stsp }
1057 41b0de12 2020-03-21 stsp
1058 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1059 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1060 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1061 41b0de12 2020-03-21 stsp char *id_str;
1062 41b0de12 2020-03-21 stsp
1063 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1064 41b0de12 2020-03-21 stsp if (err)
1065 41b0de12 2020-03-21 stsp return err;
1066 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1067 41b0de12 2020-03-21 stsp free(id_str);
1068 41b0de12 2020-03-21 stsp }
1069 41b0de12 2020-03-21 stsp
1070 41b0de12 2020-03-21 stsp return NULL;
1071 6338a6a1 2020-03-21 stsp }
1072 6338a6a1 2020-03-21 stsp
1073 6338a6a1 2020-03-21 stsp static const struct got_error *
1074 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1075 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1076 6338a6a1 2020-03-21 stsp {
1077 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1078 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1079 6338a6a1 2020-03-21 stsp char *id_str;
1080 6338a6a1 2020-03-21 stsp
1081 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1082 6338a6a1 2020-03-21 stsp if (err)
1083 6338a6a1 2020-03-21 stsp return err;
1084 6338a6a1 2020-03-21 stsp
1085 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1086 6338a6a1 2020-03-21 stsp if (err)
1087 6338a6a1 2020-03-21 stsp goto done;
1088 6338a6a1 2020-03-21 stsp
1089 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1090 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1091 6338a6a1 2020-03-21 stsp
1092 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1093 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1094 6338a6a1 2020-03-21 stsp done:
1095 6338a6a1 2020-03-21 stsp free(id_str);
1096 6338a6a1 2020-03-21 stsp return err;
1097 0e4002ca 2020-03-21 stsp }
1098 0e4002ca 2020-03-21 stsp
1099 0e4002ca 2020-03-21 stsp static int
1100 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1101 0e4002ca 2020-03-21 stsp {
1102 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1103 0e4002ca 2020-03-21 stsp return 0;
1104 0e4002ca 2020-03-21 stsp refname += 5;
1105 0e4002ca 2020-03-21 stsp
1106 0e4002ca 2020-03-21 stsp /*
1107 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1108 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1109 0e4002ca 2020-03-21 stsp */
1110 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1111 0e4002ca 2020-03-21 stsp return 0;
1112 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1113 0e4002ca 2020-03-21 stsp return 0;
1114 0e4002ca 2020-03-21 stsp
1115 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1116 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1117 0e4002ca 2020-03-21 stsp
1118 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1119 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1120 0e4002ca 2020-03-21 stsp return 1;
1121 0e4002ca 2020-03-21 stsp
1122 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1123 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1124 41b0de12 2020-03-21 stsp }
1125 41b0de12 2020-03-21 stsp
1126 0e4002ca 2020-03-21 stsp static int
1127 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1128 0e4002ca 2020-03-21 stsp {
1129 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1130 0e4002ca 2020-03-21 stsp
1131 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1132 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1133 0e4002ca 2020-03-21 stsp return 1;
1134 0e4002ca 2020-03-21 stsp }
1135 0e4002ca 2020-03-21 stsp
1136 0e4002ca 2020-03-21 stsp return 0;
1137 0e4002ca 2020-03-21 stsp }
1138 0e4002ca 2020-03-21 stsp
1139 41b0de12 2020-03-21 stsp static const struct got_error *
1140 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1141 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1142 0e4002ca 2020-03-21 stsp {
1143 0e4002ca 2020-03-21 stsp const struct got_error *err;
1144 0e4002ca 2020-03-21 stsp char *remote_refname;
1145 0e4002ca 2020-03-21 stsp
1146 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1147 0e4002ca 2020-03-21 stsp refname += 5;
1148 0e4002ca 2020-03-21 stsp
1149 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1150 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1151 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1152 0e4002ca 2020-03-21 stsp
1153 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1154 0e4002ca 2020-03-21 stsp free(remote_refname);
1155 7c0b7f42 2020-09-24 stsp return err;
1156 7c0b7f42 2020-09-24 stsp }
1157 7c0b7f42 2020-09-24 stsp
1158 7c0b7f42 2020-09-24 stsp static const struct got_error *
1159 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1160 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, int fetch_all_branches, int mirror_references,
1161 7c0b7f42 2020-09-24 stsp struct got_repository *repo)
1162 7c0b7f42 2020-09-24 stsp {
1163 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1164 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1165 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1166 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1167 7c0b7f42 2020-09-24 stsp ssize_t n;
1168 7c0b7f42 2020-09-24 stsp
1169 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1170 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1171 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1172 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1173 7c0b7f42 2020-09-24 stsp goto done;
1174 7c0b7f42 2020-09-24 stsp }
1175 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1176 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1177 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1178 7c0b7f42 2020-09-24 stsp goto done;
1179 7c0b7f42 2020-09-24 stsp }
1180 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1181 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1182 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1183 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1184 7c0b7f42 2020-09-24 stsp "%s%s%s"
1185 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1186 7c0b7f42 2020-09-24 stsp "%s"
1187 7c0b7f42 2020-09-24 stsp "}\n",
1188 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1189 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1190 7c0b7f42 2020-09-24 stsp remote_repo_path,
1191 7c0b7f42 2020-09-24 stsp mirror_references ? "\tmirror-references yes\n" : "") == -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 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1196 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1197 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1198 7c0b7f42 2020-09-24 stsp goto done;
1199 7c0b7f42 2020-09-24 stsp }
1200 7c0b7f42 2020-09-24 stsp
1201 7c0b7f42 2020-09-24 stsp done:
1202 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1203 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1204 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1205 7c0b7f42 2020-09-24 stsp return err;
1206 7c0b7f42 2020-09-24 stsp }
1207 7c0b7f42 2020-09-24 stsp
1208 7c0b7f42 2020-09-24 stsp static const struct got_error *
1209 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1210 7c0b7f42 2020-09-24 stsp int fetch_all_branches, int mirror_references, struct got_repository *repo)
1211 7c0b7f42 2020-09-24 stsp {
1212 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1213 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1214 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1215 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1216 7c0b7f42 2020-09-24 stsp ssize_t n;
1217 7c0b7f42 2020-09-24 stsp
1218 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1219 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1220 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1221 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1222 7c0b7f42 2020-09-24 stsp goto done;
1223 7c0b7f42 2020-09-24 stsp }
1224 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1225 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1226 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1227 7c0b7f42 2020-09-24 stsp goto done;
1228 7c0b7f42 2020-09-24 stsp }
1229 7c0b7f42 2020-09-24 stsp if (mirror_references) {
1230 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1231 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1232 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1233 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/*:refs/*\n"
1234 7c0b7f42 2020-09-24 stsp "\tmirror = true\n",
1235 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1236 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1237 7c0b7f42 2020-09-24 stsp goto done;
1238 7c0b7f42 2020-09-24 stsp }
1239 7c0b7f42 2020-09-24 stsp } else if (fetch_all_branches) {
1240 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1241 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1242 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1243 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1244 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1245 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1246 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1247 7c0b7f42 2020-09-24 stsp goto done;
1248 7c0b7f42 2020-09-24 stsp }
1249 7c0b7f42 2020-09-24 stsp } else {
1250 7c0b7f42 2020-09-24 stsp const char *branchname;
1251 7c0b7f42 2020-09-24 stsp
1252 7c0b7f42 2020-09-24 stsp /*
1253 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1254 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1255 7c0b7f42 2020-09-24 stsp */
1256 04d9a9ec 2020-09-24 stsp if (default_branch) {
1257 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1258 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1259 7c0b7f42 2020-09-24 stsp branchname += 11;
1260 7c0b7f42 2020-09-24 stsp } else
1261 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1262 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1263 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1264 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1265 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1266 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1267 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1268 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1269 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1270 7c0b7f42 2020-09-24 stsp goto done;
1271 7c0b7f42 2020-09-24 stsp }
1272 7c0b7f42 2020-09-24 stsp }
1273 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1274 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1275 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1276 7c0b7f42 2020-09-24 stsp goto done;
1277 7c0b7f42 2020-09-24 stsp }
1278 7c0b7f42 2020-09-24 stsp done:
1279 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1280 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1281 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1282 0e4002ca 2020-03-21 stsp return err;
1283 0e4002ca 2020-03-21 stsp }
1284 0e4002ca 2020-03-21 stsp
1285 0e4002ca 2020-03-21 stsp static const struct got_error *
1286 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1287 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1288 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1289 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo)
1290 04d9a9ec 2020-09-24 stsp {
1291 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1292 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1293 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1294 04d9a9ec 2020-09-24 stsp
1295 04d9a9ec 2020-09-24 stsp /*
1296 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1297 04d9a9ec 2020-09-24 stsp * one of those.
1298 04d9a9ec 2020-09-24 stsp */
1299 62d463ca 2020-10-20 naddy if (!TAILQ_EMPTY(wanted_branches)) {
1300 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1301 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1302 04d9a9ec 2020-09-24 stsp } else {
1303 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1304 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1305 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1306 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1307 04d9a9ec 2020-09-24 stsp
1308 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1309 04d9a9ec 2020-09-24 stsp continue;
1310 04d9a9ec 2020-09-24 stsp
1311 04d9a9ec 2020-09-24 stsp default_branch = target;
1312 04d9a9ec 2020-09-24 stsp break;
1313 04d9a9ec 2020-09-24 stsp }
1314 04d9a9ec 2020-09-24 stsp }
1315 04d9a9ec 2020-09-24 stsp
1316 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1317 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1318 04d9a9ec 2020-09-24 stsp fetch_all_branches, mirror_references, repo);
1319 04d9a9ec 2020-09-24 stsp if (err)
1320 04d9a9ec 2020-09-24 stsp return err;
1321 04d9a9ec 2020-09-24 stsp
1322 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1323 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1324 04d9a9ec 2020-09-24 stsp mirror_references, repo);
1325 04d9a9ec 2020-09-24 stsp }
1326 04d9a9ec 2020-09-24 stsp
1327 04d9a9ec 2020-09-24 stsp static const struct got_error *
1328 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1329 93658fb9 2020-03-18 stsp {
1330 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1331 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1332 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1333 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1334 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1335 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1336 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1337 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1338 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1339 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1340 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1341 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1342 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1343 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1344 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1345 93658fb9 2020-03-18 stsp
1346 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1347 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1348 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1349 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1350 d9b4d0c0 2020-03-18 stsp
1351 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1352 93658fb9 2020-03-18 stsp switch (ch) {
1353 659e7fbd 2020-03-20 stsp case 'a':
1354 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1355 4ba14133 2020-03-20 stsp break;
1356 4ba14133 2020-03-20 stsp case 'b':
1357 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1358 4ba14133 2020-03-20 stsp optarg, NULL);
1359 4ba14133 2020-03-20 stsp if (error)
1360 4ba14133 2020-03-20 stsp return error;
1361 659e7fbd 2020-03-20 stsp break;
1362 41b0de12 2020-03-21 stsp case 'l':
1363 41b0de12 2020-03-21 stsp list_refs_only = 1;
1364 41b0de12 2020-03-21 stsp break;
1365 469dd726 2020-03-20 stsp case 'm':
1366 469dd726 2020-03-20 stsp mirror_references = 1;
1367 469dd726 2020-03-20 stsp break;
1368 68999b92 2020-03-18 stsp case 'v':
1369 68999b92 2020-03-18 stsp if (verbosity < 0)
1370 68999b92 2020-03-18 stsp verbosity = 0;
1371 68999b92 2020-03-18 stsp else if (verbosity < 3)
1372 68999b92 2020-03-18 stsp verbosity++;
1373 68999b92 2020-03-18 stsp break;
1374 68999b92 2020-03-18 stsp case 'q':
1375 68999b92 2020-03-18 stsp verbosity = -1;
1376 68999b92 2020-03-18 stsp break;
1377 0e4002ca 2020-03-21 stsp case 'R':
1378 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1379 0e4002ca 2020-03-21 stsp optarg, NULL);
1380 0e4002ca 2020-03-21 stsp if (error)
1381 0e4002ca 2020-03-21 stsp return error;
1382 0e4002ca 2020-03-21 stsp break;
1383 93658fb9 2020-03-18 stsp default:
1384 93658fb9 2020-03-18 stsp usage_clone();
1385 93658fb9 2020-03-18 stsp break;
1386 93658fb9 2020-03-18 stsp }
1387 93658fb9 2020-03-18 stsp }
1388 93658fb9 2020-03-18 stsp argc -= optind;
1389 93658fb9 2020-03-18 stsp argv += optind;
1390 39c64a6a 2020-03-18 stsp
1391 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1392 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1393 41b0de12 2020-03-21 stsp if (list_refs_only) {
1394 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1395 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1396 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1397 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1398 41b0de12 2020-03-21 stsp if (mirror_references)
1399 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1400 41b0de12 2020-03-21 stsp if (verbosity == -1)
1401 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1402 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1403 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1404 41b0de12 2020-03-21 stsp }
1405 4ba14133 2020-03-20 stsp
1406 93658fb9 2020-03-18 stsp uri = argv[0];
1407 9df6f38b 2020-03-18 stsp
1408 9df6f38b 2020-03-18 stsp if (argc == 1)
1409 93658fb9 2020-03-18 stsp dirname = NULL;
1410 9df6f38b 2020-03-18 stsp else if (argc == 2)
1411 93658fb9 2020-03-18 stsp dirname = argv[1];
1412 93658fb9 2020-03-18 stsp else
1413 93658fb9 2020-03-18 stsp usage_clone();
1414 09838ffc 2020-03-18 stsp
1415 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1416 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1417 39c64a6a 2020-03-18 stsp if (error)
1418 09f63084 2020-03-20 stsp goto done;
1419 09f63084 2020-03-20 stsp
1420 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1421 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1422 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1423 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1424 09838ffc 2020-03-18 stsp goto done;
1425 09f63084 2020-03-20 stsp }
1426 09838ffc 2020-03-18 stsp
1427 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1428 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1429 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1430 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1431 39c64a6a 2020-03-18 stsp err(1, "pledge");
1432 b46f3e71 2020-03-18 stsp #endif
1433 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1434 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1435 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1436 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1437 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1438 39c64a6a 2020-03-18 stsp err(1, "pledge");
1439 b46f3e71 2020-03-18 stsp #endif
1440 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1441 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1442 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1443 39c64a6a 2020-03-18 stsp goto done;
1444 39c64a6a 2020-03-18 stsp } else {
1445 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1446 39c64a6a 2020-03-18 stsp goto done;
1447 39c64a6a 2020-03-18 stsp }
1448 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1449 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1450 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1451 bb64b798 2020-03-18 stsp goto done;
1452 bb64b798 2020-03-18 stsp }
1453 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1454 bb64b798 2020-03-18 stsp } else
1455 bb64b798 2020-03-18 stsp repo_path = dirname;
1456 bb64b798 2020-03-18 stsp
1457 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1458 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1459 2751fe64 2020-09-24 stsp if (error &&
1460 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1461 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1462 41b0de12 2020-03-21 stsp goto done;
1463 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1464 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1465 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1466 41b0de12 2020-03-21 stsp goto done;
1467 2751fe64 2020-09-24 stsp }
1468 41b0de12 2020-03-21 stsp }
1469 bb64b798 2020-03-18 stsp
1470 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1471 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1472 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1473 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1474 ee448f5f 2020-03-18 stsp goto done;
1475 ee448f5f 2020-03-18 stsp }
1476 ee448f5f 2020-03-18 stsp }
1477 f535bcd4 2020-09-30 stsp error = apply_unveil(repo_path, 0, NULL);
1478 ee448f5f 2020-03-18 stsp if (error)
1479 ee448f5f 2020-03-18 stsp goto done;
1480 f79e6490 2020-04-19 stsp
1481 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1482 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1483 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1484 ee448f5f 2020-03-18 stsp
1485 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1486 9c52365f 2020-03-21 stsp server_path, verbosity);
1487 39c64a6a 2020-03-18 stsp if (error)
1488 bb64b798 2020-03-18 stsp goto done;
1489 bb64b798 2020-03-18 stsp
1490 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1491 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1492 2751fe64 2020-09-24 stsp if (error)
1493 2751fe64 2020-09-24 stsp goto done;
1494 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1495 2751fe64 2020-09-24 stsp if (error)
1496 2751fe64 2020-09-24 stsp goto done;
1497 2751fe64 2020-09-24 stsp }
1498 2751fe64 2020-09-24 stsp
1499 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1500 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1501 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1502 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1503 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1504 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1505 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1506 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1507 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1508 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1509 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1510 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1511 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1512 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1513 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1514 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1515 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1516 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1517 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1518 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1519 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1520 39c64a6a 2020-03-18 stsp if (error)
1521 d9b4d0c0 2020-03-18 stsp goto done;
1522 d9b4d0c0 2020-03-18 stsp
1523 41b0de12 2020-03-21 stsp if (list_refs_only) {
1524 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1525 41b0de12 2020-03-21 stsp goto done;
1526 41b0de12 2020-03-21 stsp }
1527 41b0de12 2020-03-21 stsp
1528 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1529 39c64a6a 2020-03-18 stsp if (error)
1530 d9b4d0c0 2020-03-18 stsp goto done;
1531 68999b92 2020-03-18 stsp if (verbosity >= 0)
1532 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1533 d9b4d0c0 2020-03-18 stsp free(id_str);
1534 d9b4d0c0 2020-03-18 stsp
1535 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1536 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1537 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1538 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1539 7ebc0570 2020-03-18 stsp char *remote_refname;
1540 0e4002ca 2020-03-21 stsp
1541 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1542 0e4002ca 2020-03-21 stsp !mirror_references) {
1543 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1544 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1545 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1546 0e4002ca 2020-03-21 stsp if (error)
1547 0e4002ca 2020-03-21 stsp goto done;
1548 0e4002ca 2020-03-21 stsp continue;
1549 0e4002ca 2020-03-21 stsp }
1550 668a20f6 2020-03-18 stsp
1551 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1552 39c64a6a 2020-03-18 stsp if (error)
1553 d9b4d0c0 2020-03-18 stsp goto done;
1554 d9b4d0c0 2020-03-18 stsp
1555 469dd726 2020-03-20 stsp if (mirror_references)
1556 469dd726 2020-03-20 stsp continue;
1557 469dd726 2020-03-20 stsp
1558 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1559 7ebc0570 2020-03-18 stsp continue;
1560 7ebc0570 2020-03-18 stsp
1561 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1562 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1563 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1564 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1565 7ebc0570 2020-03-18 stsp goto done;
1566 7ebc0570 2020-03-18 stsp }
1567 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1568 f298ae0f 2020-03-25 stsp free(remote_refname);
1569 39c64a6a 2020-03-18 stsp if (error)
1570 d9b4d0c0 2020-03-18 stsp goto done;
1571 d9b4d0c0 2020-03-18 stsp }
1572 d9b4d0c0 2020-03-18 stsp
1573 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1574 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1575 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1576 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1577 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1578 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1579 d9b4d0c0 2020-03-18 stsp
1580 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1581 d9b4d0c0 2020-03-18 stsp continue;
1582 d9b4d0c0 2020-03-18 stsp
1583 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1584 39c64a6a 2020-03-18 stsp if (error) {
1585 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1586 55330abe 2020-03-20 stsp error = NULL;
1587 d9b4d0c0 2020-03-18 stsp continue;
1588 55330abe 2020-03-20 stsp }
1589 d9b4d0c0 2020-03-18 stsp goto done;
1590 d9b4d0c0 2020-03-18 stsp }
1591 d9b4d0c0 2020-03-18 stsp
1592 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1593 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1594 f298ae0f 2020-03-25 stsp if (error)
1595 f298ae0f 2020-03-25 stsp goto done;
1596 f298ae0f 2020-03-25 stsp
1597 f298ae0f 2020-03-25 stsp if (mirror_references)
1598 f298ae0f 2020-03-25 stsp continue;
1599 f298ae0f 2020-03-25 stsp
1600 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1601 f298ae0f 2020-03-25 stsp continue;
1602 f298ae0f 2020-03-25 stsp
1603 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1604 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1605 f298ae0f 2020-03-25 stsp refname) == -1) {
1606 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1607 f298ae0f 2020-03-25 stsp goto done;
1608 f298ae0f 2020-03-25 stsp }
1609 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1610 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1611 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1612 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1613 f298ae0f 2020-03-25 stsp free(remote_refname);
1614 f298ae0f 2020-03-25 stsp goto done;
1615 f298ae0f 2020-03-25 stsp }
1616 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1617 f298ae0f 2020-03-25 stsp if (error) {
1618 f298ae0f 2020-03-25 stsp free(remote_refname);
1619 f298ae0f 2020-03-25 stsp free(remote_target);
1620 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1621 f298ae0f 2020-03-25 stsp error = NULL;
1622 f298ae0f 2020-03-25 stsp continue;
1623 f298ae0f 2020-03-25 stsp }
1624 f298ae0f 2020-03-25 stsp goto done;
1625 f298ae0f 2020-03-25 stsp }
1626 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1627 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1628 f298ae0f 2020-03-25 stsp free(remote_refname);
1629 f298ae0f 2020-03-25 stsp free(remote_target);
1630 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1631 39c64a6a 2020-03-18 stsp if (error)
1632 d9b4d0c0 2020-03-18 stsp goto done;
1633 4ba14133 2020-03-20 stsp }
1634 4ba14133 2020-03-20 stsp if (pe == NULL) {
1635 4ba14133 2020-03-20 stsp /*
1636 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1637 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1638 4ba14133 2020-03-20 stsp * which could be fetched instead.
1639 4ba14133 2020-03-20 stsp */
1640 62d463ca 2020-10-20 naddy TAILQ_FOREACH(pe, &wanted_branches, entry) {
1641 4ba14133 2020-03-20 stsp const char *target = pe->path;
1642 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1643 d9b4d0c0 2020-03-18 stsp
1644 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1645 4ba14133 2020-03-20 stsp if (error) {
1646 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1647 4ba14133 2020-03-20 stsp error = NULL;
1648 4ba14133 2020-03-20 stsp continue;
1649 4ba14133 2020-03-20 stsp }
1650 4ba14133 2020-03-20 stsp goto done;
1651 4ba14133 2020-03-20 stsp }
1652 4ba14133 2020-03-20 stsp
1653 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1654 04d9a9ec 2020-09-24 stsp verbosity, repo);
1655 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1656 4ba14133 2020-03-20 stsp if (error)
1657 4ba14133 2020-03-20 stsp goto done;
1658 4ba14133 2020-03-20 stsp break;
1659 4ba14133 2020-03-20 stsp }
1660 659e7fbd 2020-03-20 stsp }
1661 659e7fbd 2020-03-20 stsp
1662 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1663 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1664 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1665 09838ffc 2020-03-18 stsp done:
1666 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1667 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1668 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1669 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1670 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1671 9c52365f 2020-03-21 stsp }
1672 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1673 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1674 bb64b798 2020-03-18 stsp if (repo)
1675 bb64b798 2020-03-18 stsp got_repo_close(repo);
1676 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1677 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1678 d9b4d0c0 2020-03-18 stsp free(pe->data);
1679 d9b4d0c0 2020-03-18 stsp }
1680 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1681 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1682 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1683 d9b4d0c0 2020-03-18 stsp free(pe->data);
1684 d9b4d0c0 2020-03-18 stsp }
1685 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1686 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1687 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1688 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1689 09838ffc 2020-03-18 stsp free(proto);
1690 09838ffc 2020-03-18 stsp free(host);
1691 09838ffc 2020-03-18 stsp free(port);
1692 09838ffc 2020-03-18 stsp free(server_path);
1693 09838ffc 2020-03-18 stsp free(repo_name);
1694 bb64b798 2020-03-18 stsp free(default_destdir);
1695 b46f3e71 2020-03-18 stsp free(git_url);
1696 39c64a6a 2020-03-18 stsp return error;
1697 7848a0e1 2020-03-19 stsp }
1698 7848a0e1 2020-03-19 stsp
1699 7848a0e1 2020-03-19 stsp static const struct got_error *
1700 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1701 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1702 7848a0e1 2020-03-19 stsp {
1703 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1704 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1705 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1706 7848a0e1 2020-03-19 stsp
1707 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1708 7848a0e1 2020-03-19 stsp if (err)
1709 7848a0e1 2020-03-19 stsp goto done;
1710 7848a0e1 2020-03-19 stsp
1711 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1712 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1713 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1714 88609724 2020-03-21 stsp if (err)
1715 88609724 2020-03-21 stsp goto done;
1716 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1717 88609724 2020-03-21 stsp goto done;
1718 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1719 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1720 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1721 db6d8ad8 2020-03-21 stsp }
1722 db6d8ad8 2020-03-21 stsp goto done;
1723 db6d8ad8 2020-03-21 stsp }
1724 db6d8ad8 2020-03-21 stsp
1725 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1726 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1727 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1728 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1729 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1730 688f11b3 2020-03-21 stsp }
1731 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1732 7848a0e1 2020-03-19 stsp if (err)
1733 7848a0e1 2020-03-19 stsp goto done;
1734 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1735 e8a967e0 2020-03-21 stsp if (err)
1736 e8a967e0 2020-03-21 stsp goto done;
1737 7848a0e1 2020-03-19 stsp } else {
1738 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1739 7848a0e1 2020-03-19 stsp if (err)
1740 7848a0e1 2020-03-19 stsp goto done;
1741 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1742 6338a6a1 2020-03-21 stsp goto done;
1743 6338a6a1 2020-03-21 stsp
1744 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1745 6338a6a1 2020-03-21 stsp if (err)
1746 6338a6a1 2020-03-21 stsp goto done;
1747 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1748 6338a6a1 2020-03-21 stsp if (err)
1749 6338a6a1 2020-03-21 stsp goto done;
1750 7848a0e1 2020-03-19 stsp }
1751 6338a6a1 2020-03-21 stsp
1752 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1753 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1754 6338a6a1 2020-03-21 stsp new_id_str);
1755 7848a0e1 2020-03-19 stsp done:
1756 7848a0e1 2020-03-19 stsp free(old_id);
1757 7848a0e1 2020-03-19 stsp free(new_id_str);
1758 7848a0e1 2020-03-19 stsp return err;
1759 2ab43947 2020-03-18 stsp }
1760 f1bcca34 2020-03-25 stsp
1761 f1bcca34 2020-03-25 stsp static const struct got_error *
1762 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1763 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1764 f1bcca34 2020-03-25 stsp {
1765 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1766 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1767 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1768 f1bcca34 2020-03-25 stsp
1769 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1770 bcf34b0e 2020-03-26 stsp if (err) {
1771 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1772 bcf34b0e 2020-03-26 stsp return err;
1773 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1774 bcf34b0e 2020-03-26 stsp if (err)
1775 bcf34b0e 2020-03-26 stsp goto done;
1776 2ab43947 2020-03-18 stsp
1777 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1778 bcf34b0e 2020-03-26 stsp if (err)
1779 bcf34b0e 2020-03-26 stsp goto done;
1780 f1bcca34 2020-03-25 stsp
1781 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1782 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1783 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1784 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1785 bcf34b0e 2020-03-26 stsp } else {
1786 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1787 f1bcca34 2020-03-25 stsp
1788 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1789 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1790 bcf34b0e 2020-03-26 stsp goto done;
1791 bcf34b0e 2020-03-26 stsp
1792 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1793 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1794 bcf34b0e 2020-03-26 stsp if (err)
1795 bcf34b0e 2020-03-26 stsp goto done;
1796 bcf34b0e 2020-03-26 stsp
1797 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1798 bcf34b0e 2020-03-26 stsp if (err)
1799 bcf34b0e 2020-03-26 stsp goto done;
1800 bcf34b0e 2020-03-26 stsp
1801 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1802 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1803 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1804 bcf34b0e 2020-03-26 stsp
1805 bcf34b0e 2020-03-26 stsp }
1806 f1bcca34 2020-03-25 stsp done:
1807 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1808 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1809 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1810 bcf34b0e 2020-03-26 stsp err = unlock_err;
1811 bcf34b0e 2020-03-26 stsp }
1812 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1813 f1bcca34 2020-03-25 stsp return err;
1814 f1bcca34 2020-03-25 stsp }
1815 f1bcca34 2020-03-25 stsp
1816 2ab43947 2020-03-18 stsp __dead static void
1817 7848a0e1 2020-03-19 stsp usage_fetch(void)
1818 7848a0e1 2020-03-19 stsp {
1819 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1820 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1821 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1822 13f12b09 2020-03-21 stsp getprogname());
1823 7848a0e1 2020-03-19 stsp exit(1);
1824 7848a0e1 2020-03-19 stsp }
1825 7848a0e1 2020-03-19 stsp
1826 7848a0e1 2020-03-19 stsp static const struct got_error *
1827 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1828 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1829 f21ec2f0 2020-03-21 stsp {
1830 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1831 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1832 3789fd73 2020-03-26 stsp char *id_str = NULL;
1833 3789fd73 2020-03-26 stsp
1834 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1835 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1836 3789fd73 2020-03-26 stsp if (err)
1837 3789fd73 2020-03-26 stsp return err;
1838 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1839 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1840 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1841 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1842 3789fd73 2020-03-26 stsp }
1843 3789fd73 2020-03-26 stsp } else {
1844 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1845 3789fd73 2020-03-26 stsp if (err)
1846 3789fd73 2020-03-26 stsp return err;
1847 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1848 3789fd73 2020-03-26 stsp if (err)
1849 3789fd73 2020-03-26 stsp goto done;
1850 3168e5da 2020-09-10 stsp
1851 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1852 3789fd73 2020-03-26 stsp if (err)
1853 3789fd73 2020-03-26 stsp goto done;
1854 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1855 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1856 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1857 3789fd73 2020-03-26 stsp }
1858 3789fd73 2020-03-26 stsp }
1859 3789fd73 2020-03-26 stsp done:
1860 3789fd73 2020-03-26 stsp free(id);
1861 3789fd73 2020-03-26 stsp free(id_str);
1862 3789fd73 2020-03-26 stsp return NULL;
1863 3789fd73 2020-03-26 stsp }
1864 3789fd73 2020-03-26 stsp
1865 3789fd73 2020-03-26 stsp static const struct got_error *
1866 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1867 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1868 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1869 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1870 3789fd73 2020-03-26 stsp {
1871 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1872 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1873 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1874 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1875 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1876 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1877 f21ec2f0 2020-03-21 stsp
1878 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1879 f21ec2f0 2020-03-21 stsp
1880 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1881 3789fd73 2020-03-26 stsp == -1)
1882 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1883 3789fd73 2020-03-26 stsp
1884 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1885 f21ec2f0 2020-03-21 stsp if (err)
1886 3789fd73 2020-03-26 stsp goto done;
1887 f21ec2f0 2020-03-21 stsp
1888 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1889 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1890 f21ec2f0 2020-03-21 stsp
1891 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1892 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1893 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1894 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1895 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1896 3789fd73 2020-03-26 stsp continue;
1897 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1898 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1899 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1900 3789fd73 2020-03-26 stsp goto done;
1901 3789fd73 2020-03-26 stsp }
1902 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1903 3789fd73 2020-03-26 stsp continue;
1904 3789fd73 2020-03-26 stsp }
1905 f21ec2f0 2020-03-21 stsp
1906 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1907 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1908 f21ec2f0 2020-03-21 stsp break;
1909 f21ec2f0 2020-03-21 stsp }
1910 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1911 f21ec2f0 2020-03-21 stsp continue;
1912 f21ec2f0 2020-03-21 stsp
1913 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1914 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1915 3789fd73 2020-03-26 stsp break;
1916 3789fd73 2020-03-26 stsp }
1917 3789fd73 2020-03-26 stsp if (pe != NULL)
1918 3789fd73 2020-03-26 stsp continue;
1919 f21ec2f0 2020-03-21 stsp
1920 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1921 f21ec2f0 2020-03-21 stsp if (err)
1922 f21ec2f0 2020-03-21 stsp break;
1923 3789fd73 2020-03-26 stsp
1924 3789fd73 2020-03-26 stsp if (local_refname) {
1925 3789fd73 2020-03-26 stsp struct got_reference *ref;
1926 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1927 3789fd73 2020-03-26 stsp if (err) {
1928 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1929 3789fd73 2020-03-26 stsp break;
1930 3789fd73 2020-03-26 stsp free(local_refname);
1931 3789fd73 2020-03-26 stsp local_refname = NULL;
1932 3789fd73 2020-03-26 stsp continue;
1933 3789fd73 2020-03-26 stsp }
1934 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1935 3789fd73 2020-03-26 stsp if (err)
1936 3789fd73 2020-03-26 stsp break;
1937 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1938 3789fd73 2020-03-26 stsp got_ref_close(ref);
1939 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1940 3789fd73 2020-03-26 stsp err = unlock_err;
1941 3789fd73 2020-03-26 stsp break;
1942 3789fd73 2020-03-26 stsp }
1943 3789fd73 2020-03-26 stsp
1944 3789fd73 2020-03-26 stsp free(local_refname);
1945 3789fd73 2020-03-26 stsp local_refname = NULL;
1946 6338a6a1 2020-03-21 stsp }
1947 f21ec2f0 2020-03-21 stsp }
1948 3789fd73 2020-03-26 stsp done:
1949 3789fd73 2020-03-26 stsp free(remote_namespace);
1950 3789fd73 2020-03-26 stsp free(local_refname);
1951 0e4002ca 2020-03-21 stsp return err;
1952 0e4002ca 2020-03-21 stsp }
1953 0e4002ca 2020-03-21 stsp
1954 0e4002ca 2020-03-21 stsp static const struct got_error *
1955 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1956 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1957 0e4002ca 2020-03-21 stsp {
1958 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1959 0e4002ca 2020-03-21 stsp char *remote_refname;
1960 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1961 0e4002ca 2020-03-21 stsp
1962 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1963 0e4002ca 2020-03-21 stsp refname += 5;
1964 0e4002ca 2020-03-21 stsp
1965 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1966 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1967 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1968 f21ec2f0 2020-03-21 stsp
1969 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1970 0e4002ca 2020-03-21 stsp if (err) {
1971 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1972 0e4002ca 2020-03-21 stsp goto done;
1973 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1974 0e4002ca 2020-03-21 stsp } else {
1975 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1976 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1977 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1978 9f142382 2020-03-21 stsp err = unlock_err;
1979 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1980 0e4002ca 2020-03-21 stsp }
1981 0e4002ca 2020-03-21 stsp done:
1982 0e4002ca 2020-03-21 stsp free(remote_refname);
1983 f21ec2f0 2020-03-21 stsp return err;
1984 f21ec2f0 2020-03-21 stsp }
1985 f21ec2f0 2020-03-21 stsp
1986 f21ec2f0 2020-03-21 stsp static const struct got_error *
1987 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1988 7848a0e1 2020-03-19 stsp {
1989 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1990 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1991 7848a0e1 2020-03-19 stsp const char *remote_name;
1992 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1993 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1994 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
1995 7848a0e1 2020-03-19 stsp int nremotes;
1996 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1997 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1998 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1999 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2000 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2001 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
2002 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
2003 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
2004 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
2005 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
2006 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2007 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
2008 7848a0e1 2020-03-19 stsp
2009 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
2010 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
2011 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
2012 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
2013 7848a0e1 2020-03-19 stsp
2014 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
2015 7848a0e1 2020-03-19 stsp switch (ch) {
2016 659e7fbd 2020-03-20 stsp case 'a':
2017 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
2018 4ba14133 2020-03-20 stsp break;
2019 4ba14133 2020-03-20 stsp case 'b':
2020 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
2021 4ba14133 2020-03-20 stsp optarg, NULL);
2022 4ba14133 2020-03-20 stsp if (error)
2023 4ba14133 2020-03-20 stsp return error;
2024 41b0de12 2020-03-21 stsp break;
2025 f21ec2f0 2020-03-21 stsp case 'd':
2026 f21ec2f0 2020-03-21 stsp delete_refs = 1;
2027 f21ec2f0 2020-03-21 stsp break;
2028 41b0de12 2020-03-21 stsp case 'l':
2029 41b0de12 2020-03-21 stsp list_refs_only = 1;
2030 659e7fbd 2020-03-20 stsp break;
2031 7848a0e1 2020-03-19 stsp case 'r':
2032 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
2033 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2034 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
2035 7848a0e1 2020-03-19 stsp optarg);
2036 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
2037 7848a0e1 2020-03-19 stsp break;
2038 db6d8ad8 2020-03-21 stsp case 't':
2039 db6d8ad8 2020-03-21 stsp replace_tags = 1;
2040 db6d8ad8 2020-03-21 stsp break;
2041 7848a0e1 2020-03-19 stsp case 'v':
2042 7848a0e1 2020-03-19 stsp if (verbosity < 0)
2043 7848a0e1 2020-03-19 stsp verbosity = 0;
2044 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
2045 7848a0e1 2020-03-19 stsp verbosity++;
2046 7848a0e1 2020-03-19 stsp break;
2047 7848a0e1 2020-03-19 stsp case 'q':
2048 7848a0e1 2020-03-19 stsp verbosity = -1;
2049 7848a0e1 2020-03-19 stsp break;
2050 0e4002ca 2020-03-21 stsp case 'R':
2051 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
2052 0e4002ca 2020-03-21 stsp optarg, NULL);
2053 0e4002ca 2020-03-21 stsp if (error)
2054 0e4002ca 2020-03-21 stsp return error;
2055 0e4002ca 2020-03-21 stsp break;
2056 7848a0e1 2020-03-19 stsp default:
2057 7848a0e1 2020-03-19 stsp usage_fetch();
2058 7848a0e1 2020-03-19 stsp break;
2059 7848a0e1 2020-03-19 stsp }
2060 7848a0e1 2020-03-19 stsp }
2061 7848a0e1 2020-03-19 stsp argc -= optind;
2062 7848a0e1 2020-03-19 stsp argv += optind;
2063 7848a0e1 2020-03-19 stsp
2064 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2065 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
2066 41b0de12 2020-03-21 stsp if (list_refs_only) {
2067 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2068 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
2069 41b0de12 2020-03-21 stsp if (fetch_all_branches)
2070 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
2071 f21ec2f0 2020-03-21 stsp if (delete_refs)
2072 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
2073 41b0de12 2020-03-21 stsp if (verbosity == -1)
2074 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
2075 41b0de12 2020-03-21 stsp }
2076 4ba14133 2020-03-20 stsp
2077 7848a0e1 2020-03-19 stsp if (argc == 0)
2078 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2079 7848a0e1 2020-03-19 stsp else if (argc == 1)
2080 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2081 7848a0e1 2020-03-19 stsp else
2082 7848a0e1 2020-03-19 stsp usage_fetch();
2083 7848a0e1 2020-03-19 stsp
2084 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2085 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2086 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2087 7848a0e1 2020-03-19 stsp goto done;
2088 7848a0e1 2020-03-19 stsp }
2089 7848a0e1 2020-03-19 stsp
2090 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2091 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2092 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2093 7848a0e1 2020-03-19 stsp goto done;
2094 7848a0e1 2020-03-19 stsp else
2095 7848a0e1 2020-03-19 stsp error = NULL;
2096 7848a0e1 2020-03-19 stsp if (worktree) {
2097 7848a0e1 2020-03-19 stsp repo_path =
2098 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2099 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2100 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2101 7848a0e1 2020-03-19 stsp if (error)
2102 7848a0e1 2020-03-19 stsp goto done;
2103 7848a0e1 2020-03-19 stsp } else {
2104 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2105 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2106 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2107 7848a0e1 2020-03-19 stsp goto done;
2108 7848a0e1 2020-03-19 stsp }
2109 7848a0e1 2020-03-19 stsp }
2110 7848a0e1 2020-03-19 stsp }
2111 7848a0e1 2020-03-19 stsp
2112 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2113 7848a0e1 2020-03-19 stsp if (error)
2114 7848a0e1 2020-03-19 stsp goto done;
2115 7848a0e1 2020-03-19 stsp
2116 50b0790e 2020-09-11 stsp if (worktree) {
2117 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2118 50b0790e 2020-09-11 stsp if (worktree_conf) {
2119 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2120 50b0790e 2020-09-11 stsp worktree_conf);
2121 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2122 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2123 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2124 50b0790e 2020-09-11 stsp break;
2125 54eb00d5 2020-10-20 stsp }
2126 50b0790e 2020-09-11 stsp }
2127 50b0790e 2020-09-11 stsp }
2128 7848a0e1 2020-03-19 stsp }
2129 50b0790e 2020-09-11 stsp if (remote == NULL) {
2130 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2131 50b0790e 2020-09-11 stsp if (repo_conf) {
2132 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2133 50b0790e 2020-09-11 stsp repo_conf);
2134 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2135 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2136 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2137 50b0790e 2020-09-11 stsp break;
2138 54eb00d5 2020-10-20 stsp }
2139 50b0790e 2020-09-11 stsp }
2140 50b0790e 2020-09-11 stsp }
2141 50b0790e 2020-09-11 stsp }
2142 50b0790e 2020-09-11 stsp if (remote == NULL) {
2143 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2144 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2145 54eb00d5 2020-10-20 stsp if (strcmp(remotes[i].name, remote_name) == 0) {
2146 54eb00d5 2020-10-20 stsp remote = &remotes[i];
2147 257add31 2020-09-09 stsp break;
2148 54eb00d5 2020-10-20 stsp }
2149 257add31 2020-09-09 stsp }
2150 7848a0e1 2020-03-19 stsp }
2151 50b0790e 2020-09-11 stsp if (remote == NULL) {
2152 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2153 50b0790e 2020-09-11 stsp goto done;
2154 b8adfa55 2020-09-25 stsp }
2155 b8adfa55 2020-09-25 stsp
2156 b8adfa55 2020-09-25 stsp if (TAILQ_EMPTY(&wanted_branches) && remote->nbranches > 0) {
2157 b8adfa55 2020-09-25 stsp for (i = 0; i < remote->nbranches; i++) {
2158 b8adfa55 2020-09-25 stsp got_pathlist_append(&wanted_branches,
2159 b8adfa55 2020-09-25 stsp remote->branches[i], NULL);
2160 b8adfa55 2020-09-25 stsp }
2161 b8adfa55 2020-09-25 stsp
2162 50b0790e 2020-09-11 stsp }
2163 7848a0e1 2020-03-19 stsp
2164 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2165 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2166 7848a0e1 2020-03-19 stsp if (error)
2167 7848a0e1 2020-03-19 stsp goto done;
2168 7848a0e1 2020-03-19 stsp
2169 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2170 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2171 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2172 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2173 7848a0e1 2020-03-19 stsp err(1, "pledge");
2174 7848a0e1 2020-03-19 stsp #endif
2175 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2176 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2177 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2178 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2179 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2180 7848a0e1 2020-03-19 stsp err(1, "pledge");
2181 7848a0e1 2020-03-19 stsp #endif
2182 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2183 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2184 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2185 7848a0e1 2020-03-19 stsp goto done;
2186 7848a0e1 2020-03-19 stsp } else {
2187 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2188 7848a0e1 2020-03-19 stsp goto done;
2189 7848a0e1 2020-03-19 stsp }
2190 7848a0e1 2020-03-19 stsp
2191 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2192 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2193 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2194 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2195 7848a0e1 2020-03-19 stsp goto done;
2196 7848a0e1 2020-03-19 stsp }
2197 7848a0e1 2020-03-19 stsp }
2198 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2199 7848a0e1 2020-03-19 stsp if (error)
2200 7848a0e1 2020-03-19 stsp goto done;
2201 f79e6490 2020-04-19 stsp
2202 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2203 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2204 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2205 7848a0e1 2020-03-19 stsp
2206 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2207 9c52365f 2020-03-21 stsp server_path, verbosity);
2208 7848a0e1 2020-03-19 stsp if (error)
2209 7848a0e1 2020-03-19 stsp goto done;
2210 7848a0e1 2020-03-19 stsp
2211 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2212 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2213 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2214 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2215 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2216 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2217 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2218 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2219 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2220 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2221 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2222 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2223 7848a0e1 2020-03-19 stsp if (error)
2224 7848a0e1 2020-03-19 stsp goto done;
2225 7848a0e1 2020-03-19 stsp
2226 41b0de12 2020-03-21 stsp if (list_refs_only) {
2227 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2228 41b0de12 2020-03-21 stsp goto done;
2229 41b0de12 2020-03-21 stsp }
2230 41b0de12 2020-03-21 stsp
2231 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2232 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2233 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2234 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2235 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2236 984065c8 2020-03-19 stsp if (error)
2237 984065c8 2020-03-19 stsp goto done;
2238 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2239 984065c8 2020-03-19 stsp free(id_str);
2240 984065c8 2020-03-19 stsp id_str = NULL;
2241 984065c8 2020-03-19 stsp }
2242 7848a0e1 2020-03-19 stsp
2243 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2244 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2245 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2246 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2247 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2248 7848a0e1 2020-03-19 stsp char *remote_refname;
2249 7848a0e1 2020-03-19 stsp
2250 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2251 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2252 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2253 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2254 0e4002ca 2020-03-21 stsp if (error)
2255 0e4002ca 2020-03-21 stsp goto done;
2256 0e4002ca 2020-03-21 stsp continue;
2257 0e4002ca 2020-03-21 stsp }
2258 0e4002ca 2020-03-21 stsp
2259 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2260 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2261 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2262 7848a0e1 2020-03-19 stsp if (error) {
2263 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2264 7848a0e1 2020-03-19 stsp goto done;
2265 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2266 6338a6a1 2020-03-21 stsp repo);
2267 7848a0e1 2020-03-19 stsp if (error)
2268 7848a0e1 2020-03-19 stsp goto done;
2269 7848a0e1 2020-03-19 stsp } else {
2270 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2271 db6d8ad8 2020-03-21 stsp verbosity, repo);
2272 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2273 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2274 9f142382 2020-03-21 stsp error = unlock_err;
2275 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2276 7848a0e1 2020-03-19 stsp if (error)
2277 7848a0e1 2020-03-19 stsp goto done;
2278 7848a0e1 2020-03-19 stsp }
2279 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2280 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2281 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2282 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2283 7848a0e1 2020-03-19 stsp goto done;
2284 7848a0e1 2020-03-19 stsp }
2285 7848a0e1 2020-03-19 stsp
2286 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2287 7848a0e1 2020-03-19 stsp if (error) {
2288 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2289 7848a0e1 2020-03-19 stsp goto done;
2290 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2291 688f11b3 2020-03-21 stsp verbosity, repo);
2292 7848a0e1 2020-03-19 stsp if (error)
2293 7848a0e1 2020-03-19 stsp goto done;
2294 7848a0e1 2020-03-19 stsp } else {
2295 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2296 db6d8ad8 2020-03-21 stsp verbosity, repo);
2297 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2298 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2299 9f142382 2020-03-21 stsp error = unlock_err;
2300 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2301 7848a0e1 2020-03-19 stsp if (error)
2302 7848a0e1 2020-03-19 stsp goto done;
2303 7848a0e1 2020-03-19 stsp }
2304 2ec30c80 2020-03-20 stsp
2305 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2306 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2307 2ec30c80 2020-03-20 stsp if (error) {
2308 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2309 2ec30c80 2020-03-20 stsp goto done;
2310 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2311 6338a6a1 2020-03-21 stsp repo);
2312 2ec30c80 2020-03-20 stsp if (error)
2313 2ec30c80 2020-03-20 stsp goto done;
2314 9f142382 2020-03-21 stsp } else {
2315 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2316 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2317 9f142382 2020-03-21 stsp error = unlock_err;
2318 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2319 9f142382 2020-03-21 stsp }
2320 7848a0e1 2020-03-19 stsp }
2321 7848a0e1 2020-03-19 stsp }
2322 f1bcca34 2020-03-25 stsp if (delete_refs) {
2323 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2324 3789fd73 2020-03-26 stsp verbosity, repo);
2325 f1bcca34 2020-03-25 stsp if (error)
2326 f1bcca34 2020-03-25 stsp goto done;
2327 f1bcca34 2020-03-25 stsp }
2328 f1bcca34 2020-03-25 stsp
2329 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2330 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2331 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2332 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2333 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2334 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2335 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2336 f1bcca34 2020-03-25 stsp
2337 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2338 f1bcca34 2020-03-25 stsp continue;
2339 f1bcca34 2020-03-25 stsp
2340 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2341 f1bcca34 2020-03-25 stsp continue;
2342 f1bcca34 2020-03-25 stsp
2343 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2344 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2345 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2346 f1bcca34 2020-03-25 stsp goto done;
2347 f1bcca34 2020-03-25 stsp }
2348 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2349 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2350 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2351 f1bcca34 2020-03-25 stsp free(remote_refname);
2352 f1bcca34 2020-03-25 stsp goto done;
2353 f1bcca34 2020-03-25 stsp }
2354 f1bcca34 2020-03-25 stsp
2355 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2356 f1bcca34 2020-03-25 stsp 0);
2357 f1bcca34 2020-03-25 stsp if (error) {
2358 f1bcca34 2020-03-25 stsp free(remote_refname);
2359 f1bcca34 2020-03-25 stsp free(remote_target);
2360 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2361 f1bcca34 2020-03-25 stsp error = NULL;
2362 f1bcca34 2020-03-25 stsp continue;
2363 f1bcca34 2020-03-25 stsp }
2364 f1bcca34 2020-03-25 stsp goto done;
2365 f1bcca34 2020-03-25 stsp }
2366 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2367 f1bcca34 2020-03-25 stsp verbosity, repo);
2368 f1bcca34 2020-03-25 stsp free(remote_refname);
2369 f1bcca34 2020-03-25 stsp free(remote_target);
2370 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2371 f1bcca34 2020-03-25 stsp if (error)
2372 f1bcca34 2020-03-25 stsp goto done;
2373 f1bcca34 2020-03-25 stsp }
2374 f1bcca34 2020-03-25 stsp }
2375 7848a0e1 2020-03-19 stsp done:
2376 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2377 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2378 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2379 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2380 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2381 9c52365f 2020-03-21 stsp }
2382 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2383 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2384 7848a0e1 2020-03-19 stsp if (repo)
2385 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2386 7848a0e1 2020-03-19 stsp if (worktree)
2387 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2388 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2389 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2390 7848a0e1 2020-03-19 stsp free(pe->data);
2391 7848a0e1 2020-03-19 stsp }
2392 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2393 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2394 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2395 7848a0e1 2020-03-19 stsp free(pe->data);
2396 7848a0e1 2020-03-19 stsp }
2397 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2398 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2399 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2400 7848a0e1 2020-03-19 stsp free(id_str);
2401 7848a0e1 2020-03-19 stsp free(cwd);
2402 7848a0e1 2020-03-19 stsp free(repo_path);
2403 7848a0e1 2020-03-19 stsp free(pack_hash);
2404 7848a0e1 2020-03-19 stsp free(proto);
2405 7848a0e1 2020-03-19 stsp free(host);
2406 7848a0e1 2020-03-19 stsp free(port);
2407 7848a0e1 2020-03-19 stsp free(server_path);
2408 7848a0e1 2020-03-19 stsp free(repo_name);
2409 7848a0e1 2020-03-19 stsp return error;
2410 7848a0e1 2020-03-19 stsp }
2411 7848a0e1 2020-03-19 stsp
2412 7848a0e1 2020-03-19 stsp
2413 7848a0e1 2020-03-19 stsp __dead static void
2414 2ab43947 2020-03-18 stsp usage_checkout(void)
2415 2ab43947 2020-03-18 stsp {
2416 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2417 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2418 2ab43947 2020-03-18 stsp exit(1);
2419 2ab43947 2020-03-18 stsp }
2420 2ab43947 2020-03-18 stsp
2421 2ab43947 2020-03-18 stsp static void
2422 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2423 2ab43947 2020-03-18 stsp {
2424 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2425 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2426 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2427 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2428 2ab43947 2020-03-18 stsp getprogname());
2429 2ab43947 2020-03-18 stsp }
2430 2ab43947 2020-03-18 stsp
2431 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2432 2ab43947 2020-03-18 stsp const char *worktree_path;
2433 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2434 2ab43947 2020-03-18 stsp };
2435 2ab43947 2020-03-18 stsp
2436 2ab43947 2020-03-18 stsp static const struct got_error *
2437 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2438 2ab43947 2020-03-18 stsp {
2439 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2440 2ab43947 2020-03-18 stsp
2441 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2442 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2443 2ab43947 2020-03-18 stsp return NULL;
2444 2ab43947 2020-03-18 stsp
2445 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2446 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2447 2ab43947 2020-03-18 stsp return NULL;
2448 2ab43947 2020-03-18 stsp }
2449 2ab43947 2020-03-18 stsp
2450 2ab43947 2020-03-18 stsp while (path[0] == '/')
2451 2ab43947 2020-03-18 stsp path++;
2452 2ab43947 2020-03-18 stsp
2453 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2454 2ab43947 2020-03-18 stsp return NULL;
2455 2ab43947 2020-03-18 stsp }
2456 2ab43947 2020-03-18 stsp
2457 2ab43947 2020-03-18 stsp static const struct got_error *
2458 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2459 2ab43947 2020-03-18 stsp {
2460 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2461 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2462 2ab43947 2020-03-18 stsp return NULL;
2463 2ab43947 2020-03-18 stsp }
2464 2ab43947 2020-03-18 stsp
2465 2ab43947 2020-03-18 stsp static const struct got_error *
2466 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2467 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2468 2ab43947 2020-03-18 stsp struct got_repository *repo)
2469 2ab43947 2020-03-18 stsp {
2470 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2471 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2472 2ab43947 2020-03-18 stsp
2473 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2474 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2475 2ab43947 2020-03-18 stsp if (err)
2476 2ab43947 2020-03-18 stsp return err;
2477 2ab43947 2020-03-18 stsp
2478 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2479 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2480 2ab43947 2020-03-18 stsp
2481 2ab43947 2020-03-18 stsp /*
2482 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2483 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2484 2ab43947 2020-03-18 stsp *
2485 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2486 2ab43947 2020-03-18 stsp *
2487 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2488 2ab43947 2020-03-18 stsp * \ /
2489 2ab43947 2020-03-18 stsp * C E
2490 2ab43947 2020-03-18 stsp * \ /
2491 2ab43947 2020-03-18 stsp * B (yca)
2492 2ab43947 2020-03-18 stsp * |
2493 2ab43947 2020-03-18 stsp * A
2494 2ab43947 2020-03-18 stsp *
2495 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2496 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2497 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2498 2ab43947 2020-03-18 stsp */
2499 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2500 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2501 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2502 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2503 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2504 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2505 2ab43947 2020-03-18 stsp
2506 2ab43947 2020-03-18 stsp free(yca_id);
2507 2ab43947 2020-03-18 stsp return NULL;
2508 2ab43947 2020-03-18 stsp }
2509 2ab43947 2020-03-18 stsp
2510 2ab43947 2020-03-18 stsp static const struct got_error *
2511 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2512 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2513 2ab43947 2020-03-18 stsp struct got_repository *repo)
2514 2ab43947 2020-03-18 stsp {
2515 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2516 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2517 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2518 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2519 2ab43947 2020-03-18 stsp
2520 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2521 2ab43947 2020-03-18 stsp if (err)
2522 2ab43947 2020-03-18 stsp goto done;
2523 2ab43947 2020-03-18 stsp
2524 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2525 2ab43947 2020-03-18 stsp is_same_branch = 1;
2526 2ab43947 2020-03-18 stsp goto done;
2527 2ab43947 2020-03-18 stsp }
2528 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2529 2ab43947 2020-03-18 stsp is_same_branch = 1;
2530 2ab43947 2020-03-18 stsp goto done;
2531 2ab43947 2020-03-18 stsp }
2532 2ab43947 2020-03-18 stsp
2533 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2534 2ab43947 2020-03-18 stsp if (err)
2535 2ab43947 2020-03-18 stsp goto done;
2536 2ab43947 2020-03-18 stsp
2537 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2538 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2539 2ab43947 2020-03-18 stsp if (err)
2540 2ab43947 2020-03-18 stsp goto done;
2541 2ab43947 2020-03-18 stsp
2542 2ab43947 2020-03-18 stsp for (;;) {
2543 2ab43947 2020-03-18 stsp struct got_object_id *id;
2544 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2545 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2546 2ab43947 2020-03-18 stsp if (err) {
2547 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2548 2ab43947 2020-03-18 stsp err = NULL;
2549 2ab43947 2020-03-18 stsp break;
2550 2ab43947 2020-03-18 stsp }
2551 2ab43947 2020-03-18 stsp
2552 2ab43947 2020-03-18 stsp if (id) {
2553 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2554 2ab43947 2020-03-18 stsp break;
2555 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2556 2ab43947 2020-03-18 stsp is_same_branch = 1;
2557 2ab43947 2020-03-18 stsp break;
2558 2ab43947 2020-03-18 stsp }
2559 2ab43947 2020-03-18 stsp }
2560 2ab43947 2020-03-18 stsp }
2561 2ab43947 2020-03-18 stsp done:
2562 2ab43947 2020-03-18 stsp if (graph)
2563 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2564 2ab43947 2020-03-18 stsp free(head_commit_id);
2565 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2566 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2567 2ab43947 2020-03-18 stsp return err;
2568 a367ff0f 2019-05-14 stsp }
2569 8069f636 2019-01-12 stsp
2570 4ed7e80c 2018-05-20 stsp static const struct got_error *
2571 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2572 4b6c9460 2020-03-05 stsp {
2573 4b6c9460 2020-03-05 stsp static char msg[512];
2574 4b6c9460 2020-03-05 stsp const char *branch_name;
2575 4b6c9460 2020-03-05 stsp
2576 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2577 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2578 4b6c9460 2020-03-05 stsp else
2579 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2580 4b6c9460 2020-03-05 stsp
2581 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2582 4b6c9460 2020-03-05 stsp branch_name += 11;
2583 4b6c9460 2020-03-05 stsp
2584 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2585 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2586 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2587 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2588 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2589 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2590 4b6c9460 2020-03-05 stsp
2591 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2592 4b6c9460 2020-03-05 stsp }
2593 4b6c9460 2020-03-05 stsp
2594 4b6c9460 2020-03-05 stsp static const struct got_error *
2595 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2596 c09a553d 2018-03-12 stsp {
2597 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2598 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2599 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2600 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2601 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2602 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2603 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2604 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2605 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2606 f0207f6a 2020-10-19 stsp char *cwd = NULL;
2607 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2608 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2609 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2610 f2ea84fa 2019-07-27 stsp
2611 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2612 c09a553d 2018-03-12 stsp
2613 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2614 0bb8a95e 2018-03-12 stsp switch (ch) {
2615 08573d5b 2019-05-14 stsp case 'b':
2616 08573d5b 2019-05-14 stsp branch_name = optarg;
2617 08573d5b 2019-05-14 stsp break;
2618 8069f636 2019-01-12 stsp case 'c':
2619 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2620 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2621 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2622 bb51a5b4 2020-01-13 stsp break;
2623 bb51a5b4 2020-01-13 stsp case 'E':
2624 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2625 8069f636 2019-01-12 stsp break;
2626 0bb8a95e 2018-03-12 stsp case 'p':
2627 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2628 0bb8a95e 2018-03-12 stsp break;
2629 0bb8a95e 2018-03-12 stsp default:
2630 2deda0b9 2019-03-07 stsp usage_checkout();
2631 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2632 0bb8a95e 2018-03-12 stsp }
2633 0bb8a95e 2018-03-12 stsp }
2634 0bb8a95e 2018-03-12 stsp
2635 0bb8a95e 2018-03-12 stsp argc -= optind;
2636 0bb8a95e 2018-03-12 stsp argv += optind;
2637 0bb8a95e 2018-03-12 stsp
2638 6715a751 2018-03-16 stsp #ifndef PROFILE
2639 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2640 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2641 c09a553d 2018-03-12 stsp err(1, "pledge");
2642 6715a751 2018-03-16 stsp #endif
2643 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2644 c47340da 2020-09-20 stsp char *base, *dotgit;
2645 f0207f6a 2020-10-19 stsp const char *path;
2646 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2647 76089277 2018-04-01 stsp if (repo_path == NULL)
2648 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2649 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2650 76089277 2018-04-01 stsp if (cwd == NULL) {
2651 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2652 76089277 2018-04-01 stsp goto done;
2653 76089277 2018-04-01 stsp }
2654 c47340da 2020-09-20 stsp if (path_prefix[0])
2655 f0207f6a 2020-10-19 stsp path = path_prefix;
2656 c47340da 2020-09-20 stsp else
2657 f0207f6a 2020-10-19 stsp path = repo_path;
2658 f0207f6a 2020-10-19 stsp error = got_path_basename(&base, path);
2659 f0207f6a 2020-10-19 stsp if (error)
2660 c47340da 2020-09-20 stsp goto done;
2661 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2662 c09a553d 2018-03-12 stsp if (dotgit)
2663 c09a553d 2018-03-12 stsp *dotgit = '\0';
2664 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2665 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2666 f0207f6a 2020-10-19 stsp free(base);
2667 76089277 2018-04-01 stsp goto done;
2668 c09a553d 2018-03-12 stsp }
2669 f0207f6a 2020-10-19 stsp free(base);
2670 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2671 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2672 76089277 2018-04-01 stsp if (repo_path == NULL) {
2673 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2674 76089277 2018-04-01 stsp goto done;
2675 76089277 2018-04-01 stsp }
2676 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2677 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2678 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2679 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2680 b4b3a7dd 2019-07-22 stsp argv[1]);
2681 b4b3a7dd 2019-07-22 stsp goto done;
2682 b4b3a7dd 2019-07-22 stsp }
2683 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2684 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2685 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2686 b4b3a7dd 2019-07-22 stsp goto done;
2687 b4b3a7dd 2019-07-22 stsp }
2688 76089277 2018-04-01 stsp }
2689 c09a553d 2018-03-12 stsp } else
2690 c09a553d 2018-03-12 stsp usage_checkout();
2691 c09a553d 2018-03-12 stsp
2692 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2693 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2694 13bfb272 2019-05-10 jcs
2695 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2696 c09a553d 2018-03-12 stsp if (error != NULL)
2697 c02c541e 2019-03-29 stsp goto done;
2698 c02c541e 2019-03-29 stsp
2699 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2700 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2701 c530dc23 2019-07-23 stsp if (error) {
2702 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2703 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2704 c530dc23 2019-07-23 stsp goto done;
2705 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2706 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2707 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2708 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2709 c530dc23 2019-07-23 stsp goto done;
2710 c530dc23 2019-07-23 stsp }
2711 c530dc23 2019-07-23 stsp }
2712 c530dc23 2019-07-23 stsp
2713 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2714 c02c541e 2019-03-29 stsp if (error)
2715 c09a553d 2018-03-12 stsp goto done;
2716 8069f636 2019-01-12 stsp
2717 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2718 c09a553d 2018-03-12 stsp if (error != NULL)
2719 c09a553d 2018-03-12 stsp goto done;
2720 c09a553d 2018-03-12 stsp
2721 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2722 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2723 c09a553d 2018-03-12 stsp goto done;
2724 c09a553d 2018-03-12 stsp
2725 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2726 c09a553d 2018-03-12 stsp if (error != NULL)
2727 c09a553d 2018-03-12 stsp goto done;
2728 c09a553d 2018-03-12 stsp
2729 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2730 e5dc7198 2018-12-29 stsp path_prefix);
2731 e5dc7198 2018-12-29 stsp if (error != NULL)
2732 e5dc7198 2018-12-29 stsp goto done;
2733 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2734 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2735 49520a32 2018-12-29 stsp goto done;
2736 49520a32 2018-12-29 stsp }
2737 49520a32 2018-12-29 stsp
2738 8069f636 2019-01-12 stsp if (commit_id_str) {
2739 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2740 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2741 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2742 30837e32 2019-07-25 stsp if (error)
2743 8069f636 2019-01-12 stsp goto done;
2744 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2745 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2746 8069f636 2019-01-12 stsp if (error != NULL) {
2747 8069f636 2019-01-12 stsp free(commit_id);
2748 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2749 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2750 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2751 4b6c9460 2020-03-05 stsp }
2752 8069f636 2019-01-12 stsp goto done;
2753 8069f636 2019-01-12 stsp }
2754 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2755 4b6c9460 2020-03-05 stsp if (error) {
2756 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2757 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2758 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2759 4b6c9460 2020-03-05 stsp }
2760 45d344f6 2019-05-14 stsp goto done;
2761 4b6c9460 2020-03-05 stsp }
2762 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2763 8069f636 2019-01-12 stsp commit_id);
2764 8069f636 2019-01-12 stsp free(commit_id);
2765 8069f636 2019-01-12 stsp if (error)
2766 8069f636 2019-01-12 stsp goto done;
2767 8069f636 2019-01-12 stsp }
2768 8069f636 2019-01-12 stsp
2769 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2770 f2ea84fa 2019-07-27 stsp if (error)
2771 f2ea84fa 2019-07-27 stsp goto done;
2772 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2773 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2774 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2775 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2776 c09a553d 2018-03-12 stsp if (error != NULL)
2777 c09a553d 2018-03-12 stsp goto done;
2778 c09a553d 2018-03-12 stsp
2779 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2780 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2781 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2782 c09a553d 2018-03-12 stsp done:
2783 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2784 8069f636 2019-01-12 stsp free(commit_id_str);
2785 76089277 2018-04-01 stsp free(repo_path);
2786 507dc3bb 2018-12-29 stsp free(worktree_path);
2787 c47340da 2020-09-20 stsp free(cwd);
2788 507dc3bb 2018-12-29 stsp return error;
2789 9627c110 2020-04-18 stsp }
2790 9627c110 2020-04-18 stsp
2791 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2792 9627c110 2020-04-18 stsp int did_something;
2793 9627c110 2020-04-18 stsp int conflicts;
2794 9627c110 2020-04-18 stsp int obstructed;
2795 9627c110 2020-04-18 stsp int not_updated;
2796 9627c110 2020-04-18 stsp };
2797 9627c110 2020-04-18 stsp
2798 9627c110 2020-04-18 stsp void
2799 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2800 9627c110 2020-04-18 stsp {
2801 9627c110 2020-04-18 stsp if (!upa->did_something)
2802 9627c110 2020-04-18 stsp return;
2803 9627c110 2020-04-18 stsp
2804 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2805 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2806 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2807 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2808 9627c110 2020-04-18 stsp upa->obstructed);
2809 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2810 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2811 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2812 507dc3bb 2018-12-29 stsp }
2813 507dc3bb 2018-12-29 stsp
2814 507dc3bb 2018-12-29 stsp __dead static void
2815 507dc3bb 2018-12-29 stsp usage_update(void)
2816 507dc3bb 2018-12-29 stsp {
2817 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2818 507dc3bb 2018-12-29 stsp getprogname());
2819 507dc3bb 2018-12-29 stsp exit(1);
2820 507dc3bb 2018-12-29 stsp }
2821 507dc3bb 2018-12-29 stsp
2822 1ee397ad 2019-07-12 stsp static const struct got_error *
2823 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2824 507dc3bb 2018-12-29 stsp {
2825 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2826 784955db 2019-01-12 stsp
2827 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2828 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2829 1ee397ad 2019-07-12 stsp return NULL;
2830 507dc3bb 2018-12-29 stsp
2831 9627c110 2020-04-18 stsp upa->did_something = 1;
2832 a484d721 2019-06-10 stsp
2833 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2834 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2835 1ee397ad 2019-07-12 stsp return NULL;
2836 a484d721 2019-06-10 stsp
2837 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2838 9627c110 2020-04-18 stsp upa->conflicts++;
2839 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2840 9627c110 2020-04-18 stsp upa->obstructed++;
2841 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2842 9627c110 2020-04-18 stsp upa->not_updated++;
2843 9627c110 2020-04-18 stsp
2844 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2845 507dc3bb 2018-12-29 stsp path++;
2846 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2847 1ee397ad 2019-07-12 stsp return NULL;
2848 be7061eb 2018-12-30 stsp }
2849 be7061eb 2018-12-30 stsp
2850 be7061eb 2018-12-30 stsp static const struct got_error *
2851 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2852 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2853 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2854 a1fb16d8 2019-05-24 stsp {
2855 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2856 a1fb16d8 2019-05-24 stsp char *base_id_str;
2857 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2858 a1fb16d8 2019-05-24 stsp
2859 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2860 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2861 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2862 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2863 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2864 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2865 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2866 a1fb16d8 2019-05-24 stsp }
2867 a1fb16d8 2019-05-24 stsp
2868 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2869 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2870 a1fb16d8 2019-05-24 stsp if (err) {
2871 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2872 a1fb16d8 2019-05-24 stsp return err;
2873 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2874 a1fb16d8 2019-05-24 stsp }
2875 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2876 a1fb16d8 2019-05-24 stsp return NULL;
2877 a1fb16d8 2019-05-24 stsp
2878 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2879 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2880 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2881 a1fb16d8 2019-05-24 stsp if (err)
2882 a1fb16d8 2019-05-24 stsp return err;
2883 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2884 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2885 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2886 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2887 0ebf8283 2019-07-24 stsp return NULL;
2888 0ebf8283 2019-07-24 stsp }
2889 0ebf8283 2019-07-24 stsp
2890 0ebf8283 2019-07-24 stsp static const struct got_error *
2891 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2892 0ebf8283 2019-07-24 stsp {
2893 0ebf8283 2019-07-24 stsp const struct got_error *err;
2894 0ebf8283 2019-07-24 stsp int in_progress;
2895 0ebf8283 2019-07-24 stsp
2896 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2897 0ebf8283 2019-07-24 stsp if (err)
2898 0ebf8283 2019-07-24 stsp return err;
2899 0ebf8283 2019-07-24 stsp if (in_progress)
2900 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2901 0ebf8283 2019-07-24 stsp
2902 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2903 0ebf8283 2019-07-24 stsp if (err)
2904 0ebf8283 2019-07-24 stsp return err;
2905 0ebf8283 2019-07-24 stsp if (in_progress)
2906 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2907 0ebf8283 2019-07-24 stsp
2908 a1fb16d8 2019-05-24 stsp return NULL;
2909 a5edda0a 2019-07-27 stsp }
2910 a5edda0a 2019-07-27 stsp
2911 a5edda0a 2019-07-27 stsp static const struct got_error *
2912 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2913 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2914 a5edda0a 2019-07-27 stsp {
2915 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2916 a5edda0a 2019-07-27 stsp char *path;
2917 a5edda0a 2019-07-27 stsp int i;
2918 a5edda0a 2019-07-27 stsp
2919 a5edda0a 2019-07-27 stsp if (argc == 0) {
2920 a5edda0a 2019-07-27 stsp path = strdup("");
2921 a5edda0a 2019-07-27 stsp if (path == NULL)
2922 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2923 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2924 a5edda0a 2019-07-27 stsp }
2925 a5edda0a 2019-07-27 stsp
2926 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2927 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2928 a5edda0a 2019-07-27 stsp if (err)
2929 a5edda0a 2019-07-27 stsp break;
2930 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2931 a5edda0a 2019-07-27 stsp if (err) {
2932 a5edda0a 2019-07-27 stsp free(path);
2933 a5edda0a 2019-07-27 stsp break;
2934 a5edda0a 2019-07-27 stsp }
2935 a5edda0a 2019-07-27 stsp }
2936 a5edda0a 2019-07-27 stsp
2937 a5edda0a 2019-07-27 stsp return err;
2938 a1fb16d8 2019-05-24 stsp }
2939 a1fb16d8 2019-05-24 stsp
2940 a1fb16d8 2019-05-24 stsp static const struct got_error *
2941 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2942 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2943 fa51e947 2020-03-27 stsp {
2944 fa51e947 2020-03-27 stsp const struct got_error *err;
2945 fa51e947 2020-03-27 stsp struct got_repository *repo;
2946 fa51e947 2020-03-27 stsp static char msg[512];
2947 fa51e947 2020-03-27 stsp
2948 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2949 fa51e947 2020-03-27 stsp if (err)
2950 fa51e947 2020-03-27 stsp return orig_err;
2951 fa51e947 2020-03-27 stsp
2952 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2953 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2954 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2955 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2956 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2957 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2958 fa51e947 2020-03-27 stsp got_repo_close(repo);
2959 fa51e947 2020-03-27 stsp return err;
2960 fa51e947 2020-03-27 stsp }
2961 fa51e947 2020-03-27 stsp
2962 fa51e947 2020-03-27 stsp static const struct got_error *
2963 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2964 507dc3bb 2018-12-29 stsp {
2965 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2966 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2967 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2968 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2969 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2970 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2971 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2972 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2973 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2974 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2975 9627c110 2020-04-18 stsp int ch;
2976 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2977 507dc3bb 2018-12-29 stsp
2978 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2979 f2ea84fa 2019-07-27 stsp
2980 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2981 507dc3bb 2018-12-29 stsp switch (ch) {
2982 024e9686 2019-05-14 stsp case 'b':
2983 024e9686 2019-05-14 stsp branch_name = optarg;
2984 024e9686 2019-05-14 stsp break;
2985 507dc3bb 2018-12-29 stsp case 'c':
2986 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2987 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2988 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2989 507dc3bb 2018-12-29 stsp break;
2990 507dc3bb 2018-12-29 stsp default:
2991 2deda0b9 2019-03-07 stsp usage_update();
2992 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2993 507dc3bb 2018-12-29 stsp }
2994 507dc3bb 2018-12-29 stsp }
2995 507dc3bb 2018-12-29 stsp
2996 507dc3bb 2018-12-29 stsp argc -= optind;
2997 507dc3bb 2018-12-29 stsp argv += optind;
2998 507dc3bb 2018-12-29 stsp
2999 507dc3bb 2018-12-29 stsp #ifndef PROFILE
3000 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3001 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
3002 507dc3bb 2018-12-29 stsp err(1, "pledge");
3003 507dc3bb 2018-12-29 stsp #endif
3004 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
3005 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
3006 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3007 c4cdcb68 2019-04-03 stsp goto done;
3008 c4cdcb68 2019-04-03 stsp }
3009 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
3010 fa51e947 2020-03-27 stsp if (error) {
3011 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3012 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
3013 fa51e947 2020-03-27 stsp worktree_path);
3014 7d5807f4 2019-07-11 stsp goto done;
3015 fa51e947 2020-03-27 stsp }
3016 7d5807f4 2019-07-11 stsp
3017 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3018 f2ea84fa 2019-07-27 stsp if (error)
3019 f2ea84fa 2019-07-27 stsp goto done;
3020 507dc3bb 2018-12-29 stsp
3021 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3022 c9956ddf 2019-09-08 stsp NULL);
3023 507dc3bb 2018-12-29 stsp if (error != NULL)
3024 507dc3bb 2018-12-29 stsp goto done;
3025 507dc3bb 2018-12-29 stsp
3026 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3027 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3028 087fb88c 2019-08-04 stsp if (error)
3029 087fb88c 2019-08-04 stsp goto done;
3030 087fb88c 2019-08-04 stsp
3031 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3032 0266afb7 2019-01-04 stsp if (error)
3033 0266afb7 2019-01-04 stsp goto done;
3034 0266afb7 2019-01-04 stsp
3035 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3036 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
3037 024e9686 2019-05-14 stsp if (error != NULL)
3038 024e9686 2019-05-14 stsp goto done;
3039 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
3040 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3041 9c4b8182 2019-01-02 stsp if (error != NULL)
3042 9c4b8182 2019-01-02 stsp goto done;
3043 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3044 507dc3bb 2018-12-29 stsp if (error != NULL)
3045 507dc3bb 2018-12-29 stsp goto done;
3046 507dc3bb 2018-12-29 stsp } else {
3047 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3048 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3049 04f57cb3 2019-07-25 stsp free(commit_id_str);
3050 bb787f09 2019-07-25 stsp commit_id_str = NULL;
3051 30837e32 2019-07-25 stsp if (error)
3052 a297e751 2019-07-11 stsp goto done;
3053 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
3054 a297e751 2019-07-11 stsp if (error)
3055 507dc3bb 2018-12-29 stsp goto done;
3056 507dc3bb 2018-12-29 stsp }
3057 35c965b2 2018-12-29 stsp
3058 a1fb16d8 2019-05-24 stsp if (branch_name) {
3059 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
3060 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3061 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3062 f2ea84fa 2019-07-27 stsp continue;
3063 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3064 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3065 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3066 024e9686 2019-05-14 stsp goto done;
3067 024e9686 2019-05-14 stsp }
3068 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3069 024e9686 2019-05-14 stsp if (error)
3070 024e9686 2019-05-14 stsp goto done;
3071 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3072 3aef623b 2019-10-15 stsp repo);
3073 a367ff0f 2019-05-14 stsp free(head_commit_id);
3074 024e9686 2019-05-14 stsp if (error != NULL)
3075 024e9686 2019-05-14 stsp goto done;
3076 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3077 a367ff0f 2019-05-14 stsp if (error)
3078 a367ff0f 2019-05-14 stsp goto done;
3079 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3080 024e9686 2019-05-14 stsp if (error)
3081 024e9686 2019-05-14 stsp goto done;
3082 024e9686 2019-05-14 stsp } else {
3083 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3084 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3085 a367ff0f 2019-05-14 stsp if (error != NULL) {
3086 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3087 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3088 024e9686 2019-05-14 stsp goto done;
3089 a367ff0f 2019-05-14 stsp }
3090 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3091 a367ff0f 2019-05-14 stsp if (error)
3092 a367ff0f 2019-05-14 stsp goto done;
3093 024e9686 2019-05-14 stsp }
3094 507dc3bb 2018-12-29 stsp
3095 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3096 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3097 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3098 507dc3bb 2018-12-29 stsp commit_id);
3099 507dc3bb 2018-12-29 stsp if (error)
3100 507dc3bb 2018-12-29 stsp goto done;
3101 507dc3bb 2018-12-29 stsp }
3102 507dc3bb 2018-12-29 stsp
3103 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3104 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3105 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3106 507dc3bb 2018-12-29 stsp if (error != NULL)
3107 507dc3bb 2018-12-29 stsp goto done;
3108 9c4b8182 2019-01-02 stsp
3109 9627c110 2020-04-18 stsp if (upa.did_something)
3110 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3111 784955db 2019-01-12 stsp else
3112 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3113 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3114 507dc3bb 2018-12-29 stsp done:
3115 c09a553d 2018-03-12 stsp free(worktree_path);
3116 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3117 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3118 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3119 507dc3bb 2018-12-29 stsp free(commit_id);
3120 9c4b8182 2019-01-02 stsp free(commit_id_str);
3121 c09a553d 2018-03-12 stsp return error;
3122 c09a553d 2018-03-12 stsp }
3123 c09a553d 2018-03-12 stsp
3124 f42b1b34 2018-03-12 stsp static const struct got_error *
3125 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3126 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3127 63035f9f 2019-10-06 stsp struct got_repository *repo)
3128 5c860e29 2018-03-12 stsp {
3129 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3130 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3131 79109fed 2018-03-27 stsp
3132 44392932 2019-08-25 stsp if (blob_id1) {
3133 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3134 44392932 2019-08-25 stsp if (err)
3135 44392932 2019-08-25 stsp goto done;
3136 44392932 2019-08-25 stsp }
3137 79109fed 2018-03-27 stsp
3138 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3139 44392932 2019-08-25 stsp if (err)
3140 44392932 2019-08-25 stsp goto done;
3141 79109fed 2018-03-27 stsp
3142 44392932 2019-08-25 stsp while (path[0] == '/')
3143 44392932 2019-08-25 stsp path++;
3144 fe621944 2020-11-10 stsp err = got_diff_blob(NULL, NULL, blob1, blob2, path, path,
3145 fe621944 2020-11-10 stsp diff_context, ignore_whitespace, stdout);
3146 44392932 2019-08-25 stsp done:
3147 44392932 2019-08-25 stsp if (blob1)
3148 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3149 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3150 44392932 2019-08-25 stsp return err;
3151 44392932 2019-08-25 stsp }
3152 44392932 2019-08-25 stsp
3153 44392932 2019-08-25 stsp static const struct got_error *
3154 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3155 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3156 63035f9f 2019-10-06 stsp struct got_repository *repo)
3157 44392932 2019-08-25 stsp {
3158 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3159 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3160 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3161 44392932 2019-08-25 stsp
3162 44392932 2019-08-25 stsp if (tree_id1) {
3163 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3164 79109fed 2018-03-27 stsp if (err)
3165 44392932 2019-08-25 stsp goto done;
3166 79109fed 2018-03-27 stsp }
3167 79109fed 2018-03-27 stsp
3168 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3169 0f2b3dca 2018-12-22 stsp if (err)
3170 0f2b3dca 2018-12-22 stsp goto done;
3171 0f2b3dca 2018-12-22 stsp
3172 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3173 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3174 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3175 fe621944 2020-11-10 stsp arg.line_offsets = NULL;
3176 fe621944 2020-11-10 stsp arg.nlines = 0;
3177 44392932 2019-08-25 stsp while (path[0] == '/')
3178 44392932 2019-08-25 stsp path++;
3179 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3180 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3181 0f2b3dca 2018-12-22 stsp done:
3182 79109fed 2018-03-27 stsp if (tree1)
3183 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3184 366e0a5f 2019-10-10 stsp if (tree2)
3185 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3186 44392932 2019-08-25 stsp return err;
3187 44392932 2019-08-25 stsp }
3188 44392932 2019-08-25 stsp
3189 44392932 2019-08-25 stsp static const struct got_error *
3190 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3191 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3192 0208f208 2020-05-05 stsp {
3193 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3194 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3195 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3196 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3197 0208f208 2020-05-05 stsp
3198 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3199 0208f208 2020-05-05 stsp if (qid != NULL) {
3200 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3201 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3202 0208f208 2020-05-05 stsp qid->id);
3203 0208f208 2020-05-05 stsp if (err)
3204 0208f208 2020-05-05 stsp return err;
3205 0208f208 2020-05-05 stsp
3206 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3207 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3208 0208f208 2020-05-05 stsp
3209 0208f208 2020-05-05 stsp }
3210 0208f208 2020-05-05 stsp
3211 0208f208 2020-05-05 stsp if (tree_id1) {
3212 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3213 0208f208 2020-05-05 stsp if (err)
3214 0208f208 2020-05-05 stsp goto done;
3215 0208f208 2020-05-05 stsp }
3216 0208f208 2020-05-05 stsp
3217 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3218 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3219 0208f208 2020-05-05 stsp if (err)
3220 0208f208 2020-05-05 stsp goto done;
3221 0208f208 2020-05-05 stsp
3222 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3223 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3224 0208f208 2020-05-05 stsp done:
3225 0208f208 2020-05-05 stsp if (tree1)
3226 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3227 0208f208 2020-05-05 stsp if (tree2)
3228 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3229 0208f208 2020-05-05 stsp return err;
3230 0208f208 2020-05-05 stsp }
3231 0208f208 2020-05-05 stsp
3232 0208f208 2020-05-05 stsp static const struct got_error *
3233 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3234 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3235 44392932 2019-08-25 stsp {
3236 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3237 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3238 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3239 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3240 44392932 2019-08-25 stsp struct got_object_qid *qid;
3241 44392932 2019-08-25 stsp
3242 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3243 44392932 2019-08-25 stsp if (qid != NULL) {
3244 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3245 44392932 2019-08-25 stsp qid->id);
3246 44392932 2019-08-25 stsp if (err)
3247 44392932 2019-08-25 stsp return err;
3248 44392932 2019-08-25 stsp }
3249 44392932 2019-08-25 stsp
3250 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3251 44392932 2019-08-25 stsp int obj_type;
3252 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3253 44392932 2019-08-25 stsp if (err)
3254 44392932 2019-08-25 stsp goto done;
3255 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3256 44392932 2019-08-25 stsp if (err) {
3257 44392932 2019-08-25 stsp free(obj_id2);
3258 44392932 2019-08-25 stsp goto done;
3259 44392932 2019-08-25 stsp }
3260 44392932 2019-08-25 stsp if (pcommit) {
3261 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3262 44392932 2019-08-25 stsp qid->id, path);
3263 44392932 2019-08-25 stsp if (err) {
3264 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3265 2e8c69d1 2020-05-04 stsp free(obj_id2);
3266 2e8c69d1 2020-05-04 stsp goto done;
3267 2e8c69d1 2020-05-04 stsp }
3268 2e8c69d1 2020-05-04 stsp } else {
3269 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3270 2e8c69d1 2020-05-04 stsp if (err) {
3271 2e8c69d1 2020-05-04 stsp free(obj_id2);
3272 2e8c69d1 2020-05-04 stsp goto done;
3273 2e8c69d1 2020-05-04 stsp }
3274 44392932 2019-08-25 stsp }
3275 44392932 2019-08-25 stsp }
3276 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3277 44392932 2019-08-25 stsp if (err) {
3278 44392932 2019-08-25 stsp free(obj_id2);
3279 44392932 2019-08-25 stsp goto done;
3280 44392932 2019-08-25 stsp }
3281 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3282 44392932 2019-08-25 stsp switch (obj_type) {
3283 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3284 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3285 63035f9f 2019-10-06 stsp 0, repo);
3286 44392932 2019-08-25 stsp break;
3287 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3288 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3289 63035f9f 2019-10-06 stsp 0, repo);
3290 44392932 2019-08-25 stsp break;
3291 44392932 2019-08-25 stsp default:
3292 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3293 44392932 2019-08-25 stsp break;
3294 44392932 2019-08-25 stsp }
3295 44392932 2019-08-25 stsp free(obj_id1);
3296 44392932 2019-08-25 stsp free(obj_id2);
3297 44392932 2019-08-25 stsp } else {
3298 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3299 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3300 44392932 2019-08-25 stsp if (err)
3301 44392932 2019-08-25 stsp goto done;
3302 579bd556 2020-10-24 stsp if (pcommit) {
3303 579bd556 2020-10-24 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3304 579bd556 2020-10-24 stsp err = got_object_id_str(&id_str1, obj_id1);
3305 579bd556 2020-10-24 stsp if (err)
3306 579bd556 2020-10-24 stsp goto done;
3307 579bd556 2020-10-24 stsp }
3308 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3309 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3310 44392932 2019-08-25 stsp }
3311 44392932 2019-08-25 stsp done:
3312 0f2b3dca 2018-12-22 stsp free(id_str1);
3313 0f2b3dca 2018-12-22 stsp free(id_str2);
3314 44392932 2019-08-25 stsp if (pcommit)
3315 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3316 79109fed 2018-03-27 stsp return err;
3317 79109fed 2018-03-27 stsp }
3318 79109fed 2018-03-27 stsp
3319 4bb494d5 2018-06-16 stsp static char *
3320 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3321 6c281f94 2018-06-11 stsp {
3322 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3323 09867e48 2019-08-13 stsp char *p, *s;
3324 09867e48 2019-08-13 stsp
3325 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3326 09867e48 2019-08-13 stsp if (tm == NULL)
3327 09867e48 2019-08-13 stsp return NULL;
3328 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3329 09867e48 2019-08-13 stsp if (s == NULL)
3330 09867e48 2019-08-13 stsp return NULL;
3331 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3332 6c281f94 2018-06-11 stsp if (p)
3333 6c281f94 2018-06-11 stsp *p = '\0';
3334 6c281f94 2018-06-11 stsp return s;
3335 6c281f94 2018-06-11 stsp }
3336 dc424a06 2019-08-07 stsp
3337 6841bf13 2019-11-29 kn static const struct got_error *
3338 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3339 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3340 6841bf13 2019-11-29 kn {
3341 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3342 6841bf13 2019-11-29 kn regmatch_t regmatch;
3343 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3344 6841bf13 2019-11-29 kn
3345 6841bf13 2019-11-29 kn *have_match = 0;
3346 6841bf13 2019-11-29 kn
3347 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3348 6841bf13 2019-11-29 kn if (err)
3349 6841bf13 2019-11-29 kn return err;
3350 6841bf13 2019-11-29 kn
3351 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3352 6841bf13 2019-11-29 kn if (err)
3353 6841bf13 2019-11-29 kn goto done;
3354 6841bf13 2019-11-29 kn
3355 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3356 6841bf13 2019-11-29 kn *have_match = 1;
3357 6841bf13 2019-11-29 kn done:
3358 6841bf13 2019-11-29 kn free(id_str);
3359 6841bf13 2019-11-29 kn free(logmsg);
3360 6841bf13 2019-11-29 kn return err;
3361 6841bf13 2019-11-29 kn }
3362 6841bf13 2019-11-29 kn
3363 0208f208 2020-05-05 stsp static void
3364 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3365 0208f208 2020-05-05 stsp regex_t *regex)
3366 0208f208 2020-05-05 stsp {
3367 0208f208 2020-05-05 stsp regmatch_t regmatch;
3368 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3369 0208f208 2020-05-05 stsp
3370 0208f208 2020-05-05 stsp *have_match = 0;
3371 0208f208 2020-05-05 stsp
3372 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3373 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3374 0208f208 2020-05-05 stsp *have_match = 1;
3375 0208f208 2020-05-05 stsp break;
3376 0208f208 2020-05-05 stsp }
3377 0208f208 2020-05-05 stsp }
3378 0208f208 2020-05-05 stsp }
3379 0208f208 2020-05-05 stsp
3380 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3381 6c281f94 2018-06-11 stsp
3382 79109fed 2018-03-27 stsp static const struct got_error *
3383 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3384 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3385 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3386 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3387 79109fed 2018-03-27 stsp {
3388 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3389 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3390 6c281f94 2018-06-11 stsp char datebuf[26];
3391 45d799e2 2018-12-23 stsp time_t committer_time;
3392 45d799e2 2018-12-23 stsp const char *author, *committer;
3393 199a4027 2019-02-02 stsp char *refs_str = NULL;
3394 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3395 5c860e29 2018-03-12 stsp
3396 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3397 199a4027 2019-02-02 stsp char *s;
3398 199a4027 2019-02-02 stsp const char *name;
3399 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3400 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3401 a436ad14 2019-08-13 stsp int cmp;
3402 a436ad14 2019-08-13 stsp
3403 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3404 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3405 d9498b20 2019-02-05 stsp continue;
3406 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3407 199a4027 2019-02-02 stsp name += 5;
3408 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3409 7143d404 2019-03-12 stsp continue;
3410 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3411 e34f9ed6 2019-02-02 stsp name += 6;
3412 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3413 141c2bff 2019-02-04 stsp name += 8;
3414 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3415 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3416 4343a07f 2020-04-24 stsp continue;
3417 4343a07f 2020-04-24 stsp }
3418 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3419 48cae60d 2020-09-22 stsp if (err)
3420 48cae60d 2020-09-22 stsp return err;
3421 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3422 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3423 5d844a1e 2019-08-13 stsp if (err) {
3424 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3425 48cae60d 2020-09-22 stsp free(ref_id);
3426 5d844a1e 2019-08-13 stsp return err;
3427 48cae60d 2020-09-22 stsp }
3428 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3429 5d844a1e 2019-08-13 stsp err = NULL;
3430 5d844a1e 2019-08-13 stsp tag = NULL;
3431 5d844a1e 2019-08-13 stsp }
3432 a436ad14 2019-08-13 stsp }
3433 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3434 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3435 48cae60d 2020-09-22 stsp free(ref_id);
3436 a436ad14 2019-08-13 stsp if (tag)
3437 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3438 a436ad14 2019-08-13 stsp if (cmp != 0)
3439 a436ad14 2019-08-13 stsp continue;
3440 199a4027 2019-02-02 stsp s = refs_str;
3441 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3442 199a4027 2019-02-02 stsp name) == -1) {
3443 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3444 199a4027 2019-02-02 stsp free(s);
3445 34ca4898 2019-08-13 stsp return err;
3446 199a4027 2019-02-02 stsp }
3447 199a4027 2019-02-02 stsp free(s);
3448 199a4027 2019-02-02 stsp }
3449 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3450 8bf5b3c9 2018-03-17 stsp if (err)
3451 8bf5b3c9 2018-03-17 stsp return err;
3452 788c352e 2018-06-16 stsp
3453 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3454 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3455 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3456 832c249c 2018-06-10 stsp free(id_str);
3457 d3d493d7 2019-02-21 stsp id_str = NULL;
3458 d3d493d7 2019-02-21 stsp free(refs_str);
3459 d3d493d7 2019-02-21 stsp refs_str = NULL;
3460 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3461 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3462 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3463 09867e48 2019-08-13 stsp if (datestr)
3464 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3465 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3466 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3467 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3468 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3469 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3470 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3471 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3472 3fe1abad 2018-06-10 stsp int n = 1;
3473 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3474 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3475 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3476 a0603db2 2018-06-10 stsp if (err)
3477 a0603db2 2018-06-10 stsp return err;
3478 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3479 a0603db2 2018-06-10 stsp free(id_str);
3480 a0603db2 2018-06-10 stsp }
3481 a0603db2 2018-06-10 stsp }
3482 832c249c 2018-06-10 stsp
3483 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3484 5943eee2 2019-08-13 stsp if (err)
3485 5943eee2 2019-08-13 stsp return err;
3486 8bf5b3c9 2018-03-17 stsp
3487 621015ac 2018-07-23 stsp logmsg = logmsg0;
3488 832c249c 2018-06-10 stsp do {
3489 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3490 832c249c 2018-06-10 stsp if (line)
3491 832c249c 2018-06-10 stsp printf(" %s\n", line);
3492 832c249c 2018-06-10 stsp } while (line);
3493 621015ac 2018-07-23 stsp free(logmsg0);
3494 832c249c 2018-06-10 stsp
3495 0208f208 2020-05-05 stsp if (changed_paths) {
3496 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3497 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3498 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3499 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3500 0208f208 2020-05-05 stsp }
3501 0208f208 2020-05-05 stsp printf("\n");
3502 0208f208 2020-05-05 stsp }
3503 971751ac 2018-03-27 stsp if (show_patch) {
3504 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3505 971751ac 2018-03-27 stsp if (err == 0)
3506 971751ac 2018-03-27 stsp printf("\n");
3507 971751ac 2018-03-27 stsp }
3508 07862c20 2018-09-15 stsp
3509 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3510 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3511 07862c20 2018-09-15 stsp return err;
3512 f42b1b34 2018-03-12 stsp }
3513 5c860e29 2018-03-12 stsp
3514 f42b1b34 2018-03-12 stsp static const struct got_error *
3515 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3516 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3517 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3518 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3519 f42b1b34 2018-03-12 stsp {
3520 f42b1b34 2018-03-12 stsp const struct got_error *err;
3521 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3522 6841bf13 2019-11-29 kn regex_t regex;
3523 6841bf13 2019-11-29 kn int have_match;
3524 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3525 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3526 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3527 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3528 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3529 dbec59df 2020-04-18 stsp
3530 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3531 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3532 372ccdbb 2018-06-10 stsp
3533 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3534 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3535 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3536 6841bf13 2019-11-29 kn
3537 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3538 f42b1b34 2018-03-12 stsp if (err)
3539 f42b1b34 2018-03-12 stsp return err;
3540 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3541 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3542 372ccdbb 2018-06-10 stsp if (err)
3543 fcc85cad 2018-07-22 stsp goto done;
3544 656b1f76 2019-05-11 jcs for (;;) {
3545 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3546 8bf5b3c9 2018-03-17 stsp
3547 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3548 84453469 2018-11-11 stsp break;
3549 84453469 2018-11-11 stsp
3550 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3551 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3552 372ccdbb 2018-06-10 stsp if (err) {
3553 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3554 9ba79e04 2018-06-11 stsp err = NULL;
3555 ee780d5c 2020-01-04 stsp break;
3556 7e665116 2018-04-02 stsp }
3557 b43fbaa0 2018-06-11 stsp if (id == NULL)
3558 7e665116 2018-04-02 stsp break;
3559 b43fbaa0 2018-06-11 stsp
3560 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3561 b43fbaa0 2018-06-11 stsp if (err)
3562 fcc85cad 2018-07-22 stsp break;
3563 6841bf13 2019-11-29 kn
3564 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3565 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3566 0208f208 2020-05-05 stsp if (err)
3567 0208f208 2020-05-05 stsp break;
3568 0208f208 2020-05-05 stsp }
3569 0208f208 2020-05-05 stsp
3570 6841bf13 2019-11-29 kn if (search_pattern) {
3571 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3572 6841bf13 2019-11-29 kn if (err) {
3573 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3574 6841bf13 2019-11-29 kn break;
3575 6841bf13 2019-11-29 kn }
3576 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3577 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3578 0208f208 2020-05-05 stsp &changed_paths, &regex);
3579 6841bf13 2019-11-29 kn if (have_match == 0) {
3580 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3581 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3582 0208f208 2020-05-05 stsp free((char *)pe->path);
3583 0208f208 2020-05-05 stsp free(pe->data);
3584 0208f208 2020-05-05 stsp }
3585 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3586 6841bf13 2019-11-29 kn continue;
3587 6841bf13 2019-11-29 kn }
3588 6841bf13 2019-11-29 kn }
3589 6841bf13 2019-11-29 kn
3590 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3591 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3592 dbec59df 2020-04-18 stsp if (err)
3593 dbec59df 2020-04-18 stsp break;
3594 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3595 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3596 dbec59df 2020-04-18 stsp } else {
3597 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3598 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3599 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3600 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3601 dbec59df 2020-04-18 stsp if (err)
3602 dbec59df 2020-04-18 stsp break;
3603 dbec59df 2020-04-18 stsp }
3604 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3605 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3606 7e665116 2018-04-02 stsp break;
3607 0208f208 2020-05-05 stsp
3608 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3609 0208f208 2020-05-05 stsp free((char *)pe->path);
3610 0208f208 2020-05-05 stsp free(pe->data);
3611 0208f208 2020-05-05 stsp }
3612 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3613 31cedeaf 2018-09-15 stsp }
3614 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3615 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3616 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3617 dbec59df 2020-04-18 stsp if (err)
3618 dbec59df 2020-04-18 stsp break;
3619 502b9684 2020-07-31 stsp if (show_changed_paths) {
3620 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3621 502b9684 2020-07-31 stsp commit, repo);
3622 502b9684 2020-07-31 stsp if (err)
3623 502b9684 2020-07-31 stsp break;
3624 502b9684 2020-07-31 stsp }
3625 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3626 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3627 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3628 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3629 dbec59df 2020-04-18 stsp if (err)
3630 dbec59df 2020-04-18 stsp break;
3631 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3632 502b9684 2020-07-31 stsp free((char *)pe->path);
3633 502b9684 2020-07-31 stsp free(pe->data);
3634 502b9684 2020-07-31 stsp }
3635 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3636 dbec59df 2020-04-18 stsp }
3637 dbec59df 2020-04-18 stsp }
3638 fcc85cad 2018-07-22 stsp done:
3639 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3640 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3641 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3642 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3643 dbec59df 2020-04-18 stsp }
3644 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3645 0208f208 2020-05-05 stsp free((char *)pe->path);
3646 0208f208 2020-05-05 stsp free(pe->data);
3647 0208f208 2020-05-05 stsp }
3648 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3649 6841bf13 2019-11-29 kn if (search_pattern)
3650 6841bf13 2019-11-29 kn regfree(&regex);
3651 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3652 f42b1b34 2018-03-12 stsp return err;
3653 f42b1b34 2018-03-12 stsp }
3654 5c860e29 2018-03-12 stsp
3655 4ed7e80c 2018-05-20 stsp __dead static void
3656 6f3d1eb0 2018-03-12 stsp usage_log(void)
3657 6f3d1eb0 2018-03-12 stsp {
3658 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3659 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3660 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3661 6f3d1eb0 2018-03-12 stsp exit(1);
3662 b1ebc001 2019-08-13 stsp }
3663 b1ebc001 2019-08-13 stsp
3664 b1ebc001 2019-08-13 stsp static int
3665 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3666 b1ebc001 2019-08-13 stsp {
3667 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3668 b1ebc001 2019-08-13 stsp long long n;
3669 b1ebc001 2019-08-13 stsp const char *errstr;
3670 b1ebc001 2019-08-13 stsp
3671 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3672 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3673 b1ebc001 2019-08-13 stsp return 0;
3674 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3675 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3676 b1ebc001 2019-08-13 stsp return 0;
3677 b1ebc001 2019-08-13 stsp return n;
3678 6f3d1eb0 2018-03-12 stsp }
3679 6f3d1eb0 2018-03-12 stsp
3680 4ed7e80c 2018-05-20 stsp static const struct got_error *
3681 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3682 f42b1b34 2018-03-12 stsp {
3683 f42b1b34 2018-03-12 stsp const struct got_error *error;
3684 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3685 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3686 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3687 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3688 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3689 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3690 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3691 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3692 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3693 64a96a6d 2018-04-01 stsp const char *errstr;
3694 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3695 1b3893a2 2019-03-18 stsp
3696 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3697 5c860e29 2018-03-12 stsp
3698 6715a751 2018-03-16 stsp #ifndef PROFILE
3699 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3700 6098196c 2019-01-04 stsp NULL)
3701 ad242220 2018-09-08 stsp == -1)
3702 f42b1b34 2018-03-12 stsp err(1, "pledge");
3703 6715a751 2018-03-16 stsp #endif
3704 79109fed 2018-03-27 stsp
3705 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3706 b1ebc001 2019-08-13 stsp
3707 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3708 79109fed 2018-03-27 stsp switch (ch) {
3709 79109fed 2018-03-27 stsp case 'p':
3710 79109fed 2018-03-27 stsp show_patch = 1;
3711 d142fc45 2018-04-01 stsp break;
3712 0208f208 2020-05-05 stsp case 'P':
3713 0208f208 2020-05-05 stsp show_changed_paths = 1;
3714 0208f208 2020-05-05 stsp break;
3715 d142fc45 2018-04-01 stsp case 'c':
3716 d142fc45 2018-04-01 stsp start_commit = optarg;
3717 64a96a6d 2018-04-01 stsp break;
3718 c0cc5c62 2018-10-18 stsp case 'C':
3719 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3720 4a8520aa 2018-10-18 stsp &errstr);
3721 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3722 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3723 c0cc5c62 2018-10-18 stsp break;
3724 64a96a6d 2018-04-01 stsp case 'l':
3725 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3726 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3727 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3728 cc54c501 2019-07-15 stsp break;
3729 48c8c60d 2020-01-27 stsp case 'b':
3730 48c8c60d 2020-01-27 stsp log_branches = 1;
3731 a0603db2 2018-06-10 stsp break;
3732 04ca23f4 2018-07-16 stsp case 'r':
3733 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3734 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3735 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3736 9ba1d308 2019-10-21 stsp optarg);
3737 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3738 04ca23f4 2018-07-16 stsp break;
3739 dbec59df 2020-04-18 stsp case 'R':
3740 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3741 dbec59df 2020-04-18 stsp break;
3742 6841bf13 2019-11-29 kn case 's':
3743 6841bf13 2019-11-29 kn search_pattern = optarg;
3744 6841bf13 2019-11-29 kn break;
3745 d1fe46f9 2020-04-18 stsp case 'x':
3746 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3747 d1fe46f9 2020-04-18 stsp break;
3748 79109fed 2018-03-27 stsp default:
3749 2deda0b9 2019-03-07 stsp usage_log();
3750 79109fed 2018-03-27 stsp /* NOTREACHED */
3751 79109fed 2018-03-27 stsp }
3752 79109fed 2018-03-27 stsp }
3753 79109fed 2018-03-27 stsp
3754 79109fed 2018-03-27 stsp argc -= optind;
3755 79109fed 2018-03-27 stsp argv += optind;
3756 f42b1b34 2018-03-12 stsp
3757 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3758 dc1edbfa 2019-11-29 kn diff_context = 3;
3759 dc1edbfa 2019-11-29 kn else if (!show_patch)
3760 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3761 dc1edbfa 2019-11-29 kn
3762 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3763 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3764 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3765 04ca23f4 2018-07-16 stsp goto done;
3766 04ca23f4 2018-07-16 stsp }
3767 cffc0aa4 2019-02-05 stsp
3768 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3769 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3770 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3771 50f2fada 2020-04-24 stsp goto done;
3772 50f2fada 2020-04-24 stsp error = NULL;
3773 50f2fada 2020-04-24 stsp }
3774 cffc0aa4 2019-02-05 stsp
3775 603cdeb0 2020-10-22 stsp if (argc == 1) {
3776 e7301579 2019-03-18 stsp if (worktree) {
3777 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3778 e7301579 2019-03-18 stsp argv[0]);
3779 e7301579 2019-03-18 stsp if (error)
3780 e7301579 2019-03-18 stsp goto done;
3781 e7301579 2019-03-18 stsp } else {
3782 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3783 e7301579 2019-03-18 stsp if (path == NULL) {
3784 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3785 e7301579 2019-03-18 stsp goto done;
3786 e7301579 2019-03-18 stsp }
3787 e7301579 2019-03-18 stsp }
3788 603cdeb0 2020-10-22 stsp } else if (argc != 0)
3789 cbd1af7a 2019-03-18 stsp usage_log();
3790 cbd1af7a 2019-03-18 stsp
3791 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3792 5486daa2 2019-05-11 stsp repo_path = worktree ?
3793 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3794 5486daa2 2019-05-11 stsp }
3795 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3796 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3797 cffc0aa4 2019-02-05 stsp goto done;
3798 04ca23f4 2018-07-16 stsp }
3799 6098196c 2019-01-04 stsp
3800 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3801 d7d4f210 2018-03-12 stsp if (error != NULL)
3802 04ca23f4 2018-07-16 stsp goto done;
3803 f42b1b34 2018-03-12 stsp
3804 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3805 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3806 c02c541e 2019-03-29 stsp if (error)
3807 c02c541e 2019-03-29 stsp goto done;
3808 c02c541e 2019-03-29 stsp
3809 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3810 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3811 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3812 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3813 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3814 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3815 3235492e 2018-04-01 stsp if (error != NULL)
3816 d1fe46f9 2020-04-18 stsp goto done;
3817 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3818 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3819 3235492e 2018-04-01 stsp if (error != NULL)
3820 d1fe46f9 2020-04-18 stsp goto done;
3821 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3822 d1fe46f9 2020-04-18 stsp start_id);
3823 d1fe46f9 2020-04-18 stsp if (error != NULL)
3824 d1fe46f9 2020-04-18 stsp goto done;
3825 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3826 3235492e 2018-04-01 stsp } else {
3827 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&start_id, NULL,
3828 7f9bfb31 2020-11-01 stsp start_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3829 d1fe46f9 2020-04-18 stsp if (error != NULL)
3830 d1fe46f9 2020-04-18 stsp goto done;
3831 3235492e 2018-04-01 stsp }
3832 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3833 7f9bfb31 2020-11-01 stsp error = got_repo_match_object_id(&end_id, NULL,
3834 7f9bfb31 2020-11-01 stsp end_commit, GOT_OBJ_TYPE_COMMIT, 1, repo);
3835 d1fe46f9 2020-04-18 stsp if (error != NULL)
3836 d1fe46f9 2020-04-18 stsp goto done;
3837 d1fe46f9 2020-04-18 stsp }
3838 04ca23f4 2018-07-16 stsp
3839 deeabeae 2019-08-27 stsp if (worktree) {
3840 603cdeb0 2020-10-22 stsp /*
3841 603cdeb0 2020-10-22 stsp * If a path was specified on the command line it was resolved
3842 603cdeb0 2020-10-22 stsp * to a path in the work tree above. Prepend the work tree's
3843 603cdeb0 2020-10-22 stsp * path prefix to obtain the corresponding in-repository path.
3844 603cdeb0 2020-10-22 stsp */
3845 603cdeb0 2020-10-22 stsp if (path) {
3846 603cdeb0 2020-10-22 stsp const char *prefix;
3847 603cdeb0 2020-10-22 stsp prefix = got_worktree_get_path_prefix(worktree);
3848 603cdeb0 2020-10-22 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
3849 603cdeb0 2020-10-22 stsp (path[0] != '\0') ? "/" : "", path) == -1) {
3850 603cdeb0 2020-10-22 stsp error = got_error_from_errno("asprintf");
3851 603cdeb0 2020-10-22 stsp goto done;
3852 603cdeb0 2020-10-22 stsp }
3853 603cdeb0 2020-10-22 stsp }
3854 deeabeae 2019-08-27 stsp } else
3855 603cdeb0 2020-10-22 stsp error = got_repo_map_path(&in_repo_path, repo,
3856 8fa913ec 2020-11-14 stsp path ? path : "");
3857 04ca23f4 2018-07-16 stsp if (error != NULL)
3858 04ca23f4 2018-07-16 stsp goto done;
3859 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3860 04ca23f4 2018-07-16 stsp free(path);
3861 04ca23f4 2018-07-16 stsp path = in_repo_path;
3862 04ca23f4 2018-07-16 stsp }
3863 199a4027 2019-02-02 stsp
3864 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3865 199a4027 2019-02-02 stsp if (error)
3866 199a4027 2019-02-02 stsp goto done;
3867 04ca23f4 2018-07-16 stsp
3868 603cdeb0 2020-10-22 stsp error = print_commits(start_id, end_id, repo, path ? path : "",
3869 603cdeb0 2020-10-22 stsp show_changed_paths, show_patch, search_pattern, diff_context,
3870 603cdeb0 2020-10-22 stsp limit, log_branches, reverse_display_order, &refs);
3871 04ca23f4 2018-07-16 stsp done:
3872 04ca23f4 2018-07-16 stsp free(path);
3873 04ca23f4 2018-07-16 stsp free(repo_path);
3874 04ca23f4 2018-07-16 stsp free(cwd);
3875 cffc0aa4 2019-02-05 stsp if (worktree)
3876 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3877 ad242220 2018-09-08 stsp if (repo) {
3878 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3879 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3880 ad242220 2018-09-08 stsp if (error == NULL)
3881 ad242220 2018-09-08 stsp error = repo_error;
3882 ad242220 2018-09-08 stsp }
3883 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3884 8bf5b3c9 2018-03-17 stsp return error;
3885 5c860e29 2018-03-12 stsp }
3886 5c860e29 2018-03-12 stsp
3887 4ed7e80c 2018-05-20 stsp __dead static void
3888 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3889 3f8b7d6a 2018-04-01 stsp {
3890 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3891 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3892 3f8b7d6a 2018-04-01 stsp exit(1);
3893 b00d56cd 2018-04-01 stsp }
3894 b00d56cd 2018-04-01 stsp
3895 b72f483a 2019-02-05 stsp struct print_diff_arg {
3896 b72f483a 2019-02-05 stsp struct got_repository *repo;
3897 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3898 b72f483a 2019-02-05 stsp int diff_context;
3899 3fc0c068 2019-02-10 stsp const char *id_str;
3900 3fc0c068 2019-02-10 stsp int header_shown;
3901 98eaaa12 2019-08-03 stsp int diff_staged;
3902 63035f9f 2019-10-06 stsp int ignore_whitespace;
3903 b72f483a 2019-02-05 stsp };
3904 39449a05 2020-07-23 stsp
3905 39449a05 2020-07-23 stsp /*
3906 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3907 39449a05 2020-07-23 stsp * it as content to the diff engine.
3908 39449a05 2020-07-23 stsp */
3909 39449a05 2020-07-23 stsp static const struct got_error *
3910 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3911 39449a05 2020-07-23 stsp const char *abspath)
3912 39449a05 2020-07-23 stsp {
3913 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3914 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3915 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3916 39449a05 2020-07-23 stsp
3917 39449a05 2020-07-23 stsp *fd = -1;
3918 39449a05 2020-07-23 stsp
3919 39449a05 2020-07-23 stsp if (dirfd != -1) {
3920 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3921 39449a05 2020-07-23 stsp if (target_len == -1)
3922 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3923 39449a05 2020-07-23 stsp } else {
3924 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3925 39449a05 2020-07-23 stsp if (target_len == -1)
3926 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3927 39449a05 2020-07-23 stsp }
3928 39449a05 2020-07-23 stsp
3929 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3930 39449a05 2020-07-23 stsp if (*fd == -1)
3931 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3932 39449a05 2020-07-23 stsp
3933 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3934 39449a05 2020-07-23 stsp if (outlen == -1) {
3935 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3936 39449a05 2020-07-23 stsp goto done;
3937 39449a05 2020-07-23 stsp }
3938 39449a05 2020-07-23 stsp
3939 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3940 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3941 39449a05 2020-07-23 stsp goto done;
3942 39449a05 2020-07-23 stsp }
3943 39449a05 2020-07-23 stsp done:
3944 39449a05 2020-07-23 stsp if (err) {
3945 39449a05 2020-07-23 stsp close(*fd);
3946 39449a05 2020-07-23 stsp *fd = -1;
3947 39449a05 2020-07-23 stsp }
3948 39449a05 2020-07-23 stsp return err;
3949 39449a05 2020-07-23 stsp }
3950 b72f483a 2019-02-05 stsp
3951 4ed7e80c 2018-05-20 stsp static const struct got_error *
3952 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3953 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3954 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3955 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3956 b72f483a 2019-02-05 stsp {
3957 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3958 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3959 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3960 12463d8b 2019-12-13 stsp int fd = -1;
3961 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3962 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3963 b72f483a 2019-02-05 stsp struct stat sb;
3964 b72f483a 2019-02-05 stsp
3965 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3966 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3967 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3968 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3969 98eaaa12 2019-08-03 stsp return NULL;
3970 98eaaa12 2019-08-03 stsp } else {
3971 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3972 98eaaa12 2019-08-03 stsp return NULL;
3973 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3974 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3975 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3976 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3977 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3978 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3979 98eaaa12 2019-08-03 stsp return NULL;
3980 98eaaa12 2019-08-03 stsp }
3981 3fc0c068 2019-02-10 stsp
3982 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3983 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3984 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3985 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3986 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3987 3fc0c068 2019-02-10 stsp }
3988 b72f483a 2019-02-05 stsp
3989 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3990 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3991 98eaaa12 2019-08-03 stsp switch (staged_status) {
3992 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3993 98eaaa12 2019-08-03 stsp label1 = path;
3994 98eaaa12 2019-08-03 stsp label2 = path;
3995 98eaaa12 2019-08-03 stsp break;
3996 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3997 98eaaa12 2019-08-03 stsp label2 = path;
3998 98eaaa12 2019-08-03 stsp break;
3999 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
4000 98eaaa12 2019-08-03 stsp label1 = path;
4001 98eaaa12 2019-08-03 stsp break;
4002 98eaaa12 2019-08-03 stsp default:
4003 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
4004 98eaaa12 2019-08-03 stsp }
4005 fe621944 2020-11-10 stsp return got_diff_objects_as_blobs(NULL, NULL, blob_id,
4006 fe621944 2020-11-10 stsp staged_blob_id, label1, label2, a->diff_context,
4007 fe621944 2020-11-10 stsp a->ignore_whitespace, a->repo, stdout);
4008 98eaaa12 2019-08-03 stsp }
4009 98eaaa12 2019-08-03 stsp
4010 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
4011 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
4012 4ce46740 2019-08-08 stsp char *id_str;
4013 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4014 408b4ebc 2019-08-03 stsp 8192);
4015 4ce46740 2019-08-08 stsp if (err)
4016 4ce46740 2019-08-08 stsp goto done;
4017 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4018 4ce46740 2019-08-08 stsp if (err)
4019 4ce46740 2019-08-08 stsp goto done;
4020 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4021 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4022 4ce46740 2019-08-08 stsp free(id_str);
4023 4ce46740 2019-08-08 stsp goto done;
4024 4ce46740 2019-08-08 stsp }
4025 4ce46740 2019-08-08 stsp free(id_str);
4026 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4027 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4028 4ce46740 2019-08-08 stsp if (err)
4029 4ce46740 2019-08-08 stsp goto done;
4030 4ce46740 2019-08-08 stsp }
4031 d00136be 2019-03-26 stsp
4032 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4033 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4034 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4035 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4036 2ec1f75b 2019-03-26 stsp goto done;
4037 2ec1f75b 2019-03-26 stsp }
4038 b72f483a 2019-02-05 stsp
4039 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4040 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4041 12463d8b 2019-12-13 stsp if (fd == -1) {
4042 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4043 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4044 39449a05 2020-07-23 stsp abspath);
4045 39449a05 2020-07-23 stsp goto done;
4046 39449a05 2020-07-23 stsp }
4047 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4048 39449a05 2020-07-23 stsp de_name, abspath);
4049 39449a05 2020-07-23 stsp if (err)
4050 39449a05 2020-07-23 stsp goto done;
4051 12463d8b 2019-12-13 stsp }
4052 12463d8b 2019-12-13 stsp } else {
4053 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4054 12463d8b 2019-12-13 stsp if (fd == -1) {
4055 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4056 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4057 39449a05 2020-07-23 stsp abspath);
4058 39449a05 2020-07-23 stsp goto done;
4059 39449a05 2020-07-23 stsp }
4060 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4061 39449a05 2020-07-23 stsp de_name, abspath);
4062 39449a05 2020-07-23 stsp if (err)
4063 39449a05 2020-07-23 stsp goto done;
4064 12463d8b 2019-12-13 stsp }
4065 12463d8b 2019-12-13 stsp }
4066 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4067 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4068 2ec1f75b 2019-03-26 stsp goto done;
4069 2ec1f75b 2019-03-26 stsp }
4070 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4071 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4072 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4073 2ec1f75b 2019-03-26 stsp goto done;
4074 2ec1f75b 2019-03-26 stsp }
4075 12463d8b 2019-12-13 stsp fd = -1;
4076 2ec1f75b 2019-03-26 stsp } else
4077 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4078 b72f483a 2019-02-05 stsp
4079 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4080 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
4081 b72f483a 2019-02-05 stsp done:
4082 b72f483a 2019-02-05 stsp if (blob1)
4083 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4084 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4085 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4086 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4087 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4088 b72f483a 2019-02-05 stsp free(abspath);
4089 927df6b7 2019-02-10 stsp return err;
4090 927df6b7 2019-02-10 stsp }
4091 927df6b7 2019-02-10 stsp
4092 927df6b7 2019-02-10 stsp static const struct got_error *
4093 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4094 b00d56cd 2018-04-01 stsp {
4095 b00d56cd 2018-04-01 stsp const struct got_error *error;
4096 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4097 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4098 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4099 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4100 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4101 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4102 15a94983 2018-12-23 stsp int type1, type2;
4103 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4104 c0cc5c62 2018-10-18 stsp const char *errstr;
4105 927df6b7 2019-02-10 stsp char *path = NULL;
4106 b00d56cd 2018-04-01 stsp
4107 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4108 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4109 25eccc22 2019-01-04 stsp NULL) == -1)
4110 b00d56cd 2018-04-01 stsp err(1, "pledge");
4111 b00d56cd 2018-04-01 stsp #endif
4112 b00d56cd 2018-04-01 stsp
4113 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
4114 b00d56cd 2018-04-01 stsp switch (ch) {
4115 c0cc5c62 2018-10-18 stsp case 'C':
4116 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4117 dfcab68b 2019-11-29 kn &errstr);
4118 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4119 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4120 c0cc5c62 2018-10-18 stsp break;
4121 b72f483a 2019-02-05 stsp case 'r':
4122 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4123 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4124 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4125 9ba1d308 2019-10-21 stsp optarg);
4126 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4127 b72f483a 2019-02-05 stsp break;
4128 98eaaa12 2019-08-03 stsp case 's':
4129 98eaaa12 2019-08-03 stsp diff_staged = 1;
4130 63035f9f 2019-10-06 stsp break;
4131 63035f9f 2019-10-06 stsp case 'w':
4132 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4133 98eaaa12 2019-08-03 stsp break;
4134 b00d56cd 2018-04-01 stsp default:
4135 2deda0b9 2019-03-07 stsp usage_diff();
4136 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4137 b00d56cd 2018-04-01 stsp }
4138 b00d56cd 2018-04-01 stsp }
4139 b00d56cd 2018-04-01 stsp
4140 b00d56cd 2018-04-01 stsp argc -= optind;
4141 b00d56cd 2018-04-01 stsp argv += optind;
4142 b00d56cd 2018-04-01 stsp
4143 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4144 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4145 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4146 b72f483a 2019-02-05 stsp goto done;
4147 b72f483a 2019-02-05 stsp }
4148 927df6b7 2019-02-10 stsp if (argc <= 1) {
4149 b72f483a 2019-02-05 stsp if (repo_path)
4150 b72f483a 2019-02-05 stsp errx(1,
4151 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4152 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4153 fa51e947 2020-03-27 stsp if (error) {
4154 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4155 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4156 fa51e947 2020-03-27 stsp cwd);
4157 1f03b8da 2020-03-20 stsp goto done;
4158 fa51e947 2020-03-27 stsp }
4159 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4160 927df6b7 2019-02-10 stsp if (repo_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 if (argc == 1) {
4165 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4166 cbd1af7a 2019-03-18 stsp argv[0]);
4167 927df6b7 2019-02-10 stsp if (error)
4168 927df6b7 2019-02-10 stsp goto done;
4169 927df6b7 2019-02-10 stsp } else {
4170 927df6b7 2019-02-10 stsp path = strdup("");
4171 927df6b7 2019-02-10 stsp if (path == NULL) {
4172 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4173 927df6b7 2019-02-10 stsp goto done;
4174 927df6b7 2019-02-10 stsp }
4175 927df6b7 2019-02-10 stsp }
4176 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4177 98eaaa12 2019-08-03 stsp if (diff_staged)
4178 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4179 98eaaa12 2019-08-03 stsp "objects in repository");
4180 15a94983 2018-12-23 stsp id_str1 = argv[0];
4181 15a94983 2018-12-23 stsp id_str2 = argv[1];
4182 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4183 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4184 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4185 30db809c 2019-06-05 stsp goto done;
4186 1f03b8da 2020-03-20 stsp if (worktree) {
4187 1f03b8da 2020-03-20 stsp repo_path = strdup(
4188 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4189 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4190 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4191 1f03b8da 2020-03-20 stsp goto done;
4192 1f03b8da 2020-03-20 stsp }
4193 1f03b8da 2020-03-20 stsp } else {
4194 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4195 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4196 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4197 1f03b8da 2020-03-20 stsp goto done;
4198 1f03b8da 2020-03-20 stsp }
4199 30db809c 2019-06-05 stsp }
4200 30db809c 2019-06-05 stsp }
4201 b00d56cd 2018-04-01 stsp } else
4202 b00d56cd 2018-04-01 stsp usage_diff();
4203 b00d56cd 2018-04-01 stsp
4204 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4205 76089277 2018-04-01 stsp free(repo_path);
4206 b00d56cd 2018-04-01 stsp if (error != NULL)
4207 b00d56cd 2018-04-01 stsp goto done;
4208 b00d56cd 2018-04-01 stsp
4209 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4210 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4211 c02c541e 2019-03-29 stsp if (error)
4212 c02c541e 2019-03-29 stsp goto done;
4213 c02c541e 2019-03-29 stsp
4214 30db809c 2019-06-05 stsp if (argc <= 1) {
4215 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4216 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4217 b72f483a 2019-02-05 stsp char *id_str;
4218 72ea6654 2019-07-27 stsp
4219 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4220 72ea6654 2019-07-27 stsp
4221 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4222 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4223 b72f483a 2019-02-05 stsp if (error)
4224 b72f483a 2019-02-05 stsp goto done;
4225 b72f483a 2019-02-05 stsp arg.repo = repo;
4226 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4227 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4228 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4229 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4230 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4231 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4232 b72f483a 2019-02-05 stsp
4233 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4234 72ea6654 2019-07-27 stsp if (error)
4235 72ea6654 2019-07-27 stsp goto done;
4236 72ea6654 2019-07-27 stsp
4237 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4238 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4239 b72f483a 2019-02-05 stsp free(id_str);
4240 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4241 b72f483a 2019-02-05 stsp goto done;
4242 b72f483a 2019-02-05 stsp }
4243 b72f483a 2019-02-05 stsp
4244 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4245 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4246 0adb7196 2019-08-11 stsp if (error)
4247 0adb7196 2019-08-11 stsp goto done;
4248 b00d56cd 2018-04-01 stsp
4249 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4250 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4251 0adb7196 2019-08-11 stsp if (error)
4252 0adb7196 2019-08-11 stsp goto done;
4253 b00d56cd 2018-04-01 stsp
4254 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4255 15a94983 2018-12-23 stsp if (error)
4256 15a94983 2018-12-23 stsp goto done;
4257 15a94983 2018-12-23 stsp
4258 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4259 15a94983 2018-12-23 stsp if (error)
4260 15a94983 2018-12-23 stsp goto done;
4261 15a94983 2018-12-23 stsp
4262 15a94983 2018-12-23 stsp if (type1 != type2) {
4263 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4264 b00d56cd 2018-04-01 stsp goto done;
4265 b00d56cd 2018-04-01 stsp }
4266 b00d56cd 2018-04-01 stsp
4267 15a94983 2018-12-23 stsp switch (type1) {
4268 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4269 fe621944 2020-11-10 stsp error = got_diff_objects_as_blobs(NULL, NULL, id1, id2,
4270 fe621944 2020-11-10 stsp NULL, NULL, diff_context, ignore_whitespace, repo,
4271 fe621944 2020-11-10 stsp stdout);
4272 b00d56cd 2018-04-01 stsp break;
4273 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4274 fe621944 2020-11-10 stsp error = got_diff_objects_as_trees(NULL, NULL, id1, id2,
4275 fe621944 2020-11-10 stsp "", "", diff_context, ignore_whitespace, repo, stdout);
4276 b00d56cd 2018-04-01 stsp break;
4277 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4278 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4279 fe621944 2020-11-10 stsp error = got_diff_objects_as_commits(NULL, NULL, id1, id2,
4280 fe621944 2020-11-10 stsp diff_context, ignore_whitespace, repo, stdout);
4281 b00d56cd 2018-04-01 stsp break;
4282 b00d56cd 2018-04-01 stsp default:
4283 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4284 b00d56cd 2018-04-01 stsp }
4285 b00d56cd 2018-04-01 stsp done:
4286 5e70831e 2019-05-28 stsp free(label1);
4287 5e70831e 2019-05-28 stsp free(label2);
4288 15a94983 2018-12-23 stsp free(id1);
4289 15a94983 2018-12-23 stsp free(id2);
4290 927df6b7 2019-02-10 stsp free(path);
4291 b72f483a 2019-02-05 stsp if (worktree)
4292 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4293 ad242220 2018-09-08 stsp if (repo) {
4294 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4295 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4296 ad242220 2018-09-08 stsp if (error == NULL)
4297 ad242220 2018-09-08 stsp error = repo_error;
4298 ad242220 2018-09-08 stsp }
4299 b00d56cd 2018-04-01 stsp return error;
4300 404c43c4 2018-06-21 stsp }
4301 404c43c4 2018-06-21 stsp
4302 404c43c4 2018-06-21 stsp __dead static void
4303 404c43c4 2018-06-21 stsp usage_blame(void)
4304 404c43c4 2018-06-21 stsp {
4305 9270e621 2019-02-05 stsp fprintf(stderr,
4306 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4307 404c43c4 2018-06-21 stsp getprogname());
4308 404c43c4 2018-06-21 stsp exit(1);
4309 b00d56cd 2018-04-01 stsp }
4310 b00d56cd 2018-04-01 stsp
4311 28315671 2019-08-14 stsp struct blame_line {
4312 28315671 2019-08-14 stsp int annotated;
4313 28315671 2019-08-14 stsp char *id_str;
4314 82f6abb8 2019-08-14 stsp char *committer;
4315 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4316 28315671 2019-08-14 stsp };
4317 28315671 2019-08-14 stsp
4318 28315671 2019-08-14 stsp struct blame_cb_args {
4319 28315671 2019-08-14 stsp struct blame_line *lines;
4320 28315671 2019-08-14 stsp int nlines;
4321 7ef28ff8 2019-08-14 stsp int nlines_prec;
4322 28315671 2019-08-14 stsp int lineno_cur;
4323 28315671 2019-08-14 stsp off_t *line_offsets;
4324 28315671 2019-08-14 stsp FILE *f;
4325 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4326 28315671 2019-08-14 stsp };
4327 28315671 2019-08-14 stsp
4328 404c43c4 2018-06-21 stsp static const struct got_error *
4329 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4330 28315671 2019-08-14 stsp {
4331 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4332 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4333 28315671 2019-08-14 stsp struct blame_line *bline;
4334 82f6abb8 2019-08-14 stsp char *line = NULL;
4335 28315671 2019-08-14 stsp size_t linesize = 0;
4336 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4337 28315671 2019-08-14 stsp off_t offset;
4338 bcb49d15 2019-08-14 stsp struct tm tm;
4339 bcb49d15 2019-08-14 stsp time_t committer_time;
4340 28315671 2019-08-14 stsp
4341 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4342 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4343 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4344 28315671 2019-08-14 stsp
4345 28315671 2019-08-14 stsp if (sigint_received)
4346 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4347 28315671 2019-08-14 stsp
4348 2839f8b9 2019-08-18 stsp if (lineno == -1)
4349 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4350 2839f8b9 2019-08-18 stsp
4351 28315671 2019-08-14 stsp /* Annotate this line. */
4352 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4353 28315671 2019-08-14 stsp if (bline->annotated)
4354 28315671 2019-08-14 stsp return NULL;
4355 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4356 28315671 2019-08-14 stsp if (err)
4357 28315671 2019-08-14 stsp return err;
4358 82f6abb8 2019-08-14 stsp
4359 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4360 82f6abb8 2019-08-14 stsp if (err)
4361 82f6abb8 2019-08-14 stsp goto done;
4362 82f6abb8 2019-08-14 stsp
4363 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4364 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4365 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4366 bcb49d15 2019-08-14 stsp goto done;
4367 bcb49d15 2019-08-14 stsp }
4368 bcb49d15 2019-08-14 stsp
4369 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4370 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4371 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4372 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4373 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4374 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4375 82f6abb8 2019-08-14 stsp goto done;
4376 82f6abb8 2019-08-14 stsp }
4377 28315671 2019-08-14 stsp bline->annotated = 1;
4378 28315671 2019-08-14 stsp
4379 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4380 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4381 28315671 2019-08-14 stsp if (!bline->annotated)
4382 82f6abb8 2019-08-14 stsp goto done;
4383 28315671 2019-08-14 stsp
4384 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4385 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4386 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4387 82f6abb8 2019-08-14 stsp goto done;
4388 82f6abb8 2019-08-14 stsp }
4389 28315671 2019-08-14 stsp
4390 28315671 2019-08-14 stsp while (bline->annotated) {
4391 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4392 82f6abb8 2019-08-14 stsp size_t len;
4393 82f6abb8 2019-08-14 stsp
4394 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4395 28315671 2019-08-14 stsp if (ferror(a->f))
4396 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4397 28315671 2019-08-14 stsp break;
4398 28315671 2019-08-14 stsp }
4399 82f6abb8 2019-08-14 stsp
4400 82f6abb8 2019-08-14 stsp committer = bline->committer;
4401 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4402 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4403 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4404 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4405 82f6abb8 2019-08-14 stsp if (at)
4406 82f6abb8 2019-08-14 stsp *at = '\0';
4407 82f6abb8 2019-08-14 stsp len = strlen(committer);
4408 82f6abb8 2019-08-14 stsp if (len >= 9)
4409 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4410 28315671 2019-08-14 stsp
4411 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4412 28315671 2019-08-14 stsp if (nl)
4413 28315671 2019-08-14 stsp *nl = '\0';
4414 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4415 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4416 28315671 2019-08-14 stsp
4417 28315671 2019-08-14 stsp a->lineno_cur++;
4418 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4419 28315671 2019-08-14 stsp }
4420 82f6abb8 2019-08-14 stsp done:
4421 82f6abb8 2019-08-14 stsp if (commit)
4422 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4423 28315671 2019-08-14 stsp free(line);
4424 28315671 2019-08-14 stsp return err;
4425 28315671 2019-08-14 stsp }
4426 28315671 2019-08-14 stsp
4427 28315671 2019-08-14 stsp static const struct got_error *
4428 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4429 404c43c4 2018-06-21 stsp {
4430 404c43c4 2018-06-21 stsp const struct got_error *error;
4431 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4432 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4433 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4434 0587e10c 2020-07-23 stsp char *link_target = NULL;
4435 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4436 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4437 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4438 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4439 28315671 2019-08-14 stsp struct blame_cb_args bca;
4440 28315671 2019-08-14 stsp int ch, obj_type, i;
4441 be659d10 2020-11-18 stsp off_t filesize;
4442 90f3c347 2019-08-19 stsp
4443 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4444 404c43c4 2018-06-21 stsp
4445 404c43c4 2018-06-21 stsp #ifndef PROFILE
4446 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4447 36e2fb66 2019-01-04 stsp NULL) == -1)
4448 404c43c4 2018-06-21 stsp err(1, "pledge");
4449 404c43c4 2018-06-21 stsp #endif
4450 404c43c4 2018-06-21 stsp
4451 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4452 404c43c4 2018-06-21 stsp switch (ch) {
4453 404c43c4 2018-06-21 stsp case 'c':
4454 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4455 404c43c4 2018-06-21 stsp break;
4456 66bea077 2018-08-02 stsp case 'r':
4457 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4458 66bea077 2018-08-02 stsp if (repo_path == NULL)
4459 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4460 9ba1d308 2019-10-21 stsp optarg);
4461 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4462 66bea077 2018-08-02 stsp break;
4463 404c43c4 2018-06-21 stsp default:
4464 2deda0b9 2019-03-07 stsp usage_blame();
4465 404c43c4 2018-06-21 stsp /* NOTREACHED */
4466 404c43c4 2018-06-21 stsp }
4467 404c43c4 2018-06-21 stsp }
4468 404c43c4 2018-06-21 stsp
4469 404c43c4 2018-06-21 stsp argc -= optind;
4470 404c43c4 2018-06-21 stsp argv += optind;
4471 404c43c4 2018-06-21 stsp
4472 a39318fd 2018-08-02 stsp if (argc == 1)
4473 404c43c4 2018-06-21 stsp path = argv[0];
4474 a39318fd 2018-08-02 stsp else
4475 404c43c4 2018-06-21 stsp usage_blame();
4476 404c43c4 2018-06-21 stsp
4477 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4478 66bea077 2018-08-02 stsp if (cwd == NULL) {
4479 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4480 66bea077 2018-08-02 stsp goto done;
4481 66bea077 2018-08-02 stsp }
4482 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4483 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4484 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4485 66bea077 2018-08-02 stsp goto done;
4486 0c06baac 2019-02-05 stsp else
4487 0c06baac 2019-02-05 stsp error = NULL;
4488 0c06baac 2019-02-05 stsp if (worktree) {
4489 0c06baac 2019-02-05 stsp repo_path =
4490 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4491 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4492 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4493 1f03b8da 2020-03-20 stsp if (error)
4494 1f03b8da 2020-03-20 stsp goto done;
4495 1f03b8da 2020-03-20 stsp }
4496 0c06baac 2019-02-05 stsp } else {
4497 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4498 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4499 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4500 0c06baac 2019-02-05 stsp goto done;
4501 0c06baac 2019-02-05 stsp }
4502 66bea077 2018-08-02 stsp }
4503 66bea077 2018-08-02 stsp }
4504 36e2fb66 2019-01-04 stsp
4505 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4506 c02c541e 2019-03-29 stsp if (error != NULL)
4507 404c43c4 2018-06-21 stsp goto done;
4508 404c43c4 2018-06-21 stsp
4509 0c06baac 2019-02-05 stsp if (worktree) {
4510 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4511 01740607 2020-11-04 stsp char *p;
4512 01740607 2020-11-04 stsp
4513 01740607 2020-11-04 stsp error = got_worktree_resolve_path(&p, worktree, path);
4514 01740607 2020-11-04 stsp if (error)
4515 01740607 2020-11-04 stsp goto done;
4516 01740607 2020-11-04 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4517 01740607 2020-11-04 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4518 01740607 2020-11-04 stsp p) == -1) {
4519 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4520 01740607 2020-11-04 stsp free(p);
4521 0c06baac 2019-02-05 stsp goto done;
4522 0c06baac 2019-02-05 stsp }
4523 0c06baac 2019-02-05 stsp free(p);
4524 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4525 0c06baac 2019-02-05 stsp } else {
4526 01740607 2020-11-04 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4527 01740607 2020-11-04 stsp if (error)
4528 01740607 2020-11-04 stsp goto done;
4529 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4530 0c06baac 2019-02-05 stsp }
4531 0c06baac 2019-02-05 stsp if (error)
4532 66bea077 2018-08-02 stsp goto done;
4533 66bea077 2018-08-02 stsp
4534 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4535 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4536 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4537 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4538 404c43c4 2018-06-21 stsp if (error != NULL)
4539 66bea077 2018-08-02 stsp goto done;
4540 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4541 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4542 404c43c4 2018-06-21 stsp if (error != NULL)
4543 66bea077 2018-08-02 stsp goto done;
4544 404c43c4 2018-06-21 stsp } else {
4545 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4546 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4547 30837e32 2019-07-25 stsp if (error)
4548 66bea077 2018-08-02 stsp goto done;
4549 404c43c4 2018-06-21 stsp }
4550 404c43c4 2018-06-21 stsp
4551 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4552 0587e10c 2020-07-23 stsp commit_id, repo);
4553 0587e10c 2020-07-23 stsp if (error)
4554 0587e10c 2020-07-23 stsp goto done;
4555 0587e10c 2020-07-23 stsp
4556 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4557 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4558 28315671 2019-08-14 stsp if (error)
4559 28315671 2019-08-14 stsp goto done;
4560 28315671 2019-08-14 stsp
4561 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4562 28315671 2019-08-14 stsp if (error)
4563 28315671 2019-08-14 stsp goto done;
4564 28315671 2019-08-14 stsp
4565 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4566 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4567 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4568 28315671 2019-08-14 stsp goto done;
4569 28315671 2019-08-14 stsp }
4570 28315671 2019-08-14 stsp
4571 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4572 28315671 2019-08-14 stsp if (error)
4573 28315671 2019-08-14 stsp goto done;
4574 28315671 2019-08-14 stsp bca.f = got_opentemp();
4575 28315671 2019-08-14 stsp if (bca.f == NULL) {
4576 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4577 28315671 2019-08-14 stsp goto done;
4578 28315671 2019-08-14 stsp }
4579 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4580 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4581 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4582 28315671 2019-08-14 stsp goto done;
4583 28315671 2019-08-14 stsp
4584 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4585 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4586 b02560ec 2019-08-19 stsp bca.nlines--;
4587 b02560ec 2019-08-19 stsp
4588 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4589 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4590 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4591 28315671 2019-08-14 stsp goto done;
4592 28315671 2019-08-14 stsp }
4593 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4594 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4595 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4596 7ef28ff8 2019-08-14 stsp while (i > 0) {
4597 7ef28ff8 2019-08-14 stsp i /= 10;
4598 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4599 7ef28ff8 2019-08-14 stsp }
4600 82f6abb8 2019-08-14 stsp bca.repo = repo;
4601 7ef28ff8 2019-08-14 stsp
4602 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4603 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4604 404c43c4 2018-06-21 stsp done:
4605 66bea077 2018-08-02 stsp free(in_repo_path);
4606 0587e10c 2020-07-23 stsp free(link_target);
4607 66bea077 2018-08-02 stsp free(repo_path);
4608 66bea077 2018-08-02 stsp free(cwd);
4609 404c43c4 2018-06-21 stsp free(commit_id);
4610 28315671 2019-08-14 stsp free(obj_id);
4611 28315671 2019-08-14 stsp if (blob)
4612 28315671 2019-08-14 stsp got_object_blob_close(blob);
4613 0c06baac 2019-02-05 stsp if (worktree)
4614 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4615 ad242220 2018-09-08 stsp if (repo) {
4616 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4617 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4618 ad242220 2018-09-08 stsp if (error == NULL)
4619 ad242220 2018-09-08 stsp error = repo_error;
4620 ad242220 2018-09-08 stsp }
4621 3affba96 2019-09-22 stsp if (bca.lines) {
4622 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4623 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4624 3affba96 2019-09-22 stsp free(bline->id_str);
4625 3affba96 2019-09-22 stsp free(bline->committer);
4626 3affba96 2019-09-22 stsp }
4627 3affba96 2019-09-22 stsp free(bca.lines);
4628 28315671 2019-08-14 stsp }
4629 28315671 2019-08-14 stsp free(bca.line_offsets);
4630 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4631 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4632 404c43c4 2018-06-21 stsp return error;
4633 5de5890b 2018-10-18 stsp }
4634 5de5890b 2018-10-18 stsp
4635 5de5890b 2018-10-18 stsp __dead static void
4636 5de5890b 2018-10-18 stsp usage_tree(void)
4637 5de5890b 2018-10-18 stsp {
4638 c1669e2e 2019-01-09 stsp fprintf(stderr,
4639 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4640 5de5890b 2018-10-18 stsp getprogname());
4641 5de5890b 2018-10-18 stsp exit(1);
4642 5de5890b 2018-10-18 stsp }
4643 5de5890b 2018-10-18 stsp
4644 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4645 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4646 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4647 c1669e2e 2019-01-09 stsp {
4648 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4649 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4650 848d6979 2019-08-12 stsp const char *modestr = "";
4651 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4652 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4653 5de5890b 2018-10-18 stsp
4654 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4655 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4656 c1669e2e 2019-01-09 stsp path++;
4657 c1669e2e 2019-01-09 stsp
4658 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4659 63c5ca5d 2019-08-24 stsp modestr = "$";
4660 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4661 0d6c6ee3 2020-05-20 stsp int i;
4662 0d6c6ee3 2020-05-20 stsp
4663 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4664 0d6c6ee3 2020-05-20 stsp if (err)
4665 0d6c6ee3 2020-05-20 stsp return err;
4666 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4667 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4668 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4669 0d6c6ee3 2020-05-20 stsp }
4670 0d6c6ee3 2020-05-20 stsp
4671 848d6979 2019-08-12 stsp modestr = "@";
4672 0d6c6ee3 2020-05-20 stsp }
4673 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4674 848d6979 2019-08-12 stsp modestr = "/";
4675 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4676 848d6979 2019-08-12 stsp modestr = "*";
4677 848d6979 2019-08-12 stsp
4678 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4679 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4680 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4681 0d6c6ee3 2020-05-20 stsp
4682 0d6c6ee3 2020-05-20 stsp free(link_target);
4683 0d6c6ee3 2020-05-20 stsp return NULL;
4684 c1669e2e 2019-01-09 stsp }
4685 c1669e2e 2019-01-09 stsp
4686 5de5890b 2018-10-18 stsp static const struct got_error *
4687 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4688 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4689 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4690 5de5890b 2018-10-18 stsp {
4691 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4692 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4693 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4694 56e0773d 2019-11-28 stsp int nentries, i;
4695 5de5890b 2018-10-18 stsp
4696 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4697 5de5890b 2018-10-18 stsp if (err)
4698 5de5890b 2018-10-18 stsp goto done;
4699 5de5890b 2018-10-18 stsp
4700 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4701 5de5890b 2018-10-18 stsp if (err)
4702 5de5890b 2018-10-18 stsp goto done;
4703 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4704 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4705 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4706 5de5890b 2018-10-18 stsp char *id = NULL;
4707 84453469 2018-11-11 stsp
4708 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4709 84453469 2018-11-11 stsp break;
4710 84453469 2018-11-11 stsp
4711 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4712 5de5890b 2018-10-18 stsp if (show_ids) {
4713 5de5890b 2018-10-18 stsp char *id_str;
4714 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4715 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4716 5de5890b 2018-10-18 stsp if (err)
4717 5de5890b 2018-10-18 stsp goto done;
4718 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4719 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4720 5de5890b 2018-10-18 stsp free(id_str);
4721 5de5890b 2018-10-18 stsp goto done;
4722 5de5890b 2018-10-18 stsp }
4723 5de5890b 2018-10-18 stsp free(id_str);
4724 5de5890b 2018-10-18 stsp }
4725 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4726 5de5890b 2018-10-18 stsp free(id);
4727 0d6c6ee3 2020-05-20 stsp if (err)
4728 0d6c6ee3 2020-05-20 stsp goto done;
4729 c1669e2e 2019-01-09 stsp
4730 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4731 c1669e2e 2019-01-09 stsp char *child_path;
4732 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4733 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4734 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4735 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4736 c1669e2e 2019-01-09 stsp goto done;
4737 c1669e2e 2019-01-09 stsp }
4738 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4739 c1669e2e 2019-01-09 stsp root_path, repo);
4740 c1669e2e 2019-01-09 stsp free(child_path);
4741 c1669e2e 2019-01-09 stsp if (err)
4742 c1669e2e 2019-01-09 stsp goto done;
4743 c1669e2e 2019-01-09 stsp }
4744 5de5890b 2018-10-18 stsp }
4745 5de5890b 2018-10-18 stsp done:
4746 5de5890b 2018-10-18 stsp if (tree)
4747 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4748 5de5890b 2018-10-18 stsp free(tree_id);
4749 5de5890b 2018-10-18 stsp return err;
4750 404c43c4 2018-06-21 stsp }
4751 404c43c4 2018-06-21 stsp
4752 5de5890b 2018-10-18 stsp static const struct got_error *
4753 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4754 5de5890b 2018-10-18 stsp {
4755 5de5890b 2018-10-18 stsp const struct got_error *error;
4756 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4757 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4758 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4759 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4760 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4761 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4762 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4763 5de5890b 2018-10-18 stsp int ch;
4764 5de5890b 2018-10-18 stsp
4765 5de5890b 2018-10-18 stsp #ifndef PROFILE
4766 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4767 0f8d269b 2019-01-04 stsp NULL) == -1)
4768 5de5890b 2018-10-18 stsp err(1, "pledge");
4769 5de5890b 2018-10-18 stsp #endif
4770 5de5890b 2018-10-18 stsp
4771 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4772 5de5890b 2018-10-18 stsp switch (ch) {
4773 5de5890b 2018-10-18 stsp case 'c':
4774 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4775 5de5890b 2018-10-18 stsp break;
4776 5de5890b 2018-10-18 stsp case 'r':
4777 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4778 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4779 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4780 9ba1d308 2019-10-21 stsp optarg);
4781 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4782 5de5890b 2018-10-18 stsp break;
4783 5de5890b 2018-10-18 stsp case 'i':
4784 5de5890b 2018-10-18 stsp show_ids = 1;
4785 5de5890b 2018-10-18 stsp break;
4786 c1669e2e 2019-01-09 stsp case 'R':
4787 c1669e2e 2019-01-09 stsp recurse = 1;
4788 c1669e2e 2019-01-09 stsp break;
4789 5de5890b 2018-10-18 stsp default:
4790 2deda0b9 2019-03-07 stsp usage_tree();
4791 5de5890b 2018-10-18 stsp /* NOTREACHED */
4792 5de5890b 2018-10-18 stsp }
4793 5de5890b 2018-10-18 stsp }
4794 5de5890b 2018-10-18 stsp
4795 5de5890b 2018-10-18 stsp argc -= optind;
4796 5de5890b 2018-10-18 stsp argv += optind;
4797 5de5890b 2018-10-18 stsp
4798 5de5890b 2018-10-18 stsp if (argc == 1)
4799 5de5890b 2018-10-18 stsp path = argv[0];
4800 5de5890b 2018-10-18 stsp else if (argc > 1)
4801 5de5890b 2018-10-18 stsp usage_tree();
4802 5de5890b 2018-10-18 stsp else
4803 7a2c19d6 2019-02-05 stsp path = NULL;
4804 7a2c19d6 2019-02-05 stsp
4805 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4806 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4807 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4808 9bf7a39b 2019-02-05 stsp goto done;
4809 9bf7a39b 2019-02-05 stsp }
4810 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4811 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4812 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4813 7a2c19d6 2019-02-05 stsp goto done;
4814 7a2c19d6 2019-02-05 stsp else
4815 7a2c19d6 2019-02-05 stsp error = NULL;
4816 7a2c19d6 2019-02-05 stsp if (worktree) {
4817 7a2c19d6 2019-02-05 stsp repo_path =
4818 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4819 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4820 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4821 7a2c19d6 2019-02-05 stsp if (error)
4822 7a2c19d6 2019-02-05 stsp goto done;
4823 7a2c19d6 2019-02-05 stsp } else {
4824 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4825 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4826 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4827 7a2c19d6 2019-02-05 stsp goto done;
4828 7a2c19d6 2019-02-05 stsp }
4829 5de5890b 2018-10-18 stsp }
4830 5de5890b 2018-10-18 stsp }
4831 5de5890b 2018-10-18 stsp
4832 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4833 5de5890b 2018-10-18 stsp if (error != NULL)
4834 c02c541e 2019-03-29 stsp goto done;
4835 c02c541e 2019-03-29 stsp
4836 4fedbf4c 2020-11-07 stsp if (worktree) {
4837 4fedbf4c 2020-11-07 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4838 4fedbf4c 2020-11-07 stsp char *p;
4839 5de5890b 2018-10-18 stsp
4840 4fedbf4c 2020-11-07 stsp if (path == NULL)
4841 4fedbf4c 2020-11-07 stsp path = "";
4842 4fedbf4c 2020-11-07 stsp error = got_worktree_resolve_path(&p, worktree, path);
4843 4fedbf4c 2020-11-07 stsp if (error)
4844 4fedbf4c 2020-11-07 stsp goto done;
4845 4fedbf4c 2020-11-07 stsp if (asprintf(&in_repo_path, "%s%s%s", prefix,
4846 4fedbf4c 2020-11-07 stsp (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
4847 4fedbf4c 2020-11-07 stsp p) == -1) {
4848 4fedbf4c 2020-11-07 stsp error = got_error_from_errno("asprintf");
4849 9bf7a39b 2019-02-05 stsp free(p);
4850 4fedbf4c 2020-11-07 stsp goto done;
4851 4fedbf4c 2020-11-07 stsp }
4852 4fedbf4c 2020-11-07 stsp free(p);
4853 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4854 4fedbf4c 2020-11-07 stsp if (error)
4855 4fedbf4c 2020-11-07 stsp goto done;
4856 4fedbf4c 2020-11-07 stsp } else {
4857 4fedbf4c 2020-11-07 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4858 4fedbf4c 2020-11-07 stsp if (error)
4859 4fedbf4c 2020-11-07 stsp goto done;
4860 4fedbf4c 2020-11-07 stsp if (path == NULL)
4861 9bf7a39b 2019-02-05 stsp path = "/";
4862 8fa913ec 2020-11-14 stsp error = got_repo_map_path(&in_repo_path, repo, path);
4863 9bf7a39b 2019-02-05 stsp if (error != NULL)
4864 9bf7a39b 2019-02-05 stsp goto done;
4865 9bf7a39b 2019-02-05 stsp }
4866 5de5890b 2018-10-18 stsp
4867 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4868 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4869 4e0a20a4 2020-03-23 tracey if (worktree)
4870 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4871 4e0a20a4 2020-03-23 tracey else
4872 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4873 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4874 5de5890b 2018-10-18 stsp if (error != NULL)
4875 5de5890b 2018-10-18 stsp goto done;
4876 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4877 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4878 5de5890b 2018-10-18 stsp if (error != NULL)
4879 5de5890b 2018-10-18 stsp goto done;
4880 5de5890b 2018-10-18 stsp } else {
4881 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4882 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4883 30837e32 2019-07-25 stsp if (error)
4884 5de5890b 2018-10-18 stsp goto done;
4885 5de5890b 2018-10-18 stsp }
4886 5de5890b 2018-10-18 stsp
4887 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4888 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4889 5de5890b 2018-10-18 stsp done:
4890 5de5890b 2018-10-18 stsp free(in_repo_path);
4891 5de5890b 2018-10-18 stsp free(repo_path);
4892 5de5890b 2018-10-18 stsp free(cwd);
4893 5de5890b 2018-10-18 stsp free(commit_id);
4894 7a2c19d6 2019-02-05 stsp if (worktree)
4895 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4896 5de5890b 2018-10-18 stsp if (repo) {
4897 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4898 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4899 5de5890b 2018-10-18 stsp if (error == NULL)
4900 5de5890b 2018-10-18 stsp error = repo_error;
4901 5de5890b 2018-10-18 stsp }
4902 5de5890b 2018-10-18 stsp return error;
4903 5de5890b 2018-10-18 stsp }
4904 5de5890b 2018-10-18 stsp
4905 6bad629b 2019-02-04 stsp __dead static void
4906 6bad629b 2019-02-04 stsp usage_status(void)
4907 6bad629b 2019-02-04 stsp {
4908 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4909 081470ac 2020-08-13 stsp getprogname());
4910 6bad629b 2019-02-04 stsp exit(1);
4911 6bad629b 2019-02-04 stsp }
4912 5c860e29 2018-03-12 stsp
4913 b72f483a 2019-02-05 stsp static const struct got_error *
4914 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4915 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4916 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4917 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4918 6bad629b 2019-02-04 stsp {
4919 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4920 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4921 081470ac 2020-08-13 stsp if (arg) {
4922 081470ac 2020-08-13 stsp char *status_codes = arg;
4923 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
4924 081470ac 2020-08-13 stsp int i;
4925 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4926 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
4927 081470ac 2020-08-13 stsp staged_status == status_codes[i])
4928 081470ac 2020-08-13 stsp break;
4929 081470ac 2020-08-13 stsp }
4930 081470ac 2020-08-13 stsp if (i == ncodes)
4931 081470ac 2020-08-13 stsp return NULL;
4932 081470ac 2020-08-13 stsp }
4933 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4934 b72f483a 2019-02-05 stsp return NULL;
4935 6bad629b 2019-02-04 stsp }
4936 5c860e29 2018-03-12 stsp
4937 6bad629b 2019-02-04 stsp static const struct got_error *
4938 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4939 6bad629b 2019-02-04 stsp {
4940 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4941 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4942 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4943 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
4944 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4945 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4946 081470ac 2020-08-13 stsp int ch, i;
4947 5c860e29 2018-03-12 stsp
4948 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4949 72ea6654 2019-07-27 stsp
4950 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4951 6bad629b 2019-02-04 stsp switch (ch) {
4952 081470ac 2020-08-13 stsp case 's':
4953 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
4954 081470ac 2020-08-13 stsp switch (optarg[i]) {
4955 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
4956 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
4957 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
4958 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
4959 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
4960 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
4961 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
4962 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
4963 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
4964 081470ac 2020-08-13 stsp break;
4965 081470ac 2020-08-13 stsp default:
4966 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
4967 081470ac 2020-08-13 stsp optarg[i]);
4968 081470ac 2020-08-13 stsp }
4969 081470ac 2020-08-13 stsp }
4970 081470ac 2020-08-13 stsp status_codes = optarg;
4971 081470ac 2020-08-13 stsp break;
4972 5c860e29 2018-03-12 stsp default:
4973 2deda0b9 2019-03-07 stsp usage_status();
4974 6bad629b 2019-02-04 stsp /* NOTREACHED */
4975 5c860e29 2018-03-12 stsp }
4976 5c860e29 2018-03-12 stsp }
4977 5c860e29 2018-03-12 stsp
4978 6bad629b 2019-02-04 stsp argc -= optind;
4979 6bad629b 2019-02-04 stsp argv += optind;
4980 5c860e29 2018-03-12 stsp
4981 6bad629b 2019-02-04 stsp #ifndef PROFILE
4982 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4983 6bad629b 2019-02-04 stsp NULL) == -1)
4984 6bad629b 2019-02-04 stsp err(1, "pledge");
4985 f42b1b34 2018-03-12 stsp #endif
4986 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4987 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4988 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4989 927df6b7 2019-02-10 stsp goto done;
4990 927df6b7 2019-02-10 stsp }
4991 927df6b7 2019-02-10 stsp
4992 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4993 fa51e947 2020-03-27 stsp if (error) {
4994 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4995 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4996 a5edda0a 2019-07-27 stsp goto done;
4997 fa51e947 2020-03-27 stsp }
4998 6bad629b 2019-02-04 stsp
4999 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5000 c9956ddf 2019-09-08 stsp NULL);
5001 6bad629b 2019-02-04 stsp if (error != NULL)
5002 6bad629b 2019-02-04 stsp goto done;
5003 6bad629b 2019-02-04 stsp
5004 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5005 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5006 087fb88c 2019-08-04 stsp if (error)
5007 087fb88c 2019-08-04 stsp goto done;
5008 087fb88c 2019-08-04 stsp
5009 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5010 6bad629b 2019-02-04 stsp if (error)
5011 6bad629b 2019-02-04 stsp goto done;
5012 6bad629b 2019-02-04 stsp
5013 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
5014 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
5015 6bad629b 2019-02-04 stsp done:
5016 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
5017 a5edda0a 2019-07-27 stsp free((char *)pe->path);
5018 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
5019 927df6b7 2019-02-10 stsp free(cwd);
5020 d0eebce4 2019-03-11 stsp return error;
5021 d0eebce4 2019-03-11 stsp }
5022 d0eebce4 2019-03-11 stsp
5023 d0eebce4 2019-03-11 stsp __dead static void
5024 d0eebce4 2019-03-11 stsp usage_ref(void)
5025 d0eebce4 2019-03-11 stsp {
5026 d0eebce4 2019-03-11 stsp fprintf(stderr,
5027 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5028 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5029 d0eebce4 2019-03-11 stsp getprogname());
5030 d0eebce4 2019-03-11 stsp exit(1);
5031 d0eebce4 2019-03-11 stsp }
5032 d0eebce4 2019-03-11 stsp
5033 d0eebce4 2019-03-11 stsp static const struct got_error *
5034 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5035 d0eebce4 2019-03-11 stsp {
5036 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5037 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5038 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5039 d0eebce4 2019-03-11 stsp
5040 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
5041 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5042 d0eebce4 2019-03-11 stsp if (err)
5043 d0eebce4 2019-03-11 stsp return err;
5044 d0eebce4 2019-03-11 stsp
5045 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5046 d0eebce4 2019-03-11 stsp char *refstr;
5047 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5048 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5049 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5050 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5051 d0eebce4 2019-03-11 stsp free(refstr);
5052 d0eebce4 2019-03-11 stsp }
5053 d0eebce4 2019-03-11 stsp
5054 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5055 d0eebce4 2019-03-11 stsp return NULL;
5056 d0eebce4 2019-03-11 stsp }
5057 d0eebce4 2019-03-11 stsp
5058 d0eebce4 2019-03-11 stsp static const struct got_error *
5059 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
5060 d0eebce4 2019-03-11 stsp {
5061 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5062 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5063 d0eebce4 2019-03-11 stsp
5064 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5065 d0eebce4 2019-03-11 stsp if (err)
5066 d0eebce4 2019-03-11 stsp return err;
5067 d0eebce4 2019-03-11 stsp
5068 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
5069 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5070 d0eebce4 2019-03-11 stsp return err;
5071 d0eebce4 2019-03-11 stsp }
5072 d0eebce4 2019-03-11 stsp
5073 d0eebce4 2019-03-11 stsp static const struct got_error *
5074 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5075 d0eebce4 2019-03-11 stsp {
5076 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5077 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5078 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5079 d1644381 2019-07-14 stsp
5080 d1644381 2019-07-14 stsp /*
5081 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5082 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5083 d1644381 2019-07-14 stsp * an unintended typo.
5084 d1644381 2019-07-14 stsp */
5085 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5086 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5087 d0eebce4 2019-03-11 stsp
5088 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5089 dd88155e 2019-06-29 stsp repo);
5090 d83d9d5c 2019-05-13 stsp if (err) {
5091 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5092 d0eebce4 2019-03-11 stsp
5093 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5094 d83d9d5c 2019-05-13 stsp return err;
5095 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5096 d83d9d5c 2019-05-13 stsp if (err)
5097 d83d9d5c 2019-05-13 stsp return err;
5098 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5099 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5100 d83d9d5c 2019-05-13 stsp if (err)
5101 d83d9d5c 2019-05-13 stsp return err;
5102 d83d9d5c 2019-05-13 stsp }
5103 d83d9d5c 2019-05-13 stsp
5104 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5105 d0eebce4 2019-03-11 stsp if (err)
5106 d0eebce4 2019-03-11 stsp goto done;
5107 d0eebce4 2019-03-11 stsp
5108 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5109 d0eebce4 2019-03-11 stsp done:
5110 d0eebce4 2019-03-11 stsp if (ref)
5111 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5112 d0eebce4 2019-03-11 stsp free(id);
5113 d1c1ae5f 2019-08-12 stsp return err;
5114 d1c1ae5f 2019-08-12 stsp }
5115 d1c1ae5f 2019-08-12 stsp
5116 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5117 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5118 d1c1ae5f 2019-08-12 stsp {
5119 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5120 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5121 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5122 d1c1ae5f 2019-08-12 stsp
5123 d1c1ae5f 2019-08-12 stsp /*
5124 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5125 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5126 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5127 d1c1ae5f 2019-08-12 stsp */
5128 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5129 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5130 d1c1ae5f 2019-08-12 stsp
5131 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5132 d1c1ae5f 2019-08-12 stsp if (err)
5133 d1c1ae5f 2019-08-12 stsp return err;
5134 d1c1ae5f 2019-08-12 stsp
5135 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5136 d1c1ae5f 2019-08-12 stsp if (err)
5137 d1c1ae5f 2019-08-12 stsp goto done;
5138 d1c1ae5f 2019-08-12 stsp
5139 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5140 d1c1ae5f 2019-08-12 stsp done:
5141 d1c1ae5f 2019-08-12 stsp if (target_ref)
5142 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5143 d1c1ae5f 2019-08-12 stsp if (ref)
5144 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5145 d0eebce4 2019-03-11 stsp return err;
5146 d0eebce4 2019-03-11 stsp }
5147 d0eebce4 2019-03-11 stsp
5148 d0eebce4 2019-03-11 stsp static const struct got_error *
5149 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5150 d0eebce4 2019-03-11 stsp {
5151 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5152 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5153 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5154 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5155 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5156 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5157 b2070a3f 2020-03-22 stsp char *refname = NULL;
5158 d0eebce4 2019-03-11 stsp
5159 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5160 d0eebce4 2019-03-11 stsp switch (ch) {
5161 e31abbf2 2020-03-22 stsp case 'c':
5162 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5163 e31abbf2 2020-03-22 stsp break;
5164 d0eebce4 2019-03-11 stsp case 'd':
5165 e31abbf2 2020-03-22 stsp do_delete = 1;
5166 d0eebce4 2019-03-11 stsp break;
5167 d0eebce4 2019-03-11 stsp case 'r':
5168 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5169 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5170 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5171 9ba1d308 2019-10-21 stsp optarg);
5172 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5173 d0eebce4 2019-03-11 stsp break;
5174 d0eebce4 2019-03-11 stsp case 'l':
5175 d0eebce4 2019-03-11 stsp do_list = 1;
5176 d1c1ae5f 2019-08-12 stsp break;
5177 d1c1ae5f 2019-08-12 stsp case 's':
5178 e31abbf2 2020-03-22 stsp symref_target = optarg;
5179 d0eebce4 2019-03-11 stsp break;
5180 d0eebce4 2019-03-11 stsp default:
5181 d0eebce4 2019-03-11 stsp usage_ref();
5182 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5183 d0eebce4 2019-03-11 stsp }
5184 d0eebce4 2019-03-11 stsp }
5185 d0eebce4 2019-03-11 stsp
5186 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5187 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
5188 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
5189 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
5190 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5191 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
5192 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5193 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
5194 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5195 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
5196 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5197 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
5198 d0eebce4 2019-03-11 stsp
5199 d0eebce4 2019-03-11 stsp argc -= optind;
5200 d0eebce4 2019-03-11 stsp argv += optind;
5201 d0eebce4 2019-03-11 stsp
5202 e31abbf2 2020-03-22 stsp if (do_list) {
5203 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5204 d0eebce4 2019-03-11 stsp usage_ref();
5205 b2070a3f 2020-03-22 stsp if (argc == 1) {
5206 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5207 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5208 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5209 b2070a3f 2020-03-22 stsp goto done;
5210 b2070a3f 2020-03-22 stsp }
5211 b2070a3f 2020-03-22 stsp }
5212 e31abbf2 2020-03-22 stsp } else {
5213 e31abbf2 2020-03-22 stsp if (argc != 1)
5214 e31abbf2 2020-03-22 stsp usage_ref();
5215 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5216 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5217 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5218 b2070a3f 2020-03-22 stsp goto done;
5219 b2070a3f 2020-03-22 stsp }
5220 e31abbf2 2020-03-22 stsp }
5221 b2070a3f 2020-03-22 stsp
5222 b2070a3f 2020-03-22 stsp if (refname)
5223 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5224 d0eebce4 2019-03-11 stsp
5225 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5226 e0b57350 2019-03-12 stsp if (do_list) {
5227 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5228 e0b57350 2019-03-12 stsp NULL) == -1)
5229 e0b57350 2019-03-12 stsp err(1, "pledge");
5230 e0b57350 2019-03-12 stsp } else {
5231 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5232 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5233 e0b57350 2019-03-12 stsp err(1, "pledge");
5234 e0b57350 2019-03-12 stsp }
5235 d0eebce4 2019-03-11 stsp #endif
5236 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5237 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5238 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5239 d0eebce4 2019-03-11 stsp goto done;
5240 d0eebce4 2019-03-11 stsp }
5241 d0eebce4 2019-03-11 stsp
5242 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5243 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5244 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5245 d0eebce4 2019-03-11 stsp goto done;
5246 d0eebce4 2019-03-11 stsp else
5247 d0eebce4 2019-03-11 stsp error = NULL;
5248 d0eebce4 2019-03-11 stsp if (worktree) {
5249 d0eebce4 2019-03-11 stsp repo_path =
5250 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5251 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5252 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5253 d0eebce4 2019-03-11 stsp if (error)
5254 d0eebce4 2019-03-11 stsp goto done;
5255 d0eebce4 2019-03-11 stsp } else {
5256 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5257 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5258 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5259 d0eebce4 2019-03-11 stsp goto done;
5260 d0eebce4 2019-03-11 stsp }
5261 d0eebce4 2019-03-11 stsp }
5262 d0eebce4 2019-03-11 stsp }
5263 d0eebce4 2019-03-11 stsp
5264 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5265 d0eebce4 2019-03-11 stsp if (error != NULL)
5266 d0eebce4 2019-03-11 stsp goto done;
5267 d0eebce4 2019-03-11 stsp
5268 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5269 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5270 c02c541e 2019-03-29 stsp if (error)
5271 c02c541e 2019-03-29 stsp goto done;
5272 c02c541e 2019-03-29 stsp
5273 d0eebce4 2019-03-11 stsp if (do_list)
5274 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5275 e31abbf2 2020-03-22 stsp else if (do_delete)
5276 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5277 e31abbf2 2020-03-22 stsp else if (symref_target)
5278 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5279 e31abbf2 2020-03-22 stsp else {
5280 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5281 e31abbf2 2020-03-22 stsp usage_ref();
5282 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5283 e31abbf2 2020-03-22 stsp }
5284 4e759de4 2019-06-26 stsp done:
5285 b2070a3f 2020-03-22 stsp free(refname);
5286 4e759de4 2019-06-26 stsp if (repo)
5287 4e759de4 2019-06-26 stsp got_repo_close(repo);
5288 4e759de4 2019-06-26 stsp if (worktree)
5289 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5290 4e759de4 2019-06-26 stsp free(cwd);
5291 4e759de4 2019-06-26 stsp free(repo_path);
5292 4e759de4 2019-06-26 stsp return error;
5293 4e759de4 2019-06-26 stsp }
5294 4e759de4 2019-06-26 stsp
5295 4e759de4 2019-06-26 stsp __dead static void
5296 4e759de4 2019-06-26 stsp usage_branch(void)
5297 4e759de4 2019-06-26 stsp {
5298 4e759de4 2019-06-26 stsp fprintf(stderr,
5299 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5300 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5301 4e759de4 2019-06-26 stsp exit(1);
5302 4e759de4 2019-06-26 stsp }
5303 4e759de4 2019-06-26 stsp
5304 4e759de4 2019-06-26 stsp static const struct got_error *
5305 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5306 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5307 4e99b47e 2019-10-04 stsp {
5308 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5309 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5310 4e99b47e 2019-10-04 stsp char *refstr;
5311 4e99b47e 2019-10-04 stsp
5312 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5313 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5314 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5315 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5316 4e99b47e 2019-10-04 stsp
5317 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5318 4e99b47e 2019-10-04 stsp if (err)
5319 4e99b47e 2019-10-04 stsp return err;
5320 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5321 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5322 4e99b47e 2019-10-04 stsp marker = "* ";
5323 4e99b47e 2019-10-04 stsp else
5324 4e99b47e 2019-10-04 stsp marker = "~ ";
5325 4e99b47e 2019-10-04 stsp free(id);
5326 4e99b47e 2019-10-04 stsp }
5327 4e99b47e 2019-10-04 stsp
5328 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5329 4e99b47e 2019-10-04 stsp refname += 11;
5330 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5331 4e99b47e 2019-10-04 stsp refname += 18;
5332 4e99b47e 2019-10-04 stsp
5333 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5334 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5335 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5336 4e99b47e 2019-10-04 stsp
5337 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5338 4e99b47e 2019-10-04 stsp free(refstr);
5339 4e99b47e 2019-10-04 stsp return NULL;
5340 4e99b47e 2019-10-04 stsp }
5341 4e99b47e 2019-10-04 stsp
5342 4e99b47e 2019-10-04 stsp static const struct got_error *
5343 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5344 ad89fa31 2019-10-04 stsp {
5345 ad89fa31 2019-10-04 stsp const char *refname;
5346 ad89fa31 2019-10-04 stsp
5347 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5348 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5349 ad89fa31 2019-10-04 stsp
5350 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5351 ad89fa31 2019-10-04 stsp
5352 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5353 ad89fa31 2019-10-04 stsp refname += 11;
5354 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5355 ad89fa31 2019-10-04 stsp refname += 18;
5356 ad89fa31 2019-10-04 stsp
5357 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5358 ad89fa31 2019-10-04 stsp
5359 ad89fa31 2019-10-04 stsp return NULL;
5360 ad89fa31 2019-10-04 stsp }
5361 ad89fa31 2019-10-04 stsp
5362 ad89fa31 2019-10-04 stsp static const struct got_error *
5363 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5364 4e759de4 2019-06-26 stsp {
5365 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5366 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5367 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5368 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5369 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5370 4e759de4 2019-06-26 stsp
5371 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5372 ba882ee3 2019-07-11 stsp
5373 4e99b47e 2019-10-04 stsp if (worktree) {
5374 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5375 4e99b47e 2019-10-04 stsp worktree);
5376 4e99b47e 2019-10-04 stsp if (err)
5377 4e99b47e 2019-10-04 stsp return err;
5378 4e99b47e 2019-10-04 stsp
5379 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5380 4e99b47e 2019-10-04 stsp worktree);
5381 4e99b47e 2019-10-04 stsp if (err)
5382 4e99b47e 2019-10-04 stsp return err;
5383 4e99b47e 2019-10-04 stsp
5384 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5385 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5386 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5387 4e99b47e 2019-10-04 stsp if (err)
5388 4e99b47e 2019-10-04 stsp return err;
5389 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5390 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5391 4e99b47e 2019-10-04 stsp }
5392 4e99b47e 2019-10-04 stsp }
5393 4e99b47e 2019-10-04 stsp
5394 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5395 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5396 4e759de4 2019-06-26 stsp if (err)
5397 4e759de4 2019-06-26 stsp return err;
5398 4e759de4 2019-06-26 stsp
5399 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5400 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5401 4e759de4 2019-06-26 stsp
5402 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5403 4e759de4 2019-06-26 stsp return NULL;
5404 4e759de4 2019-06-26 stsp }
5405 4e759de4 2019-06-26 stsp
5406 4e759de4 2019-06-26 stsp static const struct got_error *
5407 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5408 45cd4e47 2019-08-25 stsp const char *branch_name)
5409 4e759de4 2019-06-26 stsp {
5410 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5411 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5412 4e759de4 2019-06-26 stsp char *refname;
5413 4e759de4 2019-06-26 stsp
5414 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5415 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5416 4e759de4 2019-06-26 stsp
5417 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5418 4e759de4 2019-06-26 stsp if (err)
5419 4e759de4 2019-06-26 stsp goto done;
5420 4e759de4 2019-06-26 stsp
5421 45cd4e47 2019-08-25 stsp if (worktree &&
5422 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5423 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5424 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5425 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5426 45cd4e47 2019-08-25 stsp goto done;
5427 45cd4e47 2019-08-25 stsp }
5428 45cd4e47 2019-08-25 stsp
5429 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5430 4e759de4 2019-06-26 stsp done:
5431 45cd4e47 2019-08-25 stsp if (ref)
5432 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5433 4e759de4 2019-06-26 stsp free(refname);
5434 4e759de4 2019-06-26 stsp return err;
5435 4e759de4 2019-06-26 stsp }
5436 4e759de4 2019-06-26 stsp
5437 4e759de4 2019-06-26 stsp static const struct got_error *
5438 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5439 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5440 4e759de4 2019-06-26 stsp {
5441 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5442 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5443 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5444 d3f84d51 2019-07-11 stsp
5445 d3f84d51 2019-07-11 stsp /*
5446 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5447 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5448 d3f84d51 2019-07-11 stsp * an unintended typo.
5449 d3f84d51 2019-07-11 stsp */
5450 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5451 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5452 4e759de4 2019-06-26 stsp
5453 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5454 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5455 62d463ca 2020-10-20 naddy goto done;
5456 4e759de4 2019-06-26 stsp }
5457 4e759de4 2019-06-26 stsp
5458 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5459 4e759de4 2019-06-26 stsp if (err == NULL) {
5460 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5461 4e759de4 2019-06-26 stsp goto done;
5462 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5463 4e759de4 2019-06-26 stsp goto done;
5464 4e759de4 2019-06-26 stsp
5465 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5466 4e759de4 2019-06-26 stsp if (err)
5467 4e759de4 2019-06-26 stsp goto done;
5468 4e759de4 2019-06-26 stsp
5469 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5470 d0eebce4 2019-03-11 stsp done:
5471 4e759de4 2019-06-26 stsp if (ref)
5472 4e759de4 2019-06-26 stsp got_ref_close(ref);
5473 4e759de4 2019-06-26 stsp free(base_refname);
5474 4e759de4 2019-06-26 stsp free(refname);
5475 4e759de4 2019-06-26 stsp return err;
5476 4e759de4 2019-06-26 stsp }
5477 4e759de4 2019-06-26 stsp
5478 4e759de4 2019-06-26 stsp static const struct got_error *
5479 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5480 4e759de4 2019-06-26 stsp {
5481 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5482 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5483 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5484 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5485 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5486 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5487 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5488 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5489 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5490 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5491 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5492 4e759de4 2019-06-26 stsp
5493 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5494 da76fce2 2020-02-24 stsp
5495 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5496 4e759de4 2019-06-26 stsp switch (ch) {
5497 a74f7e83 2019-11-10 stsp case 'c':
5498 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5499 a74f7e83 2019-11-10 stsp break;
5500 4e759de4 2019-06-26 stsp case 'd':
5501 4e759de4 2019-06-26 stsp delref = optarg;
5502 4e759de4 2019-06-26 stsp break;
5503 4e759de4 2019-06-26 stsp case 'r':
5504 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5505 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5506 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5507 9ba1d308 2019-10-21 stsp optarg);
5508 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5509 4e759de4 2019-06-26 stsp break;
5510 4e759de4 2019-06-26 stsp case 'l':
5511 4e759de4 2019-06-26 stsp do_list = 1;
5512 da76fce2 2020-02-24 stsp break;
5513 da76fce2 2020-02-24 stsp case 'n':
5514 da76fce2 2020-02-24 stsp do_update = 0;
5515 4e759de4 2019-06-26 stsp break;
5516 4e759de4 2019-06-26 stsp default:
5517 4e759de4 2019-06-26 stsp usage_branch();
5518 4e759de4 2019-06-26 stsp /* NOTREACHED */
5519 4e759de4 2019-06-26 stsp }
5520 4e759de4 2019-06-26 stsp }
5521 4e759de4 2019-06-26 stsp
5522 4e759de4 2019-06-26 stsp if (do_list && delref)
5523 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5524 4e759de4 2019-06-26 stsp
5525 4e759de4 2019-06-26 stsp argc -= optind;
5526 4e759de4 2019-06-26 stsp argv += optind;
5527 ad89fa31 2019-10-04 stsp
5528 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5529 ad89fa31 2019-10-04 stsp do_show = 1;
5530 4e759de4 2019-06-26 stsp
5531 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5532 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5533 a74f7e83 2019-11-10 stsp
5534 4e759de4 2019-06-26 stsp if (do_list || delref) {
5535 4e759de4 2019-06-26 stsp if (argc > 0)
5536 4e759de4 2019-06-26 stsp usage_branch();
5537 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5538 4e759de4 2019-06-26 stsp usage_branch();
5539 4e759de4 2019-06-26 stsp
5540 4e759de4 2019-06-26 stsp #ifndef PROFILE
5541 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5542 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5543 4e759de4 2019-06-26 stsp NULL) == -1)
5544 4e759de4 2019-06-26 stsp err(1, "pledge");
5545 4e759de4 2019-06-26 stsp } else {
5546 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5547 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5548 4e759de4 2019-06-26 stsp err(1, "pledge");
5549 4e759de4 2019-06-26 stsp }
5550 4e759de4 2019-06-26 stsp #endif
5551 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5552 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5553 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5554 4e759de4 2019-06-26 stsp goto done;
5555 4e759de4 2019-06-26 stsp }
5556 4e759de4 2019-06-26 stsp
5557 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5558 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5559 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5560 4e759de4 2019-06-26 stsp goto done;
5561 4e759de4 2019-06-26 stsp else
5562 4e759de4 2019-06-26 stsp error = NULL;
5563 4e759de4 2019-06-26 stsp if (worktree) {
5564 4e759de4 2019-06-26 stsp repo_path =
5565 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5566 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5567 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5568 4e759de4 2019-06-26 stsp if (error)
5569 4e759de4 2019-06-26 stsp goto done;
5570 4e759de4 2019-06-26 stsp } else {
5571 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5572 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5573 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5574 4e759de4 2019-06-26 stsp goto done;
5575 4e759de4 2019-06-26 stsp }
5576 4e759de4 2019-06-26 stsp }
5577 4e759de4 2019-06-26 stsp }
5578 4e759de4 2019-06-26 stsp
5579 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5580 4e759de4 2019-06-26 stsp if (error != NULL)
5581 4e759de4 2019-06-26 stsp goto done;
5582 4e759de4 2019-06-26 stsp
5583 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5584 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5585 4e759de4 2019-06-26 stsp if (error)
5586 4e759de4 2019-06-26 stsp goto done;
5587 4e759de4 2019-06-26 stsp
5588 ad89fa31 2019-10-04 stsp if (do_show)
5589 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5590 ad89fa31 2019-10-04 stsp else if (do_list)
5591 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5592 4e759de4 2019-06-26 stsp else if (delref)
5593 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5594 4e759de4 2019-06-26 stsp else {
5595 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5596 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5597 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5598 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5599 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5600 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5601 a74f7e83 2019-11-10 stsp if (error)
5602 a74f7e83 2019-11-10 stsp goto done;
5603 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5604 da76fce2 2020-02-24 stsp if (error)
5605 da76fce2 2020-02-24 stsp goto done;
5606 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5607 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5608 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5609 da76fce2 2020-02-24 stsp
5610 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5611 da76fce2 2020-02-24 stsp if (error)
5612 da76fce2 2020-02-24 stsp goto done;
5613 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5614 da76fce2 2020-02-24 stsp worktree);
5615 da76fce2 2020-02-24 stsp if (error)
5616 da76fce2 2020-02-24 stsp goto done;
5617 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5618 da76fce2 2020-02-24 stsp == -1) {
5619 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5620 da76fce2 2020-02-24 stsp goto done;
5621 da76fce2 2020-02-24 stsp }
5622 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5623 da76fce2 2020-02-24 stsp free(branch_refname);
5624 da76fce2 2020-02-24 stsp if (error)
5625 da76fce2 2020-02-24 stsp goto done;
5626 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5627 da76fce2 2020-02-24 stsp repo);
5628 da76fce2 2020-02-24 stsp if (error)
5629 da76fce2 2020-02-24 stsp goto done;
5630 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5631 da76fce2 2020-02-24 stsp commit_id);
5632 da76fce2 2020-02-24 stsp if (error)
5633 da76fce2 2020-02-24 stsp goto done;
5634 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5635 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5636 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5637 9627c110 2020-04-18 stsp NULL);
5638 da76fce2 2020-02-24 stsp if (error)
5639 da76fce2 2020-02-24 stsp goto done;
5640 9627c110 2020-04-18 stsp if (upa.did_something)
5641 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5642 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5643 da76fce2 2020-02-24 stsp }
5644 4e759de4 2019-06-26 stsp }
5645 4e759de4 2019-06-26 stsp done:
5646 da76fce2 2020-02-24 stsp if (ref)
5647 da76fce2 2020-02-24 stsp got_ref_close(ref);
5648 d0eebce4 2019-03-11 stsp if (repo)
5649 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5650 d0eebce4 2019-03-11 stsp if (worktree)
5651 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5652 d0eebce4 2019-03-11 stsp free(cwd);
5653 d0eebce4 2019-03-11 stsp free(repo_path);
5654 da76fce2 2020-02-24 stsp free(commit_id);
5655 da76fce2 2020-02-24 stsp free(commit_id_str);
5656 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5657 da76fce2 2020-02-24 stsp free((char *)pe->path);
5658 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5659 d00136be 2019-03-26 stsp return error;
5660 d00136be 2019-03-26 stsp }
5661 d00136be 2019-03-26 stsp
5662 8e7bd50a 2019-08-22 stsp
5663 d00136be 2019-03-26 stsp __dead static void
5664 8e7bd50a 2019-08-22 stsp usage_tag(void)
5665 8e7bd50a 2019-08-22 stsp {
5666 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5667 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5668 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5669 8e7bd50a 2019-08-22 stsp exit(1);
5670 b8bad2ba 2019-08-23 stsp }
5671 b8bad2ba 2019-08-23 stsp
5672 b8bad2ba 2019-08-23 stsp #if 0
5673 b8bad2ba 2019-08-23 stsp static const struct got_error *
5674 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5675 b8bad2ba 2019-08-23 stsp {
5676 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5677 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5678 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5679 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5680 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5681 b8bad2ba 2019-08-23 stsp
5682 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5683 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5684 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5685 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5686 b8bad2ba 2019-08-23 stsp if (err)
5687 b8bad2ba 2019-08-23 stsp return err;
5688 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5689 b8bad2ba 2019-08-23 stsp continue;
5690 b8bad2ba 2019-08-23 stsp } else {
5691 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5692 b8bad2ba 2019-08-23 stsp if (err)
5693 b8bad2ba 2019-08-23 stsp break;
5694 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5695 b8bad2ba 2019-08-23 stsp free(re_id);
5696 b8bad2ba 2019-08-23 stsp if (err)
5697 b8bad2ba 2019-08-23 stsp break;
5698 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5699 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5700 b8bad2ba 2019-08-23 stsp }
5701 b8bad2ba 2019-08-23 stsp
5702 b8bad2ba 2019-08-23 stsp while (se) {
5703 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5704 b8bad2ba 2019-08-23 stsp if (err)
5705 b8bad2ba 2019-08-23 stsp break;
5706 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5707 b8bad2ba 2019-08-23 stsp free(se_id);
5708 b8bad2ba 2019-08-23 stsp if (err)
5709 b8bad2ba 2019-08-23 stsp break;
5710 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5711 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5712 b8bad2ba 2019-08-23 stsp
5713 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5714 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5715 b8bad2ba 2019-08-23 stsp if (err)
5716 b8bad2ba 2019-08-23 stsp return err;
5717 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5718 b8bad2ba 2019-08-23 stsp break;
5719 b8bad2ba 2019-08-23 stsp }
5720 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5721 b8bad2ba 2019-08-23 stsp continue;
5722 b8bad2ba 2019-08-23 stsp }
5723 b8bad2ba 2019-08-23 stsp }
5724 b8bad2ba 2019-08-23 stsp done:
5725 b8bad2ba 2019-08-23 stsp return err;
5726 8e7bd50a 2019-08-22 stsp }
5727 b8bad2ba 2019-08-23 stsp #endif
5728 b8bad2ba 2019-08-23 stsp
5729 b8bad2ba 2019-08-23 stsp static const struct got_error *
5730 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5731 8e7bd50a 2019-08-22 stsp {
5732 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5733 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5734 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5735 8e7bd50a 2019-08-22 stsp
5736 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5737 8e7bd50a 2019-08-22 stsp
5738 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5739 8e7bd50a 2019-08-22 stsp if (err)
5740 8e7bd50a 2019-08-22 stsp return err;
5741 8e7bd50a 2019-08-22 stsp
5742 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5743 8e7bd50a 2019-08-22 stsp const char *refname;
5744 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5745 8e7bd50a 2019-08-22 stsp char datebuf[26];
5746 d4efa91b 2020-01-14 stsp const char *tagger;
5747 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5748 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5749 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5750 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5751 8e7bd50a 2019-08-22 stsp
5752 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5753 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5754 8e7bd50a 2019-08-22 stsp continue;
5755 8e7bd50a 2019-08-22 stsp refname += 10;
5756 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5757 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5758 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5759 8e7bd50a 2019-08-22 stsp break;
5760 8e7bd50a 2019-08-22 stsp }
5761 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5762 8e7bd50a 2019-08-22 stsp free(refstr);
5763 8e7bd50a 2019-08-22 stsp
5764 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5765 8e7bd50a 2019-08-22 stsp if (err)
5766 8e7bd50a 2019-08-22 stsp break;
5767 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5768 d4efa91b 2020-01-14 stsp if (err) {
5769 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5770 d4efa91b 2020-01-14 stsp free(id);
5771 d4efa91b 2020-01-14 stsp break;
5772 d4efa91b 2020-01-14 stsp }
5773 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5774 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5775 d4efa91b 2020-01-14 stsp if (err) {
5776 d4efa91b 2020-01-14 stsp free(id);
5777 d4efa91b 2020-01-14 stsp break;
5778 d4efa91b 2020-01-14 stsp }
5779 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5780 d4efa91b 2020-01-14 stsp tagger_time =
5781 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5782 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5783 d4efa91b 2020-01-14 stsp free(id);
5784 d4efa91b 2020-01-14 stsp if (err)
5785 d4efa91b 2020-01-14 stsp break;
5786 d4efa91b 2020-01-14 stsp } else {
5787 d4efa91b 2020-01-14 stsp free(id);
5788 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5789 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5790 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5791 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5792 d4efa91b 2020-01-14 stsp if (err)
5793 d4efa91b 2020-01-14 stsp break;
5794 d4efa91b 2020-01-14 stsp }
5795 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5796 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5797 2417344c 2019-08-23 stsp if (datestr)
5798 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5799 d4efa91b 2020-01-14 stsp if (commit)
5800 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5801 d4efa91b 2020-01-14 stsp else {
5802 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5803 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5804 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5805 d4efa91b 2020-01-14 stsp id_str);
5806 d4efa91b 2020-01-14 stsp break;
5807 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5808 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5809 d4efa91b 2020-01-14 stsp id_str);
5810 d4efa91b 2020-01-14 stsp break;
5811 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5812 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5813 d4efa91b 2020-01-14 stsp id_str);
5814 d4efa91b 2020-01-14 stsp break;
5815 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5816 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5817 d4efa91b 2020-01-14 stsp id_str);
5818 d4efa91b 2020-01-14 stsp break;
5819 d4efa91b 2020-01-14 stsp default:
5820 d4efa91b 2020-01-14 stsp break;
5821 d4efa91b 2020-01-14 stsp }
5822 8e7bd50a 2019-08-22 stsp }
5823 8e7bd50a 2019-08-22 stsp free(id_str);
5824 d4efa91b 2020-01-14 stsp if (commit) {
5825 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5826 d4efa91b 2020-01-14 stsp if (err)
5827 d4efa91b 2020-01-14 stsp break;
5828 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5829 d4efa91b 2020-01-14 stsp } else {
5830 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5831 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5832 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5833 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5834 d4efa91b 2020-01-14 stsp break;
5835 d4efa91b 2020-01-14 stsp }
5836 8e7bd50a 2019-08-22 stsp }
5837 8e7bd50a 2019-08-22 stsp
5838 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5839 8e7bd50a 2019-08-22 stsp do {
5840 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5841 8e7bd50a 2019-08-22 stsp if (line)
5842 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5843 8e7bd50a 2019-08-22 stsp } while (line);
5844 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5845 8e7bd50a 2019-08-22 stsp }
5846 8e7bd50a 2019-08-22 stsp
5847 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5848 8e7bd50a 2019-08-22 stsp return NULL;
5849 8e7bd50a 2019-08-22 stsp }
5850 8e7bd50a 2019-08-22 stsp
5851 8e7bd50a 2019-08-22 stsp static const struct got_error *
5852 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5853 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5854 8e7bd50a 2019-08-22 stsp {
5855 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5856 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5857 f372d5cd 2019-10-21 stsp char *editor = NULL;
5858 1601cb9f 2020-09-11 naddy int initial_content_len;
5859 8e7bd50a 2019-08-22 stsp int fd = -1;
5860 8e7bd50a 2019-08-22 stsp
5861 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5862 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5863 8e7bd50a 2019-08-22 stsp goto done;
5864 8e7bd50a 2019-08-22 stsp }
5865 8e7bd50a 2019-08-22 stsp
5866 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5867 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5868 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5869 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5870 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5871 8e7bd50a 2019-08-22 stsp goto done;
5872 8e7bd50a 2019-08-22 stsp }
5873 8e7bd50a 2019-08-22 stsp
5874 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5875 8e7bd50a 2019-08-22 stsp if (err)
5876 8e7bd50a 2019-08-22 stsp goto done;
5877 8e7bd50a 2019-08-22 stsp
5878 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5879 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5880 97972933 2020-09-11 stsp goto done;
5881 97972933 2020-09-11 stsp }
5882 8e7bd50a 2019-08-22 stsp
5883 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5884 8e7bd50a 2019-08-22 stsp if (err)
5885 8e7bd50a 2019-08-22 stsp goto done;
5886 bfa12d5e 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
5887 bfa12d5e 2020-09-26 stsp initial_content_len);
5888 8e7bd50a 2019-08-22 stsp done:
5889 8e7bd50a 2019-08-22 stsp free(initial_content);
5890 8e7bd50a 2019-08-22 stsp free(template);
5891 8e7bd50a 2019-08-22 stsp free(editor);
5892 8e7bd50a 2019-08-22 stsp
5893 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5894 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5895 97972933 2020-09-11 stsp
5896 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5897 59f86c76 2020-09-11 stsp if (err == NULL)
5898 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5899 59f86c76 2020-09-11 stsp if (err) {
5900 59f86c76 2020-09-11 stsp free(*tagmsg);
5901 59f86c76 2020-09-11 stsp *tagmsg = NULL;
5902 8e7bd50a 2019-08-22 stsp }
5903 8e7bd50a 2019-08-22 stsp return err;
5904 8e7bd50a 2019-08-22 stsp }
5905 8e7bd50a 2019-08-22 stsp
5906 8e7bd50a 2019-08-22 stsp static const struct got_error *
5907 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5908 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5909 8e7bd50a 2019-08-22 stsp {
5910 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5911 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5912 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5913 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5914 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5915 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5916 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5917 8e7bd50a 2019-08-22 stsp
5918 8e7bd50a 2019-08-22 stsp /*
5919 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5920 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5921 8e7bd50a 2019-08-22 stsp * an unintended typo.
5922 8e7bd50a 2019-08-22 stsp */
5923 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5924 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5925 8e7bd50a 2019-08-22 stsp
5926 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
5927 8e7bd50a 2019-08-22 stsp if (err)
5928 8e7bd50a 2019-08-22 stsp return err;
5929 8e7bd50a 2019-08-22 stsp
5930 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5931 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5932 8e7bd50a 2019-08-22 stsp if (err)
5933 8e7bd50a 2019-08-22 stsp goto done;
5934 8e7bd50a 2019-08-22 stsp
5935 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5936 8e7bd50a 2019-08-22 stsp if (err)
5937 8e7bd50a 2019-08-22 stsp goto done;
5938 8e7bd50a 2019-08-22 stsp
5939 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5940 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5941 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5942 62d463ca 2020-10-20 naddy err = got_error_from_errno("strdup");
5943 62d463ca 2020-10-20 naddy goto done;
5944 8e7bd50a 2019-08-22 stsp }
5945 8e7bd50a 2019-08-22 stsp tag_name += 10;
5946 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5947 62d463ca 2020-10-20 naddy err = got_error_from_errno("asprintf");
5948 62d463ca 2020-10-20 naddy goto done;
5949 8e7bd50a 2019-08-22 stsp }
5950 8e7bd50a 2019-08-22 stsp
5951 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5952 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5953 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5954 8e7bd50a 2019-08-22 stsp goto done;
5955 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5956 8e7bd50a 2019-08-22 stsp goto done;
5957 8e7bd50a 2019-08-22 stsp
5958 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5959 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5960 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5961 f372d5cd 2019-10-21 stsp if (err) {
5962 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5963 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5964 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5965 8e7bd50a 2019-08-22 stsp goto done;
5966 f372d5cd 2019-10-21 stsp }
5967 8e7bd50a 2019-08-22 stsp }
5968 8e7bd50a 2019-08-22 stsp
5969 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5970 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5971 f372d5cd 2019-10-21 stsp if (err) {
5972 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5973 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5974 8e7bd50a 2019-08-22 stsp goto done;
5975 f372d5cd 2019-10-21 stsp }
5976 8e7bd50a 2019-08-22 stsp
5977 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5978 f372d5cd 2019-10-21 stsp if (err) {
5979 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5980 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5981 8e7bd50a 2019-08-22 stsp goto done;
5982 f372d5cd 2019-10-21 stsp }
5983 8e7bd50a 2019-08-22 stsp
5984 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5985 f372d5cd 2019-10-21 stsp if (err) {
5986 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5987 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5988 f372d5cd 2019-10-21 stsp goto done;
5989 f372d5cd 2019-10-21 stsp }
5990 8e7bd50a 2019-08-22 stsp
5991 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5992 f372d5cd 2019-10-21 stsp if (err) {
5993 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5994 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5995 f372d5cd 2019-10-21 stsp goto done;
5996 8e7bd50a 2019-08-22 stsp }
5997 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5998 8e7bd50a 2019-08-22 stsp done:
5999 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
6000 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
6001 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
6002 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
6003 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
6004 f372d5cd 2019-10-21 stsp free(tag_id_str);
6005 8e7bd50a 2019-08-22 stsp if (ref)
6006 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
6007 8e7bd50a 2019-08-22 stsp free(commit_id);
6008 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6009 8e7bd50a 2019-08-22 stsp free(refname);
6010 8e7bd50a 2019-08-22 stsp free(tagmsg);
6011 f372d5cd 2019-10-21 stsp free(tagmsg_path);
6012 aba9c984 2019-09-08 stsp free(tagger);
6013 8e7bd50a 2019-08-22 stsp return err;
6014 8e7bd50a 2019-08-22 stsp }
6015 8e7bd50a 2019-08-22 stsp
6016 8e7bd50a 2019-08-22 stsp static const struct got_error *
6017 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
6018 8e7bd50a 2019-08-22 stsp {
6019 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
6020 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
6021 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
6022 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
6023 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
6024 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
6025 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6026 8e7bd50a 2019-08-22 stsp
6027 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6028 8e7bd50a 2019-08-22 stsp switch (ch) {
6029 80106605 2020-02-24 stsp case 'c':
6030 80106605 2020-02-24 stsp commit_id_arg = optarg;
6031 80106605 2020-02-24 stsp break;
6032 8e7bd50a 2019-08-22 stsp case 'm':
6033 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6034 8e7bd50a 2019-08-22 stsp break;
6035 8e7bd50a 2019-08-22 stsp case 'r':
6036 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6037 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6038 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6039 9ba1d308 2019-10-21 stsp optarg);
6040 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6041 8e7bd50a 2019-08-22 stsp break;
6042 8e7bd50a 2019-08-22 stsp case 'l':
6043 8e7bd50a 2019-08-22 stsp do_list = 1;
6044 8e7bd50a 2019-08-22 stsp break;
6045 8e7bd50a 2019-08-22 stsp default:
6046 8e7bd50a 2019-08-22 stsp usage_tag();
6047 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6048 8e7bd50a 2019-08-22 stsp }
6049 8e7bd50a 2019-08-22 stsp }
6050 8e7bd50a 2019-08-22 stsp
6051 8e7bd50a 2019-08-22 stsp argc -= optind;
6052 8e7bd50a 2019-08-22 stsp argv += optind;
6053 8e7bd50a 2019-08-22 stsp
6054 8e7bd50a 2019-08-22 stsp if (do_list) {
6055 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6056 775ce909 2020-03-22 stsp errx(1,
6057 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6058 8e7bd50a 2019-08-22 stsp if (tagmsg)
6059 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
6060 8e7bd50a 2019-08-22 stsp if (argc > 0)
6061 8e7bd50a 2019-08-22 stsp usage_tag();
6062 80106605 2020-02-24 stsp } else if (argc != 1)
6063 8e7bd50a 2019-08-22 stsp usage_tag();
6064 80106605 2020-02-24 stsp
6065 a2887370 2019-08-23 stsp tag_name = argv[0];
6066 8e7bd50a 2019-08-22 stsp
6067 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6068 8e7bd50a 2019-08-22 stsp if (do_list) {
6069 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6070 8e7bd50a 2019-08-22 stsp NULL) == -1)
6071 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6072 8e7bd50a 2019-08-22 stsp } else {
6073 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6074 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6075 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6076 8e7bd50a 2019-08-22 stsp }
6077 8e7bd50a 2019-08-22 stsp #endif
6078 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6079 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6080 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6081 8e7bd50a 2019-08-22 stsp goto done;
6082 8e7bd50a 2019-08-22 stsp }
6083 8e7bd50a 2019-08-22 stsp
6084 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6085 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6086 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6087 8e7bd50a 2019-08-22 stsp goto done;
6088 8e7bd50a 2019-08-22 stsp else
6089 8e7bd50a 2019-08-22 stsp error = NULL;
6090 8e7bd50a 2019-08-22 stsp if (worktree) {
6091 8e7bd50a 2019-08-22 stsp repo_path =
6092 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6093 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6094 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6095 8e7bd50a 2019-08-22 stsp if (error)
6096 8e7bd50a 2019-08-22 stsp goto done;
6097 8e7bd50a 2019-08-22 stsp } else {
6098 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6099 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6100 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6101 8e7bd50a 2019-08-22 stsp goto done;
6102 8e7bd50a 2019-08-22 stsp }
6103 8e7bd50a 2019-08-22 stsp }
6104 8e7bd50a 2019-08-22 stsp }
6105 8e7bd50a 2019-08-22 stsp
6106 8e7bd50a 2019-08-22 stsp if (do_list) {
6107 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6108 c9956ddf 2019-09-08 stsp if (error != NULL)
6109 c9956ddf 2019-09-08 stsp goto done;
6110 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6111 8e7bd50a 2019-08-22 stsp if (error)
6112 8e7bd50a 2019-08-22 stsp goto done;
6113 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6114 8e7bd50a 2019-08-22 stsp } else {
6115 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6116 c9956ddf 2019-09-08 stsp if (error)
6117 c9956ddf 2019-09-08 stsp goto done;
6118 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6119 c9956ddf 2019-09-08 stsp if (error != NULL)
6120 c9956ddf 2019-09-08 stsp goto done;
6121 c9956ddf 2019-09-08 stsp
6122 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6123 42a285e2 2020-10-01 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
6124 8e7bd50a 2019-08-22 stsp if (error)
6125 8e7bd50a 2019-08-22 stsp goto done;
6126 8e7bd50a 2019-08-22 stsp }
6127 8e7bd50a 2019-08-22 stsp
6128 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6129 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6130 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6131 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6132 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6133 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6134 8e7bd50a 2019-08-22 stsp if (error)
6135 8e7bd50a 2019-08-22 stsp goto done;
6136 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6137 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6138 8e7bd50a 2019-08-22 stsp if (error)
6139 8e7bd50a 2019-08-22 stsp goto done;
6140 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6141 8e7bd50a 2019-08-22 stsp free(commit_id);
6142 8e7bd50a 2019-08-22 stsp if (error)
6143 8e7bd50a 2019-08-22 stsp goto done;
6144 8e7bd50a 2019-08-22 stsp }
6145 8e7bd50a 2019-08-22 stsp
6146 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6147 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6148 8e7bd50a 2019-08-22 stsp }
6149 8e7bd50a 2019-08-22 stsp done:
6150 8e7bd50a 2019-08-22 stsp if (repo)
6151 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
6152 8e7bd50a 2019-08-22 stsp if (worktree)
6153 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6154 8e7bd50a 2019-08-22 stsp free(cwd);
6155 8e7bd50a 2019-08-22 stsp free(repo_path);
6156 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6157 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6158 8e7bd50a 2019-08-22 stsp return error;
6159 8e7bd50a 2019-08-22 stsp }
6160 8e7bd50a 2019-08-22 stsp
6161 8e7bd50a 2019-08-22 stsp __dead static void
6162 d00136be 2019-03-26 stsp usage_add(void)
6163 d00136be 2019-03-26 stsp {
6164 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6165 022fae89 2019-12-06 tracey getprogname());
6166 d00136be 2019-03-26 stsp exit(1);
6167 d00136be 2019-03-26 stsp }
6168 d00136be 2019-03-26 stsp
6169 d00136be 2019-03-26 stsp static const struct got_error *
6170 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6171 4e68cba3 2019-11-23 stsp {
6172 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6173 4e68cba3 2019-11-23 stsp path++;
6174 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6175 4e68cba3 2019-11-23 stsp return NULL;
6176 4e68cba3 2019-11-23 stsp }
6177 4e68cba3 2019-11-23 stsp
6178 4e68cba3 2019-11-23 stsp static const struct got_error *
6179 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6180 d00136be 2019-03-26 stsp {
6181 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6182 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6183 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6184 1dd54920 2019-05-11 stsp char *cwd = NULL;
6185 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6186 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6187 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6188 1dd54920 2019-05-11 stsp
6189 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6190 d00136be 2019-03-26 stsp
6191 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6192 d00136be 2019-03-26 stsp switch (ch) {
6193 022fae89 2019-12-06 tracey case 'I':
6194 022fae89 2019-12-06 tracey no_ignores = 1;
6195 022fae89 2019-12-06 tracey break;
6196 4e68cba3 2019-11-23 stsp case 'R':
6197 4e68cba3 2019-11-23 stsp can_recurse = 1;
6198 4e68cba3 2019-11-23 stsp break;
6199 d00136be 2019-03-26 stsp default:
6200 d00136be 2019-03-26 stsp usage_add();
6201 d00136be 2019-03-26 stsp /* NOTREACHED */
6202 d00136be 2019-03-26 stsp }
6203 d00136be 2019-03-26 stsp }
6204 d00136be 2019-03-26 stsp
6205 d00136be 2019-03-26 stsp argc -= optind;
6206 d00136be 2019-03-26 stsp argv += optind;
6207 d00136be 2019-03-26 stsp
6208 43012d58 2019-07-14 stsp #ifndef PROFILE
6209 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6210 43012d58 2019-07-14 stsp NULL) == -1)
6211 43012d58 2019-07-14 stsp err(1, "pledge");
6212 43012d58 2019-07-14 stsp #endif
6213 723c305c 2019-05-11 jcs if (argc < 1)
6214 d00136be 2019-03-26 stsp usage_add();
6215 d00136be 2019-03-26 stsp
6216 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6217 d00136be 2019-03-26 stsp if (cwd == NULL) {
6218 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6219 d00136be 2019-03-26 stsp goto done;
6220 d00136be 2019-03-26 stsp }
6221 723c305c 2019-05-11 jcs
6222 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6223 fa51e947 2020-03-27 stsp if (error) {
6224 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6225 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6226 d00136be 2019-03-26 stsp goto done;
6227 fa51e947 2020-03-27 stsp }
6228 d00136be 2019-03-26 stsp
6229 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6230 c9956ddf 2019-09-08 stsp NULL);
6231 031a5338 2019-03-26 stsp if (error != NULL)
6232 031a5338 2019-03-26 stsp goto done;
6233 031a5338 2019-03-26 stsp
6234 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6235 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6236 d00136be 2019-03-26 stsp if (error)
6237 d00136be 2019-03-26 stsp goto done;
6238 d00136be 2019-03-26 stsp
6239 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6240 6d022e97 2019-08-04 stsp if (error)
6241 022fae89 2019-12-06 tracey goto done;
6242 022fae89 2019-12-06 tracey
6243 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6244 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6245 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6246 6d022e97 2019-08-04 stsp goto done;
6247 723c305c 2019-05-11 jcs
6248 022fae89 2019-12-06 tracey }
6249 022fae89 2019-12-06 tracey
6250 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6251 4e68cba3 2019-11-23 stsp char *ondisk_path;
6252 4e68cba3 2019-11-23 stsp struct stat sb;
6253 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6254 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6255 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6256 62d463ca 2020-10-20 naddy pe->path) == -1) {
6257 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6258 4e68cba3 2019-11-23 stsp goto done;
6259 4e68cba3 2019-11-23 stsp }
6260 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6261 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6262 4e68cba3 2019-11-23 stsp free(ondisk_path);
6263 4e68cba3 2019-11-23 stsp continue;
6264 4e68cba3 2019-11-23 stsp }
6265 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6266 4e68cba3 2019-11-23 stsp ondisk_path);
6267 4e68cba3 2019-11-23 stsp free(ondisk_path);
6268 4e68cba3 2019-11-23 stsp goto done;
6269 4e68cba3 2019-11-23 stsp }
6270 4e68cba3 2019-11-23 stsp free(ondisk_path);
6271 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6272 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6273 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6274 4e68cba3 2019-11-23 stsp goto done;
6275 4e68cba3 2019-11-23 stsp }
6276 4e68cba3 2019-11-23 stsp }
6277 4e68cba3 2019-11-23 stsp }
6278 022fae89 2019-12-06 tracey
6279 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6280 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6281 d00136be 2019-03-26 stsp done:
6282 031a5338 2019-03-26 stsp if (repo)
6283 031a5338 2019-03-26 stsp got_repo_close(repo);
6284 d00136be 2019-03-26 stsp if (worktree)
6285 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6286 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6287 1dd54920 2019-05-11 stsp free((char *)pe->path);
6288 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6289 2ec1f75b 2019-03-26 stsp free(cwd);
6290 2ec1f75b 2019-03-26 stsp return error;
6291 2ec1f75b 2019-03-26 stsp }
6292 2ec1f75b 2019-03-26 stsp
6293 2ec1f75b 2019-03-26 stsp __dead static void
6294 648e4ef7 2019-07-09 stsp usage_remove(void)
6295 2ec1f75b 2019-03-26 stsp {
6296 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6297 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6298 2ec1f75b 2019-03-26 stsp exit(1);
6299 2a06fe5f 2019-08-24 stsp }
6300 2a06fe5f 2019-08-24 stsp
6301 2a06fe5f 2019-08-24 stsp static const struct got_error *
6302 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6303 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6304 2a06fe5f 2019-08-24 stsp {
6305 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6306 f2a9dc41 2019-12-13 tracey path++;
6307 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6308 2a06fe5f 2019-08-24 stsp return NULL;
6309 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6310 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6311 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6312 2a06fe5f 2019-08-24 stsp return NULL;
6313 2ec1f75b 2019-03-26 stsp }
6314 2ec1f75b 2019-03-26 stsp
6315 2ec1f75b 2019-03-26 stsp static const struct got_error *
6316 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6317 2ec1f75b 2019-03-26 stsp {
6318 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6319 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6320 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6321 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6322 17ed4618 2019-06-02 stsp char *cwd = NULL;
6323 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6324 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6325 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6326 2ec1f75b 2019-03-26 stsp
6327 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6328 17ed4618 2019-06-02 stsp
6329 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6330 2ec1f75b 2019-03-26 stsp switch (ch) {
6331 2ec1f75b 2019-03-26 stsp case 'f':
6332 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6333 2ec1f75b 2019-03-26 stsp break;
6334 70e3e7f5 2019-12-13 tracey case 'k':
6335 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6336 70e3e7f5 2019-12-13 tracey break;
6337 f2a9dc41 2019-12-13 tracey case 'R':
6338 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6339 f2a9dc41 2019-12-13 tracey break;
6340 766841c2 2020-08-13 stsp case 's':
6341 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6342 766841c2 2020-08-13 stsp switch (optarg[i]) {
6343 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6344 766841c2 2020-08-13 stsp delete_local_mods = 1;
6345 766841c2 2020-08-13 stsp break;
6346 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6347 766841c2 2020-08-13 stsp break;
6348 766841c2 2020-08-13 stsp default:
6349 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6350 766841c2 2020-08-13 stsp optarg[i]);
6351 766841c2 2020-08-13 stsp }
6352 766841c2 2020-08-13 stsp }
6353 766841c2 2020-08-13 stsp status_codes = optarg;
6354 766841c2 2020-08-13 stsp break;
6355 2ec1f75b 2019-03-26 stsp default:
6356 f2a9dc41 2019-12-13 tracey usage_remove();
6357 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6358 2ec1f75b 2019-03-26 stsp }
6359 2ec1f75b 2019-03-26 stsp }
6360 2ec1f75b 2019-03-26 stsp
6361 2ec1f75b 2019-03-26 stsp argc -= optind;
6362 2ec1f75b 2019-03-26 stsp argv += optind;
6363 2ec1f75b 2019-03-26 stsp
6364 43012d58 2019-07-14 stsp #ifndef PROFILE
6365 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6366 43012d58 2019-07-14 stsp NULL) == -1)
6367 43012d58 2019-07-14 stsp err(1, "pledge");
6368 43012d58 2019-07-14 stsp #endif
6369 17ed4618 2019-06-02 stsp if (argc < 1)
6370 648e4ef7 2019-07-09 stsp usage_remove();
6371 2ec1f75b 2019-03-26 stsp
6372 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6373 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6374 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6375 2ec1f75b 2019-03-26 stsp goto done;
6376 2ec1f75b 2019-03-26 stsp }
6377 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6378 fa51e947 2020-03-27 stsp if (error) {
6379 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6380 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6381 2ec1f75b 2019-03-26 stsp goto done;
6382 fa51e947 2020-03-27 stsp }
6383 2ec1f75b 2019-03-26 stsp
6384 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6385 c9956ddf 2019-09-08 stsp NULL);
6386 2af4a041 2019-05-11 jcs if (error)
6387 2ec1f75b 2019-03-26 stsp goto done;
6388 2ec1f75b 2019-03-26 stsp
6389 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6390 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6391 2ec1f75b 2019-03-26 stsp if (error)
6392 2ec1f75b 2019-03-26 stsp goto done;
6393 2ec1f75b 2019-03-26 stsp
6394 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6395 6d022e97 2019-08-04 stsp if (error)
6396 6d022e97 2019-08-04 stsp goto done;
6397 17ed4618 2019-06-02 stsp
6398 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6399 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6400 f2a9dc41 2019-12-13 tracey struct stat sb;
6401 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6402 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6403 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6404 62d463ca 2020-10-20 naddy pe->path) == -1) {
6405 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6406 f2a9dc41 2019-12-13 tracey goto done;
6407 f2a9dc41 2019-12-13 tracey }
6408 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6409 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6410 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6411 f2a9dc41 2019-12-13 tracey continue;
6412 f2a9dc41 2019-12-13 tracey }
6413 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6414 f2a9dc41 2019-12-13 tracey ondisk_path);
6415 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6416 f2a9dc41 2019-12-13 tracey goto done;
6417 f2a9dc41 2019-12-13 tracey }
6418 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6419 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6420 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6421 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6422 f2a9dc41 2019-12-13 tracey goto done;
6423 f2a9dc41 2019-12-13 tracey }
6424 f2a9dc41 2019-12-13 tracey }
6425 f2a9dc41 2019-12-13 tracey }
6426 f2a9dc41 2019-12-13 tracey
6427 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6428 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6429 766841c2 2020-08-13 stsp repo, keep_on_disk);
6430 a129376b 2019-03-28 stsp done:
6431 a129376b 2019-03-28 stsp if (repo)
6432 a129376b 2019-03-28 stsp got_repo_close(repo);
6433 a129376b 2019-03-28 stsp if (worktree)
6434 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6435 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6436 17ed4618 2019-06-02 stsp free((char *)pe->path);
6437 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6438 a129376b 2019-03-28 stsp free(cwd);
6439 a129376b 2019-03-28 stsp return error;
6440 a129376b 2019-03-28 stsp }
6441 a129376b 2019-03-28 stsp
6442 a129376b 2019-03-28 stsp __dead static void
6443 a129376b 2019-03-28 stsp usage_revert(void)
6444 a129376b 2019-03-28 stsp {
6445 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6446 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6447 a129376b 2019-03-28 stsp exit(1);
6448 a129376b 2019-03-28 stsp }
6449 a129376b 2019-03-28 stsp
6450 1ee397ad 2019-07-12 stsp static const struct got_error *
6451 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6452 a129376b 2019-03-28 stsp {
6453 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6454 fb9704af 2020-01-27 stsp return NULL;
6455 fb9704af 2020-01-27 stsp
6456 a129376b 2019-03-28 stsp while (path[0] == '/')
6457 a129376b 2019-03-28 stsp path++;
6458 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6459 33aa809d 2019-08-08 stsp return NULL;
6460 33aa809d 2019-08-08 stsp }
6461 33aa809d 2019-08-08 stsp
6462 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6463 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6464 33aa809d 2019-08-08 stsp const char *action;
6465 33aa809d 2019-08-08 stsp };
6466 33aa809d 2019-08-08 stsp
6467 33aa809d 2019-08-08 stsp static const struct got_error *
6468 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6469 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6470 33aa809d 2019-08-08 stsp {
6471 33aa809d 2019-08-08 stsp char *line = NULL;
6472 33aa809d 2019-08-08 stsp size_t linesize = 0;
6473 33aa809d 2019-08-08 stsp ssize_t linelen;
6474 33aa809d 2019-08-08 stsp
6475 33aa809d 2019-08-08 stsp switch (status) {
6476 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6477 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6478 33aa809d 2019-08-08 stsp break;
6479 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6480 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6481 33aa809d 2019-08-08 stsp break;
6482 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6483 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6484 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6485 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6486 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6487 33aa809d 2019-08-08 stsp printf("%s", line);
6488 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6489 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6490 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6491 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6492 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6493 33aa809d 2019-08-08 stsp break;
6494 33aa809d 2019-08-08 stsp default:
6495 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6496 33aa809d 2019-08-08 stsp }
6497 33aa809d 2019-08-08 stsp
6498 33aa809d 2019-08-08 stsp return NULL;
6499 33aa809d 2019-08-08 stsp }
6500 33aa809d 2019-08-08 stsp
6501 33aa809d 2019-08-08 stsp static const struct got_error *
6502 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6503 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6504 33aa809d 2019-08-08 stsp {
6505 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6506 33aa809d 2019-08-08 stsp char *line = NULL;
6507 33aa809d 2019-08-08 stsp size_t linesize = 0;
6508 33aa809d 2019-08-08 stsp ssize_t linelen;
6509 33aa809d 2019-08-08 stsp int resp = ' ';
6510 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6511 33aa809d 2019-08-08 stsp
6512 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6513 33aa809d 2019-08-08 stsp
6514 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6515 33aa809d 2019-08-08 stsp char *nl;
6516 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6517 33aa809d 2019-08-08 stsp a->action);
6518 33aa809d 2019-08-08 stsp if (err)
6519 33aa809d 2019-08-08 stsp return err;
6520 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6521 33aa809d 2019-08-08 stsp if (linelen == -1) {
6522 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6523 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6524 33aa809d 2019-08-08 stsp return NULL;
6525 33aa809d 2019-08-08 stsp }
6526 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6527 33aa809d 2019-08-08 stsp if (nl)
6528 33aa809d 2019-08-08 stsp *nl = '\0';
6529 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6530 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6531 33aa809d 2019-08-08 stsp printf("y\n");
6532 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6533 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6534 33aa809d 2019-08-08 stsp printf("n\n");
6535 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6536 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6537 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6538 33aa809d 2019-08-08 stsp printf("q\n");
6539 33aa809d 2019-08-08 stsp } else
6540 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6541 33aa809d 2019-08-08 stsp free(line);
6542 33aa809d 2019-08-08 stsp return NULL;
6543 33aa809d 2019-08-08 stsp }
6544 33aa809d 2019-08-08 stsp
6545 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6546 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6547 33aa809d 2019-08-08 stsp a->action);
6548 33aa809d 2019-08-08 stsp if (err)
6549 33aa809d 2019-08-08 stsp return err;
6550 33aa809d 2019-08-08 stsp resp = getchar();
6551 33aa809d 2019-08-08 stsp if (resp == '\n')
6552 33aa809d 2019-08-08 stsp resp = getchar();
6553 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6554 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6555 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6556 33aa809d 2019-08-08 stsp resp = ' ';
6557 33aa809d 2019-08-08 stsp }
6558 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6559 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6560 33aa809d 2019-08-08 stsp resp = ' ';
6561 33aa809d 2019-08-08 stsp }
6562 33aa809d 2019-08-08 stsp }
6563 33aa809d 2019-08-08 stsp
6564 33aa809d 2019-08-08 stsp if (resp == 'y')
6565 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6566 33aa809d 2019-08-08 stsp else if (resp == 'n')
6567 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6568 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6569 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6570 33aa809d 2019-08-08 stsp
6571 1ee397ad 2019-07-12 stsp return NULL;
6572 a129376b 2019-03-28 stsp }
6573 a129376b 2019-03-28 stsp
6574 33aa809d 2019-08-08 stsp
6575 a129376b 2019-03-28 stsp static const struct got_error *
6576 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6577 a129376b 2019-03-28 stsp {
6578 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6579 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6580 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6581 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6582 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6583 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6584 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6585 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6586 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6587 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6588 a129376b 2019-03-28 stsp
6589 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6590 e20a8b6f 2019-06-04 stsp
6591 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6592 a129376b 2019-03-28 stsp switch (ch) {
6593 33aa809d 2019-08-08 stsp case 'p':
6594 33aa809d 2019-08-08 stsp pflag = 1;
6595 33aa809d 2019-08-08 stsp break;
6596 33aa809d 2019-08-08 stsp case 'F':
6597 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6598 33aa809d 2019-08-08 stsp break;
6599 0f6d7415 2019-08-08 stsp case 'R':
6600 0f6d7415 2019-08-08 stsp can_recurse = 1;
6601 0f6d7415 2019-08-08 stsp break;
6602 a129376b 2019-03-28 stsp default:
6603 a129376b 2019-03-28 stsp usage_revert();
6604 a129376b 2019-03-28 stsp /* NOTREACHED */
6605 a129376b 2019-03-28 stsp }
6606 a129376b 2019-03-28 stsp }
6607 a129376b 2019-03-28 stsp
6608 a129376b 2019-03-28 stsp argc -= optind;
6609 a129376b 2019-03-28 stsp argv += optind;
6610 a129376b 2019-03-28 stsp
6611 43012d58 2019-07-14 stsp #ifndef PROFILE
6612 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6613 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6614 43012d58 2019-07-14 stsp err(1, "pledge");
6615 43012d58 2019-07-14 stsp #endif
6616 e20a8b6f 2019-06-04 stsp if (argc < 1)
6617 a129376b 2019-03-28 stsp usage_revert();
6618 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6619 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6620 a129376b 2019-03-28 stsp
6621 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6622 a129376b 2019-03-28 stsp if (cwd == NULL) {
6623 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6624 a129376b 2019-03-28 stsp goto done;
6625 a129376b 2019-03-28 stsp }
6626 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6627 fa51e947 2020-03-27 stsp if (error) {
6628 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6629 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6630 2ec1f75b 2019-03-26 stsp goto done;
6631 fa51e947 2020-03-27 stsp }
6632 a129376b 2019-03-28 stsp
6633 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6634 c9956ddf 2019-09-08 stsp NULL);
6635 a129376b 2019-03-28 stsp if (error != NULL)
6636 a129376b 2019-03-28 stsp goto done;
6637 a129376b 2019-03-28 stsp
6638 33aa809d 2019-08-08 stsp if (patch_script_path) {
6639 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6640 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6641 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6642 33aa809d 2019-08-08 stsp patch_script_path);
6643 33aa809d 2019-08-08 stsp goto done;
6644 33aa809d 2019-08-08 stsp }
6645 33aa809d 2019-08-08 stsp }
6646 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6647 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6648 a129376b 2019-03-28 stsp if (error)
6649 a129376b 2019-03-28 stsp goto done;
6650 a129376b 2019-03-28 stsp
6651 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6652 6d022e97 2019-08-04 stsp if (error)
6653 6d022e97 2019-08-04 stsp goto done;
6654 00db391e 2019-08-03 stsp
6655 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6656 0f6d7415 2019-08-08 stsp char *ondisk_path;
6657 0f6d7415 2019-08-08 stsp struct stat sb;
6658 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6659 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6660 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6661 62d463ca 2020-10-20 naddy pe->path) == -1) {
6662 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6663 0f6d7415 2019-08-08 stsp goto done;
6664 0f6d7415 2019-08-08 stsp }
6665 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6666 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6667 0f6d7415 2019-08-08 stsp free(ondisk_path);
6668 0f6d7415 2019-08-08 stsp continue;
6669 0f6d7415 2019-08-08 stsp }
6670 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6671 0f6d7415 2019-08-08 stsp ondisk_path);
6672 0f6d7415 2019-08-08 stsp free(ondisk_path);
6673 0f6d7415 2019-08-08 stsp goto done;
6674 0f6d7415 2019-08-08 stsp }
6675 0f6d7415 2019-08-08 stsp free(ondisk_path);
6676 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6677 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6678 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6679 0f6d7415 2019-08-08 stsp goto done;
6680 0f6d7415 2019-08-08 stsp }
6681 0f6d7415 2019-08-08 stsp }
6682 0f6d7415 2019-08-08 stsp }
6683 0f6d7415 2019-08-08 stsp
6684 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6685 33aa809d 2019-08-08 stsp cpa.action = "revert";
6686 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6687 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6688 2ec1f75b 2019-03-26 stsp done:
6689 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6690 33aa809d 2019-08-08 stsp error == NULL)
6691 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6692 2ec1f75b 2019-03-26 stsp if (repo)
6693 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6694 2ec1f75b 2019-03-26 stsp if (worktree)
6695 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6696 2ec1f75b 2019-03-26 stsp free(path);
6697 d00136be 2019-03-26 stsp free(cwd);
6698 6bad629b 2019-02-04 stsp return error;
6699 c4296144 2019-05-09 stsp }
6700 c4296144 2019-05-09 stsp
6701 c4296144 2019-05-09 stsp __dead static void
6702 c4296144 2019-05-09 stsp usage_commit(void)
6703 c4296144 2019-05-09 stsp {
6704 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6705 5c1e53bc 2019-07-28 stsp getprogname());
6706 c4296144 2019-05-09 stsp exit(1);
6707 33ad4cbe 2019-05-12 jcs }
6708 33ad4cbe 2019-05-12 jcs
6709 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6710 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6711 e2ba3d07 2019-05-13 stsp const char *editor;
6712 e0870e44 2019-05-13 stsp const char *worktree_path;
6713 76d98825 2019-06-03 stsp const char *branch_name;
6714 314a6357 2019-05-13 stsp const char *repo_path;
6715 e0870e44 2019-05-13 stsp char *logmsg_path;
6716 e2ba3d07 2019-05-13 stsp
6717 e2ba3d07 2019-05-13 stsp };
6718 e0870e44 2019-05-13 stsp
6719 33ad4cbe 2019-05-12 jcs static const struct got_error *
6720 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6721 33ad4cbe 2019-05-12 jcs void *arg)
6722 33ad4cbe 2019-05-12 jcs {
6723 76d98825 2019-06-03 stsp char *initial_content = NULL;
6724 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6725 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6726 e0870e44 2019-05-13 stsp char *template = NULL;
6727 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6728 1601cb9f 2020-09-11 naddy int initial_content_len;
6729 97972933 2020-09-11 stsp int fd = -1;
6730 33ad4cbe 2019-05-12 jcs size_t len;
6731 33ad4cbe 2019-05-12 jcs
6732 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6733 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6734 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6735 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6736 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6737 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6738 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6739 33ad4cbe 2019-05-12 jcs return NULL;
6740 33ad4cbe 2019-05-12 jcs }
6741 33ad4cbe 2019-05-12 jcs
6742 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6743 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6744 76d98825 2019-06-03 stsp
6745 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6746 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6747 1601cb9f 2020-09-11 naddy a->branch_name);
6748 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6749 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6750 e0870e44 2019-05-13 stsp
6751 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6752 793c30b5 2019-05-13 stsp if (err)
6753 e0870e44 2019-05-13 stsp goto done;
6754 33ad4cbe 2019-05-12 jcs
6755 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6756 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6757 97972933 2020-09-11 stsp goto done;
6758 97972933 2020-09-11 stsp }
6759 33ad4cbe 2019-05-12 jcs
6760 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6761 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6762 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6763 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6764 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6765 33ad4cbe 2019-05-12 jcs }
6766 33ad4cbe 2019-05-12 jcs
6767 bfa12d5e 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
6768 bfa12d5e 2020-09-26 stsp initial_content_len);
6769 33ad4cbe 2019-05-12 jcs done:
6770 76d98825 2019-06-03 stsp free(initial_content);
6771 e0870e44 2019-05-13 stsp free(template);
6772 314a6357 2019-05-13 stsp
6773 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6774 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6775 97972933 2020-09-11 stsp
6776 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6777 59f86c76 2020-09-11 stsp if (err == NULL)
6778 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6779 59f86c76 2020-09-11 stsp if (err) {
6780 59f86c76 2020-09-11 stsp free(*logmsg);
6781 59f86c76 2020-09-11 stsp *logmsg = NULL;
6782 3ce1b845 2019-07-15 stsp }
6783 33ad4cbe 2019-05-12 jcs return err;
6784 6bad629b 2019-02-04 stsp }
6785 c4296144 2019-05-09 stsp
6786 c4296144 2019-05-09 stsp static const struct got_error *
6787 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6788 c4296144 2019-05-09 stsp {
6789 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6790 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6791 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6792 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6793 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6794 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6795 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6796 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6797 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6798 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6799 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6800 5c1e53bc 2019-07-28 stsp
6801 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6802 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6803 c4296144 2019-05-09 stsp
6804 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6805 c4296144 2019-05-09 stsp switch (ch) {
6806 c4296144 2019-05-09 stsp case 'm':
6807 c4296144 2019-05-09 stsp logmsg = optarg;
6808 c4296144 2019-05-09 stsp break;
6809 35213c7c 2020-07-23 stsp case 'S':
6810 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6811 35213c7c 2020-07-23 stsp break;
6812 c4296144 2019-05-09 stsp default:
6813 c4296144 2019-05-09 stsp usage_commit();
6814 c4296144 2019-05-09 stsp /* NOTREACHED */
6815 c4296144 2019-05-09 stsp }
6816 c4296144 2019-05-09 stsp }
6817 c4296144 2019-05-09 stsp
6818 c4296144 2019-05-09 stsp argc -= optind;
6819 c4296144 2019-05-09 stsp argv += optind;
6820 c4296144 2019-05-09 stsp
6821 43012d58 2019-07-14 stsp #ifndef PROFILE
6822 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6823 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6824 43012d58 2019-07-14 stsp err(1, "pledge");
6825 43012d58 2019-07-14 stsp #endif
6826 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6827 c4296144 2019-05-09 stsp if (cwd == NULL) {
6828 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6829 c4296144 2019-05-09 stsp goto done;
6830 c4296144 2019-05-09 stsp }
6831 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6832 fa51e947 2020-03-27 stsp if (error) {
6833 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6834 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6835 7d5807f4 2019-07-11 stsp goto done;
6836 fa51e947 2020-03-27 stsp }
6837 7d5807f4 2019-07-11 stsp
6838 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6839 c4296144 2019-05-09 stsp if (error)
6840 a698f62e 2019-07-25 stsp goto done;
6841 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6842 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6843 c4296144 2019-05-09 stsp goto done;
6844 a698f62e 2019-07-25 stsp }
6845 5c1e53bc 2019-07-28 stsp
6846 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6847 916f288c 2019-07-30 stsp worktree);
6848 5c1e53bc 2019-07-28 stsp if (error)
6849 5c1e53bc 2019-07-28 stsp goto done;
6850 c4296144 2019-05-09 stsp
6851 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6852 c9956ddf 2019-09-08 stsp if (error)
6853 c9956ddf 2019-09-08 stsp goto done;
6854 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6855 c9956ddf 2019-09-08 stsp gitconfig_path);
6856 c4296144 2019-05-09 stsp if (error != NULL)
6857 c4296144 2019-05-09 stsp goto done;
6858 c4296144 2019-05-09 stsp
6859 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6860 aba9c984 2019-09-08 stsp if (error)
6861 aba9c984 2019-09-08 stsp return error;
6862 aba9c984 2019-09-08 stsp
6863 314a6357 2019-05-13 stsp /*
6864 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6865 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6866 314a6357 2019-05-13 stsp */
6867 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6868 314a6357 2019-05-13 stsp error = get_editor(&editor);
6869 314a6357 2019-05-13 stsp else
6870 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6871 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6872 c4296144 2019-05-09 stsp if (error)
6873 c4296144 2019-05-09 stsp goto done;
6874 c4296144 2019-05-09 stsp
6875 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6876 087fb88c 2019-08-04 stsp if (error)
6877 087fb88c 2019-08-04 stsp goto done;
6878 087fb88c 2019-08-04 stsp
6879 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6880 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6881 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6882 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6883 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6884 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6885 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6886 916f288c 2019-07-30 stsp goto done;
6887 916f288c 2019-07-30 stsp }
6888 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6889 916f288c 2019-07-30 stsp }
6890 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6891 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6892 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6893 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6894 e0870e44 2019-05-13 stsp if (error) {
6895 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6896 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6897 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6898 c4296144 2019-05-09 stsp goto done;
6899 e0870e44 2019-05-13 stsp }
6900 c4296144 2019-05-09 stsp
6901 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6902 c4296144 2019-05-09 stsp if (error)
6903 c4296144 2019-05-09 stsp goto done;
6904 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6905 c4296144 2019-05-09 stsp done:
6906 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6907 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6908 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6909 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6910 7266f21f 2019-10-21 stsp error == NULL)
6911 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6912 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6913 c4296144 2019-05-09 stsp if (repo)
6914 c4296144 2019-05-09 stsp got_repo_close(repo);
6915 c4296144 2019-05-09 stsp if (worktree)
6916 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6917 c4296144 2019-05-09 stsp free(cwd);
6918 c4296144 2019-05-09 stsp free(id_str);
6919 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6920 e2ba3d07 2019-05-13 stsp free(editor);
6921 aba9c984 2019-09-08 stsp free(author);
6922 234035bc 2019-06-01 stsp return error;
6923 234035bc 2019-06-01 stsp }
6924 234035bc 2019-06-01 stsp
6925 234035bc 2019-06-01 stsp __dead static void
6926 234035bc 2019-06-01 stsp usage_cherrypick(void)
6927 234035bc 2019-06-01 stsp {
6928 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6929 234035bc 2019-06-01 stsp exit(1);
6930 234035bc 2019-06-01 stsp }
6931 234035bc 2019-06-01 stsp
6932 234035bc 2019-06-01 stsp static const struct got_error *
6933 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6934 234035bc 2019-06-01 stsp {
6935 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6936 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6937 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6938 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6939 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6940 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6941 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6942 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6943 9627c110 2020-04-18 stsp int ch;
6944 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6945 234035bc 2019-06-01 stsp
6946 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6947 234035bc 2019-06-01 stsp switch (ch) {
6948 234035bc 2019-06-01 stsp default:
6949 234035bc 2019-06-01 stsp usage_cherrypick();
6950 234035bc 2019-06-01 stsp /* NOTREACHED */
6951 234035bc 2019-06-01 stsp }
6952 234035bc 2019-06-01 stsp }
6953 234035bc 2019-06-01 stsp
6954 234035bc 2019-06-01 stsp argc -= optind;
6955 234035bc 2019-06-01 stsp argv += optind;
6956 234035bc 2019-06-01 stsp
6957 43012d58 2019-07-14 stsp #ifndef PROFILE
6958 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6959 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6960 43012d58 2019-07-14 stsp err(1, "pledge");
6961 43012d58 2019-07-14 stsp #endif
6962 234035bc 2019-06-01 stsp if (argc != 1)
6963 234035bc 2019-06-01 stsp usage_cherrypick();
6964 234035bc 2019-06-01 stsp
6965 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6966 234035bc 2019-06-01 stsp if (cwd == NULL) {
6967 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6968 234035bc 2019-06-01 stsp goto done;
6969 234035bc 2019-06-01 stsp }
6970 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6971 fa51e947 2020-03-27 stsp if (error) {
6972 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6973 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6974 fa51e947 2020-03-27 stsp cwd);
6975 234035bc 2019-06-01 stsp goto done;
6976 fa51e947 2020-03-27 stsp }
6977 234035bc 2019-06-01 stsp
6978 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6979 c9956ddf 2019-09-08 stsp NULL);
6980 234035bc 2019-06-01 stsp if (error != NULL)
6981 234035bc 2019-06-01 stsp goto done;
6982 234035bc 2019-06-01 stsp
6983 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6984 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6985 234035bc 2019-06-01 stsp if (error)
6986 234035bc 2019-06-01 stsp goto done;
6987 234035bc 2019-06-01 stsp
6988 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6989 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6990 234035bc 2019-06-01 stsp if (error != NULL) {
6991 234035bc 2019-06-01 stsp struct got_reference *ref;
6992 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6993 234035bc 2019-06-01 stsp goto done;
6994 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6995 234035bc 2019-06-01 stsp if (error != NULL)
6996 234035bc 2019-06-01 stsp goto done;
6997 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6998 234035bc 2019-06-01 stsp got_ref_close(ref);
6999 234035bc 2019-06-01 stsp if (error != NULL)
7000 234035bc 2019-06-01 stsp goto done;
7001 234035bc 2019-06-01 stsp }
7002 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
7003 234035bc 2019-06-01 stsp if (error)
7004 234035bc 2019-06-01 stsp goto done;
7005 234035bc 2019-06-01 stsp
7006 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
7007 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
7008 234035bc 2019-06-01 stsp if (error != NULL)
7009 234035bc 2019-06-01 stsp goto done;
7010 234035bc 2019-06-01 stsp
7011 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7012 234035bc 2019-06-01 stsp if (error) {
7013 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
7014 234035bc 2019-06-01 stsp goto done;
7015 234035bc 2019-06-01 stsp error = NULL;
7016 234035bc 2019-06-01 stsp } else {
7017 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
7018 234035bc 2019-06-01 stsp goto done;
7019 234035bc 2019-06-01 stsp }
7020 234035bc 2019-06-01 stsp
7021 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7022 234035bc 2019-06-01 stsp if (error)
7023 234035bc 2019-06-01 stsp goto done;
7024 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7025 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7026 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
7027 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7028 03415a1a 2019-06-02 stsp NULL);
7029 234035bc 2019-06-01 stsp if (error != NULL)
7030 234035bc 2019-06-01 stsp goto done;
7031 234035bc 2019-06-01 stsp
7032 9627c110 2020-04-18 stsp if (upa.did_something)
7033 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7034 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7035 234035bc 2019-06-01 stsp done:
7036 234035bc 2019-06-01 stsp if (commit)
7037 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7038 234035bc 2019-06-01 stsp free(commit_id_str);
7039 234035bc 2019-06-01 stsp if (head_ref)
7040 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7041 234035bc 2019-06-01 stsp if (worktree)
7042 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7043 234035bc 2019-06-01 stsp if (repo)
7044 234035bc 2019-06-01 stsp got_repo_close(repo);
7045 c4296144 2019-05-09 stsp return error;
7046 c4296144 2019-05-09 stsp }
7047 5ef14e63 2019-06-02 stsp
7048 5ef14e63 2019-06-02 stsp __dead static void
7049 5ef14e63 2019-06-02 stsp usage_backout(void)
7050 5ef14e63 2019-06-02 stsp {
7051 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7052 5ef14e63 2019-06-02 stsp exit(1);
7053 5ef14e63 2019-06-02 stsp }
7054 5ef14e63 2019-06-02 stsp
7055 5ef14e63 2019-06-02 stsp static const struct got_error *
7056 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7057 5ef14e63 2019-06-02 stsp {
7058 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7059 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7060 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7061 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7062 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7063 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7064 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7065 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7066 9627c110 2020-04-18 stsp int ch;
7067 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7068 5ef14e63 2019-06-02 stsp
7069 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7070 5ef14e63 2019-06-02 stsp switch (ch) {
7071 5ef14e63 2019-06-02 stsp default:
7072 5ef14e63 2019-06-02 stsp usage_backout();
7073 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7074 5ef14e63 2019-06-02 stsp }
7075 5ef14e63 2019-06-02 stsp }
7076 5ef14e63 2019-06-02 stsp
7077 5ef14e63 2019-06-02 stsp argc -= optind;
7078 5ef14e63 2019-06-02 stsp argv += optind;
7079 5ef14e63 2019-06-02 stsp
7080 43012d58 2019-07-14 stsp #ifndef PROFILE
7081 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7082 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7083 43012d58 2019-07-14 stsp err(1, "pledge");
7084 43012d58 2019-07-14 stsp #endif
7085 5ef14e63 2019-06-02 stsp if (argc != 1)
7086 5ef14e63 2019-06-02 stsp usage_backout();
7087 5ef14e63 2019-06-02 stsp
7088 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7089 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7090 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7091 5ef14e63 2019-06-02 stsp goto done;
7092 5ef14e63 2019-06-02 stsp }
7093 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7094 fa51e947 2020-03-27 stsp if (error) {
7095 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7096 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7097 5ef14e63 2019-06-02 stsp goto done;
7098 fa51e947 2020-03-27 stsp }
7099 5ef14e63 2019-06-02 stsp
7100 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7101 c9956ddf 2019-09-08 stsp NULL);
7102 5ef14e63 2019-06-02 stsp if (error != NULL)
7103 5ef14e63 2019-06-02 stsp goto done;
7104 5ef14e63 2019-06-02 stsp
7105 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7106 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7107 5ef14e63 2019-06-02 stsp if (error)
7108 5ef14e63 2019-06-02 stsp goto done;
7109 5ef14e63 2019-06-02 stsp
7110 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7111 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7112 5ef14e63 2019-06-02 stsp if (error != NULL) {
7113 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7114 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7115 5ef14e63 2019-06-02 stsp goto done;
7116 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7117 5ef14e63 2019-06-02 stsp if (error != NULL)
7118 5ef14e63 2019-06-02 stsp goto done;
7119 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7120 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7121 5ef14e63 2019-06-02 stsp if (error != NULL)
7122 5ef14e63 2019-06-02 stsp goto done;
7123 5ef14e63 2019-06-02 stsp }
7124 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7125 5ef14e63 2019-06-02 stsp if (error)
7126 5ef14e63 2019-06-02 stsp goto done;
7127 5ef14e63 2019-06-02 stsp
7128 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7129 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7130 5ef14e63 2019-06-02 stsp if (error != NULL)
7131 5ef14e63 2019-06-02 stsp goto done;
7132 5ef14e63 2019-06-02 stsp
7133 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7134 5ef14e63 2019-06-02 stsp if (error)
7135 5ef14e63 2019-06-02 stsp goto done;
7136 5ef14e63 2019-06-02 stsp
7137 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7138 5ef14e63 2019-06-02 stsp if (error)
7139 5ef14e63 2019-06-02 stsp goto done;
7140 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7141 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7142 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7143 5ef14e63 2019-06-02 stsp goto done;
7144 5ef14e63 2019-06-02 stsp }
7145 5ef14e63 2019-06-02 stsp
7146 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7147 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7148 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7149 5ef14e63 2019-06-02 stsp if (error != NULL)
7150 5ef14e63 2019-06-02 stsp goto done;
7151 5ef14e63 2019-06-02 stsp
7152 9627c110 2020-04-18 stsp if (upa.did_something)
7153 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7154 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7155 5ef14e63 2019-06-02 stsp done:
7156 5ef14e63 2019-06-02 stsp if (commit)
7157 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7158 5ef14e63 2019-06-02 stsp free(commit_id_str);
7159 5ef14e63 2019-06-02 stsp if (head_ref)
7160 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7161 818c7501 2019-07-11 stsp if (worktree)
7162 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7163 818c7501 2019-07-11 stsp if (repo)
7164 818c7501 2019-07-11 stsp got_repo_close(repo);
7165 818c7501 2019-07-11 stsp return error;
7166 818c7501 2019-07-11 stsp }
7167 818c7501 2019-07-11 stsp
7168 818c7501 2019-07-11 stsp __dead static void
7169 818c7501 2019-07-11 stsp usage_rebase(void)
7170 818c7501 2019-07-11 stsp {
7171 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7172 af54c8f8 2019-07-11 stsp getprogname());
7173 818c7501 2019-07-11 stsp exit(1);
7174 0ebf8283 2019-07-24 stsp }
7175 0ebf8283 2019-07-24 stsp
7176 0ebf8283 2019-07-24 stsp void
7177 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7178 0ebf8283 2019-07-24 stsp {
7179 0ebf8283 2019-07-24 stsp char *nl;
7180 0ebf8283 2019-07-24 stsp size_t len;
7181 0ebf8283 2019-07-24 stsp
7182 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7183 0ebf8283 2019-07-24 stsp if (len > limit)
7184 0ebf8283 2019-07-24 stsp len = limit;
7185 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7186 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7187 0ebf8283 2019-07-24 stsp if (nl)
7188 0ebf8283 2019-07-24 stsp *nl = '\0';
7189 0ebf8283 2019-07-24 stsp }
7190 0ebf8283 2019-07-24 stsp
7191 0ebf8283 2019-07-24 stsp static const struct got_error *
7192 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7193 0ebf8283 2019-07-24 stsp {
7194 5943eee2 2019-08-13 stsp const struct got_error *err;
7195 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7196 5943eee2 2019-08-13 stsp const char *s;
7197 0ebf8283 2019-07-24 stsp
7198 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7199 5943eee2 2019-08-13 stsp if (err)
7200 5943eee2 2019-08-13 stsp return err;
7201 0ebf8283 2019-07-24 stsp
7202 5943eee2 2019-08-13 stsp s = logmsg0;
7203 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7204 5943eee2 2019-08-13 stsp s++;
7205 5943eee2 2019-08-13 stsp
7206 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7207 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7208 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7209 5943eee2 2019-08-13 stsp goto done;
7210 5943eee2 2019-08-13 stsp }
7211 0ebf8283 2019-07-24 stsp
7212 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7213 5943eee2 2019-08-13 stsp done:
7214 5943eee2 2019-08-13 stsp free(logmsg0);
7215 a0ea4fc0 2020-02-28 stsp return err;
7216 a0ea4fc0 2020-02-28 stsp }
7217 a0ea4fc0 2020-02-28 stsp
7218 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7219 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7220 a0ea4fc0 2020-02-28 stsp {
7221 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7222 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7223 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7224 a0ea4fc0 2020-02-28 stsp
7225 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7226 a0ea4fc0 2020-02-28 stsp if (err)
7227 a0ea4fc0 2020-02-28 stsp return err;
7228 a0ea4fc0 2020-02-28 stsp
7229 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7230 a0ea4fc0 2020-02-28 stsp if (err)
7231 a0ea4fc0 2020-02-28 stsp goto done;
7232 a0ea4fc0 2020-02-28 stsp
7233 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7234 a0ea4fc0 2020-02-28 stsp
7235 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7236 a0ea4fc0 2020-02-28 stsp if (err)
7237 a0ea4fc0 2020-02-28 stsp goto done;
7238 a0ea4fc0 2020-02-28 stsp
7239 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7240 a0ea4fc0 2020-02-28 stsp done:
7241 a0ea4fc0 2020-02-28 stsp free(id_str);
7242 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7243 a0ea4fc0 2020-02-28 stsp free(logmsg);
7244 5943eee2 2019-08-13 stsp return err;
7245 818c7501 2019-07-11 stsp }
7246 818c7501 2019-07-11 stsp
7247 818c7501 2019-07-11 stsp static const struct got_error *
7248 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7249 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7250 818c7501 2019-07-11 stsp {
7251 818c7501 2019-07-11 stsp const struct got_error *err;
7252 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7253 818c7501 2019-07-11 stsp
7254 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7255 818c7501 2019-07-11 stsp if (err)
7256 818c7501 2019-07-11 stsp goto done;
7257 818c7501 2019-07-11 stsp
7258 ff0d2220 2019-07-11 stsp if (new_id) {
7259 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7260 ff0d2220 2019-07-11 stsp if (err)
7261 ff0d2220 2019-07-11 stsp goto done;
7262 ff0d2220 2019-07-11 stsp }
7263 818c7501 2019-07-11 stsp
7264 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7265 ff0d2220 2019-07-11 stsp if (new_id_str)
7266 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7267 0ebf8283 2019-07-24 stsp
7268 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7269 0ebf8283 2019-07-24 stsp if (err)
7270 0ebf8283 2019-07-24 stsp goto done;
7271 0ebf8283 2019-07-24 stsp
7272 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7273 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7274 818c7501 2019-07-11 stsp done:
7275 818c7501 2019-07-11 stsp free(old_id_str);
7276 818c7501 2019-07-11 stsp free(new_id_str);
7277 272a1371 2020-02-28 stsp free(logmsg);
7278 818c7501 2019-07-11 stsp return err;
7279 818c7501 2019-07-11 stsp }
7280 818c7501 2019-07-11 stsp
7281 1ee397ad 2019-07-12 stsp static const struct got_error *
7282 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7283 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7284 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7285 818c7501 2019-07-11 stsp {
7286 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7287 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7288 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7289 818c7501 2019-07-11 stsp }
7290 818c7501 2019-07-11 stsp
7291 818c7501 2019-07-11 stsp static const struct got_error *
7292 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7293 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7294 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7295 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7296 ff0d2220 2019-07-11 stsp {
7297 ff0d2220 2019-07-11 stsp const struct got_error *error;
7298 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7299 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7300 ff0d2220 2019-07-11 stsp
7301 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7302 ff0d2220 2019-07-11 stsp if (error)
7303 ff0d2220 2019-07-11 stsp return error;
7304 ff0d2220 2019-07-11 stsp
7305 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7306 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7307 ff0d2220 2019-07-11 stsp if (error) {
7308 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7309 ff0d2220 2019-07-11 stsp goto done;
7310 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7311 ff0d2220 2019-07-11 stsp } else {
7312 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7313 ff0d2220 2019-07-11 stsp free(new_commit_id);
7314 ff0d2220 2019-07-11 stsp }
7315 ff0d2220 2019-07-11 stsp done:
7316 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7317 ff0d2220 2019-07-11 stsp return error;
7318 64c6d990 2019-07-11 stsp }
7319 64c6d990 2019-07-11 stsp
7320 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7321 64c6d990 2019-07-11 stsp const char *path_prefix;
7322 64c6d990 2019-07-11 stsp size_t len;
7323 8ca9bd68 2019-07-25 stsp int errcode;
7324 64c6d990 2019-07-11 stsp };
7325 64c6d990 2019-07-11 stsp
7326 64c6d990 2019-07-11 stsp static const struct got_error *
7327 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7328 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7329 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7330 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7331 64c6d990 2019-07-11 stsp {
7332 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7333 64c6d990 2019-07-11 stsp
7334 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7335 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7336 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7337 64c6d990 2019-07-11 stsp
7338 64c6d990 2019-07-11 stsp return NULL;
7339 64c6d990 2019-07-11 stsp }
7340 64c6d990 2019-07-11 stsp
7341 64c6d990 2019-07-11 stsp static const struct got_error *
7342 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7343 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7344 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7345 64c6d990 2019-07-11 stsp {
7346 64c6d990 2019-07-11 stsp const struct got_error *err;
7347 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7348 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7349 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7350 64c6d990 2019-07-11 stsp
7351 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7352 64c6d990 2019-07-11 stsp return NULL;
7353 64c6d990 2019-07-11 stsp
7354 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7355 64c6d990 2019-07-11 stsp if (err)
7356 64c6d990 2019-07-11 stsp goto done;
7357 64c6d990 2019-07-11 stsp
7358 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7359 64c6d990 2019-07-11 stsp if (err)
7360 64c6d990 2019-07-11 stsp goto done;
7361 64c6d990 2019-07-11 stsp
7362 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7363 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7364 64c6d990 2019-07-11 stsp if (err)
7365 64c6d990 2019-07-11 stsp goto done;
7366 64c6d990 2019-07-11 stsp
7367 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7368 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7369 64c6d990 2019-07-11 stsp if (err)
7370 64c6d990 2019-07-11 stsp goto done;
7371 64c6d990 2019-07-11 stsp
7372 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7373 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7374 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7375 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7376 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7377 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7378 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7379 64c6d990 2019-07-11 stsp done:
7380 64c6d990 2019-07-11 stsp if (tree1)
7381 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7382 64c6d990 2019-07-11 stsp if (tree2)
7383 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7384 64c6d990 2019-07-11 stsp if (commit)
7385 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7386 64c6d990 2019-07-11 stsp if (parent_commit)
7387 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7388 64c6d990 2019-07-11 stsp return err;
7389 ff0d2220 2019-07-11 stsp }
7390 ff0d2220 2019-07-11 stsp
7391 ff0d2220 2019-07-11 stsp static const struct got_error *
7392 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7393 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7394 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7395 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7396 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7397 af61c510 2019-07-19 stsp {
7398 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7399 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7400 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7401 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7402 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7403 af61c510 2019-07-19 stsp
7404 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7405 af61c510 2019-07-19 stsp if (err)
7406 af61c510 2019-07-19 stsp return err;
7407 af61c510 2019-07-19 stsp
7408 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7409 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7410 af61c510 2019-07-19 stsp if (err)
7411 af61c510 2019-07-19 stsp goto done;
7412 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7413 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7414 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7415 af61c510 2019-07-19 stsp if (err) {
7416 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7417 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7418 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7419 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7420 af61c510 2019-07-19 stsp "been reached?!?");
7421 ee780d5c 2020-01-04 stsp }
7422 ee780d5c 2020-01-04 stsp goto done;
7423 af61c510 2019-07-19 stsp } else {
7424 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7425 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7426 af61c510 2019-07-19 stsp if (err)
7427 af61c510 2019-07-19 stsp goto done;
7428 af61c510 2019-07-19 stsp
7429 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7430 af61c510 2019-07-19 stsp if (err)
7431 af61c510 2019-07-19 stsp goto done;
7432 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7433 af61c510 2019-07-19 stsp commit_id = parent_id;
7434 af61c510 2019-07-19 stsp }
7435 af61c510 2019-07-19 stsp }
7436 af61c510 2019-07-19 stsp done:
7437 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7438 af61c510 2019-07-19 stsp return err;
7439 af61c510 2019-07-19 stsp }
7440 af61c510 2019-07-19 stsp
7441 af61c510 2019-07-19 stsp static const struct got_error *
7442 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7443 818c7501 2019-07-11 stsp {
7444 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7445 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7446 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7447 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7448 818c7501 2019-07-11 stsp char *cwd = NULL;
7449 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7450 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7451 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7452 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7453 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7454 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7455 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7456 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7457 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7458 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7459 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7460 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7461 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7462 818c7501 2019-07-11 stsp
7463 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7464 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7465 818c7501 2019-07-11 stsp
7466 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7467 818c7501 2019-07-11 stsp switch (ch) {
7468 818c7501 2019-07-11 stsp case 'a':
7469 818c7501 2019-07-11 stsp abort_rebase = 1;
7470 818c7501 2019-07-11 stsp break;
7471 818c7501 2019-07-11 stsp case 'c':
7472 818c7501 2019-07-11 stsp continue_rebase = 1;
7473 818c7501 2019-07-11 stsp break;
7474 818c7501 2019-07-11 stsp default:
7475 818c7501 2019-07-11 stsp usage_rebase();
7476 818c7501 2019-07-11 stsp /* NOTREACHED */
7477 818c7501 2019-07-11 stsp }
7478 818c7501 2019-07-11 stsp }
7479 818c7501 2019-07-11 stsp
7480 818c7501 2019-07-11 stsp argc -= optind;
7481 818c7501 2019-07-11 stsp argv += optind;
7482 818c7501 2019-07-11 stsp
7483 43012d58 2019-07-14 stsp #ifndef PROFILE
7484 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7485 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7486 43012d58 2019-07-14 stsp err(1, "pledge");
7487 43012d58 2019-07-14 stsp #endif
7488 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7489 818c7501 2019-07-11 stsp usage_rebase();
7490 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7491 818c7501 2019-07-11 stsp if (argc != 0)
7492 818c7501 2019-07-11 stsp usage_rebase();
7493 818c7501 2019-07-11 stsp } else if (argc != 1)
7494 818c7501 2019-07-11 stsp usage_rebase();
7495 818c7501 2019-07-11 stsp
7496 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7497 818c7501 2019-07-11 stsp if (cwd == NULL) {
7498 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7499 818c7501 2019-07-11 stsp goto done;
7500 818c7501 2019-07-11 stsp }
7501 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7502 fa51e947 2020-03-27 stsp if (error) {
7503 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7504 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7505 818c7501 2019-07-11 stsp goto done;
7506 fa51e947 2020-03-27 stsp }
7507 818c7501 2019-07-11 stsp
7508 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7509 c9956ddf 2019-09-08 stsp NULL);
7510 818c7501 2019-07-11 stsp if (error != NULL)
7511 818c7501 2019-07-11 stsp goto done;
7512 818c7501 2019-07-11 stsp
7513 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7514 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7515 7ef62c4e 2020-02-24 stsp if (error)
7516 7ef62c4e 2020-02-24 stsp goto done;
7517 7ef62c4e 2020-02-24 stsp
7518 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7519 7ef62c4e 2020-02-24 stsp worktree);
7520 818c7501 2019-07-11 stsp if (error)
7521 7ef62c4e 2020-02-24 stsp goto done;
7522 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7523 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7524 818c7501 2019-07-11 stsp goto done;
7525 7ef62c4e 2020-02-24 stsp }
7526 818c7501 2019-07-11 stsp
7527 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7528 818c7501 2019-07-11 stsp if (error)
7529 818c7501 2019-07-11 stsp goto done;
7530 818c7501 2019-07-11 stsp
7531 f6794adc 2019-07-23 stsp if (abort_rebase) {
7532 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7533 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7534 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7535 f6794adc 2019-07-23 stsp goto done;
7536 f6794adc 2019-07-23 stsp }
7537 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7538 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7539 3e3a69f1 2019-07-25 stsp worktree, repo);
7540 818c7501 2019-07-11 stsp if (error)
7541 818c7501 2019-07-11 stsp goto done;
7542 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7543 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7544 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7545 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7546 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7547 818c7501 2019-07-11 stsp if (error)
7548 818c7501 2019-07-11 stsp goto done;
7549 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7550 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7551 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7552 818c7501 2019-07-11 stsp }
7553 818c7501 2019-07-11 stsp
7554 818c7501 2019-07-11 stsp if (continue_rebase) {
7555 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7556 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7557 f6794adc 2019-07-23 stsp goto done;
7558 f6794adc 2019-07-23 stsp }
7559 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7560 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7561 3e3a69f1 2019-07-25 stsp worktree, repo);
7562 818c7501 2019-07-11 stsp if (error)
7563 818c7501 2019-07-11 stsp goto done;
7564 818c7501 2019-07-11 stsp
7565 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7566 01757395 2019-07-12 stsp resume_commit_id, repo);
7567 818c7501 2019-07-11 stsp if (error)
7568 818c7501 2019-07-11 stsp goto done;
7569 818c7501 2019-07-11 stsp
7570 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7571 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7572 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7573 818c7501 2019-07-11 stsp goto done;
7574 818c7501 2019-07-11 stsp }
7575 818c7501 2019-07-11 stsp } else {
7576 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7577 818c7501 2019-07-11 stsp if (error != NULL)
7578 818c7501 2019-07-11 stsp goto done;
7579 ff0d2220 2019-07-11 stsp }
7580 818c7501 2019-07-11 stsp
7581 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7582 ff0d2220 2019-07-11 stsp if (error)
7583 ff0d2220 2019-07-11 stsp goto done;
7584 ff0d2220 2019-07-11 stsp
7585 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7586 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7587 a51a74b3 2019-07-27 stsp
7588 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7589 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7590 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7591 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7592 818c7501 2019-07-11 stsp if (error)
7593 818c7501 2019-07-11 stsp goto done;
7594 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7595 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7596 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7597 818c7501 2019-07-11 stsp "with work tree's branch");
7598 818c7501 2019-07-11 stsp goto done;
7599 818c7501 2019-07-11 stsp }
7600 818c7501 2019-07-11 stsp
7601 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7602 a51a74b3 2019-07-27 stsp if (error) {
7603 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7604 a51a74b3 2019-07-27 stsp goto done;
7605 a51a74b3 2019-07-27 stsp error = NULL;
7606 a51a74b3 2019-07-27 stsp } else {
7607 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7608 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7609 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7610 a51a74b3 2019-07-27 stsp goto done;
7611 a51a74b3 2019-07-27 stsp }
7612 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7613 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7614 818c7501 2019-07-11 stsp if (error)
7615 818c7501 2019-07-11 stsp goto done;
7616 818c7501 2019-07-11 stsp }
7617 818c7501 2019-07-11 stsp
7618 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7619 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7620 818c7501 2019-07-11 stsp if (error)
7621 818c7501 2019-07-11 stsp goto done;
7622 818c7501 2019-07-11 stsp
7623 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7624 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7625 fc66b545 2019-08-12 stsp if (pid == NULL) {
7626 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7627 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7628 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7629 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7630 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7631 fc66b545 2019-08-12 stsp if (error)
7632 fc66b545 2019-08-12 stsp goto done;
7633 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7634 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7635 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7636 9627c110 2020-04-18 stsp
7637 fc66b545 2019-08-12 stsp }
7638 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7639 fc66b545 2019-08-12 stsp goto done;
7640 fc66b545 2019-08-12 stsp }
7641 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7642 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7643 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7644 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7645 818c7501 2019-07-11 stsp commit = NULL;
7646 818c7501 2019-07-11 stsp if (error)
7647 818c7501 2019-07-11 stsp goto done;
7648 64c6d990 2019-07-11 stsp
7649 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7650 38b0338b 2019-11-29 stsp if (continue_rebase) {
7651 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7652 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7653 38b0338b 2019-11-29 stsp goto done;
7654 38b0338b 2019-11-29 stsp } else {
7655 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7656 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7657 38b0338b 2019-11-29 stsp char *id_str;
7658 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7659 38b0338b 2019-11-29 stsp new_base_branch);
7660 38b0338b 2019-11-29 stsp if (error)
7661 38b0338b 2019-11-29 stsp goto done;
7662 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7663 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7664 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7665 38b0338b 2019-11-29 stsp free(id_str);
7666 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7667 38b0338b 2019-11-29 stsp new_head_commit_id);
7668 38b0338b 2019-11-29 stsp if (error)
7669 38b0338b 2019-11-29 stsp goto done;
7670 38b0338b 2019-11-29 stsp }
7671 818c7501 2019-07-11 stsp }
7672 818c7501 2019-07-11 stsp
7673 818c7501 2019-07-11 stsp pid = NULL;
7674 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7675 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7676 9627c110 2020-04-18 stsp
7677 818c7501 2019-07-11 stsp commit_id = qid->id;
7678 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7679 818c7501 2019-07-11 stsp pid = qid;
7680 818c7501 2019-07-11 stsp
7681 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7682 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7683 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7684 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7685 818c7501 2019-07-11 stsp if (error)
7686 818c7501 2019-07-11 stsp goto done;
7687 9627c110 2020-04-18 stsp
7688 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7689 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7690 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7691 818c7501 2019-07-11 stsp
7692 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7693 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7694 a0ea4fc0 2020-02-28 stsp if (error)
7695 a0ea4fc0 2020-02-28 stsp goto done;
7696 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7697 818c7501 2019-07-11 stsp break;
7698 01757395 2019-07-12 stsp }
7699 818c7501 2019-07-11 stsp
7700 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7701 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7702 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7703 818c7501 2019-07-11 stsp if (error)
7704 818c7501 2019-07-11 stsp goto done;
7705 818c7501 2019-07-11 stsp }
7706 818c7501 2019-07-11 stsp
7707 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7708 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7709 818c7501 2019-07-11 stsp if (error)
7710 818c7501 2019-07-11 stsp goto done;
7711 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7712 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7713 818c7501 2019-07-11 stsp } else
7714 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7715 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7716 818c7501 2019-07-11 stsp done:
7717 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7718 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7719 818c7501 2019-07-11 stsp free(resume_commit_id);
7720 818c7501 2019-07-11 stsp free(yca_id);
7721 818c7501 2019-07-11 stsp if (commit)
7722 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7723 818c7501 2019-07-11 stsp if (branch)
7724 818c7501 2019-07-11 stsp got_ref_close(branch);
7725 818c7501 2019-07-11 stsp if (new_base_branch)
7726 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7727 818c7501 2019-07-11 stsp if (tmp_branch)
7728 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7729 5ef14e63 2019-06-02 stsp if (worktree)
7730 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7731 5ef14e63 2019-06-02 stsp if (repo)
7732 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7733 5ef14e63 2019-06-02 stsp return error;
7734 0ebf8283 2019-07-24 stsp }
7735 0ebf8283 2019-07-24 stsp
7736 0ebf8283 2019-07-24 stsp __dead static void
7737 0ebf8283 2019-07-24 stsp usage_histedit(void)
7738 0ebf8283 2019-07-24 stsp {
7739 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7740 0ebf8283 2019-07-24 stsp getprogname());
7741 0ebf8283 2019-07-24 stsp exit(1);
7742 0ebf8283 2019-07-24 stsp }
7743 0ebf8283 2019-07-24 stsp
7744 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7745 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7746 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7747 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7748 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7749 0ebf8283 2019-07-24 stsp
7750 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7751 0ebf8283 2019-07-24 stsp unsigned char code;
7752 0ebf8283 2019-07-24 stsp const char *name;
7753 0ebf8283 2019-07-24 stsp const char *desc;
7754 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7755 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7756 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7757 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7758 82997472 2020-01-29 stsp "be used" },
7759 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7760 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7761 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7762 0ebf8283 2019-07-24 stsp };
7763 0ebf8283 2019-07-24 stsp
7764 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7765 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7766 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7767 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7768 0ebf8283 2019-07-24 stsp char *logmsg;
7769 0ebf8283 2019-07-24 stsp };
7770 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7771 0ebf8283 2019-07-24 stsp
7772 0ebf8283 2019-07-24 stsp static const struct got_error *
7773 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7774 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7775 0ebf8283 2019-07-24 stsp {
7776 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7777 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7778 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7779 8138f3e1 2019-08-11 stsp int n;
7780 0ebf8283 2019-07-24 stsp
7781 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7782 0ebf8283 2019-07-24 stsp if (err)
7783 0ebf8283 2019-07-24 stsp goto done;
7784 0ebf8283 2019-07-24 stsp
7785 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7786 0ebf8283 2019-07-24 stsp if (err)
7787 0ebf8283 2019-07-24 stsp goto done;
7788 0ebf8283 2019-07-24 stsp
7789 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7790 0ebf8283 2019-07-24 stsp if (err)
7791 0ebf8283 2019-07-24 stsp goto done;
7792 0ebf8283 2019-07-24 stsp
7793 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7794 0ebf8283 2019-07-24 stsp if (n < 0)
7795 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7796 0ebf8283 2019-07-24 stsp done:
7797 0ebf8283 2019-07-24 stsp if (commit)
7798 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7799 0ebf8283 2019-07-24 stsp free(id_str);
7800 0ebf8283 2019-07-24 stsp free(logmsg);
7801 0ebf8283 2019-07-24 stsp return err;
7802 0ebf8283 2019-07-24 stsp }
7803 0ebf8283 2019-07-24 stsp
7804 0ebf8283 2019-07-24 stsp static const struct got_error *
7805 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7806 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7807 0ebf8283 2019-07-24 stsp {
7808 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7809 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7810 0ebf8283 2019-07-24 stsp
7811 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7812 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7813 0ebf8283 2019-07-24 stsp
7814 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7815 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7816 0ebf8283 2019-07-24 stsp f, repo);
7817 0ebf8283 2019-07-24 stsp if (err)
7818 0ebf8283 2019-07-24 stsp break;
7819 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7820 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7821 083957f4 2020-02-24 stsp if (n < 0) {
7822 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7823 083957f4 2020-02-24 stsp break;
7824 083957f4 2020-02-24 stsp }
7825 083957f4 2020-02-24 stsp }
7826 0ebf8283 2019-07-24 stsp }
7827 0ebf8283 2019-07-24 stsp
7828 0ebf8283 2019-07-24 stsp return err;
7829 0ebf8283 2019-07-24 stsp }
7830 0ebf8283 2019-07-24 stsp
7831 0ebf8283 2019-07-24 stsp static const struct got_error *
7832 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7833 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7834 0ebf8283 2019-07-24 stsp {
7835 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7836 0ebf8283 2019-07-24 stsp int n, i;
7837 514f2ffe 2020-01-29 stsp char *id_str;
7838 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7839 514f2ffe 2020-01-29 stsp
7840 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7841 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7842 514f2ffe 2020-01-29 stsp if (err)
7843 514f2ffe 2020-01-29 stsp return err;
7844 514f2ffe 2020-01-29 stsp
7845 514f2ffe 2020-01-29 stsp n = fprintf(f,
7846 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7847 514f2ffe 2020-01-29 stsp "# commit %s\n"
7848 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7849 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7850 514f2ffe 2020-01-29 stsp if (n < 0) {
7851 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7852 514f2ffe 2020-01-29 stsp goto done;
7853 514f2ffe 2020-01-29 stsp }
7854 0ebf8283 2019-07-24 stsp
7855 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7856 514f2ffe 2020-01-29 stsp if (n < 0) {
7857 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7858 514f2ffe 2020-01-29 stsp goto done;
7859 514f2ffe 2020-01-29 stsp }
7860 0ebf8283 2019-07-24 stsp
7861 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7862 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7863 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7864 0ebf8283 2019-07-24 stsp cmd->desc);
7865 0ebf8283 2019-07-24 stsp if (n < 0) {
7866 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7867 0ebf8283 2019-07-24 stsp break;
7868 0ebf8283 2019-07-24 stsp }
7869 0ebf8283 2019-07-24 stsp }
7870 514f2ffe 2020-01-29 stsp done:
7871 514f2ffe 2020-01-29 stsp free(id_str);
7872 0ebf8283 2019-07-24 stsp return err;
7873 0ebf8283 2019-07-24 stsp }
7874 0ebf8283 2019-07-24 stsp
7875 0ebf8283 2019-07-24 stsp static const struct got_error *
7876 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7877 0ebf8283 2019-07-24 stsp {
7878 0ebf8283 2019-07-24 stsp static char msg[42];
7879 0ebf8283 2019-07-24 stsp int ret;
7880 0ebf8283 2019-07-24 stsp
7881 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7882 0ebf8283 2019-07-24 stsp lineno);
7883 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7884 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7885 0ebf8283 2019-07-24 stsp
7886 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7887 0ebf8283 2019-07-24 stsp }
7888 0ebf8283 2019-07-24 stsp
7889 0ebf8283 2019-07-24 stsp static const struct got_error *
7890 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7891 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7892 0ebf8283 2019-07-24 stsp {
7893 0ebf8283 2019-07-24 stsp const struct got_error *err;
7894 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7895 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7896 0ebf8283 2019-07-24 stsp
7897 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7898 0ebf8283 2019-07-24 stsp if (err)
7899 0ebf8283 2019-07-24 stsp return err;
7900 0ebf8283 2019-07-24 stsp
7901 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7902 0ebf8283 2019-07-24 stsp if (err)
7903 0ebf8283 2019-07-24 stsp goto done;
7904 0ebf8283 2019-07-24 stsp
7905 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7906 5943eee2 2019-08-13 stsp if (err)
7907 5943eee2 2019-08-13 stsp goto done;
7908 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7909 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7910 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7911 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7912 0ebf8283 2019-07-24 stsp }
7913 0ebf8283 2019-07-24 stsp done:
7914 0ebf8283 2019-07-24 stsp if (folded_commit)
7915 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7916 0ebf8283 2019-07-24 stsp free(id_str);
7917 5943eee2 2019-08-13 stsp free(folded_logmsg);
7918 0ebf8283 2019-07-24 stsp return err;
7919 0ebf8283 2019-07-24 stsp }
7920 0ebf8283 2019-07-24 stsp
7921 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7922 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7923 0ebf8283 2019-07-24 stsp {
7924 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7925 0ebf8283 2019-07-24 stsp
7926 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7927 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7928 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7929 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7930 3f9de99f 2019-07-24 stsp folded = prev;
7931 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7932 0ebf8283 2019-07-24 stsp }
7933 0ebf8283 2019-07-24 stsp
7934 0ebf8283 2019-07-24 stsp return folded;
7935 0ebf8283 2019-07-24 stsp }
7936 0ebf8283 2019-07-24 stsp
7937 0ebf8283 2019-07-24 stsp static const struct got_error *
7938 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7939 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7940 0ebf8283 2019-07-24 stsp {
7941 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7942 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7943 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7944 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7945 1601cb9f 2020-09-11 naddy int logmsg_len;
7946 0ebf8283 2019-07-24 stsp int fd;
7947 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7948 0ebf8283 2019-07-24 stsp
7949 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7950 0ebf8283 2019-07-24 stsp if (err)
7951 0ebf8283 2019-07-24 stsp return err;
7952 0ebf8283 2019-07-24 stsp
7953 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7954 0ebf8283 2019-07-24 stsp if (folded) {
7955 0ebf8283 2019-07-24 stsp while (folded != hle) {
7956 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7957 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7958 3f9de99f 2019-07-24 stsp continue;
7959 3f9de99f 2019-07-24 stsp }
7960 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7961 0ebf8283 2019-07-24 stsp logmsg, repo);
7962 0ebf8283 2019-07-24 stsp if (err)
7963 0ebf8283 2019-07-24 stsp goto done;
7964 0ebf8283 2019-07-24 stsp free(logmsg);
7965 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7966 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7967 0ebf8283 2019-07-24 stsp }
7968 0ebf8283 2019-07-24 stsp }
7969 0ebf8283 2019-07-24 stsp
7970 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7971 0ebf8283 2019-07-24 stsp if (err)
7972 0ebf8283 2019-07-24 stsp goto done;
7973 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7974 5943eee2 2019-08-13 stsp if (err)
7975 5943eee2 2019-08-13 stsp goto done;
7976 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
7977 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7978 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
7979 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
7980 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7981 0ebf8283 2019-07-24 stsp goto done;
7982 0ebf8283 2019-07-24 stsp }
7983 0ebf8283 2019-07-24 stsp free(logmsg);
7984 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7985 0ebf8283 2019-07-24 stsp
7986 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7987 0ebf8283 2019-07-24 stsp if (err)
7988 0ebf8283 2019-07-24 stsp goto done;
7989 0ebf8283 2019-07-24 stsp
7990 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7991 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7992 0ebf8283 2019-07-24 stsp if (err)
7993 0ebf8283 2019-07-24 stsp goto done;
7994 0ebf8283 2019-07-24 stsp
7995 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
7996 0ebf8283 2019-07-24 stsp close(fd);
7997 0ebf8283 2019-07-24 stsp
7998 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7999 0ebf8283 2019-07-24 stsp if (err)
8000 0ebf8283 2019-07-24 stsp goto done;
8001 0ebf8283 2019-07-24 stsp
8002 bfa12d5e 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
8003 bfa12d5e 2020-09-26 stsp logmsg_len);
8004 0ebf8283 2019-07-24 stsp if (err) {
8005 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
8006 0ebf8283 2019-07-24 stsp goto done;
8007 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
8008 0ebf8283 2019-07-24 stsp }
8009 0ebf8283 2019-07-24 stsp done:
8010 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
8011 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
8012 0ebf8283 2019-07-24 stsp free(logmsg_path);
8013 0ebf8283 2019-07-24 stsp free(logmsg);
8014 5943eee2 2019-08-13 stsp free(orig_logmsg);
8015 0ebf8283 2019-07-24 stsp free(editor);
8016 0ebf8283 2019-07-24 stsp if (commit)
8017 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8018 0ebf8283 2019-07-24 stsp return err;
8019 5ef14e63 2019-06-02 stsp }
8020 0ebf8283 2019-07-24 stsp
8021 0ebf8283 2019-07-24 stsp static const struct got_error *
8022 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
8023 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
8024 0ebf8283 2019-07-24 stsp {
8025 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8026 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
8027 0ebf8283 2019-07-24 stsp size_t size;
8028 0ebf8283 2019-07-24 stsp ssize_t len;
8029 0ebf8283 2019-07-24 stsp int lineno = 0, i;
8030 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8031 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8032 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8033 0ebf8283 2019-07-24 stsp
8034 0ebf8283 2019-07-24 stsp for (;;) {
8035 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8036 0ebf8283 2019-07-24 stsp if (len == -1) {
8037 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8038 0ebf8283 2019-07-24 stsp if (feof(f))
8039 0ebf8283 2019-07-24 stsp break;
8040 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8041 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8042 0ebf8283 2019-07-24 stsp break;
8043 0ebf8283 2019-07-24 stsp }
8044 0ebf8283 2019-07-24 stsp lineno++;
8045 0ebf8283 2019-07-24 stsp p = line;
8046 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8047 0ebf8283 2019-07-24 stsp p++;
8048 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8049 0ebf8283 2019-07-24 stsp free(line);
8050 0ebf8283 2019-07-24 stsp line = NULL;
8051 0ebf8283 2019-07-24 stsp continue;
8052 0ebf8283 2019-07-24 stsp }
8053 0ebf8283 2019-07-24 stsp cmd = NULL;
8054 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8055 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8056 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8057 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8058 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8059 0ebf8283 2019-07-24 stsp break;
8060 0ebf8283 2019-07-24 stsp }
8061 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8062 0ebf8283 2019-07-24 stsp p++;
8063 0ebf8283 2019-07-24 stsp break;
8064 0ebf8283 2019-07-24 stsp }
8065 0ebf8283 2019-07-24 stsp }
8066 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8067 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8068 0ebf8283 2019-07-24 stsp break;
8069 0ebf8283 2019-07-24 stsp }
8070 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8071 0ebf8283 2019-07-24 stsp p++;
8072 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8073 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8074 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8075 0ebf8283 2019-07-24 stsp break;
8076 0ebf8283 2019-07-24 stsp }
8077 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8078 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8079 0ebf8283 2019-07-24 stsp if (err)
8080 0ebf8283 2019-07-24 stsp break;
8081 0ebf8283 2019-07-24 stsp } else {
8082 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8083 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8084 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8085 0ebf8283 2019-07-24 stsp break;
8086 0ebf8283 2019-07-24 stsp }
8087 0ebf8283 2019-07-24 stsp }
8088 0ebf8283 2019-07-24 stsp free(line);
8089 0ebf8283 2019-07-24 stsp line = NULL;
8090 0ebf8283 2019-07-24 stsp continue;
8091 0ebf8283 2019-07-24 stsp } else {
8092 0ebf8283 2019-07-24 stsp end = p;
8093 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8094 0ebf8283 2019-07-24 stsp end++;
8095 0ebf8283 2019-07-24 stsp *end = '\0';
8096 0ebf8283 2019-07-24 stsp
8097 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8098 0ebf8283 2019-07-24 stsp if (err) {
8099 0ebf8283 2019-07-24 stsp /* override error code */
8100 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8101 0ebf8283 2019-07-24 stsp break;
8102 0ebf8283 2019-07-24 stsp }
8103 0ebf8283 2019-07-24 stsp }
8104 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8105 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8106 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8107 0ebf8283 2019-07-24 stsp break;
8108 0ebf8283 2019-07-24 stsp }
8109 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8110 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8111 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8112 0ebf8283 2019-07-24 stsp commit_id = NULL;
8113 0ebf8283 2019-07-24 stsp free(line);
8114 0ebf8283 2019-07-24 stsp line = NULL;
8115 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8116 0ebf8283 2019-07-24 stsp }
8117 0ebf8283 2019-07-24 stsp
8118 0ebf8283 2019-07-24 stsp free(line);
8119 0ebf8283 2019-07-24 stsp free(commit_id);
8120 0ebf8283 2019-07-24 stsp return err;
8121 0ebf8283 2019-07-24 stsp }
8122 0ebf8283 2019-07-24 stsp
8123 0ebf8283 2019-07-24 stsp static const struct got_error *
8124 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8125 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8126 bfce7f83 2019-07-27 stsp {
8127 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8128 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8129 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8130 5b87815e 2020-03-05 stsp static char msg[92];
8131 bfce7f83 2019-07-27 stsp char *id_str;
8132 bfce7f83 2019-07-27 stsp
8133 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8134 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8135 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8136 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
8137 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8138 5b87815e 2020-03-05 stsp
8139 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8140 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8141 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8142 5b87815e 2020-03-05 stsp if (hle == hle2)
8143 5b87815e 2020-03-05 stsp continue;
8144 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8145 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8146 5b87815e 2020-03-05 stsp continue;
8147 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8148 5b87815e 2020-03-05 stsp if (err)
8149 5b87815e 2020-03-05 stsp return err;
8150 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8151 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8152 5b87815e 2020-03-05 stsp free(id_str);
8153 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8154 5b87815e 2020-03-05 stsp }
8155 5b87815e 2020-03-05 stsp }
8156 bfce7f83 2019-07-27 stsp
8157 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8158 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8159 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8160 bfce7f83 2019-07-27 stsp break;
8161 bfce7f83 2019-07-27 stsp }
8162 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8163 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8164 bfce7f83 2019-07-27 stsp if (err)
8165 bfce7f83 2019-07-27 stsp return err;
8166 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8167 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8168 bfce7f83 2019-07-27 stsp free(id_str);
8169 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8170 bfce7f83 2019-07-27 stsp }
8171 bfce7f83 2019-07-27 stsp }
8172 bfce7f83 2019-07-27 stsp
8173 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8174 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8175 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8176 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8177 bfce7f83 2019-07-27 stsp
8178 bfce7f83 2019-07-27 stsp return NULL;
8179 bfce7f83 2019-07-27 stsp }
8180 bfce7f83 2019-07-27 stsp
8181 bfce7f83 2019-07-27 stsp static const struct got_error *
8182 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8183 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8184 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8185 0ebf8283 2019-07-24 stsp {
8186 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8187 0ebf8283 2019-07-24 stsp char *editor;
8188 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8189 0ebf8283 2019-07-24 stsp
8190 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8191 0ebf8283 2019-07-24 stsp if (err)
8192 0ebf8283 2019-07-24 stsp return err;
8193 0ebf8283 2019-07-24 stsp
8194 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8195 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8196 0ebf8283 2019-07-24 stsp goto done;
8197 0ebf8283 2019-07-24 stsp }
8198 0ebf8283 2019-07-24 stsp
8199 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8200 0ebf8283 2019-07-24 stsp if (f == NULL) {
8201 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8202 0ebf8283 2019-07-24 stsp goto done;
8203 0ebf8283 2019-07-24 stsp }
8204 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8205 bfce7f83 2019-07-27 stsp if (err)
8206 bfce7f83 2019-07-27 stsp goto done;
8207 bfce7f83 2019-07-27 stsp
8208 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8209 0ebf8283 2019-07-24 stsp done:
8210 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8211 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8212 0ebf8283 2019-07-24 stsp free(editor);
8213 0ebf8283 2019-07-24 stsp return err;
8214 0ebf8283 2019-07-24 stsp }
8215 0ebf8283 2019-07-24 stsp
8216 0ebf8283 2019-07-24 stsp static const struct got_error *
8217 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8218 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8219 514f2ffe 2020-01-29 stsp struct got_repository *);
8220 0ebf8283 2019-07-24 stsp
8221 0ebf8283 2019-07-24 stsp static const struct got_error *
8222 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8223 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8224 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
8225 0ebf8283 2019-07-24 stsp {
8226 0ebf8283 2019-07-24 stsp const struct got_error *err;
8227 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8228 0ebf8283 2019-07-24 stsp char *path = NULL;
8229 0ebf8283 2019-07-24 stsp
8230 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8231 0ebf8283 2019-07-24 stsp if (err)
8232 0ebf8283 2019-07-24 stsp return err;
8233 0ebf8283 2019-07-24 stsp
8234 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8235 0ebf8283 2019-07-24 stsp if (err)
8236 0ebf8283 2019-07-24 stsp goto done;
8237 0ebf8283 2019-07-24 stsp
8238 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8239 0ebf8283 2019-07-24 stsp if (err)
8240 0ebf8283 2019-07-24 stsp goto done;
8241 0ebf8283 2019-07-24 stsp
8242 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8243 083957f4 2020-02-24 stsp rewind(f);
8244 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8245 083957f4 2020-02-24 stsp } else {
8246 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8247 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8248 0ebf8283 2019-07-24 stsp goto done;
8249 083957f4 2020-02-24 stsp }
8250 083957f4 2020-02-24 stsp f = NULL;
8251 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8252 083957f4 2020-02-24 stsp if (err) {
8253 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8254 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8255 083957f4 2020-02-24 stsp goto done;
8256 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8257 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8258 083957f4 2020-02-24 stsp }
8259 0ebf8283 2019-07-24 stsp }
8260 0ebf8283 2019-07-24 stsp done:
8261 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8262 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8263 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8264 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8265 0ebf8283 2019-07-24 stsp free(path);
8266 0ebf8283 2019-07-24 stsp return err;
8267 0ebf8283 2019-07-24 stsp }
8268 0ebf8283 2019-07-24 stsp
8269 0ebf8283 2019-07-24 stsp static const struct got_error *
8270 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8271 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8272 0ebf8283 2019-07-24 stsp {
8273 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8274 0ebf8283 2019-07-24 stsp char *path = NULL;
8275 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8276 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8277 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8278 0ebf8283 2019-07-24 stsp
8279 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8280 0ebf8283 2019-07-24 stsp if (err)
8281 0ebf8283 2019-07-24 stsp return err;
8282 0ebf8283 2019-07-24 stsp
8283 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8284 0ebf8283 2019-07-24 stsp if (f == NULL) {
8285 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8286 0ebf8283 2019-07-24 stsp goto done;
8287 0ebf8283 2019-07-24 stsp }
8288 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8289 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8290 0ebf8283 2019-07-24 stsp repo);
8291 0ebf8283 2019-07-24 stsp if (err)
8292 0ebf8283 2019-07-24 stsp break;
8293 0ebf8283 2019-07-24 stsp
8294 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8295 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8296 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8297 0ebf8283 2019-07-24 stsp if (n < 0) {
8298 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8299 0ebf8283 2019-07-24 stsp break;
8300 0ebf8283 2019-07-24 stsp }
8301 0ebf8283 2019-07-24 stsp }
8302 0ebf8283 2019-07-24 stsp }
8303 0ebf8283 2019-07-24 stsp done:
8304 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8305 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8306 0ebf8283 2019-07-24 stsp free(path);
8307 0ebf8283 2019-07-24 stsp if (commit)
8308 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8309 0ebf8283 2019-07-24 stsp return err;
8310 0ebf8283 2019-07-24 stsp }
8311 0ebf8283 2019-07-24 stsp
8312 bfce7f83 2019-07-27 stsp void
8313 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8314 bfce7f83 2019-07-27 stsp {
8315 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8316 bfce7f83 2019-07-27 stsp
8317 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8318 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8319 bfce7f83 2019-07-27 stsp free(hle);
8320 bfce7f83 2019-07-27 stsp }
8321 bfce7f83 2019-07-27 stsp }
8322 bfce7f83 2019-07-27 stsp
8323 0ebf8283 2019-07-24 stsp static const struct got_error *
8324 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8325 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8326 0ebf8283 2019-07-24 stsp {
8327 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8328 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8329 0ebf8283 2019-07-24 stsp
8330 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8331 0ebf8283 2019-07-24 stsp if (f == NULL) {
8332 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8333 0ebf8283 2019-07-24 stsp goto done;
8334 0ebf8283 2019-07-24 stsp }
8335 0ebf8283 2019-07-24 stsp
8336 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8337 0ebf8283 2019-07-24 stsp done:
8338 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8339 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8340 0ebf8283 2019-07-24 stsp return err;
8341 0ebf8283 2019-07-24 stsp }
8342 0ebf8283 2019-07-24 stsp
8343 0ebf8283 2019-07-24 stsp static const struct got_error *
8344 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8345 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8346 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8347 0ebf8283 2019-07-24 stsp {
8348 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8349 0ebf8283 2019-07-24 stsp int resp = ' ';
8350 0ebf8283 2019-07-24 stsp
8351 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8352 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8353 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8354 0ebf8283 2019-07-24 stsp resp = getchar();
8355 a61a4414 2019-08-07 stsp if (resp == '\n')
8356 a61a4414 2019-08-07 stsp resp = getchar();
8357 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8358 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8359 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8360 bfce7f83 2019-07-27 stsp repo);
8361 426ebf2e 2019-07-25 stsp if (err) {
8362 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8363 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8364 426ebf2e 2019-07-25 stsp break;
8365 bfce7f83 2019-07-27 stsp prev_err = err;
8366 426ebf2e 2019-07-25 stsp resp = ' ';
8367 426ebf2e 2019-07-25 stsp continue;
8368 426ebf2e 2019-07-25 stsp }
8369 bfce7f83 2019-07-27 stsp break;
8370 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8371 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8372 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8373 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8374 426ebf2e 2019-07-25 stsp if (err) {
8375 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8376 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8377 426ebf2e 2019-07-25 stsp break;
8378 bfce7f83 2019-07-27 stsp prev_err = err;
8379 426ebf2e 2019-07-25 stsp resp = ' ';
8380 426ebf2e 2019-07-25 stsp continue;
8381 426ebf2e 2019-07-25 stsp }
8382 bfce7f83 2019-07-27 stsp break;
8383 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8384 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8385 0ebf8283 2019-07-24 stsp break;
8386 426ebf2e 2019-07-25 stsp } else
8387 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8388 0ebf8283 2019-07-24 stsp }
8389 0ebf8283 2019-07-24 stsp
8390 0ebf8283 2019-07-24 stsp return err;
8391 0ebf8283 2019-07-24 stsp }
8392 0ebf8283 2019-07-24 stsp
8393 0ebf8283 2019-07-24 stsp static const struct got_error *
8394 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8395 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8396 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8397 0ebf8283 2019-07-24 stsp {
8398 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8399 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8400 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8401 3e3a69f1 2019-07-25 stsp branch, repo);
8402 0ebf8283 2019-07-24 stsp }
8403 0ebf8283 2019-07-24 stsp
8404 0ebf8283 2019-07-24 stsp static const struct got_error *
8405 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8406 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8407 0ebf8283 2019-07-24 stsp {
8408 0ebf8283 2019-07-24 stsp const struct got_error *err;
8409 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8410 0ebf8283 2019-07-24 stsp
8411 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8412 0ebf8283 2019-07-24 stsp if (err)
8413 0ebf8283 2019-07-24 stsp goto done;
8414 0ebf8283 2019-07-24 stsp
8415 0ebf8283 2019-07-24 stsp if (new_id) {
8416 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8417 0ebf8283 2019-07-24 stsp if (err)
8418 0ebf8283 2019-07-24 stsp goto done;
8419 0ebf8283 2019-07-24 stsp }
8420 0ebf8283 2019-07-24 stsp
8421 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8422 0ebf8283 2019-07-24 stsp if (new_id_str)
8423 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8424 0ebf8283 2019-07-24 stsp
8425 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8426 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8427 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8428 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8429 0ebf8283 2019-07-24 stsp goto done;
8430 0ebf8283 2019-07-24 stsp }
8431 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8432 0ebf8283 2019-07-24 stsp } else {
8433 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8434 0ebf8283 2019-07-24 stsp if (err)
8435 0ebf8283 2019-07-24 stsp goto done;
8436 0ebf8283 2019-07-24 stsp }
8437 0ebf8283 2019-07-24 stsp
8438 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8439 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8440 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8441 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8442 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8443 0ebf8283 2019-07-24 stsp break;
8444 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8445 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8446 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8447 0ebf8283 2019-07-24 stsp logmsg);
8448 0ebf8283 2019-07-24 stsp break;
8449 0ebf8283 2019-07-24 stsp default:
8450 0ebf8283 2019-07-24 stsp break;
8451 0ebf8283 2019-07-24 stsp }
8452 0ebf8283 2019-07-24 stsp done:
8453 0ebf8283 2019-07-24 stsp free(old_id_str);
8454 0ebf8283 2019-07-24 stsp free(new_id_str);
8455 0ebf8283 2019-07-24 stsp return err;
8456 0ebf8283 2019-07-24 stsp }
8457 0ebf8283 2019-07-24 stsp
8458 0ebf8283 2019-07-24 stsp static const struct got_error *
8459 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8460 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8461 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8462 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8463 0ebf8283 2019-07-24 stsp {
8464 0ebf8283 2019-07-24 stsp const struct got_error *err;
8465 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8466 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8467 0ebf8283 2019-07-24 stsp
8468 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8469 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8470 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8471 0ebf8283 2019-07-24 stsp if (err)
8472 0ebf8283 2019-07-24 stsp return err;
8473 0ebf8283 2019-07-24 stsp }
8474 0ebf8283 2019-07-24 stsp
8475 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8476 0ebf8283 2019-07-24 stsp if (err)
8477 0ebf8283 2019-07-24 stsp return err;
8478 0ebf8283 2019-07-24 stsp
8479 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8480 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8481 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8482 0ebf8283 2019-07-24 stsp if (err) {
8483 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8484 0ebf8283 2019-07-24 stsp goto done;
8485 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8486 0ebf8283 2019-07-24 stsp } else {
8487 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8488 0ebf8283 2019-07-24 stsp free(new_commit_id);
8489 0ebf8283 2019-07-24 stsp }
8490 0ebf8283 2019-07-24 stsp done:
8491 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8492 0ebf8283 2019-07-24 stsp return err;
8493 0ebf8283 2019-07-24 stsp }
8494 0ebf8283 2019-07-24 stsp
8495 0ebf8283 2019-07-24 stsp static const struct got_error *
8496 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8497 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8498 0ebf8283 2019-07-24 stsp {
8499 0ebf8283 2019-07-24 stsp const struct got_error *error;
8500 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8501 0ebf8283 2019-07-24 stsp
8502 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8503 0ebf8283 2019-07-24 stsp repo);
8504 0ebf8283 2019-07-24 stsp if (error)
8505 0ebf8283 2019-07-24 stsp return error;
8506 0ebf8283 2019-07-24 stsp
8507 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8508 0ebf8283 2019-07-24 stsp if (error)
8509 0ebf8283 2019-07-24 stsp return error;
8510 0ebf8283 2019-07-24 stsp
8511 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8512 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8513 0ebf8283 2019-07-24 stsp return error;
8514 ab20a43a 2020-01-29 stsp }
8515 ab20a43a 2020-01-29 stsp
8516 ab20a43a 2020-01-29 stsp static const struct got_error *
8517 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8518 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8519 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8520 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8521 ab20a43a 2020-01-29 stsp {
8522 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8523 ab20a43a 2020-01-29 stsp
8524 ab20a43a 2020-01-29 stsp switch (status) {
8525 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8526 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8527 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8528 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8529 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8530 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8531 ab20a43a 2020-01-29 stsp default:
8532 ab20a43a 2020-01-29 stsp break;
8533 ab20a43a 2020-01-29 stsp }
8534 ab20a43a 2020-01-29 stsp
8535 ab20a43a 2020-01-29 stsp switch (staged_status) {
8536 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8537 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8538 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8539 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8540 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8541 ab20a43a 2020-01-29 stsp default:
8542 ab20a43a 2020-01-29 stsp break;
8543 ab20a43a 2020-01-29 stsp }
8544 ab20a43a 2020-01-29 stsp
8545 ab20a43a 2020-01-29 stsp return NULL;
8546 0ebf8283 2019-07-24 stsp }
8547 0ebf8283 2019-07-24 stsp
8548 0ebf8283 2019-07-24 stsp static const struct got_error *
8549 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8550 0ebf8283 2019-07-24 stsp {
8551 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8552 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8553 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8554 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8555 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8556 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8557 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8558 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8559 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8560 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8561 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8562 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8563 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8564 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8565 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8566 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8567 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8568 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8569 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8570 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8571 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8572 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8573 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8574 0ebf8283 2019-07-24 stsp
8575 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8576 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8577 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8578 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8579 0ebf8283 2019-07-24 stsp
8580 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8581 0ebf8283 2019-07-24 stsp switch (ch) {
8582 0ebf8283 2019-07-24 stsp case 'a':
8583 0ebf8283 2019-07-24 stsp abort_edit = 1;
8584 0ebf8283 2019-07-24 stsp break;
8585 0ebf8283 2019-07-24 stsp case 'c':
8586 0ebf8283 2019-07-24 stsp continue_edit = 1;
8587 0ebf8283 2019-07-24 stsp break;
8588 0ebf8283 2019-07-24 stsp case 'F':
8589 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8590 0ebf8283 2019-07-24 stsp break;
8591 083957f4 2020-02-24 stsp case 'm':
8592 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8593 083957f4 2020-02-24 stsp break;
8594 0ebf8283 2019-07-24 stsp default:
8595 0ebf8283 2019-07-24 stsp usage_histedit();
8596 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8597 0ebf8283 2019-07-24 stsp }
8598 0ebf8283 2019-07-24 stsp }
8599 0ebf8283 2019-07-24 stsp
8600 0ebf8283 2019-07-24 stsp argc -= optind;
8601 0ebf8283 2019-07-24 stsp argv += optind;
8602 0ebf8283 2019-07-24 stsp
8603 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8604 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8605 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8606 0ebf8283 2019-07-24 stsp err(1, "pledge");
8607 0ebf8283 2019-07-24 stsp #endif
8608 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8609 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8610 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8611 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8612 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8613 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8614 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8615 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8616 0ebf8283 2019-07-24 stsp if (argc != 0)
8617 0ebf8283 2019-07-24 stsp usage_histedit();
8618 0ebf8283 2019-07-24 stsp
8619 0ebf8283 2019-07-24 stsp /*
8620 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8621 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8622 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8623 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8624 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8625 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8626 0ebf8283 2019-07-24 stsp */
8627 0ebf8283 2019-07-24 stsp
8628 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8629 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8630 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8631 0ebf8283 2019-07-24 stsp goto done;
8632 0ebf8283 2019-07-24 stsp }
8633 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8634 fa51e947 2020-03-27 stsp if (error) {
8635 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8636 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8637 0ebf8283 2019-07-24 stsp goto done;
8638 fa51e947 2020-03-27 stsp }
8639 0ebf8283 2019-07-24 stsp
8640 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8641 c9956ddf 2019-09-08 stsp NULL);
8642 0ebf8283 2019-07-24 stsp if (error != NULL)
8643 0ebf8283 2019-07-24 stsp goto done;
8644 0ebf8283 2019-07-24 stsp
8645 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8646 0ebf8283 2019-07-24 stsp if (error)
8647 0ebf8283 2019-07-24 stsp goto done;
8648 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8649 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8650 0ebf8283 2019-07-24 stsp goto done;
8651 0ebf8283 2019-07-24 stsp }
8652 0ebf8283 2019-07-24 stsp
8653 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8654 0ebf8283 2019-07-24 stsp if (error)
8655 0ebf8283 2019-07-24 stsp goto done;
8656 0ebf8283 2019-07-24 stsp
8657 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8658 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8659 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8660 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8661 083957f4 2020-02-24 stsp "before the -m option can be used");
8662 083957f4 2020-02-24 stsp goto done;
8663 083957f4 2020-02-24 stsp }
8664 083957f4 2020-02-24 stsp
8665 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8666 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8667 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8668 3e3a69f1 2019-07-25 stsp worktree, repo);
8669 0ebf8283 2019-07-24 stsp if (error)
8670 0ebf8283 2019-07-24 stsp goto done;
8671 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8672 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8673 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8674 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8675 0ebf8283 2019-07-24 stsp if (error)
8676 0ebf8283 2019-07-24 stsp goto done;
8677 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8678 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8679 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8680 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8681 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8682 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8683 0ebf8283 2019-07-24 stsp goto done;
8684 0ebf8283 2019-07-24 stsp }
8685 0ebf8283 2019-07-24 stsp
8686 0ebf8283 2019-07-24 stsp if (continue_edit) {
8687 0ebf8283 2019-07-24 stsp char *path;
8688 0ebf8283 2019-07-24 stsp
8689 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8690 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8691 0ebf8283 2019-07-24 stsp goto done;
8692 0ebf8283 2019-07-24 stsp }
8693 0ebf8283 2019-07-24 stsp
8694 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8695 0ebf8283 2019-07-24 stsp if (error)
8696 0ebf8283 2019-07-24 stsp goto done;
8697 0ebf8283 2019-07-24 stsp
8698 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8699 0ebf8283 2019-07-24 stsp free(path);
8700 0ebf8283 2019-07-24 stsp if (error)
8701 0ebf8283 2019-07-24 stsp goto done;
8702 0ebf8283 2019-07-24 stsp
8703 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8704 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8705 3e3a69f1 2019-07-25 stsp worktree, repo);
8706 0ebf8283 2019-07-24 stsp if (error)
8707 0ebf8283 2019-07-24 stsp goto done;
8708 0ebf8283 2019-07-24 stsp
8709 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8710 0ebf8283 2019-07-24 stsp if (error)
8711 0ebf8283 2019-07-24 stsp goto done;
8712 0ebf8283 2019-07-24 stsp
8713 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8714 0ebf8283 2019-07-24 stsp head_commit_id);
8715 0ebf8283 2019-07-24 stsp if (error)
8716 0ebf8283 2019-07-24 stsp goto done;
8717 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8718 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8719 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8720 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8721 3aac7cf7 2019-07-25 stsp goto done;
8722 3aac7cf7 2019-07-25 stsp }
8723 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8724 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8725 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8726 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8727 0ebf8283 2019-07-24 stsp commit = NULL;
8728 0ebf8283 2019-07-24 stsp if (error)
8729 0ebf8283 2019-07-24 stsp goto done;
8730 0ebf8283 2019-07-24 stsp } else {
8731 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8732 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8733 0ebf8283 2019-07-24 stsp goto done;
8734 0ebf8283 2019-07-24 stsp }
8735 0ebf8283 2019-07-24 stsp
8736 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8737 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8738 0ebf8283 2019-07-24 stsp if (error != NULL)
8739 0ebf8283 2019-07-24 stsp goto done;
8740 0ebf8283 2019-07-24 stsp
8741 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8742 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8743 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8744 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8745 c7d20a3f 2019-07-30 stsp goto done;
8746 c7d20a3f 2019-07-30 stsp }
8747 c7d20a3f 2019-07-30 stsp
8748 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8749 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8750 bfce7f83 2019-07-27 stsp branch = NULL;
8751 0ebf8283 2019-07-24 stsp if (error)
8752 0ebf8283 2019-07-24 stsp goto done;
8753 0ebf8283 2019-07-24 stsp
8754 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8755 0ebf8283 2019-07-24 stsp head_commit_id);
8756 0ebf8283 2019-07-24 stsp if (error)
8757 0ebf8283 2019-07-24 stsp goto done;
8758 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8759 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8760 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8761 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8762 3aac7cf7 2019-07-25 stsp goto done;
8763 3aac7cf7 2019-07-25 stsp }
8764 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8765 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8766 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8767 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8768 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8769 0ebf8283 2019-07-24 stsp commit = NULL;
8770 0ebf8283 2019-07-24 stsp if (error)
8771 0ebf8283 2019-07-24 stsp goto done;
8772 0ebf8283 2019-07-24 stsp
8773 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8774 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8775 c5996fff 2020-01-29 stsp goto done;
8776 c5996fff 2020-01-29 stsp }
8777 c5996fff 2020-01-29 stsp
8778 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8779 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8780 bfce7f83 2019-07-27 stsp if (error)
8781 bfce7f83 2019-07-27 stsp goto done;
8782 bfce7f83 2019-07-27 stsp
8783 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8784 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8785 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8786 6ba2bea2 2019-07-27 stsp if (error) {
8787 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8788 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8789 9627c110 2020-04-18 stsp update_progress, &upa);
8790 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8791 0ebf8283 2019-07-24 stsp goto done;
8792 6ba2bea2 2019-07-27 stsp }
8793 0ebf8283 2019-07-24 stsp } else {
8794 514f2ffe 2020-01-29 stsp const char *branch_name;
8795 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8796 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8797 514f2ffe 2020-01-29 stsp branch_name += 11;
8798 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8799 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8800 6ba2bea2 2019-07-27 stsp if (error) {
8801 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8802 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8803 9627c110 2020-04-18 stsp update_progress, &upa);
8804 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8805 0ebf8283 2019-07-24 stsp goto done;
8806 6ba2bea2 2019-07-27 stsp }
8807 0ebf8283 2019-07-24 stsp
8808 0ebf8283 2019-07-24 stsp }
8809 0ebf8283 2019-07-24 stsp
8810 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8811 0ebf8283 2019-07-24 stsp repo);
8812 6ba2bea2 2019-07-27 stsp if (error) {
8813 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8814 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8815 9627c110 2020-04-18 stsp update_progress, &upa);
8816 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8817 0ebf8283 2019-07-24 stsp goto done;
8818 6ba2bea2 2019-07-27 stsp }
8819 0ebf8283 2019-07-24 stsp
8820 0ebf8283 2019-07-24 stsp }
8821 0ebf8283 2019-07-24 stsp
8822 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8823 4ec14e60 2019-08-28 hiltjo if (error)
8824 0ebf8283 2019-07-24 stsp goto done;
8825 0ebf8283 2019-07-24 stsp
8826 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8827 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8828 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8829 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8830 0ebf8283 2019-07-24 stsp continue;
8831 0ebf8283 2019-07-24 stsp
8832 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8833 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8834 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8835 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8836 62d463ca 2020-10-20 naddy repo);
8837 ab20a43a 2020-01-29 stsp if (error)
8838 ab20a43a 2020-01-29 stsp goto done;
8839 0ebf8283 2019-07-24 stsp } else {
8840 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8841 ab20a43a 2020-01-29 stsp int have_changes = 0;
8842 ab20a43a 2020-01-29 stsp
8843 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8844 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8845 ab20a43a 2020-01-29 stsp if (error)
8846 ab20a43a 2020-01-29 stsp goto done;
8847 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8848 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8849 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8850 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8851 ab20a43a 2020-01-29 stsp if (error) {
8852 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8853 ab20a43a 2020-01-29 stsp goto done;
8854 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8855 ab20a43a 2020-01-29 stsp goto done;
8856 ab20a43a 2020-01-29 stsp }
8857 ab20a43a 2020-01-29 stsp if (have_changes) {
8858 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8859 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8860 ab20a43a 2020-01-29 stsp if (error)
8861 ab20a43a 2020-01-29 stsp goto done;
8862 ab20a43a 2020-01-29 stsp } else {
8863 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8864 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8865 ab20a43a 2020-01-29 stsp if (error)
8866 ab20a43a 2020-01-29 stsp goto done;
8867 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8868 ab20a43a 2020-01-29 stsp hle, NULL);
8869 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8870 ab20a43a 2020-01-29 stsp commit = NULL;
8871 ab20a43a 2020-01-29 stsp if (error)
8872 ab20a43a 2020-01-29 stsp goto done;
8873 ab20a43a 2020-01-29 stsp }
8874 0ebf8283 2019-07-24 stsp }
8875 0ebf8283 2019-07-24 stsp continue;
8876 0ebf8283 2019-07-24 stsp }
8877 0ebf8283 2019-07-24 stsp
8878 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8879 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8880 0ebf8283 2019-07-24 stsp if (error)
8881 0ebf8283 2019-07-24 stsp goto done;
8882 0ebf8283 2019-07-24 stsp continue;
8883 0ebf8283 2019-07-24 stsp }
8884 0ebf8283 2019-07-24 stsp
8885 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8886 0ebf8283 2019-07-24 stsp hle->commit_id);
8887 0ebf8283 2019-07-24 stsp if (error)
8888 0ebf8283 2019-07-24 stsp goto done;
8889 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8890 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8891 0ebf8283 2019-07-24 stsp
8892 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8893 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8894 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8895 0ebf8283 2019-07-24 stsp if (error)
8896 0ebf8283 2019-07-24 stsp goto done;
8897 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8898 0ebf8283 2019-07-24 stsp commit = NULL;
8899 0ebf8283 2019-07-24 stsp
8900 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8901 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8902 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8903 9627c110 2020-04-18 stsp
8904 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8905 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8906 a0ea4fc0 2020-02-28 stsp repo);
8907 a0ea4fc0 2020-02-28 stsp if (error)
8908 a0ea4fc0 2020-02-28 stsp goto done;
8909 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8910 0ebf8283 2019-07-24 stsp break;
8911 0ebf8283 2019-07-24 stsp }
8912 0ebf8283 2019-07-24 stsp
8913 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8914 0ebf8283 2019-07-24 stsp char *id_str;
8915 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8916 0ebf8283 2019-07-24 stsp if (error)
8917 0ebf8283 2019-07-24 stsp goto done;
8918 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8919 0ebf8283 2019-07-24 stsp id_str);
8920 0ebf8283 2019-07-24 stsp free(id_str);
8921 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8922 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8923 3e3a69f1 2019-07-25 stsp fileindex);
8924 0ebf8283 2019-07-24 stsp goto done;
8925 0ebf8283 2019-07-24 stsp }
8926 0ebf8283 2019-07-24 stsp
8927 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8928 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8929 0ebf8283 2019-07-24 stsp if (error)
8930 0ebf8283 2019-07-24 stsp goto done;
8931 0ebf8283 2019-07-24 stsp continue;
8932 0ebf8283 2019-07-24 stsp }
8933 0ebf8283 2019-07-24 stsp
8934 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8935 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8936 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8937 0ebf8283 2019-07-24 stsp if (error)
8938 0ebf8283 2019-07-24 stsp goto done;
8939 0ebf8283 2019-07-24 stsp }
8940 0ebf8283 2019-07-24 stsp
8941 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8942 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8943 0ebf8283 2019-07-24 stsp if (error)
8944 0ebf8283 2019-07-24 stsp goto done;
8945 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8946 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8947 0ebf8283 2019-07-24 stsp } else
8948 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8949 3e3a69f1 2019-07-25 stsp branch, repo);
8950 0ebf8283 2019-07-24 stsp done:
8951 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8952 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8953 0ebf8283 2019-07-24 stsp free(head_commit_id);
8954 0ebf8283 2019-07-24 stsp free(base_commit_id);
8955 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8956 0ebf8283 2019-07-24 stsp if (commit)
8957 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8958 0ebf8283 2019-07-24 stsp if (branch)
8959 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8960 0ebf8283 2019-07-24 stsp if (tmp_branch)
8961 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8962 0ebf8283 2019-07-24 stsp if (worktree)
8963 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8964 2822a352 2019-10-15 stsp if (repo)
8965 2822a352 2019-10-15 stsp got_repo_close(repo);
8966 2822a352 2019-10-15 stsp return error;
8967 2822a352 2019-10-15 stsp }
8968 2822a352 2019-10-15 stsp
8969 2822a352 2019-10-15 stsp __dead static void
8970 2822a352 2019-10-15 stsp usage_integrate(void)
8971 2822a352 2019-10-15 stsp {
8972 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8973 2822a352 2019-10-15 stsp exit(1);
8974 2822a352 2019-10-15 stsp }
8975 2822a352 2019-10-15 stsp
8976 2822a352 2019-10-15 stsp static const struct got_error *
8977 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8978 2822a352 2019-10-15 stsp {
8979 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8980 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8981 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8982 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8983 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8984 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8985 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8986 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8987 9627c110 2020-04-18 stsp int ch;
8988 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8989 2822a352 2019-10-15 stsp
8990 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8991 2822a352 2019-10-15 stsp switch (ch) {
8992 2822a352 2019-10-15 stsp default:
8993 2822a352 2019-10-15 stsp usage_integrate();
8994 2822a352 2019-10-15 stsp /* NOTREACHED */
8995 2822a352 2019-10-15 stsp }
8996 2822a352 2019-10-15 stsp }
8997 2822a352 2019-10-15 stsp
8998 2822a352 2019-10-15 stsp argc -= optind;
8999 2822a352 2019-10-15 stsp argv += optind;
9000 2822a352 2019-10-15 stsp
9001 2822a352 2019-10-15 stsp if (argc != 1)
9002 2822a352 2019-10-15 stsp usage_integrate();
9003 2822a352 2019-10-15 stsp branch_arg = argv[0];
9004 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
9005 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9006 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
9007 2822a352 2019-10-15 stsp err(1, "pledge");
9008 b08a0ccd 2020-09-24 stsp #endif
9009 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
9010 2822a352 2019-10-15 stsp if (cwd == NULL) {
9011 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
9012 2822a352 2019-10-15 stsp goto done;
9013 2822a352 2019-10-15 stsp }
9014 2822a352 2019-10-15 stsp
9015 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
9016 fa51e947 2020-03-27 stsp if (error) {
9017 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9018 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
9019 fa51e947 2020-03-27 stsp cwd);
9020 2822a352 2019-10-15 stsp goto done;
9021 fa51e947 2020-03-27 stsp }
9022 2822a352 2019-10-15 stsp
9023 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
9024 2822a352 2019-10-15 stsp if (error)
9025 2822a352 2019-10-15 stsp goto done;
9026 2822a352 2019-10-15 stsp
9027 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9028 2822a352 2019-10-15 stsp NULL);
9029 2822a352 2019-10-15 stsp if (error != NULL)
9030 2822a352 2019-10-15 stsp goto done;
9031 2822a352 2019-10-15 stsp
9032 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9033 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9034 2822a352 2019-10-15 stsp if (error)
9035 2822a352 2019-10-15 stsp goto done;
9036 2822a352 2019-10-15 stsp
9037 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9038 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9039 2822a352 2019-10-15 stsp goto done;
9040 2822a352 2019-10-15 stsp }
9041 2822a352 2019-10-15 stsp
9042 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9043 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9044 2822a352 2019-10-15 stsp if (error)
9045 2822a352 2019-10-15 stsp goto done;
9046 2822a352 2019-10-15 stsp
9047 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9048 2822a352 2019-10-15 stsp if (refname == NULL) {
9049 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9050 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9051 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9052 2822a352 2019-10-15 stsp goto done;
9053 2822a352 2019-10-15 stsp }
9054 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9055 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9056 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9057 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9058 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9059 2822a352 2019-10-15 stsp goto done;
9060 2822a352 2019-10-15 stsp }
9061 2822a352 2019-10-15 stsp
9062 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9063 2822a352 2019-10-15 stsp if (error)
9064 2822a352 2019-10-15 stsp goto done;
9065 2822a352 2019-10-15 stsp
9066 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9067 2822a352 2019-10-15 stsp if (error)
9068 2822a352 2019-10-15 stsp goto done;
9069 2822a352 2019-10-15 stsp
9070 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9071 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9072 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9073 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9074 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9075 2822a352 2019-10-15 stsp goto done;
9076 2822a352 2019-10-15 stsp }
9077 2822a352 2019-10-15 stsp
9078 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9079 2822a352 2019-10-15 stsp if (error) {
9080 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9081 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9082 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9083 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9084 2822a352 2019-10-15 stsp goto done;
9085 2822a352 2019-10-15 stsp }
9086 2822a352 2019-10-15 stsp
9087 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9088 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9089 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9090 2822a352 2019-10-15 stsp check_cancelled, NULL);
9091 2822a352 2019-10-15 stsp if (error)
9092 2822a352 2019-10-15 stsp goto done;
9093 2822a352 2019-10-15 stsp
9094 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9095 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9096 2822a352 2019-10-15 stsp done:
9097 715dc77e 2019-08-03 stsp if (repo)
9098 715dc77e 2019-08-03 stsp got_repo_close(repo);
9099 2822a352 2019-10-15 stsp if (worktree)
9100 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9101 2822a352 2019-10-15 stsp free(cwd);
9102 2822a352 2019-10-15 stsp free(base_commit_id);
9103 2822a352 2019-10-15 stsp free(commit_id);
9104 2822a352 2019-10-15 stsp free(refname);
9105 2822a352 2019-10-15 stsp free(base_refname);
9106 715dc77e 2019-08-03 stsp return error;
9107 715dc77e 2019-08-03 stsp }
9108 715dc77e 2019-08-03 stsp
9109 715dc77e 2019-08-03 stsp __dead static void
9110 715dc77e 2019-08-03 stsp usage_stage(void)
9111 715dc77e 2019-08-03 stsp {
9112 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9113 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9114 715dc77e 2019-08-03 stsp getprogname());
9115 715dc77e 2019-08-03 stsp exit(1);
9116 715dc77e 2019-08-03 stsp }
9117 715dc77e 2019-08-03 stsp
9118 715dc77e 2019-08-03 stsp static const struct got_error *
9119 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9120 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9121 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9122 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9123 408b4ebc 2019-08-03 stsp {
9124 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9125 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9126 408b4ebc 2019-08-03 stsp
9127 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9128 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9129 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9130 408b4ebc 2019-08-03 stsp return NULL;
9131 408b4ebc 2019-08-03 stsp
9132 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9133 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9134 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9135 408b4ebc 2019-08-03 stsp else
9136 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9137 408b4ebc 2019-08-03 stsp if (err)
9138 408b4ebc 2019-08-03 stsp return err;
9139 408b4ebc 2019-08-03 stsp
9140 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9141 408b4ebc 2019-08-03 stsp free(id_str);
9142 dc424a06 2019-08-07 stsp return NULL;
9143 dc424a06 2019-08-07 stsp }
9144 2e1f37b0 2019-08-08 stsp
9145 dc424a06 2019-08-07 stsp static const struct got_error *
9146 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9147 715dc77e 2019-08-03 stsp {
9148 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9149 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9150 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9151 715dc77e 2019-08-03 stsp char *cwd = NULL;
9152 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
9153 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
9154 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9155 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
9156 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9157 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9158 715dc77e 2019-08-03 stsp
9159 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
9160 715dc77e 2019-08-03 stsp
9161 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9162 715dc77e 2019-08-03 stsp switch (ch) {
9163 408b4ebc 2019-08-03 stsp case 'l':
9164 408b4ebc 2019-08-03 stsp list_stage = 1;
9165 408b4ebc 2019-08-03 stsp break;
9166 dc424a06 2019-08-07 stsp case 'p':
9167 dc424a06 2019-08-07 stsp pflag = 1;
9168 dc424a06 2019-08-07 stsp break;
9169 dc424a06 2019-08-07 stsp case 'F':
9170 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9171 dc424a06 2019-08-07 stsp break;
9172 35213c7c 2020-07-23 stsp case 'S':
9173 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9174 35213c7c 2020-07-23 stsp break;
9175 715dc77e 2019-08-03 stsp default:
9176 715dc77e 2019-08-03 stsp usage_stage();
9177 715dc77e 2019-08-03 stsp /* NOTREACHED */
9178 715dc77e 2019-08-03 stsp }
9179 715dc77e 2019-08-03 stsp }
9180 715dc77e 2019-08-03 stsp
9181 715dc77e 2019-08-03 stsp argc -= optind;
9182 715dc77e 2019-08-03 stsp argv += optind;
9183 715dc77e 2019-08-03 stsp
9184 715dc77e 2019-08-03 stsp #ifndef PROFILE
9185 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9186 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9187 715dc77e 2019-08-03 stsp err(1, "pledge");
9188 715dc77e 2019-08-03 stsp #endif
9189 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9190 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9191 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9192 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9193 715dc77e 2019-08-03 stsp
9194 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9195 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9196 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9197 715dc77e 2019-08-03 stsp goto done;
9198 715dc77e 2019-08-03 stsp }
9199 715dc77e 2019-08-03 stsp
9200 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9201 fa51e947 2020-03-27 stsp if (error) {
9202 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9203 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9204 715dc77e 2019-08-03 stsp goto done;
9205 fa51e947 2020-03-27 stsp }
9206 715dc77e 2019-08-03 stsp
9207 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9208 c9956ddf 2019-09-08 stsp NULL);
9209 715dc77e 2019-08-03 stsp if (error != NULL)
9210 715dc77e 2019-08-03 stsp goto done;
9211 715dc77e 2019-08-03 stsp
9212 dc424a06 2019-08-07 stsp if (patch_script_path) {
9213 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9214 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9215 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9216 dc424a06 2019-08-07 stsp patch_script_path);
9217 dc424a06 2019-08-07 stsp goto done;
9218 dc424a06 2019-08-07 stsp }
9219 dc424a06 2019-08-07 stsp }
9220 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9221 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9222 715dc77e 2019-08-03 stsp if (error)
9223 715dc77e 2019-08-03 stsp goto done;
9224 715dc77e 2019-08-03 stsp
9225 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9226 6d022e97 2019-08-04 stsp if (error)
9227 6d022e97 2019-08-04 stsp goto done;
9228 715dc77e 2019-08-03 stsp
9229 6d022e97 2019-08-04 stsp if (list_stage)
9230 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9231 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9232 2e1f37b0 2019-08-08 stsp else {
9233 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9234 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9235 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9236 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9237 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9238 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9239 2e1f37b0 2019-08-08 stsp }
9240 ad493afc 2019-08-03 stsp done:
9241 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9242 2e1f37b0 2019-08-08 stsp error == NULL)
9243 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9244 ad493afc 2019-08-03 stsp if (repo)
9245 ad493afc 2019-08-03 stsp got_repo_close(repo);
9246 ad493afc 2019-08-03 stsp if (worktree)
9247 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9248 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9249 ad493afc 2019-08-03 stsp free((char *)pe->path);
9250 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9251 ad493afc 2019-08-03 stsp free(cwd);
9252 ad493afc 2019-08-03 stsp return error;
9253 ad493afc 2019-08-03 stsp }
9254 ad493afc 2019-08-03 stsp
9255 ad493afc 2019-08-03 stsp __dead static void
9256 ad493afc 2019-08-03 stsp usage_unstage(void)
9257 ad493afc 2019-08-03 stsp {
9258 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9259 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9260 ad493afc 2019-08-03 stsp getprogname());
9261 ad493afc 2019-08-03 stsp exit(1);
9262 ad493afc 2019-08-03 stsp }
9263 ad493afc 2019-08-03 stsp
9264 ad493afc 2019-08-03 stsp
9265 ad493afc 2019-08-03 stsp static const struct got_error *
9266 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9267 ad493afc 2019-08-03 stsp {
9268 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9269 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9270 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9271 ad493afc 2019-08-03 stsp char *cwd = NULL;
9272 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9273 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9274 9627c110 2020-04-18 stsp int ch, pflag = 0;
9275 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9276 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9277 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9278 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9279 ad493afc 2019-08-03 stsp
9280 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9281 ad493afc 2019-08-03 stsp
9282 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9283 ad493afc 2019-08-03 stsp switch (ch) {
9284 2e1f37b0 2019-08-08 stsp case 'p':
9285 2e1f37b0 2019-08-08 stsp pflag = 1;
9286 2e1f37b0 2019-08-08 stsp break;
9287 2e1f37b0 2019-08-08 stsp case 'F':
9288 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9289 2e1f37b0 2019-08-08 stsp break;
9290 ad493afc 2019-08-03 stsp default:
9291 ad493afc 2019-08-03 stsp usage_unstage();
9292 ad493afc 2019-08-03 stsp /* NOTREACHED */
9293 ad493afc 2019-08-03 stsp }
9294 ad493afc 2019-08-03 stsp }
9295 ad493afc 2019-08-03 stsp
9296 ad493afc 2019-08-03 stsp argc -= optind;
9297 ad493afc 2019-08-03 stsp argv += optind;
9298 ad493afc 2019-08-03 stsp
9299 ad493afc 2019-08-03 stsp #ifndef PROFILE
9300 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9301 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9302 ad493afc 2019-08-03 stsp err(1, "pledge");
9303 ad493afc 2019-08-03 stsp #endif
9304 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9305 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9306 2e1f37b0 2019-08-08 stsp
9307 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9308 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9309 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9310 ad493afc 2019-08-03 stsp goto done;
9311 ad493afc 2019-08-03 stsp }
9312 ad493afc 2019-08-03 stsp
9313 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9314 fa51e947 2020-03-27 stsp if (error) {
9315 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9316 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9317 ad493afc 2019-08-03 stsp goto done;
9318 fa51e947 2020-03-27 stsp }
9319 ad493afc 2019-08-03 stsp
9320 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9321 c9956ddf 2019-09-08 stsp NULL);
9322 ad493afc 2019-08-03 stsp if (error != NULL)
9323 ad493afc 2019-08-03 stsp goto done;
9324 ad493afc 2019-08-03 stsp
9325 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9326 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9327 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9328 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9329 2e1f37b0 2019-08-08 stsp patch_script_path);
9330 2e1f37b0 2019-08-08 stsp goto done;
9331 2e1f37b0 2019-08-08 stsp }
9332 2e1f37b0 2019-08-08 stsp }
9333 2e1f37b0 2019-08-08 stsp
9334 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9335 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9336 ad493afc 2019-08-03 stsp if (error)
9337 ad493afc 2019-08-03 stsp goto done;
9338 ad493afc 2019-08-03 stsp
9339 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9340 ad493afc 2019-08-03 stsp if (error)
9341 ad493afc 2019-08-03 stsp goto done;
9342 ad493afc 2019-08-03 stsp
9343 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9344 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9345 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9346 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9347 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9348 9627c110 2020-04-18 stsp if (!error)
9349 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9350 715dc77e 2019-08-03 stsp done:
9351 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9352 2e1f37b0 2019-08-08 stsp error == NULL)
9353 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9354 0ebf8283 2019-07-24 stsp if (repo)
9355 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9356 715dc77e 2019-08-03 stsp if (worktree)
9357 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9358 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9359 715dc77e 2019-08-03 stsp free((char *)pe->path);
9360 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9361 715dc77e 2019-08-03 stsp free(cwd);
9362 01073a5d 2019-08-22 stsp return error;
9363 01073a5d 2019-08-22 stsp }
9364 01073a5d 2019-08-22 stsp
9365 01073a5d 2019-08-22 stsp __dead static void
9366 01073a5d 2019-08-22 stsp usage_cat(void)
9367 01073a5d 2019-08-22 stsp {
9368 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9369 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9370 01073a5d 2019-08-22 stsp exit(1);
9371 01073a5d 2019-08-22 stsp }
9372 01073a5d 2019-08-22 stsp
9373 01073a5d 2019-08-22 stsp static const struct got_error *
9374 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9375 01073a5d 2019-08-22 stsp {
9376 01073a5d 2019-08-22 stsp const struct got_error *err;
9377 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9378 01073a5d 2019-08-22 stsp
9379 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9380 01073a5d 2019-08-22 stsp if (err)
9381 01073a5d 2019-08-22 stsp return err;
9382 01073a5d 2019-08-22 stsp
9383 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9384 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9385 01073a5d 2019-08-22 stsp return err;
9386 01073a5d 2019-08-22 stsp }
9387 01073a5d 2019-08-22 stsp
9388 01073a5d 2019-08-22 stsp static const struct got_error *
9389 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9390 01073a5d 2019-08-22 stsp {
9391 01073a5d 2019-08-22 stsp const struct got_error *err;
9392 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9393 56e0773d 2019-11-28 stsp int nentries, i;
9394 01073a5d 2019-08-22 stsp
9395 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9396 01073a5d 2019-08-22 stsp if (err)
9397 01073a5d 2019-08-22 stsp return err;
9398 01073a5d 2019-08-22 stsp
9399 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9400 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9401 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9402 01073a5d 2019-08-22 stsp char *id_str;
9403 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9404 01073a5d 2019-08-22 stsp break;
9405 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9406 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9407 01073a5d 2019-08-22 stsp if (err)
9408 01073a5d 2019-08-22 stsp break;
9409 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9410 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9411 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9412 01073a5d 2019-08-22 stsp free(id_str);
9413 01073a5d 2019-08-22 stsp }
9414 01073a5d 2019-08-22 stsp
9415 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9416 01073a5d 2019-08-22 stsp return err;
9417 01073a5d 2019-08-22 stsp }
9418 01073a5d 2019-08-22 stsp
9419 01073a5d 2019-08-22 stsp static const struct got_error *
9420 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9421 01073a5d 2019-08-22 stsp {
9422 01073a5d 2019-08-22 stsp const struct got_error *err;
9423 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9424 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9425 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9426 01073a5d 2019-08-22 stsp char *id_str = NULL;
9427 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9428 01073a5d 2019-08-22 stsp
9429 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9430 01073a5d 2019-08-22 stsp if (err)
9431 01073a5d 2019-08-22 stsp return err;
9432 01073a5d 2019-08-22 stsp
9433 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9434 01073a5d 2019-08-22 stsp if (err)
9435 01073a5d 2019-08-22 stsp goto done;
9436 01073a5d 2019-08-22 stsp
9437 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9438 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9439 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9440 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9441 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9442 01073a5d 2019-08-22 stsp char *pid_str;
9443 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9444 01073a5d 2019-08-22 stsp if (err)
9445 01073a5d 2019-08-22 stsp goto done;
9446 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9447 01073a5d 2019-08-22 stsp free(pid_str);
9448 01073a5d 2019-08-22 stsp }
9449 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9450 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9451 1367695b 2020-09-26 naddy (long long)got_object_commit_get_author_time(commit));
9452 01073a5d 2019-08-22 stsp
9453 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9454 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9455 1367695b 2020-09-26 naddy (long long)got_object_commit_get_committer_time(commit));
9456 01073a5d 2019-08-22 stsp
9457 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9458 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9459 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9460 01073a5d 2019-08-22 stsp done:
9461 01073a5d 2019-08-22 stsp free(id_str);
9462 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9463 01073a5d 2019-08-22 stsp return err;
9464 01073a5d 2019-08-22 stsp }
9465 01073a5d 2019-08-22 stsp
9466 01073a5d 2019-08-22 stsp static const struct got_error *
9467 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9468 01073a5d 2019-08-22 stsp {
9469 01073a5d 2019-08-22 stsp const struct got_error *err;
9470 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9471 01073a5d 2019-08-22 stsp char *id_str = NULL;
9472 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9473 01073a5d 2019-08-22 stsp
9474 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9475 01073a5d 2019-08-22 stsp if (err)
9476 01073a5d 2019-08-22 stsp return err;
9477 01073a5d 2019-08-22 stsp
9478 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9479 01073a5d 2019-08-22 stsp if (err)
9480 01073a5d 2019-08-22 stsp goto done;
9481 01073a5d 2019-08-22 stsp
9482 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9483 e15d5241 2019-08-22 stsp
9484 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9485 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9486 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9487 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9488 01073a5d 2019-08-22 stsp break;
9489 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9490 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9491 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9492 01073a5d 2019-08-22 stsp break;
9493 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9494 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9495 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9496 01073a5d 2019-08-22 stsp break;
9497 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9498 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9499 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9500 01073a5d 2019-08-22 stsp break;
9501 01073a5d 2019-08-22 stsp default:
9502 01073a5d 2019-08-22 stsp break;
9503 01073a5d 2019-08-22 stsp }
9504 01073a5d 2019-08-22 stsp
9505 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9506 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9507 e15d5241 2019-08-22 stsp
9508 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9509 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9510 1367695b 2020-09-26 naddy (long long)got_object_tag_get_tagger_time(tag));
9511 01073a5d 2019-08-22 stsp
9512 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9513 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9514 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9515 01073a5d 2019-08-22 stsp done:
9516 01073a5d 2019-08-22 stsp free(id_str);
9517 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9518 01073a5d 2019-08-22 stsp return err;
9519 01073a5d 2019-08-22 stsp }
9520 01073a5d 2019-08-22 stsp
9521 01073a5d 2019-08-22 stsp static const struct got_error *
9522 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9523 01073a5d 2019-08-22 stsp {
9524 01073a5d 2019-08-22 stsp const struct got_error *error;
9525 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9526 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9527 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9528 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9529 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9530 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9531 01073a5d 2019-08-22 stsp
9532 01073a5d 2019-08-22 stsp #ifndef PROFILE
9533 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9534 01073a5d 2019-08-22 stsp NULL) == -1)
9535 01073a5d 2019-08-22 stsp err(1, "pledge");
9536 01073a5d 2019-08-22 stsp #endif
9537 01073a5d 2019-08-22 stsp
9538 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9539 01073a5d 2019-08-22 stsp switch (ch) {
9540 896e9b6f 2019-08-26 stsp case 'c':
9541 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9542 896e9b6f 2019-08-26 stsp break;
9543 01073a5d 2019-08-22 stsp case 'r':
9544 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9545 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9546 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9547 9ba1d308 2019-10-21 stsp optarg);
9548 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9549 01073a5d 2019-08-22 stsp break;
9550 896e9b6f 2019-08-26 stsp case 'P':
9551 896e9b6f 2019-08-26 stsp force_path = 1;
9552 896e9b6f 2019-08-26 stsp break;
9553 01073a5d 2019-08-22 stsp default:
9554 01073a5d 2019-08-22 stsp usage_cat();
9555 01073a5d 2019-08-22 stsp /* NOTREACHED */
9556 01073a5d 2019-08-22 stsp }
9557 01073a5d 2019-08-22 stsp }
9558 01073a5d 2019-08-22 stsp
9559 01073a5d 2019-08-22 stsp argc -= optind;
9560 01073a5d 2019-08-22 stsp argv += optind;
9561 01073a5d 2019-08-22 stsp
9562 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9563 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9564 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9565 01073a5d 2019-08-22 stsp goto done;
9566 01073a5d 2019-08-22 stsp }
9567 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9568 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9569 01073a5d 2019-08-22 stsp goto done;
9570 01073a5d 2019-08-22 stsp if (worktree) {
9571 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9572 01073a5d 2019-08-22 stsp repo_path = strdup(
9573 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9574 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9575 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9576 01073a5d 2019-08-22 stsp goto done;
9577 01073a5d 2019-08-22 stsp }
9578 01073a5d 2019-08-22 stsp }
9579 01073a5d 2019-08-22 stsp }
9580 01073a5d 2019-08-22 stsp
9581 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9582 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9583 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9584 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9585 01073a5d 2019-08-22 stsp }
9586 01073a5d 2019-08-22 stsp
9587 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9588 01073a5d 2019-08-22 stsp free(repo_path);
9589 01073a5d 2019-08-22 stsp if (error != NULL)
9590 01073a5d 2019-08-22 stsp goto done;
9591 01073a5d 2019-08-22 stsp
9592 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9593 896e9b6f 2019-08-26 stsp if (error)
9594 896e9b6f 2019-08-26 stsp goto done;
9595 896e9b6f 2019-08-26 stsp
9596 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9597 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9598 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9599 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9600 01073a5d 2019-08-22 stsp if (error)
9601 01073a5d 2019-08-22 stsp goto done;
9602 01073a5d 2019-08-22 stsp
9603 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9604 896e9b6f 2019-08-26 stsp if (force_path) {
9605 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9606 896e9b6f 2019-08-26 stsp argv[i]);
9607 896e9b6f 2019-08-26 stsp if (error)
9608 896e9b6f 2019-08-26 stsp break;
9609 896e9b6f 2019-08-26 stsp } else {
9610 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9611 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9612 896e9b6f 2019-08-26 stsp if (error) {
9613 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9614 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9615 896e9b6f 2019-08-26 stsp break;
9616 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9617 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9618 896e9b6f 2019-08-26 stsp if (error)
9619 896e9b6f 2019-08-26 stsp break;
9620 896e9b6f 2019-08-26 stsp }
9621 896e9b6f 2019-08-26 stsp }
9622 01073a5d 2019-08-22 stsp
9623 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9624 01073a5d 2019-08-22 stsp if (error)
9625 01073a5d 2019-08-22 stsp break;
9626 01073a5d 2019-08-22 stsp
9627 01073a5d 2019-08-22 stsp switch (obj_type) {
9628 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9629 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9630 01073a5d 2019-08-22 stsp break;
9631 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9632 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9633 01073a5d 2019-08-22 stsp break;
9634 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9635 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9636 01073a5d 2019-08-22 stsp break;
9637 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9638 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9639 01073a5d 2019-08-22 stsp break;
9640 01073a5d 2019-08-22 stsp default:
9641 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9642 01073a5d 2019-08-22 stsp break;
9643 01073a5d 2019-08-22 stsp }
9644 01073a5d 2019-08-22 stsp if (error)
9645 01073a5d 2019-08-22 stsp break;
9646 01073a5d 2019-08-22 stsp free(label);
9647 01073a5d 2019-08-22 stsp label = NULL;
9648 01073a5d 2019-08-22 stsp free(id);
9649 01073a5d 2019-08-22 stsp id = NULL;
9650 01073a5d 2019-08-22 stsp }
9651 01073a5d 2019-08-22 stsp done:
9652 01073a5d 2019-08-22 stsp free(label);
9653 01073a5d 2019-08-22 stsp free(id);
9654 896e9b6f 2019-08-26 stsp free(commit_id);
9655 01073a5d 2019-08-22 stsp if (worktree)
9656 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9657 01073a5d 2019-08-22 stsp if (repo) {
9658 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9659 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9660 01073a5d 2019-08-22 stsp if (error == NULL)
9661 01073a5d 2019-08-22 stsp error = repo_error;
9662 b2118c49 2020-07-28 stsp }
9663 b2118c49 2020-07-28 stsp return error;
9664 b2118c49 2020-07-28 stsp }
9665 b2118c49 2020-07-28 stsp
9666 b2118c49 2020-07-28 stsp __dead static void
9667 b2118c49 2020-07-28 stsp usage_info(void)
9668 b2118c49 2020-07-28 stsp {
9669 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9670 b2118c49 2020-07-28 stsp getprogname());
9671 b2118c49 2020-07-28 stsp exit(1);
9672 b2118c49 2020-07-28 stsp }
9673 b2118c49 2020-07-28 stsp
9674 b2118c49 2020-07-28 stsp static const struct got_error *
9675 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9676 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9677 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9678 b2118c49 2020-07-28 stsp {
9679 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9680 b2118c49 2020-07-28 stsp char *id_str = NULL;
9681 b2118c49 2020-07-28 stsp char datebuf[128];
9682 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9683 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9684 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9685 b2118c49 2020-07-28 stsp
9686 b2118c49 2020-07-28 stsp /*
9687 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9688 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9689 b2118c49 2020-07-28 stsp */
9690 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9691 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9692 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9693 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9694 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9695 b2118c49 2020-07-28 stsp }
9696 b2118c49 2020-07-28 stsp
9697 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9698 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9699 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9700 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9701 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9702 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9703 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9704 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9705 b2118c49 2020-07-28 stsp else
9706 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9707 b2118c49 2020-07-28 stsp
9708 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9709 b2118c49 2020-07-28 stsp if (tm == NULL)
9710 b2118c49 2020-07-28 stsp return NULL;
9711 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9712 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9713 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9714 b2118c49 2020-07-28 stsp
9715 b2118c49 2020-07-28 stsp if (blob_id) {
9716 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9717 b2118c49 2020-07-28 stsp if (err)
9718 b2118c49 2020-07-28 stsp return err;
9719 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9720 b2118c49 2020-07-28 stsp free(id_str);
9721 b2118c49 2020-07-28 stsp }
9722 b2118c49 2020-07-28 stsp
9723 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9724 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9725 b2118c49 2020-07-28 stsp if (err)
9726 b2118c49 2020-07-28 stsp return err;
9727 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9728 b2118c49 2020-07-28 stsp free(id_str);
9729 b2118c49 2020-07-28 stsp }
9730 b2118c49 2020-07-28 stsp
9731 b2118c49 2020-07-28 stsp if (commit_id) {
9732 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9733 b2118c49 2020-07-28 stsp if (err)
9734 b2118c49 2020-07-28 stsp return err;
9735 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9736 b2118c49 2020-07-28 stsp free(id_str);
9737 b2118c49 2020-07-28 stsp }
9738 b2118c49 2020-07-28 stsp
9739 b2118c49 2020-07-28 stsp return NULL;
9740 b2118c49 2020-07-28 stsp }
9741 b2118c49 2020-07-28 stsp
9742 b2118c49 2020-07-28 stsp static const struct got_error *
9743 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9744 b2118c49 2020-07-28 stsp {
9745 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9746 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9747 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9748 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9749 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9750 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9751 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9752 b2118c49 2020-07-28 stsp
9753 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9754 b2118c49 2020-07-28 stsp
9755 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9756 b2118c49 2020-07-28 stsp switch (ch) {
9757 b2118c49 2020-07-28 stsp default:
9758 b2118c49 2020-07-28 stsp usage_info();
9759 b2118c49 2020-07-28 stsp /* NOTREACHED */
9760 b2118c49 2020-07-28 stsp }
9761 01073a5d 2019-08-22 stsp }
9762 b2118c49 2020-07-28 stsp
9763 b2118c49 2020-07-28 stsp argc -= optind;
9764 b2118c49 2020-07-28 stsp argv += optind;
9765 b2118c49 2020-07-28 stsp
9766 b2118c49 2020-07-28 stsp #ifndef PROFILE
9767 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9768 b2118c49 2020-07-28 stsp NULL) == -1)
9769 b2118c49 2020-07-28 stsp err(1, "pledge");
9770 b2118c49 2020-07-28 stsp #endif
9771 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9772 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9773 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9774 b2118c49 2020-07-28 stsp goto done;
9775 b2118c49 2020-07-28 stsp }
9776 b2118c49 2020-07-28 stsp
9777 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9778 b2118c49 2020-07-28 stsp if (error) {
9779 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9780 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9781 b2118c49 2020-07-28 stsp goto done;
9782 b2118c49 2020-07-28 stsp }
9783 b2118c49 2020-07-28 stsp
9784 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9785 b2118c49 2020-07-28 stsp if (error)
9786 b2118c49 2020-07-28 stsp goto done;
9787 b2118c49 2020-07-28 stsp
9788 b2118c49 2020-07-28 stsp if (argc >= 1) {
9789 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9790 b2118c49 2020-07-28 stsp worktree);
9791 b2118c49 2020-07-28 stsp if (error)
9792 b2118c49 2020-07-28 stsp goto done;
9793 b2118c49 2020-07-28 stsp show_files = 1;
9794 b2118c49 2020-07-28 stsp }
9795 b2118c49 2020-07-28 stsp
9796 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9797 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9798 b2118c49 2020-07-28 stsp if (error)
9799 b2118c49 2020-07-28 stsp goto done;
9800 b2118c49 2020-07-28 stsp
9801 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9802 b2118c49 2020-07-28 stsp if (error)
9803 b2118c49 2020-07-28 stsp goto done;
9804 b2118c49 2020-07-28 stsp
9805 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9806 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9807 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9808 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9809 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9810 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9811 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9812 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9813 b2118c49 2020-07-28 stsp
9814 b2118c49 2020-07-28 stsp if (show_files) {
9815 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9816 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9817 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9818 b2118c49 2020-07-28 stsp continue;
9819 b2118c49 2020-07-28 stsp /*
9820 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9821 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9822 b2118c49 2020-07-28 stsp */
9823 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9824 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9825 b2118c49 2020-07-28 stsp }
9826 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9827 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9828 b2118c49 2020-07-28 stsp if (error)
9829 b2118c49 2020-07-28 stsp goto done;
9830 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9831 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9832 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9833 b2118c49 2020-07-28 stsp break;
9834 b2118c49 2020-07-28 stsp }
9835 b2118c49 2020-07-28 stsp }
9836 b2118c49 2020-07-28 stsp }
9837 b2118c49 2020-07-28 stsp done:
9838 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9839 b2118c49 2020-07-28 stsp free((char *)pe->path);
9840 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9841 b2118c49 2020-07-28 stsp free(cwd);
9842 b2118c49 2020-07-28 stsp free(id_str);
9843 b2118c49 2020-07-28 stsp free(uuidstr);
9844 0ebf8283 2019-07-24 stsp return error;
9845 0ebf8283 2019-07-24 stsp }