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 5c860e29 2018-03-12 stsp
58 5c860e29 2018-03-12 stsp #ifndef nitems
59 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 5c860e29 2018-03-12 stsp #endif
61 99437157 2018-11-11 stsp
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
64 99437157 2018-11-11 stsp
65 99437157 2018-11-11 stsp static void
66 99437157 2018-11-11 stsp catch_sigint(int signo)
67 99437157 2018-11-11 stsp {
68 99437157 2018-11-11 stsp sigint_received = 1;
69 99437157 2018-11-11 stsp }
70 99437157 2018-11-11 stsp
71 99437157 2018-11-11 stsp static void
72 99437157 2018-11-11 stsp catch_sigpipe(int signo)
73 99437157 2018-11-11 stsp {
74 99437157 2018-11-11 stsp sigpipe_received = 1;
75 99437157 2018-11-11 stsp }
76 5c860e29 2018-03-12 stsp
77 99437157 2018-11-11 stsp
78 8cfb4057 2019-07-09 stsp struct got_cmd {
79 5c860e29 2018-03-12 stsp const char *cmd_name;
80 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
82 97b3a7be 2019-07-09 stsp const char *cmd_alias;
83 5c860e29 2018-03-12 stsp };
84 5c860e29 2018-03-12 stsp
85 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
86 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
87 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
90 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
91 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
92 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
94 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
95 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
96 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
97 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
98 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
99 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
100 d00136be 2019-03-26 stsp __dead static void usage_add(void);
101 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
102 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
103 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
104 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
105 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
106 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
107 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
108 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
109 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
110 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
111 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
112 5c860e29 2018-03-12 stsp
113 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
114 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
115 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
116 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
118 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
121 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
122 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
124 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
125 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
126 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
127 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
128 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
129 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
130 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
131 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
132 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
133 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
134 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
135 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
136 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
137 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
138 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
139 5c860e29 2018-03-12 stsp
140 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
141 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
142 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
143 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
144 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
145 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
146 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
147 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
148 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
149 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
150 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
151 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
152 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
153 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
154 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
155 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
156 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
157 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
158 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
159 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
161 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
162 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
163 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
164 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
165 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
166 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
167 5c860e29 2018-03-12 stsp };
168 ce5b7c56 2019-07-09 stsp
169 ce5b7c56 2019-07-09 stsp static void
170 ce5b7c56 2019-07-09 stsp list_commands(void)
171 ce5b7c56 2019-07-09 stsp {
172 ce5b7c56 2019-07-09 stsp int i;
173 ce5b7c56 2019-07-09 stsp
174 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
175 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
176 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
177 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
178 ce5b7c56 2019-07-09 stsp }
179 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
180 ce5b7c56 2019-07-09 stsp }
181 5c860e29 2018-03-12 stsp
182 5c860e29 2018-03-12 stsp int
183 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
184 5c860e29 2018-03-12 stsp {
185 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
186 5c860e29 2018-03-12 stsp unsigned int i;
187 5c860e29 2018-03-12 stsp int ch;
188 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
189 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
190 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
191 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
192 83cd27f8 2020-01-13 stsp };
193 5c860e29 2018-03-12 stsp
194 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
195 5c860e29 2018-03-12 stsp
196 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
197 5c860e29 2018-03-12 stsp switch (ch) {
198 1b6b95a8 2018-03-12 stsp case 'h':
199 1b6b95a8 2018-03-12 stsp hflag = 1;
200 53ccebc2 2019-07-30 stsp break;
201 53ccebc2 2019-07-30 stsp case 'V':
202 53ccebc2 2019-07-30 stsp Vflag = 1;
203 1b6b95a8 2018-03-12 stsp break;
204 5c860e29 2018-03-12 stsp default:
205 ce5b7c56 2019-07-09 stsp usage(hflag);
206 5c860e29 2018-03-12 stsp /* NOTREACHED */
207 5c860e29 2018-03-12 stsp }
208 5c860e29 2018-03-12 stsp }
209 5c860e29 2018-03-12 stsp
210 5c860e29 2018-03-12 stsp argc -= optind;
211 5c860e29 2018-03-12 stsp argv += optind;
212 1e70621d 2018-03-27 stsp optind = 0;
213 53ccebc2 2019-07-30 stsp
214 53ccebc2 2019-07-30 stsp if (Vflag) {
215 53ccebc2 2019-07-30 stsp got_version_print_str();
216 53ccebc2 2019-07-30 stsp return 1;
217 53ccebc2 2019-07-30 stsp }
218 5c860e29 2018-03-12 stsp
219 5c860e29 2018-03-12 stsp if (argc <= 0)
220 ce5b7c56 2019-07-09 stsp usage(hflag);
221 5c860e29 2018-03-12 stsp
222 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
223 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
224 99437157 2018-11-11 stsp
225 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
226 d7d4f210 2018-03-12 stsp const struct got_error *error;
227 d7d4f210 2018-03-12 stsp
228 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
229 5c860e29 2018-03-12 stsp
230 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
232 5c860e29 2018-03-12 stsp continue;
233 5c860e29 2018-03-12 stsp
234 1b6b95a8 2018-03-12 stsp if (hflag)
235 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
236 1b6b95a8 2018-03-12 stsp
237 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
238 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
239 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
240 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
241 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
242 70015d7a 2019-11-08 stsp !(sigint_received &&
243 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
245 d7d4f210 2018-03-12 stsp return 1;
246 d7d4f210 2018-03-12 stsp }
247 d7d4f210 2018-03-12 stsp
248 d7d4f210 2018-03-12 stsp return 0;
249 5c860e29 2018-03-12 stsp }
250 5c860e29 2018-03-12 stsp
251 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
252 ce5b7c56 2019-07-09 stsp list_commands();
253 5c860e29 2018-03-12 stsp return 1;
254 5c860e29 2018-03-12 stsp }
255 5c860e29 2018-03-12 stsp
256 4ed7e80c 2018-05-20 stsp __dead static void
257 ce5b7c56 2019-07-09 stsp usage(int hflag)
258 5c860e29 2018-03-12 stsp {
259 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
260 53ccebc2 2019-07-30 stsp getprogname());
261 ce5b7c56 2019-07-09 stsp if (hflag)
262 ce5b7c56 2019-07-09 stsp list_commands();
263 5c860e29 2018-03-12 stsp exit(1);
264 5c860e29 2018-03-12 stsp }
265 5c860e29 2018-03-12 stsp
266 0266afb7 2019-01-04 stsp static const struct got_error *
267 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
268 e2ba3d07 2019-05-13 stsp {
269 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
270 e2ba3d07 2019-05-13 stsp const char *editor;
271 8920fa04 2019-08-18 stsp
272 8920fa04 2019-08-18 stsp *abspath = NULL;
273 e2ba3d07 2019-05-13 stsp
274 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
275 e2ba3d07 2019-05-13 stsp if (editor == NULL)
276 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
277 e2ba3d07 2019-05-13 stsp
278 0ee7065d 2019-05-13 stsp if (editor) {
279 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
280 0ee7065d 2019-05-13 stsp if (err)
281 0ee7065d 2019-05-13 stsp return err;
282 0ee7065d 2019-05-13 stsp }
283 e2ba3d07 2019-05-13 stsp
284 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
285 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
286 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
287 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
288 0ee7065d 2019-05-13 stsp }
289 0ee7065d 2019-05-13 stsp
290 e2ba3d07 2019-05-13 stsp return NULL;
291 e2ba3d07 2019-05-13 stsp }
292 e2ba3d07 2019-05-13 stsp
293 e2ba3d07 2019-05-13 stsp static const struct got_error *
294 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
295 c530dc23 2019-07-23 stsp const char *worktree_path)
296 0266afb7 2019-01-04 stsp {
297 163ce85a 2019-05-13 stsp const struct got_error *err;
298 0266afb7 2019-01-04 stsp
299 37c06ea4 2019-07-15 stsp #ifdef PROFILE
300 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
301 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
302 37c06ea4 2019-07-15 stsp #endif
303 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
305 0266afb7 2019-01-04 stsp
306 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
308 0266afb7 2019-01-04 stsp
309 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
311 0266afb7 2019-01-04 stsp
312 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
313 163ce85a 2019-05-13 stsp if (err != NULL)
314 163ce85a 2019-05-13 stsp return err;
315 0266afb7 2019-01-04 stsp
316 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
318 0266afb7 2019-01-04 stsp
319 0266afb7 2019-01-04 stsp return NULL;
320 2c7829a4 2019-06-17 stsp }
321 2c7829a4 2019-06-17 stsp
322 2c7829a4 2019-06-17 stsp __dead static void
323 2c7829a4 2019-06-17 stsp usage_init(void)
324 2c7829a4 2019-06-17 stsp {
325 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
326 2c7829a4 2019-06-17 stsp exit(1);
327 2c7829a4 2019-06-17 stsp }
328 2c7829a4 2019-06-17 stsp
329 2c7829a4 2019-06-17 stsp static const struct got_error *
330 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
331 2c7829a4 2019-06-17 stsp {
332 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
333 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
334 2c7829a4 2019-06-17 stsp int ch;
335 2c7829a4 2019-06-17 stsp
336 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
337 2c7829a4 2019-06-17 stsp switch (ch) {
338 2c7829a4 2019-06-17 stsp default:
339 2c7829a4 2019-06-17 stsp usage_init();
340 2c7829a4 2019-06-17 stsp /* NOTREACHED */
341 2c7829a4 2019-06-17 stsp }
342 2c7829a4 2019-06-17 stsp }
343 2c7829a4 2019-06-17 stsp
344 2c7829a4 2019-06-17 stsp argc -= optind;
345 2c7829a4 2019-06-17 stsp argv += optind;
346 2c7829a4 2019-06-17 stsp
347 2c7829a4 2019-06-17 stsp #ifndef PROFILE
348 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
349 2c7829a4 2019-06-17 stsp err(1, "pledge");
350 2c7829a4 2019-06-17 stsp #endif
351 2c7829a4 2019-06-17 stsp if (argc != 1)
352 bc20e173 2019-06-17 stsp usage_init();
353 2c7829a4 2019-06-17 stsp
354 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
355 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
356 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
357 2c7829a4 2019-06-17 stsp
358 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
361 2c7829a4 2019-06-17 stsp if (error &&
362 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
363 2c7829a4 2019-06-17 stsp goto done;
364 2c7829a4 2019-06-17 stsp
365 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
366 2c7829a4 2019-06-17 stsp if (error)
367 2c7829a4 2019-06-17 stsp goto done;
368 2c7829a4 2019-06-17 stsp
369 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
370 3ce1b845 2019-07-15 stsp done:
371 3ce1b845 2019-07-15 stsp free(repo_path);
372 3ce1b845 2019-07-15 stsp return error;
373 3ce1b845 2019-07-15 stsp }
374 3ce1b845 2019-07-15 stsp
375 3ce1b845 2019-07-15 stsp __dead static void
376 3ce1b845 2019-07-15 stsp usage_import(void)
377 3ce1b845 2019-07-15 stsp {
378 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
380 3ce1b845 2019-07-15 stsp exit(1);
381 3ce1b845 2019-07-15 stsp }
382 3ce1b845 2019-07-15 stsp
383 3ce1b845 2019-07-15 stsp int
384 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
385 3ce1b845 2019-07-15 stsp {
386 3ce1b845 2019-07-15 stsp pid_t pid;
387 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
388 3ce1b845 2019-07-15 stsp int st = -1;
389 3ce1b845 2019-07-15 stsp
390 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
391 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
392 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
393 3ce1b845 2019-07-15 stsp
394 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
395 3ce1b845 2019-07-15 stsp case -1:
396 3ce1b845 2019-07-15 stsp goto doneediting;
397 3ce1b845 2019-07-15 stsp case 0:
398 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
399 3ce1b845 2019-07-15 stsp _exit(127);
400 3ce1b845 2019-07-15 stsp }
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
403 3ce1b845 2019-07-15 stsp if (errno != EINTR)
404 3ce1b845 2019-07-15 stsp break;
405 3ce1b845 2019-07-15 stsp
406 3ce1b845 2019-07-15 stsp doneediting:
407 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
408 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
409 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
410 3ce1b845 2019-07-15 stsp
411 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
412 3ce1b845 2019-07-15 stsp errno = EINTR;
413 3ce1b845 2019-07-15 stsp return -1;
414 3ce1b845 2019-07-15 stsp }
415 3ce1b845 2019-07-15 stsp
416 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
417 3ce1b845 2019-07-15 stsp }
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp static const struct got_error *
420 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 3ce1b845 2019-07-15 stsp const char *initial_content)
422 3ce1b845 2019-07-15 stsp {
423 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
424 3ce1b845 2019-07-15 stsp char buf[1024];
425 3ce1b845 2019-07-15 stsp struct stat st, st2;
426 3ce1b845 2019-07-15 stsp FILE *fp;
427 3ce1b845 2019-07-15 stsp int content_changed = 0;
428 3ce1b845 2019-07-15 stsp size_t len;
429 3ce1b845 2019-07-15 stsp
430 3ce1b845 2019-07-15 stsp *logmsg = NULL;
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
433 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
437 3ce1b845 2019-07-15 stsp
438 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
439 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
440 3ce1b845 2019-07-15 stsp
441 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
446 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
447 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
448 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
449 3ce1b845 2019-07-15 stsp len = 0;
450 3ce1b845 2019-07-15 stsp
451 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
452 3ce1b845 2019-07-15 stsp if (fp == NULL) {
453 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
454 3ce1b845 2019-07-15 stsp goto done;
455 3ce1b845 2019-07-15 stsp }
456 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
457 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
458 3ce1b845 2019-07-15 stsp content_changed = 1;
459 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
461 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
462 3ce1b845 2019-07-15 stsp }
463 3ce1b845 2019-07-15 stsp fclose(fp);
464 3ce1b845 2019-07-15 stsp
465 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
467 3ce1b845 2019-07-15 stsp len--;
468 3ce1b845 2019-07-15 stsp }
469 3ce1b845 2019-07-15 stsp
470 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
471 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
473 3ce1b845 2019-07-15 stsp done:
474 3ce1b845 2019-07-15 stsp if (err) {
475 3ce1b845 2019-07-15 stsp free(*logmsg);
476 3ce1b845 2019-07-15 stsp *logmsg = NULL;
477 3ce1b845 2019-07-15 stsp }
478 3ce1b845 2019-07-15 stsp return err;
479 3ce1b845 2019-07-15 stsp }
480 3ce1b845 2019-07-15 stsp
481 3ce1b845 2019-07-15 stsp static const struct got_error *
482 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
484 3ce1b845 2019-07-15 stsp {
485 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
486 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
487 3ce1b845 2019-07-15 stsp int fd;
488 3ce1b845 2019-07-15 stsp
489 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
490 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
491 3ce1b845 2019-07-15 stsp branch_name) == -1)
492 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
493 3ce1b845 2019-07-15 stsp
494 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
495 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
496 3ce1b845 2019-07-15 stsp if (err)
497 3ce1b845 2019-07-15 stsp goto done;
498 3ce1b845 2019-07-15 stsp
499 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
500 3ce1b845 2019-07-15 stsp close(fd);
501 3ce1b845 2019-07-15 stsp
502 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
503 3ce1b845 2019-07-15 stsp done:
504 3ce1b845 2019-07-15 stsp free(initial_content);
505 3ce1b845 2019-07-15 stsp return err;
506 3ce1b845 2019-07-15 stsp }
507 3ce1b845 2019-07-15 stsp
508 3ce1b845 2019-07-15 stsp static const struct got_error *
509 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
510 3ce1b845 2019-07-15 stsp {
511 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
512 3ce1b845 2019-07-15 stsp return NULL;
513 3ce1b845 2019-07-15 stsp }
514 3ce1b845 2019-07-15 stsp
515 3ce1b845 2019-07-15 stsp static const struct got_error *
516 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
517 84792843 2019-08-09 stsp {
518 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
519 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
520 84792843 2019-08-09 stsp
521 84792843 2019-08-09 stsp *author = NULL;
522 aba9c984 2019-09-08 stsp
523 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
524 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
525 c9956ddf 2019-09-08 stsp if (name && email) {
526 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
527 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
528 aba9c984 2019-09-08 stsp return NULL;
529 aba9c984 2019-09-08 stsp }
530 84792843 2019-08-09 stsp
531 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
532 84792843 2019-08-09 stsp if (got_author == NULL) {
533 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
534 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
535 c9956ddf 2019-09-08 stsp if (name && email) {
536 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
537 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
538 c9956ddf 2019-09-08 stsp return NULL;
539 c9956ddf 2019-09-08 stsp }
540 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
541 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
542 84792843 2019-08-09 stsp }
543 84792843 2019-08-09 stsp
544 aba9c984 2019-09-08 stsp *author = strdup(got_author);
545 aba9c984 2019-09-08 stsp if (*author == NULL)
546 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
547 84792843 2019-08-09 stsp
548 84792843 2019-08-09 stsp /*
549 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
550 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
551 84792843 2019-08-09 stsp */
552 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
553 84792843 2019-08-09 stsp got_author++;
554 aba9c984 2019-09-08 stsp if (*got_author != '<') {
555 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
556 aba9c984 2019-09-08 stsp goto done;
557 aba9c984 2019-09-08 stsp }
558 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
559 84792843 2019-08-09 stsp got_author++;
560 aba9c984 2019-09-08 stsp if (*got_author != '@') {
561 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
562 aba9c984 2019-09-08 stsp goto done;
563 aba9c984 2019-09-08 stsp }
564 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
565 84792843 2019-08-09 stsp got_author++;
566 84792843 2019-08-09 stsp if (*got_author != '>')
567 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
568 aba9c984 2019-09-08 stsp done:
569 aba9c984 2019-09-08 stsp if (err) {
570 aba9c984 2019-09-08 stsp free(*author);
571 aba9c984 2019-09-08 stsp *author = NULL;
572 aba9c984 2019-09-08 stsp }
573 aba9c984 2019-09-08 stsp return err;
574 c9956ddf 2019-09-08 stsp }
575 c9956ddf 2019-09-08 stsp
576 c9956ddf 2019-09-08 stsp static const struct got_error *
577 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
578 c9956ddf 2019-09-08 stsp {
579 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
580 c9956ddf 2019-09-08 stsp
581 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
582 c9956ddf 2019-09-08 stsp if (homedir) {
583 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
585 c9956ddf 2019-09-08 stsp
586 c9956ddf 2019-09-08 stsp }
587 c9956ddf 2019-09-08 stsp return NULL;
588 84792843 2019-08-09 stsp }
589 84792843 2019-08-09 stsp
590 84792843 2019-08-09 stsp static const struct got_error *
591 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
592 3ce1b845 2019-07-15 stsp {
593 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
594 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
597 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
599 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
601 3ce1b845 2019-07-15 stsp int ch;
602 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
603 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
604 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
605 3ce1b845 2019-07-15 stsp
606 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
607 3ce1b845 2019-07-15 stsp
608 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
609 3ce1b845 2019-07-15 stsp switch (ch) {
610 3ce1b845 2019-07-15 stsp case 'b':
611 3ce1b845 2019-07-15 stsp branch_name = optarg;
612 3ce1b845 2019-07-15 stsp break;
613 3ce1b845 2019-07-15 stsp case 'm':
614 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
615 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
616 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
617 3ce1b845 2019-07-15 stsp goto done;
618 3ce1b845 2019-07-15 stsp }
619 3ce1b845 2019-07-15 stsp break;
620 3ce1b845 2019-07-15 stsp case 'r':
621 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
622 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
623 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
624 9ba1d308 2019-10-21 stsp optarg);
625 3ce1b845 2019-07-15 stsp goto done;
626 3ce1b845 2019-07-15 stsp }
627 3ce1b845 2019-07-15 stsp break;
628 3ce1b845 2019-07-15 stsp case 'I':
629 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
630 3ce1b845 2019-07-15 stsp break;
631 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
632 3ce1b845 2019-07-15 stsp NULL);
633 3ce1b845 2019-07-15 stsp if (error)
634 3ce1b845 2019-07-15 stsp goto done;
635 3ce1b845 2019-07-15 stsp break;
636 3ce1b845 2019-07-15 stsp default:
637 b2b65d18 2019-08-22 stsp usage_import();
638 3ce1b845 2019-07-15 stsp /* NOTREACHED */
639 3ce1b845 2019-07-15 stsp }
640 3ce1b845 2019-07-15 stsp }
641 3ce1b845 2019-07-15 stsp
642 3ce1b845 2019-07-15 stsp argc -= optind;
643 3ce1b845 2019-07-15 stsp argv += optind;
644 3ce1b845 2019-07-15 stsp
645 3ce1b845 2019-07-15 stsp #ifndef PROFILE
646 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
647 aba9c984 2019-09-08 stsp "unveil",
648 3ce1b845 2019-07-15 stsp NULL) == -1)
649 3ce1b845 2019-07-15 stsp err(1, "pledge");
650 3ce1b845 2019-07-15 stsp #endif
651 3ce1b845 2019-07-15 stsp if (argc != 1)
652 3ce1b845 2019-07-15 stsp usage_import();
653 2c7829a4 2019-06-17 stsp
654 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
655 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
656 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
657 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
658 3ce1b845 2019-07-15 stsp }
659 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
660 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
661 c9956ddf 2019-09-08 stsp if (error)
662 c9956ddf 2019-09-08 stsp goto done;
663 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
664 3ce1b845 2019-07-15 stsp if (error)
665 3ce1b845 2019-07-15 stsp goto done;
666 aba9c984 2019-09-08 stsp
667 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
668 aba9c984 2019-09-08 stsp if (error)
669 aba9c984 2019-09-08 stsp return error;
670 e560b7e0 2019-11-28 stsp
671 e560b7e0 2019-11-28 stsp /*
672 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
673 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
674 e560b7e0 2019-11-28 stsp * an unintended typo.
675 e560b7e0 2019-11-28 stsp */
676 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
677 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
678 3ce1b845 2019-07-15 stsp
679 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
681 3ce1b845 2019-07-15 stsp goto done;
682 3ce1b845 2019-07-15 stsp }
683 3ce1b845 2019-07-15 stsp
684 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
685 3ce1b845 2019-07-15 stsp if (error) {
686 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
687 3ce1b845 2019-07-15 stsp goto done;
688 3ce1b845 2019-07-15 stsp } else {
689 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 3ce1b845 2019-07-15 stsp "import target branch already exists");
691 3ce1b845 2019-07-15 stsp goto done;
692 3ce1b845 2019-07-15 stsp }
693 3ce1b845 2019-07-15 stsp
694 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
695 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
696 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
697 3ce1b845 2019-07-15 stsp goto done;
698 3ce1b845 2019-07-15 stsp }
699 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
700 3ce1b845 2019-07-15 stsp
701 3ce1b845 2019-07-15 stsp /*
702 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
703 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
704 3ce1b845 2019-07-15 stsp */
705 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
706 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
707 3ce1b845 2019-07-15 stsp if (error)
708 3ce1b845 2019-07-15 stsp goto done;
709 8e158b01 2019-09-22 stsp free(logmsg);
710 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
711 ef293bdd 2019-10-21 stsp path_dir, refname);
712 ef293bdd 2019-10-21 stsp if (error) {
713 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
714 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
715 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
716 3ce1b845 2019-07-15 stsp goto done;
717 ef293bdd 2019-10-21 stsp }
718 3ce1b845 2019-07-15 stsp }
719 3ce1b845 2019-07-15 stsp
720 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
721 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
722 ef293bdd 2019-10-21 stsp if (logmsg_path)
723 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
724 3ce1b845 2019-07-15 stsp goto done;
725 ef293bdd 2019-10-21 stsp }
726 3ce1b845 2019-07-15 stsp
727 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
728 ef293bdd 2019-10-21 stsp if (error) {
729 ef293bdd 2019-10-21 stsp if (logmsg_path)
730 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
731 ef293bdd 2019-10-21 stsp goto done;
732 ef293bdd 2019-10-21 stsp }
733 ef293bdd 2019-10-21 stsp
734 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
736 ef293bdd 2019-10-21 stsp if (error) {
737 ef293bdd 2019-10-21 stsp if (logmsg_path)
738 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
739 3ce1b845 2019-07-15 stsp goto done;
740 ef293bdd 2019-10-21 stsp }
741 3ce1b845 2019-07-15 stsp
742 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
743 ef293bdd 2019-10-21 stsp if (error) {
744 ef293bdd 2019-10-21 stsp if (logmsg_path)
745 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
746 3ce1b845 2019-07-15 stsp goto done;
747 ef293bdd 2019-10-21 stsp }
748 3ce1b845 2019-07-15 stsp
749 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (logmsg_path)
752 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
753 3ce1b845 2019-07-15 stsp goto done;
754 ef293bdd 2019-10-21 stsp }
755 3ce1b845 2019-07-15 stsp
756 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
757 ef293bdd 2019-10-21 stsp if (error) {
758 ef293bdd 2019-10-21 stsp if (logmsg_path)
759 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
760 3ce1b845 2019-07-15 stsp goto done;
761 ef293bdd 2019-10-21 stsp }
762 3ce1b845 2019-07-15 stsp
763 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
764 3ce1b845 2019-07-15 stsp if (error) {
765 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
766 ef293bdd 2019-10-21 stsp if (logmsg_path)
767 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
768 3ce1b845 2019-07-15 stsp goto done;
769 ef293bdd 2019-10-21 stsp }
770 3ce1b845 2019-07-15 stsp
771 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 3ce1b845 2019-07-15 stsp branch_ref);
773 ef293bdd 2019-10-21 stsp if (error) {
774 ef293bdd 2019-10-21 stsp if (logmsg_path)
775 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
776 3ce1b845 2019-07-15 stsp goto done;
777 ef293bdd 2019-10-21 stsp }
778 3ce1b845 2019-07-15 stsp
779 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
780 ef293bdd 2019-10-21 stsp if (error) {
781 ef293bdd 2019-10-21 stsp if (logmsg_path)
782 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
783 3ce1b845 2019-07-15 stsp goto done;
784 ef293bdd 2019-10-21 stsp }
785 3ce1b845 2019-07-15 stsp }
786 3ce1b845 2019-07-15 stsp
787 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
788 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
789 2c7829a4 2019-06-17 stsp done:
790 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
791 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
792 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
793 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
795 8e158b01 2019-09-22 stsp free(logmsg);
796 ef293bdd 2019-10-21 stsp free(logmsg_path);
797 2c7829a4 2019-06-17 stsp free(repo_path);
798 3ce1b845 2019-07-15 stsp free(editor);
799 3ce1b845 2019-07-15 stsp free(refname);
800 3ce1b845 2019-07-15 stsp free(new_commit_id);
801 3ce1b845 2019-07-15 stsp free(id_str);
802 aba9c984 2019-09-08 stsp free(author);
803 c9956ddf 2019-09-08 stsp free(gitconfig_path);
804 3ce1b845 2019-07-15 stsp if (branch_ref)
805 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
806 3ce1b845 2019-07-15 stsp if (head_ref)
807 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
808 2c7829a4 2019-06-17 stsp return error;
809 93658fb9 2020-03-18 stsp }
810 93658fb9 2020-03-18 stsp
811 93658fb9 2020-03-18 stsp __dead static void
812 93658fb9 2020-03-18 stsp usage_clone(void)
813 93658fb9 2020-03-18 stsp {
814 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
815 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
816 93658fb9 2020-03-18 stsp exit(1);
817 93658fb9 2020-03-18 stsp }
818 892ac3b6 2020-03-18 stsp
819 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
820 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
821 892ac3b6 2020-03-18 stsp int last_p_indexed;
822 892ac3b6 2020-03-18 stsp int last_p_resolved;
823 68999b92 2020-03-18 stsp int verbosity;
824 892ac3b6 2020-03-18 stsp };
825 93658fb9 2020-03-18 stsp
826 93658fb9 2020-03-18 stsp static const struct got_error *
827 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
828 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
829 531c3985 2020-03-18 stsp {
830 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
831 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
832 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
833 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
834 b2409d58 2020-03-18 stsp
835 68999b92 2020-03-18 stsp if (a->verbosity < 0)
836 68999b92 2020-03-18 stsp return NULL;
837 68999b92 2020-03-18 stsp
838 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
839 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
840 892ac3b6 2020-03-18 stsp fflush(stdout);
841 12d1281e 2020-03-19 stsp return NULL;
842 b2409d58 2020-03-18 stsp }
843 b2409d58 2020-03-18 stsp
844 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
845 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
847 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 892ac3b6 2020-03-18 stsp print_size = 1;
849 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
850 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
852 892ac3b6 2020-03-18 stsp }
853 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
854 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
855 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
856 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
857 892ac3b6 2020-03-18 stsp print_indexed = 1;
858 892ac3b6 2020-03-18 stsp print_size = 1;
859 892ac3b6 2020-03-18 stsp }
860 61cc1a7a 2020-03-18 stsp }
861 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
862 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
863 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
864 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
865 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
866 892ac3b6 2020-03-18 stsp print_resolved = 1;
867 892ac3b6 2020-03-18 stsp print_indexed = 1;
868 892ac3b6 2020-03-18 stsp print_size = 1;
869 892ac3b6 2020-03-18 stsp }
870 61cc1a7a 2020-03-18 stsp }
871 b2409d58 2020-03-18 stsp
872 d2cdc636 2020-03-18 stsp }
873 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
874 892ac3b6 2020-03-18 stsp printf("\r");
875 892ac3b6 2020-03-18 stsp if (print_size)
876 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 d715f13e 2020-03-19 stsp if (print_indexed)
878 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
879 d715f13e 2020-03-19 stsp if (print_resolved)
880 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
881 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
882 892ac3b6 2020-03-18 stsp fflush(stdout);
883 892ac3b6 2020-03-18 stsp
884 531c3985 2020-03-18 stsp return NULL;
885 531c3985 2020-03-18 stsp }
886 531c3985 2020-03-18 stsp
887 531c3985 2020-03-18 stsp static const struct got_error *
888 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
889 f298ae0f 2020-03-25 stsp int verbosity, struct got_repository *repo)
890 4ba14133 2020-03-20 stsp {
891 4ba14133 2020-03-20 stsp const struct got_error *err;
892 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
893 4ba14133 2020-03-20 stsp
894 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
895 4ba14133 2020-03-20 stsp if (err)
896 4ba14133 2020-03-20 stsp return err;
897 4ba14133 2020-03-20 stsp
898 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
899 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
900 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
901 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
902 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
903 6338a6a1 2020-03-21 stsp }
904 4ba14133 2020-03-20 stsp return err;
905 4ba14133 2020-03-20 stsp }
906 4ba14133 2020-03-20 stsp
907 4ba14133 2020-03-20 stsp static const struct got_error *
908 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
909 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
910 41b0de12 2020-03-21 stsp {
911 41b0de12 2020-03-21 stsp const struct got_error *err;
912 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
913 41b0de12 2020-03-21 stsp
914 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
915 41b0de12 2020-03-21 stsp const char *refname = pe->path;
916 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
917 41b0de12 2020-03-21 stsp
918 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
919 41b0de12 2020-03-21 stsp }
920 41b0de12 2020-03-21 stsp
921 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
922 41b0de12 2020-03-21 stsp const char *refname = pe->path;
923 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
924 41b0de12 2020-03-21 stsp char *id_str;
925 41b0de12 2020-03-21 stsp
926 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
927 41b0de12 2020-03-21 stsp if (err)
928 41b0de12 2020-03-21 stsp return err;
929 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
930 41b0de12 2020-03-21 stsp free(id_str);
931 41b0de12 2020-03-21 stsp }
932 41b0de12 2020-03-21 stsp
933 41b0de12 2020-03-21 stsp return NULL;
934 6338a6a1 2020-03-21 stsp }
935 6338a6a1 2020-03-21 stsp
936 6338a6a1 2020-03-21 stsp static const struct got_error *
937 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
938 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
939 6338a6a1 2020-03-21 stsp {
940 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
941 6338a6a1 2020-03-21 stsp struct got_reference *ref;
942 6338a6a1 2020-03-21 stsp char *id_str;
943 6338a6a1 2020-03-21 stsp
944 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
945 6338a6a1 2020-03-21 stsp if (err)
946 6338a6a1 2020-03-21 stsp return err;
947 6338a6a1 2020-03-21 stsp
948 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
949 6338a6a1 2020-03-21 stsp if (err)
950 6338a6a1 2020-03-21 stsp goto done;
951 6338a6a1 2020-03-21 stsp
952 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
953 6338a6a1 2020-03-21 stsp got_ref_close(ref);
954 6338a6a1 2020-03-21 stsp
955 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
956 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
957 6338a6a1 2020-03-21 stsp done:
958 6338a6a1 2020-03-21 stsp free(id_str);
959 6338a6a1 2020-03-21 stsp return err;
960 0e4002ca 2020-03-21 stsp }
961 0e4002ca 2020-03-21 stsp
962 0e4002ca 2020-03-21 stsp static int
963 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
964 0e4002ca 2020-03-21 stsp {
965 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
966 0e4002ca 2020-03-21 stsp return 0;
967 0e4002ca 2020-03-21 stsp refname += 5;
968 0e4002ca 2020-03-21 stsp
969 0e4002ca 2020-03-21 stsp /*
970 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
971 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
972 0e4002ca 2020-03-21 stsp */
973 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
974 0e4002ca 2020-03-21 stsp return 0;
975 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
976 0e4002ca 2020-03-21 stsp return 0;
977 0e4002ca 2020-03-21 stsp
978 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
979 0e4002ca 2020-03-21 stsp wanted_ref += 5;
980 0e4002ca 2020-03-21 stsp
981 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
982 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
983 0e4002ca 2020-03-21 stsp return 1;
984 0e4002ca 2020-03-21 stsp
985 0e4002ca 2020-03-21 stsp /* Allow exact match. */
986 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
987 41b0de12 2020-03-21 stsp }
988 41b0de12 2020-03-21 stsp
989 0e4002ca 2020-03-21 stsp static int
990 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
991 0e4002ca 2020-03-21 stsp {
992 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
993 0e4002ca 2020-03-21 stsp
994 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
995 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
996 0e4002ca 2020-03-21 stsp return 1;
997 0e4002ca 2020-03-21 stsp }
998 0e4002ca 2020-03-21 stsp
999 0e4002ca 2020-03-21 stsp return 0;
1000 0e4002ca 2020-03-21 stsp }
1001 0e4002ca 2020-03-21 stsp
1002 41b0de12 2020-03-21 stsp static const struct got_error *
1003 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1004 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1005 0e4002ca 2020-03-21 stsp {
1006 0e4002ca 2020-03-21 stsp const struct got_error *err;
1007 0e4002ca 2020-03-21 stsp char *remote_refname;
1008 0e4002ca 2020-03-21 stsp
1009 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1010 0e4002ca 2020-03-21 stsp refname += 5;
1011 0e4002ca 2020-03-21 stsp
1012 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1013 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1014 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1015 0e4002ca 2020-03-21 stsp
1016 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1017 0e4002ca 2020-03-21 stsp free(remote_refname);
1018 0e4002ca 2020-03-21 stsp return err;
1019 0e4002ca 2020-03-21 stsp }
1020 0e4002ca 2020-03-21 stsp
1021 0e4002ca 2020-03-21 stsp static const struct got_error *
1022 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1023 93658fb9 2020-03-18 stsp {
1024 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1025 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1026 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1027 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1028 bb64b798 2020-03-18 stsp const char *repo_path;
1029 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1030 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1031 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1032 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1033 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1034 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1035 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1036 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1037 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
1038 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
1039 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
1040 b46f3e71 2020-03-18 stsp ssize_t n;
1041 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1042 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1043 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1044 93658fb9 2020-03-18 stsp
1045 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1046 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1047 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1048 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1049 d9b4d0c0 2020-03-18 stsp
1050 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1051 93658fb9 2020-03-18 stsp switch (ch) {
1052 659e7fbd 2020-03-20 stsp case 'a':
1053 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1054 4ba14133 2020-03-20 stsp break;
1055 4ba14133 2020-03-20 stsp case 'b':
1056 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1057 4ba14133 2020-03-20 stsp optarg, NULL);
1058 4ba14133 2020-03-20 stsp if (error)
1059 4ba14133 2020-03-20 stsp return error;
1060 659e7fbd 2020-03-20 stsp break;
1061 41b0de12 2020-03-21 stsp case 'l':
1062 41b0de12 2020-03-21 stsp list_refs_only = 1;
1063 41b0de12 2020-03-21 stsp break;
1064 469dd726 2020-03-20 stsp case 'm':
1065 469dd726 2020-03-20 stsp mirror_references = 1;
1066 469dd726 2020-03-20 stsp break;
1067 68999b92 2020-03-18 stsp case 'v':
1068 68999b92 2020-03-18 stsp if (verbosity < 0)
1069 68999b92 2020-03-18 stsp verbosity = 0;
1070 68999b92 2020-03-18 stsp else if (verbosity < 3)
1071 68999b92 2020-03-18 stsp verbosity++;
1072 68999b92 2020-03-18 stsp break;
1073 68999b92 2020-03-18 stsp case 'q':
1074 68999b92 2020-03-18 stsp verbosity = -1;
1075 68999b92 2020-03-18 stsp break;
1076 0e4002ca 2020-03-21 stsp case 'R':
1077 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1078 0e4002ca 2020-03-21 stsp optarg, NULL);
1079 0e4002ca 2020-03-21 stsp if (error)
1080 0e4002ca 2020-03-21 stsp return error;
1081 0e4002ca 2020-03-21 stsp break;
1082 93658fb9 2020-03-18 stsp default:
1083 93658fb9 2020-03-18 stsp usage_clone();
1084 93658fb9 2020-03-18 stsp break;
1085 93658fb9 2020-03-18 stsp }
1086 93658fb9 2020-03-18 stsp }
1087 93658fb9 2020-03-18 stsp argc -= optind;
1088 93658fb9 2020-03-18 stsp argv += optind;
1089 39c64a6a 2020-03-18 stsp
1090 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1091 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1092 41b0de12 2020-03-21 stsp if (list_refs_only) {
1093 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1094 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1095 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1096 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1097 41b0de12 2020-03-21 stsp if (mirror_references)
1098 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1099 41b0de12 2020-03-21 stsp if (verbosity == -1)
1100 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1101 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1102 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1103 41b0de12 2020-03-21 stsp }
1104 4ba14133 2020-03-20 stsp
1105 93658fb9 2020-03-18 stsp uri = argv[0];
1106 9df6f38b 2020-03-18 stsp
1107 9df6f38b 2020-03-18 stsp if (argc == 1)
1108 93658fb9 2020-03-18 stsp dirname = NULL;
1109 9df6f38b 2020-03-18 stsp else if (argc == 2)
1110 93658fb9 2020-03-18 stsp dirname = argv[1];
1111 93658fb9 2020-03-18 stsp else
1112 93658fb9 2020-03-18 stsp usage_clone();
1113 09838ffc 2020-03-18 stsp
1114 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1115 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1116 39c64a6a 2020-03-18 stsp if (error)
1117 09f63084 2020-03-20 stsp goto done;
1118 09f63084 2020-03-20 stsp
1119 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1120 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1121 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1122 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1123 09838ffc 2020-03-18 stsp goto done;
1124 09f63084 2020-03-20 stsp }
1125 09838ffc 2020-03-18 stsp
1126 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1127 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1128 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1129 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1130 39c64a6a 2020-03-18 stsp err(1, "pledge");
1131 b46f3e71 2020-03-18 stsp #endif
1132 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1133 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1134 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1135 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1136 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1137 39c64a6a 2020-03-18 stsp err(1, "pledge");
1138 b46f3e71 2020-03-18 stsp #endif
1139 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1140 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1141 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1142 39c64a6a 2020-03-18 stsp goto done;
1143 39c64a6a 2020-03-18 stsp } else {
1144 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1145 39c64a6a 2020-03-18 stsp goto done;
1146 39c64a6a 2020-03-18 stsp }
1147 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1148 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1149 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1150 bb64b798 2020-03-18 stsp goto done;
1151 bb64b798 2020-03-18 stsp }
1152 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1153 bb64b798 2020-03-18 stsp } else
1154 bb64b798 2020-03-18 stsp repo_path = dirname;
1155 bb64b798 2020-03-18 stsp
1156 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1157 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1158 41b0de12 2020-03-21 stsp if (error)
1159 41b0de12 2020-03-21 stsp goto done;
1160 bb64b798 2020-03-18 stsp
1161 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1162 41b0de12 2020-03-21 stsp if (error)
1163 41b0de12 2020-03-21 stsp goto done;
1164 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1165 41b0de12 2020-03-21 stsp if (error)
1166 41b0de12 2020-03-21 stsp goto done;
1167 41b0de12 2020-03-21 stsp }
1168 bb64b798 2020-03-18 stsp
1169 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1170 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1171 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1172 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1173 ee448f5f 2020-03-18 stsp goto done;
1174 ee448f5f 2020-03-18 stsp }
1175 ee448f5f 2020-03-18 stsp }
1176 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1177 ee448f5f 2020-03-18 stsp if (error)
1178 ee448f5f 2020-03-18 stsp goto done;
1179 f79e6490 2020-04-19 stsp
1180 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1181 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1182 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1183 ee448f5f 2020-03-18 stsp
1184 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1185 9c52365f 2020-03-21 stsp server_path, verbosity);
1186 39c64a6a 2020-03-18 stsp if (error)
1187 bb64b798 2020-03-18 stsp goto done;
1188 bb64b798 2020-03-18 stsp
1189 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1190 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1191 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1192 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1193 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1194 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1195 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1196 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1197 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1198 39c64a6a 2020-03-18 stsp if (error)
1199 d9b4d0c0 2020-03-18 stsp goto done;
1200 d9b4d0c0 2020-03-18 stsp
1201 41b0de12 2020-03-21 stsp if (list_refs_only) {
1202 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1203 41b0de12 2020-03-21 stsp goto done;
1204 41b0de12 2020-03-21 stsp }
1205 41b0de12 2020-03-21 stsp
1206 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1207 39c64a6a 2020-03-18 stsp if (error)
1208 d9b4d0c0 2020-03-18 stsp goto done;
1209 68999b92 2020-03-18 stsp if (verbosity >= 0)
1210 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1211 d9b4d0c0 2020-03-18 stsp free(id_str);
1212 d9b4d0c0 2020-03-18 stsp
1213 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1214 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1215 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1216 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1217 7ebc0570 2020-03-18 stsp char *remote_refname;
1218 0e4002ca 2020-03-21 stsp
1219 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1220 0e4002ca 2020-03-21 stsp !mirror_references) {
1221 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1222 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1223 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1224 0e4002ca 2020-03-21 stsp if (error)
1225 0e4002ca 2020-03-21 stsp goto done;
1226 0e4002ca 2020-03-21 stsp continue;
1227 0e4002ca 2020-03-21 stsp }
1228 668a20f6 2020-03-18 stsp
1229 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1230 39c64a6a 2020-03-18 stsp if (error)
1231 d9b4d0c0 2020-03-18 stsp goto done;
1232 d9b4d0c0 2020-03-18 stsp
1233 469dd726 2020-03-20 stsp if (mirror_references)
1234 469dd726 2020-03-20 stsp continue;
1235 469dd726 2020-03-20 stsp
1236 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1237 7ebc0570 2020-03-18 stsp continue;
1238 7ebc0570 2020-03-18 stsp
1239 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1240 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1241 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1242 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1243 7ebc0570 2020-03-18 stsp goto done;
1244 7ebc0570 2020-03-18 stsp }
1245 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1246 f298ae0f 2020-03-25 stsp free(remote_refname);
1247 39c64a6a 2020-03-18 stsp if (error)
1248 d9b4d0c0 2020-03-18 stsp goto done;
1249 d9b4d0c0 2020-03-18 stsp }
1250 d9b4d0c0 2020-03-18 stsp
1251 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1252 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1253 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1254 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1255 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1256 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1257 d9b4d0c0 2020-03-18 stsp
1258 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1259 d9b4d0c0 2020-03-18 stsp continue;
1260 d9b4d0c0 2020-03-18 stsp
1261 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1262 39c64a6a 2020-03-18 stsp if (error) {
1263 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1264 55330abe 2020-03-20 stsp error = NULL;
1265 d9b4d0c0 2020-03-18 stsp continue;
1266 55330abe 2020-03-20 stsp }
1267 d9b4d0c0 2020-03-18 stsp goto done;
1268 d9b4d0c0 2020-03-18 stsp }
1269 d9b4d0c0 2020-03-18 stsp
1270 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1271 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1272 f298ae0f 2020-03-25 stsp if (error)
1273 f298ae0f 2020-03-25 stsp goto done;
1274 f298ae0f 2020-03-25 stsp
1275 f298ae0f 2020-03-25 stsp if (mirror_references)
1276 f298ae0f 2020-03-25 stsp continue;
1277 f298ae0f 2020-03-25 stsp
1278 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1279 f298ae0f 2020-03-25 stsp continue;
1280 f298ae0f 2020-03-25 stsp
1281 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1282 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1283 f298ae0f 2020-03-25 stsp refname) == -1) {
1284 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1285 f298ae0f 2020-03-25 stsp goto done;
1286 f298ae0f 2020-03-25 stsp }
1287 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1288 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1289 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1290 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1291 f298ae0f 2020-03-25 stsp free(remote_refname);
1292 f298ae0f 2020-03-25 stsp goto done;
1293 f298ae0f 2020-03-25 stsp }
1294 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1295 f298ae0f 2020-03-25 stsp if (error) {
1296 f298ae0f 2020-03-25 stsp free(remote_refname);
1297 f298ae0f 2020-03-25 stsp free(remote_target);
1298 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1299 f298ae0f 2020-03-25 stsp error = NULL;
1300 f298ae0f 2020-03-25 stsp continue;
1301 f298ae0f 2020-03-25 stsp }
1302 f298ae0f 2020-03-25 stsp goto done;
1303 f298ae0f 2020-03-25 stsp }
1304 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1305 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1306 f298ae0f 2020-03-25 stsp free(remote_refname);
1307 f298ae0f 2020-03-25 stsp free(remote_target);
1308 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1309 39c64a6a 2020-03-18 stsp if (error)
1310 d9b4d0c0 2020-03-18 stsp goto done;
1311 4ba14133 2020-03-20 stsp }
1312 4ba14133 2020-03-20 stsp if (pe == NULL) {
1313 4ba14133 2020-03-20 stsp /*
1314 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1315 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1316 4ba14133 2020-03-20 stsp * which could be fetched instead.
1317 4ba14133 2020-03-20 stsp */
1318 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1319 4ba14133 2020-03-20 stsp const char *target = pe->path;
1320 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1321 d9b4d0c0 2020-03-18 stsp
1322 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1323 4ba14133 2020-03-20 stsp if (error) {
1324 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1325 4ba14133 2020-03-20 stsp error = NULL;
1326 4ba14133 2020-03-20 stsp continue;
1327 4ba14133 2020-03-20 stsp }
1328 4ba14133 2020-03-20 stsp goto done;
1329 4ba14133 2020-03-20 stsp }
1330 4ba14133 2020-03-20 stsp
1331 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1332 f298ae0f 2020-03-25 stsp verbosity, repo);
1333 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1334 4ba14133 2020-03-20 stsp if (error)
1335 4ba14133 2020-03-20 stsp goto done;
1336 4ba14133 2020-03-20 stsp break;
1337 4ba14133 2020-03-20 stsp }
1338 659e7fbd 2020-03-20 stsp }
1339 659e7fbd 2020-03-20 stsp
1340 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1341 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1342 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1343 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1344 659e7fbd 2020-03-20 stsp goto done;
1345 659e7fbd 2020-03-20 stsp }
1346 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1347 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1348 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1349 659e7fbd 2020-03-20 stsp goto done;
1350 b46f3e71 2020-03-18 stsp }
1351 659e7fbd 2020-03-20 stsp if (mirror_references) {
1352 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1353 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1354 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1355 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1356 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1357 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1358 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1359 659e7fbd 2020-03-20 stsp goto done;
1360 659e7fbd 2020-03-20 stsp }
1361 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1362 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1363 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1364 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1365 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1366 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1367 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1368 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1369 659e7fbd 2020-03-20 stsp goto done;
1370 659e7fbd 2020-03-20 stsp }
1371 659e7fbd 2020-03-20 stsp } else {
1372 659e7fbd 2020-03-20 stsp const char *branchname;
1373 d715f13e 2020-03-19 stsp
1374 659e7fbd 2020-03-20 stsp /*
1375 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1376 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1377 659e7fbd 2020-03-20 stsp */
1378 659e7fbd 2020-03-20 stsp if (head_symref) {
1379 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1380 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1381 659e7fbd 2020-03-20 stsp branchname += 11;
1382 659e7fbd 2020-03-20 stsp } else
1383 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1384 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1385 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1386 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1387 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1388 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1389 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1390 659e7fbd 2020-03-20 stsp branchname) == -1) {
1391 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1392 659e7fbd 2020-03-20 stsp goto done;
1393 659e7fbd 2020-03-20 stsp }
1394 659e7fbd 2020-03-20 stsp }
1395 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1396 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1397 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1398 659e7fbd 2020-03-20 stsp goto done;
1399 659e7fbd 2020-03-20 stsp }
1400 659e7fbd 2020-03-20 stsp
1401 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1402 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1403 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1404 09838ffc 2020-03-18 stsp done:
1405 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1406 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1407 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1408 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1409 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1410 9c52365f 2020-03-21 stsp }
1411 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1412 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1413 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1414 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1415 bb64b798 2020-03-18 stsp if (repo)
1416 bb64b798 2020-03-18 stsp got_repo_close(repo);
1417 659e7fbd 2020-03-20 stsp if (head_symref)
1418 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1419 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1420 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1421 d9b4d0c0 2020-03-18 stsp free(pe->data);
1422 d9b4d0c0 2020-03-18 stsp }
1423 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1424 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1425 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1426 d9b4d0c0 2020-03-18 stsp free(pe->data);
1427 d9b4d0c0 2020-03-18 stsp }
1428 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1429 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1430 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1431 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1432 09838ffc 2020-03-18 stsp free(proto);
1433 09838ffc 2020-03-18 stsp free(host);
1434 09838ffc 2020-03-18 stsp free(port);
1435 09838ffc 2020-03-18 stsp free(server_path);
1436 09838ffc 2020-03-18 stsp free(repo_name);
1437 bb64b798 2020-03-18 stsp free(default_destdir);
1438 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1439 b46f3e71 2020-03-18 stsp free(git_url);
1440 39c64a6a 2020-03-18 stsp return error;
1441 7848a0e1 2020-03-19 stsp }
1442 7848a0e1 2020-03-19 stsp
1443 7848a0e1 2020-03-19 stsp static const struct got_error *
1444 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1445 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1446 7848a0e1 2020-03-19 stsp {
1447 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1448 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1449 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1450 7848a0e1 2020-03-19 stsp
1451 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1452 7848a0e1 2020-03-19 stsp if (err)
1453 7848a0e1 2020-03-19 stsp goto done;
1454 7848a0e1 2020-03-19 stsp
1455 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1456 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1457 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1458 88609724 2020-03-21 stsp if (err)
1459 88609724 2020-03-21 stsp goto done;
1460 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1461 88609724 2020-03-21 stsp goto done;
1462 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1463 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1464 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1465 db6d8ad8 2020-03-21 stsp }
1466 db6d8ad8 2020-03-21 stsp goto done;
1467 db6d8ad8 2020-03-21 stsp }
1468 db6d8ad8 2020-03-21 stsp
1469 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1470 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1471 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1472 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1473 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1474 688f11b3 2020-03-21 stsp }
1475 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1476 7848a0e1 2020-03-19 stsp if (err)
1477 7848a0e1 2020-03-19 stsp goto done;
1478 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1479 e8a967e0 2020-03-21 stsp if (err)
1480 e8a967e0 2020-03-21 stsp goto done;
1481 7848a0e1 2020-03-19 stsp } else {
1482 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1483 7848a0e1 2020-03-19 stsp if (err)
1484 7848a0e1 2020-03-19 stsp goto done;
1485 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1486 6338a6a1 2020-03-21 stsp goto done;
1487 6338a6a1 2020-03-21 stsp
1488 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1489 6338a6a1 2020-03-21 stsp if (err)
1490 6338a6a1 2020-03-21 stsp goto done;
1491 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1492 6338a6a1 2020-03-21 stsp if (err)
1493 6338a6a1 2020-03-21 stsp goto done;
1494 7848a0e1 2020-03-19 stsp }
1495 6338a6a1 2020-03-21 stsp
1496 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1497 6338a6a1 2020-03-21 stsp printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1498 6338a6a1 2020-03-21 stsp new_id_str);
1499 7848a0e1 2020-03-19 stsp done:
1500 7848a0e1 2020-03-19 stsp free(old_id);
1501 7848a0e1 2020-03-19 stsp free(new_id_str);
1502 7848a0e1 2020-03-19 stsp return err;
1503 2ab43947 2020-03-18 stsp }
1504 f1bcca34 2020-03-25 stsp
1505 f1bcca34 2020-03-25 stsp static const struct got_error *
1506 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1507 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1508 f1bcca34 2020-03-25 stsp {
1509 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1510 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1511 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1512 f1bcca34 2020-03-25 stsp
1513 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1514 bcf34b0e 2020-03-26 stsp if (err) {
1515 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1516 bcf34b0e 2020-03-26 stsp return err;
1517 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1518 bcf34b0e 2020-03-26 stsp if (err)
1519 bcf34b0e 2020-03-26 stsp goto done;
1520 2ab43947 2020-03-18 stsp
1521 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1522 bcf34b0e 2020-03-26 stsp if (err)
1523 bcf34b0e 2020-03-26 stsp goto done;
1524 f1bcca34 2020-03-25 stsp
1525 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1526 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1527 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1528 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1529 bcf34b0e 2020-03-26 stsp } else {
1530 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1531 f1bcca34 2020-03-25 stsp
1532 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1533 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1534 bcf34b0e 2020-03-26 stsp goto done;
1535 bcf34b0e 2020-03-26 stsp
1536 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1537 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1538 bcf34b0e 2020-03-26 stsp if (err)
1539 bcf34b0e 2020-03-26 stsp goto done;
1540 bcf34b0e 2020-03-26 stsp
1541 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1542 bcf34b0e 2020-03-26 stsp if (err)
1543 bcf34b0e 2020-03-26 stsp goto done;
1544 bcf34b0e 2020-03-26 stsp
1545 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1546 bcf34b0e 2020-03-26 stsp printf("Updated reference %s: %s\n",
1547 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1548 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1549 bcf34b0e 2020-03-26 stsp
1550 bcf34b0e 2020-03-26 stsp }
1551 f1bcca34 2020-03-25 stsp done:
1552 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1553 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1554 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1555 bcf34b0e 2020-03-26 stsp err = unlock_err;
1556 bcf34b0e 2020-03-26 stsp }
1557 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1558 f1bcca34 2020-03-25 stsp return err;
1559 f1bcca34 2020-03-25 stsp }
1560 f1bcca34 2020-03-25 stsp
1561 2ab43947 2020-03-18 stsp __dead static void
1562 7848a0e1 2020-03-19 stsp usage_fetch(void)
1563 7848a0e1 2020-03-19 stsp {
1564 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1565 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1566 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1567 13f12b09 2020-03-21 stsp getprogname());
1568 7848a0e1 2020-03-19 stsp exit(1);
1569 7848a0e1 2020-03-19 stsp }
1570 7848a0e1 2020-03-19 stsp
1571 7848a0e1 2020-03-19 stsp static const struct got_error *
1572 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1573 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1574 f21ec2f0 2020-03-21 stsp {
1575 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1576 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1577 3789fd73 2020-03-26 stsp char *id_str = NULL;
1578 3789fd73 2020-03-26 stsp
1579 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1580 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1581 3789fd73 2020-03-26 stsp if (err)
1582 3789fd73 2020-03-26 stsp return err;
1583 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1584 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1585 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1586 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1587 3789fd73 2020-03-26 stsp }
1588 3789fd73 2020-03-26 stsp } else {
1589 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1590 3789fd73 2020-03-26 stsp if (err)
1591 3789fd73 2020-03-26 stsp return err;
1592 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1593 3789fd73 2020-03-26 stsp if (err)
1594 3789fd73 2020-03-26 stsp goto done;
1595 3789fd73 2020-03-26 stsp
1596 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1597 3789fd73 2020-03-26 stsp if (err)
1598 3789fd73 2020-03-26 stsp goto done;
1599 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1600 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1601 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1602 3789fd73 2020-03-26 stsp }
1603 3789fd73 2020-03-26 stsp }
1604 3789fd73 2020-03-26 stsp done:
1605 3789fd73 2020-03-26 stsp free(id);
1606 3789fd73 2020-03-26 stsp free(id_str);
1607 3789fd73 2020-03-26 stsp return NULL;
1608 3789fd73 2020-03-26 stsp }
1609 3789fd73 2020-03-26 stsp
1610 3789fd73 2020-03-26 stsp static const struct got_error *
1611 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1612 3789fd73 2020-03-26 stsp struct got_pathlist_head *their_symrefs, struct got_remote_repo *remote,
1613 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1614 3789fd73 2020-03-26 stsp {
1615 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1616 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1617 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1618 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1619 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1620 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1621 f21ec2f0 2020-03-21 stsp
1622 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1623 f21ec2f0 2020-03-21 stsp
1624 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1625 3789fd73 2020-03-26 stsp == -1)
1626 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1627 3789fd73 2020-03-26 stsp
1628 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1629 f21ec2f0 2020-03-21 stsp if (err)
1630 3789fd73 2020-03-26 stsp goto done;
1631 f21ec2f0 2020-03-21 stsp
1632 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1633 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1634 f21ec2f0 2020-03-21 stsp
1635 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1636 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1637 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1638 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1639 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1640 3789fd73 2020-03-26 stsp continue;
1641 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1642 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1643 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1644 3789fd73 2020-03-26 stsp goto done;
1645 3789fd73 2020-03-26 stsp }
1646 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1647 3789fd73 2020-03-26 stsp continue;
1648 3789fd73 2020-03-26 stsp }
1649 f21ec2f0 2020-03-21 stsp
1650 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1651 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1652 f21ec2f0 2020-03-21 stsp break;
1653 f21ec2f0 2020-03-21 stsp }
1654 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1655 f21ec2f0 2020-03-21 stsp continue;
1656 f21ec2f0 2020-03-21 stsp
1657 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1658 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1659 3789fd73 2020-03-26 stsp break;
1660 3789fd73 2020-03-26 stsp }
1661 3789fd73 2020-03-26 stsp if (pe != NULL)
1662 3789fd73 2020-03-26 stsp continue;
1663 f21ec2f0 2020-03-21 stsp
1664 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1665 f21ec2f0 2020-03-21 stsp if (err)
1666 f21ec2f0 2020-03-21 stsp break;
1667 3789fd73 2020-03-26 stsp
1668 3789fd73 2020-03-26 stsp if (local_refname) {
1669 3789fd73 2020-03-26 stsp struct got_reference *ref;
1670 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1671 3789fd73 2020-03-26 stsp if (err) {
1672 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1673 3789fd73 2020-03-26 stsp break;
1674 3789fd73 2020-03-26 stsp free(local_refname);
1675 3789fd73 2020-03-26 stsp local_refname = NULL;
1676 3789fd73 2020-03-26 stsp continue;
1677 3789fd73 2020-03-26 stsp }
1678 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1679 3789fd73 2020-03-26 stsp if (err)
1680 3789fd73 2020-03-26 stsp break;
1681 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1682 3789fd73 2020-03-26 stsp got_ref_close(ref);
1683 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1684 3789fd73 2020-03-26 stsp err = unlock_err;
1685 3789fd73 2020-03-26 stsp break;
1686 3789fd73 2020-03-26 stsp }
1687 3789fd73 2020-03-26 stsp
1688 3789fd73 2020-03-26 stsp free(local_refname);
1689 3789fd73 2020-03-26 stsp local_refname = NULL;
1690 6338a6a1 2020-03-21 stsp }
1691 f21ec2f0 2020-03-21 stsp }
1692 3789fd73 2020-03-26 stsp done:
1693 3789fd73 2020-03-26 stsp free(remote_namespace);
1694 3789fd73 2020-03-26 stsp free(local_refname);
1695 0e4002ca 2020-03-21 stsp return err;
1696 0e4002ca 2020-03-21 stsp }
1697 0e4002ca 2020-03-21 stsp
1698 0e4002ca 2020-03-21 stsp static const struct got_error *
1699 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1700 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1701 0e4002ca 2020-03-21 stsp {
1702 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1703 0e4002ca 2020-03-21 stsp char *remote_refname;
1704 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1705 0e4002ca 2020-03-21 stsp
1706 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1707 0e4002ca 2020-03-21 stsp refname += 5;
1708 0e4002ca 2020-03-21 stsp
1709 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1710 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1711 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1712 f21ec2f0 2020-03-21 stsp
1713 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1714 0e4002ca 2020-03-21 stsp if (err) {
1715 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1716 0e4002ca 2020-03-21 stsp goto done;
1717 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1718 0e4002ca 2020-03-21 stsp } else {
1719 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1720 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1721 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1722 9f142382 2020-03-21 stsp err = unlock_err;
1723 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1724 0e4002ca 2020-03-21 stsp }
1725 0e4002ca 2020-03-21 stsp done:
1726 0e4002ca 2020-03-21 stsp free(remote_refname);
1727 f21ec2f0 2020-03-21 stsp return err;
1728 f21ec2f0 2020-03-21 stsp }
1729 f21ec2f0 2020-03-21 stsp
1730 f21ec2f0 2020-03-21 stsp static const struct got_error *
1731 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1732 7848a0e1 2020-03-19 stsp {
1733 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1734 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1735 7848a0e1 2020-03-19 stsp const char *remote_name;
1736 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1737 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1738 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1739 7848a0e1 2020-03-19 stsp int nremotes;
1740 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1741 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1742 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1743 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1744 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1745 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1746 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1747 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1748 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1749 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1750 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1751 7848a0e1 2020-03-19 stsp
1752 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1753 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1754 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1755 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1756 7848a0e1 2020-03-19 stsp
1757 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1758 7848a0e1 2020-03-19 stsp switch (ch) {
1759 659e7fbd 2020-03-20 stsp case 'a':
1760 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1761 4ba14133 2020-03-20 stsp break;
1762 4ba14133 2020-03-20 stsp case 'b':
1763 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1764 4ba14133 2020-03-20 stsp optarg, NULL);
1765 4ba14133 2020-03-20 stsp if (error)
1766 4ba14133 2020-03-20 stsp return error;
1767 41b0de12 2020-03-21 stsp break;
1768 f21ec2f0 2020-03-21 stsp case 'd':
1769 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1770 f21ec2f0 2020-03-21 stsp break;
1771 41b0de12 2020-03-21 stsp case 'l':
1772 41b0de12 2020-03-21 stsp list_refs_only = 1;
1773 659e7fbd 2020-03-20 stsp break;
1774 7848a0e1 2020-03-19 stsp case 'r':
1775 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1776 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1777 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1778 7848a0e1 2020-03-19 stsp optarg);
1779 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1780 7848a0e1 2020-03-19 stsp break;
1781 db6d8ad8 2020-03-21 stsp case 't':
1782 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1783 db6d8ad8 2020-03-21 stsp break;
1784 7848a0e1 2020-03-19 stsp case 'v':
1785 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1786 7848a0e1 2020-03-19 stsp verbosity = 0;
1787 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1788 7848a0e1 2020-03-19 stsp verbosity++;
1789 7848a0e1 2020-03-19 stsp break;
1790 7848a0e1 2020-03-19 stsp case 'q':
1791 7848a0e1 2020-03-19 stsp verbosity = -1;
1792 7848a0e1 2020-03-19 stsp break;
1793 0e4002ca 2020-03-21 stsp case 'R':
1794 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1795 0e4002ca 2020-03-21 stsp optarg, NULL);
1796 0e4002ca 2020-03-21 stsp if (error)
1797 0e4002ca 2020-03-21 stsp return error;
1798 0e4002ca 2020-03-21 stsp break;
1799 7848a0e1 2020-03-19 stsp default:
1800 7848a0e1 2020-03-19 stsp usage_fetch();
1801 7848a0e1 2020-03-19 stsp break;
1802 7848a0e1 2020-03-19 stsp }
1803 7848a0e1 2020-03-19 stsp }
1804 7848a0e1 2020-03-19 stsp argc -= optind;
1805 7848a0e1 2020-03-19 stsp argv += optind;
1806 7848a0e1 2020-03-19 stsp
1807 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1808 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1809 41b0de12 2020-03-21 stsp if (list_refs_only) {
1810 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1811 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1812 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1813 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1814 f21ec2f0 2020-03-21 stsp if (delete_refs)
1815 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1816 41b0de12 2020-03-21 stsp if (verbosity == -1)
1817 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1818 41b0de12 2020-03-21 stsp }
1819 4ba14133 2020-03-20 stsp
1820 7848a0e1 2020-03-19 stsp if (argc == 0)
1821 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1822 7848a0e1 2020-03-19 stsp else if (argc == 1)
1823 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1824 7848a0e1 2020-03-19 stsp else
1825 7848a0e1 2020-03-19 stsp usage_fetch();
1826 7848a0e1 2020-03-19 stsp
1827 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1828 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1829 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1830 7848a0e1 2020-03-19 stsp goto done;
1831 7848a0e1 2020-03-19 stsp }
1832 7848a0e1 2020-03-19 stsp
1833 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1834 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1835 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1836 7848a0e1 2020-03-19 stsp goto done;
1837 7848a0e1 2020-03-19 stsp else
1838 7848a0e1 2020-03-19 stsp error = NULL;
1839 7848a0e1 2020-03-19 stsp if (worktree) {
1840 7848a0e1 2020-03-19 stsp repo_path =
1841 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1842 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1843 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1844 7848a0e1 2020-03-19 stsp if (error)
1845 7848a0e1 2020-03-19 stsp goto done;
1846 7848a0e1 2020-03-19 stsp } else {
1847 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1848 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1849 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1850 7848a0e1 2020-03-19 stsp goto done;
1851 7848a0e1 2020-03-19 stsp }
1852 7848a0e1 2020-03-19 stsp }
1853 7848a0e1 2020-03-19 stsp }
1854 7848a0e1 2020-03-19 stsp
1855 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1856 7848a0e1 2020-03-19 stsp if (error)
1857 7848a0e1 2020-03-19 stsp goto done;
1858 7848a0e1 2020-03-19 stsp
1859 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1860 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1861 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1862 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1863 7848a0e1 2020-03-19 stsp break;
1864 7848a0e1 2020-03-19 stsp }
1865 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1866 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1867 7848a0e1 2020-03-19 stsp goto done;
1868 7848a0e1 2020-03-19 stsp }
1869 7848a0e1 2020-03-19 stsp
1870 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1871 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1872 7848a0e1 2020-03-19 stsp if (error)
1873 7848a0e1 2020-03-19 stsp goto done;
1874 7848a0e1 2020-03-19 stsp
1875 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1876 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1877 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1878 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1879 7848a0e1 2020-03-19 stsp err(1, "pledge");
1880 7848a0e1 2020-03-19 stsp #endif
1881 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1882 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1883 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1884 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1885 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1886 7848a0e1 2020-03-19 stsp err(1, "pledge");
1887 7848a0e1 2020-03-19 stsp #endif
1888 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1889 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1890 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1891 7848a0e1 2020-03-19 stsp goto done;
1892 7848a0e1 2020-03-19 stsp } else {
1893 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1894 7848a0e1 2020-03-19 stsp goto done;
1895 7848a0e1 2020-03-19 stsp }
1896 7848a0e1 2020-03-19 stsp
1897 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1898 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1899 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1900 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1901 7848a0e1 2020-03-19 stsp goto done;
1902 7848a0e1 2020-03-19 stsp }
1903 7848a0e1 2020-03-19 stsp }
1904 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1905 7848a0e1 2020-03-19 stsp if (error)
1906 7848a0e1 2020-03-19 stsp goto done;
1907 f79e6490 2020-04-19 stsp
1908 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1909 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
1910 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1911 7848a0e1 2020-03-19 stsp
1912 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1913 9c52365f 2020-03-21 stsp server_path, verbosity);
1914 7848a0e1 2020-03-19 stsp if (error)
1915 7848a0e1 2020-03-19 stsp goto done;
1916 7848a0e1 2020-03-19 stsp
1917 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1918 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1919 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1920 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1921 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1922 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1923 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1924 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1925 7848a0e1 2020-03-19 stsp if (error)
1926 7848a0e1 2020-03-19 stsp goto done;
1927 7848a0e1 2020-03-19 stsp
1928 41b0de12 2020-03-21 stsp if (list_refs_only) {
1929 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1930 41b0de12 2020-03-21 stsp goto done;
1931 41b0de12 2020-03-21 stsp }
1932 41b0de12 2020-03-21 stsp
1933 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1934 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1935 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1936 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
1937 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1938 984065c8 2020-03-19 stsp if (error)
1939 984065c8 2020-03-19 stsp goto done;
1940 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1941 984065c8 2020-03-19 stsp free(id_str);
1942 984065c8 2020-03-19 stsp id_str = NULL;
1943 984065c8 2020-03-19 stsp }
1944 7848a0e1 2020-03-19 stsp
1945 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1946 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1947 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1948 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1949 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1950 7848a0e1 2020-03-19 stsp char *remote_refname;
1951 7848a0e1 2020-03-19 stsp
1952 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1953 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
1954 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
1955 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
1956 0e4002ca 2020-03-21 stsp if (error)
1957 0e4002ca 2020-03-21 stsp goto done;
1958 0e4002ca 2020-03-21 stsp continue;
1959 0e4002ca 2020-03-21 stsp }
1960 0e4002ca 2020-03-21 stsp
1961 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1962 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1963 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1964 7848a0e1 2020-03-19 stsp if (error) {
1965 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1966 7848a0e1 2020-03-19 stsp goto done;
1967 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1968 6338a6a1 2020-03-21 stsp repo);
1969 7848a0e1 2020-03-19 stsp if (error)
1970 7848a0e1 2020-03-19 stsp goto done;
1971 7848a0e1 2020-03-19 stsp } else {
1972 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1973 db6d8ad8 2020-03-21 stsp verbosity, repo);
1974 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1975 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1976 9f142382 2020-03-21 stsp error = unlock_err;
1977 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1978 7848a0e1 2020-03-19 stsp if (error)
1979 7848a0e1 2020-03-19 stsp goto done;
1980 7848a0e1 2020-03-19 stsp }
1981 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1982 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1983 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1984 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1985 7848a0e1 2020-03-19 stsp goto done;
1986 7848a0e1 2020-03-19 stsp }
1987 7848a0e1 2020-03-19 stsp
1988 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
1989 7848a0e1 2020-03-19 stsp if (error) {
1990 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1991 7848a0e1 2020-03-19 stsp goto done;
1992 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
1993 688f11b3 2020-03-21 stsp verbosity, repo);
1994 7848a0e1 2020-03-19 stsp if (error)
1995 7848a0e1 2020-03-19 stsp goto done;
1996 7848a0e1 2020-03-19 stsp } else {
1997 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1998 db6d8ad8 2020-03-21 stsp verbosity, repo);
1999 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2000 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2001 9f142382 2020-03-21 stsp error = unlock_err;
2002 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2003 7848a0e1 2020-03-19 stsp if (error)
2004 7848a0e1 2020-03-19 stsp goto done;
2005 7848a0e1 2020-03-19 stsp }
2006 2ec30c80 2020-03-20 stsp
2007 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2008 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2009 2ec30c80 2020-03-20 stsp if (error) {
2010 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2011 2ec30c80 2020-03-20 stsp goto done;
2012 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2013 6338a6a1 2020-03-21 stsp repo);
2014 2ec30c80 2020-03-20 stsp if (error)
2015 2ec30c80 2020-03-20 stsp goto done;
2016 9f142382 2020-03-21 stsp } else {
2017 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2018 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2019 9f142382 2020-03-21 stsp error = unlock_err;
2020 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2021 9f142382 2020-03-21 stsp }
2022 7848a0e1 2020-03-19 stsp }
2023 7848a0e1 2020-03-19 stsp }
2024 f1bcca34 2020-03-25 stsp if (delete_refs) {
2025 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2026 3789fd73 2020-03-26 stsp verbosity, repo);
2027 f1bcca34 2020-03-25 stsp if (error)
2028 f1bcca34 2020-03-25 stsp goto done;
2029 f1bcca34 2020-03-25 stsp }
2030 f1bcca34 2020-03-25 stsp
2031 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2032 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2033 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2034 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2035 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2036 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2037 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2038 f1bcca34 2020-03-25 stsp
2039 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2040 f1bcca34 2020-03-25 stsp continue;
2041 f1bcca34 2020-03-25 stsp
2042 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2043 f1bcca34 2020-03-25 stsp continue;
2044 f1bcca34 2020-03-25 stsp
2045 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2046 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2047 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2048 f1bcca34 2020-03-25 stsp goto done;
2049 f1bcca34 2020-03-25 stsp }
2050 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2051 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2052 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2053 f1bcca34 2020-03-25 stsp free(remote_refname);
2054 f1bcca34 2020-03-25 stsp goto done;
2055 f1bcca34 2020-03-25 stsp }
2056 f1bcca34 2020-03-25 stsp
2057 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2058 f1bcca34 2020-03-25 stsp 0);
2059 f1bcca34 2020-03-25 stsp if (error) {
2060 f1bcca34 2020-03-25 stsp free(remote_refname);
2061 f1bcca34 2020-03-25 stsp free(remote_target);
2062 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2063 f1bcca34 2020-03-25 stsp error = NULL;
2064 f1bcca34 2020-03-25 stsp continue;
2065 f1bcca34 2020-03-25 stsp }
2066 f1bcca34 2020-03-25 stsp goto done;
2067 f1bcca34 2020-03-25 stsp }
2068 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2069 f1bcca34 2020-03-25 stsp verbosity, repo);
2070 f1bcca34 2020-03-25 stsp free(remote_refname);
2071 f1bcca34 2020-03-25 stsp free(remote_target);
2072 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2073 f1bcca34 2020-03-25 stsp if (error)
2074 f1bcca34 2020-03-25 stsp goto done;
2075 f1bcca34 2020-03-25 stsp }
2076 f1bcca34 2020-03-25 stsp }
2077 7848a0e1 2020-03-19 stsp done:
2078 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2079 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2080 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2081 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2082 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2083 9c52365f 2020-03-21 stsp }
2084 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2085 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2086 7848a0e1 2020-03-19 stsp if (repo)
2087 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2088 7848a0e1 2020-03-19 stsp if (worktree)
2089 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2090 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2091 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2092 7848a0e1 2020-03-19 stsp free(pe->data);
2093 7848a0e1 2020-03-19 stsp }
2094 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2095 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2096 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2097 7848a0e1 2020-03-19 stsp free(pe->data);
2098 7848a0e1 2020-03-19 stsp }
2099 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2100 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2101 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2102 7848a0e1 2020-03-19 stsp free(id_str);
2103 7848a0e1 2020-03-19 stsp free(cwd);
2104 7848a0e1 2020-03-19 stsp free(repo_path);
2105 7848a0e1 2020-03-19 stsp free(pack_hash);
2106 7848a0e1 2020-03-19 stsp free(proto);
2107 7848a0e1 2020-03-19 stsp free(host);
2108 7848a0e1 2020-03-19 stsp free(port);
2109 7848a0e1 2020-03-19 stsp free(server_path);
2110 7848a0e1 2020-03-19 stsp free(repo_name);
2111 7848a0e1 2020-03-19 stsp return error;
2112 7848a0e1 2020-03-19 stsp }
2113 7848a0e1 2020-03-19 stsp
2114 7848a0e1 2020-03-19 stsp
2115 7848a0e1 2020-03-19 stsp __dead static void
2116 2ab43947 2020-03-18 stsp usage_checkout(void)
2117 2ab43947 2020-03-18 stsp {
2118 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2119 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2120 2ab43947 2020-03-18 stsp exit(1);
2121 2ab43947 2020-03-18 stsp }
2122 2ab43947 2020-03-18 stsp
2123 2ab43947 2020-03-18 stsp static void
2124 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2125 2ab43947 2020-03-18 stsp {
2126 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2127 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2128 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2129 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2130 2ab43947 2020-03-18 stsp getprogname());
2131 2ab43947 2020-03-18 stsp }
2132 2ab43947 2020-03-18 stsp
2133 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2134 2ab43947 2020-03-18 stsp const char *worktree_path;
2135 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2136 2ab43947 2020-03-18 stsp };
2137 2ab43947 2020-03-18 stsp
2138 2ab43947 2020-03-18 stsp static const struct got_error *
2139 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2140 2ab43947 2020-03-18 stsp {
2141 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2142 2ab43947 2020-03-18 stsp
2143 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2144 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2145 2ab43947 2020-03-18 stsp return NULL;
2146 2ab43947 2020-03-18 stsp
2147 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2148 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2149 2ab43947 2020-03-18 stsp return NULL;
2150 2ab43947 2020-03-18 stsp }
2151 2ab43947 2020-03-18 stsp
2152 2ab43947 2020-03-18 stsp while (path[0] == '/')
2153 2ab43947 2020-03-18 stsp path++;
2154 2ab43947 2020-03-18 stsp
2155 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2156 2ab43947 2020-03-18 stsp return NULL;
2157 2ab43947 2020-03-18 stsp }
2158 2ab43947 2020-03-18 stsp
2159 2ab43947 2020-03-18 stsp static const struct got_error *
2160 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2161 2ab43947 2020-03-18 stsp {
2162 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2163 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2164 2ab43947 2020-03-18 stsp return NULL;
2165 2ab43947 2020-03-18 stsp }
2166 2ab43947 2020-03-18 stsp
2167 2ab43947 2020-03-18 stsp static const struct got_error *
2168 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2169 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2170 2ab43947 2020-03-18 stsp struct got_repository *repo)
2171 2ab43947 2020-03-18 stsp {
2172 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2173 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2174 2ab43947 2020-03-18 stsp
2175 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2176 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2177 2ab43947 2020-03-18 stsp if (err)
2178 2ab43947 2020-03-18 stsp return err;
2179 2ab43947 2020-03-18 stsp
2180 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2181 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2182 2ab43947 2020-03-18 stsp
2183 2ab43947 2020-03-18 stsp /*
2184 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2185 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2186 2ab43947 2020-03-18 stsp *
2187 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2188 2ab43947 2020-03-18 stsp *
2189 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2190 2ab43947 2020-03-18 stsp * \ /
2191 2ab43947 2020-03-18 stsp * C E
2192 2ab43947 2020-03-18 stsp * \ /
2193 2ab43947 2020-03-18 stsp * B (yca)
2194 2ab43947 2020-03-18 stsp * |
2195 2ab43947 2020-03-18 stsp * A
2196 2ab43947 2020-03-18 stsp *
2197 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2198 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2199 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2200 2ab43947 2020-03-18 stsp */
2201 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2202 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2203 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2204 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2205 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2206 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2207 2ab43947 2020-03-18 stsp
2208 2ab43947 2020-03-18 stsp free(yca_id);
2209 2ab43947 2020-03-18 stsp return NULL;
2210 2ab43947 2020-03-18 stsp }
2211 2ab43947 2020-03-18 stsp
2212 2ab43947 2020-03-18 stsp static const struct got_error *
2213 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2214 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2215 2ab43947 2020-03-18 stsp struct got_repository *repo)
2216 2ab43947 2020-03-18 stsp {
2217 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2218 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2219 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2220 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2221 2ab43947 2020-03-18 stsp
2222 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2223 2ab43947 2020-03-18 stsp if (err)
2224 2ab43947 2020-03-18 stsp goto done;
2225 2ab43947 2020-03-18 stsp
2226 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2227 2ab43947 2020-03-18 stsp is_same_branch = 1;
2228 2ab43947 2020-03-18 stsp goto done;
2229 2ab43947 2020-03-18 stsp }
2230 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2231 2ab43947 2020-03-18 stsp is_same_branch = 1;
2232 2ab43947 2020-03-18 stsp goto done;
2233 2ab43947 2020-03-18 stsp }
2234 2ab43947 2020-03-18 stsp
2235 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2236 2ab43947 2020-03-18 stsp if (err)
2237 2ab43947 2020-03-18 stsp goto done;
2238 2ab43947 2020-03-18 stsp
2239 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2240 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2241 2ab43947 2020-03-18 stsp if (err)
2242 2ab43947 2020-03-18 stsp goto done;
2243 2ab43947 2020-03-18 stsp
2244 2ab43947 2020-03-18 stsp for (;;) {
2245 2ab43947 2020-03-18 stsp struct got_object_id *id;
2246 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2247 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2248 2ab43947 2020-03-18 stsp if (err) {
2249 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2250 2ab43947 2020-03-18 stsp err = NULL;
2251 2ab43947 2020-03-18 stsp break;
2252 2ab43947 2020-03-18 stsp }
2253 2ab43947 2020-03-18 stsp
2254 2ab43947 2020-03-18 stsp if (id) {
2255 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2256 2ab43947 2020-03-18 stsp break;
2257 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2258 2ab43947 2020-03-18 stsp is_same_branch = 1;
2259 2ab43947 2020-03-18 stsp break;
2260 2ab43947 2020-03-18 stsp }
2261 2ab43947 2020-03-18 stsp }
2262 2ab43947 2020-03-18 stsp }
2263 2ab43947 2020-03-18 stsp done:
2264 2ab43947 2020-03-18 stsp if (graph)
2265 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2266 2ab43947 2020-03-18 stsp free(head_commit_id);
2267 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2268 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2269 2ab43947 2020-03-18 stsp return err;
2270 a367ff0f 2019-05-14 stsp }
2271 8069f636 2019-01-12 stsp
2272 4ed7e80c 2018-05-20 stsp static const struct got_error *
2273 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2274 4b6c9460 2020-03-05 stsp {
2275 4b6c9460 2020-03-05 stsp static char msg[512];
2276 4b6c9460 2020-03-05 stsp const char *branch_name;
2277 4b6c9460 2020-03-05 stsp
2278 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2279 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2280 4b6c9460 2020-03-05 stsp else
2281 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2282 4b6c9460 2020-03-05 stsp
2283 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2284 4b6c9460 2020-03-05 stsp branch_name += 11;
2285 4b6c9460 2020-03-05 stsp
2286 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2287 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2288 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2289 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2290 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2291 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2292 4b6c9460 2020-03-05 stsp
2293 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2294 4b6c9460 2020-03-05 stsp }
2295 4b6c9460 2020-03-05 stsp
2296 4b6c9460 2020-03-05 stsp static const struct got_error *
2297 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2298 c09a553d 2018-03-12 stsp {
2299 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2300 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2301 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2302 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2303 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2304 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2305 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2306 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2307 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2308 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2309 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2310 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2311 f2ea84fa 2019-07-27 stsp
2312 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2313 c09a553d 2018-03-12 stsp
2314 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2315 0bb8a95e 2018-03-12 stsp switch (ch) {
2316 08573d5b 2019-05-14 stsp case 'b':
2317 08573d5b 2019-05-14 stsp branch_name = optarg;
2318 08573d5b 2019-05-14 stsp break;
2319 8069f636 2019-01-12 stsp case 'c':
2320 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2321 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2322 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2323 bb51a5b4 2020-01-13 stsp break;
2324 bb51a5b4 2020-01-13 stsp case 'E':
2325 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2326 8069f636 2019-01-12 stsp break;
2327 0bb8a95e 2018-03-12 stsp case 'p':
2328 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2329 0bb8a95e 2018-03-12 stsp break;
2330 0bb8a95e 2018-03-12 stsp default:
2331 2deda0b9 2019-03-07 stsp usage_checkout();
2332 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2333 0bb8a95e 2018-03-12 stsp }
2334 0bb8a95e 2018-03-12 stsp }
2335 0bb8a95e 2018-03-12 stsp
2336 0bb8a95e 2018-03-12 stsp argc -= optind;
2337 0bb8a95e 2018-03-12 stsp argv += optind;
2338 0bb8a95e 2018-03-12 stsp
2339 6715a751 2018-03-16 stsp #ifndef PROFILE
2340 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2341 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2342 c09a553d 2018-03-12 stsp err(1, "pledge");
2343 6715a751 2018-03-16 stsp #endif
2344 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2345 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2346 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2347 76089277 2018-04-01 stsp if (repo_path == NULL)
2348 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2349 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2350 76089277 2018-04-01 stsp if (cwd == NULL) {
2351 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2352 76089277 2018-04-01 stsp goto done;
2353 76089277 2018-04-01 stsp }
2354 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2355 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2356 230a42bd 2019-05-11 jcs if (base == NULL) {
2357 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2358 230a42bd 2019-05-11 jcs path_prefix);
2359 230a42bd 2019-05-11 jcs goto done;
2360 230a42bd 2019-05-11 jcs }
2361 230a42bd 2019-05-11 jcs } else {
2362 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2363 230a42bd 2019-05-11 jcs if (base == NULL) {
2364 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2365 230a42bd 2019-05-11 jcs repo_path);
2366 230a42bd 2019-05-11 jcs goto done;
2367 230a42bd 2019-05-11 jcs }
2368 76089277 2018-04-01 stsp }
2369 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2370 c09a553d 2018-03-12 stsp if (dotgit)
2371 c09a553d 2018-03-12 stsp *dotgit = '\0';
2372 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2373 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2374 c09a553d 2018-03-12 stsp free(cwd);
2375 76089277 2018-04-01 stsp goto done;
2376 c09a553d 2018-03-12 stsp }
2377 c09a553d 2018-03-12 stsp free(cwd);
2378 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2379 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2380 76089277 2018-04-01 stsp if (repo_path == NULL) {
2381 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2382 76089277 2018-04-01 stsp goto done;
2383 76089277 2018-04-01 stsp }
2384 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2385 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2386 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2387 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2388 b4b3a7dd 2019-07-22 stsp argv[1]);
2389 b4b3a7dd 2019-07-22 stsp goto done;
2390 b4b3a7dd 2019-07-22 stsp }
2391 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2392 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2393 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2394 b4b3a7dd 2019-07-22 stsp goto done;
2395 b4b3a7dd 2019-07-22 stsp }
2396 76089277 2018-04-01 stsp }
2397 c09a553d 2018-03-12 stsp } else
2398 c09a553d 2018-03-12 stsp usage_checkout();
2399 c09a553d 2018-03-12 stsp
2400 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2401 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2402 13bfb272 2019-05-10 jcs
2403 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2404 c09a553d 2018-03-12 stsp if (error != NULL)
2405 c02c541e 2019-03-29 stsp goto done;
2406 c02c541e 2019-03-29 stsp
2407 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2408 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2409 c530dc23 2019-07-23 stsp if (error) {
2410 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2411 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2412 c530dc23 2019-07-23 stsp goto done;
2413 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2414 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2415 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2416 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2417 c530dc23 2019-07-23 stsp goto done;
2418 c530dc23 2019-07-23 stsp }
2419 c530dc23 2019-07-23 stsp }
2420 c530dc23 2019-07-23 stsp
2421 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2422 c02c541e 2019-03-29 stsp if (error)
2423 c09a553d 2018-03-12 stsp goto done;
2424 8069f636 2019-01-12 stsp
2425 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2426 c09a553d 2018-03-12 stsp if (error != NULL)
2427 c09a553d 2018-03-12 stsp goto done;
2428 c09a553d 2018-03-12 stsp
2429 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2430 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2431 c09a553d 2018-03-12 stsp goto done;
2432 c09a553d 2018-03-12 stsp
2433 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2434 c09a553d 2018-03-12 stsp if (error != NULL)
2435 c09a553d 2018-03-12 stsp goto done;
2436 c09a553d 2018-03-12 stsp
2437 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2438 e5dc7198 2018-12-29 stsp path_prefix);
2439 e5dc7198 2018-12-29 stsp if (error != NULL)
2440 e5dc7198 2018-12-29 stsp goto done;
2441 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2442 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2443 49520a32 2018-12-29 stsp goto done;
2444 49520a32 2018-12-29 stsp }
2445 49520a32 2018-12-29 stsp
2446 8069f636 2019-01-12 stsp if (commit_id_str) {
2447 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2448 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2449 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2450 30837e32 2019-07-25 stsp if (error)
2451 8069f636 2019-01-12 stsp goto done;
2452 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2453 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2454 8069f636 2019-01-12 stsp if (error != NULL) {
2455 8069f636 2019-01-12 stsp free(commit_id);
2456 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2457 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2458 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2459 4b6c9460 2020-03-05 stsp }
2460 8069f636 2019-01-12 stsp goto done;
2461 8069f636 2019-01-12 stsp }
2462 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2463 4b6c9460 2020-03-05 stsp if (error) {
2464 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2465 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2466 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2467 4b6c9460 2020-03-05 stsp }
2468 45d344f6 2019-05-14 stsp goto done;
2469 4b6c9460 2020-03-05 stsp }
2470 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2471 8069f636 2019-01-12 stsp commit_id);
2472 8069f636 2019-01-12 stsp free(commit_id);
2473 8069f636 2019-01-12 stsp if (error)
2474 8069f636 2019-01-12 stsp goto done;
2475 8069f636 2019-01-12 stsp }
2476 8069f636 2019-01-12 stsp
2477 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2478 f2ea84fa 2019-07-27 stsp if (error)
2479 f2ea84fa 2019-07-27 stsp goto done;
2480 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2481 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2482 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2483 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2484 c09a553d 2018-03-12 stsp if (error != NULL)
2485 c09a553d 2018-03-12 stsp goto done;
2486 c09a553d 2018-03-12 stsp
2487 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2488 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2489 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2490 c09a553d 2018-03-12 stsp done:
2491 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2492 8069f636 2019-01-12 stsp free(commit_id_str);
2493 76089277 2018-04-01 stsp free(repo_path);
2494 507dc3bb 2018-12-29 stsp free(worktree_path);
2495 507dc3bb 2018-12-29 stsp return error;
2496 9627c110 2020-04-18 stsp }
2497 9627c110 2020-04-18 stsp
2498 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2499 9627c110 2020-04-18 stsp int did_something;
2500 9627c110 2020-04-18 stsp int conflicts;
2501 9627c110 2020-04-18 stsp int obstructed;
2502 9627c110 2020-04-18 stsp int not_updated;
2503 9627c110 2020-04-18 stsp };
2504 9627c110 2020-04-18 stsp
2505 9627c110 2020-04-18 stsp void
2506 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2507 9627c110 2020-04-18 stsp {
2508 9627c110 2020-04-18 stsp if (!upa->did_something)
2509 9627c110 2020-04-18 stsp return;
2510 9627c110 2020-04-18 stsp
2511 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2512 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2513 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2514 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2515 9627c110 2020-04-18 stsp upa->obstructed);
2516 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2517 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2518 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2519 507dc3bb 2018-12-29 stsp }
2520 507dc3bb 2018-12-29 stsp
2521 507dc3bb 2018-12-29 stsp __dead static void
2522 507dc3bb 2018-12-29 stsp usage_update(void)
2523 507dc3bb 2018-12-29 stsp {
2524 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2525 507dc3bb 2018-12-29 stsp getprogname());
2526 507dc3bb 2018-12-29 stsp exit(1);
2527 507dc3bb 2018-12-29 stsp }
2528 507dc3bb 2018-12-29 stsp
2529 1ee397ad 2019-07-12 stsp static const struct got_error *
2530 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2531 507dc3bb 2018-12-29 stsp {
2532 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2533 784955db 2019-01-12 stsp
2534 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2535 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2536 1ee397ad 2019-07-12 stsp return NULL;
2537 507dc3bb 2018-12-29 stsp
2538 9627c110 2020-04-18 stsp upa->did_something = 1;
2539 a484d721 2019-06-10 stsp
2540 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2541 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2542 1ee397ad 2019-07-12 stsp return NULL;
2543 a484d721 2019-06-10 stsp
2544 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2545 9627c110 2020-04-18 stsp upa->conflicts++;
2546 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2547 9627c110 2020-04-18 stsp upa->obstructed++;
2548 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2549 9627c110 2020-04-18 stsp upa->not_updated++;
2550 9627c110 2020-04-18 stsp
2551 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2552 507dc3bb 2018-12-29 stsp path++;
2553 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2554 1ee397ad 2019-07-12 stsp return NULL;
2555 be7061eb 2018-12-30 stsp }
2556 be7061eb 2018-12-30 stsp
2557 be7061eb 2018-12-30 stsp static const struct got_error *
2558 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2559 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2560 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2561 a1fb16d8 2019-05-24 stsp {
2562 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2563 a1fb16d8 2019-05-24 stsp char *base_id_str;
2564 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2565 a1fb16d8 2019-05-24 stsp
2566 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2567 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2568 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2569 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2570 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2571 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2572 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2573 a1fb16d8 2019-05-24 stsp }
2574 a1fb16d8 2019-05-24 stsp
2575 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2576 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2577 a1fb16d8 2019-05-24 stsp if (err) {
2578 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2579 a1fb16d8 2019-05-24 stsp return err;
2580 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2581 a1fb16d8 2019-05-24 stsp }
2582 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2583 a1fb16d8 2019-05-24 stsp return NULL;
2584 a1fb16d8 2019-05-24 stsp
2585 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2586 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2587 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2588 a1fb16d8 2019-05-24 stsp if (err)
2589 a1fb16d8 2019-05-24 stsp return err;
2590 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2591 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2592 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2593 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2594 0ebf8283 2019-07-24 stsp return NULL;
2595 0ebf8283 2019-07-24 stsp }
2596 0ebf8283 2019-07-24 stsp
2597 0ebf8283 2019-07-24 stsp static const struct got_error *
2598 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2599 0ebf8283 2019-07-24 stsp {
2600 0ebf8283 2019-07-24 stsp const struct got_error *err;
2601 0ebf8283 2019-07-24 stsp int in_progress;
2602 0ebf8283 2019-07-24 stsp
2603 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2604 0ebf8283 2019-07-24 stsp if (err)
2605 0ebf8283 2019-07-24 stsp return err;
2606 0ebf8283 2019-07-24 stsp if (in_progress)
2607 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2608 0ebf8283 2019-07-24 stsp
2609 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2610 0ebf8283 2019-07-24 stsp if (err)
2611 0ebf8283 2019-07-24 stsp return err;
2612 0ebf8283 2019-07-24 stsp if (in_progress)
2613 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2614 0ebf8283 2019-07-24 stsp
2615 a1fb16d8 2019-05-24 stsp return NULL;
2616 a5edda0a 2019-07-27 stsp }
2617 a5edda0a 2019-07-27 stsp
2618 a5edda0a 2019-07-27 stsp static const struct got_error *
2619 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2620 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2621 a5edda0a 2019-07-27 stsp {
2622 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2623 a5edda0a 2019-07-27 stsp char *path;
2624 a5edda0a 2019-07-27 stsp int i;
2625 a5edda0a 2019-07-27 stsp
2626 a5edda0a 2019-07-27 stsp if (argc == 0) {
2627 a5edda0a 2019-07-27 stsp path = strdup("");
2628 a5edda0a 2019-07-27 stsp if (path == NULL)
2629 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2630 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2631 a5edda0a 2019-07-27 stsp }
2632 a5edda0a 2019-07-27 stsp
2633 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2634 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2635 a5edda0a 2019-07-27 stsp if (err)
2636 a5edda0a 2019-07-27 stsp break;
2637 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2638 a5edda0a 2019-07-27 stsp if (err) {
2639 a5edda0a 2019-07-27 stsp free(path);
2640 a5edda0a 2019-07-27 stsp break;
2641 a5edda0a 2019-07-27 stsp }
2642 a5edda0a 2019-07-27 stsp }
2643 a5edda0a 2019-07-27 stsp
2644 a5edda0a 2019-07-27 stsp return err;
2645 a1fb16d8 2019-05-24 stsp }
2646 a1fb16d8 2019-05-24 stsp
2647 a1fb16d8 2019-05-24 stsp static const struct got_error *
2648 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2649 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2650 fa51e947 2020-03-27 stsp {
2651 fa51e947 2020-03-27 stsp const struct got_error *err;
2652 fa51e947 2020-03-27 stsp struct got_repository *repo;
2653 fa51e947 2020-03-27 stsp static char msg[512];
2654 fa51e947 2020-03-27 stsp
2655 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2656 fa51e947 2020-03-27 stsp if (err)
2657 fa51e947 2020-03-27 stsp return orig_err;
2658 fa51e947 2020-03-27 stsp
2659 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2660 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2661 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2662 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2663 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2664 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2665 fa51e947 2020-03-27 stsp got_repo_close(repo);
2666 fa51e947 2020-03-27 stsp return err;
2667 fa51e947 2020-03-27 stsp }
2668 fa51e947 2020-03-27 stsp
2669 fa51e947 2020-03-27 stsp static const struct got_error *
2670 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2671 507dc3bb 2018-12-29 stsp {
2672 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2673 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2674 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2675 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2676 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2677 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2678 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2679 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2680 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2681 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2682 9627c110 2020-04-18 stsp int ch;
2683 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2684 507dc3bb 2018-12-29 stsp
2685 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2686 f2ea84fa 2019-07-27 stsp
2687 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2688 507dc3bb 2018-12-29 stsp switch (ch) {
2689 024e9686 2019-05-14 stsp case 'b':
2690 024e9686 2019-05-14 stsp branch_name = optarg;
2691 024e9686 2019-05-14 stsp break;
2692 507dc3bb 2018-12-29 stsp case 'c':
2693 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2694 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2695 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2696 507dc3bb 2018-12-29 stsp break;
2697 507dc3bb 2018-12-29 stsp default:
2698 2deda0b9 2019-03-07 stsp usage_update();
2699 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2700 507dc3bb 2018-12-29 stsp }
2701 507dc3bb 2018-12-29 stsp }
2702 507dc3bb 2018-12-29 stsp
2703 507dc3bb 2018-12-29 stsp argc -= optind;
2704 507dc3bb 2018-12-29 stsp argv += optind;
2705 507dc3bb 2018-12-29 stsp
2706 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2707 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2708 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2709 507dc3bb 2018-12-29 stsp err(1, "pledge");
2710 507dc3bb 2018-12-29 stsp #endif
2711 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2712 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2713 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2714 c4cdcb68 2019-04-03 stsp goto done;
2715 c4cdcb68 2019-04-03 stsp }
2716 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2717 fa51e947 2020-03-27 stsp if (error) {
2718 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2719 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
2720 fa51e947 2020-03-27 stsp worktree_path);
2721 7d5807f4 2019-07-11 stsp goto done;
2722 fa51e947 2020-03-27 stsp }
2723 7d5807f4 2019-07-11 stsp
2724 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2725 f2ea84fa 2019-07-27 stsp if (error)
2726 f2ea84fa 2019-07-27 stsp goto done;
2727 507dc3bb 2018-12-29 stsp
2728 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2729 c9956ddf 2019-09-08 stsp NULL);
2730 507dc3bb 2018-12-29 stsp if (error != NULL)
2731 507dc3bb 2018-12-29 stsp goto done;
2732 507dc3bb 2018-12-29 stsp
2733 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2734 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2735 087fb88c 2019-08-04 stsp if (error)
2736 087fb88c 2019-08-04 stsp goto done;
2737 087fb88c 2019-08-04 stsp
2738 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2739 0266afb7 2019-01-04 stsp if (error)
2740 0266afb7 2019-01-04 stsp goto done;
2741 0266afb7 2019-01-04 stsp
2742 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2743 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2744 024e9686 2019-05-14 stsp if (error != NULL)
2745 024e9686 2019-05-14 stsp goto done;
2746 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2747 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2748 9c4b8182 2019-01-02 stsp if (error != NULL)
2749 9c4b8182 2019-01-02 stsp goto done;
2750 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2751 507dc3bb 2018-12-29 stsp if (error != NULL)
2752 507dc3bb 2018-12-29 stsp goto done;
2753 507dc3bb 2018-12-29 stsp } else {
2754 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2755 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2756 04f57cb3 2019-07-25 stsp free(commit_id_str);
2757 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2758 30837e32 2019-07-25 stsp if (error)
2759 a297e751 2019-07-11 stsp goto done;
2760 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2761 a297e751 2019-07-11 stsp if (error)
2762 507dc3bb 2018-12-29 stsp goto done;
2763 507dc3bb 2018-12-29 stsp }
2764 35c965b2 2018-12-29 stsp
2765 a1fb16d8 2019-05-24 stsp if (branch_name) {
2766 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2767 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2768 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2769 f2ea84fa 2019-07-27 stsp continue;
2770 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2771 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2772 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2773 024e9686 2019-05-14 stsp goto done;
2774 024e9686 2019-05-14 stsp }
2775 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2776 024e9686 2019-05-14 stsp if (error)
2777 024e9686 2019-05-14 stsp goto done;
2778 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2779 3aef623b 2019-10-15 stsp repo);
2780 a367ff0f 2019-05-14 stsp free(head_commit_id);
2781 024e9686 2019-05-14 stsp if (error != NULL)
2782 024e9686 2019-05-14 stsp goto done;
2783 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2784 a367ff0f 2019-05-14 stsp if (error)
2785 a367ff0f 2019-05-14 stsp goto done;
2786 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2787 024e9686 2019-05-14 stsp if (error)
2788 024e9686 2019-05-14 stsp goto done;
2789 024e9686 2019-05-14 stsp } else {
2790 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2791 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2792 a367ff0f 2019-05-14 stsp if (error != NULL) {
2793 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2794 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2795 024e9686 2019-05-14 stsp goto done;
2796 a367ff0f 2019-05-14 stsp }
2797 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2798 a367ff0f 2019-05-14 stsp if (error)
2799 a367ff0f 2019-05-14 stsp goto done;
2800 024e9686 2019-05-14 stsp }
2801 507dc3bb 2018-12-29 stsp
2802 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2803 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2804 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2805 507dc3bb 2018-12-29 stsp commit_id);
2806 507dc3bb 2018-12-29 stsp if (error)
2807 507dc3bb 2018-12-29 stsp goto done;
2808 507dc3bb 2018-12-29 stsp }
2809 507dc3bb 2018-12-29 stsp
2810 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
2811 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2812 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
2813 507dc3bb 2018-12-29 stsp if (error != NULL)
2814 507dc3bb 2018-12-29 stsp goto done;
2815 9c4b8182 2019-01-02 stsp
2816 9627c110 2020-04-18 stsp if (upa.did_something)
2817 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2818 784955db 2019-01-12 stsp else
2819 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2820 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
2821 507dc3bb 2018-12-29 stsp done:
2822 c09a553d 2018-03-12 stsp free(worktree_path);
2823 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2824 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2825 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2826 507dc3bb 2018-12-29 stsp free(commit_id);
2827 9c4b8182 2019-01-02 stsp free(commit_id_str);
2828 c09a553d 2018-03-12 stsp return error;
2829 c09a553d 2018-03-12 stsp }
2830 c09a553d 2018-03-12 stsp
2831 f42b1b34 2018-03-12 stsp static const struct got_error *
2832 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2833 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2834 63035f9f 2019-10-06 stsp struct got_repository *repo)
2835 5c860e29 2018-03-12 stsp {
2836 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2837 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2838 79109fed 2018-03-27 stsp
2839 44392932 2019-08-25 stsp if (blob_id1) {
2840 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2841 44392932 2019-08-25 stsp if (err)
2842 44392932 2019-08-25 stsp goto done;
2843 44392932 2019-08-25 stsp }
2844 79109fed 2018-03-27 stsp
2845 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2846 44392932 2019-08-25 stsp if (err)
2847 44392932 2019-08-25 stsp goto done;
2848 79109fed 2018-03-27 stsp
2849 44392932 2019-08-25 stsp while (path[0] == '/')
2850 44392932 2019-08-25 stsp path++;
2851 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2852 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2853 44392932 2019-08-25 stsp done:
2854 44392932 2019-08-25 stsp if (blob1)
2855 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2856 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2857 44392932 2019-08-25 stsp return err;
2858 44392932 2019-08-25 stsp }
2859 44392932 2019-08-25 stsp
2860 44392932 2019-08-25 stsp static const struct got_error *
2861 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2862 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2863 63035f9f 2019-10-06 stsp struct got_repository *repo)
2864 44392932 2019-08-25 stsp {
2865 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2866 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2867 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2868 44392932 2019-08-25 stsp
2869 44392932 2019-08-25 stsp if (tree_id1) {
2870 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2871 79109fed 2018-03-27 stsp if (err)
2872 44392932 2019-08-25 stsp goto done;
2873 79109fed 2018-03-27 stsp }
2874 79109fed 2018-03-27 stsp
2875 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2876 0f2b3dca 2018-12-22 stsp if (err)
2877 0f2b3dca 2018-12-22 stsp goto done;
2878 0f2b3dca 2018-12-22 stsp
2879 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2880 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2881 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2882 44392932 2019-08-25 stsp while (path[0] == '/')
2883 44392932 2019-08-25 stsp path++;
2884 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2885 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2886 0f2b3dca 2018-12-22 stsp done:
2887 79109fed 2018-03-27 stsp if (tree1)
2888 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2889 366e0a5f 2019-10-10 stsp if (tree2)
2890 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2891 44392932 2019-08-25 stsp return err;
2892 44392932 2019-08-25 stsp }
2893 44392932 2019-08-25 stsp
2894 44392932 2019-08-25 stsp static const struct got_error *
2895 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2896 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2897 44392932 2019-08-25 stsp {
2898 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2899 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2900 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2901 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2902 44392932 2019-08-25 stsp struct got_object_qid *qid;
2903 44392932 2019-08-25 stsp
2904 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2905 44392932 2019-08-25 stsp if (qid != NULL) {
2906 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2907 44392932 2019-08-25 stsp qid->id);
2908 44392932 2019-08-25 stsp if (err)
2909 44392932 2019-08-25 stsp return err;
2910 44392932 2019-08-25 stsp }
2911 44392932 2019-08-25 stsp
2912 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2913 44392932 2019-08-25 stsp int obj_type;
2914 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2915 44392932 2019-08-25 stsp if (err)
2916 44392932 2019-08-25 stsp goto done;
2917 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2918 44392932 2019-08-25 stsp if (err) {
2919 44392932 2019-08-25 stsp free(obj_id2);
2920 44392932 2019-08-25 stsp goto done;
2921 44392932 2019-08-25 stsp }
2922 44392932 2019-08-25 stsp if (pcommit) {
2923 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2924 44392932 2019-08-25 stsp qid->id, path);
2925 44392932 2019-08-25 stsp if (err) {
2926 44392932 2019-08-25 stsp free(obj_id2);
2927 44392932 2019-08-25 stsp goto done;
2928 44392932 2019-08-25 stsp }
2929 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2930 44392932 2019-08-25 stsp if (err) {
2931 44392932 2019-08-25 stsp free(obj_id2);
2932 44392932 2019-08-25 stsp goto done;
2933 44392932 2019-08-25 stsp }
2934 44392932 2019-08-25 stsp }
2935 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2936 44392932 2019-08-25 stsp if (err) {
2937 44392932 2019-08-25 stsp free(obj_id2);
2938 44392932 2019-08-25 stsp goto done;
2939 44392932 2019-08-25 stsp }
2940 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2941 44392932 2019-08-25 stsp switch (obj_type) {
2942 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2943 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2944 63035f9f 2019-10-06 stsp 0, repo);
2945 44392932 2019-08-25 stsp break;
2946 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2947 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2948 63035f9f 2019-10-06 stsp 0, repo);
2949 44392932 2019-08-25 stsp break;
2950 44392932 2019-08-25 stsp default:
2951 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2952 44392932 2019-08-25 stsp break;
2953 44392932 2019-08-25 stsp }
2954 44392932 2019-08-25 stsp free(obj_id1);
2955 44392932 2019-08-25 stsp free(obj_id2);
2956 44392932 2019-08-25 stsp } else {
2957 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2958 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2959 44392932 2019-08-25 stsp if (err)
2960 44392932 2019-08-25 stsp goto done;
2961 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2962 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2963 44392932 2019-08-25 stsp if (err)
2964 44392932 2019-08-25 stsp goto done;
2965 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2966 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2967 44392932 2019-08-25 stsp }
2968 44392932 2019-08-25 stsp done:
2969 0f2b3dca 2018-12-22 stsp free(id_str1);
2970 0f2b3dca 2018-12-22 stsp free(id_str2);
2971 44392932 2019-08-25 stsp if (pcommit)
2972 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2973 79109fed 2018-03-27 stsp return err;
2974 79109fed 2018-03-27 stsp }
2975 79109fed 2018-03-27 stsp
2976 4bb494d5 2018-06-16 stsp static char *
2977 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2978 6c281f94 2018-06-11 stsp {
2979 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2980 09867e48 2019-08-13 stsp char *p, *s;
2981 09867e48 2019-08-13 stsp
2982 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2983 09867e48 2019-08-13 stsp if (tm == NULL)
2984 09867e48 2019-08-13 stsp return NULL;
2985 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2986 09867e48 2019-08-13 stsp if (s == NULL)
2987 09867e48 2019-08-13 stsp return NULL;
2988 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2989 6c281f94 2018-06-11 stsp if (p)
2990 6c281f94 2018-06-11 stsp *p = '\0';
2991 6c281f94 2018-06-11 stsp return s;
2992 6c281f94 2018-06-11 stsp }
2993 dc424a06 2019-08-07 stsp
2994 6841bf13 2019-11-29 kn static const struct got_error *
2995 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2996 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2997 6841bf13 2019-11-29 kn {
2998 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2999 6841bf13 2019-11-29 kn regmatch_t regmatch;
3000 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3001 6841bf13 2019-11-29 kn
3002 6841bf13 2019-11-29 kn *have_match = 0;
3003 6841bf13 2019-11-29 kn
3004 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3005 6841bf13 2019-11-29 kn if (err)
3006 6841bf13 2019-11-29 kn return err;
3007 6841bf13 2019-11-29 kn
3008 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3009 6841bf13 2019-11-29 kn if (err)
3010 6841bf13 2019-11-29 kn goto done;
3011 6841bf13 2019-11-29 kn
3012 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3013 6841bf13 2019-11-29 kn *have_match = 1;
3014 6841bf13 2019-11-29 kn done:
3015 6841bf13 2019-11-29 kn free(id_str);
3016 6841bf13 2019-11-29 kn free(logmsg);
3017 6841bf13 2019-11-29 kn return err;
3018 6841bf13 2019-11-29 kn }
3019 6841bf13 2019-11-29 kn
3020 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3021 6c281f94 2018-06-11 stsp
3022 79109fed 2018-03-27 stsp static const struct got_error *
3023 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3024 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
3025 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3026 79109fed 2018-03-27 stsp {
3027 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3028 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3029 6c281f94 2018-06-11 stsp char datebuf[26];
3030 45d799e2 2018-12-23 stsp time_t committer_time;
3031 45d799e2 2018-12-23 stsp const char *author, *committer;
3032 199a4027 2019-02-02 stsp char *refs_str = NULL;
3033 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3034 5c860e29 2018-03-12 stsp
3035 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3036 199a4027 2019-02-02 stsp char *s;
3037 199a4027 2019-02-02 stsp const char *name;
3038 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3039 a436ad14 2019-08-13 stsp int cmp;
3040 a436ad14 2019-08-13 stsp
3041 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3042 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3043 d9498b20 2019-02-05 stsp continue;
3044 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3045 199a4027 2019-02-02 stsp name += 5;
3046 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3047 7143d404 2019-03-12 stsp continue;
3048 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3049 e34f9ed6 2019-02-02 stsp name += 6;
3050 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
3051 141c2bff 2019-02-04 stsp name += 8;
3052 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3053 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
3054 5d844a1e 2019-08-13 stsp if (err) {
3055 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
3056 5d844a1e 2019-08-13 stsp return err;
3057 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3058 5d844a1e 2019-08-13 stsp err = NULL;
3059 5d844a1e 2019-08-13 stsp tag = NULL;
3060 5d844a1e 2019-08-13 stsp }
3061 a436ad14 2019-08-13 stsp }
3062 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3063 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
3064 a436ad14 2019-08-13 stsp if (tag)
3065 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3066 a436ad14 2019-08-13 stsp if (cmp != 0)
3067 a436ad14 2019-08-13 stsp continue;
3068 199a4027 2019-02-02 stsp s = refs_str;
3069 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3070 199a4027 2019-02-02 stsp name) == -1) {
3071 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3072 199a4027 2019-02-02 stsp free(s);
3073 34ca4898 2019-08-13 stsp return err;
3074 199a4027 2019-02-02 stsp }
3075 199a4027 2019-02-02 stsp free(s);
3076 199a4027 2019-02-02 stsp }
3077 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3078 8bf5b3c9 2018-03-17 stsp if (err)
3079 8bf5b3c9 2018-03-17 stsp return err;
3080 788c352e 2018-06-16 stsp
3081 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3082 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3083 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3084 832c249c 2018-06-10 stsp free(id_str);
3085 d3d493d7 2019-02-21 stsp id_str = NULL;
3086 d3d493d7 2019-02-21 stsp free(refs_str);
3087 d3d493d7 2019-02-21 stsp refs_str = NULL;
3088 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3089 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3090 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3091 09867e48 2019-08-13 stsp if (datestr)
3092 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3093 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3094 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3095 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3096 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3097 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3098 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3099 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3100 3fe1abad 2018-06-10 stsp int n = 1;
3101 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3102 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3103 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3104 a0603db2 2018-06-10 stsp if (err)
3105 a0603db2 2018-06-10 stsp return err;
3106 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3107 a0603db2 2018-06-10 stsp free(id_str);
3108 a0603db2 2018-06-10 stsp }
3109 a0603db2 2018-06-10 stsp }
3110 832c249c 2018-06-10 stsp
3111 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3112 5943eee2 2019-08-13 stsp if (err)
3113 5943eee2 2019-08-13 stsp return err;
3114 8bf5b3c9 2018-03-17 stsp
3115 621015ac 2018-07-23 stsp logmsg = logmsg0;
3116 832c249c 2018-06-10 stsp do {
3117 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3118 832c249c 2018-06-10 stsp if (line)
3119 832c249c 2018-06-10 stsp printf(" %s\n", line);
3120 832c249c 2018-06-10 stsp } while (line);
3121 621015ac 2018-07-23 stsp free(logmsg0);
3122 832c249c 2018-06-10 stsp
3123 971751ac 2018-03-27 stsp if (show_patch) {
3124 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3125 971751ac 2018-03-27 stsp if (err == 0)
3126 971751ac 2018-03-27 stsp printf("\n");
3127 971751ac 2018-03-27 stsp }
3128 07862c20 2018-09-15 stsp
3129 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3130 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3131 07862c20 2018-09-15 stsp return err;
3132 f42b1b34 2018-03-12 stsp }
3133 5c860e29 2018-03-12 stsp
3134 f42b1b34 2018-03-12 stsp static const struct got_error *
3135 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3136 d1fe46f9 2020-04-18 stsp struct got_repository *repo, const char *path, int show_patch,
3137 d1fe46f9 2020-04-18 stsp const char *search_pattern, int diff_context, int limit, int log_branches,
3138 dbec59df 2020-04-18 stsp int reverse_display_order, struct got_reflist_head *refs)
3139 f42b1b34 2018-03-12 stsp {
3140 f42b1b34 2018-03-12 stsp const struct got_error *err;
3141 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3142 6841bf13 2019-11-29 kn regex_t regex;
3143 6841bf13 2019-11-29 kn int have_match;
3144 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3145 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3146 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3147 dbec59df 2020-04-18 stsp
3148 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3149 372ccdbb 2018-06-10 stsp
3150 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3151 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3152 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3153 6841bf13 2019-11-29 kn
3154 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3155 f42b1b34 2018-03-12 stsp if (err)
3156 f42b1b34 2018-03-12 stsp return err;
3157 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3158 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3159 372ccdbb 2018-06-10 stsp if (err)
3160 fcc85cad 2018-07-22 stsp goto done;
3161 656b1f76 2019-05-11 jcs for (;;) {
3162 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3163 8bf5b3c9 2018-03-17 stsp
3164 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3165 84453469 2018-11-11 stsp break;
3166 84453469 2018-11-11 stsp
3167 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3168 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3169 372ccdbb 2018-06-10 stsp if (err) {
3170 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3171 9ba79e04 2018-06-11 stsp err = NULL;
3172 ee780d5c 2020-01-04 stsp break;
3173 7e665116 2018-04-02 stsp }
3174 b43fbaa0 2018-06-11 stsp if (id == NULL)
3175 7e665116 2018-04-02 stsp break;
3176 b43fbaa0 2018-06-11 stsp
3177 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3178 b43fbaa0 2018-06-11 stsp if (err)
3179 fcc85cad 2018-07-22 stsp break;
3180 6841bf13 2019-11-29 kn
3181 6841bf13 2019-11-29 kn if (search_pattern) {
3182 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3183 6841bf13 2019-11-29 kn if (err) {
3184 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3185 6841bf13 2019-11-29 kn break;
3186 6841bf13 2019-11-29 kn }
3187 6841bf13 2019-11-29 kn if (have_match == 0) {
3188 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3189 6841bf13 2019-11-29 kn continue;
3190 6841bf13 2019-11-29 kn }
3191 6841bf13 2019-11-29 kn }
3192 6841bf13 2019-11-29 kn
3193 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3194 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3195 dbec59df 2020-04-18 stsp if (err)
3196 dbec59df 2020-04-18 stsp break;
3197 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3198 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3199 dbec59df 2020-04-18 stsp } else {
3200 dbec59df 2020-04-18 stsp err = print_commit(commit, id, repo, path, show_patch,
3201 dbec59df 2020-04-18 stsp diff_context, refs);
3202 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3203 dbec59df 2020-04-18 stsp if (err)
3204 dbec59df 2020-04-18 stsp break;
3205 dbec59df 2020-04-18 stsp }
3206 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3207 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3208 7e665116 2018-04-02 stsp break;
3209 31cedeaf 2018-09-15 stsp }
3210 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3211 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3212 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3213 dbec59df 2020-04-18 stsp if (err)
3214 dbec59df 2020-04-18 stsp break;
3215 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3216 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3217 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3218 dbec59df 2020-04-18 stsp if (err)
3219 dbec59df 2020-04-18 stsp break;
3220 dbec59df 2020-04-18 stsp }
3221 dbec59df 2020-04-18 stsp }
3222 fcc85cad 2018-07-22 stsp done:
3223 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3224 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3225 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3226 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3227 dbec59df 2020-04-18 stsp }
3228 6841bf13 2019-11-29 kn if (search_pattern)
3229 6841bf13 2019-11-29 kn regfree(&regex);
3230 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3231 f42b1b34 2018-03-12 stsp return err;
3232 f42b1b34 2018-03-12 stsp }
3233 5c860e29 2018-03-12 stsp
3234 4ed7e80c 2018-05-20 stsp __dead static void
3235 6f3d1eb0 2018-03-12 stsp usage_log(void)
3236 6f3d1eb0 2018-03-12 stsp {
3237 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3238 dbec59df 2020-04-18 stsp "[-p] [-x commit] [-s search-pattern] [-r repository-path] [-R] "
3239 d1fe46f9 2020-04-18 stsp "[path]\n", getprogname());
3240 6f3d1eb0 2018-03-12 stsp exit(1);
3241 b1ebc001 2019-08-13 stsp }
3242 b1ebc001 2019-08-13 stsp
3243 b1ebc001 2019-08-13 stsp static int
3244 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3245 b1ebc001 2019-08-13 stsp {
3246 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3247 b1ebc001 2019-08-13 stsp long long n;
3248 b1ebc001 2019-08-13 stsp const char *errstr;
3249 b1ebc001 2019-08-13 stsp
3250 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3251 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3252 b1ebc001 2019-08-13 stsp return 0;
3253 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3254 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3255 b1ebc001 2019-08-13 stsp return 0;
3256 b1ebc001 2019-08-13 stsp return n;
3257 d1fe46f9 2020-04-18 stsp }
3258 d1fe46f9 2020-04-18 stsp
3259 d1fe46f9 2020-04-18 stsp static const struct got_error *
3260 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3261 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3262 d1fe46f9 2020-04-18 stsp {
3263 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3264 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3265 d1fe46f9 2020-04-18 stsp
3266 d1fe46f9 2020-04-18 stsp *id = NULL;
3267 d1fe46f9 2020-04-18 stsp
3268 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3269 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3270 d1fe46f9 2020-04-18 stsp int obj_type;
3271 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3272 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3273 d1fe46f9 2020-04-18 stsp if (err)
3274 d1fe46f9 2020-04-18 stsp return err;
3275 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3276 d1fe46f9 2020-04-18 stsp if (err)
3277 d1fe46f9 2020-04-18 stsp return err;
3278 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3279 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3280 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3281 d1fe46f9 2020-04-18 stsp if (err)
3282 d1fe46f9 2020-04-18 stsp return err;
3283 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3284 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3285 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3286 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3287 d1fe46f9 2020-04-18 stsp }
3288 d1fe46f9 2020-04-18 stsp free(*id);
3289 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3290 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3291 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3292 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3293 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3294 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3295 d1fe46f9 2020-04-18 stsp if (err)
3296 d1fe46f9 2020-04-18 stsp return err;
3297 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3298 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3299 d1fe46f9 2020-04-18 stsp } else {
3300 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3301 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3302 d1fe46f9 2020-04-18 stsp }
3303 d1fe46f9 2020-04-18 stsp
3304 d1fe46f9 2020-04-18 stsp return err;
3305 6f3d1eb0 2018-03-12 stsp }
3306 6f3d1eb0 2018-03-12 stsp
3307 4ed7e80c 2018-05-20 stsp static const struct got_error *
3308 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3309 f42b1b34 2018-03-12 stsp {
3310 f42b1b34 2018-03-12 stsp const struct got_error *error;
3311 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3312 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3313 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3314 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3315 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3316 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3317 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3318 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
3319 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3320 64a96a6d 2018-04-01 stsp const char *errstr;
3321 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3322 1b3893a2 2019-03-18 stsp
3323 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3324 5c860e29 2018-03-12 stsp
3325 6715a751 2018-03-16 stsp #ifndef PROFILE
3326 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3327 6098196c 2019-01-04 stsp NULL)
3328 ad242220 2018-09-08 stsp == -1)
3329 f42b1b34 2018-03-12 stsp err(1, "pledge");
3330 6715a751 2018-03-16 stsp #endif
3331 79109fed 2018-03-27 stsp
3332 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3333 b1ebc001 2019-08-13 stsp
3334 dbec59df 2020-04-18 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:Rs:x:")) != -1) {
3335 79109fed 2018-03-27 stsp switch (ch) {
3336 79109fed 2018-03-27 stsp case 'p':
3337 79109fed 2018-03-27 stsp show_patch = 1;
3338 d142fc45 2018-04-01 stsp break;
3339 d142fc45 2018-04-01 stsp case 'c':
3340 d142fc45 2018-04-01 stsp start_commit = optarg;
3341 64a96a6d 2018-04-01 stsp break;
3342 c0cc5c62 2018-10-18 stsp case 'C':
3343 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3344 4a8520aa 2018-10-18 stsp &errstr);
3345 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3346 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3347 c0cc5c62 2018-10-18 stsp break;
3348 64a96a6d 2018-04-01 stsp case 'l':
3349 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3350 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3351 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3352 cc54c501 2019-07-15 stsp break;
3353 48c8c60d 2020-01-27 stsp case 'b':
3354 48c8c60d 2020-01-27 stsp log_branches = 1;
3355 a0603db2 2018-06-10 stsp break;
3356 04ca23f4 2018-07-16 stsp case 'r':
3357 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3358 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3359 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3360 9ba1d308 2019-10-21 stsp optarg);
3361 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3362 04ca23f4 2018-07-16 stsp break;
3363 dbec59df 2020-04-18 stsp case 'R':
3364 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3365 dbec59df 2020-04-18 stsp break;
3366 6841bf13 2019-11-29 kn case 's':
3367 6841bf13 2019-11-29 kn search_pattern = optarg;
3368 6841bf13 2019-11-29 kn break;
3369 d1fe46f9 2020-04-18 stsp case 'x':
3370 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3371 d1fe46f9 2020-04-18 stsp break;
3372 79109fed 2018-03-27 stsp default:
3373 2deda0b9 2019-03-07 stsp usage_log();
3374 79109fed 2018-03-27 stsp /* NOTREACHED */
3375 79109fed 2018-03-27 stsp }
3376 79109fed 2018-03-27 stsp }
3377 79109fed 2018-03-27 stsp
3378 79109fed 2018-03-27 stsp argc -= optind;
3379 79109fed 2018-03-27 stsp argv += optind;
3380 f42b1b34 2018-03-12 stsp
3381 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3382 dc1edbfa 2019-11-29 kn diff_context = 3;
3383 dc1edbfa 2019-11-29 kn else if (!show_patch)
3384 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3385 dc1edbfa 2019-11-29 kn
3386 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3387 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3388 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3389 04ca23f4 2018-07-16 stsp goto done;
3390 04ca23f4 2018-07-16 stsp }
3391 cffc0aa4 2019-02-05 stsp
3392 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3393 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3394 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3395 50f2fada 2020-04-24 stsp goto done;
3396 50f2fada 2020-04-24 stsp error = NULL;
3397 50f2fada 2020-04-24 stsp }
3398 cffc0aa4 2019-02-05 stsp
3399 e7301579 2019-03-18 stsp if (argc == 0) {
3400 cbd1af7a 2019-03-18 stsp path = strdup("");
3401 e7301579 2019-03-18 stsp if (path == NULL) {
3402 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3403 cbd1af7a 2019-03-18 stsp goto done;
3404 e7301579 2019-03-18 stsp }
3405 e7301579 2019-03-18 stsp } else if (argc == 1) {
3406 e7301579 2019-03-18 stsp if (worktree) {
3407 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3408 e7301579 2019-03-18 stsp argv[0]);
3409 e7301579 2019-03-18 stsp if (error)
3410 e7301579 2019-03-18 stsp goto done;
3411 e7301579 2019-03-18 stsp } else {
3412 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3413 e7301579 2019-03-18 stsp if (path == NULL) {
3414 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3415 e7301579 2019-03-18 stsp goto done;
3416 e7301579 2019-03-18 stsp }
3417 e7301579 2019-03-18 stsp }
3418 cbd1af7a 2019-03-18 stsp } else
3419 cbd1af7a 2019-03-18 stsp usage_log();
3420 cbd1af7a 2019-03-18 stsp
3421 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3422 5486daa2 2019-05-11 stsp repo_path = worktree ?
3423 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3424 5486daa2 2019-05-11 stsp }
3425 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3426 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3427 cffc0aa4 2019-02-05 stsp goto done;
3428 04ca23f4 2018-07-16 stsp }
3429 6098196c 2019-01-04 stsp
3430 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3431 d7d4f210 2018-03-12 stsp if (error != NULL)
3432 04ca23f4 2018-07-16 stsp goto done;
3433 f42b1b34 2018-03-12 stsp
3434 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3435 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3436 c02c541e 2019-03-29 stsp if (error)
3437 c02c541e 2019-03-29 stsp goto done;
3438 c02c541e 2019-03-29 stsp
3439 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3440 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3441 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3442 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3443 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3444 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3445 3235492e 2018-04-01 stsp if (error != NULL)
3446 d1fe46f9 2020-04-18 stsp goto done;
3447 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3448 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3449 3235492e 2018-04-01 stsp if (error != NULL)
3450 d1fe46f9 2020-04-18 stsp goto done;
3451 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3452 d1fe46f9 2020-04-18 stsp start_id);
3453 d1fe46f9 2020-04-18 stsp if (error != NULL)
3454 d1fe46f9 2020-04-18 stsp goto done;
3455 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3456 3235492e 2018-04-01 stsp } else {
3457 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3458 d1fe46f9 2020-04-18 stsp if (error != NULL)
3459 d1fe46f9 2020-04-18 stsp goto done;
3460 3235492e 2018-04-01 stsp }
3461 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3462 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3463 d1fe46f9 2020-04-18 stsp if (error != NULL)
3464 d1fe46f9 2020-04-18 stsp goto done;
3465 d1fe46f9 2020-04-18 stsp }
3466 04ca23f4 2018-07-16 stsp
3467 deeabeae 2019-08-27 stsp if (worktree) {
3468 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3469 deeabeae 2019-08-27 stsp char *p;
3470 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3471 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3472 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3473 deeabeae 2019-08-27 stsp goto done;
3474 deeabeae 2019-08-27 stsp }
3475 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3476 deeabeae 2019-08-27 stsp free(p);
3477 deeabeae 2019-08-27 stsp } else
3478 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3479 04ca23f4 2018-07-16 stsp if (error != NULL)
3480 04ca23f4 2018-07-16 stsp goto done;
3481 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3482 04ca23f4 2018-07-16 stsp free(path);
3483 04ca23f4 2018-07-16 stsp path = in_repo_path;
3484 04ca23f4 2018-07-16 stsp }
3485 199a4027 2019-02-02 stsp
3486 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3487 199a4027 2019-02-02 stsp if (error)
3488 199a4027 2019-02-02 stsp goto done;
3489 04ca23f4 2018-07-16 stsp
3490 d1fe46f9 2020-04-18 stsp error = print_commits(start_id, end_id, repo, path, show_patch,
3491 dbec59df 2020-04-18 stsp search_pattern, diff_context, limit, log_branches,
3492 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3493 04ca23f4 2018-07-16 stsp done:
3494 04ca23f4 2018-07-16 stsp free(path);
3495 04ca23f4 2018-07-16 stsp free(repo_path);
3496 04ca23f4 2018-07-16 stsp free(cwd);
3497 cffc0aa4 2019-02-05 stsp if (worktree)
3498 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3499 ad242220 2018-09-08 stsp if (repo) {
3500 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3501 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3502 ad242220 2018-09-08 stsp if (error == NULL)
3503 ad242220 2018-09-08 stsp error = repo_error;
3504 ad242220 2018-09-08 stsp }
3505 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3506 8bf5b3c9 2018-03-17 stsp return error;
3507 5c860e29 2018-03-12 stsp }
3508 5c860e29 2018-03-12 stsp
3509 4ed7e80c 2018-05-20 stsp __dead static void
3510 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3511 3f8b7d6a 2018-04-01 stsp {
3512 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3513 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3514 3f8b7d6a 2018-04-01 stsp exit(1);
3515 b00d56cd 2018-04-01 stsp }
3516 b00d56cd 2018-04-01 stsp
3517 b72f483a 2019-02-05 stsp struct print_diff_arg {
3518 b72f483a 2019-02-05 stsp struct got_repository *repo;
3519 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3520 b72f483a 2019-02-05 stsp int diff_context;
3521 3fc0c068 2019-02-10 stsp const char *id_str;
3522 3fc0c068 2019-02-10 stsp int header_shown;
3523 98eaaa12 2019-08-03 stsp int diff_staged;
3524 63035f9f 2019-10-06 stsp int ignore_whitespace;
3525 b72f483a 2019-02-05 stsp };
3526 b72f483a 2019-02-05 stsp
3527 4ed7e80c 2018-05-20 stsp static const struct got_error *
3528 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3529 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3530 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3531 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3532 b72f483a 2019-02-05 stsp {
3533 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3534 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3535 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3536 12463d8b 2019-12-13 stsp int fd = -1;
3537 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3538 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3539 b72f483a 2019-02-05 stsp struct stat sb;
3540 b72f483a 2019-02-05 stsp
3541 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3542 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3543 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3544 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3545 98eaaa12 2019-08-03 stsp return NULL;
3546 98eaaa12 2019-08-03 stsp } else {
3547 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3548 98eaaa12 2019-08-03 stsp return NULL;
3549 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3550 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3551 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3552 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3553 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3554 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3555 98eaaa12 2019-08-03 stsp return NULL;
3556 98eaaa12 2019-08-03 stsp }
3557 3fc0c068 2019-02-10 stsp
3558 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3559 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3560 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3561 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3562 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3563 3fc0c068 2019-02-10 stsp }
3564 b72f483a 2019-02-05 stsp
3565 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3566 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3567 98eaaa12 2019-08-03 stsp switch (staged_status) {
3568 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3569 98eaaa12 2019-08-03 stsp label1 = path;
3570 98eaaa12 2019-08-03 stsp label2 = path;
3571 98eaaa12 2019-08-03 stsp break;
3572 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3573 98eaaa12 2019-08-03 stsp label2 = path;
3574 98eaaa12 2019-08-03 stsp break;
3575 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3576 98eaaa12 2019-08-03 stsp label1 = path;
3577 98eaaa12 2019-08-03 stsp break;
3578 98eaaa12 2019-08-03 stsp default:
3579 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3580 98eaaa12 2019-08-03 stsp }
3581 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3582 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3583 63035f9f 2019-10-06 stsp a->repo, stdout);
3584 98eaaa12 2019-08-03 stsp }
3585 98eaaa12 2019-08-03 stsp
3586 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3587 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3588 4ce46740 2019-08-08 stsp char *id_str;
3589 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3590 408b4ebc 2019-08-03 stsp 8192);
3591 4ce46740 2019-08-08 stsp if (err)
3592 4ce46740 2019-08-08 stsp goto done;
3593 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3594 4ce46740 2019-08-08 stsp if (err)
3595 4ce46740 2019-08-08 stsp goto done;
3596 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3597 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3598 4ce46740 2019-08-08 stsp free(id_str);
3599 4ce46740 2019-08-08 stsp goto done;
3600 4ce46740 2019-08-08 stsp }
3601 4ce46740 2019-08-08 stsp free(id_str);
3602 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3603 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3604 4ce46740 2019-08-08 stsp if (err)
3605 4ce46740 2019-08-08 stsp goto done;
3606 4ce46740 2019-08-08 stsp }
3607 d00136be 2019-03-26 stsp
3608 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3609 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3610 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3611 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3612 2ec1f75b 2019-03-26 stsp goto done;
3613 2ec1f75b 2019-03-26 stsp }
3614 b72f483a 2019-02-05 stsp
3615 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3616 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3617 12463d8b 2019-12-13 stsp if (fd == -1) {
3618 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
3619 12463d8b 2019-12-13 stsp goto done;
3620 12463d8b 2019-12-13 stsp }
3621 12463d8b 2019-12-13 stsp } else {
3622 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3623 12463d8b 2019-12-13 stsp if (fd == -1) {
3624 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
3625 12463d8b 2019-12-13 stsp goto done;
3626 12463d8b 2019-12-13 stsp }
3627 12463d8b 2019-12-13 stsp }
3628 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3629 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3630 2ec1f75b 2019-03-26 stsp goto done;
3631 2ec1f75b 2019-03-26 stsp }
3632 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3633 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3634 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3635 2ec1f75b 2019-03-26 stsp goto done;
3636 2ec1f75b 2019-03-26 stsp }
3637 12463d8b 2019-12-13 stsp fd = -1;
3638 2ec1f75b 2019-03-26 stsp } else
3639 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3640 b72f483a 2019-02-05 stsp
3641 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3642 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3643 b72f483a 2019-02-05 stsp done:
3644 b72f483a 2019-02-05 stsp if (blob1)
3645 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3646 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3647 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3648 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3649 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3650 b72f483a 2019-02-05 stsp free(abspath);
3651 927df6b7 2019-02-10 stsp return err;
3652 927df6b7 2019-02-10 stsp }
3653 927df6b7 2019-02-10 stsp
3654 927df6b7 2019-02-10 stsp static const struct got_error *
3655 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3656 b00d56cd 2018-04-01 stsp {
3657 b00d56cd 2018-04-01 stsp const struct got_error *error;
3658 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3659 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3660 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3661 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3662 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3663 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3664 15a94983 2018-12-23 stsp int type1, type2;
3665 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3666 c0cc5c62 2018-10-18 stsp const char *errstr;
3667 927df6b7 2019-02-10 stsp char *path = NULL;
3668 b00d56cd 2018-04-01 stsp
3669 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3670 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3671 25eccc22 2019-01-04 stsp NULL) == -1)
3672 b00d56cd 2018-04-01 stsp err(1, "pledge");
3673 b00d56cd 2018-04-01 stsp #endif
3674 b00d56cd 2018-04-01 stsp
3675 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3676 b00d56cd 2018-04-01 stsp switch (ch) {
3677 c0cc5c62 2018-10-18 stsp case 'C':
3678 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3679 dfcab68b 2019-11-29 kn &errstr);
3680 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3681 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3682 c0cc5c62 2018-10-18 stsp break;
3683 b72f483a 2019-02-05 stsp case 'r':
3684 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3685 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3686 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3687 9ba1d308 2019-10-21 stsp optarg);
3688 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3689 b72f483a 2019-02-05 stsp break;
3690 98eaaa12 2019-08-03 stsp case 's':
3691 98eaaa12 2019-08-03 stsp diff_staged = 1;
3692 63035f9f 2019-10-06 stsp break;
3693 63035f9f 2019-10-06 stsp case 'w':
3694 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3695 98eaaa12 2019-08-03 stsp break;
3696 b00d56cd 2018-04-01 stsp default:
3697 2deda0b9 2019-03-07 stsp usage_diff();
3698 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3699 b00d56cd 2018-04-01 stsp }
3700 b00d56cd 2018-04-01 stsp }
3701 b00d56cd 2018-04-01 stsp
3702 b00d56cd 2018-04-01 stsp argc -= optind;
3703 b00d56cd 2018-04-01 stsp argv += optind;
3704 b00d56cd 2018-04-01 stsp
3705 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3706 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3707 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3708 b72f483a 2019-02-05 stsp goto done;
3709 b72f483a 2019-02-05 stsp }
3710 927df6b7 2019-02-10 stsp if (argc <= 1) {
3711 b72f483a 2019-02-05 stsp if (repo_path)
3712 b72f483a 2019-02-05 stsp errx(1,
3713 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3714 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3715 fa51e947 2020-03-27 stsp if (error) {
3716 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3717 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
3718 fa51e947 2020-03-27 stsp cwd);
3719 1f03b8da 2020-03-20 stsp goto done;
3720 fa51e947 2020-03-27 stsp }
3721 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3722 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3723 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3724 927df6b7 2019-02-10 stsp goto done;
3725 927df6b7 2019-02-10 stsp }
3726 927df6b7 2019-02-10 stsp if (argc == 1) {
3727 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3728 cbd1af7a 2019-03-18 stsp argv[0]);
3729 927df6b7 2019-02-10 stsp if (error)
3730 927df6b7 2019-02-10 stsp goto done;
3731 927df6b7 2019-02-10 stsp } else {
3732 927df6b7 2019-02-10 stsp path = strdup("");
3733 927df6b7 2019-02-10 stsp if (path == NULL) {
3734 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3735 927df6b7 2019-02-10 stsp goto done;
3736 927df6b7 2019-02-10 stsp }
3737 927df6b7 2019-02-10 stsp }
3738 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3739 98eaaa12 2019-08-03 stsp if (diff_staged)
3740 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3741 98eaaa12 2019-08-03 stsp "objects in repository");
3742 15a94983 2018-12-23 stsp id_str1 = argv[0];
3743 15a94983 2018-12-23 stsp id_str2 = argv[1];
3744 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3745 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3746 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3747 30db809c 2019-06-05 stsp goto done;
3748 1f03b8da 2020-03-20 stsp if (worktree) {
3749 1f03b8da 2020-03-20 stsp repo_path = strdup(
3750 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3751 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3752 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3753 1f03b8da 2020-03-20 stsp goto done;
3754 1f03b8da 2020-03-20 stsp }
3755 1f03b8da 2020-03-20 stsp } else {
3756 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3757 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3758 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3759 1f03b8da 2020-03-20 stsp goto done;
3760 1f03b8da 2020-03-20 stsp }
3761 30db809c 2019-06-05 stsp }
3762 30db809c 2019-06-05 stsp }
3763 b00d56cd 2018-04-01 stsp } else
3764 b00d56cd 2018-04-01 stsp usage_diff();
3765 b00d56cd 2018-04-01 stsp
3766 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3767 76089277 2018-04-01 stsp free(repo_path);
3768 b00d56cd 2018-04-01 stsp if (error != NULL)
3769 b00d56cd 2018-04-01 stsp goto done;
3770 b00d56cd 2018-04-01 stsp
3771 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3772 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3773 c02c541e 2019-03-29 stsp if (error)
3774 c02c541e 2019-03-29 stsp goto done;
3775 c02c541e 2019-03-29 stsp
3776 30db809c 2019-06-05 stsp if (argc <= 1) {
3777 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3778 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3779 b72f483a 2019-02-05 stsp char *id_str;
3780 72ea6654 2019-07-27 stsp
3781 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3782 72ea6654 2019-07-27 stsp
3783 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3784 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3785 b72f483a 2019-02-05 stsp if (error)
3786 b72f483a 2019-02-05 stsp goto done;
3787 b72f483a 2019-02-05 stsp arg.repo = repo;
3788 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3789 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3790 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3791 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3792 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3793 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3794 b72f483a 2019-02-05 stsp
3795 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3796 72ea6654 2019-07-27 stsp if (error)
3797 72ea6654 2019-07-27 stsp goto done;
3798 72ea6654 2019-07-27 stsp
3799 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3800 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3801 b72f483a 2019-02-05 stsp free(id_str);
3802 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3803 b72f483a 2019-02-05 stsp goto done;
3804 b72f483a 2019-02-05 stsp }
3805 b72f483a 2019-02-05 stsp
3806 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3807 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3808 0adb7196 2019-08-11 stsp if (error)
3809 0adb7196 2019-08-11 stsp goto done;
3810 b00d56cd 2018-04-01 stsp
3811 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3812 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3813 0adb7196 2019-08-11 stsp if (error)
3814 0adb7196 2019-08-11 stsp goto done;
3815 b00d56cd 2018-04-01 stsp
3816 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3817 15a94983 2018-12-23 stsp if (error)
3818 15a94983 2018-12-23 stsp goto done;
3819 15a94983 2018-12-23 stsp
3820 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3821 15a94983 2018-12-23 stsp if (error)
3822 15a94983 2018-12-23 stsp goto done;
3823 15a94983 2018-12-23 stsp
3824 15a94983 2018-12-23 stsp if (type1 != type2) {
3825 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3826 b00d56cd 2018-04-01 stsp goto done;
3827 b00d56cd 2018-04-01 stsp }
3828 b00d56cd 2018-04-01 stsp
3829 15a94983 2018-12-23 stsp switch (type1) {
3830 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3831 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3832 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3833 b00d56cd 2018-04-01 stsp break;
3834 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3835 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3836 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3837 b00d56cd 2018-04-01 stsp break;
3838 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3839 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3840 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3841 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3842 b00d56cd 2018-04-01 stsp break;
3843 b00d56cd 2018-04-01 stsp default:
3844 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3845 b00d56cd 2018-04-01 stsp }
3846 b00d56cd 2018-04-01 stsp done:
3847 5e70831e 2019-05-28 stsp free(label1);
3848 5e70831e 2019-05-28 stsp free(label2);
3849 15a94983 2018-12-23 stsp free(id1);
3850 15a94983 2018-12-23 stsp free(id2);
3851 927df6b7 2019-02-10 stsp free(path);
3852 b72f483a 2019-02-05 stsp if (worktree)
3853 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3854 ad242220 2018-09-08 stsp if (repo) {
3855 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3856 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3857 ad242220 2018-09-08 stsp if (error == NULL)
3858 ad242220 2018-09-08 stsp error = repo_error;
3859 ad242220 2018-09-08 stsp }
3860 b00d56cd 2018-04-01 stsp return error;
3861 404c43c4 2018-06-21 stsp }
3862 404c43c4 2018-06-21 stsp
3863 404c43c4 2018-06-21 stsp __dead static void
3864 404c43c4 2018-06-21 stsp usage_blame(void)
3865 404c43c4 2018-06-21 stsp {
3866 9270e621 2019-02-05 stsp fprintf(stderr,
3867 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3868 404c43c4 2018-06-21 stsp getprogname());
3869 404c43c4 2018-06-21 stsp exit(1);
3870 b00d56cd 2018-04-01 stsp }
3871 b00d56cd 2018-04-01 stsp
3872 28315671 2019-08-14 stsp struct blame_line {
3873 28315671 2019-08-14 stsp int annotated;
3874 28315671 2019-08-14 stsp char *id_str;
3875 82f6abb8 2019-08-14 stsp char *committer;
3876 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3877 28315671 2019-08-14 stsp };
3878 28315671 2019-08-14 stsp
3879 28315671 2019-08-14 stsp struct blame_cb_args {
3880 28315671 2019-08-14 stsp struct blame_line *lines;
3881 28315671 2019-08-14 stsp int nlines;
3882 7ef28ff8 2019-08-14 stsp int nlines_prec;
3883 28315671 2019-08-14 stsp int lineno_cur;
3884 28315671 2019-08-14 stsp off_t *line_offsets;
3885 28315671 2019-08-14 stsp FILE *f;
3886 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3887 28315671 2019-08-14 stsp };
3888 28315671 2019-08-14 stsp
3889 404c43c4 2018-06-21 stsp static const struct got_error *
3890 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3891 28315671 2019-08-14 stsp {
3892 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3893 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3894 28315671 2019-08-14 stsp struct blame_line *bline;
3895 82f6abb8 2019-08-14 stsp char *line = NULL;
3896 28315671 2019-08-14 stsp size_t linesize = 0;
3897 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3898 28315671 2019-08-14 stsp off_t offset;
3899 bcb49d15 2019-08-14 stsp struct tm tm;
3900 bcb49d15 2019-08-14 stsp time_t committer_time;
3901 28315671 2019-08-14 stsp
3902 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3903 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3904 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3905 28315671 2019-08-14 stsp
3906 28315671 2019-08-14 stsp if (sigint_received)
3907 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3908 28315671 2019-08-14 stsp
3909 2839f8b9 2019-08-18 stsp if (lineno == -1)
3910 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3911 2839f8b9 2019-08-18 stsp
3912 28315671 2019-08-14 stsp /* Annotate this line. */
3913 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3914 28315671 2019-08-14 stsp if (bline->annotated)
3915 28315671 2019-08-14 stsp return NULL;
3916 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3917 28315671 2019-08-14 stsp if (err)
3918 28315671 2019-08-14 stsp return err;
3919 82f6abb8 2019-08-14 stsp
3920 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3921 82f6abb8 2019-08-14 stsp if (err)
3922 82f6abb8 2019-08-14 stsp goto done;
3923 82f6abb8 2019-08-14 stsp
3924 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3925 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3926 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3927 bcb49d15 2019-08-14 stsp goto done;
3928 bcb49d15 2019-08-14 stsp }
3929 bcb49d15 2019-08-14 stsp
3930 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3931 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3932 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3933 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3934 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3935 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3936 82f6abb8 2019-08-14 stsp goto done;
3937 82f6abb8 2019-08-14 stsp }
3938 28315671 2019-08-14 stsp bline->annotated = 1;
3939 28315671 2019-08-14 stsp
3940 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3941 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3942 28315671 2019-08-14 stsp if (!bline->annotated)
3943 82f6abb8 2019-08-14 stsp goto done;
3944 28315671 2019-08-14 stsp
3945 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3946 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3947 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3948 82f6abb8 2019-08-14 stsp goto done;
3949 82f6abb8 2019-08-14 stsp }
3950 28315671 2019-08-14 stsp
3951 28315671 2019-08-14 stsp while (bline->annotated) {
3952 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3953 82f6abb8 2019-08-14 stsp size_t len;
3954 82f6abb8 2019-08-14 stsp
3955 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3956 28315671 2019-08-14 stsp if (ferror(a->f))
3957 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3958 28315671 2019-08-14 stsp break;
3959 28315671 2019-08-14 stsp }
3960 82f6abb8 2019-08-14 stsp
3961 82f6abb8 2019-08-14 stsp committer = bline->committer;
3962 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3963 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3964 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3965 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3966 82f6abb8 2019-08-14 stsp if (at)
3967 82f6abb8 2019-08-14 stsp *at = '\0';
3968 82f6abb8 2019-08-14 stsp len = strlen(committer);
3969 82f6abb8 2019-08-14 stsp if (len >= 9)
3970 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3971 28315671 2019-08-14 stsp
3972 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3973 28315671 2019-08-14 stsp if (nl)
3974 28315671 2019-08-14 stsp *nl = '\0';
3975 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3976 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3977 28315671 2019-08-14 stsp
3978 28315671 2019-08-14 stsp a->lineno_cur++;
3979 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3980 28315671 2019-08-14 stsp }
3981 82f6abb8 2019-08-14 stsp done:
3982 82f6abb8 2019-08-14 stsp if (commit)
3983 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3984 28315671 2019-08-14 stsp free(line);
3985 28315671 2019-08-14 stsp return err;
3986 28315671 2019-08-14 stsp }
3987 28315671 2019-08-14 stsp
3988 28315671 2019-08-14 stsp static const struct got_error *
3989 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3990 404c43c4 2018-06-21 stsp {
3991 404c43c4 2018-06-21 stsp const struct got_error *error;
3992 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3993 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3994 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3995 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3996 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3997 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3998 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3999 28315671 2019-08-14 stsp struct blame_cb_args bca;
4000 28315671 2019-08-14 stsp int ch, obj_type, i;
4001 b02560ec 2019-08-19 stsp size_t filesize;
4002 90f3c347 2019-08-19 stsp
4003 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4004 404c43c4 2018-06-21 stsp
4005 404c43c4 2018-06-21 stsp #ifndef PROFILE
4006 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4007 36e2fb66 2019-01-04 stsp NULL) == -1)
4008 404c43c4 2018-06-21 stsp err(1, "pledge");
4009 404c43c4 2018-06-21 stsp #endif
4010 404c43c4 2018-06-21 stsp
4011 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4012 404c43c4 2018-06-21 stsp switch (ch) {
4013 404c43c4 2018-06-21 stsp case 'c':
4014 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4015 404c43c4 2018-06-21 stsp break;
4016 66bea077 2018-08-02 stsp case 'r':
4017 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4018 66bea077 2018-08-02 stsp if (repo_path == NULL)
4019 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4020 9ba1d308 2019-10-21 stsp optarg);
4021 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4022 66bea077 2018-08-02 stsp break;
4023 404c43c4 2018-06-21 stsp default:
4024 2deda0b9 2019-03-07 stsp usage_blame();
4025 404c43c4 2018-06-21 stsp /* NOTREACHED */
4026 404c43c4 2018-06-21 stsp }
4027 404c43c4 2018-06-21 stsp }
4028 404c43c4 2018-06-21 stsp
4029 404c43c4 2018-06-21 stsp argc -= optind;
4030 404c43c4 2018-06-21 stsp argv += optind;
4031 404c43c4 2018-06-21 stsp
4032 a39318fd 2018-08-02 stsp if (argc == 1)
4033 404c43c4 2018-06-21 stsp path = argv[0];
4034 a39318fd 2018-08-02 stsp else
4035 404c43c4 2018-06-21 stsp usage_blame();
4036 404c43c4 2018-06-21 stsp
4037 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4038 66bea077 2018-08-02 stsp if (cwd == NULL) {
4039 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4040 66bea077 2018-08-02 stsp goto done;
4041 66bea077 2018-08-02 stsp }
4042 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4043 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4044 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4045 66bea077 2018-08-02 stsp goto done;
4046 0c06baac 2019-02-05 stsp else
4047 0c06baac 2019-02-05 stsp error = NULL;
4048 0c06baac 2019-02-05 stsp if (worktree) {
4049 0c06baac 2019-02-05 stsp repo_path =
4050 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4051 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4052 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4053 1f03b8da 2020-03-20 stsp if (error)
4054 1f03b8da 2020-03-20 stsp goto done;
4055 1f03b8da 2020-03-20 stsp }
4056 0c06baac 2019-02-05 stsp } else {
4057 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4058 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4059 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4060 0c06baac 2019-02-05 stsp goto done;
4061 0c06baac 2019-02-05 stsp }
4062 66bea077 2018-08-02 stsp }
4063 66bea077 2018-08-02 stsp }
4064 36e2fb66 2019-01-04 stsp
4065 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4066 c02c541e 2019-03-29 stsp if (error != NULL)
4067 36e2fb66 2019-01-04 stsp goto done;
4068 66bea077 2018-08-02 stsp
4069 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4070 c02c541e 2019-03-29 stsp if (error)
4071 404c43c4 2018-06-21 stsp goto done;
4072 404c43c4 2018-06-21 stsp
4073 0c06baac 2019-02-05 stsp if (worktree) {
4074 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4075 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4076 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4077 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4078 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4079 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4080 6efaaa2d 2019-02-05 stsp path) == -1) {
4081 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4082 0c06baac 2019-02-05 stsp goto done;
4083 0c06baac 2019-02-05 stsp }
4084 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4085 0c06baac 2019-02-05 stsp free(p);
4086 0c06baac 2019-02-05 stsp } else {
4087 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4088 0c06baac 2019-02-05 stsp }
4089 0c06baac 2019-02-05 stsp if (error)
4090 66bea077 2018-08-02 stsp goto done;
4091 66bea077 2018-08-02 stsp
4092 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4093 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4094 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4095 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4096 404c43c4 2018-06-21 stsp if (error != NULL)
4097 66bea077 2018-08-02 stsp goto done;
4098 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4099 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4100 404c43c4 2018-06-21 stsp if (error != NULL)
4101 66bea077 2018-08-02 stsp goto done;
4102 404c43c4 2018-06-21 stsp } else {
4103 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4104 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4105 30837e32 2019-07-25 stsp if (error)
4106 66bea077 2018-08-02 stsp goto done;
4107 404c43c4 2018-06-21 stsp }
4108 404c43c4 2018-06-21 stsp
4109 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
4110 28315671 2019-08-14 stsp if (error)
4111 28315671 2019-08-14 stsp goto done;
4112 28315671 2019-08-14 stsp
4113 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4114 28315671 2019-08-14 stsp if (error)
4115 28315671 2019-08-14 stsp goto done;
4116 28315671 2019-08-14 stsp
4117 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4118 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4119 28315671 2019-08-14 stsp goto done;
4120 28315671 2019-08-14 stsp }
4121 28315671 2019-08-14 stsp
4122 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4123 28315671 2019-08-14 stsp if (error)
4124 28315671 2019-08-14 stsp goto done;
4125 28315671 2019-08-14 stsp bca.f = got_opentemp();
4126 28315671 2019-08-14 stsp if (bca.f == NULL) {
4127 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4128 28315671 2019-08-14 stsp goto done;
4129 28315671 2019-08-14 stsp }
4130 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4131 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4132 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4133 28315671 2019-08-14 stsp goto done;
4134 28315671 2019-08-14 stsp
4135 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4136 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4137 b02560ec 2019-08-19 stsp bca.nlines--;
4138 b02560ec 2019-08-19 stsp
4139 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4140 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4141 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4142 28315671 2019-08-14 stsp goto done;
4143 28315671 2019-08-14 stsp }
4144 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4145 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4146 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4147 7ef28ff8 2019-08-14 stsp while (i > 0) {
4148 7ef28ff8 2019-08-14 stsp i /= 10;
4149 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4150 7ef28ff8 2019-08-14 stsp }
4151 82f6abb8 2019-08-14 stsp bca.repo = repo;
4152 7ef28ff8 2019-08-14 stsp
4153 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
4154 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4155 404c43c4 2018-06-21 stsp done:
4156 66bea077 2018-08-02 stsp free(in_repo_path);
4157 66bea077 2018-08-02 stsp free(repo_path);
4158 66bea077 2018-08-02 stsp free(cwd);
4159 404c43c4 2018-06-21 stsp free(commit_id);
4160 28315671 2019-08-14 stsp free(obj_id);
4161 28315671 2019-08-14 stsp if (blob)
4162 28315671 2019-08-14 stsp got_object_blob_close(blob);
4163 0c06baac 2019-02-05 stsp if (worktree)
4164 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4165 ad242220 2018-09-08 stsp if (repo) {
4166 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4167 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4168 ad242220 2018-09-08 stsp if (error == NULL)
4169 ad242220 2018-09-08 stsp error = repo_error;
4170 ad242220 2018-09-08 stsp }
4171 3affba96 2019-09-22 stsp if (bca.lines) {
4172 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4173 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4174 3affba96 2019-09-22 stsp free(bline->id_str);
4175 3affba96 2019-09-22 stsp free(bline->committer);
4176 3affba96 2019-09-22 stsp }
4177 3affba96 2019-09-22 stsp free(bca.lines);
4178 28315671 2019-08-14 stsp }
4179 28315671 2019-08-14 stsp free(bca.line_offsets);
4180 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4181 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4182 404c43c4 2018-06-21 stsp return error;
4183 5de5890b 2018-10-18 stsp }
4184 5de5890b 2018-10-18 stsp
4185 5de5890b 2018-10-18 stsp __dead static void
4186 5de5890b 2018-10-18 stsp usage_tree(void)
4187 5de5890b 2018-10-18 stsp {
4188 c1669e2e 2019-01-09 stsp fprintf(stderr,
4189 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
4190 5de5890b 2018-10-18 stsp getprogname());
4191 5de5890b 2018-10-18 stsp exit(1);
4192 5de5890b 2018-10-18 stsp }
4193 5de5890b 2018-10-18 stsp
4194 c1669e2e 2019-01-09 stsp static void
4195 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4196 c1669e2e 2019-01-09 stsp const char *root_path)
4197 c1669e2e 2019-01-09 stsp {
4198 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4199 848d6979 2019-08-12 stsp const char *modestr = "";
4200 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4201 5de5890b 2018-10-18 stsp
4202 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4203 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4204 c1669e2e 2019-01-09 stsp path++;
4205 c1669e2e 2019-01-09 stsp
4206 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4207 63c5ca5d 2019-08-24 stsp modestr = "$";
4208 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4209 848d6979 2019-08-12 stsp modestr = "@";
4210 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4211 848d6979 2019-08-12 stsp modestr = "/";
4212 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4213 848d6979 2019-08-12 stsp modestr = "*";
4214 848d6979 2019-08-12 stsp
4215 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
4216 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
4217 c1669e2e 2019-01-09 stsp }
4218 c1669e2e 2019-01-09 stsp
4219 5de5890b 2018-10-18 stsp static const struct got_error *
4220 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4221 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4222 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4223 5de5890b 2018-10-18 stsp {
4224 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4225 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4226 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4227 56e0773d 2019-11-28 stsp int nentries, i;
4228 5de5890b 2018-10-18 stsp
4229 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4230 5de5890b 2018-10-18 stsp if (err)
4231 5de5890b 2018-10-18 stsp goto done;
4232 5de5890b 2018-10-18 stsp
4233 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4234 5de5890b 2018-10-18 stsp if (err)
4235 5de5890b 2018-10-18 stsp goto done;
4236 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4237 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4238 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4239 5de5890b 2018-10-18 stsp char *id = NULL;
4240 84453469 2018-11-11 stsp
4241 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4242 84453469 2018-11-11 stsp break;
4243 84453469 2018-11-11 stsp
4244 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4245 5de5890b 2018-10-18 stsp if (show_ids) {
4246 5de5890b 2018-10-18 stsp char *id_str;
4247 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4248 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4249 5de5890b 2018-10-18 stsp if (err)
4250 5de5890b 2018-10-18 stsp goto done;
4251 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4252 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4253 5de5890b 2018-10-18 stsp free(id_str);
4254 5de5890b 2018-10-18 stsp goto done;
4255 5de5890b 2018-10-18 stsp }
4256 5de5890b 2018-10-18 stsp free(id_str);
4257 5de5890b 2018-10-18 stsp }
4258 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
4259 5de5890b 2018-10-18 stsp free(id);
4260 c1669e2e 2019-01-09 stsp
4261 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4262 c1669e2e 2019-01-09 stsp char *child_path;
4263 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4264 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4265 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4266 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4267 c1669e2e 2019-01-09 stsp goto done;
4268 c1669e2e 2019-01-09 stsp }
4269 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4270 c1669e2e 2019-01-09 stsp root_path, repo);
4271 c1669e2e 2019-01-09 stsp free(child_path);
4272 c1669e2e 2019-01-09 stsp if (err)
4273 c1669e2e 2019-01-09 stsp goto done;
4274 c1669e2e 2019-01-09 stsp }
4275 5de5890b 2018-10-18 stsp }
4276 5de5890b 2018-10-18 stsp done:
4277 5de5890b 2018-10-18 stsp if (tree)
4278 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4279 5de5890b 2018-10-18 stsp free(tree_id);
4280 5de5890b 2018-10-18 stsp return err;
4281 404c43c4 2018-06-21 stsp }
4282 404c43c4 2018-06-21 stsp
4283 5de5890b 2018-10-18 stsp static const struct got_error *
4284 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4285 5de5890b 2018-10-18 stsp {
4286 5de5890b 2018-10-18 stsp const struct got_error *error;
4287 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4288 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4289 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4290 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4291 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4292 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4293 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4294 5de5890b 2018-10-18 stsp int ch;
4295 5de5890b 2018-10-18 stsp
4296 5de5890b 2018-10-18 stsp #ifndef PROFILE
4297 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4298 0f8d269b 2019-01-04 stsp NULL) == -1)
4299 5de5890b 2018-10-18 stsp err(1, "pledge");
4300 5de5890b 2018-10-18 stsp #endif
4301 5de5890b 2018-10-18 stsp
4302 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4303 5de5890b 2018-10-18 stsp switch (ch) {
4304 5de5890b 2018-10-18 stsp case 'c':
4305 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4306 5de5890b 2018-10-18 stsp break;
4307 5de5890b 2018-10-18 stsp case 'r':
4308 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4309 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4310 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4311 9ba1d308 2019-10-21 stsp optarg);
4312 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4313 5de5890b 2018-10-18 stsp break;
4314 5de5890b 2018-10-18 stsp case 'i':
4315 5de5890b 2018-10-18 stsp show_ids = 1;
4316 5de5890b 2018-10-18 stsp break;
4317 c1669e2e 2019-01-09 stsp case 'R':
4318 c1669e2e 2019-01-09 stsp recurse = 1;
4319 c1669e2e 2019-01-09 stsp break;
4320 5de5890b 2018-10-18 stsp default:
4321 2deda0b9 2019-03-07 stsp usage_tree();
4322 5de5890b 2018-10-18 stsp /* NOTREACHED */
4323 5de5890b 2018-10-18 stsp }
4324 5de5890b 2018-10-18 stsp }
4325 5de5890b 2018-10-18 stsp
4326 5de5890b 2018-10-18 stsp argc -= optind;
4327 5de5890b 2018-10-18 stsp argv += optind;
4328 5de5890b 2018-10-18 stsp
4329 5de5890b 2018-10-18 stsp if (argc == 1)
4330 5de5890b 2018-10-18 stsp path = argv[0];
4331 5de5890b 2018-10-18 stsp else if (argc > 1)
4332 5de5890b 2018-10-18 stsp usage_tree();
4333 5de5890b 2018-10-18 stsp else
4334 7a2c19d6 2019-02-05 stsp path = NULL;
4335 7a2c19d6 2019-02-05 stsp
4336 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4337 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4338 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4339 9bf7a39b 2019-02-05 stsp goto done;
4340 9bf7a39b 2019-02-05 stsp }
4341 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4342 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4343 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4344 7a2c19d6 2019-02-05 stsp goto done;
4345 7a2c19d6 2019-02-05 stsp else
4346 7a2c19d6 2019-02-05 stsp error = NULL;
4347 7a2c19d6 2019-02-05 stsp if (worktree) {
4348 7a2c19d6 2019-02-05 stsp repo_path =
4349 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4350 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4351 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4352 7a2c19d6 2019-02-05 stsp if (error)
4353 7a2c19d6 2019-02-05 stsp goto done;
4354 7a2c19d6 2019-02-05 stsp } else {
4355 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4356 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4357 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4358 7a2c19d6 2019-02-05 stsp goto done;
4359 7a2c19d6 2019-02-05 stsp }
4360 5de5890b 2018-10-18 stsp }
4361 5de5890b 2018-10-18 stsp }
4362 5de5890b 2018-10-18 stsp
4363 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4364 5de5890b 2018-10-18 stsp if (error != NULL)
4365 c02c541e 2019-03-29 stsp goto done;
4366 c02c541e 2019-03-29 stsp
4367 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4368 c02c541e 2019-03-29 stsp if (error)
4369 5de5890b 2018-10-18 stsp goto done;
4370 5de5890b 2018-10-18 stsp
4371 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4372 9bf7a39b 2019-02-05 stsp if (worktree) {
4373 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4374 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4375 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4376 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4377 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4378 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4379 9bf7a39b 2019-02-05 stsp goto done;
4380 9bf7a39b 2019-02-05 stsp }
4381 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4382 9bf7a39b 2019-02-05 stsp free(p);
4383 9bf7a39b 2019-02-05 stsp if (error)
4384 9bf7a39b 2019-02-05 stsp goto done;
4385 9bf7a39b 2019-02-05 stsp } else
4386 9bf7a39b 2019-02-05 stsp path = "/";
4387 9bf7a39b 2019-02-05 stsp }
4388 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4389 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4390 9bf7a39b 2019-02-05 stsp if (error != NULL)
4391 9bf7a39b 2019-02-05 stsp goto done;
4392 9bf7a39b 2019-02-05 stsp }
4393 5de5890b 2018-10-18 stsp
4394 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4395 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4396 4e0a20a4 2020-03-23 tracey if (worktree)
4397 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4398 4e0a20a4 2020-03-23 tracey else
4399 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4400 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4401 5de5890b 2018-10-18 stsp if (error != NULL)
4402 5de5890b 2018-10-18 stsp goto done;
4403 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4404 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4405 5de5890b 2018-10-18 stsp if (error != NULL)
4406 5de5890b 2018-10-18 stsp goto done;
4407 5de5890b 2018-10-18 stsp } else {
4408 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4409 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4410 30837e32 2019-07-25 stsp if (error)
4411 5de5890b 2018-10-18 stsp goto done;
4412 5de5890b 2018-10-18 stsp }
4413 5de5890b 2018-10-18 stsp
4414 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4415 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4416 5de5890b 2018-10-18 stsp done:
4417 5de5890b 2018-10-18 stsp free(in_repo_path);
4418 5de5890b 2018-10-18 stsp free(repo_path);
4419 5de5890b 2018-10-18 stsp free(cwd);
4420 5de5890b 2018-10-18 stsp free(commit_id);
4421 7a2c19d6 2019-02-05 stsp if (worktree)
4422 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4423 5de5890b 2018-10-18 stsp if (repo) {
4424 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4425 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4426 5de5890b 2018-10-18 stsp if (error == NULL)
4427 5de5890b 2018-10-18 stsp error = repo_error;
4428 5de5890b 2018-10-18 stsp }
4429 5de5890b 2018-10-18 stsp return error;
4430 5de5890b 2018-10-18 stsp }
4431 5de5890b 2018-10-18 stsp
4432 6bad629b 2019-02-04 stsp __dead static void
4433 6bad629b 2019-02-04 stsp usage_status(void)
4434 6bad629b 2019-02-04 stsp {
4435 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4436 6bad629b 2019-02-04 stsp exit(1);
4437 6bad629b 2019-02-04 stsp }
4438 5c860e29 2018-03-12 stsp
4439 b72f483a 2019-02-05 stsp static const struct got_error *
4440 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4441 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4442 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4443 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4444 6bad629b 2019-02-04 stsp {
4445 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4446 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4447 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4448 b72f483a 2019-02-05 stsp return NULL;
4449 6bad629b 2019-02-04 stsp }
4450 5c860e29 2018-03-12 stsp
4451 6bad629b 2019-02-04 stsp static const struct got_error *
4452 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4453 6bad629b 2019-02-04 stsp {
4454 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4455 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4456 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4457 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
4458 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4459 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4460 a5edda0a 2019-07-27 stsp int ch;
4461 5c860e29 2018-03-12 stsp
4462 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4463 72ea6654 2019-07-27 stsp
4464 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4465 6bad629b 2019-02-04 stsp switch (ch) {
4466 5c860e29 2018-03-12 stsp default:
4467 2deda0b9 2019-03-07 stsp usage_status();
4468 6bad629b 2019-02-04 stsp /* NOTREACHED */
4469 5c860e29 2018-03-12 stsp }
4470 5c860e29 2018-03-12 stsp }
4471 5c860e29 2018-03-12 stsp
4472 6bad629b 2019-02-04 stsp argc -= optind;
4473 6bad629b 2019-02-04 stsp argv += optind;
4474 5c860e29 2018-03-12 stsp
4475 6bad629b 2019-02-04 stsp #ifndef PROFILE
4476 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4477 6bad629b 2019-02-04 stsp NULL) == -1)
4478 6bad629b 2019-02-04 stsp err(1, "pledge");
4479 f42b1b34 2018-03-12 stsp #endif
4480 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4481 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4482 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4483 927df6b7 2019-02-10 stsp goto done;
4484 927df6b7 2019-02-10 stsp }
4485 927df6b7 2019-02-10 stsp
4486 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4487 fa51e947 2020-03-27 stsp if (error) {
4488 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4489 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4490 a5edda0a 2019-07-27 stsp goto done;
4491 fa51e947 2020-03-27 stsp }
4492 6bad629b 2019-02-04 stsp
4493 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4494 c9956ddf 2019-09-08 stsp NULL);
4495 6bad629b 2019-02-04 stsp if (error != NULL)
4496 6bad629b 2019-02-04 stsp goto done;
4497 6bad629b 2019-02-04 stsp
4498 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4499 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4500 087fb88c 2019-08-04 stsp if (error)
4501 087fb88c 2019-08-04 stsp goto done;
4502 087fb88c 2019-08-04 stsp
4503 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4504 6bad629b 2019-02-04 stsp if (error)
4505 6bad629b 2019-02-04 stsp goto done;
4506 6bad629b 2019-02-04 stsp
4507 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4508 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4509 6bad629b 2019-02-04 stsp done:
4510 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4511 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4512 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4513 927df6b7 2019-02-10 stsp free(cwd);
4514 d0eebce4 2019-03-11 stsp return error;
4515 d0eebce4 2019-03-11 stsp }
4516 d0eebce4 2019-03-11 stsp
4517 d0eebce4 2019-03-11 stsp __dead static void
4518 d0eebce4 2019-03-11 stsp usage_ref(void)
4519 d0eebce4 2019-03-11 stsp {
4520 d0eebce4 2019-03-11 stsp fprintf(stderr,
4521 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4522 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4523 d0eebce4 2019-03-11 stsp getprogname());
4524 d0eebce4 2019-03-11 stsp exit(1);
4525 d0eebce4 2019-03-11 stsp }
4526 d0eebce4 2019-03-11 stsp
4527 d0eebce4 2019-03-11 stsp static const struct got_error *
4528 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4529 d0eebce4 2019-03-11 stsp {
4530 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4531 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4532 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4533 d0eebce4 2019-03-11 stsp
4534 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4535 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4536 d0eebce4 2019-03-11 stsp if (err)
4537 d0eebce4 2019-03-11 stsp return err;
4538 d0eebce4 2019-03-11 stsp
4539 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4540 d0eebce4 2019-03-11 stsp char *refstr;
4541 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4542 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4543 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4544 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4545 d0eebce4 2019-03-11 stsp free(refstr);
4546 d0eebce4 2019-03-11 stsp }
4547 d0eebce4 2019-03-11 stsp
4548 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4549 d0eebce4 2019-03-11 stsp return NULL;
4550 d0eebce4 2019-03-11 stsp }
4551 d0eebce4 2019-03-11 stsp
4552 d0eebce4 2019-03-11 stsp static const struct got_error *
4553 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4554 d0eebce4 2019-03-11 stsp {
4555 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4556 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4557 d0eebce4 2019-03-11 stsp
4558 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4559 d0eebce4 2019-03-11 stsp if (err)
4560 d0eebce4 2019-03-11 stsp return err;
4561 d0eebce4 2019-03-11 stsp
4562 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4563 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4564 d0eebce4 2019-03-11 stsp return err;
4565 d0eebce4 2019-03-11 stsp }
4566 d0eebce4 2019-03-11 stsp
4567 d0eebce4 2019-03-11 stsp static const struct got_error *
4568 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4569 d0eebce4 2019-03-11 stsp {
4570 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4571 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4572 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4573 d1644381 2019-07-14 stsp
4574 d1644381 2019-07-14 stsp /*
4575 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4576 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4577 d1644381 2019-07-14 stsp * an unintended typo.
4578 d1644381 2019-07-14 stsp */
4579 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4580 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4581 d0eebce4 2019-03-11 stsp
4582 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4583 dd88155e 2019-06-29 stsp repo);
4584 d83d9d5c 2019-05-13 stsp if (err) {
4585 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4586 d0eebce4 2019-03-11 stsp
4587 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4588 d83d9d5c 2019-05-13 stsp return err;
4589 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4590 d83d9d5c 2019-05-13 stsp if (err)
4591 d83d9d5c 2019-05-13 stsp return err;
4592 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4593 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4594 d83d9d5c 2019-05-13 stsp if (err)
4595 d83d9d5c 2019-05-13 stsp return err;
4596 d83d9d5c 2019-05-13 stsp }
4597 d83d9d5c 2019-05-13 stsp
4598 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4599 d0eebce4 2019-03-11 stsp if (err)
4600 d0eebce4 2019-03-11 stsp goto done;
4601 d0eebce4 2019-03-11 stsp
4602 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4603 d0eebce4 2019-03-11 stsp done:
4604 d0eebce4 2019-03-11 stsp if (ref)
4605 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4606 d0eebce4 2019-03-11 stsp free(id);
4607 d1c1ae5f 2019-08-12 stsp return err;
4608 d1c1ae5f 2019-08-12 stsp }
4609 d1c1ae5f 2019-08-12 stsp
4610 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4611 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4612 d1c1ae5f 2019-08-12 stsp {
4613 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4614 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4615 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4616 d1c1ae5f 2019-08-12 stsp
4617 d1c1ae5f 2019-08-12 stsp /*
4618 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4619 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4620 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4621 d1c1ae5f 2019-08-12 stsp */
4622 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4623 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4624 d1c1ae5f 2019-08-12 stsp
4625 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4626 d1c1ae5f 2019-08-12 stsp if (err)
4627 d1c1ae5f 2019-08-12 stsp return err;
4628 d1c1ae5f 2019-08-12 stsp
4629 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4630 d1c1ae5f 2019-08-12 stsp if (err)
4631 d1c1ae5f 2019-08-12 stsp goto done;
4632 d1c1ae5f 2019-08-12 stsp
4633 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4634 d1c1ae5f 2019-08-12 stsp done:
4635 d1c1ae5f 2019-08-12 stsp if (target_ref)
4636 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4637 d1c1ae5f 2019-08-12 stsp if (ref)
4638 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4639 d0eebce4 2019-03-11 stsp return err;
4640 d0eebce4 2019-03-11 stsp }
4641 d0eebce4 2019-03-11 stsp
4642 d0eebce4 2019-03-11 stsp static const struct got_error *
4643 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4644 d0eebce4 2019-03-11 stsp {
4645 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4646 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4647 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4648 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4649 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4650 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4651 b2070a3f 2020-03-22 stsp char *refname = NULL;
4652 d0eebce4 2019-03-11 stsp
4653 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4654 d0eebce4 2019-03-11 stsp switch (ch) {
4655 e31abbf2 2020-03-22 stsp case 'c':
4656 e31abbf2 2020-03-22 stsp obj_arg = optarg;
4657 e31abbf2 2020-03-22 stsp break;
4658 d0eebce4 2019-03-11 stsp case 'd':
4659 e31abbf2 2020-03-22 stsp do_delete = 1;
4660 d0eebce4 2019-03-11 stsp break;
4661 d0eebce4 2019-03-11 stsp case 'r':
4662 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4663 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4664 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4665 9ba1d308 2019-10-21 stsp optarg);
4666 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4667 d0eebce4 2019-03-11 stsp break;
4668 d0eebce4 2019-03-11 stsp case 'l':
4669 d0eebce4 2019-03-11 stsp do_list = 1;
4670 d1c1ae5f 2019-08-12 stsp break;
4671 d1c1ae5f 2019-08-12 stsp case 's':
4672 e31abbf2 2020-03-22 stsp symref_target = optarg;
4673 d0eebce4 2019-03-11 stsp break;
4674 d0eebce4 2019-03-11 stsp default:
4675 d0eebce4 2019-03-11 stsp usage_ref();
4676 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4677 d0eebce4 2019-03-11 stsp }
4678 d0eebce4 2019-03-11 stsp }
4679 d0eebce4 2019-03-11 stsp
4680 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
4681 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
4682 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
4683 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
4684 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
4685 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
4686 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
4687 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
4688 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
4689 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
4690 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
4691 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
4692 d0eebce4 2019-03-11 stsp
4693 d0eebce4 2019-03-11 stsp argc -= optind;
4694 d0eebce4 2019-03-11 stsp argv += optind;
4695 d0eebce4 2019-03-11 stsp
4696 e31abbf2 2020-03-22 stsp if (do_list) {
4697 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
4698 d0eebce4 2019-03-11 stsp usage_ref();
4699 b2070a3f 2020-03-22 stsp if (argc == 1) {
4700 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4701 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4702 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4703 b2070a3f 2020-03-22 stsp goto done;
4704 b2070a3f 2020-03-22 stsp }
4705 b2070a3f 2020-03-22 stsp }
4706 e31abbf2 2020-03-22 stsp } else {
4707 e31abbf2 2020-03-22 stsp if (argc != 1)
4708 e31abbf2 2020-03-22 stsp usage_ref();
4709 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4710 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4711 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4712 b2070a3f 2020-03-22 stsp goto done;
4713 b2070a3f 2020-03-22 stsp }
4714 e31abbf2 2020-03-22 stsp }
4715 b2070a3f 2020-03-22 stsp
4716 b2070a3f 2020-03-22 stsp if (refname)
4717 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
4718 d0eebce4 2019-03-11 stsp
4719 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4720 e0b57350 2019-03-12 stsp if (do_list) {
4721 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4722 e0b57350 2019-03-12 stsp NULL) == -1)
4723 e0b57350 2019-03-12 stsp err(1, "pledge");
4724 e0b57350 2019-03-12 stsp } else {
4725 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4726 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4727 e0b57350 2019-03-12 stsp err(1, "pledge");
4728 e0b57350 2019-03-12 stsp }
4729 d0eebce4 2019-03-11 stsp #endif
4730 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4731 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4732 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4733 d0eebce4 2019-03-11 stsp goto done;
4734 d0eebce4 2019-03-11 stsp }
4735 d0eebce4 2019-03-11 stsp
4736 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4737 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4738 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4739 d0eebce4 2019-03-11 stsp goto done;
4740 d0eebce4 2019-03-11 stsp else
4741 d0eebce4 2019-03-11 stsp error = NULL;
4742 d0eebce4 2019-03-11 stsp if (worktree) {
4743 d0eebce4 2019-03-11 stsp repo_path =
4744 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4745 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4746 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4747 d0eebce4 2019-03-11 stsp if (error)
4748 d0eebce4 2019-03-11 stsp goto done;
4749 d0eebce4 2019-03-11 stsp } else {
4750 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4751 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4752 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4753 d0eebce4 2019-03-11 stsp goto done;
4754 d0eebce4 2019-03-11 stsp }
4755 d0eebce4 2019-03-11 stsp }
4756 d0eebce4 2019-03-11 stsp }
4757 d0eebce4 2019-03-11 stsp
4758 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4759 d0eebce4 2019-03-11 stsp if (error != NULL)
4760 d0eebce4 2019-03-11 stsp goto done;
4761 d0eebce4 2019-03-11 stsp
4762 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4763 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4764 c02c541e 2019-03-29 stsp if (error)
4765 c02c541e 2019-03-29 stsp goto done;
4766 c02c541e 2019-03-29 stsp
4767 d0eebce4 2019-03-11 stsp if (do_list)
4768 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
4769 e31abbf2 2020-03-22 stsp else if (do_delete)
4770 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
4771 e31abbf2 2020-03-22 stsp else if (symref_target)
4772 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
4773 e31abbf2 2020-03-22 stsp else {
4774 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
4775 e31abbf2 2020-03-22 stsp usage_ref();
4776 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
4777 e31abbf2 2020-03-22 stsp }
4778 4e759de4 2019-06-26 stsp done:
4779 b2070a3f 2020-03-22 stsp free(refname);
4780 4e759de4 2019-06-26 stsp if (repo)
4781 4e759de4 2019-06-26 stsp got_repo_close(repo);
4782 4e759de4 2019-06-26 stsp if (worktree)
4783 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4784 4e759de4 2019-06-26 stsp free(cwd);
4785 4e759de4 2019-06-26 stsp free(repo_path);
4786 4e759de4 2019-06-26 stsp return error;
4787 4e759de4 2019-06-26 stsp }
4788 4e759de4 2019-06-26 stsp
4789 4e759de4 2019-06-26 stsp __dead static void
4790 4e759de4 2019-06-26 stsp usage_branch(void)
4791 4e759de4 2019-06-26 stsp {
4792 4e759de4 2019-06-26 stsp fprintf(stderr,
4793 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4794 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4795 4e759de4 2019-06-26 stsp exit(1);
4796 4e759de4 2019-06-26 stsp }
4797 4e759de4 2019-06-26 stsp
4798 4e759de4 2019-06-26 stsp static const struct got_error *
4799 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4800 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4801 4e99b47e 2019-10-04 stsp {
4802 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4803 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4804 4e99b47e 2019-10-04 stsp char *refstr;
4805 4e99b47e 2019-10-04 stsp
4806 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4807 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4808 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4809 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4810 4e99b47e 2019-10-04 stsp
4811 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4812 4e99b47e 2019-10-04 stsp if (err)
4813 4e99b47e 2019-10-04 stsp return err;
4814 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4815 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4816 4e99b47e 2019-10-04 stsp marker = "* ";
4817 4e99b47e 2019-10-04 stsp else
4818 4e99b47e 2019-10-04 stsp marker = "~ ";
4819 4e99b47e 2019-10-04 stsp free(id);
4820 4e99b47e 2019-10-04 stsp }
4821 4e99b47e 2019-10-04 stsp
4822 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4823 4e99b47e 2019-10-04 stsp refname += 11;
4824 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4825 4e99b47e 2019-10-04 stsp refname += 18;
4826 4e99b47e 2019-10-04 stsp
4827 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4828 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4829 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4830 4e99b47e 2019-10-04 stsp
4831 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4832 4e99b47e 2019-10-04 stsp free(refstr);
4833 4e99b47e 2019-10-04 stsp return NULL;
4834 4e99b47e 2019-10-04 stsp }
4835 4e99b47e 2019-10-04 stsp
4836 4e99b47e 2019-10-04 stsp static const struct got_error *
4837 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4838 ad89fa31 2019-10-04 stsp {
4839 ad89fa31 2019-10-04 stsp const char *refname;
4840 ad89fa31 2019-10-04 stsp
4841 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4842 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4843 ad89fa31 2019-10-04 stsp
4844 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4845 ad89fa31 2019-10-04 stsp
4846 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4847 ad89fa31 2019-10-04 stsp refname += 11;
4848 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4849 ad89fa31 2019-10-04 stsp refname += 18;
4850 ad89fa31 2019-10-04 stsp
4851 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4852 ad89fa31 2019-10-04 stsp
4853 ad89fa31 2019-10-04 stsp return NULL;
4854 ad89fa31 2019-10-04 stsp }
4855 ad89fa31 2019-10-04 stsp
4856 ad89fa31 2019-10-04 stsp static const struct got_error *
4857 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4858 4e759de4 2019-06-26 stsp {
4859 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4860 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4861 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4862 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4863 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4864 4e759de4 2019-06-26 stsp
4865 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4866 ba882ee3 2019-07-11 stsp
4867 4e99b47e 2019-10-04 stsp if (worktree) {
4868 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4869 4e99b47e 2019-10-04 stsp worktree);
4870 4e99b47e 2019-10-04 stsp if (err)
4871 4e99b47e 2019-10-04 stsp return err;
4872 4e99b47e 2019-10-04 stsp
4873 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4874 4e99b47e 2019-10-04 stsp worktree);
4875 4e99b47e 2019-10-04 stsp if (err)
4876 4e99b47e 2019-10-04 stsp return err;
4877 4e99b47e 2019-10-04 stsp
4878 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4879 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4880 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4881 4e99b47e 2019-10-04 stsp if (err)
4882 4e99b47e 2019-10-04 stsp return err;
4883 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4884 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4885 4e99b47e 2019-10-04 stsp }
4886 4e99b47e 2019-10-04 stsp }
4887 4e99b47e 2019-10-04 stsp
4888 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4889 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4890 4e759de4 2019-06-26 stsp if (err)
4891 4e759de4 2019-06-26 stsp return err;
4892 4e759de4 2019-06-26 stsp
4893 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4894 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4895 4e759de4 2019-06-26 stsp
4896 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4897 4e759de4 2019-06-26 stsp return NULL;
4898 4e759de4 2019-06-26 stsp }
4899 4e759de4 2019-06-26 stsp
4900 4e759de4 2019-06-26 stsp static const struct got_error *
4901 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4902 45cd4e47 2019-08-25 stsp const char *branch_name)
4903 4e759de4 2019-06-26 stsp {
4904 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4905 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4906 4e759de4 2019-06-26 stsp char *refname;
4907 4e759de4 2019-06-26 stsp
4908 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4909 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4910 4e759de4 2019-06-26 stsp
4911 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4912 4e759de4 2019-06-26 stsp if (err)
4913 4e759de4 2019-06-26 stsp goto done;
4914 4e759de4 2019-06-26 stsp
4915 45cd4e47 2019-08-25 stsp if (worktree &&
4916 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4917 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4918 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4919 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4920 45cd4e47 2019-08-25 stsp goto done;
4921 45cd4e47 2019-08-25 stsp }
4922 45cd4e47 2019-08-25 stsp
4923 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4924 4e759de4 2019-06-26 stsp done:
4925 45cd4e47 2019-08-25 stsp if (ref)
4926 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4927 4e759de4 2019-06-26 stsp free(refname);
4928 4e759de4 2019-06-26 stsp return err;
4929 4e759de4 2019-06-26 stsp }
4930 4e759de4 2019-06-26 stsp
4931 4e759de4 2019-06-26 stsp static const struct got_error *
4932 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4933 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4934 4e759de4 2019-06-26 stsp {
4935 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4936 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4937 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4938 d3f84d51 2019-07-11 stsp
4939 d3f84d51 2019-07-11 stsp /*
4940 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4941 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4942 d3f84d51 2019-07-11 stsp * an unintended typo.
4943 d3f84d51 2019-07-11 stsp */
4944 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4945 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4946 4e759de4 2019-06-26 stsp
4947 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4948 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4949 4e759de4 2019-06-26 stsp goto done;
4950 4e759de4 2019-06-26 stsp }
4951 4e759de4 2019-06-26 stsp
4952 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4953 4e759de4 2019-06-26 stsp if (err == NULL) {
4954 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4955 4e759de4 2019-06-26 stsp goto done;
4956 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4957 4e759de4 2019-06-26 stsp goto done;
4958 4e759de4 2019-06-26 stsp
4959 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4960 4e759de4 2019-06-26 stsp if (err)
4961 4e759de4 2019-06-26 stsp goto done;
4962 4e759de4 2019-06-26 stsp
4963 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4964 d0eebce4 2019-03-11 stsp done:
4965 4e759de4 2019-06-26 stsp if (ref)
4966 4e759de4 2019-06-26 stsp got_ref_close(ref);
4967 4e759de4 2019-06-26 stsp free(base_refname);
4968 4e759de4 2019-06-26 stsp free(refname);
4969 4e759de4 2019-06-26 stsp return err;
4970 4e759de4 2019-06-26 stsp }
4971 4e759de4 2019-06-26 stsp
4972 4e759de4 2019-06-26 stsp static const struct got_error *
4973 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4974 4e759de4 2019-06-26 stsp {
4975 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4976 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4977 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4978 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4979 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4980 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4981 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4982 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4983 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4984 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4985 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4986 4e759de4 2019-06-26 stsp
4987 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4988 da76fce2 2020-02-24 stsp
4989 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4990 4e759de4 2019-06-26 stsp switch (ch) {
4991 a74f7e83 2019-11-10 stsp case 'c':
4992 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4993 a74f7e83 2019-11-10 stsp break;
4994 4e759de4 2019-06-26 stsp case 'd':
4995 4e759de4 2019-06-26 stsp delref = optarg;
4996 4e759de4 2019-06-26 stsp break;
4997 4e759de4 2019-06-26 stsp case 'r':
4998 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4999 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5000 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5001 9ba1d308 2019-10-21 stsp optarg);
5002 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5003 4e759de4 2019-06-26 stsp break;
5004 4e759de4 2019-06-26 stsp case 'l':
5005 4e759de4 2019-06-26 stsp do_list = 1;
5006 da76fce2 2020-02-24 stsp break;
5007 da76fce2 2020-02-24 stsp case 'n':
5008 da76fce2 2020-02-24 stsp do_update = 0;
5009 4e759de4 2019-06-26 stsp break;
5010 4e759de4 2019-06-26 stsp default:
5011 4e759de4 2019-06-26 stsp usage_branch();
5012 4e759de4 2019-06-26 stsp /* NOTREACHED */
5013 4e759de4 2019-06-26 stsp }
5014 4e759de4 2019-06-26 stsp }
5015 4e759de4 2019-06-26 stsp
5016 4e759de4 2019-06-26 stsp if (do_list && delref)
5017 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5018 4e759de4 2019-06-26 stsp
5019 4e759de4 2019-06-26 stsp argc -= optind;
5020 4e759de4 2019-06-26 stsp argv += optind;
5021 ad89fa31 2019-10-04 stsp
5022 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5023 ad89fa31 2019-10-04 stsp do_show = 1;
5024 4e759de4 2019-06-26 stsp
5025 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5026 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5027 a74f7e83 2019-11-10 stsp
5028 4e759de4 2019-06-26 stsp if (do_list || delref) {
5029 4e759de4 2019-06-26 stsp if (argc > 0)
5030 4e759de4 2019-06-26 stsp usage_branch();
5031 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5032 4e759de4 2019-06-26 stsp usage_branch();
5033 4e759de4 2019-06-26 stsp
5034 4e759de4 2019-06-26 stsp #ifndef PROFILE
5035 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5036 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5037 4e759de4 2019-06-26 stsp NULL) == -1)
5038 4e759de4 2019-06-26 stsp err(1, "pledge");
5039 4e759de4 2019-06-26 stsp } else {
5040 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5041 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5042 4e759de4 2019-06-26 stsp err(1, "pledge");
5043 4e759de4 2019-06-26 stsp }
5044 4e759de4 2019-06-26 stsp #endif
5045 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5046 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5047 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5048 4e759de4 2019-06-26 stsp goto done;
5049 4e759de4 2019-06-26 stsp }
5050 4e759de4 2019-06-26 stsp
5051 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5052 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5053 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5054 4e759de4 2019-06-26 stsp goto done;
5055 4e759de4 2019-06-26 stsp else
5056 4e759de4 2019-06-26 stsp error = NULL;
5057 4e759de4 2019-06-26 stsp if (worktree) {
5058 4e759de4 2019-06-26 stsp repo_path =
5059 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5060 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5061 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5062 4e759de4 2019-06-26 stsp if (error)
5063 4e759de4 2019-06-26 stsp goto done;
5064 4e759de4 2019-06-26 stsp } else {
5065 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5066 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5067 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5068 4e759de4 2019-06-26 stsp goto done;
5069 4e759de4 2019-06-26 stsp }
5070 4e759de4 2019-06-26 stsp }
5071 4e759de4 2019-06-26 stsp }
5072 4e759de4 2019-06-26 stsp
5073 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5074 4e759de4 2019-06-26 stsp if (error != NULL)
5075 4e759de4 2019-06-26 stsp goto done;
5076 4e759de4 2019-06-26 stsp
5077 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5078 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5079 4e759de4 2019-06-26 stsp if (error)
5080 4e759de4 2019-06-26 stsp goto done;
5081 4e759de4 2019-06-26 stsp
5082 ad89fa31 2019-10-04 stsp if (do_show)
5083 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5084 ad89fa31 2019-10-04 stsp else if (do_list)
5085 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5086 4e759de4 2019-06-26 stsp else if (delref)
5087 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5088 4e759de4 2019-06-26 stsp else {
5089 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5090 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5091 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5092 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5093 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5094 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5095 a74f7e83 2019-11-10 stsp if (error)
5096 a74f7e83 2019-11-10 stsp goto done;
5097 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5098 da76fce2 2020-02-24 stsp if (error)
5099 da76fce2 2020-02-24 stsp goto done;
5100 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5101 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5102 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5103 da76fce2 2020-02-24 stsp
5104 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5105 da76fce2 2020-02-24 stsp if (error)
5106 da76fce2 2020-02-24 stsp goto done;
5107 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5108 da76fce2 2020-02-24 stsp worktree);
5109 da76fce2 2020-02-24 stsp if (error)
5110 da76fce2 2020-02-24 stsp goto done;
5111 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5112 da76fce2 2020-02-24 stsp == -1) {
5113 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5114 da76fce2 2020-02-24 stsp goto done;
5115 da76fce2 2020-02-24 stsp }
5116 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5117 da76fce2 2020-02-24 stsp free(branch_refname);
5118 da76fce2 2020-02-24 stsp if (error)
5119 da76fce2 2020-02-24 stsp goto done;
5120 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5121 da76fce2 2020-02-24 stsp repo);
5122 da76fce2 2020-02-24 stsp if (error)
5123 da76fce2 2020-02-24 stsp goto done;
5124 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5125 da76fce2 2020-02-24 stsp commit_id);
5126 da76fce2 2020-02-24 stsp if (error)
5127 da76fce2 2020-02-24 stsp goto done;
5128 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5129 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5130 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5131 9627c110 2020-04-18 stsp NULL);
5132 da76fce2 2020-02-24 stsp if (error)
5133 da76fce2 2020-02-24 stsp goto done;
5134 9627c110 2020-04-18 stsp if (upa.did_something)
5135 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5136 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5137 da76fce2 2020-02-24 stsp }
5138 4e759de4 2019-06-26 stsp }
5139 4e759de4 2019-06-26 stsp done:
5140 da76fce2 2020-02-24 stsp if (ref)
5141 da76fce2 2020-02-24 stsp got_ref_close(ref);
5142 d0eebce4 2019-03-11 stsp if (repo)
5143 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5144 d0eebce4 2019-03-11 stsp if (worktree)
5145 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5146 d0eebce4 2019-03-11 stsp free(cwd);
5147 d0eebce4 2019-03-11 stsp free(repo_path);
5148 da76fce2 2020-02-24 stsp free(commit_id);
5149 da76fce2 2020-02-24 stsp free(commit_id_str);
5150 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5151 da76fce2 2020-02-24 stsp free((char *)pe->path);
5152 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5153 d00136be 2019-03-26 stsp return error;
5154 d00136be 2019-03-26 stsp }
5155 d00136be 2019-03-26 stsp
5156 8e7bd50a 2019-08-22 stsp
5157 d00136be 2019-03-26 stsp __dead static void
5158 8e7bd50a 2019-08-22 stsp usage_tag(void)
5159 8e7bd50a 2019-08-22 stsp {
5160 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5161 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5162 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5163 8e7bd50a 2019-08-22 stsp exit(1);
5164 b8bad2ba 2019-08-23 stsp }
5165 b8bad2ba 2019-08-23 stsp
5166 b8bad2ba 2019-08-23 stsp #if 0
5167 b8bad2ba 2019-08-23 stsp static const struct got_error *
5168 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5169 b8bad2ba 2019-08-23 stsp {
5170 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5171 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5172 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5173 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5174 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5175 b8bad2ba 2019-08-23 stsp
5176 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5177 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5178 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5179 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5180 b8bad2ba 2019-08-23 stsp if (err)
5181 b8bad2ba 2019-08-23 stsp return err;
5182 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5183 b8bad2ba 2019-08-23 stsp continue;
5184 b8bad2ba 2019-08-23 stsp } else {
5185 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5186 b8bad2ba 2019-08-23 stsp if (err)
5187 b8bad2ba 2019-08-23 stsp break;
5188 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5189 b8bad2ba 2019-08-23 stsp free(re_id);
5190 b8bad2ba 2019-08-23 stsp if (err)
5191 b8bad2ba 2019-08-23 stsp break;
5192 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5193 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5194 b8bad2ba 2019-08-23 stsp }
5195 b8bad2ba 2019-08-23 stsp
5196 b8bad2ba 2019-08-23 stsp while (se) {
5197 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5198 b8bad2ba 2019-08-23 stsp if (err)
5199 b8bad2ba 2019-08-23 stsp break;
5200 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5201 b8bad2ba 2019-08-23 stsp free(se_id);
5202 b8bad2ba 2019-08-23 stsp if (err)
5203 b8bad2ba 2019-08-23 stsp break;
5204 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5205 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5206 b8bad2ba 2019-08-23 stsp
5207 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5208 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5209 b8bad2ba 2019-08-23 stsp if (err)
5210 b8bad2ba 2019-08-23 stsp return err;
5211 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5212 b8bad2ba 2019-08-23 stsp break;
5213 b8bad2ba 2019-08-23 stsp }
5214 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5215 b8bad2ba 2019-08-23 stsp continue;
5216 b8bad2ba 2019-08-23 stsp }
5217 b8bad2ba 2019-08-23 stsp }
5218 b8bad2ba 2019-08-23 stsp done:
5219 b8bad2ba 2019-08-23 stsp return err;
5220 8e7bd50a 2019-08-22 stsp }
5221 b8bad2ba 2019-08-23 stsp #endif
5222 b8bad2ba 2019-08-23 stsp
5223 b8bad2ba 2019-08-23 stsp static const struct got_error *
5224 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5225 8e7bd50a 2019-08-22 stsp {
5226 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5227 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5228 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5229 8e7bd50a 2019-08-22 stsp
5230 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5231 8e7bd50a 2019-08-22 stsp
5232 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5233 8e7bd50a 2019-08-22 stsp if (err)
5234 8e7bd50a 2019-08-22 stsp return err;
5235 8e7bd50a 2019-08-22 stsp
5236 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5237 8e7bd50a 2019-08-22 stsp const char *refname;
5238 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5239 8e7bd50a 2019-08-22 stsp char datebuf[26];
5240 d4efa91b 2020-01-14 stsp const char *tagger;
5241 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5242 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5243 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5244 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5245 8e7bd50a 2019-08-22 stsp
5246 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5247 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5248 8e7bd50a 2019-08-22 stsp continue;
5249 8e7bd50a 2019-08-22 stsp refname += 10;
5250 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5251 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5252 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5253 8e7bd50a 2019-08-22 stsp break;
5254 8e7bd50a 2019-08-22 stsp }
5255 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5256 8e7bd50a 2019-08-22 stsp free(refstr);
5257 8e7bd50a 2019-08-22 stsp
5258 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5259 8e7bd50a 2019-08-22 stsp if (err)
5260 8e7bd50a 2019-08-22 stsp break;
5261 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5262 d4efa91b 2020-01-14 stsp if (err) {
5263 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5264 d4efa91b 2020-01-14 stsp free(id);
5265 d4efa91b 2020-01-14 stsp break;
5266 d4efa91b 2020-01-14 stsp }
5267 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5268 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5269 d4efa91b 2020-01-14 stsp if (err) {
5270 d4efa91b 2020-01-14 stsp free(id);
5271 d4efa91b 2020-01-14 stsp break;
5272 d4efa91b 2020-01-14 stsp }
5273 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5274 d4efa91b 2020-01-14 stsp tagger_time =
5275 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5276 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5277 d4efa91b 2020-01-14 stsp free(id);
5278 d4efa91b 2020-01-14 stsp if (err)
5279 d4efa91b 2020-01-14 stsp break;
5280 d4efa91b 2020-01-14 stsp } else {
5281 d4efa91b 2020-01-14 stsp free(id);
5282 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5283 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5284 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5285 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5286 d4efa91b 2020-01-14 stsp if (err)
5287 d4efa91b 2020-01-14 stsp break;
5288 d4efa91b 2020-01-14 stsp }
5289 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5290 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5291 2417344c 2019-08-23 stsp if (datestr)
5292 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5293 d4efa91b 2020-01-14 stsp if (commit)
5294 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5295 d4efa91b 2020-01-14 stsp else {
5296 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5297 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5298 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5299 d4efa91b 2020-01-14 stsp id_str);
5300 d4efa91b 2020-01-14 stsp break;
5301 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5302 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5303 d4efa91b 2020-01-14 stsp id_str);
5304 d4efa91b 2020-01-14 stsp break;
5305 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5306 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5307 d4efa91b 2020-01-14 stsp id_str);
5308 d4efa91b 2020-01-14 stsp break;
5309 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5310 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5311 d4efa91b 2020-01-14 stsp id_str);
5312 d4efa91b 2020-01-14 stsp break;
5313 d4efa91b 2020-01-14 stsp default:
5314 d4efa91b 2020-01-14 stsp break;
5315 d4efa91b 2020-01-14 stsp }
5316 8e7bd50a 2019-08-22 stsp }
5317 8e7bd50a 2019-08-22 stsp free(id_str);
5318 d4efa91b 2020-01-14 stsp if (commit) {
5319 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5320 d4efa91b 2020-01-14 stsp if (err)
5321 d4efa91b 2020-01-14 stsp break;
5322 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5323 d4efa91b 2020-01-14 stsp } else {
5324 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5325 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5326 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5327 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5328 d4efa91b 2020-01-14 stsp break;
5329 d4efa91b 2020-01-14 stsp }
5330 8e7bd50a 2019-08-22 stsp }
5331 8e7bd50a 2019-08-22 stsp
5332 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5333 8e7bd50a 2019-08-22 stsp do {
5334 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5335 8e7bd50a 2019-08-22 stsp if (line)
5336 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5337 8e7bd50a 2019-08-22 stsp } while (line);
5338 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5339 8e7bd50a 2019-08-22 stsp }
5340 8e7bd50a 2019-08-22 stsp
5341 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5342 8e7bd50a 2019-08-22 stsp return NULL;
5343 8e7bd50a 2019-08-22 stsp }
5344 8e7bd50a 2019-08-22 stsp
5345 8e7bd50a 2019-08-22 stsp static const struct got_error *
5346 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5347 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5348 8e7bd50a 2019-08-22 stsp {
5349 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5350 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5351 f372d5cd 2019-10-21 stsp char *editor = NULL;
5352 8e7bd50a 2019-08-22 stsp int fd = -1;
5353 8e7bd50a 2019-08-22 stsp
5354 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5355 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5356 8e7bd50a 2019-08-22 stsp goto done;
5357 8e7bd50a 2019-08-22 stsp }
5358 8e7bd50a 2019-08-22 stsp
5359 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5360 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
5361 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5362 8e7bd50a 2019-08-22 stsp goto done;
5363 8e7bd50a 2019-08-22 stsp }
5364 8e7bd50a 2019-08-22 stsp
5365 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5366 8e7bd50a 2019-08-22 stsp if (err)
5367 8e7bd50a 2019-08-22 stsp goto done;
5368 8e7bd50a 2019-08-22 stsp
5369 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
5370 8e7bd50a 2019-08-22 stsp close(fd);
5371 8e7bd50a 2019-08-22 stsp
5372 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5373 8e7bd50a 2019-08-22 stsp if (err)
5374 8e7bd50a 2019-08-22 stsp goto done;
5375 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5376 8e7bd50a 2019-08-22 stsp done:
5377 8e7bd50a 2019-08-22 stsp free(initial_content);
5378 8e7bd50a 2019-08-22 stsp free(template);
5379 8e7bd50a 2019-08-22 stsp free(editor);
5380 8e7bd50a 2019-08-22 stsp
5381 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5382 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5383 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5384 8e7bd50a 2019-08-22 stsp if (err) {
5385 8e7bd50a 2019-08-22 stsp free(*tagmsg);
5386 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
5387 8e7bd50a 2019-08-22 stsp }
5388 8e7bd50a 2019-08-22 stsp }
5389 8e7bd50a 2019-08-22 stsp return err;
5390 8e7bd50a 2019-08-22 stsp }
5391 8e7bd50a 2019-08-22 stsp
5392 8e7bd50a 2019-08-22 stsp static const struct got_error *
5393 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
5394 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
5395 8e7bd50a 2019-08-22 stsp {
5396 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5397 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5398 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5399 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5400 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5401 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5402 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5403 8e7bd50a 2019-08-22 stsp
5404 8e7bd50a 2019-08-22 stsp /*
5405 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5406 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5407 8e7bd50a 2019-08-22 stsp * an unintended typo.
5408 8e7bd50a 2019-08-22 stsp */
5409 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5410 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5411 8e7bd50a 2019-08-22 stsp
5412 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
5413 8e7bd50a 2019-08-22 stsp if (err)
5414 8e7bd50a 2019-08-22 stsp return err;
5415 8e7bd50a 2019-08-22 stsp
5416 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5417 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5418 8e7bd50a 2019-08-22 stsp if (err)
5419 8e7bd50a 2019-08-22 stsp goto done;
5420 8e7bd50a 2019-08-22 stsp
5421 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5422 8e7bd50a 2019-08-22 stsp if (err)
5423 8e7bd50a 2019-08-22 stsp goto done;
5424 8e7bd50a 2019-08-22 stsp
5425 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5426 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5427 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5428 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5429 8e7bd50a 2019-08-22 stsp goto done;
5430 8e7bd50a 2019-08-22 stsp }
5431 8e7bd50a 2019-08-22 stsp tag_name += 10;
5432 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5433 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5434 8e7bd50a 2019-08-22 stsp goto done;
5435 8e7bd50a 2019-08-22 stsp }
5436 8e7bd50a 2019-08-22 stsp
5437 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5438 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5439 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5440 8e7bd50a 2019-08-22 stsp goto done;
5441 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5442 8e7bd50a 2019-08-22 stsp goto done;
5443 8e7bd50a 2019-08-22 stsp
5444 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5445 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5446 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5447 f372d5cd 2019-10-21 stsp if (err) {
5448 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5449 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5450 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5451 8e7bd50a 2019-08-22 stsp goto done;
5452 f372d5cd 2019-10-21 stsp }
5453 8e7bd50a 2019-08-22 stsp }
5454 8e7bd50a 2019-08-22 stsp
5455 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5456 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5457 f372d5cd 2019-10-21 stsp if (err) {
5458 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5459 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5460 8e7bd50a 2019-08-22 stsp goto done;
5461 f372d5cd 2019-10-21 stsp }
5462 8e7bd50a 2019-08-22 stsp
5463 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5464 f372d5cd 2019-10-21 stsp if (err) {
5465 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5466 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5467 8e7bd50a 2019-08-22 stsp goto done;
5468 f372d5cd 2019-10-21 stsp }
5469 8e7bd50a 2019-08-22 stsp
5470 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5471 f372d5cd 2019-10-21 stsp if (err) {
5472 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5473 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5474 f372d5cd 2019-10-21 stsp goto done;
5475 f372d5cd 2019-10-21 stsp }
5476 8e7bd50a 2019-08-22 stsp
5477 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5478 f372d5cd 2019-10-21 stsp if (err) {
5479 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5480 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5481 f372d5cd 2019-10-21 stsp goto done;
5482 8e7bd50a 2019-08-22 stsp }
5483 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5484 8e7bd50a 2019-08-22 stsp done:
5485 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5486 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5487 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5488 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5489 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5490 f372d5cd 2019-10-21 stsp free(tag_id_str);
5491 8e7bd50a 2019-08-22 stsp if (ref)
5492 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5493 8e7bd50a 2019-08-22 stsp free(commit_id);
5494 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5495 8e7bd50a 2019-08-22 stsp free(refname);
5496 8e7bd50a 2019-08-22 stsp free(tagmsg);
5497 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5498 aba9c984 2019-09-08 stsp free(tagger);
5499 8e7bd50a 2019-08-22 stsp return err;
5500 8e7bd50a 2019-08-22 stsp }
5501 8e7bd50a 2019-08-22 stsp
5502 8e7bd50a 2019-08-22 stsp static const struct got_error *
5503 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5504 8e7bd50a 2019-08-22 stsp {
5505 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5506 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5507 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5508 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5509 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5510 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5511 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5512 8e7bd50a 2019-08-22 stsp
5513 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5514 8e7bd50a 2019-08-22 stsp switch (ch) {
5515 80106605 2020-02-24 stsp case 'c':
5516 80106605 2020-02-24 stsp commit_id_arg = optarg;
5517 80106605 2020-02-24 stsp break;
5518 8e7bd50a 2019-08-22 stsp case 'm':
5519 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5520 8e7bd50a 2019-08-22 stsp break;
5521 8e7bd50a 2019-08-22 stsp case 'r':
5522 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5523 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5524 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5525 9ba1d308 2019-10-21 stsp optarg);
5526 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5527 8e7bd50a 2019-08-22 stsp break;
5528 8e7bd50a 2019-08-22 stsp case 'l':
5529 8e7bd50a 2019-08-22 stsp do_list = 1;
5530 8e7bd50a 2019-08-22 stsp break;
5531 8e7bd50a 2019-08-22 stsp default:
5532 8e7bd50a 2019-08-22 stsp usage_tag();
5533 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5534 8e7bd50a 2019-08-22 stsp }
5535 8e7bd50a 2019-08-22 stsp }
5536 8e7bd50a 2019-08-22 stsp
5537 8e7bd50a 2019-08-22 stsp argc -= optind;
5538 8e7bd50a 2019-08-22 stsp argv += optind;
5539 8e7bd50a 2019-08-22 stsp
5540 8e7bd50a 2019-08-22 stsp if (do_list) {
5541 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5542 775ce909 2020-03-22 stsp errx(1,
5543 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5544 8e7bd50a 2019-08-22 stsp if (tagmsg)
5545 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5546 8e7bd50a 2019-08-22 stsp if (argc > 0)
5547 8e7bd50a 2019-08-22 stsp usage_tag();
5548 80106605 2020-02-24 stsp } else if (argc != 1)
5549 8e7bd50a 2019-08-22 stsp usage_tag();
5550 80106605 2020-02-24 stsp
5551 a2887370 2019-08-23 stsp tag_name = argv[0];
5552 8e7bd50a 2019-08-22 stsp
5553 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5554 8e7bd50a 2019-08-22 stsp if (do_list) {
5555 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5556 8e7bd50a 2019-08-22 stsp NULL) == -1)
5557 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5558 8e7bd50a 2019-08-22 stsp } else {
5559 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5560 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5561 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5562 8e7bd50a 2019-08-22 stsp }
5563 8e7bd50a 2019-08-22 stsp #endif
5564 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5565 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5566 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5567 8e7bd50a 2019-08-22 stsp goto done;
5568 8e7bd50a 2019-08-22 stsp }
5569 8e7bd50a 2019-08-22 stsp
5570 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5571 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5572 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5573 8e7bd50a 2019-08-22 stsp goto done;
5574 8e7bd50a 2019-08-22 stsp else
5575 8e7bd50a 2019-08-22 stsp error = NULL;
5576 8e7bd50a 2019-08-22 stsp if (worktree) {
5577 8e7bd50a 2019-08-22 stsp repo_path =
5578 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5579 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5580 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5581 8e7bd50a 2019-08-22 stsp if (error)
5582 8e7bd50a 2019-08-22 stsp goto done;
5583 8e7bd50a 2019-08-22 stsp } else {
5584 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5585 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5586 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5587 8e7bd50a 2019-08-22 stsp goto done;
5588 8e7bd50a 2019-08-22 stsp }
5589 8e7bd50a 2019-08-22 stsp }
5590 8e7bd50a 2019-08-22 stsp }
5591 8e7bd50a 2019-08-22 stsp
5592 8e7bd50a 2019-08-22 stsp if (do_list) {
5593 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5594 c9956ddf 2019-09-08 stsp if (error != NULL)
5595 c9956ddf 2019-09-08 stsp goto done;
5596 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5597 8e7bd50a 2019-08-22 stsp if (error)
5598 8e7bd50a 2019-08-22 stsp goto done;
5599 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5600 8e7bd50a 2019-08-22 stsp } else {
5601 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5602 c9956ddf 2019-09-08 stsp if (error)
5603 c9956ddf 2019-09-08 stsp goto done;
5604 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5605 c9956ddf 2019-09-08 stsp if (error != NULL)
5606 c9956ddf 2019-09-08 stsp goto done;
5607 c9956ddf 2019-09-08 stsp
5608 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5609 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5610 8e7bd50a 2019-08-22 stsp if (error)
5611 8e7bd50a 2019-08-22 stsp goto done;
5612 8e7bd50a 2019-08-22 stsp }
5613 8e7bd50a 2019-08-22 stsp
5614 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5615 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5616 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5617 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5618 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5619 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5620 8e7bd50a 2019-08-22 stsp if (error)
5621 8e7bd50a 2019-08-22 stsp goto done;
5622 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5623 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5624 8e7bd50a 2019-08-22 stsp if (error)
5625 8e7bd50a 2019-08-22 stsp goto done;
5626 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5627 8e7bd50a 2019-08-22 stsp free(commit_id);
5628 8e7bd50a 2019-08-22 stsp if (error)
5629 8e7bd50a 2019-08-22 stsp goto done;
5630 8e7bd50a 2019-08-22 stsp }
5631 8e7bd50a 2019-08-22 stsp
5632 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5633 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5634 8e7bd50a 2019-08-22 stsp }
5635 8e7bd50a 2019-08-22 stsp done:
5636 8e7bd50a 2019-08-22 stsp if (repo)
5637 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5638 8e7bd50a 2019-08-22 stsp if (worktree)
5639 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5640 8e7bd50a 2019-08-22 stsp free(cwd);
5641 8e7bd50a 2019-08-22 stsp free(repo_path);
5642 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5643 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5644 8e7bd50a 2019-08-22 stsp return error;
5645 8e7bd50a 2019-08-22 stsp }
5646 8e7bd50a 2019-08-22 stsp
5647 8e7bd50a 2019-08-22 stsp __dead static void
5648 d00136be 2019-03-26 stsp usage_add(void)
5649 d00136be 2019-03-26 stsp {
5650 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5651 022fae89 2019-12-06 tracey getprogname());
5652 d00136be 2019-03-26 stsp exit(1);
5653 d00136be 2019-03-26 stsp }
5654 d00136be 2019-03-26 stsp
5655 d00136be 2019-03-26 stsp static const struct got_error *
5656 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5657 4e68cba3 2019-11-23 stsp {
5658 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5659 4e68cba3 2019-11-23 stsp path++;
5660 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5661 4e68cba3 2019-11-23 stsp return NULL;
5662 4e68cba3 2019-11-23 stsp }
5663 4e68cba3 2019-11-23 stsp
5664 4e68cba3 2019-11-23 stsp static const struct got_error *
5665 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5666 d00136be 2019-03-26 stsp {
5667 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5668 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5669 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5670 1dd54920 2019-05-11 stsp char *cwd = NULL;
5671 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5672 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5673 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5674 1dd54920 2019-05-11 stsp
5675 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5676 d00136be 2019-03-26 stsp
5677 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5678 d00136be 2019-03-26 stsp switch (ch) {
5679 022fae89 2019-12-06 tracey case 'I':
5680 022fae89 2019-12-06 tracey no_ignores = 1;
5681 022fae89 2019-12-06 tracey break;
5682 4e68cba3 2019-11-23 stsp case 'R':
5683 4e68cba3 2019-11-23 stsp can_recurse = 1;
5684 4e68cba3 2019-11-23 stsp break;
5685 d00136be 2019-03-26 stsp default:
5686 d00136be 2019-03-26 stsp usage_add();
5687 d00136be 2019-03-26 stsp /* NOTREACHED */
5688 d00136be 2019-03-26 stsp }
5689 d00136be 2019-03-26 stsp }
5690 d00136be 2019-03-26 stsp
5691 d00136be 2019-03-26 stsp argc -= optind;
5692 d00136be 2019-03-26 stsp argv += optind;
5693 d00136be 2019-03-26 stsp
5694 43012d58 2019-07-14 stsp #ifndef PROFILE
5695 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5696 43012d58 2019-07-14 stsp NULL) == -1)
5697 43012d58 2019-07-14 stsp err(1, "pledge");
5698 43012d58 2019-07-14 stsp #endif
5699 723c305c 2019-05-11 jcs if (argc < 1)
5700 d00136be 2019-03-26 stsp usage_add();
5701 d00136be 2019-03-26 stsp
5702 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5703 d00136be 2019-03-26 stsp if (cwd == NULL) {
5704 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5705 d00136be 2019-03-26 stsp goto done;
5706 d00136be 2019-03-26 stsp }
5707 723c305c 2019-05-11 jcs
5708 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5709 fa51e947 2020-03-27 stsp if (error) {
5710 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5711 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
5712 d00136be 2019-03-26 stsp goto done;
5713 fa51e947 2020-03-27 stsp }
5714 d00136be 2019-03-26 stsp
5715 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5716 c9956ddf 2019-09-08 stsp NULL);
5717 031a5338 2019-03-26 stsp if (error != NULL)
5718 031a5338 2019-03-26 stsp goto done;
5719 031a5338 2019-03-26 stsp
5720 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5721 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5722 d00136be 2019-03-26 stsp if (error)
5723 d00136be 2019-03-26 stsp goto done;
5724 d00136be 2019-03-26 stsp
5725 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5726 6d022e97 2019-08-04 stsp if (error)
5727 022fae89 2019-12-06 tracey goto done;
5728 022fae89 2019-12-06 tracey
5729 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5730 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5731 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5732 6d022e97 2019-08-04 stsp goto done;
5733 723c305c 2019-05-11 jcs
5734 022fae89 2019-12-06 tracey }
5735 022fae89 2019-12-06 tracey
5736 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5737 4e68cba3 2019-11-23 stsp char *ondisk_path;
5738 4e68cba3 2019-11-23 stsp struct stat sb;
5739 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5740 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5741 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5742 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5743 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5744 4e68cba3 2019-11-23 stsp goto done;
5745 4e68cba3 2019-11-23 stsp }
5746 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5747 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5748 4e68cba3 2019-11-23 stsp free(ondisk_path);
5749 4e68cba3 2019-11-23 stsp continue;
5750 4e68cba3 2019-11-23 stsp }
5751 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5752 4e68cba3 2019-11-23 stsp ondisk_path);
5753 4e68cba3 2019-11-23 stsp free(ondisk_path);
5754 4e68cba3 2019-11-23 stsp goto done;
5755 4e68cba3 2019-11-23 stsp }
5756 4e68cba3 2019-11-23 stsp free(ondisk_path);
5757 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5758 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5759 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5760 4e68cba3 2019-11-23 stsp goto done;
5761 4e68cba3 2019-11-23 stsp }
5762 4e68cba3 2019-11-23 stsp }
5763 4e68cba3 2019-11-23 stsp }
5764 022fae89 2019-12-06 tracey
5765 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5766 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5767 d00136be 2019-03-26 stsp done:
5768 031a5338 2019-03-26 stsp if (repo)
5769 031a5338 2019-03-26 stsp got_repo_close(repo);
5770 d00136be 2019-03-26 stsp if (worktree)
5771 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5772 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5773 1dd54920 2019-05-11 stsp free((char *)pe->path);
5774 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5775 2ec1f75b 2019-03-26 stsp free(cwd);
5776 2ec1f75b 2019-03-26 stsp return error;
5777 2ec1f75b 2019-03-26 stsp }
5778 2ec1f75b 2019-03-26 stsp
5779 2ec1f75b 2019-03-26 stsp __dead static void
5780 648e4ef7 2019-07-09 stsp usage_remove(void)
5781 2ec1f75b 2019-03-26 stsp {
5782 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5783 f2a9dc41 2019-12-13 tracey getprogname());
5784 2ec1f75b 2019-03-26 stsp exit(1);
5785 2a06fe5f 2019-08-24 stsp }
5786 2a06fe5f 2019-08-24 stsp
5787 2a06fe5f 2019-08-24 stsp static const struct got_error *
5788 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5789 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5790 2a06fe5f 2019-08-24 stsp {
5791 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5792 f2a9dc41 2019-12-13 tracey path++;
5793 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5794 2a06fe5f 2019-08-24 stsp return NULL;
5795 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5796 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5797 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5798 2a06fe5f 2019-08-24 stsp return NULL;
5799 2ec1f75b 2019-03-26 stsp }
5800 2ec1f75b 2019-03-26 stsp
5801 2ec1f75b 2019-03-26 stsp static const struct got_error *
5802 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5803 2ec1f75b 2019-03-26 stsp {
5804 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5805 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5806 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5807 17ed4618 2019-06-02 stsp char *cwd = NULL;
5808 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5809 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5810 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5811 2ec1f75b 2019-03-26 stsp
5812 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5813 17ed4618 2019-06-02 stsp
5814 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5815 2ec1f75b 2019-03-26 stsp switch (ch) {
5816 2ec1f75b 2019-03-26 stsp case 'f':
5817 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5818 2ec1f75b 2019-03-26 stsp break;
5819 70e3e7f5 2019-12-13 tracey case 'k':
5820 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5821 70e3e7f5 2019-12-13 tracey break;
5822 f2a9dc41 2019-12-13 tracey case 'R':
5823 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5824 f2a9dc41 2019-12-13 tracey break;
5825 2ec1f75b 2019-03-26 stsp default:
5826 f2a9dc41 2019-12-13 tracey usage_remove();
5827 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5828 2ec1f75b 2019-03-26 stsp }
5829 2ec1f75b 2019-03-26 stsp }
5830 2ec1f75b 2019-03-26 stsp
5831 2ec1f75b 2019-03-26 stsp argc -= optind;
5832 2ec1f75b 2019-03-26 stsp argv += optind;
5833 2ec1f75b 2019-03-26 stsp
5834 43012d58 2019-07-14 stsp #ifndef PROFILE
5835 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5836 43012d58 2019-07-14 stsp NULL) == -1)
5837 43012d58 2019-07-14 stsp err(1, "pledge");
5838 43012d58 2019-07-14 stsp #endif
5839 17ed4618 2019-06-02 stsp if (argc < 1)
5840 648e4ef7 2019-07-09 stsp usage_remove();
5841 2ec1f75b 2019-03-26 stsp
5842 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5843 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5844 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5845 2ec1f75b 2019-03-26 stsp goto done;
5846 2ec1f75b 2019-03-26 stsp }
5847 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5848 fa51e947 2020-03-27 stsp if (error) {
5849 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5850 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
5851 2ec1f75b 2019-03-26 stsp goto done;
5852 fa51e947 2020-03-27 stsp }
5853 2ec1f75b 2019-03-26 stsp
5854 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5855 c9956ddf 2019-09-08 stsp NULL);
5856 2af4a041 2019-05-11 jcs if (error)
5857 2ec1f75b 2019-03-26 stsp goto done;
5858 2ec1f75b 2019-03-26 stsp
5859 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5860 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5861 2ec1f75b 2019-03-26 stsp if (error)
5862 2ec1f75b 2019-03-26 stsp goto done;
5863 2ec1f75b 2019-03-26 stsp
5864 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5865 6d022e97 2019-08-04 stsp if (error)
5866 6d022e97 2019-08-04 stsp goto done;
5867 17ed4618 2019-06-02 stsp
5868 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5869 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5870 f2a9dc41 2019-12-13 tracey struct stat sb;
5871 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5872 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5873 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5874 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5875 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5876 f2a9dc41 2019-12-13 tracey goto done;
5877 f2a9dc41 2019-12-13 tracey }
5878 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5879 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5880 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5881 f2a9dc41 2019-12-13 tracey continue;
5882 f2a9dc41 2019-12-13 tracey }
5883 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5884 f2a9dc41 2019-12-13 tracey ondisk_path);
5885 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5886 f2a9dc41 2019-12-13 tracey goto done;
5887 f2a9dc41 2019-12-13 tracey }
5888 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5889 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5890 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5891 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5892 f2a9dc41 2019-12-13 tracey goto done;
5893 f2a9dc41 2019-12-13 tracey }
5894 f2a9dc41 2019-12-13 tracey }
5895 f2a9dc41 2019-12-13 tracey }
5896 f2a9dc41 2019-12-13 tracey
5897 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5898 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5899 a129376b 2019-03-28 stsp done:
5900 a129376b 2019-03-28 stsp if (repo)
5901 a129376b 2019-03-28 stsp got_repo_close(repo);
5902 a129376b 2019-03-28 stsp if (worktree)
5903 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5904 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5905 17ed4618 2019-06-02 stsp free((char *)pe->path);
5906 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5907 a129376b 2019-03-28 stsp free(cwd);
5908 a129376b 2019-03-28 stsp return error;
5909 a129376b 2019-03-28 stsp }
5910 a129376b 2019-03-28 stsp
5911 a129376b 2019-03-28 stsp __dead static void
5912 a129376b 2019-03-28 stsp usage_revert(void)
5913 a129376b 2019-03-28 stsp {
5914 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5915 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5916 a129376b 2019-03-28 stsp exit(1);
5917 a129376b 2019-03-28 stsp }
5918 a129376b 2019-03-28 stsp
5919 1ee397ad 2019-07-12 stsp static const struct got_error *
5920 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5921 a129376b 2019-03-28 stsp {
5922 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5923 fb9704af 2020-01-27 stsp return NULL;
5924 fb9704af 2020-01-27 stsp
5925 a129376b 2019-03-28 stsp while (path[0] == '/')
5926 a129376b 2019-03-28 stsp path++;
5927 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5928 33aa809d 2019-08-08 stsp return NULL;
5929 33aa809d 2019-08-08 stsp }
5930 33aa809d 2019-08-08 stsp
5931 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5932 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5933 33aa809d 2019-08-08 stsp const char *action;
5934 33aa809d 2019-08-08 stsp };
5935 33aa809d 2019-08-08 stsp
5936 33aa809d 2019-08-08 stsp static const struct got_error *
5937 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5938 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5939 33aa809d 2019-08-08 stsp {
5940 33aa809d 2019-08-08 stsp char *line = NULL;
5941 33aa809d 2019-08-08 stsp size_t linesize = 0;
5942 33aa809d 2019-08-08 stsp ssize_t linelen;
5943 33aa809d 2019-08-08 stsp
5944 33aa809d 2019-08-08 stsp switch (status) {
5945 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5946 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5947 33aa809d 2019-08-08 stsp break;
5948 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5949 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5950 33aa809d 2019-08-08 stsp break;
5951 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5952 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5953 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5954 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5955 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5956 33aa809d 2019-08-08 stsp printf("%s", line);
5957 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5958 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5959 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5960 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5961 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5962 33aa809d 2019-08-08 stsp break;
5963 33aa809d 2019-08-08 stsp default:
5964 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5965 33aa809d 2019-08-08 stsp }
5966 33aa809d 2019-08-08 stsp
5967 33aa809d 2019-08-08 stsp return NULL;
5968 33aa809d 2019-08-08 stsp }
5969 33aa809d 2019-08-08 stsp
5970 33aa809d 2019-08-08 stsp static const struct got_error *
5971 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5972 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5973 33aa809d 2019-08-08 stsp {
5974 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5975 33aa809d 2019-08-08 stsp char *line = NULL;
5976 33aa809d 2019-08-08 stsp size_t linesize = 0;
5977 33aa809d 2019-08-08 stsp ssize_t linelen;
5978 33aa809d 2019-08-08 stsp int resp = ' ';
5979 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5980 33aa809d 2019-08-08 stsp
5981 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5982 33aa809d 2019-08-08 stsp
5983 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5984 33aa809d 2019-08-08 stsp char *nl;
5985 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5986 33aa809d 2019-08-08 stsp a->action);
5987 33aa809d 2019-08-08 stsp if (err)
5988 33aa809d 2019-08-08 stsp return err;
5989 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5990 33aa809d 2019-08-08 stsp if (linelen == -1) {
5991 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5992 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5993 33aa809d 2019-08-08 stsp return NULL;
5994 33aa809d 2019-08-08 stsp }
5995 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5996 33aa809d 2019-08-08 stsp if (nl)
5997 33aa809d 2019-08-08 stsp *nl = '\0';
5998 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5999 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6000 33aa809d 2019-08-08 stsp printf("y\n");
6001 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6002 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6003 33aa809d 2019-08-08 stsp printf("n\n");
6004 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6005 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6006 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6007 33aa809d 2019-08-08 stsp printf("q\n");
6008 33aa809d 2019-08-08 stsp } else
6009 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6010 33aa809d 2019-08-08 stsp free(line);
6011 33aa809d 2019-08-08 stsp return NULL;
6012 33aa809d 2019-08-08 stsp }
6013 33aa809d 2019-08-08 stsp
6014 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6015 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6016 33aa809d 2019-08-08 stsp a->action);
6017 33aa809d 2019-08-08 stsp if (err)
6018 33aa809d 2019-08-08 stsp return err;
6019 33aa809d 2019-08-08 stsp resp = getchar();
6020 33aa809d 2019-08-08 stsp if (resp == '\n')
6021 33aa809d 2019-08-08 stsp resp = getchar();
6022 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6023 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6024 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6025 33aa809d 2019-08-08 stsp resp = ' ';
6026 33aa809d 2019-08-08 stsp }
6027 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6028 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6029 33aa809d 2019-08-08 stsp resp = ' ';
6030 33aa809d 2019-08-08 stsp }
6031 33aa809d 2019-08-08 stsp }
6032 33aa809d 2019-08-08 stsp
6033 33aa809d 2019-08-08 stsp if (resp == 'y')
6034 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6035 33aa809d 2019-08-08 stsp else if (resp == 'n')
6036 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6037 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6038 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6039 33aa809d 2019-08-08 stsp
6040 1ee397ad 2019-07-12 stsp return NULL;
6041 a129376b 2019-03-28 stsp }
6042 a129376b 2019-03-28 stsp
6043 33aa809d 2019-08-08 stsp
6044 a129376b 2019-03-28 stsp static const struct got_error *
6045 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6046 a129376b 2019-03-28 stsp {
6047 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6048 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6049 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6050 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6051 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6052 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6053 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6054 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6055 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6056 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6057 a129376b 2019-03-28 stsp
6058 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6059 e20a8b6f 2019-06-04 stsp
6060 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6061 a129376b 2019-03-28 stsp switch (ch) {
6062 33aa809d 2019-08-08 stsp case 'p':
6063 33aa809d 2019-08-08 stsp pflag = 1;
6064 33aa809d 2019-08-08 stsp break;
6065 33aa809d 2019-08-08 stsp case 'F':
6066 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6067 33aa809d 2019-08-08 stsp break;
6068 0f6d7415 2019-08-08 stsp case 'R':
6069 0f6d7415 2019-08-08 stsp can_recurse = 1;
6070 0f6d7415 2019-08-08 stsp break;
6071 a129376b 2019-03-28 stsp default:
6072 a129376b 2019-03-28 stsp usage_revert();
6073 a129376b 2019-03-28 stsp /* NOTREACHED */
6074 a129376b 2019-03-28 stsp }
6075 a129376b 2019-03-28 stsp }
6076 a129376b 2019-03-28 stsp
6077 a129376b 2019-03-28 stsp argc -= optind;
6078 a129376b 2019-03-28 stsp argv += optind;
6079 a129376b 2019-03-28 stsp
6080 43012d58 2019-07-14 stsp #ifndef PROFILE
6081 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6082 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6083 43012d58 2019-07-14 stsp err(1, "pledge");
6084 43012d58 2019-07-14 stsp #endif
6085 e20a8b6f 2019-06-04 stsp if (argc < 1)
6086 a129376b 2019-03-28 stsp usage_revert();
6087 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6088 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6089 a129376b 2019-03-28 stsp
6090 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6091 a129376b 2019-03-28 stsp if (cwd == NULL) {
6092 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6093 a129376b 2019-03-28 stsp goto done;
6094 a129376b 2019-03-28 stsp }
6095 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6096 fa51e947 2020-03-27 stsp if (error) {
6097 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6098 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6099 2ec1f75b 2019-03-26 stsp goto done;
6100 fa51e947 2020-03-27 stsp }
6101 a129376b 2019-03-28 stsp
6102 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6103 c9956ddf 2019-09-08 stsp NULL);
6104 a129376b 2019-03-28 stsp if (error != NULL)
6105 a129376b 2019-03-28 stsp goto done;
6106 a129376b 2019-03-28 stsp
6107 33aa809d 2019-08-08 stsp if (patch_script_path) {
6108 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6109 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6110 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6111 33aa809d 2019-08-08 stsp patch_script_path);
6112 33aa809d 2019-08-08 stsp goto done;
6113 33aa809d 2019-08-08 stsp }
6114 33aa809d 2019-08-08 stsp }
6115 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6116 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6117 a129376b 2019-03-28 stsp if (error)
6118 a129376b 2019-03-28 stsp goto done;
6119 a129376b 2019-03-28 stsp
6120 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6121 6d022e97 2019-08-04 stsp if (error)
6122 6d022e97 2019-08-04 stsp goto done;
6123 00db391e 2019-08-03 stsp
6124 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6125 0f6d7415 2019-08-08 stsp char *ondisk_path;
6126 0f6d7415 2019-08-08 stsp struct stat sb;
6127 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6128 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6129 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6130 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6131 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6132 0f6d7415 2019-08-08 stsp goto done;
6133 0f6d7415 2019-08-08 stsp }
6134 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6135 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6136 0f6d7415 2019-08-08 stsp free(ondisk_path);
6137 0f6d7415 2019-08-08 stsp continue;
6138 0f6d7415 2019-08-08 stsp }
6139 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6140 0f6d7415 2019-08-08 stsp ondisk_path);
6141 0f6d7415 2019-08-08 stsp free(ondisk_path);
6142 0f6d7415 2019-08-08 stsp goto done;
6143 0f6d7415 2019-08-08 stsp }
6144 0f6d7415 2019-08-08 stsp free(ondisk_path);
6145 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6146 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6147 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6148 0f6d7415 2019-08-08 stsp goto done;
6149 0f6d7415 2019-08-08 stsp }
6150 0f6d7415 2019-08-08 stsp }
6151 0f6d7415 2019-08-08 stsp }
6152 0f6d7415 2019-08-08 stsp
6153 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6154 33aa809d 2019-08-08 stsp cpa.action = "revert";
6155 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6156 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6157 2ec1f75b 2019-03-26 stsp done:
6158 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6159 33aa809d 2019-08-08 stsp error == NULL)
6160 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6161 2ec1f75b 2019-03-26 stsp if (repo)
6162 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6163 2ec1f75b 2019-03-26 stsp if (worktree)
6164 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6165 2ec1f75b 2019-03-26 stsp free(path);
6166 d00136be 2019-03-26 stsp free(cwd);
6167 6bad629b 2019-02-04 stsp return error;
6168 c4296144 2019-05-09 stsp }
6169 c4296144 2019-05-09 stsp
6170 c4296144 2019-05-09 stsp __dead static void
6171 c4296144 2019-05-09 stsp usage_commit(void)
6172 c4296144 2019-05-09 stsp {
6173 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
6174 5c1e53bc 2019-07-28 stsp getprogname());
6175 c4296144 2019-05-09 stsp exit(1);
6176 33ad4cbe 2019-05-12 jcs }
6177 33ad4cbe 2019-05-12 jcs
6178 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6179 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6180 e2ba3d07 2019-05-13 stsp const char *editor;
6181 e0870e44 2019-05-13 stsp const char *worktree_path;
6182 76d98825 2019-06-03 stsp const char *branch_name;
6183 314a6357 2019-05-13 stsp const char *repo_path;
6184 e0870e44 2019-05-13 stsp char *logmsg_path;
6185 e2ba3d07 2019-05-13 stsp
6186 e2ba3d07 2019-05-13 stsp };
6187 e0870e44 2019-05-13 stsp
6188 33ad4cbe 2019-05-12 jcs static const struct got_error *
6189 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6190 33ad4cbe 2019-05-12 jcs void *arg)
6191 33ad4cbe 2019-05-12 jcs {
6192 76d98825 2019-06-03 stsp char *initial_content = NULL;
6193 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6194 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6195 e0870e44 2019-05-13 stsp char *template = NULL;
6196 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6197 3ce1b845 2019-07-15 stsp int fd;
6198 33ad4cbe 2019-05-12 jcs size_t len;
6199 33ad4cbe 2019-05-12 jcs
6200 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6201 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6202 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6203 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6204 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6205 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6206 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6207 33ad4cbe 2019-05-12 jcs return NULL;
6208 33ad4cbe 2019-05-12 jcs }
6209 33ad4cbe 2019-05-12 jcs
6210 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6211 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6212 76d98825 2019-06-03 stsp
6213 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
6214 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6215 76d98825 2019-06-03 stsp a->branch_name) == -1)
6216 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6217 e0870e44 2019-05-13 stsp
6218 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6219 793c30b5 2019-05-13 stsp if (err)
6220 e0870e44 2019-05-13 stsp goto done;
6221 33ad4cbe 2019-05-12 jcs
6222 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
6223 33ad4cbe 2019-05-12 jcs
6224 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6225 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6226 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6227 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6228 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6229 33ad4cbe 2019-05-12 jcs }
6230 33ad4cbe 2019-05-12 jcs close(fd);
6231 33ad4cbe 2019-05-12 jcs
6232 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6233 33ad4cbe 2019-05-12 jcs done:
6234 76d98825 2019-06-03 stsp free(initial_content);
6235 e0870e44 2019-05-13 stsp free(template);
6236 314a6357 2019-05-13 stsp
6237 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6238 3ce1b845 2019-07-15 stsp if (err == NULL) {
6239 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6240 3ce1b845 2019-07-15 stsp if (err) {
6241 3ce1b845 2019-07-15 stsp free(*logmsg);
6242 3ce1b845 2019-07-15 stsp *logmsg = NULL;
6243 3ce1b845 2019-07-15 stsp }
6244 3ce1b845 2019-07-15 stsp }
6245 33ad4cbe 2019-05-12 jcs return err;
6246 6bad629b 2019-02-04 stsp }
6247 c4296144 2019-05-09 stsp
6248 c4296144 2019-05-09 stsp static const struct got_error *
6249 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6250 c4296144 2019-05-09 stsp {
6251 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6252 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6253 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6254 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6255 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6256 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6257 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6258 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6259 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6260 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6261 5c1e53bc 2019-07-28 stsp
6262 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6263 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6264 c4296144 2019-05-09 stsp
6265 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
6266 c4296144 2019-05-09 stsp switch (ch) {
6267 c4296144 2019-05-09 stsp case 'm':
6268 c4296144 2019-05-09 stsp logmsg = optarg;
6269 c4296144 2019-05-09 stsp break;
6270 c4296144 2019-05-09 stsp default:
6271 c4296144 2019-05-09 stsp usage_commit();
6272 c4296144 2019-05-09 stsp /* NOTREACHED */
6273 c4296144 2019-05-09 stsp }
6274 c4296144 2019-05-09 stsp }
6275 c4296144 2019-05-09 stsp
6276 c4296144 2019-05-09 stsp argc -= optind;
6277 c4296144 2019-05-09 stsp argv += optind;
6278 c4296144 2019-05-09 stsp
6279 43012d58 2019-07-14 stsp #ifndef PROFILE
6280 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6281 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6282 43012d58 2019-07-14 stsp err(1, "pledge");
6283 43012d58 2019-07-14 stsp #endif
6284 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6285 c4296144 2019-05-09 stsp if (cwd == NULL) {
6286 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6287 c4296144 2019-05-09 stsp goto done;
6288 c4296144 2019-05-09 stsp }
6289 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6290 fa51e947 2020-03-27 stsp if (error) {
6291 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6292 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6293 7d5807f4 2019-07-11 stsp goto done;
6294 fa51e947 2020-03-27 stsp }
6295 7d5807f4 2019-07-11 stsp
6296 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6297 c4296144 2019-05-09 stsp if (error)
6298 a698f62e 2019-07-25 stsp goto done;
6299 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6300 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6301 c4296144 2019-05-09 stsp goto done;
6302 a698f62e 2019-07-25 stsp }
6303 5c1e53bc 2019-07-28 stsp
6304 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6305 916f288c 2019-07-30 stsp worktree);
6306 5c1e53bc 2019-07-28 stsp if (error)
6307 5c1e53bc 2019-07-28 stsp goto done;
6308 c4296144 2019-05-09 stsp
6309 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6310 c9956ddf 2019-09-08 stsp if (error)
6311 c9956ddf 2019-09-08 stsp goto done;
6312 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6313 c9956ddf 2019-09-08 stsp gitconfig_path);
6314 c4296144 2019-05-09 stsp if (error != NULL)
6315 c4296144 2019-05-09 stsp goto done;
6316 c4296144 2019-05-09 stsp
6317 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
6318 aba9c984 2019-09-08 stsp if (error)
6319 aba9c984 2019-09-08 stsp return error;
6320 aba9c984 2019-09-08 stsp
6321 314a6357 2019-05-13 stsp /*
6322 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6323 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6324 314a6357 2019-05-13 stsp */
6325 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6326 314a6357 2019-05-13 stsp error = get_editor(&editor);
6327 314a6357 2019-05-13 stsp else
6328 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6329 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6330 c4296144 2019-05-09 stsp if (error)
6331 c4296144 2019-05-09 stsp goto done;
6332 c4296144 2019-05-09 stsp
6333 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6334 087fb88c 2019-08-04 stsp if (error)
6335 087fb88c 2019-08-04 stsp goto done;
6336 087fb88c 2019-08-04 stsp
6337 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6338 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6339 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6340 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6341 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6342 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6343 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6344 916f288c 2019-07-30 stsp goto done;
6345 916f288c 2019-07-30 stsp }
6346 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6347 916f288c 2019-07-30 stsp }
6348 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6349 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6350 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
6351 e0870e44 2019-05-13 stsp if (error) {
6352 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6353 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6354 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6355 c4296144 2019-05-09 stsp goto done;
6356 e0870e44 2019-05-13 stsp }
6357 c4296144 2019-05-09 stsp
6358 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6359 c4296144 2019-05-09 stsp if (error)
6360 c4296144 2019-05-09 stsp goto done;
6361 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6362 c4296144 2019-05-09 stsp done:
6363 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6364 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6365 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6366 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6367 7266f21f 2019-10-21 stsp error == NULL)
6368 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6369 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6370 c4296144 2019-05-09 stsp if (repo)
6371 c4296144 2019-05-09 stsp got_repo_close(repo);
6372 c4296144 2019-05-09 stsp if (worktree)
6373 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6374 c4296144 2019-05-09 stsp free(cwd);
6375 c4296144 2019-05-09 stsp free(id_str);
6376 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6377 e2ba3d07 2019-05-13 stsp free(editor);
6378 aba9c984 2019-09-08 stsp free(author);
6379 234035bc 2019-06-01 stsp return error;
6380 234035bc 2019-06-01 stsp }
6381 234035bc 2019-06-01 stsp
6382 234035bc 2019-06-01 stsp __dead static void
6383 234035bc 2019-06-01 stsp usage_cherrypick(void)
6384 234035bc 2019-06-01 stsp {
6385 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6386 234035bc 2019-06-01 stsp exit(1);
6387 234035bc 2019-06-01 stsp }
6388 234035bc 2019-06-01 stsp
6389 234035bc 2019-06-01 stsp static const struct got_error *
6390 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6391 234035bc 2019-06-01 stsp {
6392 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6393 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6394 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6395 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6396 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6397 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6398 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6399 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6400 9627c110 2020-04-18 stsp int ch;
6401 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6402 234035bc 2019-06-01 stsp
6403 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6404 234035bc 2019-06-01 stsp switch (ch) {
6405 234035bc 2019-06-01 stsp default:
6406 234035bc 2019-06-01 stsp usage_cherrypick();
6407 234035bc 2019-06-01 stsp /* NOTREACHED */
6408 234035bc 2019-06-01 stsp }
6409 234035bc 2019-06-01 stsp }
6410 234035bc 2019-06-01 stsp
6411 234035bc 2019-06-01 stsp argc -= optind;
6412 234035bc 2019-06-01 stsp argv += optind;
6413 234035bc 2019-06-01 stsp
6414 43012d58 2019-07-14 stsp #ifndef PROFILE
6415 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6416 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6417 43012d58 2019-07-14 stsp err(1, "pledge");
6418 43012d58 2019-07-14 stsp #endif
6419 234035bc 2019-06-01 stsp if (argc != 1)
6420 234035bc 2019-06-01 stsp usage_cherrypick();
6421 234035bc 2019-06-01 stsp
6422 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6423 234035bc 2019-06-01 stsp if (cwd == NULL) {
6424 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6425 234035bc 2019-06-01 stsp goto done;
6426 234035bc 2019-06-01 stsp }
6427 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6428 fa51e947 2020-03-27 stsp if (error) {
6429 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6430 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6431 fa51e947 2020-03-27 stsp cwd);
6432 234035bc 2019-06-01 stsp goto done;
6433 fa51e947 2020-03-27 stsp }
6434 234035bc 2019-06-01 stsp
6435 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6436 c9956ddf 2019-09-08 stsp NULL);
6437 234035bc 2019-06-01 stsp if (error != NULL)
6438 234035bc 2019-06-01 stsp goto done;
6439 234035bc 2019-06-01 stsp
6440 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6441 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6442 234035bc 2019-06-01 stsp if (error)
6443 234035bc 2019-06-01 stsp goto done;
6444 234035bc 2019-06-01 stsp
6445 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6446 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6447 234035bc 2019-06-01 stsp if (error != NULL) {
6448 234035bc 2019-06-01 stsp struct got_reference *ref;
6449 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6450 234035bc 2019-06-01 stsp goto done;
6451 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6452 234035bc 2019-06-01 stsp if (error != NULL)
6453 234035bc 2019-06-01 stsp goto done;
6454 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6455 234035bc 2019-06-01 stsp got_ref_close(ref);
6456 234035bc 2019-06-01 stsp if (error != NULL)
6457 234035bc 2019-06-01 stsp goto done;
6458 234035bc 2019-06-01 stsp }
6459 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6460 234035bc 2019-06-01 stsp if (error)
6461 234035bc 2019-06-01 stsp goto done;
6462 234035bc 2019-06-01 stsp
6463 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6464 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6465 234035bc 2019-06-01 stsp if (error != NULL)
6466 234035bc 2019-06-01 stsp goto done;
6467 234035bc 2019-06-01 stsp
6468 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6469 234035bc 2019-06-01 stsp if (error) {
6470 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6471 234035bc 2019-06-01 stsp goto done;
6472 234035bc 2019-06-01 stsp error = NULL;
6473 234035bc 2019-06-01 stsp } else {
6474 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6475 234035bc 2019-06-01 stsp goto done;
6476 234035bc 2019-06-01 stsp }
6477 234035bc 2019-06-01 stsp
6478 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6479 234035bc 2019-06-01 stsp if (error)
6480 234035bc 2019-06-01 stsp goto done;
6481 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6482 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6483 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6484 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
6485 03415a1a 2019-06-02 stsp NULL);
6486 234035bc 2019-06-01 stsp if (error != NULL)
6487 234035bc 2019-06-01 stsp goto done;
6488 234035bc 2019-06-01 stsp
6489 9627c110 2020-04-18 stsp if (upa.did_something)
6490 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6491 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6492 234035bc 2019-06-01 stsp done:
6493 234035bc 2019-06-01 stsp if (commit)
6494 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6495 234035bc 2019-06-01 stsp free(commit_id_str);
6496 234035bc 2019-06-01 stsp if (head_ref)
6497 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6498 234035bc 2019-06-01 stsp if (worktree)
6499 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6500 234035bc 2019-06-01 stsp if (repo)
6501 234035bc 2019-06-01 stsp got_repo_close(repo);
6502 c4296144 2019-05-09 stsp return error;
6503 c4296144 2019-05-09 stsp }
6504 5ef14e63 2019-06-02 stsp
6505 5ef14e63 2019-06-02 stsp __dead static void
6506 5ef14e63 2019-06-02 stsp usage_backout(void)
6507 5ef14e63 2019-06-02 stsp {
6508 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6509 5ef14e63 2019-06-02 stsp exit(1);
6510 5ef14e63 2019-06-02 stsp }
6511 5ef14e63 2019-06-02 stsp
6512 5ef14e63 2019-06-02 stsp static const struct got_error *
6513 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6514 5ef14e63 2019-06-02 stsp {
6515 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6516 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6517 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6518 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6519 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6520 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6521 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6522 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6523 9627c110 2020-04-18 stsp int ch;
6524 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6525 5ef14e63 2019-06-02 stsp
6526 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6527 5ef14e63 2019-06-02 stsp switch (ch) {
6528 5ef14e63 2019-06-02 stsp default:
6529 5ef14e63 2019-06-02 stsp usage_backout();
6530 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6531 5ef14e63 2019-06-02 stsp }
6532 5ef14e63 2019-06-02 stsp }
6533 5ef14e63 2019-06-02 stsp
6534 5ef14e63 2019-06-02 stsp argc -= optind;
6535 5ef14e63 2019-06-02 stsp argv += optind;
6536 5ef14e63 2019-06-02 stsp
6537 43012d58 2019-07-14 stsp #ifndef PROFILE
6538 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6539 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6540 43012d58 2019-07-14 stsp err(1, "pledge");
6541 43012d58 2019-07-14 stsp #endif
6542 5ef14e63 2019-06-02 stsp if (argc != 1)
6543 5ef14e63 2019-06-02 stsp usage_backout();
6544 5ef14e63 2019-06-02 stsp
6545 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6546 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6547 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6548 5ef14e63 2019-06-02 stsp goto done;
6549 5ef14e63 2019-06-02 stsp }
6550 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6551 fa51e947 2020-03-27 stsp if (error) {
6552 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6553 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6554 5ef14e63 2019-06-02 stsp goto done;
6555 fa51e947 2020-03-27 stsp }
6556 5ef14e63 2019-06-02 stsp
6557 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6558 c9956ddf 2019-09-08 stsp NULL);
6559 5ef14e63 2019-06-02 stsp if (error != NULL)
6560 5ef14e63 2019-06-02 stsp goto done;
6561 5ef14e63 2019-06-02 stsp
6562 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6563 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6564 5ef14e63 2019-06-02 stsp if (error)
6565 5ef14e63 2019-06-02 stsp goto done;
6566 5ef14e63 2019-06-02 stsp
6567 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6568 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6569 5ef14e63 2019-06-02 stsp if (error != NULL) {
6570 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6571 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6572 5ef14e63 2019-06-02 stsp goto done;
6573 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6574 5ef14e63 2019-06-02 stsp if (error != NULL)
6575 5ef14e63 2019-06-02 stsp goto done;
6576 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6577 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6578 5ef14e63 2019-06-02 stsp if (error != NULL)
6579 5ef14e63 2019-06-02 stsp goto done;
6580 5ef14e63 2019-06-02 stsp }
6581 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6582 5ef14e63 2019-06-02 stsp if (error)
6583 5ef14e63 2019-06-02 stsp goto done;
6584 5ef14e63 2019-06-02 stsp
6585 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6586 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6587 5ef14e63 2019-06-02 stsp if (error != NULL)
6588 5ef14e63 2019-06-02 stsp goto done;
6589 5ef14e63 2019-06-02 stsp
6590 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6591 5ef14e63 2019-06-02 stsp if (error)
6592 5ef14e63 2019-06-02 stsp goto done;
6593 5ef14e63 2019-06-02 stsp
6594 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6595 5ef14e63 2019-06-02 stsp if (error)
6596 5ef14e63 2019-06-02 stsp goto done;
6597 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6598 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6599 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6600 5ef14e63 2019-06-02 stsp goto done;
6601 5ef14e63 2019-06-02 stsp }
6602 5ef14e63 2019-06-02 stsp
6603 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6604 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6605 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
6606 5ef14e63 2019-06-02 stsp if (error != NULL)
6607 5ef14e63 2019-06-02 stsp goto done;
6608 5ef14e63 2019-06-02 stsp
6609 9627c110 2020-04-18 stsp if (upa.did_something)
6610 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6611 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6612 5ef14e63 2019-06-02 stsp done:
6613 5ef14e63 2019-06-02 stsp if (commit)
6614 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6615 5ef14e63 2019-06-02 stsp free(commit_id_str);
6616 5ef14e63 2019-06-02 stsp if (head_ref)
6617 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6618 818c7501 2019-07-11 stsp if (worktree)
6619 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6620 818c7501 2019-07-11 stsp if (repo)
6621 818c7501 2019-07-11 stsp got_repo_close(repo);
6622 818c7501 2019-07-11 stsp return error;
6623 818c7501 2019-07-11 stsp }
6624 818c7501 2019-07-11 stsp
6625 818c7501 2019-07-11 stsp __dead static void
6626 818c7501 2019-07-11 stsp usage_rebase(void)
6627 818c7501 2019-07-11 stsp {
6628 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6629 af54c8f8 2019-07-11 stsp getprogname());
6630 818c7501 2019-07-11 stsp exit(1);
6631 0ebf8283 2019-07-24 stsp }
6632 0ebf8283 2019-07-24 stsp
6633 0ebf8283 2019-07-24 stsp void
6634 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6635 0ebf8283 2019-07-24 stsp {
6636 0ebf8283 2019-07-24 stsp char *nl;
6637 0ebf8283 2019-07-24 stsp size_t len;
6638 0ebf8283 2019-07-24 stsp
6639 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6640 0ebf8283 2019-07-24 stsp if (len > limit)
6641 0ebf8283 2019-07-24 stsp len = limit;
6642 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6643 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6644 0ebf8283 2019-07-24 stsp if (nl)
6645 0ebf8283 2019-07-24 stsp *nl = '\0';
6646 0ebf8283 2019-07-24 stsp }
6647 0ebf8283 2019-07-24 stsp
6648 0ebf8283 2019-07-24 stsp static const struct got_error *
6649 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6650 0ebf8283 2019-07-24 stsp {
6651 5943eee2 2019-08-13 stsp const struct got_error *err;
6652 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6653 5943eee2 2019-08-13 stsp const char *s;
6654 0ebf8283 2019-07-24 stsp
6655 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6656 5943eee2 2019-08-13 stsp if (err)
6657 5943eee2 2019-08-13 stsp return err;
6658 0ebf8283 2019-07-24 stsp
6659 5943eee2 2019-08-13 stsp s = logmsg0;
6660 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6661 5943eee2 2019-08-13 stsp s++;
6662 5943eee2 2019-08-13 stsp
6663 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6664 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6665 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6666 5943eee2 2019-08-13 stsp goto done;
6667 5943eee2 2019-08-13 stsp }
6668 0ebf8283 2019-07-24 stsp
6669 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6670 5943eee2 2019-08-13 stsp done:
6671 5943eee2 2019-08-13 stsp free(logmsg0);
6672 a0ea4fc0 2020-02-28 stsp return err;
6673 a0ea4fc0 2020-02-28 stsp }
6674 a0ea4fc0 2020-02-28 stsp
6675 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6676 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6677 a0ea4fc0 2020-02-28 stsp {
6678 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6679 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6680 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6681 a0ea4fc0 2020-02-28 stsp
6682 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6683 a0ea4fc0 2020-02-28 stsp if (err)
6684 a0ea4fc0 2020-02-28 stsp return err;
6685 a0ea4fc0 2020-02-28 stsp
6686 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6687 a0ea4fc0 2020-02-28 stsp if (err)
6688 a0ea4fc0 2020-02-28 stsp goto done;
6689 a0ea4fc0 2020-02-28 stsp
6690 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6691 a0ea4fc0 2020-02-28 stsp
6692 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6693 a0ea4fc0 2020-02-28 stsp if (err)
6694 a0ea4fc0 2020-02-28 stsp goto done;
6695 a0ea4fc0 2020-02-28 stsp
6696 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6697 a0ea4fc0 2020-02-28 stsp done:
6698 a0ea4fc0 2020-02-28 stsp free(id_str);
6699 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6700 a0ea4fc0 2020-02-28 stsp free(logmsg);
6701 5943eee2 2019-08-13 stsp return err;
6702 818c7501 2019-07-11 stsp }
6703 818c7501 2019-07-11 stsp
6704 818c7501 2019-07-11 stsp static const struct got_error *
6705 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6706 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6707 818c7501 2019-07-11 stsp {
6708 818c7501 2019-07-11 stsp const struct got_error *err;
6709 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6710 818c7501 2019-07-11 stsp
6711 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6712 818c7501 2019-07-11 stsp if (err)
6713 818c7501 2019-07-11 stsp goto done;
6714 818c7501 2019-07-11 stsp
6715 ff0d2220 2019-07-11 stsp if (new_id) {
6716 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6717 ff0d2220 2019-07-11 stsp if (err)
6718 ff0d2220 2019-07-11 stsp goto done;
6719 ff0d2220 2019-07-11 stsp }
6720 818c7501 2019-07-11 stsp
6721 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6722 ff0d2220 2019-07-11 stsp if (new_id_str)
6723 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6724 0ebf8283 2019-07-24 stsp
6725 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6726 0ebf8283 2019-07-24 stsp if (err)
6727 0ebf8283 2019-07-24 stsp goto done;
6728 0ebf8283 2019-07-24 stsp
6729 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6730 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6731 818c7501 2019-07-11 stsp done:
6732 818c7501 2019-07-11 stsp free(old_id_str);
6733 818c7501 2019-07-11 stsp free(new_id_str);
6734 272a1371 2020-02-28 stsp free(logmsg);
6735 818c7501 2019-07-11 stsp return err;
6736 818c7501 2019-07-11 stsp }
6737 818c7501 2019-07-11 stsp
6738 1ee397ad 2019-07-12 stsp static const struct got_error *
6739 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6740 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6741 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6742 818c7501 2019-07-11 stsp {
6743 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6744 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6745 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6746 818c7501 2019-07-11 stsp }
6747 818c7501 2019-07-11 stsp
6748 818c7501 2019-07-11 stsp static const struct got_error *
6749 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6750 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6751 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6752 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6753 ff0d2220 2019-07-11 stsp {
6754 ff0d2220 2019-07-11 stsp const struct got_error *error;
6755 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6756 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6757 ff0d2220 2019-07-11 stsp
6758 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6759 ff0d2220 2019-07-11 stsp if (error)
6760 ff0d2220 2019-07-11 stsp return error;
6761 ff0d2220 2019-07-11 stsp
6762 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6763 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6764 ff0d2220 2019-07-11 stsp if (error) {
6765 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6766 ff0d2220 2019-07-11 stsp goto done;
6767 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6768 ff0d2220 2019-07-11 stsp } else {
6769 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6770 ff0d2220 2019-07-11 stsp free(new_commit_id);
6771 ff0d2220 2019-07-11 stsp }
6772 ff0d2220 2019-07-11 stsp done:
6773 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6774 ff0d2220 2019-07-11 stsp return error;
6775 64c6d990 2019-07-11 stsp }
6776 64c6d990 2019-07-11 stsp
6777 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6778 64c6d990 2019-07-11 stsp const char *path_prefix;
6779 64c6d990 2019-07-11 stsp size_t len;
6780 8ca9bd68 2019-07-25 stsp int errcode;
6781 64c6d990 2019-07-11 stsp };
6782 64c6d990 2019-07-11 stsp
6783 64c6d990 2019-07-11 stsp static const struct got_error *
6784 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6785 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6786 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6787 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6788 64c6d990 2019-07-11 stsp {
6789 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6790 64c6d990 2019-07-11 stsp
6791 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6792 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6793 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6794 64c6d990 2019-07-11 stsp
6795 64c6d990 2019-07-11 stsp return NULL;
6796 64c6d990 2019-07-11 stsp }
6797 64c6d990 2019-07-11 stsp
6798 64c6d990 2019-07-11 stsp static const struct got_error *
6799 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6800 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6801 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6802 64c6d990 2019-07-11 stsp {
6803 64c6d990 2019-07-11 stsp const struct got_error *err;
6804 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6805 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6806 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6807 64c6d990 2019-07-11 stsp
6808 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6809 64c6d990 2019-07-11 stsp return NULL;
6810 64c6d990 2019-07-11 stsp
6811 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6812 64c6d990 2019-07-11 stsp if (err)
6813 64c6d990 2019-07-11 stsp goto done;
6814 64c6d990 2019-07-11 stsp
6815 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6816 64c6d990 2019-07-11 stsp if (err)
6817 64c6d990 2019-07-11 stsp goto done;
6818 64c6d990 2019-07-11 stsp
6819 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6820 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6821 64c6d990 2019-07-11 stsp if (err)
6822 64c6d990 2019-07-11 stsp goto done;
6823 64c6d990 2019-07-11 stsp
6824 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6825 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6826 64c6d990 2019-07-11 stsp if (err)
6827 64c6d990 2019-07-11 stsp goto done;
6828 64c6d990 2019-07-11 stsp
6829 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6830 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6831 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6832 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6833 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6834 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6835 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6836 64c6d990 2019-07-11 stsp done:
6837 64c6d990 2019-07-11 stsp if (tree1)
6838 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6839 64c6d990 2019-07-11 stsp if (tree2)
6840 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6841 64c6d990 2019-07-11 stsp if (commit)
6842 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6843 64c6d990 2019-07-11 stsp if (parent_commit)
6844 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6845 64c6d990 2019-07-11 stsp return err;
6846 ff0d2220 2019-07-11 stsp }
6847 ff0d2220 2019-07-11 stsp
6848 ff0d2220 2019-07-11 stsp static const struct got_error *
6849 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6850 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6851 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6852 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6853 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6854 af61c510 2019-07-19 stsp {
6855 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6856 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6857 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6858 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6859 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6860 af61c510 2019-07-19 stsp
6861 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6862 af61c510 2019-07-19 stsp if (err)
6863 af61c510 2019-07-19 stsp return err;
6864 af61c510 2019-07-19 stsp
6865 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6866 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6867 af61c510 2019-07-19 stsp if (err)
6868 af61c510 2019-07-19 stsp goto done;
6869 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6870 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6871 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6872 af61c510 2019-07-19 stsp if (err) {
6873 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6874 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6875 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6876 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6877 af61c510 2019-07-19 stsp "been reached?!?");
6878 ee780d5c 2020-01-04 stsp }
6879 ee780d5c 2020-01-04 stsp goto done;
6880 af61c510 2019-07-19 stsp } else {
6881 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6882 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6883 af61c510 2019-07-19 stsp if (err)
6884 af61c510 2019-07-19 stsp goto done;
6885 af61c510 2019-07-19 stsp
6886 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6887 af61c510 2019-07-19 stsp if (err)
6888 af61c510 2019-07-19 stsp goto done;
6889 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6890 af61c510 2019-07-19 stsp commit_id = parent_id;
6891 af61c510 2019-07-19 stsp }
6892 af61c510 2019-07-19 stsp }
6893 af61c510 2019-07-19 stsp done:
6894 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6895 af61c510 2019-07-19 stsp return err;
6896 af61c510 2019-07-19 stsp }
6897 af61c510 2019-07-19 stsp
6898 af61c510 2019-07-19 stsp static const struct got_error *
6899 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6900 818c7501 2019-07-11 stsp {
6901 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6902 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6903 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6904 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6905 818c7501 2019-07-11 stsp char *cwd = NULL;
6906 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6907 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6908 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6909 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6910 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6911 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6912 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6913 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6914 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6915 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6916 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6917 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6918 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6919 818c7501 2019-07-11 stsp
6920 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6921 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6922 818c7501 2019-07-11 stsp
6923 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6924 818c7501 2019-07-11 stsp switch (ch) {
6925 818c7501 2019-07-11 stsp case 'a':
6926 818c7501 2019-07-11 stsp abort_rebase = 1;
6927 818c7501 2019-07-11 stsp break;
6928 818c7501 2019-07-11 stsp case 'c':
6929 818c7501 2019-07-11 stsp continue_rebase = 1;
6930 818c7501 2019-07-11 stsp break;
6931 818c7501 2019-07-11 stsp default:
6932 818c7501 2019-07-11 stsp usage_rebase();
6933 818c7501 2019-07-11 stsp /* NOTREACHED */
6934 818c7501 2019-07-11 stsp }
6935 818c7501 2019-07-11 stsp }
6936 818c7501 2019-07-11 stsp
6937 818c7501 2019-07-11 stsp argc -= optind;
6938 818c7501 2019-07-11 stsp argv += optind;
6939 818c7501 2019-07-11 stsp
6940 43012d58 2019-07-14 stsp #ifndef PROFILE
6941 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6942 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6943 43012d58 2019-07-14 stsp err(1, "pledge");
6944 43012d58 2019-07-14 stsp #endif
6945 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6946 818c7501 2019-07-11 stsp usage_rebase();
6947 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6948 818c7501 2019-07-11 stsp if (argc != 0)
6949 818c7501 2019-07-11 stsp usage_rebase();
6950 818c7501 2019-07-11 stsp } else if (argc != 1)
6951 818c7501 2019-07-11 stsp usage_rebase();
6952 818c7501 2019-07-11 stsp
6953 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6954 818c7501 2019-07-11 stsp if (cwd == NULL) {
6955 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6956 818c7501 2019-07-11 stsp goto done;
6957 818c7501 2019-07-11 stsp }
6958 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6959 fa51e947 2020-03-27 stsp if (error) {
6960 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6961 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
6962 818c7501 2019-07-11 stsp goto done;
6963 fa51e947 2020-03-27 stsp }
6964 818c7501 2019-07-11 stsp
6965 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6966 c9956ddf 2019-09-08 stsp NULL);
6967 818c7501 2019-07-11 stsp if (error != NULL)
6968 818c7501 2019-07-11 stsp goto done;
6969 818c7501 2019-07-11 stsp
6970 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6971 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6972 7ef62c4e 2020-02-24 stsp if (error)
6973 7ef62c4e 2020-02-24 stsp goto done;
6974 7ef62c4e 2020-02-24 stsp
6975 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6976 7ef62c4e 2020-02-24 stsp worktree);
6977 818c7501 2019-07-11 stsp if (error)
6978 7ef62c4e 2020-02-24 stsp goto done;
6979 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6980 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6981 818c7501 2019-07-11 stsp goto done;
6982 7ef62c4e 2020-02-24 stsp }
6983 818c7501 2019-07-11 stsp
6984 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6985 818c7501 2019-07-11 stsp if (error)
6986 818c7501 2019-07-11 stsp goto done;
6987 818c7501 2019-07-11 stsp
6988 f6794adc 2019-07-23 stsp if (abort_rebase) {
6989 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6990 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6991 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6992 f6794adc 2019-07-23 stsp goto done;
6993 f6794adc 2019-07-23 stsp }
6994 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6995 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6996 3e3a69f1 2019-07-25 stsp worktree, repo);
6997 818c7501 2019-07-11 stsp if (error)
6998 818c7501 2019-07-11 stsp goto done;
6999 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7000 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7001 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7002 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7003 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7004 818c7501 2019-07-11 stsp if (error)
7005 818c7501 2019-07-11 stsp goto done;
7006 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7007 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7008 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7009 818c7501 2019-07-11 stsp }
7010 818c7501 2019-07-11 stsp
7011 818c7501 2019-07-11 stsp if (continue_rebase) {
7012 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7013 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7014 f6794adc 2019-07-23 stsp goto done;
7015 f6794adc 2019-07-23 stsp }
7016 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7017 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7018 3e3a69f1 2019-07-25 stsp worktree, repo);
7019 818c7501 2019-07-11 stsp if (error)
7020 818c7501 2019-07-11 stsp goto done;
7021 818c7501 2019-07-11 stsp
7022 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7023 01757395 2019-07-12 stsp resume_commit_id, repo);
7024 818c7501 2019-07-11 stsp if (error)
7025 818c7501 2019-07-11 stsp goto done;
7026 818c7501 2019-07-11 stsp
7027 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7028 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7029 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7030 818c7501 2019-07-11 stsp goto done;
7031 818c7501 2019-07-11 stsp }
7032 818c7501 2019-07-11 stsp } else {
7033 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7034 818c7501 2019-07-11 stsp if (error != NULL)
7035 818c7501 2019-07-11 stsp goto done;
7036 ff0d2220 2019-07-11 stsp }
7037 818c7501 2019-07-11 stsp
7038 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7039 ff0d2220 2019-07-11 stsp if (error)
7040 ff0d2220 2019-07-11 stsp goto done;
7041 ff0d2220 2019-07-11 stsp
7042 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7043 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7044 a51a74b3 2019-07-27 stsp
7045 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7046 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7047 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7048 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7049 818c7501 2019-07-11 stsp if (error)
7050 818c7501 2019-07-11 stsp goto done;
7051 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7052 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7053 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7054 818c7501 2019-07-11 stsp "with work tree's branch");
7055 818c7501 2019-07-11 stsp goto done;
7056 818c7501 2019-07-11 stsp }
7057 818c7501 2019-07-11 stsp
7058 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7059 a51a74b3 2019-07-27 stsp if (error) {
7060 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7061 a51a74b3 2019-07-27 stsp goto done;
7062 a51a74b3 2019-07-27 stsp error = NULL;
7063 a51a74b3 2019-07-27 stsp } else {
7064 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7065 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7066 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7067 a51a74b3 2019-07-27 stsp goto done;
7068 a51a74b3 2019-07-27 stsp }
7069 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7070 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7071 818c7501 2019-07-11 stsp if (error)
7072 818c7501 2019-07-11 stsp goto done;
7073 818c7501 2019-07-11 stsp }
7074 818c7501 2019-07-11 stsp
7075 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7076 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7077 818c7501 2019-07-11 stsp if (error)
7078 818c7501 2019-07-11 stsp goto done;
7079 818c7501 2019-07-11 stsp
7080 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7081 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7082 fc66b545 2019-08-12 stsp if (pid == NULL) {
7083 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7084 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7085 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7086 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7087 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7088 fc66b545 2019-08-12 stsp if (error)
7089 fc66b545 2019-08-12 stsp goto done;
7090 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7091 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7092 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7093 9627c110 2020-04-18 stsp
7094 fc66b545 2019-08-12 stsp }
7095 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7096 fc66b545 2019-08-12 stsp goto done;
7097 fc66b545 2019-08-12 stsp }
7098 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7099 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7100 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7101 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7102 818c7501 2019-07-11 stsp commit = NULL;
7103 818c7501 2019-07-11 stsp if (error)
7104 818c7501 2019-07-11 stsp goto done;
7105 64c6d990 2019-07-11 stsp
7106 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7107 38b0338b 2019-11-29 stsp if (continue_rebase) {
7108 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7109 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7110 38b0338b 2019-11-29 stsp goto done;
7111 38b0338b 2019-11-29 stsp } else {
7112 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7113 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7114 38b0338b 2019-11-29 stsp char *id_str;
7115 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7116 38b0338b 2019-11-29 stsp new_base_branch);
7117 38b0338b 2019-11-29 stsp if (error)
7118 38b0338b 2019-11-29 stsp goto done;
7119 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7120 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7121 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7122 38b0338b 2019-11-29 stsp free(id_str);
7123 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7124 38b0338b 2019-11-29 stsp new_head_commit_id);
7125 38b0338b 2019-11-29 stsp if (error)
7126 38b0338b 2019-11-29 stsp goto done;
7127 38b0338b 2019-11-29 stsp }
7128 818c7501 2019-07-11 stsp }
7129 818c7501 2019-07-11 stsp
7130 818c7501 2019-07-11 stsp pid = NULL;
7131 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7132 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7133 9627c110 2020-04-18 stsp
7134 818c7501 2019-07-11 stsp commit_id = qid->id;
7135 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7136 818c7501 2019-07-11 stsp pid = qid;
7137 818c7501 2019-07-11 stsp
7138 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7139 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7140 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7141 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7142 818c7501 2019-07-11 stsp if (error)
7143 818c7501 2019-07-11 stsp goto done;
7144 9627c110 2020-04-18 stsp
7145 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7146 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7147 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7148 818c7501 2019-07-11 stsp
7149 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7150 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7151 a0ea4fc0 2020-02-28 stsp if (error)
7152 a0ea4fc0 2020-02-28 stsp goto done;
7153 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7154 818c7501 2019-07-11 stsp break;
7155 01757395 2019-07-12 stsp }
7156 818c7501 2019-07-11 stsp
7157 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7158 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7159 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7160 818c7501 2019-07-11 stsp if (error)
7161 818c7501 2019-07-11 stsp goto done;
7162 818c7501 2019-07-11 stsp }
7163 818c7501 2019-07-11 stsp
7164 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7165 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7166 818c7501 2019-07-11 stsp if (error)
7167 818c7501 2019-07-11 stsp goto done;
7168 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7169 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7170 818c7501 2019-07-11 stsp } else
7171 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7172 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7173 818c7501 2019-07-11 stsp done:
7174 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7175 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7176 818c7501 2019-07-11 stsp free(resume_commit_id);
7177 818c7501 2019-07-11 stsp free(yca_id);
7178 818c7501 2019-07-11 stsp if (commit)
7179 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7180 818c7501 2019-07-11 stsp if (branch)
7181 818c7501 2019-07-11 stsp got_ref_close(branch);
7182 818c7501 2019-07-11 stsp if (new_base_branch)
7183 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7184 818c7501 2019-07-11 stsp if (tmp_branch)
7185 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7186 5ef14e63 2019-06-02 stsp if (worktree)
7187 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7188 5ef14e63 2019-06-02 stsp if (repo)
7189 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7190 5ef14e63 2019-06-02 stsp return error;
7191 0ebf8283 2019-07-24 stsp }
7192 0ebf8283 2019-07-24 stsp
7193 0ebf8283 2019-07-24 stsp __dead static void
7194 0ebf8283 2019-07-24 stsp usage_histedit(void)
7195 0ebf8283 2019-07-24 stsp {
7196 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7197 0ebf8283 2019-07-24 stsp getprogname());
7198 0ebf8283 2019-07-24 stsp exit(1);
7199 0ebf8283 2019-07-24 stsp }
7200 0ebf8283 2019-07-24 stsp
7201 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7202 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7203 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7204 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7205 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7206 0ebf8283 2019-07-24 stsp
7207 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7208 0ebf8283 2019-07-24 stsp unsigned char code;
7209 0ebf8283 2019-07-24 stsp const char *name;
7210 0ebf8283 2019-07-24 stsp const char *desc;
7211 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7212 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7213 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7214 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7215 82997472 2020-01-29 stsp "be used" },
7216 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7217 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7218 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7219 0ebf8283 2019-07-24 stsp };
7220 0ebf8283 2019-07-24 stsp
7221 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7222 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7223 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7224 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7225 0ebf8283 2019-07-24 stsp char *logmsg;
7226 0ebf8283 2019-07-24 stsp };
7227 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7228 0ebf8283 2019-07-24 stsp
7229 0ebf8283 2019-07-24 stsp static const struct got_error *
7230 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7231 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7232 0ebf8283 2019-07-24 stsp {
7233 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7234 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7235 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7236 8138f3e1 2019-08-11 stsp int n;
7237 0ebf8283 2019-07-24 stsp
7238 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7239 0ebf8283 2019-07-24 stsp if (err)
7240 0ebf8283 2019-07-24 stsp goto done;
7241 0ebf8283 2019-07-24 stsp
7242 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7243 0ebf8283 2019-07-24 stsp if (err)
7244 0ebf8283 2019-07-24 stsp goto done;
7245 0ebf8283 2019-07-24 stsp
7246 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7247 0ebf8283 2019-07-24 stsp if (err)
7248 0ebf8283 2019-07-24 stsp goto done;
7249 0ebf8283 2019-07-24 stsp
7250 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7251 0ebf8283 2019-07-24 stsp if (n < 0)
7252 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7253 0ebf8283 2019-07-24 stsp done:
7254 0ebf8283 2019-07-24 stsp if (commit)
7255 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7256 0ebf8283 2019-07-24 stsp free(id_str);
7257 0ebf8283 2019-07-24 stsp free(logmsg);
7258 0ebf8283 2019-07-24 stsp return err;
7259 0ebf8283 2019-07-24 stsp }
7260 0ebf8283 2019-07-24 stsp
7261 0ebf8283 2019-07-24 stsp static const struct got_error *
7262 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7263 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7264 0ebf8283 2019-07-24 stsp {
7265 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7266 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7267 0ebf8283 2019-07-24 stsp
7268 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7269 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7270 0ebf8283 2019-07-24 stsp
7271 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7272 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7273 0ebf8283 2019-07-24 stsp f, repo);
7274 0ebf8283 2019-07-24 stsp if (err)
7275 0ebf8283 2019-07-24 stsp break;
7276 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7277 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7278 083957f4 2020-02-24 stsp if (n < 0) {
7279 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7280 083957f4 2020-02-24 stsp break;
7281 083957f4 2020-02-24 stsp }
7282 083957f4 2020-02-24 stsp }
7283 0ebf8283 2019-07-24 stsp }
7284 0ebf8283 2019-07-24 stsp
7285 0ebf8283 2019-07-24 stsp return err;
7286 0ebf8283 2019-07-24 stsp }
7287 0ebf8283 2019-07-24 stsp
7288 0ebf8283 2019-07-24 stsp static const struct got_error *
7289 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7290 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7291 0ebf8283 2019-07-24 stsp {
7292 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7293 0ebf8283 2019-07-24 stsp int n, i;
7294 514f2ffe 2020-01-29 stsp char *id_str;
7295 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7296 514f2ffe 2020-01-29 stsp
7297 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7298 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7299 514f2ffe 2020-01-29 stsp if (err)
7300 514f2ffe 2020-01-29 stsp return err;
7301 514f2ffe 2020-01-29 stsp
7302 514f2ffe 2020-01-29 stsp n = fprintf(f,
7303 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7304 514f2ffe 2020-01-29 stsp "# commit %s\n"
7305 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7306 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7307 514f2ffe 2020-01-29 stsp if (n < 0) {
7308 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7309 514f2ffe 2020-01-29 stsp goto done;
7310 514f2ffe 2020-01-29 stsp }
7311 0ebf8283 2019-07-24 stsp
7312 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7313 514f2ffe 2020-01-29 stsp if (n < 0) {
7314 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7315 514f2ffe 2020-01-29 stsp goto done;
7316 514f2ffe 2020-01-29 stsp }
7317 0ebf8283 2019-07-24 stsp
7318 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7319 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7320 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7321 0ebf8283 2019-07-24 stsp cmd->desc);
7322 0ebf8283 2019-07-24 stsp if (n < 0) {
7323 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7324 0ebf8283 2019-07-24 stsp break;
7325 0ebf8283 2019-07-24 stsp }
7326 0ebf8283 2019-07-24 stsp }
7327 514f2ffe 2020-01-29 stsp done:
7328 514f2ffe 2020-01-29 stsp free(id_str);
7329 0ebf8283 2019-07-24 stsp return err;
7330 0ebf8283 2019-07-24 stsp }
7331 0ebf8283 2019-07-24 stsp
7332 0ebf8283 2019-07-24 stsp static const struct got_error *
7333 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7334 0ebf8283 2019-07-24 stsp {
7335 0ebf8283 2019-07-24 stsp static char msg[42];
7336 0ebf8283 2019-07-24 stsp int ret;
7337 0ebf8283 2019-07-24 stsp
7338 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7339 0ebf8283 2019-07-24 stsp lineno);
7340 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7341 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7342 0ebf8283 2019-07-24 stsp
7343 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7344 0ebf8283 2019-07-24 stsp }
7345 0ebf8283 2019-07-24 stsp
7346 0ebf8283 2019-07-24 stsp static const struct got_error *
7347 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7348 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7349 0ebf8283 2019-07-24 stsp {
7350 0ebf8283 2019-07-24 stsp const struct got_error *err;
7351 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7352 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7353 0ebf8283 2019-07-24 stsp
7354 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7355 0ebf8283 2019-07-24 stsp if (err)
7356 0ebf8283 2019-07-24 stsp return err;
7357 0ebf8283 2019-07-24 stsp
7358 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7359 0ebf8283 2019-07-24 stsp if (err)
7360 0ebf8283 2019-07-24 stsp goto done;
7361 0ebf8283 2019-07-24 stsp
7362 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7363 5943eee2 2019-08-13 stsp if (err)
7364 5943eee2 2019-08-13 stsp goto done;
7365 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7366 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7367 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7368 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7369 0ebf8283 2019-07-24 stsp }
7370 0ebf8283 2019-07-24 stsp done:
7371 0ebf8283 2019-07-24 stsp if (folded_commit)
7372 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7373 0ebf8283 2019-07-24 stsp free(id_str);
7374 5943eee2 2019-08-13 stsp free(folded_logmsg);
7375 0ebf8283 2019-07-24 stsp return err;
7376 0ebf8283 2019-07-24 stsp }
7377 0ebf8283 2019-07-24 stsp
7378 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7379 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7380 0ebf8283 2019-07-24 stsp {
7381 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7382 0ebf8283 2019-07-24 stsp
7383 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7384 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7385 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7386 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7387 3f9de99f 2019-07-24 stsp folded = prev;
7388 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7389 0ebf8283 2019-07-24 stsp }
7390 0ebf8283 2019-07-24 stsp
7391 0ebf8283 2019-07-24 stsp return folded;
7392 0ebf8283 2019-07-24 stsp }
7393 0ebf8283 2019-07-24 stsp
7394 0ebf8283 2019-07-24 stsp static const struct got_error *
7395 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7396 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7397 0ebf8283 2019-07-24 stsp {
7398 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7399 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7400 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7401 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7402 0ebf8283 2019-07-24 stsp int fd;
7403 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7404 0ebf8283 2019-07-24 stsp
7405 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7406 0ebf8283 2019-07-24 stsp if (err)
7407 0ebf8283 2019-07-24 stsp return err;
7408 0ebf8283 2019-07-24 stsp
7409 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7410 0ebf8283 2019-07-24 stsp if (folded) {
7411 0ebf8283 2019-07-24 stsp while (folded != hle) {
7412 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7413 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7414 3f9de99f 2019-07-24 stsp continue;
7415 3f9de99f 2019-07-24 stsp }
7416 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7417 0ebf8283 2019-07-24 stsp logmsg, repo);
7418 0ebf8283 2019-07-24 stsp if (err)
7419 0ebf8283 2019-07-24 stsp goto done;
7420 0ebf8283 2019-07-24 stsp free(logmsg);
7421 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7422 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7423 0ebf8283 2019-07-24 stsp }
7424 0ebf8283 2019-07-24 stsp }
7425 0ebf8283 2019-07-24 stsp
7426 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7427 0ebf8283 2019-07-24 stsp if (err)
7428 0ebf8283 2019-07-24 stsp goto done;
7429 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7430 5943eee2 2019-08-13 stsp if (err)
7431 5943eee2 2019-08-13 stsp goto done;
7432 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
7433 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7434 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7435 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7436 0ebf8283 2019-07-24 stsp goto done;
7437 0ebf8283 2019-07-24 stsp }
7438 0ebf8283 2019-07-24 stsp free(logmsg);
7439 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7440 0ebf8283 2019-07-24 stsp
7441 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7442 0ebf8283 2019-07-24 stsp if (err)
7443 0ebf8283 2019-07-24 stsp goto done;
7444 0ebf8283 2019-07-24 stsp
7445 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7446 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7447 0ebf8283 2019-07-24 stsp if (err)
7448 0ebf8283 2019-07-24 stsp goto done;
7449 0ebf8283 2019-07-24 stsp
7450 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
7451 0ebf8283 2019-07-24 stsp close(fd);
7452 0ebf8283 2019-07-24 stsp
7453 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7454 0ebf8283 2019-07-24 stsp if (err)
7455 0ebf8283 2019-07-24 stsp goto done;
7456 0ebf8283 2019-07-24 stsp
7457 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7458 0ebf8283 2019-07-24 stsp if (err) {
7459 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7460 0ebf8283 2019-07-24 stsp goto done;
7461 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7462 0ebf8283 2019-07-24 stsp }
7463 0ebf8283 2019-07-24 stsp done:
7464 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7465 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7466 0ebf8283 2019-07-24 stsp free(logmsg_path);
7467 0ebf8283 2019-07-24 stsp free(logmsg);
7468 5943eee2 2019-08-13 stsp free(orig_logmsg);
7469 0ebf8283 2019-07-24 stsp free(editor);
7470 0ebf8283 2019-07-24 stsp if (commit)
7471 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7472 0ebf8283 2019-07-24 stsp return err;
7473 5ef14e63 2019-06-02 stsp }
7474 0ebf8283 2019-07-24 stsp
7475 0ebf8283 2019-07-24 stsp static const struct got_error *
7476 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7477 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7478 0ebf8283 2019-07-24 stsp {
7479 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7480 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7481 0ebf8283 2019-07-24 stsp size_t size;
7482 0ebf8283 2019-07-24 stsp ssize_t len;
7483 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7484 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7485 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7486 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7487 0ebf8283 2019-07-24 stsp
7488 0ebf8283 2019-07-24 stsp for (;;) {
7489 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7490 0ebf8283 2019-07-24 stsp if (len == -1) {
7491 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7492 0ebf8283 2019-07-24 stsp if (feof(f))
7493 0ebf8283 2019-07-24 stsp break;
7494 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7495 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7496 0ebf8283 2019-07-24 stsp break;
7497 0ebf8283 2019-07-24 stsp }
7498 0ebf8283 2019-07-24 stsp lineno++;
7499 0ebf8283 2019-07-24 stsp p = line;
7500 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7501 0ebf8283 2019-07-24 stsp p++;
7502 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7503 0ebf8283 2019-07-24 stsp free(line);
7504 0ebf8283 2019-07-24 stsp line = NULL;
7505 0ebf8283 2019-07-24 stsp continue;
7506 0ebf8283 2019-07-24 stsp }
7507 0ebf8283 2019-07-24 stsp cmd = NULL;
7508 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7509 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7510 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7511 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7512 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7513 0ebf8283 2019-07-24 stsp break;
7514 0ebf8283 2019-07-24 stsp }
7515 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7516 0ebf8283 2019-07-24 stsp p++;
7517 0ebf8283 2019-07-24 stsp break;
7518 0ebf8283 2019-07-24 stsp }
7519 0ebf8283 2019-07-24 stsp }
7520 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7521 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7522 0ebf8283 2019-07-24 stsp break;
7523 0ebf8283 2019-07-24 stsp }
7524 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7525 0ebf8283 2019-07-24 stsp p++;
7526 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7527 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7528 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7529 0ebf8283 2019-07-24 stsp break;
7530 0ebf8283 2019-07-24 stsp }
7531 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7532 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7533 0ebf8283 2019-07-24 stsp if (err)
7534 0ebf8283 2019-07-24 stsp break;
7535 0ebf8283 2019-07-24 stsp } else {
7536 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7537 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7538 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7539 0ebf8283 2019-07-24 stsp break;
7540 0ebf8283 2019-07-24 stsp }
7541 0ebf8283 2019-07-24 stsp }
7542 0ebf8283 2019-07-24 stsp free(line);
7543 0ebf8283 2019-07-24 stsp line = NULL;
7544 0ebf8283 2019-07-24 stsp continue;
7545 0ebf8283 2019-07-24 stsp } else {
7546 0ebf8283 2019-07-24 stsp end = p;
7547 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7548 0ebf8283 2019-07-24 stsp end++;
7549 0ebf8283 2019-07-24 stsp *end = '\0';
7550 0ebf8283 2019-07-24 stsp
7551 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7552 0ebf8283 2019-07-24 stsp if (err) {
7553 0ebf8283 2019-07-24 stsp /* override error code */
7554 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7555 0ebf8283 2019-07-24 stsp break;
7556 0ebf8283 2019-07-24 stsp }
7557 0ebf8283 2019-07-24 stsp }
7558 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7559 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7560 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7561 0ebf8283 2019-07-24 stsp break;
7562 0ebf8283 2019-07-24 stsp }
7563 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7564 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7565 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7566 0ebf8283 2019-07-24 stsp commit_id = NULL;
7567 0ebf8283 2019-07-24 stsp free(line);
7568 0ebf8283 2019-07-24 stsp line = NULL;
7569 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7570 0ebf8283 2019-07-24 stsp }
7571 0ebf8283 2019-07-24 stsp
7572 0ebf8283 2019-07-24 stsp free(line);
7573 0ebf8283 2019-07-24 stsp free(commit_id);
7574 0ebf8283 2019-07-24 stsp return err;
7575 0ebf8283 2019-07-24 stsp }
7576 0ebf8283 2019-07-24 stsp
7577 0ebf8283 2019-07-24 stsp static const struct got_error *
7578 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7579 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7580 bfce7f83 2019-07-27 stsp {
7581 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7582 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7583 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7584 5b87815e 2020-03-05 stsp static char msg[92];
7585 bfce7f83 2019-07-27 stsp char *id_str;
7586 bfce7f83 2019-07-27 stsp
7587 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7588 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7589 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7590 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7591 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7592 5b87815e 2020-03-05 stsp
7593 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7594 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7595 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7596 5b87815e 2020-03-05 stsp if (hle == hle2)
7597 5b87815e 2020-03-05 stsp continue;
7598 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7599 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7600 5b87815e 2020-03-05 stsp continue;
7601 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7602 5b87815e 2020-03-05 stsp if (err)
7603 5b87815e 2020-03-05 stsp return err;
7604 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7605 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7606 5b87815e 2020-03-05 stsp free(id_str);
7607 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7608 5b87815e 2020-03-05 stsp }
7609 5b87815e 2020-03-05 stsp }
7610 bfce7f83 2019-07-27 stsp
7611 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7612 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7613 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7614 bfce7f83 2019-07-27 stsp break;
7615 bfce7f83 2019-07-27 stsp }
7616 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7617 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7618 bfce7f83 2019-07-27 stsp if (err)
7619 bfce7f83 2019-07-27 stsp return err;
7620 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7621 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7622 bfce7f83 2019-07-27 stsp free(id_str);
7623 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7624 bfce7f83 2019-07-27 stsp }
7625 bfce7f83 2019-07-27 stsp }
7626 bfce7f83 2019-07-27 stsp
7627 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7628 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7629 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7630 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7631 bfce7f83 2019-07-27 stsp
7632 bfce7f83 2019-07-27 stsp return NULL;
7633 bfce7f83 2019-07-27 stsp }
7634 bfce7f83 2019-07-27 stsp
7635 bfce7f83 2019-07-27 stsp static const struct got_error *
7636 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7637 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7638 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7639 0ebf8283 2019-07-24 stsp {
7640 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7641 0ebf8283 2019-07-24 stsp char *editor;
7642 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7643 0ebf8283 2019-07-24 stsp
7644 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7645 0ebf8283 2019-07-24 stsp if (err)
7646 0ebf8283 2019-07-24 stsp return err;
7647 0ebf8283 2019-07-24 stsp
7648 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7649 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7650 0ebf8283 2019-07-24 stsp goto done;
7651 0ebf8283 2019-07-24 stsp }
7652 0ebf8283 2019-07-24 stsp
7653 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7654 0ebf8283 2019-07-24 stsp if (f == NULL) {
7655 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7656 0ebf8283 2019-07-24 stsp goto done;
7657 0ebf8283 2019-07-24 stsp }
7658 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7659 bfce7f83 2019-07-27 stsp if (err)
7660 bfce7f83 2019-07-27 stsp goto done;
7661 bfce7f83 2019-07-27 stsp
7662 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7663 0ebf8283 2019-07-24 stsp done:
7664 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7665 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7666 0ebf8283 2019-07-24 stsp free(editor);
7667 0ebf8283 2019-07-24 stsp return err;
7668 0ebf8283 2019-07-24 stsp }
7669 0ebf8283 2019-07-24 stsp
7670 0ebf8283 2019-07-24 stsp static const struct got_error *
7671 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7672 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7673 514f2ffe 2020-01-29 stsp struct got_repository *);
7674 0ebf8283 2019-07-24 stsp
7675 0ebf8283 2019-07-24 stsp static const struct got_error *
7676 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7677 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7678 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7679 0ebf8283 2019-07-24 stsp {
7680 0ebf8283 2019-07-24 stsp const struct got_error *err;
7681 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7682 0ebf8283 2019-07-24 stsp char *path = NULL;
7683 0ebf8283 2019-07-24 stsp
7684 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7685 0ebf8283 2019-07-24 stsp if (err)
7686 0ebf8283 2019-07-24 stsp return err;
7687 0ebf8283 2019-07-24 stsp
7688 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7689 0ebf8283 2019-07-24 stsp if (err)
7690 0ebf8283 2019-07-24 stsp goto done;
7691 0ebf8283 2019-07-24 stsp
7692 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7693 0ebf8283 2019-07-24 stsp if (err)
7694 0ebf8283 2019-07-24 stsp goto done;
7695 0ebf8283 2019-07-24 stsp
7696 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7697 083957f4 2020-02-24 stsp rewind(f);
7698 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7699 083957f4 2020-02-24 stsp } else {
7700 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7701 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7702 0ebf8283 2019-07-24 stsp goto done;
7703 083957f4 2020-02-24 stsp }
7704 083957f4 2020-02-24 stsp f = NULL;
7705 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7706 083957f4 2020-02-24 stsp if (err) {
7707 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7708 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7709 083957f4 2020-02-24 stsp goto done;
7710 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7711 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7712 083957f4 2020-02-24 stsp }
7713 0ebf8283 2019-07-24 stsp }
7714 0ebf8283 2019-07-24 stsp done:
7715 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7716 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7717 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7718 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7719 0ebf8283 2019-07-24 stsp free(path);
7720 0ebf8283 2019-07-24 stsp return err;
7721 0ebf8283 2019-07-24 stsp }
7722 0ebf8283 2019-07-24 stsp
7723 0ebf8283 2019-07-24 stsp static const struct got_error *
7724 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7725 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7726 0ebf8283 2019-07-24 stsp {
7727 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7728 0ebf8283 2019-07-24 stsp char *path = NULL;
7729 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7730 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7731 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7732 0ebf8283 2019-07-24 stsp
7733 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7734 0ebf8283 2019-07-24 stsp if (err)
7735 0ebf8283 2019-07-24 stsp return err;
7736 0ebf8283 2019-07-24 stsp
7737 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7738 0ebf8283 2019-07-24 stsp if (f == NULL) {
7739 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7740 0ebf8283 2019-07-24 stsp goto done;
7741 0ebf8283 2019-07-24 stsp }
7742 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7743 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7744 0ebf8283 2019-07-24 stsp repo);
7745 0ebf8283 2019-07-24 stsp if (err)
7746 0ebf8283 2019-07-24 stsp break;
7747 0ebf8283 2019-07-24 stsp
7748 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7749 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7750 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7751 0ebf8283 2019-07-24 stsp if (n < 0) {
7752 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7753 0ebf8283 2019-07-24 stsp break;
7754 0ebf8283 2019-07-24 stsp }
7755 0ebf8283 2019-07-24 stsp }
7756 0ebf8283 2019-07-24 stsp }
7757 0ebf8283 2019-07-24 stsp done:
7758 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7759 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7760 0ebf8283 2019-07-24 stsp free(path);
7761 0ebf8283 2019-07-24 stsp if (commit)
7762 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7763 0ebf8283 2019-07-24 stsp return err;
7764 0ebf8283 2019-07-24 stsp }
7765 0ebf8283 2019-07-24 stsp
7766 bfce7f83 2019-07-27 stsp void
7767 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7768 bfce7f83 2019-07-27 stsp {
7769 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7770 bfce7f83 2019-07-27 stsp
7771 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7772 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7773 bfce7f83 2019-07-27 stsp free(hle);
7774 bfce7f83 2019-07-27 stsp }
7775 bfce7f83 2019-07-27 stsp }
7776 bfce7f83 2019-07-27 stsp
7777 0ebf8283 2019-07-24 stsp static const struct got_error *
7778 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7779 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7780 0ebf8283 2019-07-24 stsp {
7781 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7782 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7783 0ebf8283 2019-07-24 stsp
7784 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7785 0ebf8283 2019-07-24 stsp if (f == NULL) {
7786 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7787 0ebf8283 2019-07-24 stsp goto done;
7788 0ebf8283 2019-07-24 stsp }
7789 0ebf8283 2019-07-24 stsp
7790 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7791 0ebf8283 2019-07-24 stsp done:
7792 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7793 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7794 0ebf8283 2019-07-24 stsp return err;
7795 0ebf8283 2019-07-24 stsp }
7796 0ebf8283 2019-07-24 stsp
7797 0ebf8283 2019-07-24 stsp static const struct got_error *
7798 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7799 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7800 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7801 0ebf8283 2019-07-24 stsp {
7802 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7803 0ebf8283 2019-07-24 stsp int resp = ' ';
7804 0ebf8283 2019-07-24 stsp
7805 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7806 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7807 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7808 0ebf8283 2019-07-24 stsp resp = getchar();
7809 a61a4414 2019-08-07 stsp if (resp == '\n')
7810 a61a4414 2019-08-07 stsp resp = getchar();
7811 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7812 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7813 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7814 bfce7f83 2019-07-27 stsp repo);
7815 426ebf2e 2019-07-25 stsp if (err) {
7816 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7817 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7818 426ebf2e 2019-07-25 stsp break;
7819 bfce7f83 2019-07-27 stsp prev_err = err;
7820 426ebf2e 2019-07-25 stsp resp = ' ';
7821 426ebf2e 2019-07-25 stsp continue;
7822 426ebf2e 2019-07-25 stsp }
7823 bfce7f83 2019-07-27 stsp break;
7824 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7825 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7826 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7827 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7828 426ebf2e 2019-07-25 stsp if (err) {
7829 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7830 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7831 426ebf2e 2019-07-25 stsp break;
7832 bfce7f83 2019-07-27 stsp prev_err = err;
7833 426ebf2e 2019-07-25 stsp resp = ' ';
7834 426ebf2e 2019-07-25 stsp continue;
7835 426ebf2e 2019-07-25 stsp }
7836 bfce7f83 2019-07-27 stsp break;
7837 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7838 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7839 0ebf8283 2019-07-24 stsp break;
7840 426ebf2e 2019-07-25 stsp } else
7841 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7842 0ebf8283 2019-07-24 stsp }
7843 0ebf8283 2019-07-24 stsp
7844 0ebf8283 2019-07-24 stsp return err;
7845 0ebf8283 2019-07-24 stsp }
7846 0ebf8283 2019-07-24 stsp
7847 0ebf8283 2019-07-24 stsp static const struct got_error *
7848 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7849 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7850 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7851 0ebf8283 2019-07-24 stsp {
7852 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7853 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7854 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7855 3e3a69f1 2019-07-25 stsp branch, repo);
7856 0ebf8283 2019-07-24 stsp }
7857 0ebf8283 2019-07-24 stsp
7858 0ebf8283 2019-07-24 stsp static const struct got_error *
7859 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7860 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7861 0ebf8283 2019-07-24 stsp {
7862 0ebf8283 2019-07-24 stsp const struct got_error *err;
7863 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7864 0ebf8283 2019-07-24 stsp
7865 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7866 0ebf8283 2019-07-24 stsp if (err)
7867 0ebf8283 2019-07-24 stsp goto done;
7868 0ebf8283 2019-07-24 stsp
7869 0ebf8283 2019-07-24 stsp if (new_id) {
7870 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7871 0ebf8283 2019-07-24 stsp if (err)
7872 0ebf8283 2019-07-24 stsp goto done;
7873 0ebf8283 2019-07-24 stsp }
7874 0ebf8283 2019-07-24 stsp
7875 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7876 0ebf8283 2019-07-24 stsp if (new_id_str)
7877 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7878 0ebf8283 2019-07-24 stsp
7879 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7880 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7881 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7882 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7883 0ebf8283 2019-07-24 stsp goto done;
7884 0ebf8283 2019-07-24 stsp }
7885 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7886 0ebf8283 2019-07-24 stsp } else {
7887 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7888 0ebf8283 2019-07-24 stsp if (err)
7889 0ebf8283 2019-07-24 stsp goto done;
7890 0ebf8283 2019-07-24 stsp }
7891 0ebf8283 2019-07-24 stsp
7892 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7893 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7894 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7895 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7896 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7897 0ebf8283 2019-07-24 stsp break;
7898 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7899 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7900 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7901 0ebf8283 2019-07-24 stsp logmsg);
7902 0ebf8283 2019-07-24 stsp break;
7903 0ebf8283 2019-07-24 stsp default:
7904 0ebf8283 2019-07-24 stsp break;
7905 0ebf8283 2019-07-24 stsp }
7906 0ebf8283 2019-07-24 stsp done:
7907 0ebf8283 2019-07-24 stsp free(old_id_str);
7908 0ebf8283 2019-07-24 stsp free(new_id_str);
7909 0ebf8283 2019-07-24 stsp return err;
7910 0ebf8283 2019-07-24 stsp }
7911 0ebf8283 2019-07-24 stsp
7912 0ebf8283 2019-07-24 stsp static const struct got_error *
7913 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7914 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7915 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7916 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7917 0ebf8283 2019-07-24 stsp {
7918 0ebf8283 2019-07-24 stsp const struct got_error *err;
7919 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7920 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7921 0ebf8283 2019-07-24 stsp
7922 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7923 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7924 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7925 0ebf8283 2019-07-24 stsp if (err)
7926 0ebf8283 2019-07-24 stsp return err;
7927 0ebf8283 2019-07-24 stsp }
7928 0ebf8283 2019-07-24 stsp
7929 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7930 0ebf8283 2019-07-24 stsp if (err)
7931 0ebf8283 2019-07-24 stsp return err;
7932 0ebf8283 2019-07-24 stsp
7933 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7934 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7935 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7936 0ebf8283 2019-07-24 stsp if (err) {
7937 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7938 0ebf8283 2019-07-24 stsp goto done;
7939 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7940 0ebf8283 2019-07-24 stsp } else {
7941 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7942 0ebf8283 2019-07-24 stsp free(new_commit_id);
7943 0ebf8283 2019-07-24 stsp }
7944 0ebf8283 2019-07-24 stsp done:
7945 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7946 0ebf8283 2019-07-24 stsp return err;
7947 0ebf8283 2019-07-24 stsp }
7948 0ebf8283 2019-07-24 stsp
7949 0ebf8283 2019-07-24 stsp static const struct got_error *
7950 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7951 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7952 0ebf8283 2019-07-24 stsp {
7953 0ebf8283 2019-07-24 stsp const struct got_error *error;
7954 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7955 0ebf8283 2019-07-24 stsp
7956 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7957 0ebf8283 2019-07-24 stsp repo);
7958 0ebf8283 2019-07-24 stsp if (error)
7959 0ebf8283 2019-07-24 stsp return error;
7960 0ebf8283 2019-07-24 stsp
7961 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7962 0ebf8283 2019-07-24 stsp if (error)
7963 0ebf8283 2019-07-24 stsp return error;
7964 0ebf8283 2019-07-24 stsp
7965 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7966 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7967 0ebf8283 2019-07-24 stsp return error;
7968 ab20a43a 2020-01-29 stsp }
7969 ab20a43a 2020-01-29 stsp
7970 ab20a43a 2020-01-29 stsp static const struct got_error *
7971 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7972 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7973 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7974 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7975 ab20a43a 2020-01-29 stsp {
7976 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7977 ab20a43a 2020-01-29 stsp
7978 ab20a43a 2020-01-29 stsp switch (status) {
7979 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7980 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7981 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7982 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7983 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7984 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7985 ab20a43a 2020-01-29 stsp default:
7986 ab20a43a 2020-01-29 stsp break;
7987 ab20a43a 2020-01-29 stsp }
7988 ab20a43a 2020-01-29 stsp
7989 ab20a43a 2020-01-29 stsp switch (staged_status) {
7990 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7991 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7992 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7993 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7994 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7995 ab20a43a 2020-01-29 stsp default:
7996 ab20a43a 2020-01-29 stsp break;
7997 ab20a43a 2020-01-29 stsp }
7998 ab20a43a 2020-01-29 stsp
7999 ab20a43a 2020-01-29 stsp return NULL;
8000 0ebf8283 2019-07-24 stsp }
8001 0ebf8283 2019-07-24 stsp
8002 0ebf8283 2019-07-24 stsp static const struct got_error *
8003 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8004 0ebf8283 2019-07-24 stsp {
8005 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8006 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8007 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8008 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8009 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8010 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8011 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8012 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8013 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8014 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8015 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8016 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8017 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8018 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8019 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8020 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8021 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8022 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8023 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8024 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8025 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8026 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8027 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8028 0ebf8283 2019-07-24 stsp
8029 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8030 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8031 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8032 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8033 0ebf8283 2019-07-24 stsp
8034 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8035 0ebf8283 2019-07-24 stsp switch (ch) {
8036 0ebf8283 2019-07-24 stsp case 'a':
8037 0ebf8283 2019-07-24 stsp abort_edit = 1;
8038 0ebf8283 2019-07-24 stsp break;
8039 0ebf8283 2019-07-24 stsp case 'c':
8040 0ebf8283 2019-07-24 stsp continue_edit = 1;
8041 0ebf8283 2019-07-24 stsp break;
8042 0ebf8283 2019-07-24 stsp case 'F':
8043 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8044 0ebf8283 2019-07-24 stsp break;
8045 083957f4 2020-02-24 stsp case 'm':
8046 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8047 083957f4 2020-02-24 stsp break;
8048 0ebf8283 2019-07-24 stsp default:
8049 0ebf8283 2019-07-24 stsp usage_histedit();
8050 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8051 0ebf8283 2019-07-24 stsp }
8052 0ebf8283 2019-07-24 stsp }
8053 0ebf8283 2019-07-24 stsp
8054 0ebf8283 2019-07-24 stsp argc -= optind;
8055 0ebf8283 2019-07-24 stsp argv += optind;
8056 0ebf8283 2019-07-24 stsp
8057 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8058 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8059 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8060 0ebf8283 2019-07-24 stsp err(1, "pledge");
8061 0ebf8283 2019-07-24 stsp #endif
8062 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8063 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8064 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8065 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8066 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8067 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8068 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8069 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8070 0ebf8283 2019-07-24 stsp if (argc != 0)
8071 0ebf8283 2019-07-24 stsp usage_histedit();
8072 0ebf8283 2019-07-24 stsp
8073 0ebf8283 2019-07-24 stsp /*
8074 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8075 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8076 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8077 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8078 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8079 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8080 0ebf8283 2019-07-24 stsp */
8081 0ebf8283 2019-07-24 stsp
8082 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8083 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8084 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8085 0ebf8283 2019-07-24 stsp goto done;
8086 0ebf8283 2019-07-24 stsp }
8087 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8088 fa51e947 2020-03-27 stsp if (error) {
8089 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8090 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8091 0ebf8283 2019-07-24 stsp goto done;
8092 fa51e947 2020-03-27 stsp }
8093 0ebf8283 2019-07-24 stsp
8094 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8095 c9956ddf 2019-09-08 stsp NULL);
8096 0ebf8283 2019-07-24 stsp if (error != NULL)
8097 0ebf8283 2019-07-24 stsp goto done;
8098 0ebf8283 2019-07-24 stsp
8099 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8100 0ebf8283 2019-07-24 stsp if (error)
8101 0ebf8283 2019-07-24 stsp goto done;
8102 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8103 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8104 0ebf8283 2019-07-24 stsp goto done;
8105 0ebf8283 2019-07-24 stsp }
8106 0ebf8283 2019-07-24 stsp
8107 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8108 0ebf8283 2019-07-24 stsp if (error)
8109 0ebf8283 2019-07-24 stsp goto done;
8110 0ebf8283 2019-07-24 stsp
8111 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8112 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8113 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8114 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8115 083957f4 2020-02-24 stsp "before the -m option can be used");
8116 083957f4 2020-02-24 stsp goto done;
8117 083957f4 2020-02-24 stsp }
8118 083957f4 2020-02-24 stsp
8119 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8120 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8121 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8122 3e3a69f1 2019-07-25 stsp worktree, repo);
8123 0ebf8283 2019-07-24 stsp if (error)
8124 0ebf8283 2019-07-24 stsp goto done;
8125 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8126 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8127 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8128 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8129 0ebf8283 2019-07-24 stsp if (error)
8130 0ebf8283 2019-07-24 stsp goto done;
8131 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8132 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8133 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8134 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8135 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8136 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8137 0ebf8283 2019-07-24 stsp goto done;
8138 0ebf8283 2019-07-24 stsp }
8139 0ebf8283 2019-07-24 stsp
8140 0ebf8283 2019-07-24 stsp if (continue_edit) {
8141 0ebf8283 2019-07-24 stsp char *path;
8142 0ebf8283 2019-07-24 stsp
8143 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8144 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8145 0ebf8283 2019-07-24 stsp goto done;
8146 0ebf8283 2019-07-24 stsp }
8147 0ebf8283 2019-07-24 stsp
8148 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8149 0ebf8283 2019-07-24 stsp if (error)
8150 0ebf8283 2019-07-24 stsp goto done;
8151 0ebf8283 2019-07-24 stsp
8152 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8153 0ebf8283 2019-07-24 stsp free(path);
8154 0ebf8283 2019-07-24 stsp if (error)
8155 0ebf8283 2019-07-24 stsp goto done;
8156 0ebf8283 2019-07-24 stsp
8157 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8158 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8159 3e3a69f1 2019-07-25 stsp worktree, repo);
8160 0ebf8283 2019-07-24 stsp if (error)
8161 0ebf8283 2019-07-24 stsp goto done;
8162 0ebf8283 2019-07-24 stsp
8163 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8164 0ebf8283 2019-07-24 stsp if (error)
8165 0ebf8283 2019-07-24 stsp goto done;
8166 0ebf8283 2019-07-24 stsp
8167 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8168 0ebf8283 2019-07-24 stsp head_commit_id);
8169 0ebf8283 2019-07-24 stsp if (error)
8170 0ebf8283 2019-07-24 stsp goto done;
8171 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8172 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8173 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8174 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8175 3aac7cf7 2019-07-25 stsp goto done;
8176 3aac7cf7 2019-07-25 stsp }
8177 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8178 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8179 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8180 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8181 0ebf8283 2019-07-24 stsp commit = NULL;
8182 0ebf8283 2019-07-24 stsp if (error)
8183 0ebf8283 2019-07-24 stsp goto done;
8184 0ebf8283 2019-07-24 stsp } else {
8185 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8186 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8187 0ebf8283 2019-07-24 stsp goto done;
8188 0ebf8283 2019-07-24 stsp }
8189 0ebf8283 2019-07-24 stsp
8190 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8191 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8192 0ebf8283 2019-07-24 stsp if (error != NULL)
8193 0ebf8283 2019-07-24 stsp goto done;
8194 0ebf8283 2019-07-24 stsp
8195 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8196 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8197 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8198 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8199 c7d20a3f 2019-07-30 stsp goto done;
8200 c7d20a3f 2019-07-30 stsp }
8201 c7d20a3f 2019-07-30 stsp
8202 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8203 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8204 bfce7f83 2019-07-27 stsp branch = NULL;
8205 0ebf8283 2019-07-24 stsp if (error)
8206 0ebf8283 2019-07-24 stsp goto done;
8207 0ebf8283 2019-07-24 stsp
8208 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8209 0ebf8283 2019-07-24 stsp head_commit_id);
8210 0ebf8283 2019-07-24 stsp if (error)
8211 0ebf8283 2019-07-24 stsp goto done;
8212 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8213 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8214 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8215 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8216 3aac7cf7 2019-07-25 stsp goto done;
8217 3aac7cf7 2019-07-25 stsp }
8218 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8219 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8220 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8221 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8222 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8223 0ebf8283 2019-07-24 stsp commit = NULL;
8224 0ebf8283 2019-07-24 stsp if (error)
8225 0ebf8283 2019-07-24 stsp goto done;
8226 0ebf8283 2019-07-24 stsp
8227 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8228 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8229 c5996fff 2020-01-29 stsp goto done;
8230 c5996fff 2020-01-29 stsp }
8231 c5996fff 2020-01-29 stsp
8232 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8233 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8234 bfce7f83 2019-07-27 stsp if (error)
8235 bfce7f83 2019-07-27 stsp goto done;
8236 bfce7f83 2019-07-27 stsp
8237 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8238 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8239 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8240 6ba2bea2 2019-07-27 stsp if (error) {
8241 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8242 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8243 9627c110 2020-04-18 stsp update_progress, &upa);
8244 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8245 0ebf8283 2019-07-24 stsp goto done;
8246 6ba2bea2 2019-07-27 stsp }
8247 0ebf8283 2019-07-24 stsp } else {
8248 514f2ffe 2020-01-29 stsp const char *branch_name;
8249 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8250 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8251 514f2ffe 2020-01-29 stsp branch_name += 11;
8252 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8253 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8254 6ba2bea2 2019-07-27 stsp if (error) {
8255 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8256 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8257 9627c110 2020-04-18 stsp update_progress, &upa);
8258 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8259 0ebf8283 2019-07-24 stsp goto done;
8260 6ba2bea2 2019-07-27 stsp }
8261 0ebf8283 2019-07-24 stsp
8262 0ebf8283 2019-07-24 stsp }
8263 0ebf8283 2019-07-24 stsp
8264 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8265 0ebf8283 2019-07-24 stsp repo);
8266 6ba2bea2 2019-07-27 stsp if (error) {
8267 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8268 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8269 9627c110 2020-04-18 stsp update_progress, &upa);
8270 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8271 0ebf8283 2019-07-24 stsp goto done;
8272 6ba2bea2 2019-07-27 stsp }
8273 0ebf8283 2019-07-24 stsp
8274 0ebf8283 2019-07-24 stsp }
8275 0ebf8283 2019-07-24 stsp
8276 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8277 4ec14e60 2019-08-28 hiltjo if (error)
8278 0ebf8283 2019-07-24 stsp goto done;
8279 0ebf8283 2019-07-24 stsp
8280 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8281 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8282 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8283 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8284 0ebf8283 2019-07-24 stsp continue;
8285 0ebf8283 2019-07-24 stsp
8286 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8287 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8288 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8289 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8290 0ebf8283 2019-07-24 stsp repo);
8291 ab20a43a 2020-01-29 stsp if (error)
8292 ab20a43a 2020-01-29 stsp goto done;
8293 0ebf8283 2019-07-24 stsp } else {
8294 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8295 ab20a43a 2020-01-29 stsp int have_changes = 0;
8296 ab20a43a 2020-01-29 stsp
8297 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8298 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8299 ab20a43a 2020-01-29 stsp if (error)
8300 ab20a43a 2020-01-29 stsp goto done;
8301 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8302 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8303 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8304 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8305 ab20a43a 2020-01-29 stsp if (error) {
8306 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8307 ab20a43a 2020-01-29 stsp goto done;
8308 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8309 ab20a43a 2020-01-29 stsp goto done;
8310 ab20a43a 2020-01-29 stsp }
8311 ab20a43a 2020-01-29 stsp if (have_changes) {
8312 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8313 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8314 ab20a43a 2020-01-29 stsp if (error)
8315 ab20a43a 2020-01-29 stsp goto done;
8316 ab20a43a 2020-01-29 stsp } else {
8317 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8318 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8319 ab20a43a 2020-01-29 stsp if (error)
8320 ab20a43a 2020-01-29 stsp goto done;
8321 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8322 ab20a43a 2020-01-29 stsp hle, NULL);
8323 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8324 ab20a43a 2020-01-29 stsp commit = NULL;
8325 ab20a43a 2020-01-29 stsp if (error)
8326 ab20a43a 2020-01-29 stsp goto done;
8327 ab20a43a 2020-01-29 stsp }
8328 0ebf8283 2019-07-24 stsp }
8329 0ebf8283 2019-07-24 stsp continue;
8330 0ebf8283 2019-07-24 stsp }
8331 0ebf8283 2019-07-24 stsp
8332 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8333 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8334 0ebf8283 2019-07-24 stsp if (error)
8335 0ebf8283 2019-07-24 stsp goto done;
8336 0ebf8283 2019-07-24 stsp continue;
8337 0ebf8283 2019-07-24 stsp }
8338 0ebf8283 2019-07-24 stsp
8339 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8340 0ebf8283 2019-07-24 stsp hle->commit_id);
8341 0ebf8283 2019-07-24 stsp if (error)
8342 0ebf8283 2019-07-24 stsp goto done;
8343 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8344 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8345 0ebf8283 2019-07-24 stsp
8346 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8347 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8348 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8349 0ebf8283 2019-07-24 stsp if (error)
8350 0ebf8283 2019-07-24 stsp goto done;
8351 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8352 0ebf8283 2019-07-24 stsp commit = NULL;
8353 0ebf8283 2019-07-24 stsp
8354 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8355 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8356 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8357 9627c110 2020-04-18 stsp
8358 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8359 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8360 a0ea4fc0 2020-02-28 stsp repo);
8361 a0ea4fc0 2020-02-28 stsp if (error)
8362 a0ea4fc0 2020-02-28 stsp goto done;
8363 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8364 0ebf8283 2019-07-24 stsp break;
8365 0ebf8283 2019-07-24 stsp }
8366 0ebf8283 2019-07-24 stsp
8367 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8368 0ebf8283 2019-07-24 stsp char *id_str;
8369 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8370 0ebf8283 2019-07-24 stsp if (error)
8371 0ebf8283 2019-07-24 stsp goto done;
8372 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8373 0ebf8283 2019-07-24 stsp id_str);
8374 0ebf8283 2019-07-24 stsp free(id_str);
8375 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8376 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8377 3e3a69f1 2019-07-25 stsp fileindex);
8378 0ebf8283 2019-07-24 stsp goto done;
8379 0ebf8283 2019-07-24 stsp }
8380 0ebf8283 2019-07-24 stsp
8381 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8382 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8383 0ebf8283 2019-07-24 stsp if (error)
8384 0ebf8283 2019-07-24 stsp goto done;
8385 0ebf8283 2019-07-24 stsp continue;
8386 0ebf8283 2019-07-24 stsp }
8387 0ebf8283 2019-07-24 stsp
8388 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8389 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8390 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8391 0ebf8283 2019-07-24 stsp if (error)
8392 0ebf8283 2019-07-24 stsp goto done;
8393 0ebf8283 2019-07-24 stsp }
8394 0ebf8283 2019-07-24 stsp
8395 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8396 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8397 0ebf8283 2019-07-24 stsp if (error)
8398 0ebf8283 2019-07-24 stsp goto done;
8399 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8400 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8401 0ebf8283 2019-07-24 stsp } else
8402 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8403 3e3a69f1 2019-07-25 stsp branch, repo);
8404 0ebf8283 2019-07-24 stsp done:
8405 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8406 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8407 0ebf8283 2019-07-24 stsp free(head_commit_id);
8408 0ebf8283 2019-07-24 stsp free(base_commit_id);
8409 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8410 0ebf8283 2019-07-24 stsp if (commit)
8411 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8412 0ebf8283 2019-07-24 stsp if (branch)
8413 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8414 0ebf8283 2019-07-24 stsp if (tmp_branch)
8415 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8416 0ebf8283 2019-07-24 stsp if (worktree)
8417 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8418 2822a352 2019-10-15 stsp if (repo)
8419 2822a352 2019-10-15 stsp got_repo_close(repo);
8420 2822a352 2019-10-15 stsp return error;
8421 2822a352 2019-10-15 stsp }
8422 2822a352 2019-10-15 stsp
8423 2822a352 2019-10-15 stsp __dead static void
8424 2822a352 2019-10-15 stsp usage_integrate(void)
8425 2822a352 2019-10-15 stsp {
8426 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8427 2822a352 2019-10-15 stsp exit(1);
8428 2822a352 2019-10-15 stsp }
8429 2822a352 2019-10-15 stsp
8430 2822a352 2019-10-15 stsp static const struct got_error *
8431 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8432 2822a352 2019-10-15 stsp {
8433 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8434 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8435 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8436 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8437 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8438 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8439 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8440 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8441 9627c110 2020-04-18 stsp int ch;
8442 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8443 2822a352 2019-10-15 stsp
8444 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8445 2822a352 2019-10-15 stsp switch (ch) {
8446 2822a352 2019-10-15 stsp default:
8447 2822a352 2019-10-15 stsp usage_integrate();
8448 2822a352 2019-10-15 stsp /* NOTREACHED */
8449 2822a352 2019-10-15 stsp }
8450 2822a352 2019-10-15 stsp }
8451 2822a352 2019-10-15 stsp
8452 2822a352 2019-10-15 stsp argc -= optind;
8453 2822a352 2019-10-15 stsp argv += optind;
8454 2822a352 2019-10-15 stsp
8455 2822a352 2019-10-15 stsp if (argc != 1)
8456 2822a352 2019-10-15 stsp usage_integrate();
8457 2822a352 2019-10-15 stsp branch_arg = argv[0];
8458 2822a352 2019-10-15 stsp
8459 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8460 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8461 2822a352 2019-10-15 stsp err(1, "pledge");
8462 2822a352 2019-10-15 stsp
8463 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8464 2822a352 2019-10-15 stsp if (cwd == NULL) {
8465 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8466 2822a352 2019-10-15 stsp goto done;
8467 2822a352 2019-10-15 stsp }
8468 2822a352 2019-10-15 stsp
8469 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8470 fa51e947 2020-03-27 stsp if (error) {
8471 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8472 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
8473 fa51e947 2020-03-27 stsp cwd);
8474 2822a352 2019-10-15 stsp goto done;
8475 fa51e947 2020-03-27 stsp }
8476 2822a352 2019-10-15 stsp
8477 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8478 2822a352 2019-10-15 stsp if (error)
8479 2822a352 2019-10-15 stsp goto done;
8480 2822a352 2019-10-15 stsp
8481 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8482 2822a352 2019-10-15 stsp NULL);
8483 2822a352 2019-10-15 stsp if (error != NULL)
8484 2822a352 2019-10-15 stsp goto done;
8485 2822a352 2019-10-15 stsp
8486 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8487 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8488 2822a352 2019-10-15 stsp if (error)
8489 2822a352 2019-10-15 stsp goto done;
8490 2822a352 2019-10-15 stsp
8491 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8492 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8493 2822a352 2019-10-15 stsp goto done;
8494 2822a352 2019-10-15 stsp }
8495 2822a352 2019-10-15 stsp
8496 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8497 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8498 2822a352 2019-10-15 stsp if (error)
8499 2822a352 2019-10-15 stsp goto done;
8500 2822a352 2019-10-15 stsp
8501 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8502 2822a352 2019-10-15 stsp if (refname == NULL) {
8503 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8504 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8505 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8506 2822a352 2019-10-15 stsp goto done;
8507 2822a352 2019-10-15 stsp }
8508 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8509 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8510 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8511 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8512 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8513 2822a352 2019-10-15 stsp goto done;
8514 2822a352 2019-10-15 stsp }
8515 2822a352 2019-10-15 stsp
8516 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8517 2822a352 2019-10-15 stsp if (error)
8518 2822a352 2019-10-15 stsp goto done;
8519 2822a352 2019-10-15 stsp
8520 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8521 2822a352 2019-10-15 stsp if (error)
8522 2822a352 2019-10-15 stsp goto done;
8523 2822a352 2019-10-15 stsp
8524 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8525 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8526 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8527 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8528 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8529 2822a352 2019-10-15 stsp goto done;
8530 2822a352 2019-10-15 stsp }
8531 2822a352 2019-10-15 stsp
8532 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8533 2822a352 2019-10-15 stsp if (error) {
8534 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8535 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8536 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8537 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8538 2822a352 2019-10-15 stsp goto done;
8539 2822a352 2019-10-15 stsp }
8540 2822a352 2019-10-15 stsp
8541 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8542 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8543 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
8544 2822a352 2019-10-15 stsp check_cancelled, NULL);
8545 2822a352 2019-10-15 stsp if (error)
8546 2822a352 2019-10-15 stsp goto done;
8547 2822a352 2019-10-15 stsp
8548 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8549 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8550 2822a352 2019-10-15 stsp done:
8551 715dc77e 2019-08-03 stsp if (repo)
8552 715dc77e 2019-08-03 stsp got_repo_close(repo);
8553 2822a352 2019-10-15 stsp if (worktree)
8554 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8555 2822a352 2019-10-15 stsp free(cwd);
8556 2822a352 2019-10-15 stsp free(base_commit_id);
8557 2822a352 2019-10-15 stsp free(commit_id);
8558 2822a352 2019-10-15 stsp free(refname);
8559 2822a352 2019-10-15 stsp free(base_refname);
8560 715dc77e 2019-08-03 stsp return error;
8561 715dc77e 2019-08-03 stsp }
8562 715dc77e 2019-08-03 stsp
8563 715dc77e 2019-08-03 stsp __dead static void
8564 715dc77e 2019-08-03 stsp usage_stage(void)
8565 715dc77e 2019-08-03 stsp {
8566 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8567 f3055044 2019-08-07 stsp "[file-path ...]\n",
8568 715dc77e 2019-08-03 stsp getprogname());
8569 715dc77e 2019-08-03 stsp exit(1);
8570 715dc77e 2019-08-03 stsp }
8571 715dc77e 2019-08-03 stsp
8572 715dc77e 2019-08-03 stsp static const struct got_error *
8573 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8574 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8575 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8576 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8577 408b4ebc 2019-08-03 stsp {
8578 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8579 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8580 408b4ebc 2019-08-03 stsp
8581 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8582 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8583 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8584 408b4ebc 2019-08-03 stsp return NULL;
8585 408b4ebc 2019-08-03 stsp
8586 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8587 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8588 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8589 408b4ebc 2019-08-03 stsp else
8590 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8591 408b4ebc 2019-08-03 stsp if (err)
8592 408b4ebc 2019-08-03 stsp return err;
8593 408b4ebc 2019-08-03 stsp
8594 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8595 408b4ebc 2019-08-03 stsp free(id_str);
8596 dc424a06 2019-08-07 stsp return NULL;
8597 dc424a06 2019-08-07 stsp }
8598 2e1f37b0 2019-08-08 stsp
8599 dc424a06 2019-08-07 stsp static const struct got_error *
8600 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8601 715dc77e 2019-08-03 stsp {
8602 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8603 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8604 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8605 715dc77e 2019-08-03 stsp char *cwd = NULL;
8606 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8607 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8608 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8609 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8610 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8611 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8612 715dc77e 2019-08-03 stsp
8613 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8614 715dc77e 2019-08-03 stsp
8615 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8616 715dc77e 2019-08-03 stsp switch (ch) {
8617 408b4ebc 2019-08-03 stsp case 'l':
8618 408b4ebc 2019-08-03 stsp list_stage = 1;
8619 408b4ebc 2019-08-03 stsp break;
8620 dc424a06 2019-08-07 stsp case 'p':
8621 dc424a06 2019-08-07 stsp pflag = 1;
8622 dc424a06 2019-08-07 stsp break;
8623 dc424a06 2019-08-07 stsp case 'F':
8624 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8625 dc424a06 2019-08-07 stsp break;
8626 715dc77e 2019-08-03 stsp default:
8627 715dc77e 2019-08-03 stsp usage_stage();
8628 715dc77e 2019-08-03 stsp /* NOTREACHED */
8629 715dc77e 2019-08-03 stsp }
8630 715dc77e 2019-08-03 stsp }
8631 715dc77e 2019-08-03 stsp
8632 715dc77e 2019-08-03 stsp argc -= optind;
8633 715dc77e 2019-08-03 stsp argv += optind;
8634 715dc77e 2019-08-03 stsp
8635 715dc77e 2019-08-03 stsp #ifndef PROFILE
8636 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8637 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8638 715dc77e 2019-08-03 stsp err(1, "pledge");
8639 715dc77e 2019-08-03 stsp #endif
8640 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8641 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8642 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8643 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8644 715dc77e 2019-08-03 stsp
8645 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8646 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8647 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8648 715dc77e 2019-08-03 stsp goto done;
8649 715dc77e 2019-08-03 stsp }
8650 715dc77e 2019-08-03 stsp
8651 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8652 fa51e947 2020-03-27 stsp if (error) {
8653 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8654 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
8655 715dc77e 2019-08-03 stsp goto done;
8656 fa51e947 2020-03-27 stsp }
8657 715dc77e 2019-08-03 stsp
8658 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8659 c9956ddf 2019-09-08 stsp NULL);
8660 715dc77e 2019-08-03 stsp if (error != NULL)
8661 715dc77e 2019-08-03 stsp goto done;
8662 715dc77e 2019-08-03 stsp
8663 dc424a06 2019-08-07 stsp if (patch_script_path) {
8664 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8665 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8666 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8667 dc424a06 2019-08-07 stsp patch_script_path);
8668 dc424a06 2019-08-07 stsp goto done;
8669 dc424a06 2019-08-07 stsp }
8670 dc424a06 2019-08-07 stsp }
8671 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8672 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8673 715dc77e 2019-08-03 stsp if (error)
8674 715dc77e 2019-08-03 stsp goto done;
8675 715dc77e 2019-08-03 stsp
8676 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8677 6d022e97 2019-08-04 stsp if (error)
8678 6d022e97 2019-08-04 stsp goto done;
8679 715dc77e 2019-08-03 stsp
8680 6d022e97 2019-08-04 stsp if (list_stage)
8681 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8682 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8683 2e1f37b0 2019-08-08 stsp else {
8684 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8685 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8686 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8687 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8688 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8689 2e1f37b0 2019-08-08 stsp }
8690 ad493afc 2019-08-03 stsp done:
8691 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8692 2e1f37b0 2019-08-08 stsp error == NULL)
8693 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8694 ad493afc 2019-08-03 stsp if (repo)
8695 ad493afc 2019-08-03 stsp got_repo_close(repo);
8696 ad493afc 2019-08-03 stsp if (worktree)
8697 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8698 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8699 ad493afc 2019-08-03 stsp free((char *)pe->path);
8700 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8701 ad493afc 2019-08-03 stsp free(cwd);
8702 ad493afc 2019-08-03 stsp return error;
8703 ad493afc 2019-08-03 stsp }
8704 ad493afc 2019-08-03 stsp
8705 ad493afc 2019-08-03 stsp __dead static void
8706 ad493afc 2019-08-03 stsp usage_unstage(void)
8707 ad493afc 2019-08-03 stsp {
8708 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8709 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8710 ad493afc 2019-08-03 stsp getprogname());
8711 ad493afc 2019-08-03 stsp exit(1);
8712 ad493afc 2019-08-03 stsp }
8713 ad493afc 2019-08-03 stsp
8714 ad493afc 2019-08-03 stsp
8715 ad493afc 2019-08-03 stsp static const struct got_error *
8716 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8717 ad493afc 2019-08-03 stsp {
8718 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8719 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8720 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8721 ad493afc 2019-08-03 stsp char *cwd = NULL;
8722 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8723 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8724 9627c110 2020-04-18 stsp int ch, pflag = 0;
8725 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8726 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8727 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8728 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8729 ad493afc 2019-08-03 stsp
8730 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8731 ad493afc 2019-08-03 stsp
8732 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8733 ad493afc 2019-08-03 stsp switch (ch) {
8734 2e1f37b0 2019-08-08 stsp case 'p':
8735 2e1f37b0 2019-08-08 stsp pflag = 1;
8736 2e1f37b0 2019-08-08 stsp break;
8737 2e1f37b0 2019-08-08 stsp case 'F':
8738 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8739 2e1f37b0 2019-08-08 stsp break;
8740 ad493afc 2019-08-03 stsp default:
8741 ad493afc 2019-08-03 stsp usage_unstage();
8742 ad493afc 2019-08-03 stsp /* NOTREACHED */
8743 ad493afc 2019-08-03 stsp }
8744 ad493afc 2019-08-03 stsp }
8745 ad493afc 2019-08-03 stsp
8746 ad493afc 2019-08-03 stsp argc -= optind;
8747 ad493afc 2019-08-03 stsp argv += optind;
8748 ad493afc 2019-08-03 stsp
8749 ad493afc 2019-08-03 stsp #ifndef PROFILE
8750 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8751 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8752 ad493afc 2019-08-03 stsp err(1, "pledge");
8753 ad493afc 2019-08-03 stsp #endif
8754 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8755 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8756 2e1f37b0 2019-08-08 stsp
8757 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8758 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8759 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8760 ad493afc 2019-08-03 stsp goto done;
8761 ad493afc 2019-08-03 stsp }
8762 ad493afc 2019-08-03 stsp
8763 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8764 fa51e947 2020-03-27 stsp if (error) {
8765 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8766 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
8767 ad493afc 2019-08-03 stsp goto done;
8768 fa51e947 2020-03-27 stsp }
8769 ad493afc 2019-08-03 stsp
8770 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8771 c9956ddf 2019-09-08 stsp NULL);
8772 ad493afc 2019-08-03 stsp if (error != NULL)
8773 ad493afc 2019-08-03 stsp goto done;
8774 ad493afc 2019-08-03 stsp
8775 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8776 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8777 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8778 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8779 2e1f37b0 2019-08-08 stsp patch_script_path);
8780 2e1f37b0 2019-08-08 stsp goto done;
8781 2e1f37b0 2019-08-08 stsp }
8782 2e1f37b0 2019-08-08 stsp }
8783 2e1f37b0 2019-08-08 stsp
8784 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8785 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8786 ad493afc 2019-08-03 stsp if (error)
8787 ad493afc 2019-08-03 stsp goto done;
8788 ad493afc 2019-08-03 stsp
8789 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8790 ad493afc 2019-08-03 stsp if (error)
8791 ad493afc 2019-08-03 stsp goto done;
8792 ad493afc 2019-08-03 stsp
8793 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8794 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8795 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8796 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8797 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
8798 9627c110 2020-04-18 stsp if (!error)
8799 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8800 715dc77e 2019-08-03 stsp done:
8801 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8802 2e1f37b0 2019-08-08 stsp error == NULL)
8803 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8804 0ebf8283 2019-07-24 stsp if (repo)
8805 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8806 715dc77e 2019-08-03 stsp if (worktree)
8807 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8808 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8809 715dc77e 2019-08-03 stsp free((char *)pe->path);
8810 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8811 715dc77e 2019-08-03 stsp free(cwd);
8812 01073a5d 2019-08-22 stsp return error;
8813 01073a5d 2019-08-22 stsp }
8814 01073a5d 2019-08-22 stsp
8815 01073a5d 2019-08-22 stsp __dead static void
8816 01073a5d 2019-08-22 stsp usage_cat(void)
8817 01073a5d 2019-08-22 stsp {
8818 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8819 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8820 01073a5d 2019-08-22 stsp exit(1);
8821 01073a5d 2019-08-22 stsp }
8822 01073a5d 2019-08-22 stsp
8823 01073a5d 2019-08-22 stsp static const struct got_error *
8824 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8825 01073a5d 2019-08-22 stsp {
8826 01073a5d 2019-08-22 stsp const struct got_error *err;
8827 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8828 01073a5d 2019-08-22 stsp
8829 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8830 01073a5d 2019-08-22 stsp if (err)
8831 01073a5d 2019-08-22 stsp return err;
8832 01073a5d 2019-08-22 stsp
8833 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8834 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8835 01073a5d 2019-08-22 stsp return err;
8836 01073a5d 2019-08-22 stsp }
8837 01073a5d 2019-08-22 stsp
8838 01073a5d 2019-08-22 stsp static const struct got_error *
8839 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8840 01073a5d 2019-08-22 stsp {
8841 01073a5d 2019-08-22 stsp const struct got_error *err;
8842 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8843 56e0773d 2019-11-28 stsp int nentries, i;
8844 01073a5d 2019-08-22 stsp
8845 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8846 01073a5d 2019-08-22 stsp if (err)
8847 01073a5d 2019-08-22 stsp return err;
8848 01073a5d 2019-08-22 stsp
8849 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8850 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8851 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8852 01073a5d 2019-08-22 stsp char *id_str;
8853 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8854 01073a5d 2019-08-22 stsp break;
8855 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8856 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8857 01073a5d 2019-08-22 stsp if (err)
8858 01073a5d 2019-08-22 stsp break;
8859 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8860 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8861 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8862 01073a5d 2019-08-22 stsp free(id_str);
8863 01073a5d 2019-08-22 stsp }
8864 01073a5d 2019-08-22 stsp
8865 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8866 01073a5d 2019-08-22 stsp return err;
8867 01073a5d 2019-08-22 stsp }
8868 01073a5d 2019-08-22 stsp
8869 01073a5d 2019-08-22 stsp static const struct got_error *
8870 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8871 01073a5d 2019-08-22 stsp {
8872 01073a5d 2019-08-22 stsp const struct got_error *err;
8873 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8874 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8875 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8876 01073a5d 2019-08-22 stsp char *id_str = NULL;
8877 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8878 01073a5d 2019-08-22 stsp
8879 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8880 01073a5d 2019-08-22 stsp if (err)
8881 01073a5d 2019-08-22 stsp return err;
8882 01073a5d 2019-08-22 stsp
8883 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8884 01073a5d 2019-08-22 stsp if (err)
8885 01073a5d 2019-08-22 stsp goto done;
8886 01073a5d 2019-08-22 stsp
8887 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8888 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8889 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8890 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8891 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8892 01073a5d 2019-08-22 stsp char *pid_str;
8893 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8894 01073a5d 2019-08-22 stsp if (err)
8895 01073a5d 2019-08-22 stsp goto done;
8896 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8897 01073a5d 2019-08-22 stsp free(pid_str);
8898 01073a5d 2019-08-22 stsp }
8899 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8900 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8901 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8902 01073a5d 2019-08-22 stsp
8903 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8904 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8905 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8906 01073a5d 2019-08-22 stsp
8907 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8908 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8909 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8910 01073a5d 2019-08-22 stsp done:
8911 01073a5d 2019-08-22 stsp free(id_str);
8912 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8913 01073a5d 2019-08-22 stsp return err;
8914 01073a5d 2019-08-22 stsp }
8915 01073a5d 2019-08-22 stsp
8916 01073a5d 2019-08-22 stsp static const struct got_error *
8917 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8918 01073a5d 2019-08-22 stsp {
8919 01073a5d 2019-08-22 stsp const struct got_error *err;
8920 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8921 01073a5d 2019-08-22 stsp char *id_str = NULL;
8922 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8923 01073a5d 2019-08-22 stsp
8924 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8925 01073a5d 2019-08-22 stsp if (err)
8926 01073a5d 2019-08-22 stsp return err;
8927 01073a5d 2019-08-22 stsp
8928 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8929 01073a5d 2019-08-22 stsp if (err)
8930 01073a5d 2019-08-22 stsp goto done;
8931 01073a5d 2019-08-22 stsp
8932 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8933 e15d5241 2019-08-22 stsp
8934 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8935 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8936 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8937 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8938 01073a5d 2019-08-22 stsp break;
8939 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8940 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8941 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8942 01073a5d 2019-08-22 stsp break;
8943 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8944 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8945 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8946 01073a5d 2019-08-22 stsp break;
8947 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8948 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8949 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8950 01073a5d 2019-08-22 stsp break;
8951 01073a5d 2019-08-22 stsp default:
8952 01073a5d 2019-08-22 stsp break;
8953 01073a5d 2019-08-22 stsp }
8954 01073a5d 2019-08-22 stsp
8955 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8956 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8957 e15d5241 2019-08-22 stsp
8958 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8959 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8960 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8961 01073a5d 2019-08-22 stsp
8962 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8963 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8964 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8965 01073a5d 2019-08-22 stsp done:
8966 01073a5d 2019-08-22 stsp free(id_str);
8967 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8968 01073a5d 2019-08-22 stsp return err;
8969 01073a5d 2019-08-22 stsp }
8970 01073a5d 2019-08-22 stsp
8971 01073a5d 2019-08-22 stsp static const struct got_error *
8972 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8973 01073a5d 2019-08-22 stsp {
8974 01073a5d 2019-08-22 stsp const struct got_error *error;
8975 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8976 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8977 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8978 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8979 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8980 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8981 01073a5d 2019-08-22 stsp
8982 01073a5d 2019-08-22 stsp #ifndef PROFILE
8983 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8984 01073a5d 2019-08-22 stsp NULL) == -1)
8985 01073a5d 2019-08-22 stsp err(1, "pledge");
8986 01073a5d 2019-08-22 stsp #endif
8987 01073a5d 2019-08-22 stsp
8988 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8989 01073a5d 2019-08-22 stsp switch (ch) {
8990 896e9b6f 2019-08-26 stsp case 'c':
8991 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8992 896e9b6f 2019-08-26 stsp break;
8993 01073a5d 2019-08-22 stsp case 'r':
8994 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8995 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8996 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8997 9ba1d308 2019-10-21 stsp optarg);
8998 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8999 01073a5d 2019-08-22 stsp break;
9000 896e9b6f 2019-08-26 stsp case 'P':
9001 896e9b6f 2019-08-26 stsp force_path = 1;
9002 896e9b6f 2019-08-26 stsp break;
9003 01073a5d 2019-08-22 stsp default:
9004 01073a5d 2019-08-22 stsp usage_cat();
9005 01073a5d 2019-08-22 stsp /* NOTREACHED */
9006 01073a5d 2019-08-22 stsp }
9007 01073a5d 2019-08-22 stsp }
9008 01073a5d 2019-08-22 stsp
9009 01073a5d 2019-08-22 stsp argc -= optind;
9010 01073a5d 2019-08-22 stsp argv += optind;
9011 01073a5d 2019-08-22 stsp
9012 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9013 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9014 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9015 01073a5d 2019-08-22 stsp goto done;
9016 01073a5d 2019-08-22 stsp }
9017 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9018 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9019 01073a5d 2019-08-22 stsp goto done;
9020 01073a5d 2019-08-22 stsp if (worktree) {
9021 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9022 01073a5d 2019-08-22 stsp repo_path = strdup(
9023 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9024 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9025 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9026 01073a5d 2019-08-22 stsp goto done;
9027 01073a5d 2019-08-22 stsp }
9028 01073a5d 2019-08-22 stsp }
9029 01073a5d 2019-08-22 stsp }
9030 01073a5d 2019-08-22 stsp
9031 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9032 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9033 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9034 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9035 01073a5d 2019-08-22 stsp }
9036 01073a5d 2019-08-22 stsp
9037 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9038 01073a5d 2019-08-22 stsp free(repo_path);
9039 01073a5d 2019-08-22 stsp if (error != NULL)
9040 01073a5d 2019-08-22 stsp goto done;
9041 01073a5d 2019-08-22 stsp
9042 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9043 896e9b6f 2019-08-26 stsp if (error)
9044 896e9b6f 2019-08-26 stsp goto done;
9045 896e9b6f 2019-08-26 stsp
9046 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9047 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9048 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9049 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9050 01073a5d 2019-08-22 stsp if (error)
9051 01073a5d 2019-08-22 stsp goto done;
9052 01073a5d 2019-08-22 stsp
9053 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9054 896e9b6f 2019-08-26 stsp if (force_path) {
9055 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9056 896e9b6f 2019-08-26 stsp argv[i]);
9057 896e9b6f 2019-08-26 stsp if (error)
9058 896e9b6f 2019-08-26 stsp break;
9059 896e9b6f 2019-08-26 stsp } else {
9060 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9061 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9062 896e9b6f 2019-08-26 stsp if (error) {
9063 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9064 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9065 896e9b6f 2019-08-26 stsp break;
9066 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9067 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9068 896e9b6f 2019-08-26 stsp if (error)
9069 896e9b6f 2019-08-26 stsp break;
9070 896e9b6f 2019-08-26 stsp }
9071 896e9b6f 2019-08-26 stsp }
9072 01073a5d 2019-08-22 stsp
9073 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9074 01073a5d 2019-08-22 stsp if (error)
9075 01073a5d 2019-08-22 stsp break;
9076 01073a5d 2019-08-22 stsp
9077 01073a5d 2019-08-22 stsp switch (obj_type) {
9078 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9079 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9080 01073a5d 2019-08-22 stsp break;
9081 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9082 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9083 01073a5d 2019-08-22 stsp break;
9084 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9085 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9086 01073a5d 2019-08-22 stsp break;
9087 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9088 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9089 01073a5d 2019-08-22 stsp break;
9090 01073a5d 2019-08-22 stsp default:
9091 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9092 01073a5d 2019-08-22 stsp break;
9093 01073a5d 2019-08-22 stsp }
9094 01073a5d 2019-08-22 stsp if (error)
9095 01073a5d 2019-08-22 stsp break;
9096 01073a5d 2019-08-22 stsp free(label);
9097 01073a5d 2019-08-22 stsp label = NULL;
9098 01073a5d 2019-08-22 stsp free(id);
9099 01073a5d 2019-08-22 stsp id = NULL;
9100 01073a5d 2019-08-22 stsp }
9101 01073a5d 2019-08-22 stsp done:
9102 01073a5d 2019-08-22 stsp free(label);
9103 01073a5d 2019-08-22 stsp free(id);
9104 896e9b6f 2019-08-26 stsp free(commit_id);
9105 01073a5d 2019-08-22 stsp if (worktree)
9106 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9107 01073a5d 2019-08-22 stsp if (repo) {
9108 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9109 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9110 01073a5d 2019-08-22 stsp if (error == NULL)
9111 01073a5d 2019-08-22 stsp error = repo_error;
9112 01073a5d 2019-08-22 stsp }
9113 0ebf8283 2019-07-24 stsp return error;
9114 0ebf8283 2019-07-24 stsp }