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 659e7fbd 2020-03-20 stsp fprintf(stderr, "usage: %s clone [-a] [-m] [-q] [-v] repository-url "
815 8a8b05ce 2020-03-19 stsp "[target-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 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
889 93658fb9 2020-03-18 stsp {
890 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
891 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
892 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
893 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
894 bb64b798 2020-03-18 stsp const char *repo_path;
895 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
896 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
897 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
898 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
899 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
900 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
901 b46f3e71 2020-03-18 stsp char *git_url = NULL;
902 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
903 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
904 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
905 b46f3e71 2020-03-18 stsp ssize_t n;
906 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
907 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
908 93658fb9 2020-03-18 stsp
909 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
910 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
911 d9b4d0c0 2020-03-18 stsp
912 659e7fbd 2020-03-20 stsp while ((ch = getopt(argc, argv, "amvq")) != -1) {
913 93658fb9 2020-03-18 stsp switch (ch) {
914 659e7fbd 2020-03-20 stsp case 'a':
915 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
916 659e7fbd 2020-03-20 stsp break;
917 469dd726 2020-03-20 stsp case 'm':
918 469dd726 2020-03-20 stsp mirror_references = 1;
919 469dd726 2020-03-20 stsp break;
920 68999b92 2020-03-18 stsp case 'v':
921 68999b92 2020-03-18 stsp if (verbosity < 0)
922 68999b92 2020-03-18 stsp verbosity = 0;
923 68999b92 2020-03-18 stsp else if (verbosity < 3)
924 68999b92 2020-03-18 stsp verbosity++;
925 68999b92 2020-03-18 stsp break;
926 68999b92 2020-03-18 stsp case 'q':
927 68999b92 2020-03-18 stsp verbosity = -1;
928 68999b92 2020-03-18 stsp break;
929 93658fb9 2020-03-18 stsp default:
930 93658fb9 2020-03-18 stsp usage_clone();
931 93658fb9 2020-03-18 stsp break;
932 93658fb9 2020-03-18 stsp }
933 93658fb9 2020-03-18 stsp }
934 93658fb9 2020-03-18 stsp argc -= optind;
935 93658fb9 2020-03-18 stsp argv += optind;
936 39c64a6a 2020-03-18 stsp
937 93658fb9 2020-03-18 stsp uri = argv[0];
938 9df6f38b 2020-03-18 stsp
939 9df6f38b 2020-03-18 stsp if (argc == 1)
940 93658fb9 2020-03-18 stsp dirname = NULL;
941 9df6f38b 2020-03-18 stsp else if (argc == 2)
942 93658fb9 2020-03-18 stsp dirname = argv[1];
943 93658fb9 2020-03-18 stsp else
944 93658fb9 2020-03-18 stsp usage_clone();
945 09838ffc 2020-03-18 stsp
946 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
948 39c64a6a 2020-03-18 stsp if (error)
949 09f63084 2020-03-20 stsp goto done;
950 09f63084 2020-03-20 stsp
951 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
952 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
953 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
954 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
955 09838ffc 2020-03-18 stsp goto done;
956 09f63084 2020-03-20 stsp }
957 09838ffc 2020-03-18 stsp
958 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
959 b46f3e71 2020-03-18 stsp #ifndef PROFILE
960 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
961 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
962 39c64a6a 2020-03-18 stsp err(1, "pledge");
963 b46f3e71 2020-03-18 stsp #endif
964 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
965 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
966 b46f3e71 2020-03-18 stsp #ifndef PROFILE
967 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
968 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
969 39c64a6a 2020-03-18 stsp err(1, "pledge");
970 b46f3e71 2020-03-18 stsp #endif
971 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
972 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
973 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
974 39c64a6a 2020-03-18 stsp goto done;
975 39c64a6a 2020-03-18 stsp } else {
976 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
977 39c64a6a 2020-03-18 stsp goto done;
978 39c64a6a 2020-03-18 stsp }
979 bb64b798 2020-03-18 stsp if (dirname == NULL) {
980 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
981 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
982 bb64b798 2020-03-18 stsp goto done;
983 bb64b798 2020-03-18 stsp }
984 bb64b798 2020-03-18 stsp repo_path = default_destdir;
985 bb64b798 2020-03-18 stsp } else
986 bb64b798 2020-03-18 stsp repo_path = dirname;
987 bb64b798 2020-03-18 stsp
988 39c64a6a 2020-03-18 stsp error = got_path_mkdir(repo_path);
989 39c64a6a 2020-03-18 stsp if (error)
990 bb64b798 2020-03-18 stsp goto done;
991 bb64b798 2020-03-18 stsp
992 39c64a6a 2020-03-18 stsp error = got_repo_init(repo_path);
993 39c64a6a 2020-03-18 stsp if (error)
994 bb64b798 2020-03-18 stsp goto done;
995 bb64b798 2020-03-18 stsp
996 39c64a6a 2020-03-18 stsp error = got_repo_open(&repo, repo_path, NULL);
997 39c64a6a 2020-03-18 stsp if (error)
998 294dfefd 2020-03-18 stsp goto done;
999 294dfefd 2020-03-18 stsp
1000 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1001 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1002 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1003 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1004 ee448f5f 2020-03-18 stsp goto done;
1005 ee448f5f 2020-03-18 stsp }
1006 ee448f5f 2020-03-18 stsp }
1007 ee448f5f 2020-03-18 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1008 ee448f5f 2020-03-18 stsp if (error)
1009 ee448f5f 2020-03-18 stsp goto done;
1010 ee448f5f 2020-03-18 stsp
1011 68999b92 2020-03-18 stsp error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1012 68999b92 2020-03-18 stsp verbosity);
1013 39c64a6a 2020-03-18 stsp if (error)
1014 bb64b798 2020-03-18 stsp goto done;
1015 294dfefd 2020-03-18 stsp
1016 68999b92 2020-03-18 stsp if (verbosity >= 0)
1017 62a4c94c 2020-03-20 stsp printf("Connected to %s%s%s\n", host,
1018 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1019 bb64b798 2020-03-18 stsp
1020 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1021 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1022 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1023 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1024 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1025 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1026 659e7fbd 2020-03-20 stsp fetch_all_branches, fetchfd, repo, fetch_progress, &fpa);
1027 39c64a6a 2020-03-18 stsp if (error)
1028 d9b4d0c0 2020-03-18 stsp goto done;
1029 d9b4d0c0 2020-03-18 stsp
1030 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1031 39c64a6a 2020-03-18 stsp if (error)
1032 d9b4d0c0 2020-03-18 stsp goto done;
1033 68999b92 2020-03-18 stsp if (verbosity >= 0)
1034 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1035 d9b4d0c0 2020-03-18 stsp free(id_str);
1036 d9b4d0c0 2020-03-18 stsp
1037 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1038 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1039 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1040 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1041 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1042 7ebc0570 2020-03-18 stsp char *remote_refname;
1043 668a20f6 2020-03-18 stsp
1044 39c64a6a 2020-03-18 stsp error = got_ref_alloc(&ref, refname, id);
1045 39c64a6a 2020-03-18 stsp if (error)
1046 d9b4d0c0 2020-03-18 stsp goto done;
1047 7ebc0570 2020-03-18 stsp error = got_ref_write(ref, repo);
1048 7ebc0570 2020-03-18 stsp got_ref_close(ref);
1049 7ebc0570 2020-03-18 stsp if (error)
1050 7ebc0570 2020-03-18 stsp goto done;
1051 d9b4d0c0 2020-03-18 stsp
1052 469dd726 2020-03-20 stsp if (mirror_references)
1053 469dd726 2020-03-20 stsp continue;
1054 469dd726 2020-03-20 stsp
1055 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1056 7ebc0570 2020-03-18 stsp continue;
1057 7ebc0570 2020-03-18 stsp
1058 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1059 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1060 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1061 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1062 7ebc0570 2020-03-18 stsp goto done;
1063 7ebc0570 2020-03-18 stsp }
1064 7ebc0570 2020-03-18 stsp error = got_ref_alloc(&ref, remote_refname, id);
1065 39c64a6a 2020-03-18 stsp if (error)
1066 d9b4d0c0 2020-03-18 stsp goto done;
1067 39c64a6a 2020-03-18 stsp error = got_ref_write(ref, repo);
1068 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1069 39c64a6a 2020-03-18 stsp if (error)
1070 d9b4d0c0 2020-03-18 stsp goto done;
1071 d9b4d0c0 2020-03-18 stsp }
1072 d9b4d0c0 2020-03-18 stsp
1073 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1074 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1075 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1076 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1077 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1078 d9b4d0c0 2020-03-18 stsp
1079 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1080 d9b4d0c0 2020-03-18 stsp continue;
1081 d9b4d0c0 2020-03-18 stsp
1082 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1083 39c64a6a 2020-03-18 stsp if (error) {
1084 39c64a6a 2020-03-18 stsp if (error->code == GOT_ERR_NOT_REF)
1085 d9b4d0c0 2020-03-18 stsp continue;
1086 d9b4d0c0 2020-03-18 stsp goto done;
1087 d9b4d0c0 2020-03-18 stsp }
1088 d9b4d0c0 2020-03-18 stsp
1089 659e7fbd 2020-03-20 stsp error = got_ref_alloc_symref(&head_symref,
1090 659e7fbd 2020-03-20 stsp GOT_REF_HEAD, target_ref);
1091 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1092 39c64a6a 2020-03-18 stsp if (error)
1093 d9b4d0c0 2020-03-18 stsp goto done;
1094 d9b4d0c0 2020-03-18 stsp
1095 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1096 68999b92 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1097 659e7fbd 2020-03-20 stsp got_ref_get_symref_target(head_symref));
1098 d9b4d0c0 2020-03-18 stsp
1099 659e7fbd 2020-03-20 stsp error = got_ref_write(head_symref, repo);
1100 d9b4d0c0 2020-03-18 stsp break;
1101 659e7fbd 2020-03-20 stsp }
1102 659e7fbd 2020-03-20 stsp
1103 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1104 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1105 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1106 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1107 659e7fbd 2020-03-20 stsp goto done;
1108 659e7fbd 2020-03-20 stsp }
1109 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1110 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1111 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1112 659e7fbd 2020-03-20 stsp goto done;
1113 b46f3e71 2020-03-18 stsp }
1114 659e7fbd 2020-03-20 stsp if (mirror_references) {
1115 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1116 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1117 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1118 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1119 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1120 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1121 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1122 659e7fbd 2020-03-20 stsp goto done;
1123 659e7fbd 2020-03-20 stsp }
1124 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1125 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1126 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1127 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1128 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1129 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1130 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1131 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1132 659e7fbd 2020-03-20 stsp goto done;
1133 659e7fbd 2020-03-20 stsp }
1134 659e7fbd 2020-03-20 stsp } else {
1135 659e7fbd 2020-03-20 stsp const char *branchname;
1136 d715f13e 2020-03-19 stsp
1137 659e7fbd 2020-03-20 stsp /*
1138 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1139 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1140 659e7fbd 2020-03-20 stsp */
1141 659e7fbd 2020-03-20 stsp if (head_symref) {
1142 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1143 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1144 659e7fbd 2020-03-20 stsp branchname += 11;
1145 659e7fbd 2020-03-20 stsp } else
1146 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1147 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1148 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1149 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1150 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1151 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1152 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1153 659e7fbd 2020-03-20 stsp branchname) == -1) {
1154 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1155 659e7fbd 2020-03-20 stsp goto done;
1156 659e7fbd 2020-03-20 stsp }
1157 659e7fbd 2020-03-20 stsp }
1158 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1159 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1160 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1161 659e7fbd 2020-03-20 stsp goto done;
1162 659e7fbd 2020-03-20 stsp }
1163 659e7fbd 2020-03-20 stsp
1164 659e7fbd 2020-03-20 stsp
1165 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1166 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1167 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1168 09838ffc 2020-03-18 stsp done:
1169 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1170 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1171 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1172 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1173 bb64b798 2020-03-18 stsp if (repo)
1174 bb64b798 2020-03-18 stsp got_repo_close(repo);
1175 659e7fbd 2020-03-20 stsp if (head_symref)
1176 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1177 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1178 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1179 d9b4d0c0 2020-03-18 stsp free(pe->data);
1180 d9b4d0c0 2020-03-18 stsp }
1181 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1182 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1183 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1184 d9b4d0c0 2020-03-18 stsp free(pe->data);
1185 d9b4d0c0 2020-03-18 stsp }
1186 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1187 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1188 09838ffc 2020-03-18 stsp free(proto);
1189 09838ffc 2020-03-18 stsp free(host);
1190 09838ffc 2020-03-18 stsp free(port);
1191 09838ffc 2020-03-18 stsp free(server_path);
1192 09838ffc 2020-03-18 stsp free(repo_name);
1193 bb64b798 2020-03-18 stsp free(default_destdir);
1194 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1195 b46f3e71 2020-03-18 stsp free(git_url);
1196 39c64a6a 2020-03-18 stsp return error;
1197 7848a0e1 2020-03-19 stsp }
1198 7848a0e1 2020-03-19 stsp
1199 7848a0e1 2020-03-19 stsp static const struct got_error *
1200 7848a0e1 2020-03-19 stsp create_ref(const char *refname, struct got_object_id *id,
1201 7848a0e1 2020-03-19 stsp const char *id_str, struct got_repository *repo)
1202 7848a0e1 2020-03-19 stsp {
1203 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1204 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1205 7848a0e1 2020-03-19 stsp
1206 7848a0e1 2020-03-19 stsp printf("Creating %s: %s\n", refname, id_str);
1207 7848a0e1 2020-03-19 stsp
1208 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&ref, refname, id);
1209 7848a0e1 2020-03-19 stsp if (err)
1210 7848a0e1 2020-03-19 stsp return err;
1211 7848a0e1 2020-03-19 stsp
1212 7848a0e1 2020-03-19 stsp err = got_ref_write(ref, repo);
1213 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1214 7848a0e1 2020-03-19 stsp return err;
1215 7848a0e1 2020-03-19 stsp }
1216 7848a0e1 2020-03-19 stsp
1217 7848a0e1 2020-03-19 stsp static const struct got_error *
1218 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1219 7848a0e1 2020-03-19 stsp struct got_repository *repo)
1220 7848a0e1 2020-03-19 stsp {
1221 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1222 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1223 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1224 7848a0e1 2020-03-19 stsp
1225 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1226 7848a0e1 2020-03-19 stsp if (err)
1227 7848a0e1 2020-03-19 stsp goto done;
1228 7848a0e1 2020-03-19 stsp
1229 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1230 7848a0e1 2020-03-19 stsp struct got_reference *new_ref;
1231 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1232 7848a0e1 2020-03-19 stsp if (err)
1233 7848a0e1 2020-03-19 stsp goto done;
1234 7848a0e1 2020-03-19 stsp printf("Deleting symbolic reference %s -> %s\n",
1235 7848a0e1 2020-03-19 stsp got_ref_get_name(ref), got_ref_get_symref_target(ref));
1236 7848a0e1 2020-03-19 stsp err = got_ref_delete(ref, repo);
1237 7848a0e1 2020-03-19 stsp if (err)
1238 7848a0e1 2020-03-19 stsp goto done;
1239 7848a0e1 2020-03-19 stsp printf("Setting %s to %s\n", got_ref_get_name(ref),
1240 7848a0e1 2020-03-19 stsp new_id_str);
1241 7848a0e1 2020-03-19 stsp err = got_ref_write(new_ref, repo);
1242 7848a0e1 2020-03-19 stsp if (err)
1243 7848a0e1 2020-03-19 stsp goto done;
1244 7848a0e1 2020-03-19 stsp } else {
1245 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1246 7848a0e1 2020-03-19 stsp if (err)
1247 7848a0e1 2020-03-19 stsp goto done;
1248 7848a0e1 2020-03-19 stsp if (got_object_id_cmp(old_id, new_id) != 0) {
1249 7848a0e1 2020-03-19 stsp printf("Setting %s to %s\n",
1250 7848a0e1 2020-03-19 stsp got_ref_get_name(ref), new_id_str);
1251 7848a0e1 2020-03-19 stsp err = got_ref_change_ref(ref, new_id);
1252 7848a0e1 2020-03-19 stsp if (err)
1253 7848a0e1 2020-03-19 stsp goto done;
1254 7848a0e1 2020-03-19 stsp err = got_ref_write(ref, repo);
1255 7848a0e1 2020-03-19 stsp if (err)
1256 7848a0e1 2020-03-19 stsp goto done;
1257 7848a0e1 2020-03-19 stsp }
1258 7848a0e1 2020-03-19 stsp }
1259 7848a0e1 2020-03-19 stsp done:
1260 7848a0e1 2020-03-19 stsp free(old_id);
1261 7848a0e1 2020-03-19 stsp free(new_id_str);
1262 7848a0e1 2020-03-19 stsp return err;
1263 2ab43947 2020-03-18 stsp }
1264 2ab43947 2020-03-18 stsp
1265 2ab43947 2020-03-18 stsp __dead static void
1266 7848a0e1 2020-03-19 stsp usage_fetch(void)
1267 7848a0e1 2020-03-19 stsp {
1268 659e7fbd 2020-03-20 stsp fprintf(stderr, "usage: %s fetch [-a] [-r repository-path] [-q] [-v] "
1269 7848a0e1 2020-03-19 stsp "[remote-repository-name]\n", getprogname());
1270 7848a0e1 2020-03-19 stsp exit(1);
1271 7848a0e1 2020-03-19 stsp }
1272 7848a0e1 2020-03-19 stsp
1273 7848a0e1 2020-03-19 stsp static const struct got_error *
1274 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1275 7848a0e1 2020-03-19 stsp {
1276 7848a0e1 2020-03-19 stsp const struct got_error *error = NULL;
1277 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1278 7848a0e1 2020-03-19 stsp const char *remote_name;
1279 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1280 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1281 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1282 7848a0e1 2020-03-19 stsp int nremotes;
1283 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1284 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1285 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1286 7848a0e1 2020-03-19 stsp struct got_pathlist_head refs, symrefs;
1287 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1288 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1289 7848a0e1 2020-03-19 stsp int i, ch, fetchfd = -1;
1290 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1291 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0;
1292 7848a0e1 2020-03-19 stsp
1293 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1294 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1295 7848a0e1 2020-03-19 stsp
1296 659e7fbd 2020-03-20 stsp while ((ch = getopt(argc, argv, "ar:vq")) != -1) {
1297 7848a0e1 2020-03-19 stsp switch (ch) {
1298 659e7fbd 2020-03-20 stsp case 'a':
1299 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1300 659e7fbd 2020-03-20 stsp break;
1301 7848a0e1 2020-03-19 stsp case 'r':
1302 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1303 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1304 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1305 7848a0e1 2020-03-19 stsp optarg);
1306 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1307 7848a0e1 2020-03-19 stsp break;
1308 7848a0e1 2020-03-19 stsp case 'v':
1309 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1310 7848a0e1 2020-03-19 stsp verbosity = 0;
1311 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1312 7848a0e1 2020-03-19 stsp verbosity++;
1313 7848a0e1 2020-03-19 stsp break;
1314 7848a0e1 2020-03-19 stsp case 'q':
1315 7848a0e1 2020-03-19 stsp verbosity = -1;
1316 7848a0e1 2020-03-19 stsp break;
1317 7848a0e1 2020-03-19 stsp default:
1318 7848a0e1 2020-03-19 stsp usage_fetch();
1319 7848a0e1 2020-03-19 stsp break;
1320 7848a0e1 2020-03-19 stsp }
1321 7848a0e1 2020-03-19 stsp }
1322 7848a0e1 2020-03-19 stsp argc -= optind;
1323 7848a0e1 2020-03-19 stsp argv += optind;
1324 7848a0e1 2020-03-19 stsp
1325 7848a0e1 2020-03-19 stsp if (argc == 0)
1326 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1327 7848a0e1 2020-03-19 stsp else if (argc == 1)
1328 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1329 7848a0e1 2020-03-19 stsp else
1330 7848a0e1 2020-03-19 stsp usage_fetch();
1331 7848a0e1 2020-03-19 stsp
1332 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1333 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1334 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1335 7848a0e1 2020-03-19 stsp goto done;
1336 7848a0e1 2020-03-19 stsp }
1337 7848a0e1 2020-03-19 stsp
1338 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1339 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1340 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1341 7848a0e1 2020-03-19 stsp goto done;
1342 7848a0e1 2020-03-19 stsp else
1343 7848a0e1 2020-03-19 stsp error = NULL;
1344 7848a0e1 2020-03-19 stsp if (worktree) {
1345 7848a0e1 2020-03-19 stsp repo_path =
1346 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1347 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1348 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1349 7848a0e1 2020-03-19 stsp if (error)
1350 7848a0e1 2020-03-19 stsp goto done;
1351 7848a0e1 2020-03-19 stsp } else {
1352 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1353 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1354 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1355 7848a0e1 2020-03-19 stsp goto done;
1356 7848a0e1 2020-03-19 stsp }
1357 7848a0e1 2020-03-19 stsp }
1358 7848a0e1 2020-03-19 stsp }
1359 7848a0e1 2020-03-19 stsp
1360 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1361 7848a0e1 2020-03-19 stsp if (error)
1362 7848a0e1 2020-03-19 stsp goto done;
1363 7848a0e1 2020-03-19 stsp
1364 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1365 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1366 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1367 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1368 7848a0e1 2020-03-19 stsp break;
1369 7848a0e1 2020-03-19 stsp }
1370 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1371 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1372 7848a0e1 2020-03-19 stsp goto done;
1373 7848a0e1 2020-03-19 stsp }
1374 7848a0e1 2020-03-19 stsp
1375 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1376 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1377 7848a0e1 2020-03-19 stsp if (error)
1378 7848a0e1 2020-03-19 stsp goto done;
1379 7848a0e1 2020-03-19 stsp
1380 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1381 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1382 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1383 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1384 7848a0e1 2020-03-19 stsp err(1, "pledge");
1385 7848a0e1 2020-03-19 stsp #endif
1386 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1387 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1388 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1389 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1390 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1391 7848a0e1 2020-03-19 stsp err(1, "pledge");
1392 7848a0e1 2020-03-19 stsp #endif
1393 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1394 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1395 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1396 7848a0e1 2020-03-19 stsp goto done;
1397 7848a0e1 2020-03-19 stsp } else {
1398 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1399 7848a0e1 2020-03-19 stsp goto done;
1400 7848a0e1 2020-03-19 stsp }
1401 7848a0e1 2020-03-19 stsp
1402 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1403 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1404 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1405 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1406 7848a0e1 2020-03-19 stsp goto done;
1407 7848a0e1 2020-03-19 stsp }
1408 7848a0e1 2020-03-19 stsp }
1409 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1410 7848a0e1 2020-03-19 stsp if (error)
1411 7848a0e1 2020-03-19 stsp goto done;
1412 7848a0e1 2020-03-19 stsp
1413 7848a0e1 2020-03-19 stsp error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1414 7848a0e1 2020-03-19 stsp verbosity);
1415 7848a0e1 2020-03-19 stsp if (error)
1416 7848a0e1 2020-03-19 stsp goto done;
1417 7848a0e1 2020-03-19 stsp
1418 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1419 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1420 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1421 7848a0e1 2020-03-19 stsp
1422 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1423 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1424 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1425 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1426 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1427 659e7fbd 2020-03-20 stsp remote->mirror_references, fetch_all_branches, fetchfd, repo,
1428 659e7fbd 2020-03-20 stsp fetch_progress, &fpa);
1429 7848a0e1 2020-03-19 stsp if (error)
1430 7848a0e1 2020-03-19 stsp goto done;
1431 7848a0e1 2020-03-19 stsp
1432 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1433 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1434 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1435 7848a0e1 2020-03-19 stsp goto done;
1436 7848a0e1 2020-03-19 stsp }
1437 7848a0e1 2020-03-19 stsp
1438 984065c8 2020-03-19 stsp if (verbosity >= 0) {
1439 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1440 984065c8 2020-03-19 stsp if (error)
1441 984065c8 2020-03-19 stsp goto done;
1442 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1443 984065c8 2020-03-19 stsp free(id_str);
1444 984065c8 2020-03-19 stsp id_str = NULL;
1445 984065c8 2020-03-19 stsp }
1446 7848a0e1 2020-03-19 stsp
1447 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1448 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1449 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1450 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1451 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1452 7848a0e1 2020-03-19 stsp char *remote_refname;
1453 7848a0e1 2020-03-19 stsp
1454 fed0b873 2020-03-20 stsp if (remote->mirror_references) {
1455 fed0b873 2020-03-20 stsp error = got_ref_alloc(&ref, refname, id);
1456 fed0b873 2020-03-20 stsp if (error)
1457 fed0b873 2020-03-20 stsp goto done;
1458 fed0b873 2020-03-20 stsp error = got_ref_write(ref, repo);
1459 fed0b873 2020-03-20 stsp got_ref_close(ref);
1460 fed0b873 2020-03-20 stsp if (error)
1461 fed0b873 2020-03-20 stsp goto done;
1462 fed0b873 2020-03-20 stsp continue;
1463 fed0b873 2020-03-20 stsp }
1464 fed0b873 2020-03-20 stsp
1465 7848a0e1 2020-03-19 stsp error = got_object_id_str(&id_str, id);
1466 7848a0e1 2020-03-19 stsp if (error)
1467 7848a0e1 2020-03-19 stsp goto done;
1468 7848a0e1 2020-03-19 stsp
1469 7848a0e1 2020-03-19 stsp if (strncmp("refs/tags/", refname, 10) == 0) {
1470 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, refname, 0);
1471 7848a0e1 2020-03-19 stsp if (error) {
1472 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1473 7848a0e1 2020-03-19 stsp goto done;
1474 7848a0e1 2020-03-19 stsp error = create_ref(refname, id, id_str, repo);
1475 7848a0e1 2020-03-19 stsp if (error)
1476 7848a0e1 2020-03-19 stsp goto done;
1477 7848a0e1 2020-03-19 stsp } else {
1478 7848a0e1 2020-03-19 stsp error = update_ref(ref, id, repo);
1479 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1480 7848a0e1 2020-03-19 stsp if (error)
1481 7848a0e1 2020-03-19 stsp goto done;
1482 7848a0e1 2020-03-19 stsp }
1483 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1484 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1485 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1486 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1487 7848a0e1 2020-03-19 stsp goto done;
1488 7848a0e1 2020-03-19 stsp }
1489 7848a0e1 2020-03-19 stsp
1490 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, remote_refname, 0);
1491 7848a0e1 2020-03-19 stsp if (error) {
1492 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1493 7848a0e1 2020-03-19 stsp goto done;
1494 e5083482 2020-03-20 stsp error = create_ref(remote_refname, id, id_str,
1495 e5083482 2020-03-20 stsp repo);
1496 7848a0e1 2020-03-19 stsp if (error)
1497 7848a0e1 2020-03-19 stsp goto done;
1498 7848a0e1 2020-03-19 stsp } else {
1499 7848a0e1 2020-03-19 stsp error = update_ref(ref, id, repo);
1500 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1501 7848a0e1 2020-03-19 stsp if (error)
1502 7848a0e1 2020-03-19 stsp goto done;
1503 7848a0e1 2020-03-19 stsp }
1504 7848a0e1 2020-03-19 stsp }
1505 7848a0e1 2020-03-19 stsp free(id_str);
1506 7848a0e1 2020-03-19 stsp id_str = NULL;
1507 7848a0e1 2020-03-19 stsp }
1508 7848a0e1 2020-03-19 stsp done:
1509 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1510 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1511 7848a0e1 2020-03-19 stsp if (repo)
1512 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1513 7848a0e1 2020-03-19 stsp if (worktree)
1514 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1515 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1516 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1517 7848a0e1 2020-03-19 stsp free(pe->data);
1518 7848a0e1 2020-03-19 stsp }
1519 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
1520 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1521 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1522 7848a0e1 2020-03-19 stsp free(pe->data);
1523 7848a0e1 2020-03-19 stsp }
1524 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
1525 7848a0e1 2020-03-19 stsp free(id_str);
1526 7848a0e1 2020-03-19 stsp free(cwd);
1527 7848a0e1 2020-03-19 stsp free(repo_path);
1528 7848a0e1 2020-03-19 stsp free(pack_hash);
1529 7848a0e1 2020-03-19 stsp free(proto);
1530 7848a0e1 2020-03-19 stsp free(host);
1531 7848a0e1 2020-03-19 stsp free(port);
1532 7848a0e1 2020-03-19 stsp free(server_path);
1533 7848a0e1 2020-03-19 stsp free(repo_name);
1534 7848a0e1 2020-03-19 stsp return error;
1535 7848a0e1 2020-03-19 stsp }
1536 7848a0e1 2020-03-19 stsp
1537 7848a0e1 2020-03-19 stsp
1538 7848a0e1 2020-03-19 stsp __dead static void
1539 2ab43947 2020-03-18 stsp usage_checkout(void)
1540 2ab43947 2020-03-18 stsp {
1541 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1542 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1543 2ab43947 2020-03-18 stsp exit(1);
1544 2ab43947 2020-03-18 stsp }
1545 2ab43947 2020-03-18 stsp
1546 2ab43947 2020-03-18 stsp static void
1547 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1548 2ab43947 2020-03-18 stsp {
1549 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1550 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1551 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1552 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1553 2ab43947 2020-03-18 stsp getprogname());
1554 2ab43947 2020-03-18 stsp }
1555 2ab43947 2020-03-18 stsp
1556 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1557 2ab43947 2020-03-18 stsp const char *worktree_path;
1558 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1559 2ab43947 2020-03-18 stsp };
1560 2ab43947 2020-03-18 stsp
1561 2ab43947 2020-03-18 stsp static const struct got_error *
1562 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1563 2ab43947 2020-03-18 stsp {
1564 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1565 2ab43947 2020-03-18 stsp
1566 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1567 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1568 2ab43947 2020-03-18 stsp return NULL;
1569 2ab43947 2020-03-18 stsp
1570 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1571 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1572 2ab43947 2020-03-18 stsp return NULL;
1573 2ab43947 2020-03-18 stsp }
1574 2ab43947 2020-03-18 stsp
1575 2ab43947 2020-03-18 stsp while (path[0] == '/')
1576 2ab43947 2020-03-18 stsp path++;
1577 2ab43947 2020-03-18 stsp
1578 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1579 2ab43947 2020-03-18 stsp return NULL;
1580 2ab43947 2020-03-18 stsp }
1581 2ab43947 2020-03-18 stsp
1582 2ab43947 2020-03-18 stsp static const struct got_error *
1583 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1584 2ab43947 2020-03-18 stsp {
1585 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1586 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1587 2ab43947 2020-03-18 stsp return NULL;
1588 2ab43947 2020-03-18 stsp }
1589 2ab43947 2020-03-18 stsp
1590 2ab43947 2020-03-18 stsp static const struct got_error *
1591 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1592 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1593 2ab43947 2020-03-18 stsp struct got_repository *repo)
1594 2ab43947 2020-03-18 stsp {
1595 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1596 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1597 2ab43947 2020-03-18 stsp
1598 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1599 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
1600 2ab43947 2020-03-18 stsp if (err)
1601 2ab43947 2020-03-18 stsp return err;
1602 2ab43947 2020-03-18 stsp
1603 2ab43947 2020-03-18 stsp if (yca_id == NULL)
1604 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1605 2ab43947 2020-03-18 stsp
1606 2ab43947 2020-03-18 stsp /*
1607 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
1608 2ab43947 2020-03-18 stsp * and the work tree's base commit.
1609 2ab43947 2020-03-18 stsp *
1610 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
1611 2ab43947 2020-03-18 stsp *
1612 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
1613 2ab43947 2020-03-18 stsp * \ /
1614 2ab43947 2020-03-18 stsp * C E
1615 2ab43947 2020-03-18 stsp * \ /
1616 2ab43947 2020-03-18 stsp * B (yca)
1617 2ab43947 2020-03-18 stsp * |
1618 2ab43947 2020-03-18 stsp * A
1619 2ab43947 2020-03-18 stsp *
1620 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
1621 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
1622 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
1623 2ab43947 2020-03-18 stsp */
1624 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
1625 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1626 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1627 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1628 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
1629 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1630 2ab43947 2020-03-18 stsp
1631 2ab43947 2020-03-18 stsp free(yca_id);
1632 2ab43947 2020-03-18 stsp return NULL;
1633 2ab43947 2020-03-18 stsp }
1634 2ab43947 2020-03-18 stsp
1635 2ab43947 2020-03-18 stsp static const struct got_error *
1636 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
1637 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
1638 2ab43947 2020-03-18 stsp struct got_repository *repo)
1639 2ab43947 2020-03-18 stsp {
1640 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1641 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
1642 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
1643 2ab43947 2020-03-18 stsp int is_same_branch = 0;
1644 2ab43947 2020-03-18 stsp
1645 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
1646 2ab43947 2020-03-18 stsp if (err)
1647 2ab43947 2020-03-18 stsp goto done;
1648 2ab43947 2020-03-18 stsp
1649 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1650 2ab43947 2020-03-18 stsp is_same_branch = 1;
1651 2ab43947 2020-03-18 stsp goto done;
1652 2ab43947 2020-03-18 stsp }
1653 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1654 2ab43947 2020-03-18 stsp is_same_branch = 1;
1655 2ab43947 2020-03-18 stsp goto done;
1656 2ab43947 2020-03-18 stsp }
1657 2ab43947 2020-03-18 stsp
1658 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
1659 2ab43947 2020-03-18 stsp if (err)
1660 2ab43947 2020-03-18 stsp goto done;
1661 2ab43947 2020-03-18 stsp
1662 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1663 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1664 2ab43947 2020-03-18 stsp if (err)
1665 2ab43947 2020-03-18 stsp goto done;
1666 2ab43947 2020-03-18 stsp
1667 2ab43947 2020-03-18 stsp for (;;) {
1668 2ab43947 2020-03-18 stsp struct got_object_id *id;
1669 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1670 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1671 2ab43947 2020-03-18 stsp if (err) {
1672 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1673 2ab43947 2020-03-18 stsp err = NULL;
1674 2ab43947 2020-03-18 stsp break;
1675 2ab43947 2020-03-18 stsp }
1676 2ab43947 2020-03-18 stsp
1677 2ab43947 2020-03-18 stsp if (id) {
1678 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1679 2ab43947 2020-03-18 stsp break;
1680 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
1681 2ab43947 2020-03-18 stsp is_same_branch = 1;
1682 2ab43947 2020-03-18 stsp break;
1683 2ab43947 2020-03-18 stsp }
1684 2ab43947 2020-03-18 stsp }
1685 2ab43947 2020-03-18 stsp }
1686 2ab43947 2020-03-18 stsp done:
1687 2ab43947 2020-03-18 stsp if (graph)
1688 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
1689 2ab43947 2020-03-18 stsp free(head_commit_id);
1690 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
1691 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
1692 2ab43947 2020-03-18 stsp return err;
1693 a367ff0f 2019-05-14 stsp }
1694 8069f636 2019-01-12 stsp
1695 4ed7e80c 2018-05-20 stsp static const struct got_error *
1696 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1697 4b6c9460 2020-03-05 stsp {
1698 4b6c9460 2020-03-05 stsp static char msg[512];
1699 4b6c9460 2020-03-05 stsp const char *branch_name;
1700 4b6c9460 2020-03-05 stsp
1701 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1702 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1703 4b6c9460 2020-03-05 stsp else
1704 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1705 4b6c9460 2020-03-05 stsp
1706 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1707 4b6c9460 2020-03-05 stsp branch_name += 11;
1708 4b6c9460 2020-03-05 stsp
1709 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1710 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1711 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1712 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1713 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1714 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1715 4b6c9460 2020-03-05 stsp
1716 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1717 4b6c9460 2020-03-05 stsp }
1718 4b6c9460 2020-03-05 stsp
1719 4b6c9460 2020-03-05 stsp static const struct got_error *
1720 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1721 c09a553d 2018-03-12 stsp {
1722 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1723 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1724 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1725 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1726 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1727 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1728 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1729 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1730 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1731 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1732 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1733 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1734 f2ea84fa 2019-07-27 stsp
1735 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1736 c09a553d 2018-03-12 stsp
1737 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1738 0bb8a95e 2018-03-12 stsp switch (ch) {
1739 08573d5b 2019-05-14 stsp case 'b':
1740 08573d5b 2019-05-14 stsp branch_name = optarg;
1741 08573d5b 2019-05-14 stsp break;
1742 8069f636 2019-01-12 stsp case 'c':
1743 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1744 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1745 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1746 bb51a5b4 2020-01-13 stsp break;
1747 bb51a5b4 2020-01-13 stsp case 'E':
1748 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1749 8069f636 2019-01-12 stsp break;
1750 0bb8a95e 2018-03-12 stsp case 'p':
1751 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1752 0bb8a95e 2018-03-12 stsp break;
1753 0bb8a95e 2018-03-12 stsp default:
1754 2deda0b9 2019-03-07 stsp usage_checkout();
1755 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1756 0bb8a95e 2018-03-12 stsp }
1757 0bb8a95e 2018-03-12 stsp }
1758 0bb8a95e 2018-03-12 stsp
1759 0bb8a95e 2018-03-12 stsp argc -= optind;
1760 0bb8a95e 2018-03-12 stsp argv += optind;
1761 0bb8a95e 2018-03-12 stsp
1762 6715a751 2018-03-16 stsp #ifndef PROFILE
1763 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1764 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1765 c09a553d 2018-03-12 stsp err(1, "pledge");
1766 6715a751 2018-03-16 stsp #endif
1767 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1768 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1769 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1770 76089277 2018-04-01 stsp if (repo_path == NULL)
1771 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1772 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1773 76089277 2018-04-01 stsp if (cwd == NULL) {
1774 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1775 76089277 2018-04-01 stsp goto done;
1776 76089277 2018-04-01 stsp }
1777 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1778 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1779 230a42bd 2019-05-11 jcs if (base == NULL) {
1780 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1781 230a42bd 2019-05-11 jcs path_prefix);
1782 230a42bd 2019-05-11 jcs goto done;
1783 230a42bd 2019-05-11 jcs }
1784 230a42bd 2019-05-11 jcs } else {
1785 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1786 230a42bd 2019-05-11 jcs if (base == NULL) {
1787 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1788 230a42bd 2019-05-11 jcs repo_path);
1789 230a42bd 2019-05-11 jcs goto done;
1790 230a42bd 2019-05-11 jcs }
1791 76089277 2018-04-01 stsp }
1792 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1793 c09a553d 2018-03-12 stsp if (dotgit)
1794 c09a553d 2018-03-12 stsp *dotgit = '\0';
1795 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1796 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1797 c09a553d 2018-03-12 stsp free(cwd);
1798 76089277 2018-04-01 stsp goto done;
1799 c09a553d 2018-03-12 stsp }
1800 c09a553d 2018-03-12 stsp free(cwd);
1801 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1802 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1803 76089277 2018-04-01 stsp if (repo_path == NULL) {
1804 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1805 76089277 2018-04-01 stsp goto done;
1806 76089277 2018-04-01 stsp }
1807 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1808 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1809 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1810 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1811 b4b3a7dd 2019-07-22 stsp argv[1]);
1812 b4b3a7dd 2019-07-22 stsp goto done;
1813 b4b3a7dd 2019-07-22 stsp }
1814 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1815 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1816 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1817 b4b3a7dd 2019-07-22 stsp goto done;
1818 b4b3a7dd 2019-07-22 stsp }
1819 76089277 2018-04-01 stsp }
1820 c09a553d 2018-03-12 stsp } else
1821 c09a553d 2018-03-12 stsp usage_checkout();
1822 c09a553d 2018-03-12 stsp
1823 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1824 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1825 13bfb272 2019-05-10 jcs
1826 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1827 c09a553d 2018-03-12 stsp if (error != NULL)
1828 c02c541e 2019-03-29 stsp goto done;
1829 c02c541e 2019-03-29 stsp
1830 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1831 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1832 c530dc23 2019-07-23 stsp if (error) {
1833 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1834 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1835 c530dc23 2019-07-23 stsp goto done;
1836 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1837 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1838 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1839 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1840 c530dc23 2019-07-23 stsp goto done;
1841 c530dc23 2019-07-23 stsp }
1842 c530dc23 2019-07-23 stsp }
1843 c530dc23 2019-07-23 stsp
1844 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1845 c02c541e 2019-03-29 stsp if (error)
1846 c09a553d 2018-03-12 stsp goto done;
1847 8069f636 2019-01-12 stsp
1848 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1849 c09a553d 2018-03-12 stsp if (error != NULL)
1850 c09a553d 2018-03-12 stsp goto done;
1851 c09a553d 2018-03-12 stsp
1852 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1853 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1854 c09a553d 2018-03-12 stsp goto done;
1855 c09a553d 2018-03-12 stsp
1856 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1857 c09a553d 2018-03-12 stsp if (error != NULL)
1858 c09a553d 2018-03-12 stsp goto done;
1859 c09a553d 2018-03-12 stsp
1860 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1861 e5dc7198 2018-12-29 stsp path_prefix);
1862 e5dc7198 2018-12-29 stsp if (error != NULL)
1863 e5dc7198 2018-12-29 stsp goto done;
1864 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1865 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1866 49520a32 2018-12-29 stsp goto done;
1867 49520a32 2018-12-29 stsp }
1868 49520a32 2018-12-29 stsp
1869 8069f636 2019-01-12 stsp if (commit_id_str) {
1870 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1871 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1872 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1873 30837e32 2019-07-25 stsp if (error)
1874 8069f636 2019-01-12 stsp goto done;
1875 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1876 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1877 8069f636 2019-01-12 stsp if (error != NULL) {
1878 8069f636 2019-01-12 stsp free(commit_id);
1879 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1880 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1881 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1882 4b6c9460 2020-03-05 stsp }
1883 8069f636 2019-01-12 stsp goto done;
1884 8069f636 2019-01-12 stsp }
1885 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1886 4b6c9460 2020-03-05 stsp if (error) {
1887 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1888 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1889 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1890 4b6c9460 2020-03-05 stsp }
1891 45d344f6 2019-05-14 stsp goto done;
1892 4b6c9460 2020-03-05 stsp }
1893 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1894 8069f636 2019-01-12 stsp commit_id);
1895 8069f636 2019-01-12 stsp free(commit_id);
1896 8069f636 2019-01-12 stsp if (error)
1897 8069f636 2019-01-12 stsp goto done;
1898 8069f636 2019-01-12 stsp }
1899 8069f636 2019-01-12 stsp
1900 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1901 f2ea84fa 2019-07-27 stsp if (error)
1902 f2ea84fa 2019-07-27 stsp goto done;
1903 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1904 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1905 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1906 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1907 c09a553d 2018-03-12 stsp if (error != NULL)
1908 c09a553d 2018-03-12 stsp goto done;
1909 c09a553d 2018-03-12 stsp
1910 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1911 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1912 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1913 c09a553d 2018-03-12 stsp done:
1914 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1915 8069f636 2019-01-12 stsp free(commit_id_str);
1916 76089277 2018-04-01 stsp free(repo_path);
1917 507dc3bb 2018-12-29 stsp free(worktree_path);
1918 507dc3bb 2018-12-29 stsp return error;
1919 507dc3bb 2018-12-29 stsp }
1920 507dc3bb 2018-12-29 stsp
1921 507dc3bb 2018-12-29 stsp __dead static void
1922 507dc3bb 2018-12-29 stsp usage_update(void)
1923 507dc3bb 2018-12-29 stsp {
1924 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1925 507dc3bb 2018-12-29 stsp getprogname());
1926 507dc3bb 2018-12-29 stsp exit(1);
1927 507dc3bb 2018-12-29 stsp }
1928 507dc3bb 2018-12-29 stsp
1929 1ee397ad 2019-07-12 stsp static const struct got_error *
1930 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1931 507dc3bb 2018-12-29 stsp {
1932 784955db 2019-01-12 stsp int *did_something = arg;
1933 784955db 2019-01-12 stsp
1934 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1935 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1936 1ee397ad 2019-07-12 stsp return NULL;
1937 507dc3bb 2018-12-29 stsp
1938 1545c615 2019-02-10 stsp *did_something = 1;
1939 a484d721 2019-06-10 stsp
1940 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1941 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1942 1ee397ad 2019-07-12 stsp return NULL;
1943 a484d721 2019-06-10 stsp
1944 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1945 507dc3bb 2018-12-29 stsp path++;
1946 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1947 1ee397ad 2019-07-12 stsp return NULL;
1948 be7061eb 2018-12-30 stsp }
1949 be7061eb 2018-12-30 stsp
1950 be7061eb 2018-12-30 stsp static const struct got_error *
1951 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1952 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1953 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1954 a1fb16d8 2019-05-24 stsp {
1955 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1956 a1fb16d8 2019-05-24 stsp char *base_id_str;
1957 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1958 a1fb16d8 2019-05-24 stsp
1959 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1960 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1961 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1962 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1963 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1964 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1965 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1966 a1fb16d8 2019-05-24 stsp }
1967 a1fb16d8 2019-05-24 stsp
1968 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1969 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1970 a1fb16d8 2019-05-24 stsp if (err) {
1971 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1972 a1fb16d8 2019-05-24 stsp return err;
1973 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1974 a1fb16d8 2019-05-24 stsp }
1975 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1976 a1fb16d8 2019-05-24 stsp return NULL;
1977 a1fb16d8 2019-05-24 stsp
1978 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1979 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1980 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1981 a1fb16d8 2019-05-24 stsp if (err)
1982 a1fb16d8 2019-05-24 stsp return err;
1983 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1984 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1985 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1986 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1987 0ebf8283 2019-07-24 stsp return NULL;
1988 0ebf8283 2019-07-24 stsp }
1989 0ebf8283 2019-07-24 stsp
1990 0ebf8283 2019-07-24 stsp static const struct got_error *
1991 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1992 0ebf8283 2019-07-24 stsp {
1993 0ebf8283 2019-07-24 stsp const struct got_error *err;
1994 0ebf8283 2019-07-24 stsp int in_progress;
1995 0ebf8283 2019-07-24 stsp
1996 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1997 0ebf8283 2019-07-24 stsp if (err)
1998 0ebf8283 2019-07-24 stsp return err;
1999 0ebf8283 2019-07-24 stsp if (in_progress)
2000 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2001 0ebf8283 2019-07-24 stsp
2002 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2003 0ebf8283 2019-07-24 stsp if (err)
2004 0ebf8283 2019-07-24 stsp return err;
2005 0ebf8283 2019-07-24 stsp if (in_progress)
2006 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2007 0ebf8283 2019-07-24 stsp
2008 a1fb16d8 2019-05-24 stsp return NULL;
2009 a5edda0a 2019-07-27 stsp }
2010 a5edda0a 2019-07-27 stsp
2011 a5edda0a 2019-07-27 stsp static const struct got_error *
2012 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2013 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2014 a5edda0a 2019-07-27 stsp {
2015 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2016 a5edda0a 2019-07-27 stsp char *path;
2017 a5edda0a 2019-07-27 stsp int i;
2018 a5edda0a 2019-07-27 stsp
2019 a5edda0a 2019-07-27 stsp if (argc == 0) {
2020 a5edda0a 2019-07-27 stsp path = strdup("");
2021 a5edda0a 2019-07-27 stsp if (path == NULL)
2022 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2023 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2024 a5edda0a 2019-07-27 stsp }
2025 a5edda0a 2019-07-27 stsp
2026 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2027 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2028 a5edda0a 2019-07-27 stsp if (err)
2029 a5edda0a 2019-07-27 stsp break;
2030 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2031 a5edda0a 2019-07-27 stsp if (err) {
2032 a5edda0a 2019-07-27 stsp free(path);
2033 a5edda0a 2019-07-27 stsp break;
2034 a5edda0a 2019-07-27 stsp }
2035 a5edda0a 2019-07-27 stsp }
2036 a5edda0a 2019-07-27 stsp
2037 a5edda0a 2019-07-27 stsp return err;
2038 a1fb16d8 2019-05-24 stsp }
2039 a1fb16d8 2019-05-24 stsp
2040 a1fb16d8 2019-05-24 stsp static const struct got_error *
2041 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2042 507dc3bb 2018-12-29 stsp {
2043 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2044 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2045 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2046 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2047 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2048 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2049 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2050 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2051 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2052 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2053 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2054 507dc3bb 2018-12-29 stsp
2055 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2056 f2ea84fa 2019-07-27 stsp
2057 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2058 507dc3bb 2018-12-29 stsp switch (ch) {
2059 024e9686 2019-05-14 stsp case 'b':
2060 024e9686 2019-05-14 stsp branch_name = optarg;
2061 024e9686 2019-05-14 stsp break;
2062 507dc3bb 2018-12-29 stsp case 'c':
2063 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2064 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2065 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2066 507dc3bb 2018-12-29 stsp break;
2067 507dc3bb 2018-12-29 stsp default:
2068 2deda0b9 2019-03-07 stsp usage_update();
2069 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2070 507dc3bb 2018-12-29 stsp }
2071 507dc3bb 2018-12-29 stsp }
2072 507dc3bb 2018-12-29 stsp
2073 507dc3bb 2018-12-29 stsp argc -= optind;
2074 507dc3bb 2018-12-29 stsp argv += optind;
2075 507dc3bb 2018-12-29 stsp
2076 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2077 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2078 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2079 507dc3bb 2018-12-29 stsp err(1, "pledge");
2080 507dc3bb 2018-12-29 stsp #endif
2081 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2082 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2083 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2084 c4cdcb68 2019-04-03 stsp goto done;
2085 c4cdcb68 2019-04-03 stsp }
2086 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2087 7d5807f4 2019-07-11 stsp if (error)
2088 7d5807f4 2019-07-11 stsp goto done;
2089 7d5807f4 2019-07-11 stsp
2090 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2091 f2ea84fa 2019-07-27 stsp if (error)
2092 f2ea84fa 2019-07-27 stsp goto done;
2093 507dc3bb 2018-12-29 stsp
2094 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2095 c9956ddf 2019-09-08 stsp NULL);
2096 507dc3bb 2018-12-29 stsp if (error != NULL)
2097 507dc3bb 2018-12-29 stsp goto done;
2098 507dc3bb 2018-12-29 stsp
2099 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2100 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2101 087fb88c 2019-08-04 stsp if (error)
2102 087fb88c 2019-08-04 stsp goto done;
2103 087fb88c 2019-08-04 stsp
2104 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2105 0266afb7 2019-01-04 stsp if (error)
2106 0266afb7 2019-01-04 stsp goto done;
2107 0266afb7 2019-01-04 stsp
2108 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2109 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2110 024e9686 2019-05-14 stsp if (error != NULL)
2111 024e9686 2019-05-14 stsp goto done;
2112 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2113 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2114 9c4b8182 2019-01-02 stsp if (error != NULL)
2115 9c4b8182 2019-01-02 stsp goto done;
2116 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2117 507dc3bb 2018-12-29 stsp if (error != NULL)
2118 507dc3bb 2018-12-29 stsp goto done;
2119 507dc3bb 2018-12-29 stsp } else {
2120 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2121 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2122 04f57cb3 2019-07-25 stsp free(commit_id_str);
2123 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2124 30837e32 2019-07-25 stsp if (error)
2125 a297e751 2019-07-11 stsp goto done;
2126 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2127 a297e751 2019-07-11 stsp if (error)
2128 507dc3bb 2018-12-29 stsp goto done;
2129 507dc3bb 2018-12-29 stsp }
2130 35c965b2 2018-12-29 stsp
2131 a1fb16d8 2019-05-24 stsp if (branch_name) {
2132 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2133 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2134 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2135 f2ea84fa 2019-07-27 stsp continue;
2136 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2137 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2138 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2139 024e9686 2019-05-14 stsp goto done;
2140 024e9686 2019-05-14 stsp }
2141 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2142 024e9686 2019-05-14 stsp if (error)
2143 024e9686 2019-05-14 stsp goto done;
2144 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2145 3aef623b 2019-10-15 stsp repo);
2146 a367ff0f 2019-05-14 stsp free(head_commit_id);
2147 024e9686 2019-05-14 stsp if (error != NULL)
2148 024e9686 2019-05-14 stsp goto done;
2149 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2150 a367ff0f 2019-05-14 stsp if (error)
2151 a367ff0f 2019-05-14 stsp goto done;
2152 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2153 024e9686 2019-05-14 stsp if (error)
2154 024e9686 2019-05-14 stsp goto done;
2155 024e9686 2019-05-14 stsp } else {
2156 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2157 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2158 a367ff0f 2019-05-14 stsp if (error != NULL) {
2159 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2160 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2161 024e9686 2019-05-14 stsp goto done;
2162 a367ff0f 2019-05-14 stsp }
2163 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2164 a367ff0f 2019-05-14 stsp if (error)
2165 a367ff0f 2019-05-14 stsp goto done;
2166 024e9686 2019-05-14 stsp }
2167 507dc3bb 2018-12-29 stsp
2168 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2169 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2170 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2171 507dc3bb 2018-12-29 stsp commit_id);
2172 507dc3bb 2018-12-29 stsp if (error)
2173 507dc3bb 2018-12-29 stsp goto done;
2174 507dc3bb 2018-12-29 stsp }
2175 507dc3bb 2018-12-29 stsp
2176 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2177 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2178 507dc3bb 2018-12-29 stsp if (error != NULL)
2179 507dc3bb 2018-12-29 stsp goto done;
2180 9c4b8182 2019-01-02 stsp
2181 784955db 2019-01-12 stsp if (did_something)
2182 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2183 784955db 2019-01-12 stsp else
2184 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2185 507dc3bb 2018-12-29 stsp done:
2186 c09a553d 2018-03-12 stsp free(worktree_path);
2187 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2188 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2189 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2190 507dc3bb 2018-12-29 stsp free(commit_id);
2191 9c4b8182 2019-01-02 stsp free(commit_id_str);
2192 c09a553d 2018-03-12 stsp return error;
2193 c09a553d 2018-03-12 stsp }
2194 c09a553d 2018-03-12 stsp
2195 f42b1b34 2018-03-12 stsp static const struct got_error *
2196 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2197 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2198 63035f9f 2019-10-06 stsp struct got_repository *repo)
2199 5c860e29 2018-03-12 stsp {
2200 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2201 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2202 79109fed 2018-03-27 stsp
2203 44392932 2019-08-25 stsp if (blob_id1) {
2204 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2205 44392932 2019-08-25 stsp if (err)
2206 44392932 2019-08-25 stsp goto done;
2207 44392932 2019-08-25 stsp }
2208 79109fed 2018-03-27 stsp
2209 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2210 44392932 2019-08-25 stsp if (err)
2211 44392932 2019-08-25 stsp goto done;
2212 79109fed 2018-03-27 stsp
2213 44392932 2019-08-25 stsp while (path[0] == '/')
2214 44392932 2019-08-25 stsp path++;
2215 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2216 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2217 44392932 2019-08-25 stsp done:
2218 44392932 2019-08-25 stsp if (blob1)
2219 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2220 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2221 44392932 2019-08-25 stsp return err;
2222 44392932 2019-08-25 stsp }
2223 44392932 2019-08-25 stsp
2224 44392932 2019-08-25 stsp static const struct got_error *
2225 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2226 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2227 63035f9f 2019-10-06 stsp struct got_repository *repo)
2228 44392932 2019-08-25 stsp {
2229 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2230 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2231 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2232 44392932 2019-08-25 stsp
2233 44392932 2019-08-25 stsp if (tree_id1) {
2234 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2235 79109fed 2018-03-27 stsp if (err)
2236 44392932 2019-08-25 stsp goto done;
2237 79109fed 2018-03-27 stsp }
2238 79109fed 2018-03-27 stsp
2239 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2240 0f2b3dca 2018-12-22 stsp if (err)
2241 0f2b3dca 2018-12-22 stsp goto done;
2242 0f2b3dca 2018-12-22 stsp
2243 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2244 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2245 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2246 44392932 2019-08-25 stsp while (path[0] == '/')
2247 44392932 2019-08-25 stsp path++;
2248 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2249 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2250 0f2b3dca 2018-12-22 stsp done:
2251 79109fed 2018-03-27 stsp if (tree1)
2252 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2253 366e0a5f 2019-10-10 stsp if (tree2)
2254 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2255 44392932 2019-08-25 stsp return err;
2256 44392932 2019-08-25 stsp }
2257 44392932 2019-08-25 stsp
2258 44392932 2019-08-25 stsp static const struct got_error *
2259 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2260 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2261 44392932 2019-08-25 stsp {
2262 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2263 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2264 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2265 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2266 44392932 2019-08-25 stsp struct got_object_qid *qid;
2267 44392932 2019-08-25 stsp
2268 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2269 44392932 2019-08-25 stsp if (qid != NULL) {
2270 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2271 44392932 2019-08-25 stsp qid->id);
2272 44392932 2019-08-25 stsp if (err)
2273 44392932 2019-08-25 stsp return err;
2274 44392932 2019-08-25 stsp }
2275 44392932 2019-08-25 stsp
2276 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2277 44392932 2019-08-25 stsp int obj_type;
2278 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2279 44392932 2019-08-25 stsp if (err)
2280 44392932 2019-08-25 stsp goto done;
2281 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2282 44392932 2019-08-25 stsp if (err) {
2283 44392932 2019-08-25 stsp free(obj_id2);
2284 44392932 2019-08-25 stsp goto done;
2285 44392932 2019-08-25 stsp }
2286 44392932 2019-08-25 stsp if (pcommit) {
2287 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2288 44392932 2019-08-25 stsp qid->id, path);
2289 44392932 2019-08-25 stsp if (err) {
2290 44392932 2019-08-25 stsp free(obj_id2);
2291 44392932 2019-08-25 stsp goto done;
2292 44392932 2019-08-25 stsp }
2293 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2294 44392932 2019-08-25 stsp if (err) {
2295 44392932 2019-08-25 stsp free(obj_id2);
2296 44392932 2019-08-25 stsp goto done;
2297 44392932 2019-08-25 stsp }
2298 44392932 2019-08-25 stsp }
2299 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2300 44392932 2019-08-25 stsp if (err) {
2301 44392932 2019-08-25 stsp free(obj_id2);
2302 44392932 2019-08-25 stsp goto done;
2303 44392932 2019-08-25 stsp }
2304 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2305 44392932 2019-08-25 stsp switch (obj_type) {
2306 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2307 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2308 63035f9f 2019-10-06 stsp 0, repo);
2309 44392932 2019-08-25 stsp break;
2310 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2311 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2312 63035f9f 2019-10-06 stsp 0, repo);
2313 44392932 2019-08-25 stsp break;
2314 44392932 2019-08-25 stsp default:
2315 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2316 44392932 2019-08-25 stsp break;
2317 44392932 2019-08-25 stsp }
2318 44392932 2019-08-25 stsp free(obj_id1);
2319 44392932 2019-08-25 stsp free(obj_id2);
2320 44392932 2019-08-25 stsp } else {
2321 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2322 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2323 44392932 2019-08-25 stsp if (err)
2324 44392932 2019-08-25 stsp goto done;
2325 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2326 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2327 44392932 2019-08-25 stsp if (err)
2328 44392932 2019-08-25 stsp goto done;
2329 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2330 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2331 44392932 2019-08-25 stsp }
2332 44392932 2019-08-25 stsp done:
2333 0f2b3dca 2018-12-22 stsp free(id_str1);
2334 0f2b3dca 2018-12-22 stsp free(id_str2);
2335 44392932 2019-08-25 stsp if (pcommit)
2336 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2337 79109fed 2018-03-27 stsp return err;
2338 79109fed 2018-03-27 stsp }
2339 79109fed 2018-03-27 stsp
2340 4bb494d5 2018-06-16 stsp static char *
2341 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2342 6c281f94 2018-06-11 stsp {
2343 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2344 09867e48 2019-08-13 stsp char *p, *s;
2345 09867e48 2019-08-13 stsp
2346 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2347 09867e48 2019-08-13 stsp if (tm == NULL)
2348 09867e48 2019-08-13 stsp return NULL;
2349 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2350 09867e48 2019-08-13 stsp if (s == NULL)
2351 09867e48 2019-08-13 stsp return NULL;
2352 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2353 6c281f94 2018-06-11 stsp if (p)
2354 6c281f94 2018-06-11 stsp *p = '\0';
2355 6c281f94 2018-06-11 stsp return s;
2356 6c281f94 2018-06-11 stsp }
2357 dc424a06 2019-08-07 stsp
2358 6841bf13 2019-11-29 kn static const struct got_error *
2359 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2360 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2361 6841bf13 2019-11-29 kn {
2362 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2363 6841bf13 2019-11-29 kn regmatch_t regmatch;
2364 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2365 6841bf13 2019-11-29 kn
2366 6841bf13 2019-11-29 kn *have_match = 0;
2367 6841bf13 2019-11-29 kn
2368 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2369 6841bf13 2019-11-29 kn if (err)
2370 6841bf13 2019-11-29 kn return err;
2371 6841bf13 2019-11-29 kn
2372 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2373 6841bf13 2019-11-29 kn if (err)
2374 6841bf13 2019-11-29 kn goto done;
2375 6841bf13 2019-11-29 kn
2376 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2377 6841bf13 2019-11-29 kn *have_match = 1;
2378 6841bf13 2019-11-29 kn done:
2379 6841bf13 2019-11-29 kn free(id_str);
2380 6841bf13 2019-11-29 kn free(logmsg);
2381 6841bf13 2019-11-29 kn return err;
2382 6841bf13 2019-11-29 kn }
2383 6841bf13 2019-11-29 kn
2384 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2385 6c281f94 2018-06-11 stsp
2386 79109fed 2018-03-27 stsp static const struct got_error *
2387 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2388 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2389 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2390 79109fed 2018-03-27 stsp {
2391 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2392 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2393 6c281f94 2018-06-11 stsp char datebuf[26];
2394 45d799e2 2018-12-23 stsp time_t committer_time;
2395 45d799e2 2018-12-23 stsp const char *author, *committer;
2396 199a4027 2019-02-02 stsp char *refs_str = NULL;
2397 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2398 5c860e29 2018-03-12 stsp
2399 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2400 199a4027 2019-02-02 stsp char *s;
2401 199a4027 2019-02-02 stsp const char *name;
2402 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2403 a436ad14 2019-08-13 stsp int cmp;
2404 a436ad14 2019-08-13 stsp
2405 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2406 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2407 d9498b20 2019-02-05 stsp continue;
2408 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2409 199a4027 2019-02-02 stsp name += 5;
2410 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2411 7143d404 2019-03-12 stsp continue;
2412 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2413 e34f9ed6 2019-02-02 stsp name += 6;
2414 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2415 141c2bff 2019-02-04 stsp name += 8;
2416 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2417 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2418 5d844a1e 2019-08-13 stsp if (err) {
2419 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2420 5d844a1e 2019-08-13 stsp return err;
2421 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2422 5d844a1e 2019-08-13 stsp err = NULL;
2423 5d844a1e 2019-08-13 stsp tag = NULL;
2424 5d844a1e 2019-08-13 stsp }
2425 a436ad14 2019-08-13 stsp }
2426 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2427 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2428 a436ad14 2019-08-13 stsp if (tag)
2429 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2430 a436ad14 2019-08-13 stsp if (cmp != 0)
2431 a436ad14 2019-08-13 stsp continue;
2432 199a4027 2019-02-02 stsp s = refs_str;
2433 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2434 199a4027 2019-02-02 stsp name) == -1) {
2435 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2436 199a4027 2019-02-02 stsp free(s);
2437 34ca4898 2019-08-13 stsp return err;
2438 199a4027 2019-02-02 stsp }
2439 199a4027 2019-02-02 stsp free(s);
2440 199a4027 2019-02-02 stsp }
2441 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2442 8bf5b3c9 2018-03-17 stsp if (err)
2443 8bf5b3c9 2018-03-17 stsp return err;
2444 788c352e 2018-06-16 stsp
2445 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2446 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2447 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2448 832c249c 2018-06-10 stsp free(id_str);
2449 d3d493d7 2019-02-21 stsp id_str = NULL;
2450 d3d493d7 2019-02-21 stsp free(refs_str);
2451 d3d493d7 2019-02-21 stsp refs_str = NULL;
2452 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2453 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2454 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2455 09867e48 2019-08-13 stsp if (datestr)
2456 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2457 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2458 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2459 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2460 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2461 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2462 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2463 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2464 3fe1abad 2018-06-10 stsp int n = 1;
2465 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2466 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2467 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2468 a0603db2 2018-06-10 stsp if (err)
2469 a0603db2 2018-06-10 stsp return err;
2470 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2471 a0603db2 2018-06-10 stsp free(id_str);
2472 a0603db2 2018-06-10 stsp }
2473 a0603db2 2018-06-10 stsp }
2474 832c249c 2018-06-10 stsp
2475 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2476 5943eee2 2019-08-13 stsp if (err)
2477 5943eee2 2019-08-13 stsp return err;
2478 8bf5b3c9 2018-03-17 stsp
2479 621015ac 2018-07-23 stsp logmsg = logmsg0;
2480 832c249c 2018-06-10 stsp do {
2481 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2482 832c249c 2018-06-10 stsp if (line)
2483 832c249c 2018-06-10 stsp printf(" %s\n", line);
2484 832c249c 2018-06-10 stsp } while (line);
2485 621015ac 2018-07-23 stsp free(logmsg0);
2486 832c249c 2018-06-10 stsp
2487 971751ac 2018-03-27 stsp if (show_patch) {
2488 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2489 971751ac 2018-03-27 stsp if (err == 0)
2490 971751ac 2018-03-27 stsp printf("\n");
2491 971751ac 2018-03-27 stsp }
2492 07862c20 2018-09-15 stsp
2493 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2494 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2495 07862c20 2018-09-15 stsp return err;
2496 f42b1b34 2018-03-12 stsp }
2497 5c860e29 2018-03-12 stsp
2498 f42b1b34 2018-03-12 stsp static const struct got_error *
2499 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2500 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2501 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2502 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2503 f42b1b34 2018-03-12 stsp {
2504 f42b1b34 2018-03-12 stsp const struct got_error *err;
2505 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2506 6841bf13 2019-11-29 kn regex_t regex;
2507 6841bf13 2019-11-29 kn int have_match;
2508 372ccdbb 2018-06-10 stsp
2509 6841bf13 2019-11-29 kn if (search_pattern &&
2510 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2511 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2512 6841bf13 2019-11-29 kn
2513 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2514 f42b1b34 2018-03-12 stsp if (err)
2515 f42b1b34 2018-03-12 stsp return err;
2516 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2517 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2518 372ccdbb 2018-06-10 stsp if (err)
2519 fcc85cad 2018-07-22 stsp goto done;
2520 656b1f76 2019-05-11 jcs for (;;) {
2521 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2522 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2523 8bf5b3c9 2018-03-17 stsp
2524 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2525 84453469 2018-11-11 stsp break;
2526 84453469 2018-11-11 stsp
2527 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2528 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2529 372ccdbb 2018-06-10 stsp if (err) {
2530 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2531 9ba79e04 2018-06-11 stsp err = NULL;
2532 ee780d5c 2020-01-04 stsp break;
2533 7e665116 2018-04-02 stsp }
2534 b43fbaa0 2018-06-11 stsp if (id == NULL)
2535 7e665116 2018-04-02 stsp break;
2536 b43fbaa0 2018-06-11 stsp
2537 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2538 b43fbaa0 2018-06-11 stsp if (err)
2539 fcc85cad 2018-07-22 stsp break;
2540 6841bf13 2019-11-29 kn
2541 6841bf13 2019-11-29 kn if (search_pattern) {
2542 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
2543 6841bf13 2019-11-29 kn if (err) {
2544 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2545 6841bf13 2019-11-29 kn break;
2546 6841bf13 2019-11-29 kn }
2547 6841bf13 2019-11-29 kn if (have_match == 0) {
2548 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2549 6841bf13 2019-11-29 kn continue;
2550 6841bf13 2019-11-29 kn }
2551 6841bf13 2019-11-29 kn }
2552 6841bf13 2019-11-29 kn
2553 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2554 44392932 2019-08-25 stsp diff_context, refs);
2555 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2556 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2557 7e665116 2018-04-02 stsp break;
2558 31cedeaf 2018-09-15 stsp }
2559 fcc85cad 2018-07-22 stsp done:
2560 6841bf13 2019-11-29 kn if (search_pattern)
2561 6841bf13 2019-11-29 kn regfree(&regex);
2562 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2563 f42b1b34 2018-03-12 stsp return err;
2564 f42b1b34 2018-03-12 stsp }
2565 5c860e29 2018-03-12 stsp
2566 4ed7e80c 2018-05-20 stsp __dead static void
2567 6f3d1eb0 2018-03-12 stsp usage_log(void)
2568 6f3d1eb0 2018-03-12 stsp {
2569 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2570 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2571 6f3d1eb0 2018-03-12 stsp exit(1);
2572 b1ebc001 2019-08-13 stsp }
2573 b1ebc001 2019-08-13 stsp
2574 b1ebc001 2019-08-13 stsp static int
2575 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2576 b1ebc001 2019-08-13 stsp {
2577 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2578 b1ebc001 2019-08-13 stsp long long n;
2579 b1ebc001 2019-08-13 stsp const char *errstr;
2580 b1ebc001 2019-08-13 stsp
2581 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2582 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2583 b1ebc001 2019-08-13 stsp return 0;
2584 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2585 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2586 b1ebc001 2019-08-13 stsp return 0;
2587 b1ebc001 2019-08-13 stsp return n;
2588 6f3d1eb0 2018-03-12 stsp }
2589 6f3d1eb0 2018-03-12 stsp
2590 4ed7e80c 2018-05-20 stsp static const struct got_error *
2591 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2592 f42b1b34 2018-03-12 stsp {
2593 f42b1b34 2018-03-12 stsp const struct got_error *error;
2594 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2595 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2596 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2597 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2598 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2599 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2600 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2601 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2602 64a96a6d 2018-04-01 stsp const char *errstr;
2603 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2604 1b3893a2 2019-03-18 stsp
2605 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2606 5c860e29 2018-03-12 stsp
2607 6715a751 2018-03-16 stsp #ifndef PROFILE
2608 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2609 6098196c 2019-01-04 stsp NULL)
2610 ad242220 2018-09-08 stsp == -1)
2611 f42b1b34 2018-03-12 stsp err(1, "pledge");
2612 6715a751 2018-03-16 stsp #endif
2613 79109fed 2018-03-27 stsp
2614 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2615 b1ebc001 2019-08-13 stsp
2616 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2617 79109fed 2018-03-27 stsp switch (ch) {
2618 79109fed 2018-03-27 stsp case 'p':
2619 79109fed 2018-03-27 stsp show_patch = 1;
2620 d142fc45 2018-04-01 stsp break;
2621 d142fc45 2018-04-01 stsp case 'c':
2622 d142fc45 2018-04-01 stsp start_commit = optarg;
2623 64a96a6d 2018-04-01 stsp break;
2624 c0cc5c62 2018-10-18 stsp case 'C':
2625 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2626 4a8520aa 2018-10-18 stsp &errstr);
2627 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2628 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2629 c0cc5c62 2018-10-18 stsp break;
2630 64a96a6d 2018-04-01 stsp case 'l':
2631 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2632 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2633 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2634 cc54c501 2019-07-15 stsp break;
2635 48c8c60d 2020-01-27 stsp case 'b':
2636 48c8c60d 2020-01-27 stsp log_branches = 1;
2637 a0603db2 2018-06-10 stsp break;
2638 04ca23f4 2018-07-16 stsp case 'r':
2639 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2640 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2641 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2642 9ba1d308 2019-10-21 stsp optarg);
2643 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2644 04ca23f4 2018-07-16 stsp break;
2645 6841bf13 2019-11-29 kn case 's':
2646 6841bf13 2019-11-29 kn search_pattern = optarg;
2647 6841bf13 2019-11-29 kn break;
2648 79109fed 2018-03-27 stsp default:
2649 2deda0b9 2019-03-07 stsp usage_log();
2650 79109fed 2018-03-27 stsp /* NOTREACHED */
2651 79109fed 2018-03-27 stsp }
2652 79109fed 2018-03-27 stsp }
2653 79109fed 2018-03-27 stsp
2654 79109fed 2018-03-27 stsp argc -= optind;
2655 79109fed 2018-03-27 stsp argv += optind;
2656 f42b1b34 2018-03-12 stsp
2657 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2658 dc1edbfa 2019-11-29 kn diff_context = 3;
2659 dc1edbfa 2019-11-29 kn else if (!show_patch)
2660 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2661 dc1edbfa 2019-11-29 kn
2662 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2663 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2664 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2665 04ca23f4 2018-07-16 stsp goto done;
2666 04ca23f4 2018-07-16 stsp }
2667 cffc0aa4 2019-02-05 stsp
2668 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2669 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2670 cffc0aa4 2019-02-05 stsp goto done;
2671 cffc0aa4 2019-02-05 stsp error = NULL;
2672 cffc0aa4 2019-02-05 stsp
2673 e7301579 2019-03-18 stsp if (argc == 0) {
2674 cbd1af7a 2019-03-18 stsp path = strdup("");
2675 e7301579 2019-03-18 stsp if (path == NULL) {
2676 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2677 cbd1af7a 2019-03-18 stsp goto done;
2678 e7301579 2019-03-18 stsp }
2679 e7301579 2019-03-18 stsp } else if (argc == 1) {
2680 e7301579 2019-03-18 stsp if (worktree) {
2681 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2682 e7301579 2019-03-18 stsp argv[0]);
2683 e7301579 2019-03-18 stsp if (error)
2684 e7301579 2019-03-18 stsp goto done;
2685 e7301579 2019-03-18 stsp } else {
2686 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2687 e7301579 2019-03-18 stsp if (path == NULL) {
2688 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2689 e7301579 2019-03-18 stsp goto done;
2690 e7301579 2019-03-18 stsp }
2691 e7301579 2019-03-18 stsp }
2692 cbd1af7a 2019-03-18 stsp } else
2693 cbd1af7a 2019-03-18 stsp usage_log();
2694 cbd1af7a 2019-03-18 stsp
2695 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2696 5486daa2 2019-05-11 stsp repo_path = worktree ?
2697 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2698 5486daa2 2019-05-11 stsp }
2699 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2700 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2701 cffc0aa4 2019-02-05 stsp goto done;
2702 04ca23f4 2018-07-16 stsp }
2703 6098196c 2019-01-04 stsp
2704 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2705 d7d4f210 2018-03-12 stsp if (error != NULL)
2706 04ca23f4 2018-07-16 stsp goto done;
2707 f42b1b34 2018-03-12 stsp
2708 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2709 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2710 c02c541e 2019-03-29 stsp if (error)
2711 c02c541e 2019-03-29 stsp goto done;
2712 c02c541e 2019-03-29 stsp
2713 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2714 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2715 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2716 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2717 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2718 3235492e 2018-04-01 stsp if (error != NULL)
2719 3235492e 2018-04-01 stsp return error;
2720 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2721 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2722 3235492e 2018-04-01 stsp if (error != NULL)
2723 3235492e 2018-04-01 stsp return error;
2724 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2725 3235492e 2018-04-01 stsp } else {
2726 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2727 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2728 3235492e 2018-04-01 stsp if (error == NULL) {
2729 1caad61b 2019-02-01 stsp int obj_type;
2730 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2731 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2732 d1f2edc9 2018-06-13 stsp if (error != NULL)
2733 1caad61b 2019-02-01 stsp goto done;
2734 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2735 1caad61b 2019-02-01 stsp if (error != NULL)
2736 1caad61b 2019-02-01 stsp goto done;
2737 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2738 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2739 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2740 1caad61b 2019-02-01 stsp if (error != NULL)
2741 1caad61b 2019-02-01 stsp goto done;
2742 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2743 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2744 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2745 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2746 1caad61b 2019-02-01 stsp goto done;
2747 1caad61b 2019-02-01 stsp }
2748 1caad61b 2019-02-01 stsp free(id);
2749 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2750 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2751 1caad61b 2019-02-01 stsp if (id == NULL)
2752 638f9024 2019-05-13 stsp error = got_error_from_errno(
2753 230a42bd 2019-05-11 jcs "got_object_id_dup");
2754 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2755 1caad61b 2019-02-01 stsp if (error)
2756 1caad61b 2019-02-01 stsp goto done;
2757 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2758 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2759 1caad61b 2019-02-01 stsp goto done;
2760 1caad61b 2019-02-01 stsp }
2761 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2762 d1f2edc9 2018-06-13 stsp if (error != NULL)
2763 1caad61b 2019-02-01 stsp goto done;
2764 d1f2edc9 2018-06-13 stsp }
2765 15a94983 2018-12-23 stsp if (commit == NULL) {
2766 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2767 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2768 d1f2edc9 2018-06-13 stsp if (error != NULL)
2769 d1f2edc9 2018-06-13 stsp return error;
2770 3235492e 2018-04-01 stsp }
2771 3235492e 2018-04-01 stsp }
2772 d7d4f210 2018-03-12 stsp if (error != NULL)
2773 04ca23f4 2018-07-16 stsp goto done;
2774 04ca23f4 2018-07-16 stsp
2775 deeabeae 2019-08-27 stsp if (worktree) {
2776 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2777 deeabeae 2019-08-27 stsp char *p;
2778 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2779 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2780 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2781 deeabeae 2019-08-27 stsp goto done;
2782 deeabeae 2019-08-27 stsp }
2783 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2784 deeabeae 2019-08-27 stsp free(p);
2785 deeabeae 2019-08-27 stsp } else
2786 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2787 04ca23f4 2018-07-16 stsp if (error != NULL)
2788 04ca23f4 2018-07-16 stsp goto done;
2789 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2790 04ca23f4 2018-07-16 stsp free(path);
2791 04ca23f4 2018-07-16 stsp path = in_repo_path;
2792 04ca23f4 2018-07-16 stsp }
2793 199a4027 2019-02-02 stsp
2794 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2795 199a4027 2019-02-02 stsp if (error)
2796 199a4027 2019-02-02 stsp goto done;
2797 04ca23f4 2018-07-16 stsp
2798 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2799 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2800 04ca23f4 2018-07-16 stsp done:
2801 04ca23f4 2018-07-16 stsp free(path);
2802 04ca23f4 2018-07-16 stsp free(repo_path);
2803 04ca23f4 2018-07-16 stsp free(cwd);
2804 f42b1b34 2018-03-12 stsp free(id);
2805 cffc0aa4 2019-02-05 stsp if (worktree)
2806 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2807 ad242220 2018-09-08 stsp if (repo) {
2808 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2809 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2810 ad242220 2018-09-08 stsp if (error == NULL)
2811 ad242220 2018-09-08 stsp error = repo_error;
2812 ad242220 2018-09-08 stsp }
2813 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2814 8bf5b3c9 2018-03-17 stsp return error;
2815 5c860e29 2018-03-12 stsp }
2816 5c860e29 2018-03-12 stsp
2817 4ed7e80c 2018-05-20 stsp __dead static void
2818 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2819 3f8b7d6a 2018-04-01 stsp {
2820 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2821 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2822 3f8b7d6a 2018-04-01 stsp exit(1);
2823 b00d56cd 2018-04-01 stsp }
2824 b00d56cd 2018-04-01 stsp
2825 b72f483a 2019-02-05 stsp struct print_diff_arg {
2826 b72f483a 2019-02-05 stsp struct got_repository *repo;
2827 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2828 b72f483a 2019-02-05 stsp int diff_context;
2829 3fc0c068 2019-02-10 stsp const char *id_str;
2830 3fc0c068 2019-02-10 stsp int header_shown;
2831 98eaaa12 2019-08-03 stsp int diff_staged;
2832 63035f9f 2019-10-06 stsp int ignore_whitespace;
2833 b72f483a 2019-02-05 stsp };
2834 b72f483a 2019-02-05 stsp
2835 4ed7e80c 2018-05-20 stsp static const struct got_error *
2836 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2837 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2838 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2839 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2840 b72f483a 2019-02-05 stsp {
2841 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2842 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2843 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2844 12463d8b 2019-12-13 stsp int fd = -1;
2845 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2846 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2847 b72f483a 2019-02-05 stsp struct stat sb;
2848 b72f483a 2019-02-05 stsp
2849 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2850 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2851 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2852 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2853 98eaaa12 2019-08-03 stsp return NULL;
2854 98eaaa12 2019-08-03 stsp } else {
2855 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2856 98eaaa12 2019-08-03 stsp return NULL;
2857 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2858 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2859 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2860 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2861 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2862 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2863 98eaaa12 2019-08-03 stsp return NULL;
2864 98eaaa12 2019-08-03 stsp }
2865 3fc0c068 2019-02-10 stsp
2866 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2867 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2868 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2869 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2870 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2871 3fc0c068 2019-02-10 stsp }
2872 b72f483a 2019-02-05 stsp
2873 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2874 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2875 98eaaa12 2019-08-03 stsp switch (staged_status) {
2876 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2877 98eaaa12 2019-08-03 stsp label1 = path;
2878 98eaaa12 2019-08-03 stsp label2 = path;
2879 98eaaa12 2019-08-03 stsp break;
2880 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2881 98eaaa12 2019-08-03 stsp label2 = path;
2882 98eaaa12 2019-08-03 stsp break;
2883 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2884 98eaaa12 2019-08-03 stsp label1 = path;
2885 98eaaa12 2019-08-03 stsp break;
2886 98eaaa12 2019-08-03 stsp default:
2887 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2888 98eaaa12 2019-08-03 stsp }
2889 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2890 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2891 63035f9f 2019-10-06 stsp a->repo, stdout);
2892 98eaaa12 2019-08-03 stsp }
2893 98eaaa12 2019-08-03 stsp
2894 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2895 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2896 4ce46740 2019-08-08 stsp char *id_str;
2897 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2898 408b4ebc 2019-08-03 stsp 8192);
2899 4ce46740 2019-08-08 stsp if (err)
2900 4ce46740 2019-08-08 stsp goto done;
2901 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2902 4ce46740 2019-08-08 stsp if (err)
2903 4ce46740 2019-08-08 stsp goto done;
2904 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2905 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2906 4ce46740 2019-08-08 stsp free(id_str);
2907 4ce46740 2019-08-08 stsp goto done;
2908 4ce46740 2019-08-08 stsp }
2909 4ce46740 2019-08-08 stsp free(id_str);
2910 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2911 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2912 4ce46740 2019-08-08 stsp if (err)
2913 4ce46740 2019-08-08 stsp goto done;
2914 4ce46740 2019-08-08 stsp }
2915 d00136be 2019-03-26 stsp
2916 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2917 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2918 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2919 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2920 2ec1f75b 2019-03-26 stsp goto done;
2921 2ec1f75b 2019-03-26 stsp }
2922 b72f483a 2019-02-05 stsp
2923 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2924 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2925 12463d8b 2019-12-13 stsp if (fd == -1) {
2926 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2927 12463d8b 2019-12-13 stsp goto done;
2928 12463d8b 2019-12-13 stsp }
2929 12463d8b 2019-12-13 stsp } else {
2930 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2931 12463d8b 2019-12-13 stsp if (fd == -1) {
2932 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2933 12463d8b 2019-12-13 stsp goto done;
2934 12463d8b 2019-12-13 stsp }
2935 12463d8b 2019-12-13 stsp }
2936 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2937 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2938 2ec1f75b 2019-03-26 stsp goto done;
2939 2ec1f75b 2019-03-26 stsp }
2940 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2941 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2942 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2943 2ec1f75b 2019-03-26 stsp goto done;
2944 2ec1f75b 2019-03-26 stsp }
2945 12463d8b 2019-12-13 stsp fd = -1;
2946 2ec1f75b 2019-03-26 stsp } else
2947 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2948 b72f483a 2019-02-05 stsp
2949 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2950 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2951 b72f483a 2019-02-05 stsp done:
2952 b72f483a 2019-02-05 stsp if (blob1)
2953 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2954 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2955 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2956 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2957 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2958 b72f483a 2019-02-05 stsp free(abspath);
2959 927df6b7 2019-02-10 stsp return err;
2960 927df6b7 2019-02-10 stsp }
2961 927df6b7 2019-02-10 stsp
2962 927df6b7 2019-02-10 stsp static const struct got_error *
2963 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2964 b00d56cd 2018-04-01 stsp {
2965 b00d56cd 2018-04-01 stsp const struct got_error *error;
2966 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2967 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2968 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2969 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2970 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2971 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2972 15a94983 2018-12-23 stsp int type1, type2;
2973 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2974 c0cc5c62 2018-10-18 stsp const char *errstr;
2975 927df6b7 2019-02-10 stsp char *path = NULL;
2976 b00d56cd 2018-04-01 stsp
2977 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2978 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2979 25eccc22 2019-01-04 stsp NULL) == -1)
2980 b00d56cd 2018-04-01 stsp err(1, "pledge");
2981 b00d56cd 2018-04-01 stsp #endif
2982 b00d56cd 2018-04-01 stsp
2983 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2984 b00d56cd 2018-04-01 stsp switch (ch) {
2985 c0cc5c62 2018-10-18 stsp case 'C':
2986 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2987 dfcab68b 2019-11-29 kn &errstr);
2988 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2989 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2990 c0cc5c62 2018-10-18 stsp break;
2991 b72f483a 2019-02-05 stsp case 'r':
2992 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2993 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2994 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2995 9ba1d308 2019-10-21 stsp optarg);
2996 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2997 b72f483a 2019-02-05 stsp break;
2998 98eaaa12 2019-08-03 stsp case 's':
2999 98eaaa12 2019-08-03 stsp diff_staged = 1;
3000 63035f9f 2019-10-06 stsp break;
3001 63035f9f 2019-10-06 stsp case 'w':
3002 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3003 98eaaa12 2019-08-03 stsp break;
3004 b00d56cd 2018-04-01 stsp default:
3005 2deda0b9 2019-03-07 stsp usage_diff();
3006 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3007 b00d56cd 2018-04-01 stsp }
3008 b00d56cd 2018-04-01 stsp }
3009 b00d56cd 2018-04-01 stsp
3010 b00d56cd 2018-04-01 stsp argc -= optind;
3011 b00d56cd 2018-04-01 stsp argv += optind;
3012 b00d56cd 2018-04-01 stsp
3013 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3014 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3015 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3016 b72f483a 2019-02-05 stsp goto done;
3017 b72f483a 2019-02-05 stsp }
3018 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3019 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3020 927df6b7 2019-02-10 stsp goto done;
3021 927df6b7 2019-02-10 stsp if (argc <= 1) {
3022 927df6b7 2019-02-10 stsp if (worktree == NULL) {
3023 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
3024 927df6b7 2019-02-10 stsp goto done;
3025 927df6b7 2019-02-10 stsp }
3026 b72f483a 2019-02-05 stsp if (repo_path)
3027 b72f483a 2019-02-05 stsp errx(1,
3028 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3029 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3030 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3031 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3032 927df6b7 2019-02-10 stsp goto done;
3033 927df6b7 2019-02-10 stsp }
3034 927df6b7 2019-02-10 stsp if (argc == 1) {
3035 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3036 cbd1af7a 2019-03-18 stsp argv[0]);
3037 927df6b7 2019-02-10 stsp if (error)
3038 927df6b7 2019-02-10 stsp goto done;
3039 927df6b7 2019-02-10 stsp } else {
3040 927df6b7 2019-02-10 stsp path = strdup("");
3041 927df6b7 2019-02-10 stsp if (path == NULL) {
3042 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3043 927df6b7 2019-02-10 stsp goto done;
3044 927df6b7 2019-02-10 stsp }
3045 927df6b7 2019-02-10 stsp }
3046 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3047 98eaaa12 2019-08-03 stsp if (diff_staged)
3048 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3049 98eaaa12 2019-08-03 stsp "objects in repository");
3050 15a94983 2018-12-23 stsp id_str1 = argv[0];
3051 15a94983 2018-12-23 stsp id_str2 = argv[1];
3052 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
3053 30db809c 2019-06-05 stsp repo_path =
3054 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
3055 30db809c 2019-06-05 stsp if (repo_path == NULL) {
3056 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
3057 30db809c 2019-06-05 stsp goto done;
3058 30db809c 2019-06-05 stsp }
3059 30db809c 2019-06-05 stsp }
3060 b00d56cd 2018-04-01 stsp } else
3061 b00d56cd 2018-04-01 stsp usage_diff();
3062 25eccc22 2019-01-04 stsp
3063 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
3064 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
3065 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3066 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3067 b72f483a 2019-02-05 stsp }
3068 b00d56cd 2018-04-01 stsp
3069 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3070 76089277 2018-04-01 stsp free(repo_path);
3071 b00d56cd 2018-04-01 stsp if (error != NULL)
3072 b00d56cd 2018-04-01 stsp goto done;
3073 b00d56cd 2018-04-01 stsp
3074 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3075 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3076 c02c541e 2019-03-29 stsp if (error)
3077 c02c541e 2019-03-29 stsp goto done;
3078 c02c541e 2019-03-29 stsp
3079 30db809c 2019-06-05 stsp if (argc <= 1) {
3080 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3081 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3082 b72f483a 2019-02-05 stsp char *id_str;
3083 72ea6654 2019-07-27 stsp
3084 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3085 72ea6654 2019-07-27 stsp
3086 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3087 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3088 b72f483a 2019-02-05 stsp if (error)
3089 b72f483a 2019-02-05 stsp goto done;
3090 b72f483a 2019-02-05 stsp arg.repo = repo;
3091 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3092 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3093 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3094 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3095 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3096 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3097 b72f483a 2019-02-05 stsp
3098 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3099 72ea6654 2019-07-27 stsp if (error)
3100 72ea6654 2019-07-27 stsp goto done;
3101 72ea6654 2019-07-27 stsp
3102 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3103 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3104 b72f483a 2019-02-05 stsp free(id_str);
3105 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3106 b72f483a 2019-02-05 stsp goto done;
3107 b72f483a 2019-02-05 stsp }
3108 b72f483a 2019-02-05 stsp
3109 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3110 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3111 0adb7196 2019-08-11 stsp if (error)
3112 0adb7196 2019-08-11 stsp goto done;
3113 b00d56cd 2018-04-01 stsp
3114 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3115 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3116 0adb7196 2019-08-11 stsp if (error)
3117 0adb7196 2019-08-11 stsp goto done;
3118 b00d56cd 2018-04-01 stsp
3119 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3120 15a94983 2018-12-23 stsp if (error)
3121 15a94983 2018-12-23 stsp goto done;
3122 15a94983 2018-12-23 stsp
3123 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3124 15a94983 2018-12-23 stsp if (error)
3125 15a94983 2018-12-23 stsp goto done;
3126 15a94983 2018-12-23 stsp
3127 15a94983 2018-12-23 stsp if (type1 != type2) {
3128 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3129 b00d56cd 2018-04-01 stsp goto done;
3130 b00d56cd 2018-04-01 stsp }
3131 b00d56cd 2018-04-01 stsp
3132 15a94983 2018-12-23 stsp switch (type1) {
3133 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3134 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3135 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3136 b00d56cd 2018-04-01 stsp break;
3137 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3138 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3139 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3140 b00d56cd 2018-04-01 stsp break;
3141 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3142 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3143 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3144 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3145 b00d56cd 2018-04-01 stsp break;
3146 b00d56cd 2018-04-01 stsp default:
3147 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3148 b00d56cd 2018-04-01 stsp }
3149 b00d56cd 2018-04-01 stsp done:
3150 5e70831e 2019-05-28 stsp free(label1);
3151 5e70831e 2019-05-28 stsp free(label2);
3152 15a94983 2018-12-23 stsp free(id1);
3153 15a94983 2018-12-23 stsp free(id2);
3154 927df6b7 2019-02-10 stsp free(path);
3155 b72f483a 2019-02-05 stsp if (worktree)
3156 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3157 ad242220 2018-09-08 stsp if (repo) {
3158 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3159 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3160 ad242220 2018-09-08 stsp if (error == NULL)
3161 ad242220 2018-09-08 stsp error = repo_error;
3162 ad242220 2018-09-08 stsp }
3163 b00d56cd 2018-04-01 stsp return error;
3164 404c43c4 2018-06-21 stsp }
3165 404c43c4 2018-06-21 stsp
3166 404c43c4 2018-06-21 stsp __dead static void
3167 404c43c4 2018-06-21 stsp usage_blame(void)
3168 404c43c4 2018-06-21 stsp {
3169 9270e621 2019-02-05 stsp fprintf(stderr,
3170 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3171 404c43c4 2018-06-21 stsp getprogname());
3172 404c43c4 2018-06-21 stsp exit(1);
3173 b00d56cd 2018-04-01 stsp }
3174 b00d56cd 2018-04-01 stsp
3175 28315671 2019-08-14 stsp struct blame_line {
3176 28315671 2019-08-14 stsp int annotated;
3177 28315671 2019-08-14 stsp char *id_str;
3178 82f6abb8 2019-08-14 stsp char *committer;
3179 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3180 28315671 2019-08-14 stsp };
3181 28315671 2019-08-14 stsp
3182 28315671 2019-08-14 stsp struct blame_cb_args {
3183 28315671 2019-08-14 stsp struct blame_line *lines;
3184 28315671 2019-08-14 stsp int nlines;
3185 7ef28ff8 2019-08-14 stsp int nlines_prec;
3186 28315671 2019-08-14 stsp int lineno_cur;
3187 28315671 2019-08-14 stsp off_t *line_offsets;
3188 28315671 2019-08-14 stsp FILE *f;
3189 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3190 28315671 2019-08-14 stsp };
3191 28315671 2019-08-14 stsp
3192 404c43c4 2018-06-21 stsp static const struct got_error *
3193 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3194 28315671 2019-08-14 stsp {
3195 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3196 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3197 28315671 2019-08-14 stsp struct blame_line *bline;
3198 82f6abb8 2019-08-14 stsp char *line = NULL;
3199 28315671 2019-08-14 stsp size_t linesize = 0;
3200 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3201 28315671 2019-08-14 stsp off_t offset;
3202 bcb49d15 2019-08-14 stsp struct tm tm;
3203 bcb49d15 2019-08-14 stsp time_t committer_time;
3204 28315671 2019-08-14 stsp
3205 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3206 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3207 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3208 28315671 2019-08-14 stsp
3209 28315671 2019-08-14 stsp if (sigint_received)
3210 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3211 28315671 2019-08-14 stsp
3212 2839f8b9 2019-08-18 stsp if (lineno == -1)
3213 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3214 2839f8b9 2019-08-18 stsp
3215 28315671 2019-08-14 stsp /* Annotate this line. */
3216 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3217 28315671 2019-08-14 stsp if (bline->annotated)
3218 28315671 2019-08-14 stsp return NULL;
3219 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3220 28315671 2019-08-14 stsp if (err)
3221 28315671 2019-08-14 stsp return err;
3222 82f6abb8 2019-08-14 stsp
3223 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3224 82f6abb8 2019-08-14 stsp if (err)
3225 82f6abb8 2019-08-14 stsp goto done;
3226 82f6abb8 2019-08-14 stsp
3227 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3228 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3229 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3230 bcb49d15 2019-08-14 stsp goto done;
3231 bcb49d15 2019-08-14 stsp }
3232 bcb49d15 2019-08-14 stsp
3233 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3234 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3235 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3236 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3237 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3238 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3239 82f6abb8 2019-08-14 stsp goto done;
3240 82f6abb8 2019-08-14 stsp }
3241 28315671 2019-08-14 stsp bline->annotated = 1;
3242 28315671 2019-08-14 stsp
3243 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3244 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3245 28315671 2019-08-14 stsp if (!bline->annotated)
3246 82f6abb8 2019-08-14 stsp goto done;
3247 28315671 2019-08-14 stsp
3248 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3249 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3250 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3251 82f6abb8 2019-08-14 stsp goto done;
3252 82f6abb8 2019-08-14 stsp }
3253 28315671 2019-08-14 stsp
3254 28315671 2019-08-14 stsp while (bline->annotated) {
3255 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3256 82f6abb8 2019-08-14 stsp size_t len;
3257 82f6abb8 2019-08-14 stsp
3258 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3259 28315671 2019-08-14 stsp if (ferror(a->f))
3260 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3261 28315671 2019-08-14 stsp break;
3262 28315671 2019-08-14 stsp }
3263 82f6abb8 2019-08-14 stsp
3264 82f6abb8 2019-08-14 stsp committer = bline->committer;
3265 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3266 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3267 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3268 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3269 82f6abb8 2019-08-14 stsp if (at)
3270 82f6abb8 2019-08-14 stsp *at = '\0';
3271 82f6abb8 2019-08-14 stsp len = strlen(committer);
3272 82f6abb8 2019-08-14 stsp if (len >= 9)
3273 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3274 28315671 2019-08-14 stsp
3275 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3276 28315671 2019-08-14 stsp if (nl)
3277 28315671 2019-08-14 stsp *nl = '\0';
3278 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3279 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3280 28315671 2019-08-14 stsp
3281 28315671 2019-08-14 stsp a->lineno_cur++;
3282 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3283 28315671 2019-08-14 stsp }
3284 82f6abb8 2019-08-14 stsp done:
3285 82f6abb8 2019-08-14 stsp if (commit)
3286 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3287 28315671 2019-08-14 stsp free(line);
3288 28315671 2019-08-14 stsp return err;
3289 28315671 2019-08-14 stsp }
3290 28315671 2019-08-14 stsp
3291 28315671 2019-08-14 stsp static const struct got_error *
3292 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3293 404c43c4 2018-06-21 stsp {
3294 404c43c4 2018-06-21 stsp const struct got_error *error;
3295 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3296 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3297 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3298 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3299 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3300 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3301 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3302 28315671 2019-08-14 stsp struct blame_cb_args bca;
3303 28315671 2019-08-14 stsp int ch, obj_type, i;
3304 b02560ec 2019-08-19 stsp size_t filesize;
3305 90f3c347 2019-08-19 stsp
3306 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3307 404c43c4 2018-06-21 stsp
3308 404c43c4 2018-06-21 stsp #ifndef PROFILE
3309 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3310 36e2fb66 2019-01-04 stsp NULL) == -1)
3311 404c43c4 2018-06-21 stsp err(1, "pledge");
3312 404c43c4 2018-06-21 stsp #endif
3313 404c43c4 2018-06-21 stsp
3314 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3315 404c43c4 2018-06-21 stsp switch (ch) {
3316 404c43c4 2018-06-21 stsp case 'c':
3317 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3318 404c43c4 2018-06-21 stsp break;
3319 66bea077 2018-08-02 stsp case 'r':
3320 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3321 66bea077 2018-08-02 stsp if (repo_path == NULL)
3322 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3323 9ba1d308 2019-10-21 stsp optarg);
3324 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3325 66bea077 2018-08-02 stsp break;
3326 404c43c4 2018-06-21 stsp default:
3327 2deda0b9 2019-03-07 stsp usage_blame();
3328 404c43c4 2018-06-21 stsp /* NOTREACHED */
3329 404c43c4 2018-06-21 stsp }
3330 404c43c4 2018-06-21 stsp }
3331 404c43c4 2018-06-21 stsp
3332 404c43c4 2018-06-21 stsp argc -= optind;
3333 404c43c4 2018-06-21 stsp argv += optind;
3334 404c43c4 2018-06-21 stsp
3335 a39318fd 2018-08-02 stsp if (argc == 1)
3336 404c43c4 2018-06-21 stsp path = argv[0];
3337 a39318fd 2018-08-02 stsp else
3338 404c43c4 2018-06-21 stsp usage_blame();
3339 404c43c4 2018-06-21 stsp
3340 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3341 66bea077 2018-08-02 stsp if (cwd == NULL) {
3342 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3343 66bea077 2018-08-02 stsp goto done;
3344 66bea077 2018-08-02 stsp }
3345 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3346 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3347 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3348 66bea077 2018-08-02 stsp goto done;
3349 0c06baac 2019-02-05 stsp else
3350 0c06baac 2019-02-05 stsp error = NULL;
3351 0c06baac 2019-02-05 stsp if (worktree) {
3352 0c06baac 2019-02-05 stsp repo_path =
3353 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3354 0c06baac 2019-02-05 stsp if (repo_path == NULL)
3355 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3356 0c06baac 2019-02-05 stsp if (error)
3357 0c06baac 2019-02-05 stsp goto done;
3358 0c06baac 2019-02-05 stsp } else {
3359 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3360 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3361 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3362 0c06baac 2019-02-05 stsp goto done;
3363 0c06baac 2019-02-05 stsp }
3364 66bea077 2018-08-02 stsp }
3365 66bea077 2018-08-02 stsp }
3366 36e2fb66 2019-01-04 stsp
3367 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3368 c02c541e 2019-03-29 stsp if (error != NULL)
3369 36e2fb66 2019-01-04 stsp goto done;
3370 66bea077 2018-08-02 stsp
3371 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3372 c02c541e 2019-03-29 stsp if (error)
3373 404c43c4 2018-06-21 stsp goto done;
3374 404c43c4 2018-06-21 stsp
3375 0c06baac 2019-02-05 stsp if (worktree) {
3376 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3377 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3378 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3379 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3380 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3381 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3382 6efaaa2d 2019-02-05 stsp path) == -1) {
3383 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3384 0c06baac 2019-02-05 stsp goto done;
3385 0c06baac 2019-02-05 stsp }
3386 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3387 0c06baac 2019-02-05 stsp free(p);
3388 0c06baac 2019-02-05 stsp } else {
3389 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3390 0c06baac 2019-02-05 stsp }
3391 0c06baac 2019-02-05 stsp if (error)
3392 66bea077 2018-08-02 stsp goto done;
3393 66bea077 2018-08-02 stsp
3394 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3395 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3396 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3397 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3398 404c43c4 2018-06-21 stsp if (error != NULL)
3399 66bea077 2018-08-02 stsp goto done;
3400 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3401 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3402 404c43c4 2018-06-21 stsp if (error != NULL)
3403 66bea077 2018-08-02 stsp goto done;
3404 404c43c4 2018-06-21 stsp } else {
3405 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3406 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3407 30837e32 2019-07-25 stsp if (error)
3408 66bea077 2018-08-02 stsp goto done;
3409 404c43c4 2018-06-21 stsp }
3410 404c43c4 2018-06-21 stsp
3411 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3412 28315671 2019-08-14 stsp if (error)
3413 28315671 2019-08-14 stsp goto done;
3414 28315671 2019-08-14 stsp
3415 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3416 28315671 2019-08-14 stsp if (error)
3417 28315671 2019-08-14 stsp goto done;
3418 28315671 2019-08-14 stsp
3419 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3420 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3421 28315671 2019-08-14 stsp goto done;
3422 28315671 2019-08-14 stsp }
3423 28315671 2019-08-14 stsp
3424 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3425 28315671 2019-08-14 stsp if (error)
3426 28315671 2019-08-14 stsp goto done;
3427 28315671 2019-08-14 stsp bca.f = got_opentemp();
3428 28315671 2019-08-14 stsp if (bca.f == NULL) {
3429 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3430 28315671 2019-08-14 stsp goto done;
3431 28315671 2019-08-14 stsp }
3432 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3433 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3434 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3435 28315671 2019-08-14 stsp goto done;
3436 28315671 2019-08-14 stsp
3437 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3438 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3439 b02560ec 2019-08-19 stsp bca.nlines--;
3440 b02560ec 2019-08-19 stsp
3441 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3442 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3443 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3444 28315671 2019-08-14 stsp goto done;
3445 28315671 2019-08-14 stsp }
3446 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3447 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3448 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3449 7ef28ff8 2019-08-14 stsp while (i > 0) {
3450 7ef28ff8 2019-08-14 stsp i /= 10;
3451 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3452 7ef28ff8 2019-08-14 stsp }
3453 82f6abb8 2019-08-14 stsp bca.repo = repo;
3454 7ef28ff8 2019-08-14 stsp
3455 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3456 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3457 404c43c4 2018-06-21 stsp done:
3458 66bea077 2018-08-02 stsp free(in_repo_path);
3459 66bea077 2018-08-02 stsp free(repo_path);
3460 66bea077 2018-08-02 stsp free(cwd);
3461 404c43c4 2018-06-21 stsp free(commit_id);
3462 28315671 2019-08-14 stsp free(obj_id);
3463 28315671 2019-08-14 stsp if (blob)
3464 28315671 2019-08-14 stsp got_object_blob_close(blob);
3465 0c06baac 2019-02-05 stsp if (worktree)
3466 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3467 ad242220 2018-09-08 stsp if (repo) {
3468 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3469 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3470 ad242220 2018-09-08 stsp if (error == NULL)
3471 ad242220 2018-09-08 stsp error = repo_error;
3472 ad242220 2018-09-08 stsp }
3473 3affba96 2019-09-22 stsp if (bca.lines) {
3474 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3475 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3476 3affba96 2019-09-22 stsp free(bline->id_str);
3477 3affba96 2019-09-22 stsp free(bline->committer);
3478 3affba96 2019-09-22 stsp }
3479 3affba96 2019-09-22 stsp free(bca.lines);
3480 28315671 2019-08-14 stsp }
3481 28315671 2019-08-14 stsp free(bca.line_offsets);
3482 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3483 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3484 404c43c4 2018-06-21 stsp return error;
3485 5de5890b 2018-10-18 stsp }
3486 5de5890b 2018-10-18 stsp
3487 5de5890b 2018-10-18 stsp __dead static void
3488 5de5890b 2018-10-18 stsp usage_tree(void)
3489 5de5890b 2018-10-18 stsp {
3490 c1669e2e 2019-01-09 stsp fprintf(stderr,
3491 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3492 5de5890b 2018-10-18 stsp getprogname());
3493 5de5890b 2018-10-18 stsp exit(1);
3494 5de5890b 2018-10-18 stsp }
3495 5de5890b 2018-10-18 stsp
3496 c1669e2e 2019-01-09 stsp static void
3497 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3498 c1669e2e 2019-01-09 stsp const char *root_path)
3499 c1669e2e 2019-01-09 stsp {
3500 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3501 848d6979 2019-08-12 stsp const char *modestr = "";
3502 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3503 5de5890b 2018-10-18 stsp
3504 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3505 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3506 c1669e2e 2019-01-09 stsp path++;
3507 c1669e2e 2019-01-09 stsp
3508 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3509 63c5ca5d 2019-08-24 stsp modestr = "$";
3510 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3511 848d6979 2019-08-12 stsp modestr = "@";
3512 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3513 848d6979 2019-08-12 stsp modestr = "/";
3514 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3515 848d6979 2019-08-12 stsp modestr = "*";
3516 848d6979 2019-08-12 stsp
3517 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3518 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3519 c1669e2e 2019-01-09 stsp }
3520 c1669e2e 2019-01-09 stsp
3521 5de5890b 2018-10-18 stsp static const struct got_error *
3522 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3523 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3524 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3525 5de5890b 2018-10-18 stsp {
3526 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3527 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3528 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3529 56e0773d 2019-11-28 stsp int nentries, i;
3530 5de5890b 2018-10-18 stsp
3531 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3532 5de5890b 2018-10-18 stsp if (err)
3533 5de5890b 2018-10-18 stsp goto done;
3534 5de5890b 2018-10-18 stsp
3535 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3536 5de5890b 2018-10-18 stsp if (err)
3537 5de5890b 2018-10-18 stsp goto done;
3538 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3539 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3540 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3541 5de5890b 2018-10-18 stsp char *id = NULL;
3542 84453469 2018-11-11 stsp
3543 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3544 84453469 2018-11-11 stsp break;
3545 84453469 2018-11-11 stsp
3546 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3547 5de5890b 2018-10-18 stsp if (show_ids) {
3548 5de5890b 2018-10-18 stsp char *id_str;
3549 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3550 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3551 5de5890b 2018-10-18 stsp if (err)
3552 5de5890b 2018-10-18 stsp goto done;
3553 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3554 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3555 5de5890b 2018-10-18 stsp free(id_str);
3556 5de5890b 2018-10-18 stsp goto done;
3557 5de5890b 2018-10-18 stsp }
3558 5de5890b 2018-10-18 stsp free(id_str);
3559 5de5890b 2018-10-18 stsp }
3560 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3561 5de5890b 2018-10-18 stsp free(id);
3562 c1669e2e 2019-01-09 stsp
3563 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3564 c1669e2e 2019-01-09 stsp char *child_path;
3565 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3566 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3567 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3568 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3569 c1669e2e 2019-01-09 stsp goto done;
3570 c1669e2e 2019-01-09 stsp }
3571 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3572 c1669e2e 2019-01-09 stsp root_path, repo);
3573 c1669e2e 2019-01-09 stsp free(child_path);
3574 c1669e2e 2019-01-09 stsp if (err)
3575 c1669e2e 2019-01-09 stsp goto done;
3576 c1669e2e 2019-01-09 stsp }
3577 5de5890b 2018-10-18 stsp }
3578 5de5890b 2018-10-18 stsp done:
3579 5de5890b 2018-10-18 stsp if (tree)
3580 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3581 5de5890b 2018-10-18 stsp free(tree_id);
3582 5de5890b 2018-10-18 stsp return err;
3583 404c43c4 2018-06-21 stsp }
3584 404c43c4 2018-06-21 stsp
3585 5de5890b 2018-10-18 stsp static const struct got_error *
3586 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3587 5de5890b 2018-10-18 stsp {
3588 5de5890b 2018-10-18 stsp const struct got_error *error;
3589 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3590 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3591 7a2c19d6 2019-02-05 stsp const char *path;
3592 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3593 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3594 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3595 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3596 5de5890b 2018-10-18 stsp int ch;
3597 5de5890b 2018-10-18 stsp
3598 5de5890b 2018-10-18 stsp #ifndef PROFILE
3599 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3600 0f8d269b 2019-01-04 stsp NULL) == -1)
3601 5de5890b 2018-10-18 stsp err(1, "pledge");
3602 5de5890b 2018-10-18 stsp #endif
3603 5de5890b 2018-10-18 stsp
3604 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3605 5de5890b 2018-10-18 stsp switch (ch) {
3606 5de5890b 2018-10-18 stsp case 'c':
3607 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3608 5de5890b 2018-10-18 stsp break;
3609 5de5890b 2018-10-18 stsp case 'r':
3610 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3611 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3612 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3613 9ba1d308 2019-10-21 stsp optarg);
3614 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3615 5de5890b 2018-10-18 stsp break;
3616 5de5890b 2018-10-18 stsp case 'i':
3617 5de5890b 2018-10-18 stsp show_ids = 1;
3618 5de5890b 2018-10-18 stsp break;
3619 c1669e2e 2019-01-09 stsp case 'R':
3620 c1669e2e 2019-01-09 stsp recurse = 1;
3621 c1669e2e 2019-01-09 stsp break;
3622 5de5890b 2018-10-18 stsp default:
3623 2deda0b9 2019-03-07 stsp usage_tree();
3624 5de5890b 2018-10-18 stsp /* NOTREACHED */
3625 5de5890b 2018-10-18 stsp }
3626 5de5890b 2018-10-18 stsp }
3627 5de5890b 2018-10-18 stsp
3628 5de5890b 2018-10-18 stsp argc -= optind;
3629 5de5890b 2018-10-18 stsp argv += optind;
3630 5de5890b 2018-10-18 stsp
3631 5de5890b 2018-10-18 stsp if (argc == 1)
3632 5de5890b 2018-10-18 stsp path = argv[0];
3633 5de5890b 2018-10-18 stsp else if (argc > 1)
3634 5de5890b 2018-10-18 stsp usage_tree();
3635 5de5890b 2018-10-18 stsp else
3636 7a2c19d6 2019-02-05 stsp path = NULL;
3637 7a2c19d6 2019-02-05 stsp
3638 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3639 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3640 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3641 9bf7a39b 2019-02-05 stsp goto done;
3642 9bf7a39b 2019-02-05 stsp }
3643 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3644 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3645 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3646 7a2c19d6 2019-02-05 stsp goto done;
3647 7a2c19d6 2019-02-05 stsp else
3648 7a2c19d6 2019-02-05 stsp error = NULL;
3649 7a2c19d6 2019-02-05 stsp if (worktree) {
3650 7a2c19d6 2019-02-05 stsp repo_path =
3651 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3652 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3653 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3654 7a2c19d6 2019-02-05 stsp if (error)
3655 7a2c19d6 2019-02-05 stsp goto done;
3656 7a2c19d6 2019-02-05 stsp } else {
3657 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3658 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3659 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3660 7a2c19d6 2019-02-05 stsp goto done;
3661 7a2c19d6 2019-02-05 stsp }
3662 5de5890b 2018-10-18 stsp }
3663 5de5890b 2018-10-18 stsp }
3664 5de5890b 2018-10-18 stsp
3665 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3666 5de5890b 2018-10-18 stsp if (error != NULL)
3667 c02c541e 2019-03-29 stsp goto done;
3668 c02c541e 2019-03-29 stsp
3669 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3670 c02c541e 2019-03-29 stsp if (error)
3671 5de5890b 2018-10-18 stsp goto done;
3672 5de5890b 2018-10-18 stsp
3673 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3674 9bf7a39b 2019-02-05 stsp if (worktree) {
3675 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3676 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3677 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3678 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3679 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3680 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3681 9bf7a39b 2019-02-05 stsp goto done;
3682 9bf7a39b 2019-02-05 stsp }
3683 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3684 9bf7a39b 2019-02-05 stsp free(p);
3685 9bf7a39b 2019-02-05 stsp if (error)
3686 9bf7a39b 2019-02-05 stsp goto done;
3687 9bf7a39b 2019-02-05 stsp } else
3688 9bf7a39b 2019-02-05 stsp path = "/";
3689 9bf7a39b 2019-02-05 stsp }
3690 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3691 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3692 9bf7a39b 2019-02-05 stsp if (error != NULL)
3693 9bf7a39b 2019-02-05 stsp goto done;
3694 9bf7a39b 2019-02-05 stsp }
3695 5de5890b 2018-10-18 stsp
3696 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3697 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3698 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3699 5de5890b 2018-10-18 stsp if (error != NULL)
3700 5de5890b 2018-10-18 stsp goto done;
3701 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3702 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3703 5de5890b 2018-10-18 stsp if (error != NULL)
3704 5de5890b 2018-10-18 stsp goto done;
3705 5de5890b 2018-10-18 stsp } else {
3706 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3707 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3708 30837e32 2019-07-25 stsp if (error)
3709 5de5890b 2018-10-18 stsp goto done;
3710 5de5890b 2018-10-18 stsp }
3711 5de5890b 2018-10-18 stsp
3712 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3713 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3714 5de5890b 2018-10-18 stsp done:
3715 5de5890b 2018-10-18 stsp free(in_repo_path);
3716 5de5890b 2018-10-18 stsp free(repo_path);
3717 5de5890b 2018-10-18 stsp free(cwd);
3718 5de5890b 2018-10-18 stsp free(commit_id);
3719 7a2c19d6 2019-02-05 stsp if (worktree)
3720 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3721 5de5890b 2018-10-18 stsp if (repo) {
3722 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3723 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3724 5de5890b 2018-10-18 stsp if (error == NULL)
3725 5de5890b 2018-10-18 stsp error = repo_error;
3726 5de5890b 2018-10-18 stsp }
3727 5de5890b 2018-10-18 stsp return error;
3728 5de5890b 2018-10-18 stsp }
3729 5de5890b 2018-10-18 stsp
3730 6bad629b 2019-02-04 stsp __dead static void
3731 6bad629b 2019-02-04 stsp usage_status(void)
3732 6bad629b 2019-02-04 stsp {
3733 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3734 6bad629b 2019-02-04 stsp exit(1);
3735 6bad629b 2019-02-04 stsp }
3736 5c860e29 2018-03-12 stsp
3737 b72f483a 2019-02-05 stsp static const struct got_error *
3738 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3739 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3740 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3741 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3742 6bad629b 2019-02-04 stsp {
3743 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3744 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3745 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3746 b72f483a 2019-02-05 stsp return NULL;
3747 6bad629b 2019-02-04 stsp }
3748 5c860e29 2018-03-12 stsp
3749 6bad629b 2019-02-04 stsp static const struct got_error *
3750 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3751 6bad629b 2019-02-04 stsp {
3752 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3753 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3754 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3755 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3756 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3757 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3758 a5edda0a 2019-07-27 stsp int ch;
3759 5c860e29 2018-03-12 stsp
3760 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3761 72ea6654 2019-07-27 stsp
3762 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3763 6bad629b 2019-02-04 stsp switch (ch) {
3764 5c860e29 2018-03-12 stsp default:
3765 2deda0b9 2019-03-07 stsp usage_status();
3766 6bad629b 2019-02-04 stsp /* NOTREACHED */
3767 5c860e29 2018-03-12 stsp }
3768 5c860e29 2018-03-12 stsp }
3769 5c860e29 2018-03-12 stsp
3770 6bad629b 2019-02-04 stsp argc -= optind;
3771 6bad629b 2019-02-04 stsp argv += optind;
3772 5c860e29 2018-03-12 stsp
3773 6bad629b 2019-02-04 stsp #ifndef PROFILE
3774 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3775 6bad629b 2019-02-04 stsp NULL) == -1)
3776 6bad629b 2019-02-04 stsp err(1, "pledge");
3777 f42b1b34 2018-03-12 stsp #endif
3778 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3779 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3780 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3781 927df6b7 2019-02-10 stsp goto done;
3782 927df6b7 2019-02-10 stsp }
3783 927df6b7 2019-02-10 stsp
3784 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3785 927df6b7 2019-02-10 stsp if (error != NULL)
3786 a5edda0a 2019-07-27 stsp goto done;
3787 6bad629b 2019-02-04 stsp
3788 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3789 c9956ddf 2019-09-08 stsp NULL);
3790 6bad629b 2019-02-04 stsp if (error != NULL)
3791 6bad629b 2019-02-04 stsp goto done;
3792 6bad629b 2019-02-04 stsp
3793 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3794 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3795 087fb88c 2019-08-04 stsp if (error)
3796 087fb88c 2019-08-04 stsp goto done;
3797 087fb88c 2019-08-04 stsp
3798 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3799 6bad629b 2019-02-04 stsp if (error)
3800 6bad629b 2019-02-04 stsp goto done;
3801 6bad629b 2019-02-04 stsp
3802 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3803 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3804 6bad629b 2019-02-04 stsp done:
3805 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3806 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3807 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3808 927df6b7 2019-02-10 stsp free(cwd);
3809 d0eebce4 2019-03-11 stsp return error;
3810 d0eebce4 2019-03-11 stsp }
3811 d0eebce4 2019-03-11 stsp
3812 d0eebce4 2019-03-11 stsp __dead static void
3813 d0eebce4 2019-03-11 stsp usage_ref(void)
3814 d0eebce4 2019-03-11 stsp {
3815 d0eebce4 2019-03-11 stsp fprintf(stderr,
3816 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3817 d0eebce4 2019-03-11 stsp getprogname());
3818 d0eebce4 2019-03-11 stsp exit(1);
3819 d0eebce4 2019-03-11 stsp }
3820 d0eebce4 2019-03-11 stsp
3821 d0eebce4 2019-03-11 stsp static const struct got_error *
3822 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3823 d0eebce4 2019-03-11 stsp {
3824 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3825 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3826 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3827 d0eebce4 2019-03-11 stsp
3828 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3829 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3830 d0eebce4 2019-03-11 stsp if (err)
3831 d0eebce4 2019-03-11 stsp return err;
3832 d0eebce4 2019-03-11 stsp
3833 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3834 d0eebce4 2019-03-11 stsp char *refstr;
3835 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3836 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3837 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3838 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3839 d0eebce4 2019-03-11 stsp free(refstr);
3840 d0eebce4 2019-03-11 stsp }
3841 d0eebce4 2019-03-11 stsp
3842 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3843 d0eebce4 2019-03-11 stsp return NULL;
3844 d0eebce4 2019-03-11 stsp }
3845 d0eebce4 2019-03-11 stsp
3846 d0eebce4 2019-03-11 stsp static const struct got_error *
3847 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3848 d0eebce4 2019-03-11 stsp {
3849 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3850 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3851 d0eebce4 2019-03-11 stsp
3852 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3853 d0eebce4 2019-03-11 stsp if (err)
3854 d0eebce4 2019-03-11 stsp return err;
3855 d0eebce4 2019-03-11 stsp
3856 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3857 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3858 d0eebce4 2019-03-11 stsp return err;
3859 d0eebce4 2019-03-11 stsp }
3860 d0eebce4 2019-03-11 stsp
3861 d0eebce4 2019-03-11 stsp static const struct got_error *
3862 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3863 d0eebce4 2019-03-11 stsp {
3864 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3865 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3866 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3867 d1644381 2019-07-14 stsp
3868 d1644381 2019-07-14 stsp /*
3869 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3870 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3871 d1644381 2019-07-14 stsp * an unintended typo.
3872 d1644381 2019-07-14 stsp */
3873 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3874 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3875 d0eebce4 2019-03-11 stsp
3876 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3877 dd88155e 2019-06-29 stsp repo);
3878 d83d9d5c 2019-05-13 stsp if (err) {
3879 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3880 d0eebce4 2019-03-11 stsp
3881 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3882 d83d9d5c 2019-05-13 stsp return err;
3883 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3884 d83d9d5c 2019-05-13 stsp if (err)
3885 d83d9d5c 2019-05-13 stsp return err;
3886 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3887 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3888 d83d9d5c 2019-05-13 stsp if (err)
3889 d83d9d5c 2019-05-13 stsp return err;
3890 d83d9d5c 2019-05-13 stsp }
3891 d83d9d5c 2019-05-13 stsp
3892 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3893 d0eebce4 2019-03-11 stsp if (err)
3894 d0eebce4 2019-03-11 stsp goto done;
3895 d0eebce4 2019-03-11 stsp
3896 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3897 d0eebce4 2019-03-11 stsp done:
3898 d0eebce4 2019-03-11 stsp if (ref)
3899 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3900 d0eebce4 2019-03-11 stsp free(id);
3901 d1c1ae5f 2019-08-12 stsp return err;
3902 d1c1ae5f 2019-08-12 stsp }
3903 d1c1ae5f 2019-08-12 stsp
3904 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3905 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3906 d1c1ae5f 2019-08-12 stsp {
3907 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3908 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3909 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3910 d1c1ae5f 2019-08-12 stsp
3911 d1c1ae5f 2019-08-12 stsp /*
3912 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3913 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3914 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3915 d1c1ae5f 2019-08-12 stsp */
3916 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3917 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3918 d1c1ae5f 2019-08-12 stsp
3919 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3920 d1c1ae5f 2019-08-12 stsp if (err)
3921 d1c1ae5f 2019-08-12 stsp return err;
3922 d1c1ae5f 2019-08-12 stsp
3923 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3924 d1c1ae5f 2019-08-12 stsp if (err)
3925 d1c1ae5f 2019-08-12 stsp goto done;
3926 d1c1ae5f 2019-08-12 stsp
3927 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3928 d1c1ae5f 2019-08-12 stsp done:
3929 d1c1ae5f 2019-08-12 stsp if (target_ref)
3930 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3931 d1c1ae5f 2019-08-12 stsp if (ref)
3932 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3933 d0eebce4 2019-03-11 stsp return err;
3934 d0eebce4 2019-03-11 stsp }
3935 d0eebce4 2019-03-11 stsp
3936 d0eebce4 2019-03-11 stsp static const struct got_error *
3937 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3938 d0eebce4 2019-03-11 stsp {
3939 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3940 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3941 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3942 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3943 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3944 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3945 d0eebce4 2019-03-11 stsp
3946 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3947 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3948 d0eebce4 2019-03-11 stsp switch (ch) {
3949 d0eebce4 2019-03-11 stsp case 'd':
3950 d0eebce4 2019-03-11 stsp delref = optarg;
3951 d0eebce4 2019-03-11 stsp break;
3952 d0eebce4 2019-03-11 stsp case 'r':
3953 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3954 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3955 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3956 9ba1d308 2019-10-21 stsp optarg);
3957 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3958 d0eebce4 2019-03-11 stsp break;
3959 d0eebce4 2019-03-11 stsp case 'l':
3960 d0eebce4 2019-03-11 stsp do_list = 1;
3961 d1c1ae5f 2019-08-12 stsp break;
3962 d1c1ae5f 2019-08-12 stsp case 's':
3963 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3964 d0eebce4 2019-03-11 stsp break;
3965 d0eebce4 2019-03-11 stsp default:
3966 d0eebce4 2019-03-11 stsp usage_ref();
3967 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3968 d0eebce4 2019-03-11 stsp }
3969 d0eebce4 2019-03-11 stsp }
3970 d0eebce4 2019-03-11 stsp
3971 d0eebce4 2019-03-11 stsp if (do_list && delref)
3972 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3973 d0eebce4 2019-03-11 stsp
3974 d0eebce4 2019-03-11 stsp argc -= optind;
3975 d0eebce4 2019-03-11 stsp argv += optind;
3976 d0eebce4 2019-03-11 stsp
3977 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3978 d1c1ae5f 2019-08-12 stsp if (create_symref)
3979 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3980 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3981 d0eebce4 2019-03-11 stsp if (argc > 0)
3982 d0eebce4 2019-03-11 stsp usage_ref();
3983 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3984 d0eebce4 2019-03-11 stsp usage_ref();
3985 d0eebce4 2019-03-11 stsp
3986 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3987 e0b57350 2019-03-12 stsp if (do_list) {
3988 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3989 e0b57350 2019-03-12 stsp NULL) == -1)
3990 e0b57350 2019-03-12 stsp err(1, "pledge");
3991 e0b57350 2019-03-12 stsp } else {
3992 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3993 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3994 e0b57350 2019-03-12 stsp err(1, "pledge");
3995 e0b57350 2019-03-12 stsp }
3996 d0eebce4 2019-03-11 stsp #endif
3997 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3998 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3999 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4000 d0eebce4 2019-03-11 stsp goto done;
4001 d0eebce4 2019-03-11 stsp }
4002 d0eebce4 2019-03-11 stsp
4003 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4004 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4005 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4006 d0eebce4 2019-03-11 stsp goto done;
4007 d0eebce4 2019-03-11 stsp else
4008 d0eebce4 2019-03-11 stsp error = NULL;
4009 d0eebce4 2019-03-11 stsp if (worktree) {
4010 d0eebce4 2019-03-11 stsp repo_path =
4011 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4012 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4013 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4014 d0eebce4 2019-03-11 stsp if (error)
4015 d0eebce4 2019-03-11 stsp goto done;
4016 d0eebce4 2019-03-11 stsp } else {
4017 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4018 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4019 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4020 d0eebce4 2019-03-11 stsp goto done;
4021 d0eebce4 2019-03-11 stsp }
4022 d0eebce4 2019-03-11 stsp }
4023 d0eebce4 2019-03-11 stsp }
4024 d0eebce4 2019-03-11 stsp
4025 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4026 d0eebce4 2019-03-11 stsp if (error != NULL)
4027 d0eebce4 2019-03-11 stsp goto done;
4028 d0eebce4 2019-03-11 stsp
4029 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4030 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4031 c02c541e 2019-03-29 stsp if (error)
4032 c02c541e 2019-03-29 stsp goto done;
4033 c02c541e 2019-03-29 stsp
4034 d0eebce4 2019-03-11 stsp if (do_list)
4035 d0eebce4 2019-03-11 stsp error = list_refs(repo);
4036 d0eebce4 2019-03-11 stsp else if (delref)
4037 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
4038 d1c1ae5f 2019-08-12 stsp else if (create_symref)
4039 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
4040 d0eebce4 2019-03-11 stsp else
4041 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
4042 4e759de4 2019-06-26 stsp done:
4043 4e759de4 2019-06-26 stsp if (repo)
4044 4e759de4 2019-06-26 stsp got_repo_close(repo);
4045 4e759de4 2019-06-26 stsp if (worktree)
4046 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4047 4e759de4 2019-06-26 stsp free(cwd);
4048 4e759de4 2019-06-26 stsp free(repo_path);
4049 4e759de4 2019-06-26 stsp return error;
4050 4e759de4 2019-06-26 stsp }
4051 4e759de4 2019-06-26 stsp
4052 4e759de4 2019-06-26 stsp __dead static void
4053 4e759de4 2019-06-26 stsp usage_branch(void)
4054 4e759de4 2019-06-26 stsp {
4055 4e759de4 2019-06-26 stsp fprintf(stderr,
4056 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4057 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4058 4e759de4 2019-06-26 stsp exit(1);
4059 4e759de4 2019-06-26 stsp }
4060 4e759de4 2019-06-26 stsp
4061 4e759de4 2019-06-26 stsp static const struct got_error *
4062 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4063 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4064 4e99b47e 2019-10-04 stsp {
4065 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4066 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4067 4e99b47e 2019-10-04 stsp char *refstr;
4068 4e99b47e 2019-10-04 stsp
4069 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4070 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4071 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4072 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4073 4e99b47e 2019-10-04 stsp
4074 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4075 4e99b47e 2019-10-04 stsp if (err)
4076 4e99b47e 2019-10-04 stsp return err;
4077 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4078 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4079 4e99b47e 2019-10-04 stsp marker = "* ";
4080 4e99b47e 2019-10-04 stsp else
4081 4e99b47e 2019-10-04 stsp marker = "~ ";
4082 4e99b47e 2019-10-04 stsp free(id);
4083 4e99b47e 2019-10-04 stsp }
4084 4e99b47e 2019-10-04 stsp
4085 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4086 4e99b47e 2019-10-04 stsp refname += 11;
4087 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4088 4e99b47e 2019-10-04 stsp refname += 18;
4089 4e99b47e 2019-10-04 stsp
4090 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4091 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4092 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4093 4e99b47e 2019-10-04 stsp
4094 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4095 4e99b47e 2019-10-04 stsp free(refstr);
4096 4e99b47e 2019-10-04 stsp return NULL;
4097 4e99b47e 2019-10-04 stsp }
4098 4e99b47e 2019-10-04 stsp
4099 4e99b47e 2019-10-04 stsp static const struct got_error *
4100 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4101 ad89fa31 2019-10-04 stsp {
4102 ad89fa31 2019-10-04 stsp const char *refname;
4103 ad89fa31 2019-10-04 stsp
4104 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4105 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4106 ad89fa31 2019-10-04 stsp
4107 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4108 ad89fa31 2019-10-04 stsp
4109 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4110 ad89fa31 2019-10-04 stsp refname += 11;
4111 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4112 ad89fa31 2019-10-04 stsp refname += 18;
4113 ad89fa31 2019-10-04 stsp
4114 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4115 ad89fa31 2019-10-04 stsp
4116 ad89fa31 2019-10-04 stsp return NULL;
4117 ad89fa31 2019-10-04 stsp }
4118 ad89fa31 2019-10-04 stsp
4119 ad89fa31 2019-10-04 stsp static const struct got_error *
4120 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4121 4e759de4 2019-06-26 stsp {
4122 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4123 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4124 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4125 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4126 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4127 4e759de4 2019-06-26 stsp
4128 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4129 ba882ee3 2019-07-11 stsp
4130 4e99b47e 2019-10-04 stsp if (worktree) {
4131 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4132 4e99b47e 2019-10-04 stsp worktree);
4133 4e99b47e 2019-10-04 stsp if (err)
4134 4e99b47e 2019-10-04 stsp return err;
4135 4e99b47e 2019-10-04 stsp
4136 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4137 4e99b47e 2019-10-04 stsp worktree);
4138 4e99b47e 2019-10-04 stsp if (err)
4139 4e99b47e 2019-10-04 stsp return err;
4140 4e99b47e 2019-10-04 stsp
4141 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4142 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4143 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4144 4e99b47e 2019-10-04 stsp if (err)
4145 4e99b47e 2019-10-04 stsp return err;
4146 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4147 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4148 4e99b47e 2019-10-04 stsp }
4149 4e99b47e 2019-10-04 stsp }
4150 4e99b47e 2019-10-04 stsp
4151 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4152 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4153 4e759de4 2019-06-26 stsp if (err)
4154 4e759de4 2019-06-26 stsp return err;
4155 4e759de4 2019-06-26 stsp
4156 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4157 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4158 4e759de4 2019-06-26 stsp
4159 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4160 4e759de4 2019-06-26 stsp return NULL;
4161 4e759de4 2019-06-26 stsp }
4162 4e759de4 2019-06-26 stsp
4163 4e759de4 2019-06-26 stsp static const struct got_error *
4164 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4165 45cd4e47 2019-08-25 stsp const char *branch_name)
4166 4e759de4 2019-06-26 stsp {
4167 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4168 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4169 4e759de4 2019-06-26 stsp char *refname;
4170 4e759de4 2019-06-26 stsp
4171 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4172 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4173 4e759de4 2019-06-26 stsp
4174 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4175 4e759de4 2019-06-26 stsp if (err)
4176 4e759de4 2019-06-26 stsp goto done;
4177 4e759de4 2019-06-26 stsp
4178 45cd4e47 2019-08-25 stsp if (worktree &&
4179 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4180 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4181 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4182 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4183 45cd4e47 2019-08-25 stsp goto done;
4184 45cd4e47 2019-08-25 stsp }
4185 45cd4e47 2019-08-25 stsp
4186 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4187 4e759de4 2019-06-26 stsp done:
4188 45cd4e47 2019-08-25 stsp if (ref)
4189 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4190 4e759de4 2019-06-26 stsp free(refname);
4191 4e759de4 2019-06-26 stsp return err;
4192 4e759de4 2019-06-26 stsp }
4193 4e759de4 2019-06-26 stsp
4194 4e759de4 2019-06-26 stsp static const struct got_error *
4195 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4196 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4197 4e759de4 2019-06-26 stsp {
4198 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4199 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4200 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4201 d3f84d51 2019-07-11 stsp
4202 d3f84d51 2019-07-11 stsp /*
4203 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4204 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4205 d3f84d51 2019-07-11 stsp * an unintended typo.
4206 d3f84d51 2019-07-11 stsp */
4207 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4208 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4209 4e759de4 2019-06-26 stsp
4210 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4211 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4212 4e759de4 2019-06-26 stsp goto done;
4213 4e759de4 2019-06-26 stsp }
4214 4e759de4 2019-06-26 stsp
4215 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4216 4e759de4 2019-06-26 stsp if (err == NULL) {
4217 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4218 4e759de4 2019-06-26 stsp goto done;
4219 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4220 4e759de4 2019-06-26 stsp goto done;
4221 4e759de4 2019-06-26 stsp
4222 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4223 4e759de4 2019-06-26 stsp if (err)
4224 4e759de4 2019-06-26 stsp goto done;
4225 4e759de4 2019-06-26 stsp
4226 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4227 d0eebce4 2019-03-11 stsp done:
4228 4e759de4 2019-06-26 stsp if (ref)
4229 4e759de4 2019-06-26 stsp got_ref_close(ref);
4230 4e759de4 2019-06-26 stsp free(base_refname);
4231 4e759de4 2019-06-26 stsp free(refname);
4232 4e759de4 2019-06-26 stsp return err;
4233 4e759de4 2019-06-26 stsp }
4234 4e759de4 2019-06-26 stsp
4235 4e759de4 2019-06-26 stsp static const struct got_error *
4236 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4237 4e759de4 2019-06-26 stsp {
4238 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4239 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4240 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4241 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4242 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4243 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4244 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4245 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4246 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4247 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4248 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4249 4e759de4 2019-06-26 stsp
4250 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4251 da76fce2 2020-02-24 stsp
4252 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4253 4e759de4 2019-06-26 stsp switch (ch) {
4254 a74f7e83 2019-11-10 stsp case 'c':
4255 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4256 a74f7e83 2019-11-10 stsp break;
4257 4e759de4 2019-06-26 stsp case 'd':
4258 4e759de4 2019-06-26 stsp delref = optarg;
4259 4e759de4 2019-06-26 stsp break;
4260 4e759de4 2019-06-26 stsp case 'r':
4261 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4262 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4263 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4264 9ba1d308 2019-10-21 stsp optarg);
4265 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4266 4e759de4 2019-06-26 stsp break;
4267 4e759de4 2019-06-26 stsp case 'l':
4268 4e759de4 2019-06-26 stsp do_list = 1;
4269 da76fce2 2020-02-24 stsp break;
4270 da76fce2 2020-02-24 stsp case 'n':
4271 da76fce2 2020-02-24 stsp do_update = 0;
4272 4e759de4 2019-06-26 stsp break;
4273 4e759de4 2019-06-26 stsp default:
4274 4e759de4 2019-06-26 stsp usage_branch();
4275 4e759de4 2019-06-26 stsp /* NOTREACHED */
4276 4e759de4 2019-06-26 stsp }
4277 4e759de4 2019-06-26 stsp }
4278 4e759de4 2019-06-26 stsp
4279 4e759de4 2019-06-26 stsp if (do_list && delref)
4280 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
4281 4e759de4 2019-06-26 stsp
4282 4e759de4 2019-06-26 stsp argc -= optind;
4283 4e759de4 2019-06-26 stsp argv += optind;
4284 ad89fa31 2019-10-04 stsp
4285 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4286 ad89fa31 2019-10-04 stsp do_show = 1;
4287 4e759de4 2019-06-26 stsp
4288 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4289 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4290 a74f7e83 2019-11-10 stsp
4291 4e759de4 2019-06-26 stsp if (do_list || delref) {
4292 4e759de4 2019-06-26 stsp if (argc > 0)
4293 4e759de4 2019-06-26 stsp usage_branch();
4294 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4295 4e759de4 2019-06-26 stsp usage_branch();
4296 4e759de4 2019-06-26 stsp
4297 4e759de4 2019-06-26 stsp #ifndef PROFILE
4298 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4299 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4300 4e759de4 2019-06-26 stsp NULL) == -1)
4301 4e759de4 2019-06-26 stsp err(1, "pledge");
4302 4e759de4 2019-06-26 stsp } else {
4303 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4304 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4305 4e759de4 2019-06-26 stsp err(1, "pledge");
4306 4e759de4 2019-06-26 stsp }
4307 4e759de4 2019-06-26 stsp #endif
4308 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4309 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4310 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4311 4e759de4 2019-06-26 stsp goto done;
4312 4e759de4 2019-06-26 stsp }
4313 4e759de4 2019-06-26 stsp
4314 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4315 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4316 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4317 4e759de4 2019-06-26 stsp goto done;
4318 4e759de4 2019-06-26 stsp else
4319 4e759de4 2019-06-26 stsp error = NULL;
4320 4e759de4 2019-06-26 stsp if (worktree) {
4321 4e759de4 2019-06-26 stsp repo_path =
4322 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4323 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4324 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4325 4e759de4 2019-06-26 stsp if (error)
4326 4e759de4 2019-06-26 stsp goto done;
4327 4e759de4 2019-06-26 stsp } else {
4328 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4329 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4330 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4331 4e759de4 2019-06-26 stsp goto done;
4332 4e759de4 2019-06-26 stsp }
4333 4e759de4 2019-06-26 stsp }
4334 4e759de4 2019-06-26 stsp }
4335 4e759de4 2019-06-26 stsp
4336 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4337 4e759de4 2019-06-26 stsp if (error != NULL)
4338 4e759de4 2019-06-26 stsp goto done;
4339 4e759de4 2019-06-26 stsp
4340 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4341 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4342 4e759de4 2019-06-26 stsp if (error)
4343 4e759de4 2019-06-26 stsp goto done;
4344 4e759de4 2019-06-26 stsp
4345 ad89fa31 2019-10-04 stsp if (do_show)
4346 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4347 ad89fa31 2019-10-04 stsp else if (do_list)
4348 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4349 4e759de4 2019-06-26 stsp else if (delref)
4350 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4351 4e759de4 2019-06-26 stsp else {
4352 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4353 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4354 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4355 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4356 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4357 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4358 a74f7e83 2019-11-10 stsp if (error)
4359 a74f7e83 2019-11-10 stsp goto done;
4360 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4361 da76fce2 2020-02-24 stsp if (error)
4362 da76fce2 2020-02-24 stsp goto done;
4363 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4364 da76fce2 2020-02-24 stsp int did_something = 0;
4365 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4366 da76fce2 2020-02-24 stsp
4367 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4368 da76fce2 2020-02-24 stsp if (error)
4369 da76fce2 2020-02-24 stsp goto done;
4370 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4371 da76fce2 2020-02-24 stsp worktree);
4372 da76fce2 2020-02-24 stsp if (error)
4373 da76fce2 2020-02-24 stsp goto done;
4374 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4375 da76fce2 2020-02-24 stsp == -1) {
4376 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4377 da76fce2 2020-02-24 stsp goto done;
4378 da76fce2 2020-02-24 stsp }
4379 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4380 da76fce2 2020-02-24 stsp free(branch_refname);
4381 da76fce2 2020-02-24 stsp if (error)
4382 da76fce2 2020-02-24 stsp goto done;
4383 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4384 da76fce2 2020-02-24 stsp repo);
4385 da76fce2 2020-02-24 stsp if (error)
4386 da76fce2 2020-02-24 stsp goto done;
4387 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4388 da76fce2 2020-02-24 stsp commit_id);
4389 da76fce2 2020-02-24 stsp if (error)
4390 da76fce2 2020-02-24 stsp goto done;
4391 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4392 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4393 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4394 da76fce2 2020-02-24 stsp if (error)
4395 da76fce2 2020-02-24 stsp goto done;
4396 da76fce2 2020-02-24 stsp if (did_something)
4397 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4398 da76fce2 2020-02-24 stsp }
4399 4e759de4 2019-06-26 stsp }
4400 4e759de4 2019-06-26 stsp done:
4401 da76fce2 2020-02-24 stsp if (ref)
4402 da76fce2 2020-02-24 stsp got_ref_close(ref);
4403 d0eebce4 2019-03-11 stsp if (repo)
4404 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4405 d0eebce4 2019-03-11 stsp if (worktree)
4406 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4407 d0eebce4 2019-03-11 stsp free(cwd);
4408 d0eebce4 2019-03-11 stsp free(repo_path);
4409 da76fce2 2020-02-24 stsp free(commit_id);
4410 da76fce2 2020-02-24 stsp free(commit_id_str);
4411 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4412 da76fce2 2020-02-24 stsp free((char *)pe->path);
4413 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4414 d00136be 2019-03-26 stsp return error;
4415 d00136be 2019-03-26 stsp }
4416 d00136be 2019-03-26 stsp
4417 8e7bd50a 2019-08-22 stsp
4418 d00136be 2019-03-26 stsp __dead static void
4419 8e7bd50a 2019-08-22 stsp usage_tag(void)
4420 8e7bd50a 2019-08-22 stsp {
4421 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4422 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4423 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4424 8e7bd50a 2019-08-22 stsp exit(1);
4425 b8bad2ba 2019-08-23 stsp }
4426 b8bad2ba 2019-08-23 stsp
4427 b8bad2ba 2019-08-23 stsp #if 0
4428 b8bad2ba 2019-08-23 stsp static const struct got_error *
4429 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4430 b8bad2ba 2019-08-23 stsp {
4431 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4432 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4433 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4434 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4435 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4436 b8bad2ba 2019-08-23 stsp
4437 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4438 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4439 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4440 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4441 b8bad2ba 2019-08-23 stsp if (err)
4442 b8bad2ba 2019-08-23 stsp return err;
4443 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4444 b8bad2ba 2019-08-23 stsp continue;
4445 b8bad2ba 2019-08-23 stsp } else {
4446 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4447 b8bad2ba 2019-08-23 stsp if (err)
4448 b8bad2ba 2019-08-23 stsp break;
4449 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4450 b8bad2ba 2019-08-23 stsp free(re_id);
4451 b8bad2ba 2019-08-23 stsp if (err)
4452 b8bad2ba 2019-08-23 stsp break;
4453 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4454 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4455 b8bad2ba 2019-08-23 stsp }
4456 b8bad2ba 2019-08-23 stsp
4457 b8bad2ba 2019-08-23 stsp while (se) {
4458 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4459 b8bad2ba 2019-08-23 stsp if (err)
4460 b8bad2ba 2019-08-23 stsp break;
4461 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4462 b8bad2ba 2019-08-23 stsp free(se_id);
4463 b8bad2ba 2019-08-23 stsp if (err)
4464 b8bad2ba 2019-08-23 stsp break;
4465 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4466 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4467 b8bad2ba 2019-08-23 stsp
4468 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4469 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4470 b8bad2ba 2019-08-23 stsp if (err)
4471 b8bad2ba 2019-08-23 stsp return err;
4472 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4473 b8bad2ba 2019-08-23 stsp break;
4474 b8bad2ba 2019-08-23 stsp }
4475 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4476 b8bad2ba 2019-08-23 stsp continue;
4477 b8bad2ba 2019-08-23 stsp }
4478 b8bad2ba 2019-08-23 stsp }
4479 b8bad2ba 2019-08-23 stsp done:
4480 b8bad2ba 2019-08-23 stsp return err;
4481 8e7bd50a 2019-08-22 stsp }
4482 b8bad2ba 2019-08-23 stsp #endif
4483 b8bad2ba 2019-08-23 stsp
4484 b8bad2ba 2019-08-23 stsp static const struct got_error *
4485 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4486 8e7bd50a 2019-08-22 stsp {
4487 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4488 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4489 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4490 8e7bd50a 2019-08-22 stsp
4491 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4492 8e7bd50a 2019-08-22 stsp
4493 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4494 8e7bd50a 2019-08-22 stsp if (err)
4495 8e7bd50a 2019-08-22 stsp return err;
4496 8e7bd50a 2019-08-22 stsp
4497 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4498 8e7bd50a 2019-08-22 stsp const char *refname;
4499 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4500 8e7bd50a 2019-08-22 stsp char datebuf[26];
4501 d4efa91b 2020-01-14 stsp const char *tagger;
4502 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4503 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4504 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4505 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4506 8e7bd50a 2019-08-22 stsp
4507 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4508 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4509 8e7bd50a 2019-08-22 stsp continue;
4510 8e7bd50a 2019-08-22 stsp refname += 10;
4511 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4512 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4513 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4514 8e7bd50a 2019-08-22 stsp break;
4515 8e7bd50a 2019-08-22 stsp }
4516 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4517 8e7bd50a 2019-08-22 stsp free(refstr);
4518 8e7bd50a 2019-08-22 stsp
4519 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4520 8e7bd50a 2019-08-22 stsp if (err)
4521 8e7bd50a 2019-08-22 stsp break;
4522 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4523 d4efa91b 2020-01-14 stsp if (err) {
4524 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4525 d4efa91b 2020-01-14 stsp free(id);
4526 d4efa91b 2020-01-14 stsp break;
4527 d4efa91b 2020-01-14 stsp }
4528 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4529 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4530 d4efa91b 2020-01-14 stsp if (err) {
4531 d4efa91b 2020-01-14 stsp free(id);
4532 d4efa91b 2020-01-14 stsp break;
4533 d4efa91b 2020-01-14 stsp }
4534 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4535 d4efa91b 2020-01-14 stsp tagger_time =
4536 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4537 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4538 d4efa91b 2020-01-14 stsp free(id);
4539 d4efa91b 2020-01-14 stsp if (err)
4540 d4efa91b 2020-01-14 stsp break;
4541 d4efa91b 2020-01-14 stsp } else {
4542 d4efa91b 2020-01-14 stsp free(id);
4543 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4544 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4545 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4546 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4547 d4efa91b 2020-01-14 stsp if (err)
4548 d4efa91b 2020-01-14 stsp break;
4549 d4efa91b 2020-01-14 stsp }
4550 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4551 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4552 2417344c 2019-08-23 stsp if (datestr)
4553 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4554 d4efa91b 2020-01-14 stsp if (commit)
4555 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4556 d4efa91b 2020-01-14 stsp else {
4557 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4558 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4559 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4560 d4efa91b 2020-01-14 stsp id_str);
4561 d4efa91b 2020-01-14 stsp break;
4562 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4563 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4564 d4efa91b 2020-01-14 stsp id_str);
4565 d4efa91b 2020-01-14 stsp break;
4566 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4567 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4568 d4efa91b 2020-01-14 stsp id_str);
4569 d4efa91b 2020-01-14 stsp break;
4570 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4571 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4572 d4efa91b 2020-01-14 stsp id_str);
4573 d4efa91b 2020-01-14 stsp break;
4574 d4efa91b 2020-01-14 stsp default:
4575 d4efa91b 2020-01-14 stsp break;
4576 d4efa91b 2020-01-14 stsp }
4577 8e7bd50a 2019-08-22 stsp }
4578 8e7bd50a 2019-08-22 stsp free(id_str);
4579 d4efa91b 2020-01-14 stsp if (commit) {
4580 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4581 d4efa91b 2020-01-14 stsp if (err)
4582 d4efa91b 2020-01-14 stsp break;
4583 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4584 d4efa91b 2020-01-14 stsp } else {
4585 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4586 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4587 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4588 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4589 d4efa91b 2020-01-14 stsp break;
4590 d4efa91b 2020-01-14 stsp }
4591 8e7bd50a 2019-08-22 stsp }
4592 8e7bd50a 2019-08-22 stsp
4593 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4594 8e7bd50a 2019-08-22 stsp do {
4595 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4596 8e7bd50a 2019-08-22 stsp if (line)
4597 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4598 8e7bd50a 2019-08-22 stsp } while (line);
4599 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4600 8e7bd50a 2019-08-22 stsp }
4601 8e7bd50a 2019-08-22 stsp
4602 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4603 8e7bd50a 2019-08-22 stsp return NULL;
4604 8e7bd50a 2019-08-22 stsp }
4605 8e7bd50a 2019-08-22 stsp
4606 8e7bd50a 2019-08-22 stsp static const struct got_error *
4607 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4608 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4609 8e7bd50a 2019-08-22 stsp {
4610 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4611 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4612 f372d5cd 2019-10-21 stsp char *editor = NULL;
4613 8e7bd50a 2019-08-22 stsp int fd = -1;
4614 8e7bd50a 2019-08-22 stsp
4615 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4616 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4617 8e7bd50a 2019-08-22 stsp goto done;
4618 8e7bd50a 2019-08-22 stsp }
4619 8e7bd50a 2019-08-22 stsp
4620 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4621 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4622 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4623 8e7bd50a 2019-08-22 stsp goto done;
4624 8e7bd50a 2019-08-22 stsp }
4625 8e7bd50a 2019-08-22 stsp
4626 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4627 8e7bd50a 2019-08-22 stsp if (err)
4628 8e7bd50a 2019-08-22 stsp goto done;
4629 8e7bd50a 2019-08-22 stsp
4630 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4631 8e7bd50a 2019-08-22 stsp close(fd);
4632 8e7bd50a 2019-08-22 stsp
4633 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4634 8e7bd50a 2019-08-22 stsp if (err)
4635 8e7bd50a 2019-08-22 stsp goto done;
4636 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4637 8e7bd50a 2019-08-22 stsp done:
4638 8e7bd50a 2019-08-22 stsp free(initial_content);
4639 8e7bd50a 2019-08-22 stsp free(template);
4640 8e7bd50a 2019-08-22 stsp free(editor);
4641 8e7bd50a 2019-08-22 stsp
4642 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4643 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4644 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4645 8e7bd50a 2019-08-22 stsp if (err) {
4646 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4647 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4648 8e7bd50a 2019-08-22 stsp }
4649 8e7bd50a 2019-08-22 stsp }
4650 8e7bd50a 2019-08-22 stsp return err;
4651 8e7bd50a 2019-08-22 stsp }
4652 8e7bd50a 2019-08-22 stsp
4653 8e7bd50a 2019-08-22 stsp static const struct got_error *
4654 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4655 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4656 8e7bd50a 2019-08-22 stsp {
4657 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4658 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4659 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4660 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4661 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4662 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4663 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4664 8e7bd50a 2019-08-22 stsp
4665 8e7bd50a 2019-08-22 stsp /*
4666 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4667 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4668 8e7bd50a 2019-08-22 stsp * an unintended typo.
4669 8e7bd50a 2019-08-22 stsp */
4670 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4671 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4672 8e7bd50a 2019-08-22 stsp
4673 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4674 8e7bd50a 2019-08-22 stsp if (err)
4675 8e7bd50a 2019-08-22 stsp return err;
4676 8e7bd50a 2019-08-22 stsp
4677 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4678 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4679 8e7bd50a 2019-08-22 stsp if (err)
4680 8e7bd50a 2019-08-22 stsp goto done;
4681 8e7bd50a 2019-08-22 stsp
4682 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4683 8e7bd50a 2019-08-22 stsp if (err)
4684 8e7bd50a 2019-08-22 stsp goto done;
4685 8e7bd50a 2019-08-22 stsp
4686 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4687 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4688 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4689 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4690 8e7bd50a 2019-08-22 stsp goto done;
4691 8e7bd50a 2019-08-22 stsp }
4692 8e7bd50a 2019-08-22 stsp tag_name += 10;
4693 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4694 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4695 8e7bd50a 2019-08-22 stsp goto done;
4696 8e7bd50a 2019-08-22 stsp }
4697 8e7bd50a 2019-08-22 stsp
4698 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4699 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4700 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4701 8e7bd50a 2019-08-22 stsp goto done;
4702 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4703 8e7bd50a 2019-08-22 stsp goto done;
4704 8e7bd50a 2019-08-22 stsp
4705 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4706 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4707 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4708 f372d5cd 2019-10-21 stsp if (err) {
4709 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4710 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4711 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4712 8e7bd50a 2019-08-22 stsp goto done;
4713 f372d5cd 2019-10-21 stsp }
4714 8e7bd50a 2019-08-22 stsp }
4715 8e7bd50a 2019-08-22 stsp
4716 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4717 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4718 f372d5cd 2019-10-21 stsp if (err) {
4719 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4720 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4721 8e7bd50a 2019-08-22 stsp goto done;
4722 f372d5cd 2019-10-21 stsp }
4723 8e7bd50a 2019-08-22 stsp
4724 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4725 f372d5cd 2019-10-21 stsp if (err) {
4726 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4727 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4728 8e7bd50a 2019-08-22 stsp goto done;
4729 f372d5cd 2019-10-21 stsp }
4730 8e7bd50a 2019-08-22 stsp
4731 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4732 f372d5cd 2019-10-21 stsp if (err) {
4733 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4734 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4735 f372d5cd 2019-10-21 stsp goto done;
4736 f372d5cd 2019-10-21 stsp }
4737 8e7bd50a 2019-08-22 stsp
4738 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4739 f372d5cd 2019-10-21 stsp if (err) {
4740 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4741 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4742 f372d5cd 2019-10-21 stsp goto done;
4743 8e7bd50a 2019-08-22 stsp }
4744 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4745 8e7bd50a 2019-08-22 stsp done:
4746 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4747 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4748 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4749 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4750 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4751 f372d5cd 2019-10-21 stsp free(tag_id_str);
4752 8e7bd50a 2019-08-22 stsp if (ref)
4753 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4754 8e7bd50a 2019-08-22 stsp free(commit_id);
4755 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4756 8e7bd50a 2019-08-22 stsp free(refname);
4757 8e7bd50a 2019-08-22 stsp free(tagmsg);
4758 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4759 aba9c984 2019-09-08 stsp free(tagger);
4760 8e7bd50a 2019-08-22 stsp return err;
4761 8e7bd50a 2019-08-22 stsp }
4762 8e7bd50a 2019-08-22 stsp
4763 8e7bd50a 2019-08-22 stsp static const struct got_error *
4764 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4765 8e7bd50a 2019-08-22 stsp {
4766 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4767 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4768 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4769 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4770 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4771 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4772 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4773 8e7bd50a 2019-08-22 stsp
4774 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4775 8e7bd50a 2019-08-22 stsp switch (ch) {
4776 80106605 2020-02-24 stsp case 'c':
4777 80106605 2020-02-24 stsp commit_id_arg = optarg;
4778 80106605 2020-02-24 stsp break;
4779 8e7bd50a 2019-08-22 stsp case 'm':
4780 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4781 8e7bd50a 2019-08-22 stsp break;
4782 8e7bd50a 2019-08-22 stsp case 'r':
4783 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4784 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4785 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4786 9ba1d308 2019-10-21 stsp optarg);
4787 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4788 8e7bd50a 2019-08-22 stsp break;
4789 8e7bd50a 2019-08-22 stsp case 'l':
4790 8e7bd50a 2019-08-22 stsp do_list = 1;
4791 8e7bd50a 2019-08-22 stsp break;
4792 8e7bd50a 2019-08-22 stsp default:
4793 8e7bd50a 2019-08-22 stsp usage_tag();
4794 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4795 8e7bd50a 2019-08-22 stsp }
4796 8e7bd50a 2019-08-22 stsp }
4797 8e7bd50a 2019-08-22 stsp
4798 8e7bd50a 2019-08-22 stsp argc -= optind;
4799 8e7bd50a 2019-08-22 stsp argv += optind;
4800 8e7bd50a 2019-08-22 stsp
4801 8e7bd50a 2019-08-22 stsp if (do_list) {
4802 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4803 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4804 8e7bd50a 2019-08-22 stsp if (tagmsg)
4805 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4806 8e7bd50a 2019-08-22 stsp if (argc > 0)
4807 8e7bd50a 2019-08-22 stsp usage_tag();
4808 80106605 2020-02-24 stsp } else if (argc != 1)
4809 8e7bd50a 2019-08-22 stsp usage_tag();
4810 80106605 2020-02-24 stsp
4811 a2887370 2019-08-23 stsp tag_name = argv[0];
4812 8e7bd50a 2019-08-22 stsp
4813 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4814 8e7bd50a 2019-08-22 stsp if (do_list) {
4815 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4816 8e7bd50a 2019-08-22 stsp NULL) == -1)
4817 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4818 8e7bd50a 2019-08-22 stsp } else {
4819 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4820 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4821 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4822 8e7bd50a 2019-08-22 stsp }
4823 8e7bd50a 2019-08-22 stsp #endif
4824 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4825 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4826 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4827 8e7bd50a 2019-08-22 stsp goto done;
4828 8e7bd50a 2019-08-22 stsp }
4829 8e7bd50a 2019-08-22 stsp
4830 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4831 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4832 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4833 8e7bd50a 2019-08-22 stsp goto done;
4834 8e7bd50a 2019-08-22 stsp else
4835 8e7bd50a 2019-08-22 stsp error = NULL;
4836 8e7bd50a 2019-08-22 stsp if (worktree) {
4837 8e7bd50a 2019-08-22 stsp repo_path =
4838 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4839 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4840 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4841 8e7bd50a 2019-08-22 stsp if (error)
4842 8e7bd50a 2019-08-22 stsp goto done;
4843 8e7bd50a 2019-08-22 stsp } else {
4844 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4845 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4846 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4847 8e7bd50a 2019-08-22 stsp goto done;
4848 8e7bd50a 2019-08-22 stsp }
4849 8e7bd50a 2019-08-22 stsp }
4850 8e7bd50a 2019-08-22 stsp }
4851 8e7bd50a 2019-08-22 stsp
4852 8e7bd50a 2019-08-22 stsp if (do_list) {
4853 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4854 c9956ddf 2019-09-08 stsp if (error != NULL)
4855 c9956ddf 2019-09-08 stsp goto done;
4856 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4857 8e7bd50a 2019-08-22 stsp if (error)
4858 8e7bd50a 2019-08-22 stsp goto done;
4859 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4860 8e7bd50a 2019-08-22 stsp } else {
4861 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4862 c9956ddf 2019-09-08 stsp if (error)
4863 c9956ddf 2019-09-08 stsp goto done;
4864 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4865 c9956ddf 2019-09-08 stsp if (error != NULL)
4866 c9956ddf 2019-09-08 stsp goto done;
4867 c9956ddf 2019-09-08 stsp
4868 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4869 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4870 8e7bd50a 2019-08-22 stsp if (error)
4871 8e7bd50a 2019-08-22 stsp goto done;
4872 8e7bd50a 2019-08-22 stsp }
4873 8e7bd50a 2019-08-22 stsp
4874 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4875 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4876 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4877 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4878 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4879 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4880 8e7bd50a 2019-08-22 stsp if (error)
4881 8e7bd50a 2019-08-22 stsp goto done;
4882 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4883 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4884 8e7bd50a 2019-08-22 stsp if (error)
4885 8e7bd50a 2019-08-22 stsp goto done;
4886 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4887 8e7bd50a 2019-08-22 stsp free(commit_id);
4888 8e7bd50a 2019-08-22 stsp if (error)
4889 8e7bd50a 2019-08-22 stsp goto done;
4890 8e7bd50a 2019-08-22 stsp }
4891 8e7bd50a 2019-08-22 stsp
4892 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4893 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4894 8e7bd50a 2019-08-22 stsp }
4895 8e7bd50a 2019-08-22 stsp done:
4896 8e7bd50a 2019-08-22 stsp if (repo)
4897 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4898 8e7bd50a 2019-08-22 stsp if (worktree)
4899 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4900 8e7bd50a 2019-08-22 stsp free(cwd);
4901 8e7bd50a 2019-08-22 stsp free(repo_path);
4902 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4903 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4904 8e7bd50a 2019-08-22 stsp return error;
4905 8e7bd50a 2019-08-22 stsp }
4906 8e7bd50a 2019-08-22 stsp
4907 8e7bd50a 2019-08-22 stsp __dead static void
4908 d00136be 2019-03-26 stsp usage_add(void)
4909 d00136be 2019-03-26 stsp {
4910 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4911 022fae89 2019-12-06 tracey getprogname());
4912 d00136be 2019-03-26 stsp exit(1);
4913 d00136be 2019-03-26 stsp }
4914 d00136be 2019-03-26 stsp
4915 d00136be 2019-03-26 stsp static const struct got_error *
4916 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4917 4e68cba3 2019-11-23 stsp {
4918 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4919 4e68cba3 2019-11-23 stsp path++;
4920 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4921 4e68cba3 2019-11-23 stsp return NULL;
4922 4e68cba3 2019-11-23 stsp }
4923 4e68cba3 2019-11-23 stsp
4924 4e68cba3 2019-11-23 stsp static const struct got_error *
4925 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4926 d00136be 2019-03-26 stsp {
4927 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4928 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4929 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4930 1dd54920 2019-05-11 stsp char *cwd = NULL;
4931 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4932 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4933 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4934 1dd54920 2019-05-11 stsp
4935 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4936 d00136be 2019-03-26 stsp
4937 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4938 d00136be 2019-03-26 stsp switch (ch) {
4939 022fae89 2019-12-06 tracey case 'I':
4940 022fae89 2019-12-06 tracey no_ignores = 1;
4941 022fae89 2019-12-06 tracey break;
4942 4e68cba3 2019-11-23 stsp case 'R':
4943 4e68cba3 2019-11-23 stsp can_recurse = 1;
4944 4e68cba3 2019-11-23 stsp break;
4945 d00136be 2019-03-26 stsp default:
4946 d00136be 2019-03-26 stsp usage_add();
4947 d00136be 2019-03-26 stsp /* NOTREACHED */
4948 d00136be 2019-03-26 stsp }
4949 d00136be 2019-03-26 stsp }
4950 d00136be 2019-03-26 stsp
4951 d00136be 2019-03-26 stsp argc -= optind;
4952 d00136be 2019-03-26 stsp argv += optind;
4953 d00136be 2019-03-26 stsp
4954 43012d58 2019-07-14 stsp #ifndef PROFILE
4955 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4956 43012d58 2019-07-14 stsp NULL) == -1)
4957 43012d58 2019-07-14 stsp err(1, "pledge");
4958 43012d58 2019-07-14 stsp #endif
4959 723c305c 2019-05-11 jcs if (argc < 1)
4960 d00136be 2019-03-26 stsp usage_add();
4961 d00136be 2019-03-26 stsp
4962 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4963 d00136be 2019-03-26 stsp if (cwd == NULL) {
4964 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4965 d00136be 2019-03-26 stsp goto done;
4966 d00136be 2019-03-26 stsp }
4967 723c305c 2019-05-11 jcs
4968 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4969 d00136be 2019-03-26 stsp if (error)
4970 d00136be 2019-03-26 stsp goto done;
4971 d00136be 2019-03-26 stsp
4972 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4973 c9956ddf 2019-09-08 stsp NULL);
4974 031a5338 2019-03-26 stsp if (error != NULL)
4975 031a5338 2019-03-26 stsp goto done;
4976 031a5338 2019-03-26 stsp
4977 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4978 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4979 d00136be 2019-03-26 stsp if (error)
4980 d00136be 2019-03-26 stsp goto done;
4981 d00136be 2019-03-26 stsp
4982 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4983 6d022e97 2019-08-04 stsp if (error)
4984 022fae89 2019-12-06 tracey goto done;
4985 022fae89 2019-12-06 tracey
4986 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4987 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4988 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4989 6d022e97 2019-08-04 stsp goto done;
4990 723c305c 2019-05-11 jcs
4991 022fae89 2019-12-06 tracey }
4992 022fae89 2019-12-06 tracey
4993 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4994 4e68cba3 2019-11-23 stsp char *ondisk_path;
4995 4e68cba3 2019-11-23 stsp struct stat sb;
4996 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4997 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4998 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4999 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5000 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5001 4e68cba3 2019-11-23 stsp goto done;
5002 4e68cba3 2019-11-23 stsp }
5003 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5004 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5005 4e68cba3 2019-11-23 stsp free(ondisk_path);
5006 4e68cba3 2019-11-23 stsp continue;
5007 4e68cba3 2019-11-23 stsp }
5008 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5009 4e68cba3 2019-11-23 stsp ondisk_path);
5010 4e68cba3 2019-11-23 stsp free(ondisk_path);
5011 4e68cba3 2019-11-23 stsp goto done;
5012 4e68cba3 2019-11-23 stsp }
5013 4e68cba3 2019-11-23 stsp free(ondisk_path);
5014 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5015 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5016 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5017 4e68cba3 2019-11-23 stsp goto done;
5018 4e68cba3 2019-11-23 stsp }
5019 4e68cba3 2019-11-23 stsp }
5020 4e68cba3 2019-11-23 stsp }
5021 022fae89 2019-12-06 tracey
5022 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5023 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5024 d00136be 2019-03-26 stsp done:
5025 031a5338 2019-03-26 stsp if (repo)
5026 031a5338 2019-03-26 stsp got_repo_close(repo);
5027 d00136be 2019-03-26 stsp if (worktree)
5028 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5029 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5030 1dd54920 2019-05-11 stsp free((char *)pe->path);
5031 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5032 2ec1f75b 2019-03-26 stsp free(cwd);
5033 2ec1f75b 2019-03-26 stsp return error;
5034 2ec1f75b 2019-03-26 stsp }
5035 2ec1f75b 2019-03-26 stsp
5036 2ec1f75b 2019-03-26 stsp __dead static void
5037 648e4ef7 2019-07-09 stsp usage_remove(void)
5038 2ec1f75b 2019-03-26 stsp {
5039 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5040 f2a9dc41 2019-12-13 tracey getprogname());
5041 2ec1f75b 2019-03-26 stsp exit(1);
5042 2a06fe5f 2019-08-24 stsp }
5043 2a06fe5f 2019-08-24 stsp
5044 2a06fe5f 2019-08-24 stsp static const struct got_error *
5045 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5046 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5047 2a06fe5f 2019-08-24 stsp {
5048 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5049 f2a9dc41 2019-12-13 tracey path++;
5050 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5051 2a06fe5f 2019-08-24 stsp return NULL;
5052 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5053 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5054 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5055 2a06fe5f 2019-08-24 stsp return NULL;
5056 2ec1f75b 2019-03-26 stsp }
5057 2ec1f75b 2019-03-26 stsp
5058 2ec1f75b 2019-03-26 stsp static const struct got_error *
5059 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5060 2ec1f75b 2019-03-26 stsp {
5061 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5062 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5063 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5064 17ed4618 2019-06-02 stsp char *cwd = NULL;
5065 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5066 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5067 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5068 2ec1f75b 2019-03-26 stsp
5069 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5070 17ed4618 2019-06-02 stsp
5071 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5072 2ec1f75b 2019-03-26 stsp switch (ch) {
5073 2ec1f75b 2019-03-26 stsp case 'f':
5074 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5075 2ec1f75b 2019-03-26 stsp break;
5076 70e3e7f5 2019-12-13 tracey case 'k':
5077 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5078 70e3e7f5 2019-12-13 tracey break;
5079 f2a9dc41 2019-12-13 tracey case 'R':
5080 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5081 f2a9dc41 2019-12-13 tracey break;
5082 2ec1f75b 2019-03-26 stsp default:
5083 f2a9dc41 2019-12-13 tracey usage_remove();
5084 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5085 2ec1f75b 2019-03-26 stsp }
5086 2ec1f75b 2019-03-26 stsp }
5087 2ec1f75b 2019-03-26 stsp
5088 2ec1f75b 2019-03-26 stsp argc -= optind;
5089 2ec1f75b 2019-03-26 stsp argv += optind;
5090 2ec1f75b 2019-03-26 stsp
5091 43012d58 2019-07-14 stsp #ifndef PROFILE
5092 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5093 43012d58 2019-07-14 stsp NULL) == -1)
5094 43012d58 2019-07-14 stsp err(1, "pledge");
5095 43012d58 2019-07-14 stsp #endif
5096 17ed4618 2019-06-02 stsp if (argc < 1)
5097 648e4ef7 2019-07-09 stsp usage_remove();
5098 2ec1f75b 2019-03-26 stsp
5099 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5100 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5101 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5102 2ec1f75b 2019-03-26 stsp goto done;
5103 2ec1f75b 2019-03-26 stsp }
5104 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5105 2ec1f75b 2019-03-26 stsp if (error)
5106 2ec1f75b 2019-03-26 stsp goto done;
5107 2ec1f75b 2019-03-26 stsp
5108 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5109 c9956ddf 2019-09-08 stsp NULL);
5110 2af4a041 2019-05-11 jcs if (error)
5111 2ec1f75b 2019-03-26 stsp goto done;
5112 2ec1f75b 2019-03-26 stsp
5113 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5114 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5115 2ec1f75b 2019-03-26 stsp if (error)
5116 2ec1f75b 2019-03-26 stsp goto done;
5117 2ec1f75b 2019-03-26 stsp
5118 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5119 6d022e97 2019-08-04 stsp if (error)
5120 6d022e97 2019-08-04 stsp goto done;
5121 17ed4618 2019-06-02 stsp
5122 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5123 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5124 f2a9dc41 2019-12-13 tracey struct stat sb;
5125 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5126 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5127 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5128 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5129 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5130 f2a9dc41 2019-12-13 tracey goto done;
5131 f2a9dc41 2019-12-13 tracey }
5132 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5133 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5134 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5135 f2a9dc41 2019-12-13 tracey continue;
5136 f2a9dc41 2019-12-13 tracey }
5137 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5138 f2a9dc41 2019-12-13 tracey ondisk_path);
5139 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5140 f2a9dc41 2019-12-13 tracey goto done;
5141 f2a9dc41 2019-12-13 tracey }
5142 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5143 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5144 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5145 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5146 f2a9dc41 2019-12-13 tracey goto done;
5147 f2a9dc41 2019-12-13 tracey }
5148 f2a9dc41 2019-12-13 tracey }
5149 f2a9dc41 2019-12-13 tracey }
5150 f2a9dc41 2019-12-13 tracey
5151 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5152 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5153 a129376b 2019-03-28 stsp done:
5154 a129376b 2019-03-28 stsp if (repo)
5155 a129376b 2019-03-28 stsp got_repo_close(repo);
5156 a129376b 2019-03-28 stsp if (worktree)
5157 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5158 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5159 17ed4618 2019-06-02 stsp free((char *)pe->path);
5160 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5161 a129376b 2019-03-28 stsp free(cwd);
5162 a129376b 2019-03-28 stsp return error;
5163 a129376b 2019-03-28 stsp }
5164 a129376b 2019-03-28 stsp
5165 a129376b 2019-03-28 stsp __dead static void
5166 a129376b 2019-03-28 stsp usage_revert(void)
5167 a129376b 2019-03-28 stsp {
5168 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5169 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5170 a129376b 2019-03-28 stsp exit(1);
5171 a129376b 2019-03-28 stsp }
5172 a129376b 2019-03-28 stsp
5173 1ee397ad 2019-07-12 stsp static const struct got_error *
5174 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5175 a129376b 2019-03-28 stsp {
5176 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5177 fb9704af 2020-01-27 stsp return NULL;
5178 fb9704af 2020-01-27 stsp
5179 a129376b 2019-03-28 stsp while (path[0] == '/')
5180 a129376b 2019-03-28 stsp path++;
5181 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5182 33aa809d 2019-08-08 stsp return NULL;
5183 33aa809d 2019-08-08 stsp }
5184 33aa809d 2019-08-08 stsp
5185 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5186 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5187 33aa809d 2019-08-08 stsp const char *action;
5188 33aa809d 2019-08-08 stsp };
5189 33aa809d 2019-08-08 stsp
5190 33aa809d 2019-08-08 stsp static const struct got_error *
5191 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5192 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5193 33aa809d 2019-08-08 stsp {
5194 33aa809d 2019-08-08 stsp char *line = NULL;
5195 33aa809d 2019-08-08 stsp size_t linesize = 0;
5196 33aa809d 2019-08-08 stsp ssize_t linelen;
5197 33aa809d 2019-08-08 stsp
5198 33aa809d 2019-08-08 stsp switch (status) {
5199 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5200 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5201 33aa809d 2019-08-08 stsp break;
5202 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5203 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5204 33aa809d 2019-08-08 stsp break;
5205 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5206 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5207 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5208 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5209 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5210 33aa809d 2019-08-08 stsp printf("%s", line);
5211 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5212 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5213 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5214 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5215 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5216 33aa809d 2019-08-08 stsp break;
5217 33aa809d 2019-08-08 stsp default:
5218 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5219 33aa809d 2019-08-08 stsp }
5220 33aa809d 2019-08-08 stsp
5221 33aa809d 2019-08-08 stsp return NULL;
5222 33aa809d 2019-08-08 stsp }
5223 33aa809d 2019-08-08 stsp
5224 33aa809d 2019-08-08 stsp static const struct got_error *
5225 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5226 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5227 33aa809d 2019-08-08 stsp {
5228 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5229 33aa809d 2019-08-08 stsp char *line = NULL;
5230 33aa809d 2019-08-08 stsp size_t linesize = 0;
5231 33aa809d 2019-08-08 stsp ssize_t linelen;
5232 33aa809d 2019-08-08 stsp int resp = ' ';
5233 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5234 33aa809d 2019-08-08 stsp
5235 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5236 33aa809d 2019-08-08 stsp
5237 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5238 33aa809d 2019-08-08 stsp char *nl;
5239 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5240 33aa809d 2019-08-08 stsp a->action);
5241 33aa809d 2019-08-08 stsp if (err)
5242 33aa809d 2019-08-08 stsp return err;
5243 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5244 33aa809d 2019-08-08 stsp if (linelen == -1) {
5245 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5246 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5247 33aa809d 2019-08-08 stsp return NULL;
5248 33aa809d 2019-08-08 stsp }
5249 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5250 33aa809d 2019-08-08 stsp if (nl)
5251 33aa809d 2019-08-08 stsp *nl = '\0';
5252 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5253 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5254 33aa809d 2019-08-08 stsp printf("y\n");
5255 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5256 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5257 33aa809d 2019-08-08 stsp printf("n\n");
5258 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5259 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5260 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5261 33aa809d 2019-08-08 stsp printf("q\n");
5262 33aa809d 2019-08-08 stsp } else
5263 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5264 33aa809d 2019-08-08 stsp free(line);
5265 33aa809d 2019-08-08 stsp return NULL;
5266 33aa809d 2019-08-08 stsp }
5267 33aa809d 2019-08-08 stsp
5268 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5269 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5270 33aa809d 2019-08-08 stsp a->action);
5271 33aa809d 2019-08-08 stsp if (err)
5272 33aa809d 2019-08-08 stsp return err;
5273 33aa809d 2019-08-08 stsp resp = getchar();
5274 33aa809d 2019-08-08 stsp if (resp == '\n')
5275 33aa809d 2019-08-08 stsp resp = getchar();
5276 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5277 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5278 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5279 33aa809d 2019-08-08 stsp resp = ' ';
5280 33aa809d 2019-08-08 stsp }
5281 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5282 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5283 33aa809d 2019-08-08 stsp resp = ' ';
5284 33aa809d 2019-08-08 stsp }
5285 33aa809d 2019-08-08 stsp }
5286 33aa809d 2019-08-08 stsp
5287 33aa809d 2019-08-08 stsp if (resp == 'y')
5288 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5289 33aa809d 2019-08-08 stsp else if (resp == 'n')
5290 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5291 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5292 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5293 33aa809d 2019-08-08 stsp
5294 1ee397ad 2019-07-12 stsp return NULL;
5295 a129376b 2019-03-28 stsp }
5296 a129376b 2019-03-28 stsp
5297 33aa809d 2019-08-08 stsp
5298 a129376b 2019-03-28 stsp static const struct got_error *
5299 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5300 a129376b 2019-03-28 stsp {
5301 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5302 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5303 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5304 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5305 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5306 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5307 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5308 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5309 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5310 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5311 a129376b 2019-03-28 stsp
5312 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5313 e20a8b6f 2019-06-04 stsp
5314 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5315 a129376b 2019-03-28 stsp switch (ch) {
5316 33aa809d 2019-08-08 stsp case 'p':
5317 33aa809d 2019-08-08 stsp pflag = 1;
5318 33aa809d 2019-08-08 stsp break;
5319 33aa809d 2019-08-08 stsp case 'F':
5320 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5321 33aa809d 2019-08-08 stsp break;
5322 0f6d7415 2019-08-08 stsp case 'R':
5323 0f6d7415 2019-08-08 stsp can_recurse = 1;
5324 0f6d7415 2019-08-08 stsp break;
5325 a129376b 2019-03-28 stsp default:
5326 a129376b 2019-03-28 stsp usage_revert();
5327 a129376b 2019-03-28 stsp /* NOTREACHED */
5328 a129376b 2019-03-28 stsp }
5329 a129376b 2019-03-28 stsp }
5330 a129376b 2019-03-28 stsp
5331 a129376b 2019-03-28 stsp argc -= optind;
5332 a129376b 2019-03-28 stsp argv += optind;
5333 a129376b 2019-03-28 stsp
5334 43012d58 2019-07-14 stsp #ifndef PROFILE
5335 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5336 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5337 43012d58 2019-07-14 stsp err(1, "pledge");
5338 43012d58 2019-07-14 stsp #endif
5339 e20a8b6f 2019-06-04 stsp if (argc < 1)
5340 a129376b 2019-03-28 stsp usage_revert();
5341 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5342 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5343 a129376b 2019-03-28 stsp
5344 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5345 a129376b 2019-03-28 stsp if (cwd == NULL) {
5346 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5347 a129376b 2019-03-28 stsp goto done;
5348 a129376b 2019-03-28 stsp }
5349 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5350 2ec1f75b 2019-03-26 stsp if (error)
5351 2ec1f75b 2019-03-26 stsp goto done;
5352 a129376b 2019-03-28 stsp
5353 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5354 c9956ddf 2019-09-08 stsp NULL);
5355 a129376b 2019-03-28 stsp if (error != NULL)
5356 a129376b 2019-03-28 stsp goto done;
5357 a129376b 2019-03-28 stsp
5358 33aa809d 2019-08-08 stsp if (patch_script_path) {
5359 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5360 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5361 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5362 33aa809d 2019-08-08 stsp patch_script_path);
5363 33aa809d 2019-08-08 stsp goto done;
5364 33aa809d 2019-08-08 stsp }
5365 33aa809d 2019-08-08 stsp }
5366 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5367 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5368 a129376b 2019-03-28 stsp if (error)
5369 a129376b 2019-03-28 stsp goto done;
5370 a129376b 2019-03-28 stsp
5371 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5372 6d022e97 2019-08-04 stsp if (error)
5373 6d022e97 2019-08-04 stsp goto done;
5374 00db391e 2019-08-03 stsp
5375 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5376 0f6d7415 2019-08-08 stsp char *ondisk_path;
5377 0f6d7415 2019-08-08 stsp struct stat sb;
5378 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5379 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5380 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5381 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5382 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5383 0f6d7415 2019-08-08 stsp goto done;
5384 0f6d7415 2019-08-08 stsp }
5385 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5386 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5387 0f6d7415 2019-08-08 stsp free(ondisk_path);
5388 0f6d7415 2019-08-08 stsp continue;
5389 0f6d7415 2019-08-08 stsp }
5390 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5391 0f6d7415 2019-08-08 stsp ondisk_path);
5392 0f6d7415 2019-08-08 stsp free(ondisk_path);
5393 0f6d7415 2019-08-08 stsp goto done;
5394 0f6d7415 2019-08-08 stsp }
5395 0f6d7415 2019-08-08 stsp free(ondisk_path);
5396 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5397 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5398 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5399 0f6d7415 2019-08-08 stsp goto done;
5400 0f6d7415 2019-08-08 stsp }
5401 0f6d7415 2019-08-08 stsp }
5402 0f6d7415 2019-08-08 stsp }
5403 0f6d7415 2019-08-08 stsp
5404 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5405 33aa809d 2019-08-08 stsp cpa.action = "revert";
5406 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5407 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5408 2ec1f75b 2019-03-26 stsp done:
5409 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5410 33aa809d 2019-08-08 stsp error == NULL)
5411 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5412 2ec1f75b 2019-03-26 stsp if (repo)
5413 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5414 2ec1f75b 2019-03-26 stsp if (worktree)
5415 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5416 2ec1f75b 2019-03-26 stsp free(path);
5417 d00136be 2019-03-26 stsp free(cwd);
5418 6bad629b 2019-02-04 stsp return error;
5419 c4296144 2019-05-09 stsp }
5420 c4296144 2019-05-09 stsp
5421 c4296144 2019-05-09 stsp __dead static void
5422 c4296144 2019-05-09 stsp usage_commit(void)
5423 c4296144 2019-05-09 stsp {
5424 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5425 5c1e53bc 2019-07-28 stsp getprogname());
5426 c4296144 2019-05-09 stsp exit(1);
5427 33ad4cbe 2019-05-12 jcs }
5428 33ad4cbe 2019-05-12 jcs
5429 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5430 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5431 e2ba3d07 2019-05-13 stsp const char *editor;
5432 e0870e44 2019-05-13 stsp const char *worktree_path;
5433 76d98825 2019-06-03 stsp const char *branch_name;
5434 314a6357 2019-05-13 stsp const char *repo_path;
5435 e0870e44 2019-05-13 stsp char *logmsg_path;
5436 e2ba3d07 2019-05-13 stsp
5437 e2ba3d07 2019-05-13 stsp };
5438 e0870e44 2019-05-13 stsp
5439 33ad4cbe 2019-05-12 jcs static const struct got_error *
5440 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5441 33ad4cbe 2019-05-12 jcs void *arg)
5442 33ad4cbe 2019-05-12 jcs {
5443 76d98825 2019-06-03 stsp char *initial_content = NULL;
5444 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5445 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5446 e0870e44 2019-05-13 stsp char *template = NULL;
5447 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5448 3ce1b845 2019-07-15 stsp int fd;
5449 33ad4cbe 2019-05-12 jcs size_t len;
5450 33ad4cbe 2019-05-12 jcs
5451 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5452 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5453 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5454 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5455 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5456 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5457 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5458 33ad4cbe 2019-05-12 jcs return NULL;
5459 33ad4cbe 2019-05-12 jcs }
5460 33ad4cbe 2019-05-12 jcs
5461 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5462 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5463 76d98825 2019-06-03 stsp
5464 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5465 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5466 76d98825 2019-06-03 stsp a->branch_name) == -1)
5467 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5468 e0870e44 2019-05-13 stsp
5469 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5470 793c30b5 2019-05-13 stsp if (err)
5471 e0870e44 2019-05-13 stsp goto done;
5472 33ad4cbe 2019-05-12 jcs
5473 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5474 33ad4cbe 2019-05-12 jcs
5475 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5476 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5477 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5478 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5479 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5480 33ad4cbe 2019-05-12 jcs }
5481 33ad4cbe 2019-05-12 jcs close(fd);
5482 33ad4cbe 2019-05-12 jcs
5483 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5484 33ad4cbe 2019-05-12 jcs done:
5485 76d98825 2019-06-03 stsp free(initial_content);
5486 e0870e44 2019-05-13 stsp free(template);
5487 314a6357 2019-05-13 stsp
5488 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5489 3ce1b845 2019-07-15 stsp if (err == NULL) {
5490 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5491 3ce1b845 2019-07-15 stsp if (err) {
5492 3ce1b845 2019-07-15 stsp free(*logmsg);
5493 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5494 3ce1b845 2019-07-15 stsp }
5495 3ce1b845 2019-07-15 stsp }
5496 33ad4cbe 2019-05-12 jcs return err;
5497 6bad629b 2019-02-04 stsp }
5498 c4296144 2019-05-09 stsp
5499 c4296144 2019-05-09 stsp static const struct got_error *
5500 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5501 c4296144 2019-05-09 stsp {
5502 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5503 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5504 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5505 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5506 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5507 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5508 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5509 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5510 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5511 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5512 5c1e53bc 2019-07-28 stsp
5513 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5514 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5515 c4296144 2019-05-09 stsp
5516 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5517 c4296144 2019-05-09 stsp switch (ch) {
5518 c4296144 2019-05-09 stsp case 'm':
5519 c4296144 2019-05-09 stsp logmsg = optarg;
5520 c4296144 2019-05-09 stsp break;
5521 c4296144 2019-05-09 stsp default:
5522 c4296144 2019-05-09 stsp usage_commit();
5523 c4296144 2019-05-09 stsp /* NOTREACHED */
5524 c4296144 2019-05-09 stsp }
5525 c4296144 2019-05-09 stsp }
5526 c4296144 2019-05-09 stsp
5527 c4296144 2019-05-09 stsp argc -= optind;
5528 c4296144 2019-05-09 stsp argv += optind;
5529 c4296144 2019-05-09 stsp
5530 43012d58 2019-07-14 stsp #ifndef PROFILE
5531 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5532 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5533 43012d58 2019-07-14 stsp err(1, "pledge");
5534 43012d58 2019-07-14 stsp #endif
5535 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5536 c4296144 2019-05-09 stsp if (cwd == NULL) {
5537 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5538 c4296144 2019-05-09 stsp goto done;
5539 c4296144 2019-05-09 stsp }
5540 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5541 7d5807f4 2019-07-11 stsp if (error)
5542 7d5807f4 2019-07-11 stsp goto done;
5543 7d5807f4 2019-07-11 stsp
5544 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5545 c4296144 2019-05-09 stsp if (error)
5546 a698f62e 2019-07-25 stsp goto done;
5547 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5548 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5549 c4296144 2019-05-09 stsp goto done;
5550 a698f62e 2019-07-25 stsp }
5551 5c1e53bc 2019-07-28 stsp
5552 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5553 916f288c 2019-07-30 stsp worktree);
5554 5c1e53bc 2019-07-28 stsp if (error)
5555 5c1e53bc 2019-07-28 stsp goto done;
5556 c4296144 2019-05-09 stsp
5557 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5558 c9956ddf 2019-09-08 stsp if (error)
5559 c9956ddf 2019-09-08 stsp goto done;
5560 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5561 c9956ddf 2019-09-08 stsp gitconfig_path);
5562 c4296144 2019-05-09 stsp if (error != NULL)
5563 c4296144 2019-05-09 stsp goto done;
5564 c4296144 2019-05-09 stsp
5565 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5566 aba9c984 2019-09-08 stsp if (error)
5567 aba9c984 2019-09-08 stsp return error;
5568 aba9c984 2019-09-08 stsp
5569 314a6357 2019-05-13 stsp /*
5570 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5571 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5572 314a6357 2019-05-13 stsp */
5573 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5574 314a6357 2019-05-13 stsp error = get_editor(&editor);
5575 314a6357 2019-05-13 stsp else
5576 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5577 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5578 c4296144 2019-05-09 stsp if (error)
5579 c4296144 2019-05-09 stsp goto done;
5580 c4296144 2019-05-09 stsp
5581 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5582 087fb88c 2019-08-04 stsp if (error)
5583 087fb88c 2019-08-04 stsp goto done;
5584 087fb88c 2019-08-04 stsp
5585 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5586 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5587 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5588 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5589 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5590 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5591 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5592 916f288c 2019-07-30 stsp goto done;
5593 916f288c 2019-07-30 stsp }
5594 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5595 916f288c 2019-07-30 stsp }
5596 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5597 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5598 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5599 e0870e44 2019-05-13 stsp if (error) {
5600 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5601 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5602 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5603 c4296144 2019-05-09 stsp goto done;
5604 e0870e44 2019-05-13 stsp }
5605 c4296144 2019-05-09 stsp
5606 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5607 c4296144 2019-05-09 stsp if (error)
5608 c4296144 2019-05-09 stsp goto done;
5609 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5610 c4296144 2019-05-09 stsp done:
5611 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5612 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5613 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5614 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5615 7266f21f 2019-10-21 stsp error == NULL)
5616 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5617 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5618 c4296144 2019-05-09 stsp if (repo)
5619 c4296144 2019-05-09 stsp got_repo_close(repo);
5620 c4296144 2019-05-09 stsp if (worktree)
5621 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5622 c4296144 2019-05-09 stsp free(cwd);
5623 c4296144 2019-05-09 stsp free(id_str);
5624 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5625 e2ba3d07 2019-05-13 stsp free(editor);
5626 aba9c984 2019-09-08 stsp free(author);
5627 234035bc 2019-06-01 stsp return error;
5628 234035bc 2019-06-01 stsp }
5629 234035bc 2019-06-01 stsp
5630 234035bc 2019-06-01 stsp __dead static void
5631 234035bc 2019-06-01 stsp usage_cherrypick(void)
5632 234035bc 2019-06-01 stsp {
5633 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5634 234035bc 2019-06-01 stsp exit(1);
5635 234035bc 2019-06-01 stsp }
5636 234035bc 2019-06-01 stsp
5637 234035bc 2019-06-01 stsp static const struct got_error *
5638 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5639 234035bc 2019-06-01 stsp {
5640 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5641 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5642 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5643 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5644 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5645 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5646 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5647 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5648 234035bc 2019-06-01 stsp int ch, did_something = 0;
5649 234035bc 2019-06-01 stsp
5650 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5651 234035bc 2019-06-01 stsp switch (ch) {
5652 234035bc 2019-06-01 stsp default:
5653 234035bc 2019-06-01 stsp usage_cherrypick();
5654 234035bc 2019-06-01 stsp /* NOTREACHED */
5655 234035bc 2019-06-01 stsp }
5656 234035bc 2019-06-01 stsp }
5657 234035bc 2019-06-01 stsp
5658 234035bc 2019-06-01 stsp argc -= optind;
5659 234035bc 2019-06-01 stsp argv += optind;
5660 234035bc 2019-06-01 stsp
5661 43012d58 2019-07-14 stsp #ifndef PROFILE
5662 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5663 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5664 43012d58 2019-07-14 stsp err(1, "pledge");
5665 43012d58 2019-07-14 stsp #endif
5666 234035bc 2019-06-01 stsp if (argc != 1)
5667 234035bc 2019-06-01 stsp usage_cherrypick();
5668 234035bc 2019-06-01 stsp
5669 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5670 234035bc 2019-06-01 stsp if (cwd == NULL) {
5671 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5672 234035bc 2019-06-01 stsp goto done;
5673 234035bc 2019-06-01 stsp }
5674 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5675 234035bc 2019-06-01 stsp if (error)
5676 234035bc 2019-06-01 stsp goto done;
5677 234035bc 2019-06-01 stsp
5678 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5679 c9956ddf 2019-09-08 stsp NULL);
5680 234035bc 2019-06-01 stsp if (error != NULL)
5681 234035bc 2019-06-01 stsp goto done;
5682 234035bc 2019-06-01 stsp
5683 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5684 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5685 234035bc 2019-06-01 stsp if (error)
5686 234035bc 2019-06-01 stsp goto done;
5687 234035bc 2019-06-01 stsp
5688 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5689 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5690 234035bc 2019-06-01 stsp if (error != NULL) {
5691 234035bc 2019-06-01 stsp struct got_reference *ref;
5692 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5693 234035bc 2019-06-01 stsp goto done;
5694 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5695 234035bc 2019-06-01 stsp if (error != NULL)
5696 234035bc 2019-06-01 stsp goto done;
5697 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5698 234035bc 2019-06-01 stsp got_ref_close(ref);
5699 234035bc 2019-06-01 stsp if (error != NULL)
5700 234035bc 2019-06-01 stsp goto done;
5701 234035bc 2019-06-01 stsp }
5702 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5703 234035bc 2019-06-01 stsp if (error)
5704 234035bc 2019-06-01 stsp goto done;
5705 234035bc 2019-06-01 stsp
5706 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5707 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5708 234035bc 2019-06-01 stsp if (error != NULL)
5709 234035bc 2019-06-01 stsp goto done;
5710 234035bc 2019-06-01 stsp
5711 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5712 234035bc 2019-06-01 stsp if (error) {
5713 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5714 234035bc 2019-06-01 stsp goto done;
5715 234035bc 2019-06-01 stsp error = NULL;
5716 234035bc 2019-06-01 stsp } else {
5717 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5718 234035bc 2019-06-01 stsp goto done;
5719 234035bc 2019-06-01 stsp }
5720 234035bc 2019-06-01 stsp
5721 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5722 234035bc 2019-06-01 stsp if (error)
5723 234035bc 2019-06-01 stsp goto done;
5724 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5725 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5726 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5727 03415a1a 2019-06-02 stsp NULL);
5728 234035bc 2019-06-01 stsp if (error != NULL)
5729 234035bc 2019-06-01 stsp goto done;
5730 234035bc 2019-06-01 stsp
5731 234035bc 2019-06-01 stsp if (did_something)
5732 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5733 234035bc 2019-06-01 stsp done:
5734 234035bc 2019-06-01 stsp if (commit)
5735 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5736 234035bc 2019-06-01 stsp free(commit_id_str);
5737 234035bc 2019-06-01 stsp if (head_ref)
5738 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5739 234035bc 2019-06-01 stsp if (worktree)
5740 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5741 234035bc 2019-06-01 stsp if (repo)
5742 234035bc 2019-06-01 stsp got_repo_close(repo);
5743 c4296144 2019-05-09 stsp return error;
5744 c4296144 2019-05-09 stsp }
5745 5ef14e63 2019-06-02 stsp
5746 5ef14e63 2019-06-02 stsp __dead static void
5747 5ef14e63 2019-06-02 stsp usage_backout(void)
5748 5ef14e63 2019-06-02 stsp {
5749 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5750 5ef14e63 2019-06-02 stsp exit(1);
5751 5ef14e63 2019-06-02 stsp }
5752 5ef14e63 2019-06-02 stsp
5753 5ef14e63 2019-06-02 stsp static const struct got_error *
5754 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5755 5ef14e63 2019-06-02 stsp {
5756 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5757 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5758 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5759 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5760 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5761 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5762 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5763 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5764 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5765 5ef14e63 2019-06-02 stsp
5766 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5767 5ef14e63 2019-06-02 stsp switch (ch) {
5768 5ef14e63 2019-06-02 stsp default:
5769 5ef14e63 2019-06-02 stsp usage_backout();
5770 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5771 5ef14e63 2019-06-02 stsp }
5772 5ef14e63 2019-06-02 stsp }
5773 5ef14e63 2019-06-02 stsp
5774 5ef14e63 2019-06-02 stsp argc -= optind;
5775 5ef14e63 2019-06-02 stsp argv += optind;
5776 5ef14e63 2019-06-02 stsp
5777 43012d58 2019-07-14 stsp #ifndef PROFILE
5778 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5779 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5780 43012d58 2019-07-14 stsp err(1, "pledge");
5781 43012d58 2019-07-14 stsp #endif
5782 5ef14e63 2019-06-02 stsp if (argc != 1)
5783 5ef14e63 2019-06-02 stsp usage_backout();
5784 5ef14e63 2019-06-02 stsp
5785 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5786 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5787 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5788 5ef14e63 2019-06-02 stsp goto done;
5789 5ef14e63 2019-06-02 stsp }
5790 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5791 5ef14e63 2019-06-02 stsp if (error)
5792 5ef14e63 2019-06-02 stsp goto done;
5793 5ef14e63 2019-06-02 stsp
5794 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5795 c9956ddf 2019-09-08 stsp NULL);
5796 5ef14e63 2019-06-02 stsp if (error != NULL)
5797 5ef14e63 2019-06-02 stsp goto done;
5798 5ef14e63 2019-06-02 stsp
5799 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5800 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5801 5ef14e63 2019-06-02 stsp if (error)
5802 5ef14e63 2019-06-02 stsp goto done;
5803 5ef14e63 2019-06-02 stsp
5804 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5805 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5806 5ef14e63 2019-06-02 stsp if (error != NULL) {
5807 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5808 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5809 5ef14e63 2019-06-02 stsp goto done;
5810 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5811 5ef14e63 2019-06-02 stsp if (error != NULL)
5812 5ef14e63 2019-06-02 stsp goto done;
5813 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5814 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5815 5ef14e63 2019-06-02 stsp if (error != NULL)
5816 5ef14e63 2019-06-02 stsp goto done;
5817 5ef14e63 2019-06-02 stsp }
5818 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5819 5ef14e63 2019-06-02 stsp if (error)
5820 5ef14e63 2019-06-02 stsp goto done;
5821 5ef14e63 2019-06-02 stsp
5822 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5823 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5824 5ef14e63 2019-06-02 stsp if (error != NULL)
5825 5ef14e63 2019-06-02 stsp goto done;
5826 5ef14e63 2019-06-02 stsp
5827 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5828 5ef14e63 2019-06-02 stsp if (error)
5829 5ef14e63 2019-06-02 stsp goto done;
5830 5ef14e63 2019-06-02 stsp
5831 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5832 5ef14e63 2019-06-02 stsp if (error)
5833 5ef14e63 2019-06-02 stsp goto done;
5834 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5835 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5836 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5837 5ef14e63 2019-06-02 stsp goto done;
5838 5ef14e63 2019-06-02 stsp }
5839 5ef14e63 2019-06-02 stsp
5840 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5841 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5842 5ef14e63 2019-06-02 stsp if (error != NULL)
5843 5ef14e63 2019-06-02 stsp goto done;
5844 5ef14e63 2019-06-02 stsp
5845 5ef14e63 2019-06-02 stsp if (did_something)
5846 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5847 5ef14e63 2019-06-02 stsp done:
5848 5ef14e63 2019-06-02 stsp if (commit)
5849 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5850 5ef14e63 2019-06-02 stsp free(commit_id_str);
5851 5ef14e63 2019-06-02 stsp if (head_ref)
5852 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5853 818c7501 2019-07-11 stsp if (worktree)
5854 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5855 818c7501 2019-07-11 stsp if (repo)
5856 818c7501 2019-07-11 stsp got_repo_close(repo);
5857 818c7501 2019-07-11 stsp return error;
5858 818c7501 2019-07-11 stsp }
5859 818c7501 2019-07-11 stsp
5860 818c7501 2019-07-11 stsp __dead static void
5861 818c7501 2019-07-11 stsp usage_rebase(void)
5862 818c7501 2019-07-11 stsp {
5863 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5864 af54c8f8 2019-07-11 stsp getprogname());
5865 818c7501 2019-07-11 stsp exit(1);
5866 0ebf8283 2019-07-24 stsp }
5867 0ebf8283 2019-07-24 stsp
5868 0ebf8283 2019-07-24 stsp void
5869 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5870 0ebf8283 2019-07-24 stsp {
5871 0ebf8283 2019-07-24 stsp char *nl;
5872 0ebf8283 2019-07-24 stsp size_t len;
5873 0ebf8283 2019-07-24 stsp
5874 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5875 0ebf8283 2019-07-24 stsp if (len > limit)
5876 0ebf8283 2019-07-24 stsp len = limit;
5877 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5878 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5879 0ebf8283 2019-07-24 stsp if (nl)
5880 0ebf8283 2019-07-24 stsp *nl = '\0';
5881 0ebf8283 2019-07-24 stsp }
5882 0ebf8283 2019-07-24 stsp
5883 0ebf8283 2019-07-24 stsp static const struct got_error *
5884 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5885 0ebf8283 2019-07-24 stsp {
5886 5943eee2 2019-08-13 stsp const struct got_error *err;
5887 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5888 5943eee2 2019-08-13 stsp const char *s;
5889 0ebf8283 2019-07-24 stsp
5890 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5891 5943eee2 2019-08-13 stsp if (err)
5892 5943eee2 2019-08-13 stsp return err;
5893 0ebf8283 2019-07-24 stsp
5894 5943eee2 2019-08-13 stsp s = logmsg0;
5895 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5896 5943eee2 2019-08-13 stsp s++;
5897 5943eee2 2019-08-13 stsp
5898 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5899 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5900 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5901 5943eee2 2019-08-13 stsp goto done;
5902 5943eee2 2019-08-13 stsp }
5903 0ebf8283 2019-07-24 stsp
5904 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5905 5943eee2 2019-08-13 stsp done:
5906 5943eee2 2019-08-13 stsp free(logmsg0);
5907 a0ea4fc0 2020-02-28 stsp return err;
5908 a0ea4fc0 2020-02-28 stsp }
5909 a0ea4fc0 2020-02-28 stsp
5910 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5911 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5912 a0ea4fc0 2020-02-28 stsp {
5913 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5914 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5915 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5916 a0ea4fc0 2020-02-28 stsp
5917 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5918 a0ea4fc0 2020-02-28 stsp if (err)
5919 a0ea4fc0 2020-02-28 stsp return err;
5920 a0ea4fc0 2020-02-28 stsp
5921 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5922 a0ea4fc0 2020-02-28 stsp if (err)
5923 a0ea4fc0 2020-02-28 stsp goto done;
5924 a0ea4fc0 2020-02-28 stsp
5925 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5926 a0ea4fc0 2020-02-28 stsp
5927 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5928 a0ea4fc0 2020-02-28 stsp if (err)
5929 a0ea4fc0 2020-02-28 stsp goto done;
5930 a0ea4fc0 2020-02-28 stsp
5931 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5932 a0ea4fc0 2020-02-28 stsp done:
5933 a0ea4fc0 2020-02-28 stsp free(id_str);
5934 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5935 a0ea4fc0 2020-02-28 stsp free(logmsg);
5936 5943eee2 2019-08-13 stsp return err;
5937 818c7501 2019-07-11 stsp }
5938 818c7501 2019-07-11 stsp
5939 818c7501 2019-07-11 stsp static const struct got_error *
5940 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5941 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5942 818c7501 2019-07-11 stsp {
5943 818c7501 2019-07-11 stsp const struct got_error *err;
5944 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5945 818c7501 2019-07-11 stsp
5946 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5947 818c7501 2019-07-11 stsp if (err)
5948 818c7501 2019-07-11 stsp goto done;
5949 818c7501 2019-07-11 stsp
5950 ff0d2220 2019-07-11 stsp if (new_id) {
5951 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5952 ff0d2220 2019-07-11 stsp if (err)
5953 ff0d2220 2019-07-11 stsp goto done;
5954 ff0d2220 2019-07-11 stsp }
5955 818c7501 2019-07-11 stsp
5956 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5957 ff0d2220 2019-07-11 stsp if (new_id_str)
5958 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5959 0ebf8283 2019-07-24 stsp
5960 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5961 0ebf8283 2019-07-24 stsp if (err)
5962 0ebf8283 2019-07-24 stsp goto done;
5963 0ebf8283 2019-07-24 stsp
5964 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5965 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5966 818c7501 2019-07-11 stsp done:
5967 818c7501 2019-07-11 stsp free(old_id_str);
5968 818c7501 2019-07-11 stsp free(new_id_str);
5969 272a1371 2020-02-28 stsp free(logmsg);
5970 818c7501 2019-07-11 stsp return err;
5971 818c7501 2019-07-11 stsp }
5972 818c7501 2019-07-11 stsp
5973 1ee397ad 2019-07-12 stsp static const struct got_error *
5974 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5975 818c7501 2019-07-11 stsp {
5976 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5977 818c7501 2019-07-11 stsp
5978 818c7501 2019-07-11 stsp while (path[0] == '/')
5979 818c7501 2019-07-11 stsp path++;
5980 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5981 818c7501 2019-07-11 stsp
5982 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5983 1ee397ad 2019-07-12 stsp return NULL;
5984 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5985 818c7501 2019-07-11 stsp *rebase_status = status;
5986 1ee397ad 2019-07-12 stsp return NULL;
5987 818c7501 2019-07-11 stsp }
5988 818c7501 2019-07-11 stsp
5989 818c7501 2019-07-11 stsp static const struct got_error *
5990 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5991 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5992 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5993 818c7501 2019-07-11 stsp {
5994 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5995 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5996 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5997 818c7501 2019-07-11 stsp }
5998 818c7501 2019-07-11 stsp
5999 818c7501 2019-07-11 stsp static const struct got_error *
6000 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6001 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6002 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6003 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6004 ff0d2220 2019-07-11 stsp {
6005 ff0d2220 2019-07-11 stsp const struct got_error *error;
6006 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6007 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6008 ff0d2220 2019-07-11 stsp
6009 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6010 ff0d2220 2019-07-11 stsp if (error)
6011 ff0d2220 2019-07-11 stsp return error;
6012 ff0d2220 2019-07-11 stsp
6013 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6014 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6015 ff0d2220 2019-07-11 stsp if (error) {
6016 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6017 ff0d2220 2019-07-11 stsp goto done;
6018 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6019 ff0d2220 2019-07-11 stsp } else {
6020 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6021 ff0d2220 2019-07-11 stsp free(new_commit_id);
6022 ff0d2220 2019-07-11 stsp }
6023 ff0d2220 2019-07-11 stsp done:
6024 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6025 ff0d2220 2019-07-11 stsp return error;
6026 64c6d990 2019-07-11 stsp }
6027 64c6d990 2019-07-11 stsp
6028 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6029 64c6d990 2019-07-11 stsp const char *path_prefix;
6030 64c6d990 2019-07-11 stsp size_t len;
6031 8ca9bd68 2019-07-25 stsp int errcode;
6032 64c6d990 2019-07-11 stsp };
6033 64c6d990 2019-07-11 stsp
6034 64c6d990 2019-07-11 stsp static const struct got_error *
6035 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6036 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6037 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6038 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6039 64c6d990 2019-07-11 stsp {
6040 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6041 64c6d990 2019-07-11 stsp
6042 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6043 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6044 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6045 64c6d990 2019-07-11 stsp
6046 64c6d990 2019-07-11 stsp return NULL;
6047 64c6d990 2019-07-11 stsp }
6048 64c6d990 2019-07-11 stsp
6049 64c6d990 2019-07-11 stsp static const struct got_error *
6050 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6051 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6052 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6053 64c6d990 2019-07-11 stsp {
6054 64c6d990 2019-07-11 stsp const struct got_error *err;
6055 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6056 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6057 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6058 64c6d990 2019-07-11 stsp
6059 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6060 64c6d990 2019-07-11 stsp return NULL;
6061 64c6d990 2019-07-11 stsp
6062 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6063 64c6d990 2019-07-11 stsp if (err)
6064 64c6d990 2019-07-11 stsp goto done;
6065 64c6d990 2019-07-11 stsp
6066 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6067 64c6d990 2019-07-11 stsp if (err)
6068 64c6d990 2019-07-11 stsp goto done;
6069 64c6d990 2019-07-11 stsp
6070 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6071 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6072 64c6d990 2019-07-11 stsp if (err)
6073 64c6d990 2019-07-11 stsp goto done;
6074 64c6d990 2019-07-11 stsp
6075 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6076 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6077 64c6d990 2019-07-11 stsp if (err)
6078 64c6d990 2019-07-11 stsp goto done;
6079 64c6d990 2019-07-11 stsp
6080 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6081 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6082 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6083 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6084 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6085 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6086 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6087 64c6d990 2019-07-11 stsp done:
6088 64c6d990 2019-07-11 stsp if (tree1)
6089 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6090 64c6d990 2019-07-11 stsp if (tree2)
6091 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6092 64c6d990 2019-07-11 stsp if (commit)
6093 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6094 64c6d990 2019-07-11 stsp if (parent_commit)
6095 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6096 64c6d990 2019-07-11 stsp return err;
6097 ff0d2220 2019-07-11 stsp }
6098 ff0d2220 2019-07-11 stsp
6099 ff0d2220 2019-07-11 stsp static const struct got_error *
6100 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6101 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6102 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6103 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6104 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6105 af61c510 2019-07-19 stsp {
6106 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6107 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6108 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6109 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6110 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6111 af61c510 2019-07-19 stsp
6112 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6113 af61c510 2019-07-19 stsp if (err)
6114 af61c510 2019-07-19 stsp return err;
6115 af61c510 2019-07-19 stsp
6116 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6117 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6118 af61c510 2019-07-19 stsp if (err)
6119 af61c510 2019-07-19 stsp goto done;
6120 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6121 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6122 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6123 af61c510 2019-07-19 stsp if (err) {
6124 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6125 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6126 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6127 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6128 af61c510 2019-07-19 stsp "been reached?!?");
6129 ee780d5c 2020-01-04 stsp }
6130 ee780d5c 2020-01-04 stsp goto done;
6131 af61c510 2019-07-19 stsp } else {
6132 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6133 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6134 af61c510 2019-07-19 stsp if (err)
6135 af61c510 2019-07-19 stsp goto done;
6136 af61c510 2019-07-19 stsp
6137 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6138 af61c510 2019-07-19 stsp if (err)
6139 af61c510 2019-07-19 stsp goto done;
6140 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6141 af61c510 2019-07-19 stsp commit_id = parent_id;
6142 af61c510 2019-07-19 stsp }
6143 af61c510 2019-07-19 stsp }
6144 af61c510 2019-07-19 stsp done:
6145 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6146 af61c510 2019-07-19 stsp return err;
6147 af61c510 2019-07-19 stsp }
6148 af61c510 2019-07-19 stsp
6149 af61c510 2019-07-19 stsp static const struct got_error *
6150 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6151 818c7501 2019-07-11 stsp {
6152 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6153 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6154 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6155 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6156 818c7501 2019-07-11 stsp char *cwd = NULL;
6157 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6158 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6159 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6160 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6161 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6162 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6163 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6164 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6165 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6166 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6167 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6168 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6169 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6170 818c7501 2019-07-11 stsp
6171 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6172 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6173 818c7501 2019-07-11 stsp
6174 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6175 818c7501 2019-07-11 stsp switch (ch) {
6176 818c7501 2019-07-11 stsp case 'a':
6177 818c7501 2019-07-11 stsp abort_rebase = 1;
6178 818c7501 2019-07-11 stsp break;
6179 818c7501 2019-07-11 stsp case 'c':
6180 818c7501 2019-07-11 stsp continue_rebase = 1;
6181 818c7501 2019-07-11 stsp break;
6182 818c7501 2019-07-11 stsp default:
6183 818c7501 2019-07-11 stsp usage_rebase();
6184 818c7501 2019-07-11 stsp /* NOTREACHED */
6185 818c7501 2019-07-11 stsp }
6186 818c7501 2019-07-11 stsp }
6187 818c7501 2019-07-11 stsp
6188 818c7501 2019-07-11 stsp argc -= optind;
6189 818c7501 2019-07-11 stsp argv += optind;
6190 818c7501 2019-07-11 stsp
6191 43012d58 2019-07-14 stsp #ifndef PROFILE
6192 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6193 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6194 43012d58 2019-07-14 stsp err(1, "pledge");
6195 43012d58 2019-07-14 stsp #endif
6196 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6197 818c7501 2019-07-11 stsp usage_rebase();
6198 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6199 818c7501 2019-07-11 stsp if (argc != 0)
6200 818c7501 2019-07-11 stsp usage_rebase();
6201 818c7501 2019-07-11 stsp } else if (argc != 1)
6202 818c7501 2019-07-11 stsp usage_rebase();
6203 818c7501 2019-07-11 stsp
6204 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6205 818c7501 2019-07-11 stsp if (cwd == NULL) {
6206 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6207 818c7501 2019-07-11 stsp goto done;
6208 818c7501 2019-07-11 stsp }
6209 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6210 818c7501 2019-07-11 stsp if (error)
6211 818c7501 2019-07-11 stsp goto done;
6212 818c7501 2019-07-11 stsp
6213 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6214 c9956ddf 2019-09-08 stsp NULL);
6215 818c7501 2019-07-11 stsp if (error != NULL)
6216 818c7501 2019-07-11 stsp goto done;
6217 818c7501 2019-07-11 stsp
6218 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6219 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6220 7ef62c4e 2020-02-24 stsp if (error)
6221 7ef62c4e 2020-02-24 stsp goto done;
6222 7ef62c4e 2020-02-24 stsp
6223 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6224 7ef62c4e 2020-02-24 stsp worktree);
6225 818c7501 2019-07-11 stsp if (error)
6226 7ef62c4e 2020-02-24 stsp goto done;
6227 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6228 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6229 818c7501 2019-07-11 stsp goto done;
6230 7ef62c4e 2020-02-24 stsp }
6231 818c7501 2019-07-11 stsp
6232 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6233 818c7501 2019-07-11 stsp if (error)
6234 818c7501 2019-07-11 stsp goto done;
6235 818c7501 2019-07-11 stsp
6236 f6794adc 2019-07-23 stsp if (abort_rebase) {
6237 818c7501 2019-07-11 stsp int did_something;
6238 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6239 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6240 f6794adc 2019-07-23 stsp goto done;
6241 f6794adc 2019-07-23 stsp }
6242 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6243 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6244 3e3a69f1 2019-07-25 stsp worktree, repo);
6245 818c7501 2019-07-11 stsp if (error)
6246 818c7501 2019-07-11 stsp goto done;
6247 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6248 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6249 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6250 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6251 818c7501 2019-07-11 stsp if (error)
6252 818c7501 2019-07-11 stsp goto done;
6253 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6254 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6255 818c7501 2019-07-11 stsp }
6256 818c7501 2019-07-11 stsp
6257 818c7501 2019-07-11 stsp if (continue_rebase) {
6258 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6259 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6260 f6794adc 2019-07-23 stsp goto done;
6261 f6794adc 2019-07-23 stsp }
6262 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6263 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6264 3e3a69f1 2019-07-25 stsp worktree, repo);
6265 818c7501 2019-07-11 stsp if (error)
6266 818c7501 2019-07-11 stsp goto done;
6267 818c7501 2019-07-11 stsp
6268 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6269 01757395 2019-07-12 stsp resume_commit_id, repo);
6270 818c7501 2019-07-11 stsp if (error)
6271 818c7501 2019-07-11 stsp goto done;
6272 818c7501 2019-07-11 stsp
6273 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6274 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6275 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6276 818c7501 2019-07-11 stsp goto done;
6277 818c7501 2019-07-11 stsp }
6278 818c7501 2019-07-11 stsp } else {
6279 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6280 818c7501 2019-07-11 stsp if (error != NULL)
6281 818c7501 2019-07-11 stsp goto done;
6282 ff0d2220 2019-07-11 stsp }
6283 818c7501 2019-07-11 stsp
6284 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6285 ff0d2220 2019-07-11 stsp if (error)
6286 ff0d2220 2019-07-11 stsp goto done;
6287 ff0d2220 2019-07-11 stsp
6288 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6289 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6290 a51a74b3 2019-07-27 stsp
6291 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6292 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6293 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6294 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6295 818c7501 2019-07-11 stsp if (error)
6296 818c7501 2019-07-11 stsp goto done;
6297 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6298 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6299 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6300 818c7501 2019-07-11 stsp "with work tree's branch");
6301 818c7501 2019-07-11 stsp goto done;
6302 818c7501 2019-07-11 stsp }
6303 818c7501 2019-07-11 stsp
6304 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6305 a51a74b3 2019-07-27 stsp if (error) {
6306 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6307 a51a74b3 2019-07-27 stsp goto done;
6308 a51a74b3 2019-07-27 stsp error = NULL;
6309 a51a74b3 2019-07-27 stsp } else {
6310 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6311 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6312 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6313 a51a74b3 2019-07-27 stsp goto done;
6314 a51a74b3 2019-07-27 stsp }
6315 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6316 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6317 818c7501 2019-07-11 stsp if (error)
6318 818c7501 2019-07-11 stsp goto done;
6319 818c7501 2019-07-11 stsp }
6320 818c7501 2019-07-11 stsp
6321 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6322 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6323 818c7501 2019-07-11 stsp if (error)
6324 818c7501 2019-07-11 stsp goto done;
6325 818c7501 2019-07-11 stsp
6326 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6327 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6328 fc66b545 2019-08-12 stsp if (pid == NULL) {
6329 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6330 fc66b545 2019-08-12 stsp int did_something;
6331 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6332 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6333 fc66b545 2019-08-12 stsp &did_something);
6334 fc66b545 2019-08-12 stsp if (error)
6335 fc66b545 2019-08-12 stsp goto done;
6336 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6337 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6338 fc66b545 2019-08-12 stsp }
6339 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6340 fc66b545 2019-08-12 stsp goto done;
6341 fc66b545 2019-08-12 stsp }
6342 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6343 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6344 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6345 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6346 818c7501 2019-07-11 stsp commit = NULL;
6347 818c7501 2019-07-11 stsp if (error)
6348 818c7501 2019-07-11 stsp goto done;
6349 64c6d990 2019-07-11 stsp
6350 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6351 38b0338b 2019-11-29 stsp if (continue_rebase) {
6352 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6353 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6354 38b0338b 2019-11-29 stsp goto done;
6355 38b0338b 2019-11-29 stsp } else {
6356 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6357 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6358 38b0338b 2019-11-29 stsp char *id_str;
6359 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6360 38b0338b 2019-11-29 stsp new_base_branch);
6361 38b0338b 2019-11-29 stsp if (error)
6362 38b0338b 2019-11-29 stsp goto done;
6363 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6364 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6365 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6366 38b0338b 2019-11-29 stsp free(id_str);
6367 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6368 38b0338b 2019-11-29 stsp new_head_commit_id);
6369 38b0338b 2019-11-29 stsp if (error)
6370 38b0338b 2019-11-29 stsp goto done;
6371 38b0338b 2019-11-29 stsp }
6372 818c7501 2019-07-11 stsp }
6373 818c7501 2019-07-11 stsp
6374 818c7501 2019-07-11 stsp pid = NULL;
6375 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6376 818c7501 2019-07-11 stsp commit_id = qid->id;
6377 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6378 818c7501 2019-07-11 stsp pid = qid;
6379 818c7501 2019-07-11 stsp
6380 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6381 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6382 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6383 818c7501 2019-07-11 stsp if (error)
6384 818c7501 2019-07-11 stsp goto done;
6385 818c7501 2019-07-11 stsp
6386 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6387 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6388 a0ea4fc0 2020-02-28 stsp if (error)
6389 a0ea4fc0 2020-02-28 stsp goto done;
6390 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6391 818c7501 2019-07-11 stsp break;
6392 01757395 2019-07-12 stsp }
6393 818c7501 2019-07-11 stsp
6394 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6395 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6396 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6397 818c7501 2019-07-11 stsp if (error)
6398 818c7501 2019-07-11 stsp goto done;
6399 818c7501 2019-07-11 stsp }
6400 818c7501 2019-07-11 stsp
6401 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6402 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6403 818c7501 2019-07-11 stsp if (error)
6404 818c7501 2019-07-11 stsp goto done;
6405 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6406 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6407 818c7501 2019-07-11 stsp } else
6408 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6409 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6410 818c7501 2019-07-11 stsp done:
6411 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6412 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6413 818c7501 2019-07-11 stsp free(resume_commit_id);
6414 818c7501 2019-07-11 stsp free(yca_id);
6415 818c7501 2019-07-11 stsp if (commit)
6416 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6417 818c7501 2019-07-11 stsp if (branch)
6418 818c7501 2019-07-11 stsp got_ref_close(branch);
6419 818c7501 2019-07-11 stsp if (new_base_branch)
6420 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6421 818c7501 2019-07-11 stsp if (tmp_branch)
6422 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6423 5ef14e63 2019-06-02 stsp if (worktree)
6424 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6425 5ef14e63 2019-06-02 stsp if (repo)
6426 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6427 5ef14e63 2019-06-02 stsp return error;
6428 0ebf8283 2019-07-24 stsp }
6429 0ebf8283 2019-07-24 stsp
6430 0ebf8283 2019-07-24 stsp __dead static void
6431 0ebf8283 2019-07-24 stsp usage_histedit(void)
6432 0ebf8283 2019-07-24 stsp {
6433 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6434 0ebf8283 2019-07-24 stsp getprogname());
6435 0ebf8283 2019-07-24 stsp exit(1);
6436 0ebf8283 2019-07-24 stsp }
6437 0ebf8283 2019-07-24 stsp
6438 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6439 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6440 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6441 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6442 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6443 0ebf8283 2019-07-24 stsp
6444 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6445 0ebf8283 2019-07-24 stsp unsigned char code;
6446 0ebf8283 2019-07-24 stsp const char *name;
6447 0ebf8283 2019-07-24 stsp const char *desc;
6448 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6449 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6450 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6451 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6452 82997472 2020-01-29 stsp "be used" },
6453 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6454 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6455 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6456 0ebf8283 2019-07-24 stsp };
6457 0ebf8283 2019-07-24 stsp
6458 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6459 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6460 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6461 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6462 0ebf8283 2019-07-24 stsp char *logmsg;
6463 0ebf8283 2019-07-24 stsp };
6464 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6465 0ebf8283 2019-07-24 stsp
6466 0ebf8283 2019-07-24 stsp static const struct got_error *
6467 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6468 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6469 0ebf8283 2019-07-24 stsp {
6470 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6471 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6472 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6473 8138f3e1 2019-08-11 stsp int n;
6474 0ebf8283 2019-07-24 stsp
6475 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6476 0ebf8283 2019-07-24 stsp if (err)
6477 0ebf8283 2019-07-24 stsp goto done;
6478 0ebf8283 2019-07-24 stsp
6479 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6480 0ebf8283 2019-07-24 stsp if (err)
6481 0ebf8283 2019-07-24 stsp goto done;
6482 0ebf8283 2019-07-24 stsp
6483 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6484 0ebf8283 2019-07-24 stsp if (err)
6485 0ebf8283 2019-07-24 stsp goto done;
6486 0ebf8283 2019-07-24 stsp
6487 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6488 0ebf8283 2019-07-24 stsp if (n < 0)
6489 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6490 0ebf8283 2019-07-24 stsp done:
6491 0ebf8283 2019-07-24 stsp if (commit)
6492 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6493 0ebf8283 2019-07-24 stsp free(id_str);
6494 0ebf8283 2019-07-24 stsp free(logmsg);
6495 0ebf8283 2019-07-24 stsp return err;
6496 0ebf8283 2019-07-24 stsp }
6497 0ebf8283 2019-07-24 stsp
6498 0ebf8283 2019-07-24 stsp static const struct got_error *
6499 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6500 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6501 0ebf8283 2019-07-24 stsp {
6502 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6503 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6504 0ebf8283 2019-07-24 stsp
6505 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6506 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6507 0ebf8283 2019-07-24 stsp
6508 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6509 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6510 0ebf8283 2019-07-24 stsp f, repo);
6511 0ebf8283 2019-07-24 stsp if (err)
6512 0ebf8283 2019-07-24 stsp break;
6513 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6514 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6515 083957f4 2020-02-24 stsp if (n < 0) {
6516 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6517 083957f4 2020-02-24 stsp break;
6518 083957f4 2020-02-24 stsp }
6519 083957f4 2020-02-24 stsp }
6520 0ebf8283 2019-07-24 stsp }
6521 0ebf8283 2019-07-24 stsp
6522 0ebf8283 2019-07-24 stsp return err;
6523 0ebf8283 2019-07-24 stsp }
6524 0ebf8283 2019-07-24 stsp
6525 0ebf8283 2019-07-24 stsp static const struct got_error *
6526 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6527 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6528 0ebf8283 2019-07-24 stsp {
6529 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6530 0ebf8283 2019-07-24 stsp int n, i;
6531 514f2ffe 2020-01-29 stsp char *id_str;
6532 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6533 514f2ffe 2020-01-29 stsp
6534 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6535 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6536 514f2ffe 2020-01-29 stsp if (err)
6537 514f2ffe 2020-01-29 stsp return err;
6538 514f2ffe 2020-01-29 stsp
6539 514f2ffe 2020-01-29 stsp n = fprintf(f,
6540 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6541 514f2ffe 2020-01-29 stsp "# commit %s\n"
6542 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6543 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6544 514f2ffe 2020-01-29 stsp if (n < 0) {
6545 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6546 514f2ffe 2020-01-29 stsp goto done;
6547 514f2ffe 2020-01-29 stsp }
6548 0ebf8283 2019-07-24 stsp
6549 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6550 514f2ffe 2020-01-29 stsp if (n < 0) {
6551 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6552 514f2ffe 2020-01-29 stsp goto done;
6553 514f2ffe 2020-01-29 stsp }
6554 0ebf8283 2019-07-24 stsp
6555 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6556 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6557 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6558 0ebf8283 2019-07-24 stsp cmd->desc);
6559 0ebf8283 2019-07-24 stsp if (n < 0) {
6560 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6561 0ebf8283 2019-07-24 stsp break;
6562 0ebf8283 2019-07-24 stsp }
6563 0ebf8283 2019-07-24 stsp }
6564 514f2ffe 2020-01-29 stsp done:
6565 514f2ffe 2020-01-29 stsp free(id_str);
6566 0ebf8283 2019-07-24 stsp return err;
6567 0ebf8283 2019-07-24 stsp }
6568 0ebf8283 2019-07-24 stsp
6569 0ebf8283 2019-07-24 stsp static const struct got_error *
6570 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6571 0ebf8283 2019-07-24 stsp {
6572 0ebf8283 2019-07-24 stsp static char msg[42];
6573 0ebf8283 2019-07-24 stsp int ret;
6574 0ebf8283 2019-07-24 stsp
6575 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6576 0ebf8283 2019-07-24 stsp lineno);
6577 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6578 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6579 0ebf8283 2019-07-24 stsp
6580 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6581 0ebf8283 2019-07-24 stsp }
6582 0ebf8283 2019-07-24 stsp
6583 0ebf8283 2019-07-24 stsp static const struct got_error *
6584 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6585 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6586 0ebf8283 2019-07-24 stsp {
6587 0ebf8283 2019-07-24 stsp const struct got_error *err;
6588 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6589 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6590 0ebf8283 2019-07-24 stsp
6591 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6592 0ebf8283 2019-07-24 stsp if (err)
6593 0ebf8283 2019-07-24 stsp return err;
6594 0ebf8283 2019-07-24 stsp
6595 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6596 0ebf8283 2019-07-24 stsp if (err)
6597 0ebf8283 2019-07-24 stsp goto done;
6598 0ebf8283 2019-07-24 stsp
6599 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6600 5943eee2 2019-08-13 stsp if (err)
6601 5943eee2 2019-08-13 stsp goto done;
6602 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6603 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6604 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6605 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6606 0ebf8283 2019-07-24 stsp }
6607 0ebf8283 2019-07-24 stsp done:
6608 0ebf8283 2019-07-24 stsp if (folded_commit)
6609 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6610 0ebf8283 2019-07-24 stsp free(id_str);
6611 5943eee2 2019-08-13 stsp free(folded_logmsg);
6612 0ebf8283 2019-07-24 stsp return err;
6613 0ebf8283 2019-07-24 stsp }
6614 0ebf8283 2019-07-24 stsp
6615 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6616 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6617 0ebf8283 2019-07-24 stsp {
6618 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6619 0ebf8283 2019-07-24 stsp
6620 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6621 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6622 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6623 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6624 3f9de99f 2019-07-24 stsp folded = prev;
6625 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6626 0ebf8283 2019-07-24 stsp }
6627 0ebf8283 2019-07-24 stsp
6628 0ebf8283 2019-07-24 stsp return folded;
6629 0ebf8283 2019-07-24 stsp }
6630 0ebf8283 2019-07-24 stsp
6631 0ebf8283 2019-07-24 stsp static const struct got_error *
6632 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6633 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6634 0ebf8283 2019-07-24 stsp {
6635 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6636 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6637 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6638 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6639 0ebf8283 2019-07-24 stsp int fd;
6640 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6641 0ebf8283 2019-07-24 stsp
6642 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6643 0ebf8283 2019-07-24 stsp if (err)
6644 0ebf8283 2019-07-24 stsp return err;
6645 0ebf8283 2019-07-24 stsp
6646 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6647 0ebf8283 2019-07-24 stsp if (folded) {
6648 0ebf8283 2019-07-24 stsp while (folded != hle) {
6649 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6650 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6651 3f9de99f 2019-07-24 stsp continue;
6652 3f9de99f 2019-07-24 stsp }
6653 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6654 0ebf8283 2019-07-24 stsp logmsg, repo);
6655 0ebf8283 2019-07-24 stsp if (err)
6656 0ebf8283 2019-07-24 stsp goto done;
6657 0ebf8283 2019-07-24 stsp free(logmsg);
6658 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6659 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6660 0ebf8283 2019-07-24 stsp }
6661 0ebf8283 2019-07-24 stsp }
6662 0ebf8283 2019-07-24 stsp
6663 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6664 0ebf8283 2019-07-24 stsp if (err)
6665 0ebf8283 2019-07-24 stsp goto done;
6666 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6667 5943eee2 2019-08-13 stsp if (err)
6668 5943eee2 2019-08-13 stsp goto done;
6669 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6670 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6671 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6672 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6673 0ebf8283 2019-07-24 stsp goto done;
6674 0ebf8283 2019-07-24 stsp }
6675 0ebf8283 2019-07-24 stsp free(logmsg);
6676 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6677 0ebf8283 2019-07-24 stsp
6678 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6679 0ebf8283 2019-07-24 stsp if (err)
6680 0ebf8283 2019-07-24 stsp goto done;
6681 0ebf8283 2019-07-24 stsp
6682 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6683 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6684 0ebf8283 2019-07-24 stsp if (err)
6685 0ebf8283 2019-07-24 stsp goto done;
6686 0ebf8283 2019-07-24 stsp
6687 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6688 0ebf8283 2019-07-24 stsp close(fd);
6689 0ebf8283 2019-07-24 stsp
6690 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6691 0ebf8283 2019-07-24 stsp if (err)
6692 0ebf8283 2019-07-24 stsp goto done;
6693 0ebf8283 2019-07-24 stsp
6694 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6695 0ebf8283 2019-07-24 stsp if (err) {
6696 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6697 0ebf8283 2019-07-24 stsp goto done;
6698 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6699 0ebf8283 2019-07-24 stsp }
6700 0ebf8283 2019-07-24 stsp done:
6701 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6702 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6703 0ebf8283 2019-07-24 stsp free(logmsg_path);
6704 0ebf8283 2019-07-24 stsp free(logmsg);
6705 5943eee2 2019-08-13 stsp free(orig_logmsg);
6706 0ebf8283 2019-07-24 stsp free(editor);
6707 0ebf8283 2019-07-24 stsp if (commit)
6708 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6709 0ebf8283 2019-07-24 stsp return err;
6710 5ef14e63 2019-06-02 stsp }
6711 0ebf8283 2019-07-24 stsp
6712 0ebf8283 2019-07-24 stsp static const struct got_error *
6713 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6714 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6715 0ebf8283 2019-07-24 stsp {
6716 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6717 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6718 0ebf8283 2019-07-24 stsp size_t size;
6719 0ebf8283 2019-07-24 stsp ssize_t len;
6720 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6721 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6722 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6723 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6724 0ebf8283 2019-07-24 stsp
6725 0ebf8283 2019-07-24 stsp for (;;) {
6726 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6727 0ebf8283 2019-07-24 stsp if (len == -1) {
6728 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6729 0ebf8283 2019-07-24 stsp if (feof(f))
6730 0ebf8283 2019-07-24 stsp break;
6731 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6732 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6733 0ebf8283 2019-07-24 stsp break;
6734 0ebf8283 2019-07-24 stsp }
6735 0ebf8283 2019-07-24 stsp lineno++;
6736 0ebf8283 2019-07-24 stsp p = line;
6737 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6738 0ebf8283 2019-07-24 stsp p++;
6739 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6740 0ebf8283 2019-07-24 stsp free(line);
6741 0ebf8283 2019-07-24 stsp line = NULL;
6742 0ebf8283 2019-07-24 stsp continue;
6743 0ebf8283 2019-07-24 stsp }
6744 0ebf8283 2019-07-24 stsp cmd = NULL;
6745 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6746 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6747 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6748 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6749 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6750 0ebf8283 2019-07-24 stsp break;
6751 0ebf8283 2019-07-24 stsp }
6752 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6753 0ebf8283 2019-07-24 stsp p++;
6754 0ebf8283 2019-07-24 stsp break;
6755 0ebf8283 2019-07-24 stsp }
6756 0ebf8283 2019-07-24 stsp }
6757 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6758 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6759 0ebf8283 2019-07-24 stsp break;
6760 0ebf8283 2019-07-24 stsp }
6761 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6762 0ebf8283 2019-07-24 stsp p++;
6763 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6764 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6765 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6766 0ebf8283 2019-07-24 stsp break;
6767 0ebf8283 2019-07-24 stsp }
6768 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6769 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6770 0ebf8283 2019-07-24 stsp if (err)
6771 0ebf8283 2019-07-24 stsp break;
6772 0ebf8283 2019-07-24 stsp } else {
6773 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6774 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6775 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6776 0ebf8283 2019-07-24 stsp break;
6777 0ebf8283 2019-07-24 stsp }
6778 0ebf8283 2019-07-24 stsp }
6779 0ebf8283 2019-07-24 stsp free(line);
6780 0ebf8283 2019-07-24 stsp line = NULL;
6781 0ebf8283 2019-07-24 stsp continue;
6782 0ebf8283 2019-07-24 stsp } else {
6783 0ebf8283 2019-07-24 stsp end = p;
6784 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6785 0ebf8283 2019-07-24 stsp end++;
6786 0ebf8283 2019-07-24 stsp *end = '\0';
6787 0ebf8283 2019-07-24 stsp
6788 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6789 0ebf8283 2019-07-24 stsp if (err) {
6790 0ebf8283 2019-07-24 stsp /* override error code */
6791 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6792 0ebf8283 2019-07-24 stsp break;
6793 0ebf8283 2019-07-24 stsp }
6794 0ebf8283 2019-07-24 stsp }
6795 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6796 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6797 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6798 0ebf8283 2019-07-24 stsp break;
6799 0ebf8283 2019-07-24 stsp }
6800 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6801 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6802 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6803 0ebf8283 2019-07-24 stsp commit_id = NULL;
6804 0ebf8283 2019-07-24 stsp free(line);
6805 0ebf8283 2019-07-24 stsp line = NULL;
6806 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6807 0ebf8283 2019-07-24 stsp }
6808 0ebf8283 2019-07-24 stsp
6809 0ebf8283 2019-07-24 stsp free(line);
6810 0ebf8283 2019-07-24 stsp free(commit_id);
6811 0ebf8283 2019-07-24 stsp return err;
6812 0ebf8283 2019-07-24 stsp }
6813 0ebf8283 2019-07-24 stsp
6814 0ebf8283 2019-07-24 stsp static const struct got_error *
6815 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6816 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6817 bfce7f83 2019-07-27 stsp {
6818 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6819 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6820 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6821 5b87815e 2020-03-05 stsp static char msg[92];
6822 bfce7f83 2019-07-27 stsp char *id_str;
6823 bfce7f83 2019-07-27 stsp
6824 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6825 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6826 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6827 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6828 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6829 5b87815e 2020-03-05 stsp
6830 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6831 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6832 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6833 5b87815e 2020-03-05 stsp if (hle == hle2)
6834 5b87815e 2020-03-05 stsp continue;
6835 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6836 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6837 5b87815e 2020-03-05 stsp continue;
6838 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6839 5b87815e 2020-03-05 stsp if (err)
6840 5b87815e 2020-03-05 stsp return err;
6841 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6842 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6843 5b87815e 2020-03-05 stsp free(id_str);
6844 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6845 5b87815e 2020-03-05 stsp }
6846 5b87815e 2020-03-05 stsp }
6847 bfce7f83 2019-07-27 stsp
6848 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6849 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6850 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6851 bfce7f83 2019-07-27 stsp break;
6852 bfce7f83 2019-07-27 stsp }
6853 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6854 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6855 bfce7f83 2019-07-27 stsp if (err)
6856 bfce7f83 2019-07-27 stsp return err;
6857 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6858 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6859 bfce7f83 2019-07-27 stsp free(id_str);
6860 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6861 bfce7f83 2019-07-27 stsp }
6862 bfce7f83 2019-07-27 stsp }
6863 bfce7f83 2019-07-27 stsp
6864 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6865 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6866 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6867 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6868 bfce7f83 2019-07-27 stsp
6869 bfce7f83 2019-07-27 stsp return NULL;
6870 bfce7f83 2019-07-27 stsp }
6871 bfce7f83 2019-07-27 stsp
6872 bfce7f83 2019-07-27 stsp static const struct got_error *
6873 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6874 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6875 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6876 0ebf8283 2019-07-24 stsp {
6877 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6878 0ebf8283 2019-07-24 stsp char *editor;
6879 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6880 0ebf8283 2019-07-24 stsp
6881 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6882 0ebf8283 2019-07-24 stsp if (err)
6883 0ebf8283 2019-07-24 stsp return err;
6884 0ebf8283 2019-07-24 stsp
6885 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6886 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6887 0ebf8283 2019-07-24 stsp goto done;
6888 0ebf8283 2019-07-24 stsp }
6889 0ebf8283 2019-07-24 stsp
6890 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6891 0ebf8283 2019-07-24 stsp if (f == NULL) {
6892 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6893 0ebf8283 2019-07-24 stsp goto done;
6894 0ebf8283 2019-07-24 stsp }
6895 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6896 bfce7f83 2019-07-27 stsp if (err)
6897 bfce7f83 2019-07-27 stsp goto done;
6898 bfce7f83 2019-07-27 stsp
6899 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6900 0ebf8283 2019-07-24 stsp done:
6901 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6902 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6903 0ebf8283 2019-07-24 stsp free(editor);
6904 0ebf8283 2019-07-24 stsp return err;
6905 0ebf8283 2019-07-24 stsp }
6906 0ebf8283 2019-07-24 stsp
6907 0ebf8283 2019-07-24 stsp static const struct got_error *
6908 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6909 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6910 514f2ffe 2020-01-29 stsp struct got_repository *);
6911 0ebf8283 2019-07-24 stsp
6912 0ebf8283 2019-07-24 stsp static const struct got_error *
6913 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6914 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6915 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6916 0ebf8283 2019-07-24 stsp {
6917 0ebf8283 2019-07-24 stsp const struct got_error *err;
6918 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6919 0ebf8283 2019-07-24 stsp char *path = NULL;
6920 0ebf8283 2019-07-24 stsp
6921 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6922 0ebf8283 2019-07-24 stsp if (err)
6923 0ebf8283 2019-07-24 stsp return err;
6924 0ebf8283 2019-07-24 stsp
6925 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6926 0ebf8283 2019-07-24 stsp if (err)
6927 0ebf8283 2019-07-24 stsp goto done;
6928 0ebf8283 2019-07-24 stsp
6929 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6930 0ebf8283 2019-07-24 stsp if (err)
6931 0ebf8283 2019-07-24 stsp goto done;
6932 0ebf8283 2019-07-24 stsp
6933 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6934 083957f4 2020-02-24 stsp rewind(f);
6935 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6936 083957f4 2020-02-24 stsp } else {
6937 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6938 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6939 0ebf8283 2019-07-24 stsp goto done;
6940 083957f4 2020-02-24 stsp }
6941 083957f4 2020-02-24 stsp f = NULL;
6942 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6943 083957f4 2020-02-24 stsp if (err) {
6944 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6945 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6946 083957f4 2020-02-24 stsp goto done;
6947 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6948 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6949 083957f4 2020-02-24 stsp }
6950 0ebf8283 2019-07-24 stsp }
6951 0ebf8283 2019-07-24 stsp done:
6952 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6953 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6954 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6955 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6956 0ebf8283 2019-07-24 stsp free(path);
6957 0ebf8283 2019-07-24 stsp return err;
6958 0ebf8283 2019-07-24 stsp }
6959 0ebf8283 2019-07-24 stsp
6960 0ebf8283 2019-07-24 stsp static const struct got_error *
6961 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6962 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6963 0ebf8283 2019-07-24 stsp {
6964 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6965 0ebf8283 2019-07-24 stsp char *path = NULL;
6966 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6967 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6968 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6969 0ebf8283 2019-07-24 stsp
6970 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6971 0ebf8283 2019-07-24 stsp if (err)
6972 0ebf8283 2019-07-24 stsp return err;
6973 0ebf8283 2019-07-24 stsp
6974 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6975 0ebf8283 2019-07-24 stsp if (f == NULL) {
6976 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6977 0ebf8283 2019-07-24 stsp goto done;
6978 0ebf8283 2019-07-24 stsp }
6979 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6980 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6981 0ebf8283 2019-07-24 stsp repo);
6982 0ebf8283 2019-07-24 stsp if (err)
6983 0ebf8283 2019-07-24 stsp break;
6984 0ebf8283 2019-07-24 stsp
6985 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6986 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6987 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6988 0ebf8283 2019-07-24 stsp if (n < 0) {
6989 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6990 0ebf8283 2019-07-24 stsp break;
6991 0ebf8283 2019-07-24 stsp }
6992 0ebf8283 2019-07-24 stsp }
6993 0ebf8283 2019-07-24 stsp }
6994 0ebf8283 2019-07-24 stsp done:
6995 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6996 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6997 0ebf8283 2019-07-24 stsp free(path);
6998 0ebf8283 2019-07-24 stsp if (commit)
6999 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7000 0ebf8283 2019-07-24 stsp return err;
7001 0ebf8283 2019-07-24 stsp }
7002 0ebf8283 2019-07-24 stsp
7003 bfce7f83 2019-07-27 stsp void
7004 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7005 bfce7f83 2019-07-27 stsp {
7006 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7007 bfce7f83 2019-07-27 stsp
7008 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7009 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7010 bfce7f83 2019-07-27 stsp free(hle);
7011 bfce7f83 2019-07-27 stsp }
7012 bfce7f83 2019-07-27 stsp }
7013 bfce7f83 2019-07-27 stsp
7014 0ebf8283 2019-07-24 stsp static const struct got_error *
7015 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7016 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7017 0ebf8283 2019-07-24 stsp {
7018 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7019 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7020 0ebf8283 2019-07-24 stsp
7021 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7022 0ebf8283 2019-07-24 stsp if (f == NULL) {
7023 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7024 0ebf8283 2019-07-24 stsp goto done;
7025 0ebf8283 2019-07-24 stsp }
7026 0ebf8283 2019-07-24 stsp
7027 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7028 0ebf8283 2019-07-24 stsp done:
7029 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7030 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7031 0ebf8283 2019-07-24 stsp return err;
7032 0ebf8283 2019-07-24 stsp }
7033 0ebf8283 2019-07-24 stsp
7034 0ebf8283 2019-07-24 stsp static const struct got_error *
7035 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7036 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7037 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7038 0ebf8283 2019-07-24 stsp {
7039 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7040 0ebf8283 2019-07-24 stsp int resp = ' ';
7041 0ebf8283 2019-07-24 stsp
7042 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7043 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7044 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7045 0ebf8283 2019-07-24 stsp resp = getchar();
7046 a61a4414 2019-08-07 stsp if (resp == '\n')
7047 a61a4414 2019-08-07 stsp resp = getchar();
7048 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7049 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7050 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7051 bfce7f83 2019-07-27 stsp repo);
7052 426ebf2e 2019-07-25 stsp if (err) {
7053 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7054 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7055 426ebf2e 2019-07-25 stsp break;
7056 bfce7f83 2019-07-27 stsp prev_err = err;
7057 426ebf2e 2019-07-25 stsp resp = ' ';
7058 426ebf2e 2019-07-25 stsp continue;
7059 426ebf2e 2019-07-25 stsp }
7060 bfce7f83 2019-07-27 stsp break;
7061 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7062 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7063 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7064 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7065 426ebf2e 2019-07-25 stsp if (err) {
7066 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7067 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7068 426ebf2e 2019-07-25 stsp break;
7069 bfce7f83 2019-07-27 stsp prev_err = err;
7070 426ebf2e 2019-07-25 stsp resp = ' ';
7071 426ebf2e 2019-07-25 stsp continue;
7072 426ebf2e 2019-07-25 stsp }
7073 bfce7f83 2019-07-27 stsp break;
7074 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7075 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7076 0ebf8283 2019-07-24 stsp break;
7077 426ebf2e 2019-07-25 stsp } else
7078 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7079 0ebf8283 2019-07-24 stsp }
7080 0ebf8283 2019-07-24 stsp
7081 0ebf8283 2019-07-24 stsp return err;
7082 0ebf8283 2019-07-24 stsp }
7083 0ebf8283 2019-07-24 stsp
7084 0ebf8283 2019-07-24 stsp static const struct got_error *
7085 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7086 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7087 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7088 0ebf8283 2019-07-24 stsp {
7089 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7090 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7091 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7092 3e3a69f1 2019-07-25 stsp branch, repo);
7093 0ebf8283 2019-07-24 stsp }
7094 0ebf8283 2019-07-24 stsp
7095 0ebf8283 2019-07-24 stsp static const struct got_error *
7096 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7097 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7098 0ebf8283 2019-07-24 stsp {
7099 0ebf8283 2019-07-24 stsp const struct got_error *err;
7100 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7101 0ebf8283 2019-07-24 stsp
7102 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7103 0ebf8283 2019-07-24 stsp if (err)
7104 0ebf8283 2019-07-24 stsp goto done;
7105 0ebf8283 2019-07-24 stsp
7106 0ebf8283 2019-07-24 stsp if (new_id) {
7107 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7108 0ebf8283 2019-07-24 stsp if (err)
7109 0ebf8283 2019-07-24 stsp goto done;
7110 0ebf8283 2019-07-24 stsp }
7111 0ebf8283 2019-07-24 stsp
7112 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7113 0ebf8283 2019-07-24 stsp if (new_id_str)
7114 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7115 0ebf8283 2019-07-24 stsp
7116 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7117 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7118 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7119 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7120 0ebf8283 2019-07-24 stsp goto done;
7121 0ebf8283 2019-07-24 stsp }
7122 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7123 0ebf8283 2019-07-24 stsp } else {
7124 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7125 0ebf8283 2019-07-24 stsp if (err)
7126 0ebf8283 2019-07-24 stsp goto done;
7127 0ebf8283 2019-07-24 stsp }
7128 0ebf8283 2019-07-24 stsp
7129 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7130 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7131 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7132 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7133 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7134 0ebf8283 2019-07-24 stsp break;
7135 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7136 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7137 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7138 0ebf8283 2019-07-24 stsp logmsg);
7139 0ebf8283 2019-07-24 stsp break;
7140 0ebf8283 2019-07-24 stsp default:
7141 0ebf8283 2019-07-24 stsp break;
7142 0ebf8283 2019-07-24 stsp }
7143 0ebf8283 2019-07-24 stsp done:
7144 0ebf8283 2019-07-24 stsp free(old_id_str);
7145 0ebf8283 2019-07-24 stsp free(new_id_str);
7146 0ebf8283 2019-07-24 stsp return err;
7147 0ebf8283 2019-07-24 stsp }
7148 0ebf8283 2019-07-24 stsp
7149 0ebf8283 2019-07-24 stsp static const struct got_error *
7150 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7151 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7152 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7153 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7154 0ebf8283 2019-07-24 stsp {
7155 0ebf8283 2019-07-24 stsp const struct got_error *err;
7156 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7157 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7158 0ebf8283 2019-07-24 stsp
7159 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7160 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7161 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7162 0ebf8283 2019-07-24 stsp if (err)
7163 0ebf8283 2019-07-24 stsp return err;
7164 0ebf8283 2019-07-24 stsp }
7165 0ebf8283 2019-07-24 stsp
7166 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7167 0ebf8283 2019-07-24 stsp if (err)
7168 0ebf8283 2019-07-24 stsp return err;
7169 0ebf8283 2019-07-24 stsp
7170 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7171 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7172 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7173 0ebf8283 2019-07-24 stsp if (err) {
7174 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7175 0ebf8283 2019-07-24 stsp goto done;
7176 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7177 0ebf8283 2019-07-24 stsp } else {
7178 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7179 0ebf8283 2019-07-24 stsp free(new_commit_id);
7180 0ebf8283 2019-07-24 stsp }
7181 0ebf8283 2019-07-24 stsp done:
7182 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7183 0ebf8283 2019-07-24 stsp return err;
7184 0ebf8283 2019-07-24 stsp }
7185 0ebf8283 2019-07-24 stsp
7186 0ebf8283 2019-07-24 stsp static const struct got_error *
7187 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7188 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7189 0ebf8283 2019-07-24 stsp {
7190 0ebf8283 2019-07-24 stsp const struct got_error *error;
7191 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7192 0ebf8283 2019-07-24 stsp
7193 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7194 0ebf8283 2019-07-24 stsp repo);
7195 0ebf8283 2019-07-24 stsp if (error)
7196 0ebf8283 2019-07-24 stsp return error;
7197 0ebf8283 2019-07-24 stsp
7198 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7199 0ebf8283 2019-07-24 stsp if (error)
7200 0ebf8283 2019-07-24 stsp return error;
7201 0ebf8283 2019-07-24 stsp
7202 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7203 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7204 0ebf8283 2019-07-24 stsp return error;
7205 ab20a43a 2020-01-29 stsp }
7206 ab20a43a 2020-01-29 stsp
7207 ab20a43a 2020-01-29 stsp static const struct got_error *
7208 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7209 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7210 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7211 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7212 ab20a43a 2020-01-29 stsp {
7213 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7214 ab20a43a 2020-01-29 stsp
7215 ab20a43a 2020-01-29 stsp switch (status) {
7216 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7217 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7218 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7219 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7220 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7221 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7222 ab20a43a 2020-01-29 stsp default:
7223 ab20a43a 2020-01-29 stsp break;
7224 ab20a43a 2020-01-29 stsp }
7225 ab20a43a 2020-01-29 stsp
7226 ab20a43a 2020-01-29 stsp switch (staged_status) {
7227 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7228 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7229 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7230 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7231 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7232 ab20a43a 2020-01-29 stsp default:
7233 ab20a43a 2020-01-29 stsp break;
7234 ab20a43a 2020-01-29 stsp }
7235 ab20a43a 2020-01-29 stsp
7236 ab20a43a 2020-01-29 stsp return NULL;
7237 0ebf8283 2019-07-24 stsp }
7238 0ebf8283 2019-07-24 stsp
7239 0ebf8283 2019-07-24 stsp static const struct got_error *
7240 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7241 0ebf8283 2019-07-24 stsp {
7242 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7243 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7244 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7245 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7246 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7247 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7248 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7249 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7250 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7251 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7252 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7253 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7254 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7255 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7256 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7257 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7258 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7259 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7260 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7261 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7262 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7263 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7264 0ebf8283 2019-07-24 stsp
7265 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7266 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7267 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7268 0ebf8283 2019-07-24 stsp
7269 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7270 0ebf8283 2019-07-24 stsp switch (ch) {
7271 0ebf8283 2019-07-24 stsp case 'a':
7272 0ebf8283 2019-07-24 stsp abort_edit = 1;
7273 0ebf8283 2019-07-24 stsp break;
7274 0ebf8283 2019-07-24 stsp case 'c':
7275 0ebf8283 2019-07-24 stsp continue_edit = 1;
7276 0ebf8283 2019-07-24 stsp break;
7277 0ebf8283 2019-07-24 stsp case 'F':
7278 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7279 0ebf8283 2019-07-24 stsp break;
7280 083957f4 2020-02-24 stsp case 'm':
7281 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7282 083957f4 2020-02-24 stsp break;
7283 0ebf8283 2019-07-24 stsp default:
7284 0ebf8283 2019-07-24 stsp usage_histedit();
7285 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7286 0ebf8283 2019-07-24 stsp }
7287 0ebf8283 2019-07-24 stsp }
7288 0ebf8283 2019-07-24 stsp
7289 0ebf8283 2019-07-24 stsp argc -= optind;
7290 0ebf8283 2019-07-24 stsp argv += optind;
7291 0ebf8283 2019-07-24 stsp
7292 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7293 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7294 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7295 0ebf8283 2019-07-24 stsp err(1, "pledge");
7296 0ebf8283 2019-07-24 stsp #endif
7297 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7298 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7299 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7300 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7301 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7302 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7303 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7304 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7305 0ebf8283 2019-07-24 stsp if (argc != 0)
7306 0ebf8283 2019-07-24 stsp usage_histedit();
7307 0ebf8283 2019-07-24 stsp
7308 0ebf8283 2019-07-24 stsp /*
7309 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7310 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7311 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7312 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7313 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7314 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7315 0ebf8283 2019-07-24 stsp */
7316 0ebf8283 2019-07-24 stsp
7317 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7318 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7319 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7320 0ebf8283 2019-07-24 stsp goto done;
7321 0ebf8283 2019-07-24 stsp }
7322 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7323 0ebf8283 2019-07-24 stsp if (error)
7324 0ebf8283 2019-07-24 stsp goto done;
7325 0ebf8283 2019-07-24 stsp
7326 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7327 c9956ddf 2019-09-08 stsp NULL);
7328 0ebf8283 2019-07-24 stsp if (error != NULL)
7329 0ebf8283 2019-07-24 stsp goto done;
7330 0ebf8283 2019-07-24 stsp
7331 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7332 0ebf8283 2019-07-24 stsp if (error)
7333 0ebf8283 2019-07-24 stsp goto done;
7334 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7335 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7336 0ebf8283 2019-07-24 stsp goto done;
7337 0ebf8283 2019-07-24 stsp }
7338 0ebf8283 2019-07-24 stsp
7339 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7340 0ebf8283 2019-07-24 stsp if (error)
7341 0ebf8283 2019-07-24 stsp goto done;
7342 0ebf8283 2019-07-24 stsp
7343 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7344 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7345 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7346 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7347 083957f4 2020-02-24 stsp "before the -m option can be used");
7348 083957f4 2020-02-24 stsp goto done;
7349 083957f4 2020-02-24 stsp }
7350 083957f4 2020-02-24 stsp
7351 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7352 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7353 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7354 3e3a69f1 2019-07-25 stsp worktree, repo);
7355 0ebf8283 2019-07-24 stsp if (error)
7356 0ebf8283 2019-07-24 stsp goto done;
7357 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7358 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7359 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7360 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7361 0ebf8283 2019-07-24 stsp if (error)
7362 0ebf8283 2019-07-24 stsp goto done;
7363 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7364 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7365 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7366 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7367 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7368 0ebf8283 2019-07-24 stsp goto done;
7369 0ebf8283 2019-07-24 stsp }
7370 0ebf8283 2019-07-24 stsp
7371 0ebf8283 2019-07-24 stsp if (continue_edit) {
7372 0ebf8283 2019-07-24 stsp char *path;
7373 0ebf8283 2019-07-24 stsp
7374 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7375 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7376 0ebf8283 2019-07-24 stsp goto done;
7377 0ebf8283 2019-07-24 stsp }
7378 0ebf8283 2019-07-24 stsp
7379 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7380 0ebf8283 2019-07-24 stsp if (error)
7381 0ebf8283 2019-07-24 stsp goto done;
7382 0ebf8283 2019-07-24 stsp
7383 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7384 0ebf8283 2019-07-24 stsp free(path);
7385 0ebf8283 2019-07-24 stsp if (error)
7386 0ebf8283 2019-07-24 stsp goto done;
7387 0ebf8283 2019-07-24 stsp
7388 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7389 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7390 3e3a69f1 2019-07-25 stsp worktree, repo);
7391 0ebf8283 2019-07-24 stsp if (error)
7392 0ebf8283 2019-07-24 stsp goto done;
7393 0ebf8283 2019-07-24 stsp
7394 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7395 0ebf8283 2019-07-24 stsp if (error)
7396 0ebf8283 2019-07-24 stsp goto done;
7397 0ebf8283 2019-07-24 stsp
7398 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7399 0ebf8283 2019-07-24 stsp head_commit_id);
7400 0ebf8283 2019-07-24 stsp if (error)
7401 0ebf8283 2019-07-24 stsp goto done;
7402 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7403 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7404 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7405 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7406 3aac7cf7 2019-07-25 stsp goto done;
7407 3aac7cf7 2019-07-25 stsp }
7408 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7409 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7410 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7411 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7412 0ebf8283 2019-07-24 stsp commit = NULL;
7413 0ebf8283 2019-07-24 stsp if (error)
7414 0ebf8283 2019-07-24 stsp goto done;
7415 0ebf8283 2019-07-24 stsp } else {
7416 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7417 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7418 0ebf8283 2019-07-24 stsp goto done;
7419 0ebf8283 2019-07-24 stsp }
7420 0ebf8283 2019-07-24 stsp
7421 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7422 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7423 0ebf8283 2019-07-24 stsp if (error != NULL)
7424 0ebf8283 2019-07-24 stsp goto done;
7425 0ebf8283 2019-07-24 stsp
7426 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7427 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7428 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7429 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7430 c7d20a3f 2019-07-30 stsp goto done;
7431 c7d20a3f 2019-07-30 stsp }
7432 c7d20a3f 2019-07-30 stsp
7433 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7434 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7435 bfce7f83 2019-07-27 stsp branch = NULL;
7436 0ebf8283 2019-07-24 stsp if (error)
7437 0ebf8283 2019-07-24 stsp goto done;
7438 0ebf8283 2019-07-24 stsp
7439 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7440 0ebf8283 2019-07-24 stsp head_commit_id);
7441 0ebf8283 2019-07-24 stsp if (error)
7442 0ebf8283 2019-07-24 stsp goto done;
7443 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7444 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7445 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7446 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7447 3aac7cf7 2019-07-25 stsp goto done;
7448 3aac7cf7 2019-07-25 stsp }
7449 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7450 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7451 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7452 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7453 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7454 0ebf8283 2019-07-24 stsp commit = NULL;
7455 0ebf8283 2019-07-24 stsp if (error)
7456 0ebf8283 2019-07-24 stsp goto done;
7457 0ebf8283 2019-07-24 stsp
7458 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7459 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7460 c5996fff 2020-01-29 stsp goto done;
7461 c5996fff 2020-01-29 stsp }
7462 c5996fff 2020-01-29 stsp
7463 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7464 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7465 bfce7f83 2019-07-27 stsp if (error)
7466 bfce7f83 2019-07-27 stsp goto done;
7467 bfce7f83 2019-07-27 stsp
7468 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7469 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7470 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7471 6ba2bea2 2019-07-27 stsp if (error) {
7472 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7473 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7474 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7475 0ebf8283 2019-07-24 stsp goto done;
7476 6ba2bea2 2019-07-27 stsp }
7477 0ebf8283 2019-07-24 stsp } else {
7478 514f2ffe 2020-01-29 stsp const char *branch_name;
7479 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7480 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7481 514f2ffe 2020-01-29 stsp branch_name += 11;
7482 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7483 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7484 6ba2bea2 2019-07-27 stsp if (error) {
7485 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7486 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7487 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7488 0ebf8283 2019-07-24 stsp goto done;
7489 6ba2bea2 2019-07-27 stsp }
7490 0ebf8283 2019-07-24 stsp
7491 0ebf8283 2019-07-24 stsp }
7492 0ebf8283 2019-07-24 stsp
7493 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7494 0ebf8283 2019-07-24 stsp repo);
7495 6ba2bea2 2019-07-27 stsp if (error) {
7496 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7497 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7498 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7499 0ebf8283 2019-07-24 stsp goto done;
7500 6ba2bea2 2019-07-27 stsp }
7501 0ebf8283 2019-07-24 stsp
7502 0ebf8283 2019-07-24 stsp }
7503 0ebf8283 2019-07-24 stsp
7504 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7505 4ec14e60 2019-08-28 hiltjo if (error)
7506 0ebf8283 2019-07-24 stsp goto done;
7507 0ebf8283 2019-07-24 stsp
7508 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7509 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7510 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7511 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7512 0ebf8283 2019-07-24 stsp continue;
7513 0ebf8283 2019-07-24 stsp
7514 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7515 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7516 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7517 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7518 0ebf8283 2019-07-24 stsp repo);
7519 ab20a43a 2020-01-29 stsp if (error)
7520 ab20a43a 2020-01-29 stsp goto done;
7521 0ebf8283 2019-07-24 stsp } else {
7522 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7523 ab20a43a 2020-01-29 stsp int have_changes = 0;
7524 ab20a43a 2020-01-29 stsp
7525 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7526 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7527 ab20a43a 2020-01-29 stsp if (error)
7528 ab20a43a 2020-01-29 stsp goto done;
7529 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7530 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7531 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7532 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7533 ab20a43a 2020-01-29 stsp if (error) {
7534 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7535 ab20a43a 2020-01-29 stsp goto done;
7536 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7537 ab20a43a 2020-01-29 stsp goto done;
7538 ab20a43a 2020-01-29 stsp }
7539 ab20a43a 2020-01-29 stsp if (have_changes) {
7540 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7541 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7542 ab20a43a 2020-01-29 stsp if (error)
7543 ab20a43a 2020-01-29 stsp goto done;
7544 ab20a43a 2020-01-29 stsp } else {
7545 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7546 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7547 ab20a43a 2020-01-29 stsp if (error)
7548 ab20a43a 2020-01-29 stsp goto done;
7549 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7550 ab20a43a 2020-01-29 stsp hle, NULL);
7551 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7552 ab20a43a 2020-01-29 stsp commit = NULL;
7553 ab20a43a 2020-01-29 stsp if (error)
7554 ab20a43a 2020-01-29 stsp goto done;
7555 ab20a43a 2020-01-29 stsp }
7556 0ebf8283 2019-07-24 stsp }
7557 0ebf8283 2019-07-24 stsp continue;
7558 0ebf8283 2019-07-24 stsp }
7559 0ebf8283 2019-07-24 stsp
7560 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7561 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7562 0ebf8283 2019-07-24 stsp if (error)
7563 0ebf8283 2019-07-24 stsp goto done;
7564 0ebf8283 2019-07-24 stsp continue;
7565 0ebf8283 2019-07-24 stsp }
7566 0ebf8283 2019-07-24 stsp
7567 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7568 0ebf8283 2019-07-24 stsp hle->commit_id);
7569 0ebf8283 2019-07-24 stsp if (error)
7570 0ebf8283 2019-07-24 stsp goto done;
7571 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7572 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7573 0ebf8283 2019-07-24 stsp
7574 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7575 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7576 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7577 0ebf8283 2019-07-24 stsp if (error)
7578 0ebf8283 2019-07-24 stsp goto done;
7579 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7580 0ebf8283 2019-07-24 stsp commit = NULL;
7581 0ebf8283 2019-07-24 stsp
7582 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7583 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7584 a0ea4fc0 2020-02-28 stsp repo);
7585 a0ea4fc0 2020-02-28 stsp if (error)
7586 a0ea4fc0 2020-02-28 stsp goto done;
7587 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7588 0ebf8283 2019-07-24 stsp break;
7589 0ebf8283 2019-07-24 stsp }
7590 0ebf8283 2019-07-24 stsp
7591 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7592 0ebf8283 2019-07-24 stsp char *id_str;
7593 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7594 0ebf8283 2019-07-24 stsp if (error)
7595 0ebf8283 2019-07-24 stsp goto done;
7596 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7597 0ebf8283 2019-07-24 stsp id_str);
7598 0ebf8283 2019-07-24 stsp free(id_str);
7599 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7600 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7601 3e3a69f1 2019-07-25 stsp fileindex);
7602 0ebf8283 2019-07-24 stsp goto done;
7603 0ebf8283 2019-07-24 stsp }
7604 0ebf8283 2019-07-24 stsp
7605 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7606 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7607 0ebf8283 2019-07-24 stsp if (error)
7608 0ebf8283 2019-07-24 stsp goto done;
7609 0ebf8283 2019-07-24 stsp continue;
7610 0ebf8283 2019-07-24 stsp }
7611 0ebf8283 2019-07-24 stsp
7612 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7613 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7614 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7615 0ebf8283 2019-07-24 stsp if (error)
7616 0ebf8283 2019-07-24 stsp goto done;
7617 0ebf8283 2019-07-24 stsp }
7618 0ebf8283 2019-07-24 stsp
7619 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7620 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7621 0ebf8283 2019-07-24 stsp if (error)
7622 0ebf8283 2019-07-24 stsp goto done;
7623 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7624 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7625 0ebf8283 2019-07-24 stsp } else
7626 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7627 3e3a69f1 2019-07-25 stsp branch, repo);
7628 0ebf8283 2019-07-24 stsp done:
7629 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7630 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7631 0ebf8283 2019-07-24 stsp free(head_commit_id);
7632 0ebf8283 2019-07-24 stsp free(base_commit_id);
7633 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7634 0ebf8283 2019-07-24 stsp if (commit)
7635 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7636 0ebf8283 2019-07-24 stsp if (branch)
7637 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7638 0ebf8283 2019-07-24 stsp if (tmp_branch)
7639 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7640 0ebf8283 2019-07-24 stsp if (worktree)
7641 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7642 2822a352 2019-10-15 stsp if (repo)
7643 2822a352 2019-10-15 stsp got_repo_close(repo);
7644 2822a352 2019-10-15 stsp return error;
7645 2822a352 2019-10-15 stsp }
7646 2822a352 2019-10-15 stsp
7647 2822a352 2019-10-15 stsp __dead static void
7648 2822a352 2019-10-15 stsp usage_integrate(void)
7649 2822a352 2019-10-15 stsp {
7650 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7651 2822a352 2019-10-15 stsp exit(1);
7652 2822a352 2019-10-15 stsp }
7653 2822a352 2019-10-15 stsp
7654 2822a352 2019-10-15 stsp static const struct got_error *
7655 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7656 2822a352 2019-10-15 stsp {
7657 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7658 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7659 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7660 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7661 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7662 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7663 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7664 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7665 2822a352 2019-10-15 stsp int ch, did_something = 0;
7666 2822a352 2019-10-15 stsp
7667 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7668 2822a352 2019-10-15 stsp switch (ch) {
7669 2822a352 2019-10-15 stsp default:
7670 2822a352 2019-10-15 stsp usage_integrate();
7671 2822a352 2019-10-15 stsp /* NOTREACHED */
7672 2822a352 2019-10-15 stsp }
7673 2822a352 2019-10-15 stsp }
7674 2822a352 2019-10-15 stsp
7675 2822a352 2019-10-15 stsp argc -= optind;
7676 2822a352 2019-10-15 stsp argv += optind;
7677 2822a352 2019-10-15 stsp
7678 2822a352 2019-10-15 stsp if (argc != 1)
7679 2822a352 2019-10-15 stsp usage_integrate();
7680 2822a352 2019-10-15 stsp branch_arg = argv[0];
7681 2822a352 2019-10-15 stsp
7682 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7683 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7684 2822a352 2019-10-15 stsp err(1, "pledge");
7685 2822a352 2019-10-15 stsp
7686 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7687 2822a352 2019-10-15 stsp if (cwd == NULL) {
7688 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7689 2822a352 2019-10-15 stsp goto done;
7690 2822a352 2019-10-15 stsp }
7691 2822a352 2019-10-15 stsp
7692 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7693 2822a352 2019-10-15 stsp if (error)
7694 2822a352 2019-10-15 stsp goto done;
7695 2822a352 2019-10-15 stsp
7696 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7697 2822a352 2019-10-15 stsp if (error)
7698 2822a352 2019-10-15 stsp goto done;
7699 2822a352 2019-10-15 stsp
7700 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7701 2822a352 2019-10-15 stsp NULL);
7702 2822a352 2019-10-15 stsp if (error != NULL)
7703 2822a352 2019-10-15 stsp goto done;
7704 2822a352 2019-10-15 stsp
7705 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7706 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7707 2822a352 2019-10-15 stsp if (error)
7708 2822a352 2019-10-15 stsp goto done;
7709 2822a352 2019-10-15 stsp
7710 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7711 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7712 2822a352 2019-10-15 stsp goto done;
7713 2822a352 2019-10-15 stsp }
7714 2822a352 2019-10-15 stsp
7715 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7716 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7717 2822a352 2019-10-15 stsp if (error)
7718 2822a352 2019-10-15 stsp goto done;
7719 2822a352 2019-10-15 stsp
7720 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7721 2822a352 2019-10-15 stsp if (refname == NULL) {
7722 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7723 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7724 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7725 2822a352 2019-10-15 stsp goto done;
7726 2822a352 2019-10-15 stsp }
7727 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7728 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7729 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7730 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7731 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7732 2822a352 2019-10-15 stsp goto done;
7733 2822a352 2019-10-15 stsp }
7734 2822a352 2019-10-15 stsp
7735 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7736 2822a352 2019-10-15 stsp if (error)
7737 2822a352 2019-10-15 stsp goto done;
7738 2822a352 2019-10-15 stsp
7739 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7740 2822a352 2019-10-15 stsp if (error)
7741 2822a352 2019-10-15 stsp goto done;
7742 2822a352 2019-10-15 stsp
7743 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7744 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7745 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7746 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7747 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7748 2822a352 2019-10-15 stsp goto done;
7749 2822a352 2019-10-15 stsp }
7750 2822a352 2019-10-15 stsp
7751 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7752 2822a352 2019-10-15 stsp if (error) {
7753 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7754 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7755 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7756 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7757 2822a352 2019-10-15 stsp goto done;
7758 2822a352 2019-10-15 stsp }
7759 2822a352 2019-10-15 stsp
7760 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7761 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7762 2822a352 2019-10-15 stsp check_cancelled, NULL);
7763 2822a352 2019-10-15 stsp if (error)
7764 2822a352 2019-10-15 stsp goto done;
7765 2822a352 2019-10-15 stsp
7766 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7767 2822a352 2019-10-15 stsp done:
7768 715dc77e 2019-08-03 stsp if (repo)
7769 715dc77e 2019-08-03 stsp got_repo_close(repo);
7770 2822a352 2019-10-15 stsp if (worktree)
7771 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7772 2822a352 2019-10-15 stsp free(cwd);
7773 2822a352 2019-10-15 stsp free(base_commit_id);
7774 2822a352 2019-10-15 stsp free(commit_id);
7775 2822a352 2019-10-15 stsp free(refname);
7776 2822a352 2019-10-15 stsp free(base_refname);
7777 715dc77e 2019-08-03 stsp return error;
7778 715dc77e 2019-08-03 stsp }
7779 715dc77e 2019-08-03 stsp
7780 715dc77e 2019-08-03 stsp __dead static void
7781 715dc77e 2019-08-03 stsp usage_stage(void)
7782 715dc77e 2019-08-03 stsp {
7783 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7784 f3055044 2019-08-07 stsp "[file-path ...]\n",
7785 715dc77e 2019-08-03 stsp getprogname());
7786 715dc77e 2019-08-03 stsp exit(1);
7787 715dc77e 2019-08-03 stsp }
7788 715dc77e 2019-08-03 stsp
7789 715dc77e 2019-08-03 stsp static const struct got_error *
7790 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7791 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7792 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7793 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7794 408b4ebc 2019-08-03 stsp {
7795 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7796 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7797 408b4ebc 2019-08-03 stsp
7798 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7799 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7800 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7801 408b4ebc 2019-08-03 stsp return NULL;
7802 408b4ebc 2019-08-03 stsp
7803 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7804 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7805 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7806 408b4ebc 2019-08-03 stsp else
7807 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7808 408b4ebc 2019-08-03 stsp if (err)
7809 408b4ebc 2019-08-03 stsp return err;
7810 408b4ebc 2019-08-03 stsp
7811 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7812 408b4ebc 2019-08-03 stsp free(id_str);
7813 dc424a06 2019-08-07 stsp return NULL;
7814 dc424a06 2019-08-07 stsp }
7815 2e1f37b0 2019-08-08 stsp
7816 dc424a06 2019-08-07 stsp static const struct got_error *
7817 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7818 715dc77e 2019-08-03 stsp {
7819 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7820 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7821 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7822 715dc77e 2019-08-03 stsp char *cwd = NULL;
7823 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7824 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7825 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7826 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7827 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7828 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7829 715dc77e 2019-08-03 stsp
7830 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7831 715dc77e 2019-08-03 stsp
7832 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7833 715dc77e 2019-08-03 stsp switch (ch) {
7834 408b4ebc 2019-08-03 stsp case 'l':
7835 408b4ebc 2019-08-03 stsp list_stage = 1;
7836 408b4ebc 2019-08-03 stsp break;
7837 dc424a06 2019-08-07 stsp case 'p':
7838 dc424a06 2019-08-07 stsp pflag = 1;
7839 dc424a06 2019-08-07 stsp break;
7840 dc424a06 2019-08-07 stsp case 'F':
7841 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7842 dc424a06 2019-08-07 stsp break;
7843 715dc77e 2019-08-03 stsp default:
7844 715dc77e 2019-08-03 stsp usage_stage();
7845 715dc77e 2019-08-03 stsp /* NOTREACHED */
7846 715dc77e 2019-08-03 stsp }
7847 715dc77e 2019-08-03 stsp }
7848 715dc77e 2019-08-03 stsp
7849 715dc77e 2019-08-03 stsp argc -= optind;
7850 715dc77e 2019-08-03 stsp argv += optind;
7851 715dc77e 2019-08-03 stsp
7852 715dc77e 2019-08-03 stsp #ifndef PROFILE
7853 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7854 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7855 715dc77e 2019-08-03 stsp err(1, "pledge");
7856 715dc77e 2019-08-03 stsp #endif
7857 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7858 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7859 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7860 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7861 715dc77e 2019-08-03 stsp
7862 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7863 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7864 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7865 715dc77e 2019-08-03 stsp goto done;
7866 715dc77e 2019-08-03 stsp }
7867 715dc77e 2019-08-03 stsp
7868 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7869 715dc77e 2019-08-03 stsp if (error)
7870 715dc77e 2019-08-03 stsp goto done;
7871 715dc77e 2019-08-03 stsp
7872 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7873 c9956ddf 2019-09-08 stsp NULL);
7874 715dc77e 2019-08-03 stsp if (error != NULL)
7875 715dc77e 2019-08-03 stsp goto done;
7876 715dc77e 2019-08-03 stsp
7877 dc424a06 2019-08-07 stsp if (patch_script_path) {
7878 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7879 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7880 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7881 dc424a06 2019-08-07 stsp patch_script_path);
7882 dc424a06 2019-08-07 stsp goto done;
7883 dc424a06 2019-08-07 stsp }
7884 dc424a06 2019-08-07 stsp }
7885 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7886 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7887 715dc77e 2019-08-03 stsp if (error)
7888 715dc77e 2019-08-03 stsp goto done;
7889 715dc77e 2019-08-03 stsp
7890 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7891 6d022e97 2019-08-04 stsp if (error)
7892 6d022e97 2019-08-04 stsp goto done;
7893 715dc77e 2019-08-03 stsp
7894 6d022e97 2019-08-04 stsp if (list_stage)
7895 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7896 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7897 2e1f37b0 2019-08-08 stsp else {
7898 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7899 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7900 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7901 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7902 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7903 2e1f37b0 2019-08-08 stsp }
7904 ad493afc 2019-08-03 stsp done:
7905 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7906 2e1f37b0 2019-08-08 stsp error == NULL)
7907 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7908 ad493afc 2019-08-03 stsp if (repo)
7909 ad493afc 2019-08-03 stsp got_repo_close(repo);
7910 ad493afc 2019-08-03 stsp if (worktree)
7911 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7912 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7913 ad493afc 2019-08-03 stsp free((char *)pe->path);
7914 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7915 ad493afc 2019-08-03 stsp free(cwd);
7916 ad493afc 2019-08-03 stsp return error;
7917 ad493afc 2019-08-03 stsp }
7918 ad493afc 2019-08-03 stsp
7919 ad493afc 2019-08-03 stsp __dead static void
7920 ad493afc 2019-08-03 stsp usage_unstage(void)
7921 ad493afc 2019-08-03 stsp {
7922 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7923 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7924 ad493afc 2019-08-03 stsp getprogname());
7925 ad493afc 2019-08-03 stsp exit(1);
7926 ad493afc 2019-08-03 stsp }
7927 ad493afc 2019-08-03 stsp
7928 ad493afc 2019-08-03 stsp
7929 ad493afc 2019-08-03 stsp static const struct got_error *
7930 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7931 ad493afc 2019-08-03 stsp {
7932 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7933 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7934 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7935 ad493afc 2019-08-03 stsp char *cwd = NULL;
7936 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7937 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7938 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7939 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7940 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7941 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7942 ad493afc 2019-08-03 stsp
7943 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7944 ad493afc 2019-08-03 stsp
7945 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7946 ad493afc 2019-08-03 stsp switch (ch) {
7947 2e1f37b0 2019-08-08 stsp case 'p':
7948 2e1f37b0 2019-08-08 stsp pflag = 1;
7949 2e1f37b0 2019-08-08 stsp break;
7950 2e1f37b0 2019-08-08 stsp case 'F':
7951 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7952 2e1f37b0 2019-08-08 stsp break;
7953 ad493afc 2019-08-03 stsp default:
7954 ad493afc 2019-08-03 stsp usage_unstage();
7955 ad493afc 2019-08-03 stsp /* NOTREACHED */
7956 ad493afc 2019-08-03 stsp }
7957 ad493afc 2019-08-03 stsp }
7958 ad493afc 2019-08-03 stsp
7959 ad493afc 2019-08-03 stsp argc -= optind;
7960 ad493afc 2019-08-03 stsp argv += optind;
7961 ad493afc 2019-08-03 stsp
7962 ad493afc 2019-08-03 stsp #ifndef PROFILE
7963 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7964 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7965 ad493afc 2019-08-03 stsp err(1, "pledge");
7966 ad493afc 2019-08-03 stsp #endif
7967 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7968 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7969 2e1f37b0 2019-08-08 stsp
7970 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7971 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7972 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7973 ad493afc 2019-08-03 stsp goto done;
7974 ad493afc 2019-08-03 stsp }
7975 ad493afc 2019-08-03 stsp
7976 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7977 ad493afc 2019-08-03 stsp if (error)
7978 ad493afc 2019-08-03 stsp goto done;
7979 ad493afc 2019-08-03 stsp
7980 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7981 c9956ddf 2019-09-08 stsp NULL);
7982 ad493afc 2019-08-03 stsp if (error != NULL)
7983 ad493afc 2019-08-03 stsp goto done;
7984 ad493afc 2019-08-03 stsp
7985 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7986 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7987 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7988 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7989 2e1f37b0 2019-08-08 stsp patch_script_path);
7990 2e1f37b0 2019-08-08 stsp goto done;
7991 2e1f37b0 2019-08-08 stsp }
7992 2e1f37b0 2019-08-08 stsp }
7993 2e1f37b0 2019-08-08 stsp
7994 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7995 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7996 ad493afc 2019-08-03 stsp if (error)
7997 ad493afc 2019-08-03 stsp goto done;
7998 ad493afc 2019-08-03 stsp
7999 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8000 ad493afc 2019-08-03 stsp if (error)
8001 ad493afc 2019-08-03 stsp goto done;
8002 ad493afc 2019-08-03 stsp
8003 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8004 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8005 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8006 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8007 715dc77e 2019-08-03 stsp done:
8008 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8009 2e1f37b0 2019-08-08 stsp error == NULL)
8010 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8011 0ebf8283 2019-07-24 stsp if (repo)
8012 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8013 715dc77e 2019-08-03 stsp if (worktree)
8014 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8015 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8016 715dc77e 2019-08-03 stsp free((char *)pe->path);
8017 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8018 715dc77e 2019-08-03 stsp free(cwd);
8019 01073a5d 2019-08-22 stsp return error;
8020 01073a5d 2019-08-22 stsp }
8021 01073a5d 2019-08-22 stsp
8022 01073a5d 2019-08-22 stsp __dead static void
8023 01073a5d 2019-08-22 stsp usage_cat(void)
8024 01073a5d 2019-08-22 stsp {
8025 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8026 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8027 01073a5d 2019-08-22 stsp exit(1);
8028 01073a5d 2019-08-22 stsp }
8029 01073a5d 2019-08-22 stsp
8030 01073a5d 2019-08-22 stsp static const struct got_error *
8031 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8032 01073a5d 2019-08-22 stsp {
8033 01073a5d 2019-08-22 stsp const struct got_error *err;
8034 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8035 01073a5d 2019-08-22 stsp
8036 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8037 01073a5d 2019-08-22 stsp if (err)
8038 01073a5d 2019-08-22 stsp return err;
8039 01073a5d 2019-08-22 stsp
8040 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8041 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8042 01073a5d 2019-08-22 stsp return err;
8043 01073a5d 2019-08-22 stsp }
8044 01073a5d 2019-08-22 stsp
8045 01073a5d 2019-08-22 stsp static const struct got_error *
8046 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8047 01073a5d 2019-08-22 stsp {
8048 01073a5d 2019-08-22 stsp const struct got_error *err;
8049 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8050 56e0773d 2019-11-28 stsp int nentries, i;
8051 01073a5d 2019-08-22 stsp
8052 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8053 01073a5d 2019-08-22 stsp if (err)
8054 01073a5d 2019-08-22 stsp return err;
8055 01073a5d 2019-08-22 stsp
8056 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8057 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8058 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8059 01073a5d 2019-08-22 stsp char *id_str;
8060 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8061 01073a5d 2019-08-22 stsp break;
8062 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8063 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8064 01073a5d 2019-08-22 stsp if (err)
8065 01073a5d 2019-08-22 stsp break;
8066 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8067 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8068 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8069 01073a5d 2019-08-22 stsp free(id_str);
8070 01073a5d 2019-08-22 stsp }
8071 01073a5d 2019-08-22 stsp
8072 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8073 01073a5d 2019-08-22 stsp return err;
8074 01073a5d 2019-08-22 stsp }
8075 01073a5d 2019-08-22 stsp
8076 01073a5d 2019-08-22 stsp static const struct got_error *
8077 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8078 01073a5d 2019-08-22 stsp {
8079 01073a5d 2019-08-22 stsp const struct got_error *err;
8080 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8081 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8082 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8083 01073a5d 2019-08-22 stsp char *id_str = NULL;
8084 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8085 01073a5d 2019-08-22 stsp
8086 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8087 01073a5d 2019-08-22 stsp if (err)
8088 01073a5d 2019-08-22 stsp return err;
8089 01073a5d 2019-08-22 stsp
8090 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8091 01073a5d 2019-08-22 stsp if (err)
8092 01073a5d 2019-08-22 stsp goto done;
8093 01073a5d 2019-08-22 stsp
8094 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8095 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8096 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8097 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8098 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8099 01073a5d 2019-08-22 stsp char *pid_str;
8100 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8101 01073a5d 2019-08-22 stsp if (err)
8102 01073a5d 2019-08-22 stsp goto done;
8103 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8104 01073a5d 2019-08-22 stsp free(pid_str);
8105 01073a5d 2019-08-22 stsp }
8106 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8107 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8108 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8109 01073a5d 2019-08-22 stsp
8110 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8111 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8112 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8113 01073a5d 2019-08-22 stsp
8114 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8115 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8116 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8117 01073a5d 2019-08-22 stsp done:
8118 01073a5d 2019-08-22 stsp free(id_str);
8119 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8120 01073a5d 2019-08-22 stsp return err;
8121 01073a5d 2019-08-22 stsp }
8122 01073a5d 2019-08-22 stsp
8123 01073a5d 2019-08-22 stsp static const struct got_error *
8124 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8125 01073a5d 2019-08-22 stsp {
8126 01073a5d 2019-08-22 stsp const struct got_error *err;
8127 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8128 01073a5d 2019-08-22 stsp char *id_str = NULL;
8129 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8130 01073a5d 2019-08-22 stsp
8131 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8132 01073a5d 2019-08-22 stsp if (err)
8133 01073a5d 2019-08-22 stsp return err;
8134 01073a5d 2019-08-22 stsp
8135 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8136 01073a5d 2019-08-22 stsp if (err)
8137 01073a5d 2019-08-22 stsp goto done;
8138 01073a5d 2019-08-22 stsp
8139 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8140 e15d5241 2019-08-22 stsp
8141 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8142 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8143 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8144 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8145 01073a5d 2019-08-22 stsp break;
8146 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8147 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8148 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8149 01073a5d 2019-08-22 stsp break;
8150 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8151 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8152 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8153 01073a5d 2019-08-22 stsp break;
8154 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8155 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8156 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8157 01073a5d 2019-08-22 stsp break;
8158 01073a5d 2019-08-22 stsp default:
8159 01073a5d 2019-08-22 stsp break;
8160 01073a5d 2019-08-22 stsp }
8161 01073a5d 2019-08-22 stsp
8162 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8163 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8164 e15d5241 2019-08-22 stsp
8165 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8166 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8167 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8168 01073a5d 2019-08-22 stsp
8169 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8170 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8171 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8172 01073a5d 2019-08-22 stsp done:
8173 01073a5d 2019-08-22 stsp free(id_str);
8174 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8175 01073a5d 2019-08-22 stsp return err;
8176 01073a5d 2019-08-22 stsp }
8177 01073a5d 2019-08-22 stsp
8178 01073a5d 2019-08-22 stsp static const struct got_error *
8179 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8180 01073a5d 2019-08-22 stsp {
8181 01073a5d 2019-08-22 stsp const struct got_error *error;
8182 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8183 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8184 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8185 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8186 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8187 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8188 01073a5d 2019-08-22 stsp
8189 01073a5d 2019-08-22 stsp #ifndef PROFILE
8190 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8191 01073a5d 2019-08-22 stsp NULL) == -1)
8192 01073a5d 2019-08-22 stsp err(1, "pledge");
8193 01073a5d 2019-08-22 stsp #endif
8194 01073a5d 2019-08-22 stsp
8195 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8196 01073a5d 2019-08-22 stsp switch (ch) {
8197 896e9b6f 2019-08-26 stsp case 'c':
8198 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8199 896e9b6f 2019-08-26 stsp break;
8200 01073a5d 2019-08-22 stsp case 'r':
8201 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8202 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8203 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8204 9ba1d308 2019-10-21 stsp optarg);
8205 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8206 01073a5d 2019-08-22 stsp break;
8207 896e9b6f 2019-08-26 stsp case 'P':
8208 896e9b6f 2019-08-26 stsp force_path = 1;
8209 896e9b6f 2019-08-26 stsp break;
8210 01073a5d 2019-08-22 stsp default:
8211 01073a5d 2019-08-22 stsp usage_cat();
8212 01073a5d 2019-08-22 stsp /* NOTREACHED */
8213 01073a5d 2019-08-22 stsp }
8214 01073a5d 2019-08-22 stsp }
8215 01073a5d 2019-08-22 stsp
8216 01073a5d 2019-08-22 stsp argc -= optind;
8217 01073a5d 2019-08-22 stsp argv += optind;
8218 01073a5d 2019-08-22 stsp
8219 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8220 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8221 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8222 01073a5d 2019-08-22 stsp goto done;
8223 01073a5d 2019-08-22 stsp }
8224 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8225 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8226 01073a5d 2019-08-22 stsp goto done;
8227 01073a5d 2019-08-22 stsp if (worktree) {
8228 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8229 01073a5d 2019-08-22 stsp repo_path = strdup(
8230 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8231 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8232 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8233 01073a5d 2019-08-22 stsp goto done;
8234 01073a5d 2019-08-22 stsp }
8235 01073a5d 2019-08-22 stsp }
8236 01073a5d 2019-08-22 stsp }
8237 01073a5d 2019-08-22 stsp
8238 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8239 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8240 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8241 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8242 01073a5d 2019-08-22 stsp }
8243 01073a5d 2019-08-22 stsp
8244 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8245 01073a5d 2019-08-22 stsp free(repo_path);
8246 01073a5d 2019-08-22 stsp if (error != NULL)
8247 01073a5d 2019-08-22 stsp goto done;
8248 01073a5d 2019-08-22 stsp
8249 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8250 896e9b6f 2019-08-26 stsp if (error)
8251 896e9b6f 2019-08-26 stsp goto done;
8252 896e9b6f 2019-08-26 stsp
8253 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8254 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8255 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8256 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8257 01073a5d 2019-08-22 stsp if (error)
8258 01073a5d 2019-08-22 stsp goto done;
8259 01073a5d 2019-08-22 stsp
8260 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8261 896e9b6f 2019-08-26 stsp if (force_path) {
8262 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8263 896e9b6f 2019-08-26 stsp argv[i]);
8264 896e9b6f 2019-08-26 stsp if (error)
8265 896e9b6f 2019-08-26 stsp break;
8266 896e9b6f 2019-08-26 stsp } else {
8267 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8268 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8269 896e9b6f 2019-08-26 stsp if (error) {
8270 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8271 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8272 896e9b6f 2019-08-26 stsp break;
8273 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8274 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8275 896e9b6f 2019-08-26 stsp if (error)
8276 896e9b6f 2019-08-26 stsp break;
8277 896e9b6f 2019-08-26 stsp }
8278 896e9b6f 2019-08-26 stsp }
8279 01073a5d 2019-08-22 stsp
8280 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8281 01073a5d 2019-08-22 stsp if (error)
8282 01073a5d 2019-08-22 stsp break;
8283 01073a5d 2019-08-22 stsp
8284 01073a5d 2019-08-22 stsp switch (obj_type) {
8285 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8286 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8287 01073a5d 2019-08-22 stsp break;
8288 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8289 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8290 01073a5d 2019-08-22 stsp break;
8291 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8292 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8293 01073a5d 2019-08-22 stsp break;
8294 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8295 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8296 01073a5d 2019-08-22 stsp break;
8297 01073a5d 2019-08-22 stsp default:
8298 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8299 01073a5d 2019-08-22 stsp break;
8300 01073a5d 2019-08-22 stsp }
8301 01073a5d 2019-08-22 stsp if (error)
8302 01073a5d 2019-08-22 stsp break;
8303 01073a5d 2019-08-22 stsp free(label);
8304 01073a5d 2019-08-22 stsp label = NULL;
8305 01073a5d 2019-08-22 stsp free(id);
8306 01073a5d 2019-08-22 stsp id = NULL;
8307 01073a5d 2019-08-22 stsp }
8308 01073a5d 2019-08-22 stsp done:
8309 01073a5d 2019-08-22 stsp free(label);
8310 01073a5d 2019-08-22 stsp free(id);
8311 896e9b6f 2019-08-26 stsp free(commit_id);
8312 01073a5d 2019-08-22 stsp if (worktree)
8313 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8314 01073a5d 2019-08-22 stsp if (repo) {
8315 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8316 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8317 01073a5d 2019-08-22 stsp if (error == NULL)
8318 01073a5d 2019-08-22 stsp error = repo_error;
8319 01073a5d 2019-08-22 stsp }
8320 0ebf8283 2019-07-24 stsp return error;
8321 0ebf8283 2019-07-24 stsp }