Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 5c860e29 2018-03-12 stsp *
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
8 5c860e29 2018-03-12 stsp *
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 5c860e29 2018-03-12 stsp */
17 5c860e29 2018-03-12 stsp
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.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 5c860e29 2018-03-12 stsp #include <locale.h>
28 99437157 2018-11-11 stsp #include <signal.h>
29 5c860e29 2018-03-12 stsp #include <stdio.h>
30 5c860e29 2018-03-12 stsp #include <stdlib.h>
31 5c860e29 2018-03-12 stsp #include <string.h>
32 5c860e29 2018-03-12 stsp #include <unistd.h>
33 c09a553d 2018-03-12 stsp #include <libgen.h>
34 c0768b0f 2018-06-10 stsp #include <time.h>
35 33ad4cbe 2019-05-12 jcs #include <paths.h>
36 5c860e29 2018-03-12 stsp
37 f42b1b34 2018-03-12 stsp #include "got_error.h"
38 f42b1b34 2018-03-12 stsp #include "got_object.h"
39 5261c201 2018-04-01 stsp #include "got_reference.h"
40 f42b1b34 2018-03-12 stsp #include "got_repository.h"
41 1dd54920 2019-05-11 stsp #include "got_path.h"
42 c09a553d 2018-03-12 stsp #include "got_worktree.h"
43 79109fed 2018-03-27 stsp #include "got_diff.h"
44 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
45 404c43c4 2018-06-21 stsp #include "got_blame.h"
46 63219cd2 2019-01-04 stsp #include "got_privsep.h"
47 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
48 5c860e29 2018-03-12 stsp
49 5c860e29 2018-03-12 stsp #ifndef nitems
50 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 5c860e29 2018-03-12 stsp #endif
52 99437157 2018-11-11 stsp
53 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
54 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
55 99437157 2018-11-11 stsp
56 99437157 2018-11-11 stsp static void
57 99437157 2018-11-11 stsp catch_sigint(int signo)
58 99437157 2018-11-11 stsp {
59 99437157 2018-11-11 stsp sigint_received = 1;
60 99437157 2018-11-11 stsp }
61 99437157 2018-11-11 stsp
62 99437157 2018-11-11 stsp static void
63 99437157 2018-11-11 stsp catch_sigpipe(int signo)
64 99437157 2018-11-11 stsp {
65 99437157 2018-11-11 stsp sigpipe_received = 1;
66 99437157 2018-11-11 stsp }
67 5c860e29 2018-03-12 stsp
68 99437157 2018-11-11 stsp
69 5c860e29 2018-03-12 stsp struct cmd {
70 5c860e29 2018-03-12 stsp const char *cmd_name;
71 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
72 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
73 5c860e29 2018-03-12 stsp };
74 5c860e29 2018-03-12 stsp
75 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
76 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
77 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
78 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
79 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
81 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
82 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
83 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
84 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
85 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
86 d00136be 2019-03-26 stsp __dead static void usage_add(void);
87 2ec1f75b 2019-03-26 stsp __dead static void usage_rm(void);
88 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
89 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
90 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
91 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
92 5c860e29 2018-03-12 stsp
93 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
94 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
95 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
96 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
97 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
98 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
99 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
100 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
101 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
102 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
103 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
104 2ec1f75b 2019-03-26 stsp static const struct got_error* cmd_rm(int, char *[]);
105 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
106 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
107 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
108 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
109 5c860e29 2018-03-12 stsp
110 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
111 5e070240 2019-06-22 stsp { "init", cmd_init, usage_init },
112 5e070240 2019-06-22 stsp { "checkout", cmd_checkout, usage_checkout },
113 5e070240 2019-06-22 stsp { "update", cmd_update, usage_update },
114 5e070240 2019-06-22 stsp { "log", cmd_log, usage_log },
115 5e070240 2019-06-22 stsp { "diff", cmd_diff, usage_diff },
116 5e070240 2019-06-22 stsp { "blame", cmd_blame, usage_blame },
117 5e070240 2019-06-22 stsp { "tree", cmd_tree, usage_tree },
118 5e070240 2019-06-22 stsp { "status", cmd_status, usage_status },
119 5e070240 2019-06-22 stsp { "ref", cmd_ref, usage_ref },
120 4e759de4 2019-06-26 stsp { "branch", cmd_branch, usage_branch },
121 5e070240 2019-06-22 stsp { "add", cmd_add, usage_add },
122 5e070240 2019-06-22 stsp { "rm", cmd_rm, usage_rm },
123 5e070240 2019-06-22 stsp { "revert", cmd_revert, usage_revert },
124 5e070240 2019-06-22 stsp { "commit", cmd_commit, usage_commit },
125 5e070240 2019-06-22 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick },
126 5e070240 2019-06-22 stsp { "backout", cmd_backout, usage_backout },
127 5c860e29 2018-03-12 stsp };
128 5c860e29 2018-03-12 stsp
129 5c860e29 2018-03-12 stsp int
130 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
131 5c860e29 2018-03-12 stsp {
132 5c860e29 2018-03-12 stsp struct cmd *cmd;
133 5c860e29 2018-03-12 stsp unsigned int i;
134 5c860e29 2018-03-12 stsp int ch;
135 1b6b95a8 2018-03-12 stsp int hflag = 0;
136 5c860e29 2018-03-12 stsp
137 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
138 5c860e29 2018-03-12 stsp
139 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
140 5c860e29 2018-03-12 stsp switch (ch) {
141 1b6b95a8 2018-03-12 stsp case 'h':
142 1b6b95a8 2018-03-12 stsp hflag = 1;
143 1b6b95a8 2018-03-12 stsp break;
144 5c860e29 2018-03-12 stsp default:
145 5c860e29 2018-03-12 stsp usage();
146 5c860e29 2018-03-12 stsp /* NOTREACHED */
147 5c860e29 2018-03-12 stsp }
148 5c860e29 2018-03-12 stsp }
149 5c860e29 2018-03-12 stsp
150 5c860e29 2018-03-12 stsp argc -= optind;
151 5c860e29 2018-03-12 stsp argv += optind;
152 1e70621d 2018-03-27 stsp optind = 0;
153 5c860e29 2018-03-12 stsp
154 5c860e29 2018-03-12 stsp if (argc <= 0)
155 5c860e29 2018-03-12 stsp usage();
156 5c860e29 2018-03-12 stsp
157 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
158 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
159 99437157 2018-11-11 stsp
160 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
161 d7d4f210 2018-03-12 stsp const struct got_error *error;
162 d7d4f210 2018-03-12 stsp
163 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
164 5c860e29 2018-03-12 stsp
165 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
166 5c860e29 2018-03-12 stsp continue;
167 5c860e29 2018-03-12 stsp
168 1b6b95a8 2018-03-12 stsp if (hflag)
169 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
170 1b6b95a8 2018-03-12 stsp
171 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
172 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
173 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
174 d7d4f210 2018-03-12 stsp return 1;
175 d7d4f210 2018-03-12 stsp }
176 d7d4f210 2018-03-12 stsp
177 d7d4f210 2018-03-12 stsp return 0;
178 5c860e29 2018-03-12 stsp }
179 5c860e29 2018-03-12 stsp
180 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
181 5c860e29 2018-03-12 stsp return 1;
182 5c860e29 2018-03-12 stsp }
183 5c860e29 2018-03-12 stsp
184 4ed7e80c 2018-05-20 stsp __dead static void
185 5c860e29 2018-03-12 stsp usage(void)
186 5c860e29 2018-03-12 stsp {
187 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
188 5c860e29 2018-03-12 stsp exit(1);
189 5c860e29 2018-03-12 stsp }
190 5c860e29 2018-03-12 stsp
191 0266afb7 2019-01-04 stsp static const struct got_error *
192 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
193 e2ba3d07 2019-05-13 stsp {
194 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
195 e2ba3d07 2019-05-13 stsp const char *editor;
196 e2ba3d07 2019-05-13 stsp
197 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
198 e2ba3d07 2019-05-13 stsp if (editor == NULL)
199 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
200 e2ba3d07 2019-05-13 stsp
201 0ee7065d 2019-05-13 stsp if (editor) {
202 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
203 0ee7065d 2019-05-13 stsp if (err)
204 0ee7065d 2019-05-13 stsp return err;
205 0ee7065d 2019-05-13 stsp }
206 e2ba3d07 2019-05-13 stsp
207 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
208 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
209 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
210 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
211 0ee7065d 2019-05-13 stsp }
212 0ee7065d 2019-05-13 stsp
213 e2ba3d07 2019-05-13 stsp return NULL;
214 e2ba3d07 2019-05-13 stsp }
215 e2ba3d07 2019-05-13 stsp
216 e2ba3d07 2019-05-13 stsp static const struct got_error *
217 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
218 314a6357 2019-05-13 stsp const char *worktree_path, int create_worktree)
219 0266afb7 2019-01-04 stsp {
220 163ce85a 2019-05-13 stsp const struct got_error *err;
221 0266afb7 2019-01-04 stsp
222 a0937847 2019-05-11 stsp if (create_worktree) {
223 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
224 163ce85a 2019-05-13 stsp err = got_path_mkdir(worktree_path);
225 3c45a30a 2019-05-12 jcs
226 3c45a30a 2019-05-12 jcs if (errno == EEXIST) {
227 280f921b 2019-05-12 stsp if (got_path_dir_is_empty(worktree_path)) {
228 3c45a30a 2019-05-12 jcs errno = 0;
229 163ce85a 2019-05-13 stsp err = NULL;
230 3c45a30a 2019-05-12 jcs } else {
231 df056ada 2019-05-15 stsp err = got_error_path(worktree_path,
232 df056ada 2019-05-15 stsp GOT_ERR_DIR_NOT_EMPTY);
233 3c45a30a 2019-05-12 jcs }
234 3c45a30a 2019-05-12 jcs }
235 3c45a30a 2019-05-12 jcs
236 163ce85a 2019-05-13 stsp if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
237 163ce85a 2019-05-13 stsp return err;
238 a0937847 2019-05-11 stsp }
239 a0937847 2019-05-11 stsp
240 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
241 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
242 0266afb7 2019-01-04 stsp
243 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
244 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
245 0266afb7 2019-01-04 stsp
246 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
247 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
248 0266afb7 2019-01-04 stsp
249 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
250 163ce85a 2019-05-13 stsp if (err != NULL)
251 163ce85a 2019-05-13 stsp return err;
252 0266afb7 2019-01-04 stsp
253 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
254 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
255 0266afb7 2019-01-04 stsp
256 0266afb7 2019-01-04 stsp return NULL;
257 2c7829a4 2019-06-17 stsp }
258 2c7829a4 2019-06-17 stsp
259 2c7829a4 2019-06-17 stsp __dead static void
260 2c7829a4 2019-06-17 stsp usage_init(void)
261 2c7829a4 2019-06-17 stsp {
262 2c7829a4 2019-06-17 stsp fprintf(stderr, "usage: %s init path\n", getprogname());
263 2c7829a4 2019-06-17 stsp exit(1);
264 2c7829a4 2019-06-17 stsp }
265 2c7829a4 2019-06-17 stsp
266 2c7829a4 2019-06-17 stsp static const struct got_error *
267 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
268 2c7829a4 2019-06-17 stsp {
269 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
270 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
271 2c7829a4 2019-06-17 stsp int ch;
272 2c7829a4 2019-06-17 stsp
273 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
274 2c7829a4 2019-06-17 stsp switch (ch) {
275 2c7829a4 2019-06-17 stsp default:
276 2c7829a4 2019-06-17 stsp usage_init();
277 2c7829a4 2019-06-17 stsp /* NOTREACHED */
278 2c7829a4 2019-06-17 stsp }
279 2c7829a4 2019-06-17 stsp }
280 2c7829a4 2019-06-17 stsp
281 2c7829a4 2019-06-17 stsp argc -= optind;
282 2c7829a4 2019-06-17 stsp argv += optind;
283 2c7829a4 2019-06-17 stsp
284 2c7829a4 2019-06-17 stsp #ifndef PROFILE
285 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
286 2c7829a4 2019-06-17 stsp err(1, "pledge");
287 2c7829a4 2019-06-17 stsp #endif
288 2c7829a4 2019-06-17 stsp if (argc != 1)
289 bc20e173 2019-06-17 stsp usage_init();
290 2c7829a4 2019-06-17 stsp
291 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
292 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
293 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
294 2c7829a4 2019-06-17 stsp
295 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
296 2c7829a4 2019-06-17 stsp
297 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
298 2c7829a4 2019-06-17 stsp if (error &&
299 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
300 2c7829a4 2019-06-17 stsp goto done;
301 2c7829a4 2019-06-17 stsp
302 2c7829a4 2019-06-17 stsp error = apply_unveil(repo_path, 0, NULL, 0);
303 2c7829a4 2019-06-17 stsp if (error)
304 2c7829a4 2019-06-17 stsp goto done;
305 2c7829a4 2019-06-17 stsp
306 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
307 2c7829a4 2019-06-17 stsp if (error != NULL)
308 2c7829a4 2019-06-17 stsp goto done;
309 2c7829a4 2019-06-17 stsp
310 2c7829a4 2019-06-17 stsp done:
311 2c7829a4 2019-06-17 stsp free(repo_path);
312 2c7829a4 2019-06-17 stsp return error;
313 0266afb7 2019-01-04 stsp }
314 0266afb7 2019-01-04 stsp
315 4ed7e80c 2018-05-20 stsp __dead static void
316 c09a553d 2018-03-12 stsp usage_checkout(void)
317 c09a553d 2018-03-12 stsp {
318 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
319 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
320 c09a553d 2018-03-12 stsp exit(1);
321 92a684f4 2018-03-12 stsp }
322 92a684f4 2018-03-12 stsp
323 92a684f4 2018-03-12 stsp static void
324 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
325 92a684f4 2018-03-12 stsp {
326 92a684f4 2018-03-12 stsp char *worktree_path = arg;
327 a484d721 2019-06-10 stsp
328 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
329 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
330 a484d721 2019-06-10 stsp return;
331 92a684f4 2018-03-12 stsp
332 92a684f4 2018-03-12 stsp while (path[0] == '/')
333 92a684f4 2018-03-12 stsp path++;
334 92a684f4 2018-03-12 stsp
335 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
336 99437157 2018-11-11 stsp }
337 99437157 2018-11-11 stsp
338 99437157 2018-11-11 stsp static const struct got_error *
339 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
340 99437157 2018-11-11 stsp {
341 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
342 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
343 99437157 2018-11-11 stsp return NULL;
344 8069f636 2019-01-12 stsp }
345 8069f636 2019-01-12 stsp
346 8069f636 2019-01-12 stsp static const struct got_error *
347 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
348 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
349 8069f636 2019-01-12 stsp {
350 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
351 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
352 8069f636 2019-01-12 stsp
353 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
354 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
355 36a38700 2019-05-10 stsp if (err)
356 36a38700 2019-05-10 stsp return err;
357 8069f636 2019-01-12 stsp
358 d5bea539 2019-05-13 stsp if (yca_id == NULL)
359 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
360 8069f636 2019-01-12 stsp
361 d5bea539 2019-05-13 stsp /*
362 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
363 d5bea539 2019-05-13 stsp * and the work tree's base commit.
364 d5bea539 2019-05-13 stsp *
365 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
366 d5bea539 2019-05-13 stsp *
367 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
368 d5bea539 2019-05-13 stsp * \ /
369 d5bea539 2019-05-13 stsp * C E
370 d5bea539 2019-05-13 stsp * \ /
371 d5bea539 2019-05-13 stsp * B (yca)
372 d5bea539 2019-05-13 stsp * |
373 d5bea539 2019-05-13 stsp * A
374 d5bea539 2019-05-13 stsp *
375 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
376 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
377 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
378 d5bea539 2019-05-13 stsp */
379 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
380 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
381 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
382 8069f636 2019-01-12 stsp
383 d5bea539 2019-05-13 stsp free(yca_id);
384 d5bea539 2019-05-13 stsp return NULL;
385 c09a553d 2018-03-12 stsp }
386 a367ff0f 2019-05-14 stsp
387 a367ff0f 2019-05-14 stsp static const struct got_error *
388 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
389 a367ff0f 2019-05-14 stsp struct got_reference *head_ref, struct got_repository *repo)
390 a367ff0f 2019-05-14 stsp {
391 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
392 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
393 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
394 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
395 a367ff0f 2019-05-14 stsp
396 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
397 a367ff0f 2019-05-14 stsp if (err)
398 a367ff0f 2019-05-14 stsp goto done;
399 a367ff0f 2019-05-14 stsp
400 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
401 a367ff0f 2019-05-14 stsp if (err)
402 a367ff0f 2019-05-14 stsp goto done;
403 a367ff0f 2019-05-14 stsp
404 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
405 a367ff0f 2019-05-14 stsp if (err)
406 a367ff0f 2019-05-14 stsp goto done;
407 a367ff0f 2019-05-14 stsp
408 a367ff0f 2019-05-14 stsp for (;;) {
409 a367ff0f 2019-05-14 stsp struct got_object_id *id;
410 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
411 a367ff0f 2019-05-14 stsp if (err) {
412 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
413 a367ff0f 2019-05-14 stsp err = NULL;
414 a367ff0f 2019-05-14 stsp break;
415 a367ff0f 2019-05-14 stsp }
416 a367ff0f 2019-05-14 stsp else if (err->code != GOT_ERR_ITER_NEED_MORE)
417 a367ff0f 2019-05-14 stsp break;
418 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
419 a367ff0f 2019-05-14 stsp repo);
420 a367ff0f 2019-05-14 stsp if (err)
421 a367ff0f 2019-05-14 stsp break;
422 a367ff0f 2019-05-14 stsp }
423 c09a553d 2018-03-12 stsp
424 a367ff0f 2019-05-14 stsp if (id) {
425 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
426 a367ff0f 2019-05-14 stsp is_same_branch = 1;
427 a367ff0f 2019-05-14 stsp break;
428 a367ff0f 2019-05-14 stsp }
429 a367ff0f 2019-05-14 stsp }
430 a367ff0f 2019-05-14 stsp }
431 a367ff0f 2019-05-14 stsp done:
432 a367ff0f 2019-05-14 stsp if (graph)
433 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
434 a367ff0f 2019-05-14 stsp free(head_commit_id);
435 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
436 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
437 a367ff0f 2019-05-14 stsp return err;
438 a367ff0f 2019-05-14 stsp }
439 8069f636 2019-01-12 stsp
440 4ed7e80c 2018-05-20 stsp static const struct got_error *
441 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
442 c09a553d 2018-03-12 stsp {
443 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
444 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
445 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
446 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
447 c09a553d 2018-03-12 stsp char *repo_path = NULL;
448 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
449 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
450 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
451 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
452 72151b04 2019-05-11 stsp int ch, same_path_prefix;
453 c09a553d 2018-03-12 stsp
454 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
455 0bb8a95e 2018-03-12 stsp switch (ch) {
456 08573d5b 2019-05-14 stsp case 'b':
457 08573d5b 2019-05-14 stsp branch_name = optarg;
458 08573d5b 2019-05-14 stsp break;
459 8069f636 2019-01-12 stsp case 'c':
460 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
461 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
462 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
463 8069f636 2019-01-12 stsp break;
464 0bb8a95e 2018-03-12 stsp case 'p':
465 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
466 0bb8a95e 2018-03-12 stsp break;
467 0bb8a95e 2018-03-12 stsp default:
468 2deda0b9 2019-03-07 stsp usage_checkout();
469 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
470 0bb8a95e 2018-03-12 stsp }
471 0bb8a95e 2018-03-12 stsp }
472 0bb8a95e 2018-03-12 stsp
473 0bb8a95e 2018-03-12 stsp argc -= optind;
474 0bb8a95e 2018-03-12 stsp argv += optind;
475 0bb8a95e 2018-03-12 stsp
476 6715a751 2018-03-16 stsp #ifndef PROFILE
477 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
478 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
479 c09a553d 2018-03-12 stsp err(1, "pledge");
480 6715a751 2018-03-16 stsp #endif
481 0bb8a95e 2018-03-12 stsp if (argc == 1) {
482 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
483 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
484 76089277 2018-04-01 stsp if (repo_path == NULL)
485 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
486 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
487 76089277 2018-04-01 stsp if (cwd == NULL) {
488 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
489 76089277 2018-04-01 stsp goto done;
490 76089277 2018-04-01 stsp }
491 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
492 230a42bd 2019-05-11 jcs base = basename(path_prefix);
493 230a42bd 2019-05-11 jcs if (base == NULL) {
494 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
495 230a42bd 2019-05-11 jcs path_prefix);
496 230a42bd 2019-05-11 jcs goto done;
497 230a42bd 2019-05-11 jcs }
498 230a42bd 2019-05-11 jcs } else {
499 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
500 230a42bd 2019-05-11 jcs if (base == NULL) {
501 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
502 230a42bd 2019-05-11 jcs repo_path);
503 230a42bd 2019-05-11 jcs goto done;
504 230a42bd 2019-05-11 jcs }
505 76089277 2018-04-01 stsp }
506 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
507 c09a553d 2018-03-12 stsp if (dotgit)
508 c09a553d 2018-03-12 stsp *dotgit = '\0';
509 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
510 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
511 c09a553d 2018-03-12 stsp free(cwd);
512 76089277 2018-04-01 stsp goto done;
513 c09a553d 2018-03-12 stsp }
514 c09a553d 2018-03-12 stsp free(cwd);
515 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
516 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
517 76089277 2018-04-01 stsp if (repo_path == NULL) {
518 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
519 76089277 2018-04-01 stsp goto done;
520 76089277 2018-04-01 stsp }
521 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
522 76089277 2018-04-01 stsp if (worktree_path == NULL) {
523 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[1]);
524 76089277 2018-04-01 stsp goto done;
525 76089277 2018-04-01 stsp }
526 c09a553d 2018-03-12 stsp } else
527 c09a553d 2018-03-12 stsp usage_checkout();
528 c09a553d 2018-03-12 stsp
529 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
530 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
531 13bfb272 2019-05-10 jcs
532 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
533 c09a553d 2018-03-12 stsp if (error != NULL)
534 c02c541e 2019-03-29 stsp goto done;
535 c02c541e 2019-03-29 stsp
536 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
537 c02c541e 2019-03-29 stsp if (error)
538 c09a553d 2018-03-12 stsp goto done;
539 8069f636 2019-01-12 stsp
540 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
541 c09a553d 2018-03-12 stsp if (error != NULL)
542 c09a553d 2018-03-12 stsp goto done;
543 c09a553d 2018-03-12 stsp
544 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
545 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
546 c09a553d 2018-03-12 stsp goto done;
547 c09a553d 2018-03-12 stsp
548 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
549 c09a553d 2018-03-12 stsp if (error != NULL)
550 c09a553d 2018-03-12 stsp goto done;
551 c09a553d 2018-03-12 stsp
552 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
553 e5dc7198 2018-12-29 stsp path_prefix);
554 e5dc7198 2018-12-29 stsp if (error != NULL)
555 e5dc7198 2018-12-29 stsp goto done;
556 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
557 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
558 49520a32 2018-12-29 stsp goto done;
559 49520a32 2018-12-29 stsp }
560 49520a32 2018-12-29 stsp
561 8069f636 2019-01-12 stsp if (commit_id_str) {
562 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
563 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
564 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
565 8069f636 2019-01-12 stsp if (error != NULL)
566 8069f636 2019-01-12 stsp goto done;
567 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
568 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
569 8069f636 2019-01-12 stsp if (error != NULL) {
570 8069f636 2019-01-12 stsp free(commit_id);
571 8069f636 2019-01-12 stsp goto done;
572 8069f636 2019-01-12 stsp }
573 45d344f6 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
574 45d344f6 2019-05-14 stsp if (error)
575 45d344f6 2019-05-14 stsp goto done;
576 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
577 8069f636 2019-01-12 stsp commit_id);
578 8069f636 2019-01-12 stsp free(commit_id);
579 8069f636 2019-01-12 stsp if (error)
580 8069f636 2019-01-12 stsp goto done;
581 8069f636 2019-01-12 stsp }
582 8069f636 2019-01-12 stsp
583 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
584 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
585 c09a553d 2018-03-12 stsp if (error != NULL)
586 c09a553d 2018-03-12 stsp goto done;
587 c09a553d 2018-03-12 stsp
588 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
589 c09a553d 2018-03-12 stsp
590 c09a553d 2018-03-12 stsp done:
591 8069f636 2019-01-12 stsp free(commit_id_str);
592 76089277 2018-04-01 stsp free(repo_path);
593 507dc3bb 2018-12-29 stsp free(worktree_path);
594 507dc3bb 2018-12-29 stsp return error;
595 507dc3bb 2018-12-29 stsp }
596 507dc3bb 2018-12-29 stsp
597 507dc3bb 2018-12-29 stsp __dead static void
598 507dc3bb 2018-12-29 stsp usage_update(void)
599 507dc3bb 2018-12-29 stsp {
600 024e9686 2019-05-14 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
601 507dc3bb 2018-12-29 stsp getprogname());
602 507dc3bb 2018-12-29 stsp exit(1);
603 507dc3bb 2018-12-29 stsp }
604 507dc3bb 2018-12-29 stsp
605 507dc3bb 2018-12-29 stsp static void
606 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
607 507dc3bb 2018-12-29 stsp {
608 784955db 2019-01-12 stsp int *did_something = arg;
609 784955db 2019-01-12 stsp
610 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
611 507dc3bb 2018-12-29 stsp return;
612 507dc3bb 2018-12-29 stsp
613 1545c615 2019-02-10 stsp *did_something = 1;
614 a484d721 2019-06-10 stsp
615 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
616 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
617 a484d721 2019-06-10 stsp return;
618 a484d721 2019-06-10 stsp
619 507dc3bb 2018-12-29 stsp while (path[0] == '/')
620 507dc3bb 2018-12-29 stsp path++;
621 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
622 be7061eb 2018-12-30 stsp }
623 be7061eb 2018-12-30 stsp
624 be7061eb 2018-12-30 stsp static const struct got_error *
625 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
626 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
627 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
628 a1fb16d8 2019-05-24 stsp {
629 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
630 a1fb16d8 2019-05-24 stsp char *base_id_str;
631 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
632 a1fb16d8 2019-05-24 stsp
633 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
634 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
635 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
636 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
637 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
638 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
639 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
640 a1fb16d8 2019-05-24 stsp }
641 a1fb16d8 2019-05-24 stsp
642 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
643 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
644 a1fb16d8 2019-05-24 stsp if (err) {
645 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
646 a1fb16d8 2019-05-24 stsp return err;
647 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
648 a1fb16d8 2019-05-24 stsp }
649 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
650 a1fb16d8 2019-05-24 stsp return NULL;
651 a1fb16d8 2019-05-24 stsp
652 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
653 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
654 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
655 a1fb16d8 2019-05-24 stsp if (err)
656 a1fb16d8 2019-05-24 stsp return err;
657 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
658 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
659 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
660 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
661 a1fb16d8 2019-05-24 stsp return NULL;
662 a1fb16d8 2019-05-24 stsp }
663 a1fb16d8 2019-05-24 stsp
664 a1fb16d8 2019-05-24 stsp static const struct got_error *
665 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
666 507dc3bb 2018-12-29 stsp {
667 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
668 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
669 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
670 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
671 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
672 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
673 024e9686 2019-05-14 stsp const char *branch_name = NULL;
674 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
675 784955db 2019-01-12 stsp int ch, did_something = 0;
676 507dc3bb 2018-12-29 stsp
677 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
678 507dc3bb 2018-12-29 stsp switch (ch) {
679 024e9686 2019-05-14 stsp case 'b':
680 024e9686 2019-05-14 stsp branch_name = optarg;
681 024e9686 2019-05-14 stsp break;
682 507dc3bb 2018-12-29 stsp case 'c':
683 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
684 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
685 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
686 507dc3bb 2018-12-29 stsp break;
687 507dc3bb 2018-12-29 stsp default:
688 2deda0b9 2019-03-07 stsp usage_update();
689 507dc3bb 2018-12-29 stsp /* NOTREACHED */
690 507dc3bb 2018-12-29 stsp }
691 507dc3bb 2018-12-29 stsp }
692 507dc3bb 2018-12-29 stsp
693 507dc3bb 2018-12-29 stsp argc -= optind;
694 507dc3bb 2018-12-29 stsp argv += optind;
695 507dc3bb 2018-12-29 stsp
696 507dc3bb 2018-12-29 stsp #ifndef PROFILE
697 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
698 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
699 507dc3bb 2018-12-29 stsp err(1, "pledge");
700 507dc3bb 2018-12-29 stsp #endif
701 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
702 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
703 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
704 c4cdcb68 2019-04-03 stsp goto done;
705 c4cdcb68 2019-04-03 stsp }
706 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
707 c4cdcb68 2019-04-03 stsp if (error)
708 c4cdcb68 2019-04-03 stsp goto done;
709 c4cdcb68 2019-04-03 stsp
710 507dc3bb 2018-12-29 stsp if (argc == 0) {
711 c4cdcb68 2019-04-03 stsp path = strdup("");
712 c4cdcb68 2019-04-03 stsp if (path == NULL) {
713 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
714 507dc3bb 2018-12-29 stsp goto done;
715 507dc3bb 2018-12-29 stsp }
716 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
717 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
718 c4cdcb68 2019-04-03 stsp if (error)
719 507dc3bb 2018-12-29 stsp goto done;
720 507dc3bb 2018-12-29 stsp } else
721 507dc3bb 2018-12-29 stsp usage_update();
722 507dc3bb 2018-12-29 stsp
723 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
724 507dc3bb 2018-12-29 stsp if (error != NULL)
725 507dc3bb 2018-12-29 stsp goto done;
726 507dc3bb 2018-12-29 stsp
727 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
728 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
729 0266afb7 2019-01-04 stsp if (error)
730 0266afb7 2019-01-04 stsp goto done;
731 0266afb7 2019-01-04 stsp
732 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
733 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
734 024e9686 2019-05-14 stsp if (error != NULL)
735 024e9686 2019-05-14 stsp goto done;
736 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
737 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
738 9c4b8182 2019-01-02 stsp if (error != NULL)
739 9c4b8182 2019-01-02 stsp goto done;
740 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
741 507dc3bb 2018-12-29 stsp if (error != NULL)
742 507dc3bb 2018-12-29 stsp goto done;
743 507dc3bb 2018-12-29 stsp } else {
744 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
745 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
746 507dc3bb 2018-12-29 stsp if (error != NULL)
747 507dc3bb 2018-12-29 stsp goto done;
748 507dc3bb 2018-12-29 stsp }
749 35c965b2 2018-12-29 stsp
750 a1fb16d8 2019-05-24 stsp if (branch_name) {
751 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
752 024e9686 2019-05-14 stsp if (strlen(path) != 0) {
753 a1fb16d8 2019-05-24 stsp fprintf(stderr, "%s: switching between branches "
754 a1fb16d8 2019-05-24 stsp "requires that the entire work tree "
755 024e9686 2019-05-14 stsp "gets updated, not just '%s'\n",
756 024e9686 2019-05-14 stsp getprogname(), path);
757 024e9686 2019-05-14 stsp error = got_error(GOT_ERR_BAD_PATH);
758 024e9686 2019-05-14 stsp goto done;
759 024e9686 2019-05-14 stsp }
760 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
761 024e9686 2019-05-14 stsp if (error)
762 024e9686 2019-05-14 stsp goto done;
763 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
764 a367ff0f 2019-05-14 stsp free(head_commit_id);
765 024e9686 2019-05-14 stsp if (error != NULL)
766 024e9686 2019-05-14 stsp goto done;
767 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
768 a367ff0f 2019-05-14 stsp if (error)
769 a367ff0f 2019-05-14 stsp goto done;
770 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
771 024e9686 2019-05-14 stsp if (error)
772 024e9686 2019-05-14 stsp goto done;
773 024e9686 2019-05-14 stsp } else {
774 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
775 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
776 a367ff0f 2019-05-14 stsp if (error != NULL) {
777 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
778 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
779 024e9686 2019-05-14 stsp goto done;
780 a367ff0f 2019-05-14 stsp }
781 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
782 a367ff0f 2019-05-14 stsp if (error)
783 a367ff0f 2019-05-14 stsp goto done;
784 024e9686 2019-05-14 stsp }
785 507dc3bb 2018-12-29 stsp
786 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
787 507dc3bb 2018-12-29 stsp commit_id) != 0) {
788 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
789 507dc3bb 2018-12-29 stsp commit_id);
790 507dc3bb 2018-12-29 stsp if (error)
791 507dc3bb 2018-12-29 stsp goto done;
792 507dc3bb 2018-12-29 stsp }
793 507dc3bb 2018-12-29 stsp
794 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
795 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
796 507dc3bb 2018-12-29 stsp if (error != NULL)
797 507dc3bb 2018-12-29 stsp goto done;
798 9c4b8182 2019-01-02 stsp
799 784955db 2019-01-12 stsp if (did_something)
800 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
801 784955db 2019-01-12 stsp else
802 784955db 2019-01-12 stsp printf("Already up-to-date\n");
803 507dc3bb 2018-12-29 stsp done:
804 c09a553d 2018-03-12 stsp free(worktree_path);
805 c4cdcb68 2019-04-03 stsp free(path);
806 507dc3bb 2018-12-29 stsp free(commit_id);
807 9c4b8182 2019-01-02 stsp free(commit_id_str);
808 c09a553d 2018-03-12 stsp return error;
809 c09a553d 2018-03-12 stsp }
810 c09a553d 2018-03-12 stsp
811 f42b1b34 2018-03-12 stsp static const struct got_error *
812 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
813 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
814 5c860e29 2018-03-12 stsp {
815 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
816 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
817 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
818 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
819 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
820 79109fed 2018-03-27 stsp
821 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
822 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
823 79109fed 2018-03-27 stsp if (err)
824 79109fed 2018-03-27 stsp return err;
825 79109fed 2018-03-27 stsp
826 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
827 79f35eb3 2018-06-11 stsp if (qid != NULL) {
828 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
829 79109fed 2018-03-27 stsp
830 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
831 79109fed 2018-03-27 stsp if (err)
832 79109fed 2018-03-27 stsp return err;
833 79109fed 2018-03-27 stsp
834 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
835 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
836 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
837 0f2b3dca 2018-12-22 stsp if (err)
838 0f2b3dca 2018-12-22 stsp return err;
839 0f2b3dca 2018-12-22 stsp
840 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
841 79109fed 2018-03-27 stsp if (err)
842 79109fed 2018-03-27 stsp return err;
843 79109fed 2018-03-27 stsp }
844 79109fed 2018-03-27 stsp
845 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
846 0f2b3dca 2018-12-22 stsp if (err)
847 0f2b3dca 2018-12-22 stsp goto done;
848 0f2b3dca 2018-12-22 stsp
849 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
850 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
851 aaa13589 2019-06-01 stsp arg.outfile = stdout;
852 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
853 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff, &arg);
854 0f2b3dca 2018-12-22 stsp done:
855 79109fed 2018-03-27 stsp if (tree1)
856 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
857 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
858 0f2b3dca 2018-12-22 stsp free(id_str1);
859 0f2b3dca 2018-12-22 stsp free(id_str2);
860 79109fed 2018-03-27 stsp return err;
861 79109fed 2018-03-27 stsp }
862 79109fed 2018-03-27 stsp
863 4bb494d5 2018-06-16 stsp static char *
864 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
865 6c281f94 2018-06-11 stsp {
866 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
867 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
868 6c281f94 2018-06-11 stsp if (p)
869 6c281f94 2018-06-11 stsp *p = '\0';
870 6c281f94 2018-06-11 stsp return s;
871 6c281f94 2018-06-11 stsp }
872 6c281f94 2018-06-11 stsp
873 79109fed 2018-03-27 stsp static const struct got_error *
874 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
875 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
876 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
877 79109fed 2018-03-27 stsp {
878 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
879 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
880 6c281f94 2018-06-11 stsp char datebuf[26];
881 45d799e2 2018-12-23 stsp time_t committer_time;
882 45d799e2 2018-12-23 stsp const char *author, *committer;
883 199a4027 2019-02-02 stsp char *refs_str = NULL;
884 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
885 5c860e29 2018-03-12 stsp
886 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
887 199a4027 2019-02-02 stsp char *s;
888 199a4027 2019-02-02 stsp const char *name;
889 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
890 199a4027 2019-02-02 stsp continue;
891 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
892 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
893 d9498b20 2019-02-05 stsp continue;
894 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
895 199a4027 2019-02-02 stsp name += 5;
896 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
897 7143d404 2019-03-12 stsp continue;
898 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
899 e34f9ed6 2019-02-02 stsp name += 6;
900 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
901 141c2bff 2019-02-04 stsp name += 8;
902 199a4027 2019-02-02 stsp s = refs_str;
903 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
904 199a4027 2019-02-02 stsp name) == -1) {
905 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
906 199a4027 2019-02-02 stsp free(s);
907 199a4027 2019-02-02 stsp break;
908 199a4027 2019-02-02 stsp }
909 199a4027 2019-02-02 stsp free(s);
910 199a4027 2019-02-02 stsp }
911 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
912 8bf5b3c9 2018-03-17 stsp if (err)
913 8bf5b3c9 2018-03-17 stsp return err;
914 788c352e 2018-06-16 stsp
915 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
916 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
917 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
918 832c249c 2018-06-10 stsp free(id_str);
919 d3d493d7 2019-02-21 stsp id_str = NULL;
920 d3d493d7 2019-02-21 stsp free(refs_str);
921 d3d493d7 2019-02-21 stsp refs_str = NULL;
922 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
923 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
924 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
925 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
926 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
927 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
928 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
929 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
930 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
931 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
932 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
933 3fe1abad 2018-06-10 stsp int n = 1;
934 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
935 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
936 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
937 a0603db2 2018-06-10 stsp if (err)
938 a0603db2 2018-06-10 stsp return err;
939 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
940 a0603db2 2018-06-10 stsp free(id_str);
941 a0603db2 2018-06-10 stsp }
942 a0603db2 2018-06-10 stsp }
943 832c249c 2018-06-10 stsp
944 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
945 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
946 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
947 8bf5b3c9 2018-03-17 stsp
948 621015ac 2018-07-23 stsp logmsg = logmsg0;
949 832c249c 2018-06-10 stsp do {
950 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
951 832c249c 2018-06-10 stsp if (line)
952 832c249c 2018-06-10 stsp printf(" %s\n", line);
953 832c249c 2018-06-10 stsp } while (line);
954 621015ac 2018-07-23 stsp free(logmsg0);
955 832c249c 2018-06-10 stsp
956 971751ac 2018-03-27 stsp if (show_patch) {
957 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
958 971751ac 2018-03-27 stsp if (err == 0)
959 971751ac 2018-03-27 stsp printf("\n");
960 971751ac 2018-03-27 stsp }
961 07862c20 2018-09-15 stsp
962 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
963 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
964 07862c20 2018-09-15 stsp return err;
965 f42b1b34 2018-03-12 stsp }
966 5c860e29 2018-03-12 stsp
967 f42b1b34 2018-03-12 stsp static const struct got_error *
968 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
969 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
970 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
971 f42b1b34 2018-03-12 stsp {
972 f42b1b34 2018-03-12 stsp const struct got_error *err;
973 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
974 372ccdbb 2018-06-10 stsp
975 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
976 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
977 f42b1b34 2018-03-12 stsp if (err)
978 f42b1b34 2018-03-12 stsp return err;
979 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
980 372ccdbb 2018-06-10 stsp if (err)
981 fcc85cad 2018-07-22 stsp goto done;
982 656b1f76 2019-05-11 jcs for (;;) {
983 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
984 372ccdbb 2018-06-10 stsp struct got_object_id *id;
985 8bf5b3c9 2018-03-17 stsp
986 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
987 84453469 2018-11-11 stsp break;
988 84453469 2018-11-11 stsp
989 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
990 372ccdbb 2018-06-10 stsp if (err) {
991 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
992 9ba79e04 2018-06-11 stsp err = NULL;
993 9ba79e04 2018-06-11 stsp break;
994 9ba79e04 2018-06-11 stsp }
995 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
996 372ccdbb 2018-06-10 stsp break;
997 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
998 372ccdbb 2018-06-10 stsp if (err)
999 372ccdbb 2018-06-10 stsp break;
1000 372ccdbb 2018-06-10 stsp else
1001 372ccdbb 2018-06-10 stsp continue;
1002 7e665116 2018-04-02 stsp }
1003 b43fbaa0 2018-06-11 stsp if (id == NULL)
1004 7e665116 2018-04-02 stsp break;
1005 b43fbaa0 2018-06-11 stsp
1006 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1007 b43fbaa0 2018-06-11 stsp if (err)
1008 fcc85cad 2018-07-22 stsp break;
1009 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1010 199a4027 2019-02-02 stsp refs);
1011 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1012 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1013 7e665116 2018-04-02 stsp break;
1014 31cedeaf 2018-09-15 stsp }
1015 fcc85cad 2018-07-22 stsp done:
1016 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1017 f42b1b34 2018-03-12 stsp return err;
1018 f42b1b34 2018-03-12 stsp }
1019 5c860e29 2018-03-12 stsp
1020 4ed7e80c 2018-05-20 stsp __dead static void
1021 6f3d1eb0 2018-03-12 stsp usage_log(void)
1022 6f3d1eb0 2018-03-12 stsp {
1023 499d7ecc 2019-05-22 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1024 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1025 6f3d1eb0 2018-03-12 stsp exit(1);
1026 6f3d1eb0 2018-03-12 stsp }
1027 6f3d1eb0 2018-03-12 stsp
1028 4ed7e80c 2018-05-20 stsp static const struct got_error *
1029 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1030 f42b1b34 2018-03-12 stsp {
1031 f42b1b34 2018-03-12 stsp const struct got_error *error;
1032 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1033 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1034 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1035 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1036 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1037 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1038 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1039 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1040 64a96a6d 2018-04-01 stsp const char *errstr;
1041 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1042 1b3893a2 2019-03-18 stsp
1043 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1044 5c860e29 2018-03-12 stsp
1045 6715a751 2018-03-16 stsp #ifndef PROFILE
1046 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1047 6098196c 2019-01-04 stsp NULL)
1048 ad242220 2018-09-08 stsp == -1)
1049 f42b1b34 2018-03-12 stsp err(1, "pledge");
1050 6715a751 2018-03-16 stsp #endif
1051 79109fed 2018-03-27 stsp
1052 499d7ecc 2019-05-22 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1053 79109fed 2018-03-27 stsp switch (ch) {
1054 499d7ecc 2019-05-22 stsp case 'b':
1055 499d7ecc 2019-05-22 stsp first_parent_traversal = 1;
1056 499d7ecc 2019-05-22 stsp break;
1057 79109fed 2018-03-27 stsp case 'p':
1058 79109fed 2018-03-27 stsp show_patch = 1;
1059 d142fc45 2018-04-01 stsp break;
1060 d142fc45 2018-04-01 stsp case 'c':
1061 d142fc45 2018-04-01 stsp start_commit = optarg;
1062 64a96a6d 2018-04-01 stsp break;
1063 c0cc5c62 2018-10-18 stsp case 'C':
1064 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1065 4a8520aa 2018-10-18 stsp &errstr);
1066 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1067 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1068 c0cc5c62 2018-10-18 stsp break;
1069 64a96a6d 2018-04-01 stsp case 'l':
1070 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1071 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1072 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1073 a0603db2 2018-06-10 stsp break;
1074 04ca23f4 2018-07-16 stsp case 'r':
1075 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1076 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1077 04ca23f4 2018-07-16 stsp err(1, "-r option");
1078 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1079 04ca23f4 2018-07-16 stsp break;
1080 79109fed 2018-03-27 stsp default:
1081 2deda0b9 2019-03-07 stsp usage_log();
1082 79109fed 2018-03-27 stsp /* NOTREACHED */
1083 79109fed 2018-03-27 stsp }
1084 79109fed 2018-03-27 stsp }
1085 79109fed 2018-03-27 stsp
1086 79109fed 2018-03-27 stsp argc -= optind;
1087 79109fed 2018-03-27 stsp argv += optind;
1088 f42b1b34 2018-03-12 stsp
1089 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1090 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1091 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1092 04ca23f4 2018-07-16 stsp goto done;
1093 04ca23f4 2018-07-16 stsp }
1094 cffc0aa4 2019-02-05 stsp
1095 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1096 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1097 cffc0aa4 2019-02-05 stsp goto done;
1098 cffc0aa4 2019-02-05 stsp error = NULL;
1099 cffc0aa4 2019-02-05 stsp
1100 e7301579 2019-03-18 stsp if (argc == 0) {
1101 cbd1af7a 2019-03-18 stsp path = strdup("");
1102 e7301579 2019-03-18 stsp if (path == NULL) {
1103 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1104 cbd1af7a 2019-03-18 stsp goto done;
1105 e7301579 2019-03-18 stsp }
1106 e7301579 2019-03-18 stsp } else if (argc == 1) {
1107 e7301579 2019-03-18 stsp if (worktree) {
1108 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1109 e7301579 2019-03-18 stsp argv[0]);
1110 e7301579 2019-03-18 stsp if (error)
1111 e7301579 2019-03-18 stsp goto done;
1112 e7301579 2019-03-18 stsp } else {
1113 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1114 e7301579 2019-03-18 stsp if (path == NULL) {
1115 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1116 e7301579 2019-03-18 stsp goto done;
1117 e7301579 2019-03-18 stsp }
1118 e7301579 2019-03-18 stsp }
1119 cbd1af7a 2019-03-18 stsp } else
1120 cbd1af7a 2019-03-18 stsp usage_log();
1121 cbd1af7a 2019-03-18 stsp
1122 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1123 5486daa2 2019-05-11 stsp repo_path = worktree ?
1124 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1125 5486daa2 2019-05-11 stsp }
1126 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1127 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1128 cffc0aa4 2019-02-05 stsp goto done;
1129 04ca23f4 2018-07-16 stsp }
1130 6098196c 2019-01-04 stsp
1131 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1132 d7d4f210 2018-03-12 stsp if (error != NULL)
1133 04ca23f4 2018-07-16 stsp goto done;
1134 f42b1b34 2018-03-12 stsp
1135 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1136 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1137 c02c541e 2019-03-29 stsp if (error)
1138 c02c541e 2019-03-29 stsp goto done;
1139 c02c541e 2019-03-29 stsp
1140 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1141 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1142 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1143 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1144 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1145 3235492e 2018-04-01 stsp if (error != NULL)
1146 3235492e 2018-04-01 stsp return error;
1147 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1148 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1149 3235492e 2018-04-01 stsp if (error != NULL)
1150 3235492e 2018-04-01 stsp return error;
1151 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1152 3235492e 2018-04-01 stsp } else {
1153 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1154 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1155 3235492e 2018-04-01 stsp if (error == NULL) {
1156 1caad61b 2019-02-01 stsp int obj_type;
1157 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1158 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1159 d1f2edc9 2018-06-13 stsp if (error != NULL)
1160 1caad61b 2019-02-01 stsp goto done;
1161 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1162 1caad61b 2019-02-01 stsp if (error != NULL)
1163 1caad61b 2019-02-01 stsp goto done;
1164 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1165 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1166 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1167 1caad61b 2019-02-01 stsp if (error != NULL)
1168 1caad61b 2019-02-01 stsp goto done;
1169 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1170 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1171 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1172 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1173 1caad61b 2019-02-01 stsp goto done;
1174 1caad61b 2019-02-01 stsp }
1175 1caad61b 2019-02-01 stsp free(id);
1176 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1177 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1178 1caad61b 2019-02-01 stsp if (id == NULL)
1179 638f9024 2019-05-13 stsp error = got_error_from_errno(
1180 230a42bd 2019-05-11 jcs "got_object_id_dup");
1181 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1182 1caad61b 2019-02-01 stsp if (error)
1183 1caad61b 2019-02-01 stsp goto done;
1184 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1185 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1186 1caad61b 2019-02-01 stsp goto done;
1187 1caad61b 2019-02-01 stsp }
1188 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1189 d1f2edc9 2018-06-13 stsp if (error != NULL)
1190 1caad61b 2019-02-01 stsp goto done;
1191 d1f2edc9 2018-06-13 stsp }
1192 15a94983 2018-12-23 stsp if (commit == NULL) {
1193 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1194 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1195 d1f2edc9 2018-06-13 stsp if (error != NULL)
1196 d1f2edc9 2018-06-13 stsp return error;
1197 3235492e 2018-04-01 stsp }
1198 3235492e 2018-04-01 stsp }
1199 d7d4f210 2018-03-12 stsp if (error != NULL)
1200 04ca23f4 2018-07-16 stsp goto done;
1201 04ca23f4 2018-07-16 stsp
1202 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1203 04ca23f4 2018-07-16 stsp if (error != NULL)
1204 04ca23f4 2018-07-16 stsp goto done;
1205 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1206 04ca23f4 2018-07-16 stsp free(path);
1207 04ca23f4 2018-07-16 stsp path = in_repo_path;
1208 04ca23f4 2018-07-16 stsp }
1209 199a4027 2019-02-02 stsp
1210 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1211 199a4027 2019-02-02 stsp if (error)
1212 199a4027 2019-02-02 stsp goto done;
1213 04ca23f4 2018-07-16 stsp
1214 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1215 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1216 04ca23f4 2018-07-16 stsp done:
1217 04ca23f4 2018-07-16 stsp free(path);
1218 04ca23f4 2018-07-16 stsp free(repo_path);
1219 04ca23f4 2018-07-16 stsp free(cwd);
1220 f42b1b34 2018-03-12 stsp free(id);
1221 cffc0aa4 2019-02-05 stsp if (worktree)
1222 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1223 ad242220 2018-09-08 stsp if (repo) {
1224 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1225 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1226 ad242220 2018-09-08 stsp if (error == NULL)
1227 ad242220 2018-09-08 stsp error = repo_error;
1228 ad242220 2018-09-08 stsp }
1229 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1230 8bf5b3c9 2018-03-17 stsp return error;
1231 5c860e29 2018-03-12 stsp }
1232 5c860e29 2018-03-12 stsp
1233 4ed7e80c 2018-05-20 stsp __dead static void
1234 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1235 3f8b7d6a 2018-04-01 stsp {
1236 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1237 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1238 3f8b7d6a 2018-04-01 stsp exit(1);
1239 b00d56cd 2018-04-01 stsp }
1240 b00d56cd 2018-04-01 stsp
1241 b72f483a 2019-02-05 stsp struct print_diff_arg {
1242 b72f483a 2019-02-05 stsp struct got_repository *repo;
1243 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1244 b72f483a 2019-02-05 stsp int diff_context;
1245 3fc0c068 2019-02-10 stsp const char *id_str;
1246 3fc0c068 2019-02-10 stsp int header_shown;
1247 b72f483a 2019-02-05 stsp };
1248 b72f483a 2019-02-05 stsp
1249 4ed7e80c 2018-05-20 stsp static const struct got_error *
1250 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1251 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1252 b72f483a 2019-02-05 stsp {
1253 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1254 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1255 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1256 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1257 b72f483a 2019-02-05 stsp char *abspath = NULL;
1258 b72f483a 2019-02-05 stsp struct stat sb;
1259 b72f483a 2019-02-05 stsp
1260 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1261 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1262 b72f483a 2019-02-05 stsp return NULL;
1263 3fc0c068 2019-02-10 stsp
1264 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1265 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1266 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1267 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1268 3fc0c068 2019-02-10 stsp }
1269 b72f483a 2019-02-05 stsp
1270 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1271 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1272 d00136be 2019-03-26 stsp if (err)
1273 d00136be 2019-03-26 stsp goto done;
1274 b72f483a 2019-02-05 stsp
1275 d00136be 2019-03-26 stsp }
1276 d00136be 2019-03-26 stsp
1277 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1278 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1279 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1280 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1281 2ec1f75b 2019-03-26 stsp goto done;
1282 2ec1f75b 2019-03-26 stsp }
1283 b72f483a 2019-02-05 stsp
1284 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1285 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1286 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1287 2ec1f75b 2019-03-26 stsp goto done;
1288 2ec1f75b 2019-03-26 stsp }
1289 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1290 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1291 2ec1f75b 2019-03-26 stsp goto done;
1292 2ec1f75b 2019-03-26 stsp }
1293 2ec1f75b 2019-03-26 stsp } else
1294 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1295 b72f483a 2019-02-05 stsp
1296 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1297 b72f483a 2019-02-05 stsp stdout);
1298 b72f483a 2019-02-05 stsp done:
1299 b72f483a 2019-02-05 stsp if (blob1)
1300 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1301 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1302 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1303 b72f483a 2019-02-05 stsp free(abspath);
1304 927df6b7 2019-02-10 stsp return err;
1305 927df6b7 2019-02-10 stsp }
1306 927df6b7 2019-02-10 stsp
1307 927df6b7 2019-02-10 stsp static const struct got_error *
1308 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1309 b00d56cd 2018-04-01 stsp {
1310 b00d56cd 2018-04-01 stsp const struct got_error *error;
1311 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1312 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1313 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1314 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1315 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1316 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1317 15a94983 2018-12-23 stsp int type1, type2;
1318 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1319 c0cc5c62 2018-10-18 stsp const char *errstr;
1320 927df6b7 2019-02-10 stsp char *path = NULL;
1321 b00d56cd 2018-04-01 stsp
1322 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1323 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1324 25eccc22 2019-01-04 stsp NULL) == -1)
1325 b00d56cd 2018-04-01 stsp err(1, "pledge");
1326 b00d56cd 2018-04-01 stsp #endif
1327 b00d56cd 2018-04-01 stsp
1328 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1329 b00d56cd 2018-04-01 stsp switch (ch) {
1330 c0cc5c62 2018-10-18 stsp case 'C':
1331 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1332 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1333 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1334 c0cc5c62 2018-10-18 stsp break;
1335 b72f483a 2019-02-05 stsp case 'r':
1336 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1337 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1338 b72f483a 2019-02-05 stsp err(1, "-r option");
1339 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1340 b72f483a 2019-02-05 stsp break;
1341 b00d56cd 2018-04-01 stsp default:
1342 2deda0b9 2019-03-07 stsp usage_diff();
1343 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1344 b00d56cd 2018-04-01 stsp }
1345 b00d56cd 2018-04-01 stsp }
1346 b00d56cd 2018-04-01 stsp
1347 b00d56cd 2018-04-01 stsp argc -= optind;
1348 b00d56cd 2018-04-01 stsp argv += optind;
1349 b00d56cd 2018-04-01 stsp
1350 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1351 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1352 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1353 b72f483a 2019-02-05 stsp goto done;
1354 b72f483a 2019-02-05 stsp }
1355 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1356 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1357 927df6b7 2019-02-10 stsp goto done;
1358 927df6b7 2019-02-10 stsp if (argc <= 1) {
1359 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1360 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1361 927df6b7 2019-02-10 stsp goto done;
1362 927df6b7 2019-02-10 stsp }
1363 b72f483a 2019-02-05 stsp if (repo_path)
1364 b72f483a 2019-02-05 stsp errx(1,
1365 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1366 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1367 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1368 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1369 927df6b7 2019-02-10 stsp goto done;
1370 927df6b7 2019-02-10 stsp }
1371 927df6b7 2019-02-10 stsp if (argc == 1) {
1372 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1373 cbd1af7a 2019-03-18 stsp argv[0]);
1374 927df6b7 2019-02-10 stsp if (error)
1375 927df6b7 2019-02-10 stsp goto done;
1376 927df6b7 2019-02-10 stsp } else {
1377 927df6b7 2019-02-10 stsp path = strdup("");
1378 927df6b7 2019-02-10 stsp if (path == NULL) {
1379 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1380 927df6b7 2019-02-10 stsp goto done;
1381 927df6b7 2019-02-10 stsp }
1382 927df6b7 2019-02-10 stsp }
1383 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1384 15a94983 2018-12-23 stsp id_str1 = argv[0];
1385 15a94983 2018-12-23 stsp id_str2 = argv[1];
1386 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1387 30db809c 2019-06-05 stsp repo_path =
1388 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1389 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1390 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1391 30db809c 2019-06-05 stsp goto done;
1392 30db809c 2019-06-05 stsp }
1393 30db809c 2019-06-05 stsp }
1394 b00d56cd 2018-04-01 stsp } else
1395 b00d56cd 2018-04-01 stsp usage_diff();
1396 25eccc22 2019-01-04 stsp
1397 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1398 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1399 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1400 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1401 b72f483a 2019-02-05 stsp }
1402 b00d56cd 2018-04-01 stsp
1403 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1404 76089277 2018-04-01 stsp free(repo_path);
1405 b00d56cd 2018-04-01 stsp if (error != NULL)
1406 b00d56cd 2018-04-01 stsp goto done;
1407 b00d56cd 2018-04-01 stsp
1408 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1409 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1410 c02c541e 2019-03-29 stsp if (error)
1411 c02c541e 2019-03-29 stsp goto done;
1412 c02c541e 2019-03-29 stsp
1413 30db809c 2019-06-05 stsp if (argc <= 1) {
1414 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1415 b72f483a 2019-02-05 stsp char *id_str;
1416 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1417 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1418 b72f483a 2019-02-05 stsp if (error)
1419 b72f483a 2019-02-05 stsp goto done;
1420 b72f483a 2019-02-05 stsp arg.repo = repo;
1421 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1422 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1423 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1424 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1425 b72f483a 2019-02-05 stsp
1426 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1427 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1428 b72f483a 2019-02-05 stsp free(id_str);
1429 b72f483a 2019-02-05 stsp goto done;
1430 b72f483a 2019-02-05 stsp }
1431 b72f483a 2019-02-05 stsp
1432 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
1433 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1434 e02e74af 2019-05-28 stsp if (error) {
1435 e02e74af 2019-05-28 stsp struct got_reference *ref;
1436 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1437 e02e74af 2019-05-28 stsp goto done;
1438 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1439 e02e74af 2019-05-28 stsp if (error != NULL)
1440 e02e74af 2019-05-28 stsp goto done;
1441 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1442 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1443 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1444 5e70831e 2019-05-28 stsp goto done;
1445 5e70831e 2019-05-28 stsp }
1446 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1447 e02e74af 2019-05-28 stsp got_ref_close(ref);
1448 e02e74af 2019-05-28 stsp if (error != NULL)
1449 5e70831e 2019-05-28 stsp goto done;
1450 5e70831e 2019-05-28 stsp } else {
1451 5e70831e 2019-05-28 stsp label1 = strdup(id_str1);
1452 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1453 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1454 e02e74af 2019-05-28 stsp goto done;
1455 5e70831e 2019-05-28 stsp }
1456 e02e74af 2019-05-28 stsp }
1457 b00d56cd 2018-04-01 stsp
1458 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
1459 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1460 e02e74af 2019-05-28 stsp if (error) {
1461 e02e74af 2019-05-28 stsp struct got_reference *ref;
1462 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1463 e02e74af 2019-05-28 stsp goto done;
1464 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1465 e02e74af 2019-05-28 stsp if (error != NULL)
1466 5e70831e 2019-05-28 stsp goto done;
1467 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1468 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1469 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1470 e02e74af 2019-05-28 stsp goto done;
1471 5e70831e 2019-05-28 stsp }
1472 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1473 e02e74af 2019-05-28 stsp got_ref_close(ref);
1474 e02e74af 2019-05-28 stsp if (error != NULL)
1475 e02e74af 2019-05-28 stsp goto done;
1476 5e70831e 2019-05-28 stsp } else {
1477 5e70831e 2019-05-28 stsp label2 = strdup(id_str2);
1478 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1479 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1480 5e70831e 2019-05-28 stsp goto done;
1481 5e70831e 2019-05-28 stsp }
1482 e02e74af 2019-05-28 stsp }
1483 b00d56cd 2018-04-01 stsp
1484 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1485 15a94983 2018-12-23 stsp if (error)
1486 15a94983 2018-12-23 stsp goto done;
1487 15a94983 2018-12-23 stsp
1488 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1489 15a94983 2018-12-23 stsp if (error)
1490 15a94983 2018-12-23 stsp goto done;
1491 15a94983 2018-12-23 stsp
1492 15a94983 2018-12-23 stsp if (type1 != type2) {
1493 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1494 b00d56cd 2018-04-01 stsp goto done;
1495 b00d56cd 2018-04-01 stsp }
1496 b00d56cd 2018-04-01 stsp
1497 15a94983 2018-12-23 stsp switch (type1) {
1498 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1499 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1500 54156555 2018-12-24 stsp diff_context, repo, stdout);
1501 b00d56cd 2018-04-01 stsp break;
1502 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1503 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1504 54156555 2018-12-24 stsp diff_context, repo, stdout);
1505 b00d56cd 2018-04-01 stsp break;
1506 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1507 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
1508 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1509 c0cc5c62 2018-10-18 stsp repo, stdout);
1510 b00d56cd 2018-04-01 stsp break;
1511 b00d56cd 2018-04-01 stsp default:
1512 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1513 b00d56cd 2018-04-01 stsp }
1514 b00d56cd 2018-04-01 stsp
1515 b00d56cd 2018-04-01 stsp done:
1516 5e70831e 2019-05-28 stsp free(label1);
1517 5e70831e 2019-05-28 stsp free(label2);
1518 15a94983 2018-12-23 stsp free(id1);
1519 15a94983 2018-12-23 stsp free(id2);
1520 927df6b7 2019-02-10 stsp free(path);
1521 b72f483a 2019-02-05 stsp if (worktree)
1522 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1523 ad242220 2018-09-08 stsp if (repo) {
1524 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1525 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1526 ad242220 2018-09-08 stsp if (error == NULL)
1527 ad242220 2018-09-08 stsp error = repo_error;
1528 ad242220 2018-09-08 stsp }
1529 b00d56cd 2018-04-01 stsp return error;
1530 404c43c4 2018-06-21 stsp }
1531 404c43c4 2018-06-21 stsp
1532 404c43c4 2018-06-21 stsp __dead static void
1533 404c43c4 2018-06-21 stsp usage_blame(void)
1534 404c43c4 2018-06-21 stsp {
1535 9270e621 2019-02-05 stsp fprintf(stderr,
1536 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1537 404c43c4 2018-06-21 stsp getprogname());
1538 404c43c4 2018-06-21 stsp exit(1);
1539 b00d56cd 2018-04-01 stsp }
1540 b00d56cd 2018-04-01 stsp
1541 404c43c4 2018-06-21 stsp static const struct got_error *
1542 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1543 404c43c4 2018-06-21 stsp {
1544 404c43c4 2018-06-21 stsp const struct got_error *error;
1545 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1546 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1547 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1548 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1549 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1550 404c43c4 2018-06-21 stsp int ch;
1551 404c43c4 2018-06-21 stsp
1552 404c43c4 2018-06-21 stsp #ifndef PROFILE
1553 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1554 36e2fb66 2019-01-04 stsp NULL) == -1)
1555 404c43c4 2018-06-21 stsp err(1, "pledge");
1556 404c43c4 2018-06-21 stsp #endif
1557 404c43c4 2018-06-21 stsp
1558 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1559 404c43c4 2018-06-21 stsp switch (ch) {
1560 404c43c4 2018-06-21 stsp case 'c':
1561 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1562 404c43c4 2018-06-21 stsp break;
1563 66bea077 2018-08-02 stsp case 'r':
1564 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1565 66bea077 2018-08-02 stsp if (repo_path == NULL)
1566 66bea077 2018-08-02 stsp err(1, "-r option");
1567 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1568 66bea077 2018-08-02 stsp break;
1569 404c43c4 2018-06-21 stsp default:
1570 2deda0b9 2019-03-07 stsp usage_blame();
1571 404c43c4 2018-06-21 stsp /* NOTREACHED */
1572 404c43c4 2018-06-21 stsp }
1573 404c43c4 2018-06-21 stsp }
1574 404c43c4 2018-06-21 stsp
1575 404c43c4 2018-06-21 stsp argc -= optind;
1576 404c43c4 2018-06-21 stsp argv += optind;
1577 404c43c4 2018-06-21 stsp
1578 a39318fd 2018-08-02 stsp if (argc == 1)
1579 404c43c4 2018-06-21 stsp path = argv[0];
1580 a39318fd 2018-08-02 stsp else
1581 404c43c4 2018-06-21 stsp usage_blame();
1582 404c43c4 2018-06-21 stsp
1583 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1584 66bea077 2018-08-02 stsp if (cwd == NULL) {
1585 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1586 66bea077 2018-08-02 stsp goto done;
1587 66bea077 2018-08-02 stsp }
1588 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1589 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1590 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1591 66bea077 2018-08-02 stsp goto done;
1592 0c06baac 2019-02-05 stsp else
1593 0c06baac 2019-02-05 stsp error = NULL;
1594 0c06baac 2019-02-05 stsp if (worktree) {
1595 0c06baac 2019-02-05 stsp repo_path =
1596 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1597 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1598 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1599 0c06baac 2019-02-05 stsp if (error)
1600 0c06baac 2019-02-05 stsp goto done;
1601 0c06baac 2019-02-05 stsp } else {
1602 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1603 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1604 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1605 0c06baac 2019-02-05 stsp goto done;
1606 0c06baac 2019-02-05 stsp }
1607 66bea077 2018-08-02 stsp }
1608 66bea077 2018-08-02 stsp }
1609 36e2fb66 2019-01-04 stsp
1610 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1611 c02c541e 2019-03-29 stsp if (error != NULL)
1612 36e2fb66 2019-01-04 stsp goto done;
1613 66bea077 2018-08-02 stsp
1614 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1615 c02c541e 2019-03-29 stsp if (error)
1616 404c43c4 2018-06-21 stsp goto done;
1617 404c43c4 2018-06-21 stsp
1618 0c06baac 2019-02-05 stsp if (worktree) {
1619 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1620 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1621 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1622 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1623 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1624 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1625 6efaaa2d 2019-02-05 stsp path) == -1) {
1626 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1627 0c06baac 2019-02-05 stsp goto done;
1628 0c06baac 2019-02-05 stsp }
1629 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1630 0c06baac 2019-02-05 stsp free(p);
1631 0c06baac 2019-02-05 stsp } else {
1632 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1633 0c06baac 2019-02-05 stsp }
1634 0c06baac 2019-02-05 stsp if (error)
1635 66bea077 2018-08-02 stsp goto done;
1636 66bea077 2018-08-02 stsp
1637 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1638 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1639 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1640 404c43c4 2018-06-21 stsp if (error != NULL)
1641 66bea077 2018-08-02 stsp goto done;
1642 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1643 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
1644 404c43c4 2018-06-21 stsp if (error != NULL)
1645 66bea077 2018-08-02 stsp goto done;
1646 404c43c4 2018-06-21 stsp } else {
1647 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
1648 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1649 404c43c4 2018-06-21 stsp if (error != NULL)
1650 66bea077 2018-08-02 stsp goto done;
1651 404c43c4 2018-06-21 stsp }
1652 404c43c4 2018-06-21 stsp
1653 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1654 404c43c4 2018-06-21 stsp done:
1655 66bea077 2018-08-02 stsp free(in_repo_path);
1656 66bea077 2018-08-02 stsp free(repo_path);
1657 66bea077 2018-08-02 stsp free(cwd);
1658 404c43c4 2018-06-21 stsp free(commit_id);
1659 0c06baac 2019-02-05 stsp if (worktree)
1660 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
1661 ad242220 2018-09-08 stsp if (repo) {
1662 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1663 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1664 ad242220 2018-09-08 stsp if (error == NULL)
1665 ad242220 2018-09-08 stsp error = repo_error;
1666 ad242220 2018-09-08 stsp }
1667 404c43c4 2018-06-21 stsp return error;
1668 5de5890b 2018-10-18 stsp }
1669 5de5890b 2018-10-18 stsp
1670 5de5890b 2018-10-18 stsp __dead static void
1671 5de5890b 2018-10-18 stsp usage_tree(void)
1672 5de5890b 2018-10-18 stsp {
1673 c1669e2e 2019-01-09 stsp fprintf(stderr,
1674 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1675 5de5890b 2018-10-18 stsp getprogname());
1676 5de5890b 2018-10-18 stsp exit(1);
1677 5de5890b 2018-10-18 stsp }
1678 5de5890b 2018-10-18 stsp
1679 c1669e2e 2019-01-09 stsp static void
1680 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
1681 c1669e2e 2019-01-09 stsp const char *root_path)
1682 c1669e2e 2019-01-09 stsp {
1683 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
1684 5de5890b 2018-10-18 stsp
1685 c1669e2e 2019-01-09 stsp path += strlen(root_path);
1686 c1669e2e 2019-01-09 stsp while (path[0] == '/')
1687 c1669e2e 2019-01-09 stsp path++;
1688 c1669e2e 2019-01-09 stsp
1689 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
1690 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
1691 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1692 c1669e2e 2019-01-09 stsp }
1693 c1669e2e 2019-01-09 stsp
1694 5de5890b 2018-10-18 stsp static const struct got_error *
1695 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1696 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
1697 c1669e2e 2019-01-09 stsp struct got_repository *repo)
1698 5de5890b 2018-10-18 stsp {
1699 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1700 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1701 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1702 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1703 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1704 5de5890b 2018-10-18 stsp
1705 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1706 5de5890b 2018-10-18 stsp if (err)
1707 5de5890b 2018-10-18 stsp goto done;
1708 5de5890b 2018-10-18 stsp
1709 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1710 5de5890b 2018-10-18 stsp if (err)
1711 5de5890b 2018-10-18 stsp goto done;
1712 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1713 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1714 5de5890b 2018-10-18 stsp while (te) {
1715 5de5890b 2018-10-18 stsp char *id = NULL;
1716 84453469 2018-11-11 stsp
1717 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1718 84453469 2018-11-11 stsp break;
1719 84453469 2018-11-11 stsp
1720 5de5890b 2018-10-18 stsp if (show_ids) {
1721 5de5890b 2018-10-18 stsp char *id_str;
1722 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1723 5de5890b 2018-10-18 stsp if (err)
1724 5de5890b 2018-10-18 stsp goto done;
1725 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1726 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1727 5de5890b 2018-10-18 stsp free(id_str);
1728 5de5890b 2018-10-18 stsp goto done;
1729 5de5890b 2018-10-18 stsp }
1730 5de5890b 2018-10-18 stsp free(id_str);
1731 5de5890b 2018-10-18 stsp }
1732 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
1733 5de5890b 2018-10-18 stsp free(id);
1734 c1669e2e 2019-01-09 stsp
1735 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
1736 c1669e2e 2019-01-09 stsp char *child_path;
1737 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
1738 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
1739 c1669e2e 2019-01-09 stsp te->name) == -1) {
1740 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1741 c1669e2e 2019-01-09 stsp goto done;
1742 c1669e2e 2019-01-09 stsp }
1743 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
1744 c1669e2e 2019-01-09 stsp root_path, repo);
1745 c1669e2e 2019-01-09 stsp free(child_path);
1746 c1669e2e 2019-01-09 stsp if (err)
1747 c1669e2e 2019-01-09 stsp goto done;
1748 c1669e2e 2019-01-09 stsp }
1749 c1669e2e 2019-01-09 stsp
1750 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
1751 5de5890b 2018-10-18 stsp }
1752 5de5890b 2018-10-18 stsp done:
1753 5de5890b 2018-10-18 stsp if (tree)
1754 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1755 5de5890b 2018-10-18 stsp free(tree_id);
1756 5de5890b 2018-10-18 stsp return err;
1757 404c43c4 2018-06-21 stsp }
1758 404c43c4 2018-06-21 stsp
1759 5de5890b 2018-10-18 stsp static const struct got_error *
1760 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1761 5de5890b 2018-10-18 stsp {
1762 5de5890b 2018-10-18 stsp const struct got_error *error;
1763 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1764 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
1765 7a2c19d6 2019-02-05 stsp const char *path;
1766 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1767 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1768 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1769 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
1770 5de5890b 2018-10-18 stsp int ch;
1771 5de5890b 2018-10-18 stsp
1772 5de5890b 2018-10-18 stsp #ifndef PROFILE
1773 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1774 0f8d269b 2019-01-04 stsp NULL) == -1)
1775 5de5890b 2018-10-18 stsp err(1, "pledge");
1776 5de5890b 2018-10-18 stsp #endif
1777 5de5890b 2018-10-18 stsp
1778 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1779 5de5890b 2018-10-18 stsp switch (ch) {
1780 5de5890b 2018-10-18 stsp case 'c':
1781 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1782 5de5890b 2018-10-18 stsp break;
1783 5de5890b 2018-10-18 stsp case 'r':
1784 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1785 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1786 5de5890b 2018-10-18 stsp err(1, "-r option");
1787 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1788 5de5890b 2018-10-18 stsp break;
1789 5de5890b 2018-10-18 stsp case 'i':
1790 5de5890b 2018-10-18 stsp show_ids = 1;
1791 5de5890b 2018-10-18 stsp break;
1792 c1669e2e 2019-01-09 stsp case 'R':
1793 c1669e2e 2019-01-09 stsp recurse = 1;
1794 c1669e2e 2019-01-09 stsp break;
1795 5de5890b 2018-10-18 stsp default:
1796 2deda0b9 2019-03-07 stsp usage_tree();
1797 5de5890b 2018-10-18 stsp /* NOTREACHED */
1798 5de5890b 2018-10-18 stsp }
1799 5de5890b 2018-10-18 stsp }
1800 5de5890b 2018-10-18 stsp
1801 5de5890b 2018-10-18 stsp argc -= optind;
1802 5de5890b 2018-10-18 stsp argv += optind;
1803 5de5890b 2018-10-18 stsp
1804 5de5890b 2018-10-18 stsp if (argc == 1)
1805 5de5890b 2018-10-18 stsp path = argv[0];
1806 5de5890b 2018-10-18 stsp else if (argc > 1)
1807 5de5890b 2018-10-18 stsp usage_tree();
1808 5de5890b 2018-10-18 stsp else
1809 7a2c19d6 2019-02-05 stsp path = NULL;
1810 7a2c19d6 2019-02-05 stsp
1811 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
1812 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
1813 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1814 9bf7a39b 2019-02-05 stsp goto done;
1815 9bf7a39b 2019-02-05 stsp }
1816 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1817 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1818 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1819 7a2c19d6 2019-02-05 stsp goto done;
1820 7a2c19d6 2019-02-05 stsp else
1821 7a2c19d6 2019-02-05 stsp error = NULL;
1822 7a2c19d6 2019-02-05 stsp if (worktree) {
1823 7a2c19d6 2019-02-05 stsp repo_path =
1824 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1825 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
1826 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1827 7a2c19d6 2019-02-05 stsp if (error)
1828 7a2c19d6 2019-02-05 stsp goto done;
1829 7a2c19d6 2019-02-05 stsp } else {
1830 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
1831 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
1832 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1833 7a2c19d6 2019-02-05 stsp goto done;
1834 7a2c19d6 2019-02-05 stsp }
1835 5de5890b 2018-10-18 stsp }
1836 5de5890b 2018-10-18 stsp }
1837 5de5890b 2018-10-18 stsp
1838 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1839 5de5890b 2018-10-18 stsp if (error != NULL)
1840 c02c541e 2019-03-29 stsp goto done;
1841 c02c541e 2019-03-29 stsp
1842 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1843 c02c541e 2019-03-29 stsp if (error)
1844 5de5890b 2018-10-18 stsp goto done;
1845 5de5890b 2018-10-18 stsp
1846 9bf7a39b 2019-02-05 stsp if (path == NULL) {
1847 9bf7a39b 2019-02-05 stsp if (worktree) {
1848 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1849 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1850 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
1851 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
1852 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
1853 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1854 9bf7a39b 2019-02-05 stsp goto done;
1855 9bf7a39b 2019-02-05 stsp }
1856 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1857 9bf7a39b 2019-02-05 stsp free(p);
1858 9bf7a39b 2019-02-05 stsp if (error)
1859 9bf7a39b 2019-02-05 stsp goto done;
1860 9bf7a39b 2019-02-05 stsp } else
1861 9bf7a39b 2019-02-05 stsp path = "/";
1862 9bf7a39b 2019-02-05 stsp }
1863 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
1864 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1865 9bf7a39b 2019-02-05 stsp if (error != NULL)
1866 9bf7a39b 2019-02-05 stsp goto done;
1867 9bf7a39b 2019-02-05 stsp }
1868 5de5890b 2018-10-18 stsp
1869 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1870 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1871 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1872 5de5890b 2018-10-18 stsp if (error != NULL)
1873 5de5890b 2018-10-18 stsp goto done;
1874 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1875 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1876 5de5890b 2018-10-18 stsp if (error != NULL)
1877 5de5890b 2018-10-18 stsp goto done;
1878 5de5890b 2018-10-18 stsp } else {
1879 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
1880 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1881 5de5890b 2018-10-18 stsp if (error != NULL)
1882 5de5890b 2018-10-18 stsp goto done;
1883 5de5890b 2018-10-18 stsp }
1884 5de5890b 2018-10-18 stsp
1885 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1886 c1669e2e 2019-01-09 stsp in_repo_path, repo);
1887 5de5890b 2018-10-18 stsp done:
1888 5de5890b 2018-10-18 stsp free(in_repo_path);
1889 5de5890b 2018-10-18 stsp free(repo_path);
1890 5de5890b 2018-10-18 stsp free(cwd);
1891 5de5890b 2018-10-18 stsp free(commit_id);
1892 7a2c19d6 2019-02-05 stsp if (worktree)
1893 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
1894 5de5890b 2018-10-18 stsp if (repo) {
1895 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1896 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1897 5de5890b 2018-10-18 stsp if (error == NULL)
1898 5de5890b 2018-10-18 stsp error = repo_error;
1899 5de5890b 2018-10-18 stsp }
1900 5de5890b 2018-10-18 stsp return error;
1901 5de5890b 2018-10-18 stsp }
1902 5de5890b 2018-10-18 stsp
1903 6bad629b 2019-02-04 stsp __dead static void
1904 6bad629b 2019-02-04 stsp usage_status(void)
1905 6bad629b 2019-02-04 stsp {
1906 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
1907 6bad629b 2019-02-04 stsp exit(1);
1908 6bad629b 2019-02-04 stsp }
1909 5c860e29 2018-03-12 stsp
1910 b72f483a 2019-02-05 stsp static const struct got_error *
1911 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
1912 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1913 6bad629b 2019-02-04 stsp {
1914 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
1915 b72f483a 2019-02-05 stsp return NULL;
1916 6bad629b 2019-02-04 stsp }
1917 5c860e29 2018-03-12 stsp
1918 6bad629b 2019-02-04 stsp static const struct got_error *
1919 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
1920 6bad629b 2019-02-04 stsp {
1921 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
1922 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
1923 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
1924 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
1925 6bad629b 2019-02-04 stsp int ch;
1926 5c860e29 2018-03-12 stsp
1927 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
1928 6bad629b 2019-02-04 stsp switch (ch) {
1929 5c860e29 2018-03-12 stsp default:
1930 2deda0b9 2019-03-07 stsp usage_status();
1931 6bad629b 2019-02-04 stsp /* NOTREACHED */
1932 5c860e29 2018-03-12 stsp }
1933 5c860e29 2018-03-12 stsp }
1934 5c860e29 2018-03-12 stsp
1935 6bad629b 2019-02-04 stsp argc -= optind;
1936 6bad629b 2019-02-04 stsp argv += optind;
1937 5c860e29 2018-03-12 stsp
1938 6bad629b 2019-02-04 stsp #ifndef PROFILE
1939 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1940 6bad629b 2019-02-04 stsp NULL) == -1)
1941 6bad629b 2019-02-04 stsp err(1, "pledge");
1942 f42b1b34 2018-03-12 stsp #endif
1943 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
1944 927df6b7 2019-02-10 stsp if (cwd == NULL) {
1945 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1946 927df6b7 2019-02-10 stsp goto done;
1947 927df6b7 2019-02-10 stsp }
1948 927df6b7 2019-02-10 stsp
1949 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1950 927df6b7 2019-02-10 stsp if (error != NULL)
1951 927df6b7 2019-02-10 stsp goto done;
1952 927df6b7 2019-02-10 stsp
1953 6bad629b 2019-02-04 stsp if (argc == 0) {
1954 927df6b7 2019-02-10 stsp path = strdup("");
1955 927df6b7 2019-02-10 stsp if (path == NULL) {
1956 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1957 6bad629b 2019-02-04 stsp goto done;
1958 6bad629b 2019-02-04 stsp }
1959 6bad629b 2019-02-04 stsp } else if (argc == 1) {
1960 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1961 927df6b7 2019-02-10 stsp if (error)
1962 6bad629b 2019-02-04 stsp goto done;
1963 6bad629b 2019-02-04 stsp } else
1964 6bad629b 2019-02-04 stsp usage_status();
1965 6bad629b 2019-02-04 stsp
1966 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1967 6bad629b 2019-02-04 stsp if (error != NULL)
1968 6bad629b 2019-02-04 stsp goto done;
1969 6bad629b 2019-02-04 stsp
1970 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1971 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
1972 6bad629b 2019-02-04 stsp if (error)
1973 6bad629b 2019-02-04 stsp goto done;
1974 6bad629b 2019-02-04 stsp
1975 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
1976 6bad629b 2019-02-04 stsp check_cancelled, NULL);
1977 6bad629b 2019-02-04 stsp done:
1978 927df6b7 2019-02-10 stsp free(cwd);
1979 927df6b7 2019-02-10 stsp free(path);
1980 d0eebce4 2019-03-11 stsp return error;
1981 d0eebce4 2019-03-11 stsp }
1982 d0eebce4 2019-03-11 stsp
1983 d0eebce4 2019-03-11 stsp __dead static void
1984 d0eebce4 2019-03-11 stsp usage_ref(void)
1985 d0eebce4 2019-03-11 stsp {
1986 d0eebce4 2019-03-11 stsp fprintf(stderr,
1987 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
1988 d0eebce4 2019-03-11 stsp getprogname());
1989 d0eebce4 2019-03-11 stsp exit(1);
1990 d0eebce4 2019-03-11 stsp }
1991 d0eebce4 2019-03-11 stsp
1992 d0eebce4 2019-03-11 stsp static const struct got_error *
1993 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
1994 d0eebce4 2019-03-11 stsp {
1995 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
1996 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
1997 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
1998 d0eebce4 2019-03-11 stsp
1999 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2000 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2001 d0eebce4 2019-03-11 stsp if (err)
2002 d0eebce4 2019-03-11 stsp return err;
2003 d0eebce4 2019-03-11 stsp
2004 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2005 d0eebce4 2019-03-11 stsp char *refstr;
2006 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2007 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2008 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2009 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2010 d0eebce4 2019-03-11 stsp free(refstr);
2011 d0eebce4 2019-03-11 stsp }
2012 d0eebce4 2019-03-11 stsp
2013 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2014 d0eebce4 2019-03-11 stsp return NULL;
2015 d0eebce4 2019-03-11 stsp }
2016 d0eebce4 2019-03-11 stsp
2017 d0eebce4 2019-03-11 stsp static const struct got_error *
2018 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2019 d0eebce4 2019-03-11 stsp {
2020 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2021 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2022 d0eebce4 2019-03-11 stsp
2023 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2024 d0eebce4 2019-03-11 stsp if (err)
2025 d0eebce4 2019-03-11 stsp return err;
2026 d0eebce4 2019-03-11 stsp
2027 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2028 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2029 d0eebce4 2019-03-11 stsp return err;
2030 d0eebce4 2019-03-11 stsp }
2031 d0eebce4 2019-03-11 stsp
2032 d0eebce4 2019-03-11 stsp static const struct got_error *
2033 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2034 d0eebce4 2019-03-11 stsp {
2035 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2036 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2037 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2038 d0eebce4 2019-03-11 stsp
2039 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2040 dd88155e 2019-06-29 stsp repo);
2041 d83d9d5c 2019-05-13 stsp if (err) {
2042 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2043 d0eebce4 2019-03-11 stsp
2044 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2045 d83d9d5c 2019-05-13 stsp return err;
2046 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2047 d83d9d5c 2019-05-13 stsp if (err)
2048 d83d9d5c 2019-05-13 stsp return err;
2049 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2050 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2051 d83d9d5c 2019-05-13 stsp if (err)
2052 d83d9d5c 2019-05-13 stsp return err;
2053 d83d9d5c 2019-05-13 stsp }
2054 d83d9d5c 2019-05-13 stsp
2055 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2056 d0eebce4 2019-03-11 stsp if (err)
2057 d0eebce4 2019-03-11 stsp goto done;
2058 d0eebce4 2019-03-11 stsp
2059 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2060 d0eebce4 2019-03-11 stsp done:
2061 d0eebce4 2019-03-11 stsp if (ref)
2062 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2063 d0eebce4 2019-03-11 stsp free(id);
2064 d0eebce4 2019-03-11 stsp return err;
2065 d0eebce4 2019-03-11 stsp }
2066 d0eebce4 2019-03-11 stsp
2067 d0eebce4 2019-03-11 stsp static const struct got_error *
2068 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2069 d0eebce4 2019-03-11 stsp {
2070 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2071 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2072 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2073 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2074 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2075 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2076 d0eebce4 2019-03-11 stsp
2077 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2078 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2079 d0eebce4 2019-03-11 stsp switch (ch) {
2080 d0eebce4 2019-03-11 stsp case 'd':
2081 d0eebce4 2019-03-11 stsp delref = optarg;
2082 d0eebce4 2019-03-11 stsp break;
2083 d0eebce4 2019-03-11 stsp case 'r':
2084 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2085 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2086 d0eebce4 2019-03-11 stsp err(1, "-r option");
2087 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2088 d0eebce4 2019-03-11 stsp break;
2089 d0eebce4 2019-03-11 stsp case 'l':
2090 d0eebce4 2019-03-11 stsp do_list = 1;
2091 d0eebce4 2019-03-11 stsp break;
2092 d0eebce4 2019-03-11 stsp default:
2093 d0eebce4 2019-03-11 stsp usage_ref();
2094 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2095 d0eebce4 2019-03-11 stsp }
2096 d0eebce4 2019-03-11 stsp }
2097 d0eebce4 2019-03-11 stsp
2098 d0eebce4 2019-03-11 stsp if (do_list && delref)
2099 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2100 d0eebce4 2019-03-11 stsp
2101 d0eebce4 2019-03-11 stsp argc -= optind;
2102 d0eebce4 2019-03-11 stsp argv += optind;
2103 d0eebce4 2019-03-11 stsp
2104 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2105 d0eebce4 2019-03-11 stsp if (argc > 0)
2106 d0eebce4 2019-03-11 stsp usage_ref();
2107 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2108 d0eebce4 2019-03-11 stsp usage_ref();
2109 d0eebce4 2019-03-11 stsp
2110 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2111 e0b57350 2019-03-12 stsp if (do_list) {
2112 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2113 e0b57350 2019-03-12 stsp NULL) == -1)
2114 e0b57350 2019-03-12 stsp err(1, "pledge");
2115 e0b57350 2019-03-12 stsp } else {
2116 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2117 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2118 e0b57350 2019-03-12 stsp err(1, "pledge");
2119 e0b57350 2019-03-12 stsp }
2120 d0eebce4 2019-03-11 stsp #endif
2121 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2122 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2123 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2124 d0eebce4 2019-03-11 stsp goto done;
2125 d0eebce4 2019-03-11 stsp }
2126 d0eebce4 2019-03-11 stsp
2127 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2128 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2129 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2130 d0eebce4 2019-03-11 stsp goto done;
2131 d0eebce4 2019-03-11 stsp else
2132 d0eebce4 2019-03-11 stsp error = NULL;
2133 d0eebce4 2019-03-11 stsp if (worktree) {
2134 d0eebce4 2019-03-11 stsp repo_path =
2135 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2136 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2137 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2138 d0eebce4 2019-03-11 stsp if (error)
2139 d0eebce4 2019-03-11 stsp goto done;
2140 d0eebce4 2019-03-11 stsp } else {
2141 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2142 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2143 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2144 d0eebce4 2019-03-11 stsp goto done;
2145 d0eebce4 2019-03-11 stsp }
2146 d0eebce4 2019-03-11 stsp }
2147 d0eebce4 2019-03-11 stsp }
2148 d0eebce4 2019-03-11 stsp
2149 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2150 d0eebce4 2019-03-11 stsp if (error != NULL)
2151 d0eebce4 2019-03-11 stsp goto done;
2152 d0eebce4 2019-03-11 stsp
2153 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2154 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2155 c02c541e 2019-03-29 stsp if (error)
2156 c02c541e 2019-03-29 stsp goto done;
2157 c02c541e 2019-03-29 stsp
2158 d0eebce4 2019-03-11 stsp if (do_list)
2159 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2160 d0eebce4 2019-03-11 stsp else if (delref)
2161 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2162 d0eebce4 2019-03-11 stsp else
2163 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2164 4e759de4 2019-06-26 stsp done:
2165 4e759de4 2019-06-26 stsp if (repo)
2166 4e759de4 2019-06-26 stsp got_repo_close(repo);
2167 4e759de4 2019-06-26 stsp if (worktree)
2168 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2169 4e759de4 2019-06-26 stsp free(cwd);
2170 4e759de4 2019-06-26 stsp free(repo_path);
2171 4e759de4 2019-06-26 stsp return error;
2172 4e759de4 2019-06-26 stsp }
2173 4e759de4 2019-06-26 stsp
2174 4e759de4 2019-06-26 stsp __dead static void
2175 4e759de4 2019-06-26 stsp usage_branch(void)
2176 4e759de4 2019-06-26 stsp {
2177 4e759de4 2019-06-26 stsp fprintf(stderr,
2178 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2179 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2180 4e759de4 2019-06-26 stsp exit(1);
2181 4e759de4 2019-06-26 stsp }
2182 4e759de4 2019-06-26 stsp
2183 4e759de4 2019-06-26 stsp static const struct got_error *
2184 4e759de4 2019-06-26 stsp list_branches(struct got_repository *repo)
2185 4e759de4 2019-06-26 stsp {
2186 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2187 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2188 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2189 4e759de4 2019-06-26 stsp
2190 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2191 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2192 4e759de4 2019-06-26 stsp if (err)
2193 4e759de4 2019-06-26 stsp return err;
2194 4e759de4 2019-06-26 stsp
2195 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2196 4e759de4 2019-06-26 stsp const char *refname;
2197 4e759de4 2019-06-26 stsp char *refstr;
2198 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2199 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2200 4e759de4 2019-06-26 stsp continue;
2201 4e759de4 2019-06-26 stsp refname += 11;
2202 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2203 4e759de4 2019-06-26 stsp if (refstr == NULL)
2204 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2205 4e759de4 2019-06-26 stsp printf("%s: %s\n", refname, refstr);
2206 4e759de4 2019-06-26 stsp free(refstr);
2207 4e759de4 2019-06-26 stsp }
2208 4e759de4 2019-06-26 stsp
2209 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2210 4e759de4 2019-06-26 stsp return NULL;
2211 4e759de4 2019-06-26 stsp }
2212 4e759de4 2019-06-26 stsp
2213 4e759de4 2019-06-26 stsp static const struct got_error *
2214 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2215 4e759de4 2019-06-26 stsp {
2216 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2217 4e759de4 2019-06-26 stsp struct got_reference *ref;
2218 4e759de4 2019-06-26 stsp char *refname;
2219 4e759de4 2019-06-26 stsp
2220 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2221 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2222 4e759de4 2019-06-26 stsp
2223 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2224 4e759de4 2019-06-26 stsp if (err)
2225 4e759de4 2019-06-26 stsp goto done;
2226 4e759de4 2019-06-26 stsp
2227 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2228 4e759de4 2019-06-26 stsp got_ref_close(ref);
2229 4e759de4 2019-06-26 stsp done:
2230 4e759de4 2019-06-26 stsp free(refname);
2231 4e759de4 2019-06-26 stsp return err;
2232 4e759de4 2019-06-26 stsp }
2233 4e759de4 2019-06-26 stsp
2234 4e759de4 2019-06-26 stsp static const struct got_error *
2235 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2236 4e759de4 2019-06-26 stsp const char *base_branch)
2237 4e759de4 2019-06-26 stsp {
2238 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2239 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2240 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2241 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2242 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2243 4e759de4 2019-06-26 stsp
2244 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2245 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2246 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2247 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2248 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2249 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2250 4e759de4 2019-06-26 stsp
2251 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2252 4e759de4 2019-06-26 stsp if (err)
2253 4e759de4 2019-06-26 stsp goto done;
2254 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2255 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2256 4e759de4 2019-06-26 stsp if (err)
2257 4e759de4 2019-06-26 stsp goto done;
2258 4e759de4 2019-06-26 stsp
2259 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2260 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2261 4e759de4 2019-06-26 stsp goto done;
2262 4e759de4 2019-06-26 stsp }
2263 4e759de4 2019-06-26 stsp
2264 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2265 4e759de4 2019-06-26 stsp if (err == NULL) {
2266 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2267 4e759de4 2019-06-26 stsp goto done;
2268 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2269 4e759de4 2019-06-26 stsp goto done;
2270 4e759de4 2019-06-26 stsp
2271 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2272 4e759de4 2019-06-26 stsp if (err)
2273 4e759de4 2019-06-26 stsp goto done;
2274 4e759de4 2019-06-26 stsp
2275 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2276 d0eebce4 2019-03-11 stsp done:
2277 4e759de4 2019-06-26 stsp if (ref)
2278 4e759de4 2019-06-26 stsp got_ref_close(ref);
2279 4e759de4 2019-06-26 stsp free(id);
2280 4e759de4 2019-06-26 stsp free(base_refname);
2281 4e759de4 2019-06-26 stsp free(refname);
2282 4e759de4 2019-06-26 stsp return err;
2283 4e759de4 2019-06-26 stsp }
2284 4e759de4 2019-06-26 stsp
2285 4e759de4 2019-06-26 stsp static const struct got_error *
2286 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2287 4e759de4 2019-06-26 stsp {
2288 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2289 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2290 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2291 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2292 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2293 4e759de4 2019-06-26 stsp const char *delref = NULL;
2294 4e759de4 2019-06-26 stsp
2295 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2296 4e759de4 2019-06-26 stsp switch (ch) {
2297 4e759de4 2019-06-26 stsp case 'd':
2298 4e759de4 2019-06-26 stsp delref = optarg;
2299 4e759de4 2019-06-26 stsp break;
2300 4e759de4 2019-06-26 stsp case 'r':
2301 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2302 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2303 4e759de4 2019-06-26 stsp err(1, "-r option");
2304 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2305 4e759de4 2019-06-26 stsp break;
2306 4e759de4 2019-06-26 stsp case 'l':
2307 4e759de4 2019-06-26 stsp do_list = 1;
2308 4e759de4 2019-06-26 stsp break;
2309 4e759de4 2019-06-26 stsp default:
2310 4e759de4 2019-06-26 stsp usage_branch();
2311 4e759de4 2019-06-26 stsp /* NOTREACHED */
2312 4e759de4 2019-06-26 stsp }
2313 4e759de4 2019-06-26 stsp }
2314 4e759de4 2019-06-26 stsp
2315 4e759de4 2019-06-26 stsp if (do_list && delref)
2316 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2317 4e759de4 2019-06-26 stsp
2318 4e759de4 2019-06-26 stsp argc -= optind;
2319 4e759de4 2019-06-26 stsp argv += optind;
2320 4e759de4 2019-06-26 stsp
2321 4e759de4 2019-06-26 stsp if (do_list || delref) {
2322 4e759de4 2019-06-26 stsp if (argc > 0)
2323 4e759de4 2019-06-26 stsp usage_branch();
2324 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2325 4e759de4 2019-06-26 stsp usage_branch();
2326 4e759de4 2019-06-26 stsp
2327 4e759de4 2019-06-26 stsp #ifndef PROFILE
2328 4e759de4 2019-06-26 stsp if (do_list) {
2329 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2330 4e759de4 2019-06-26 stsp NULL) == -1)
2331 4e759de4 2019-06-26 stsp err(1, "pledge");
2332 4e759de4 2019-06-26 stsp } else {
2333 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2334 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
2335 4e759de4 2019-06-26 stsp err(1, "pledge");
2336 4e759de4 2019-06-26 stsp }
2337 4e759de4 2019-06-26 stsp #endif
2338 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
2339 4e759de4 2019-06-26 stsp if (cwd == NULL) {
2340 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
2341 4e759de4 2019-06-26 stsp goto done;
2342 4e759de4 2019-06-26 stsp }
2343 4e759de4 2019-06-26 stsp
2344 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2345 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
2346 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2347 4e759de4 2019-06-26 stsp goto done;
2348 4e759de4 2019-06-26 stsp else
2349 4e759de4 2019-06-26 stsp error = NULL;
2350 4e759de4 2019-06-26 stsp if (worktree) {
2351 4e759de4 2019-06-26 stsp repo_path =
2352 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
2353 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2354 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2355 4e759de4 2019-06-26 stsp if (error)
2356 4e759de4 2019-06-26 stsp goto done;
2357 4e759de4 2019-06-26 stsp } else {
2358 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
2359 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2360 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2361 4e759de4 2019-06-26 stsp goto done;
2362 4e759de4 2019-06-26 stsp }
2363 4e759de4 2019-06-26 stsp }
2364 4e759de4 2019-06-26 stsp }
2365 4e759de4 2019-06-26 stsp
2366 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
2367 4e759de4 2019-06-26 stsp if (error != NULL)
2368 4e759de4 2019-06-26 stsp goto done;
2369 4e759de4 2019-06-26 stsp
2370 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2371 4e759de4 2019-06-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2372 4e759de4 2019-06-26 stsp if (error)
2373 4e759de4 2019-06-26 stsp goto done;
2374 4e759de4 2019-06-26 stsp
2375 4e759de4 2019-06-26 stsp if (do_list)
2376 4e759de4 2019-06-26 stsp error = list_branches(repo);
2377 4e759de4 2019-06-26 stsp else if (delref)
2378 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
2379 4e759de4 2019-06-26 stsp else {
2380 4e759de4 2019-06-26 stsp const char *base_branch;
2381 4e759de4 2019-06-26 stsp if (argc == 1) {
2382 4e759de4 2019-06-26 stsp base_branch = worktree ?
2383 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
2384 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
2385 4e759de4 2019-06-26 stsp } else
2386 4e759de4 2019-06-26 stsp base_branch = argv[1];
2387 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
2388 4e759de4 2019-06-26 stsp }
2389 4e759de4 2019-06-26 stsp done:
2390 d0eebce4 2019-03-11 stsp if (repo)
2391 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2392 d0eebce4 2019-03-11 stsp if (worktree)
2393 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2394 d0eebce4 2019-03-11 stsp free(cwd);
2395 d0eebce4 2019-03-11 stsp free(repo_path);
2396 d00136be 2019-03-26 stsp return error;
2397 d00136be 2019-03-26 stsp }
2398 d00136be 2019-03-26 stsp
2399 d00136be 2019-03-26 stsp __dead static void
2400 d00136be 2019-03-26 stsp usage_add(void)
2401 d00136be 2019-03-26 stsp {
2402 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2403 d00136be 2019-03-26 stsp exit(1);
2404 d00136be 2019-03-26 stsp }
2405 d00136be 2019-03-26 stsp
2406 d00136be 2019-03-26 stsp static const struct got_error *
2407 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2408 d00136be 2019-03-26 stsp {
2409 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2410 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2411 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2412 1dd54920 2019-05-11 stsp char *cwd = NULL;
2413 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2414 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2415 723c305c 2019-05-11 jcs int ch, x;
2416 1dd54920 2019-05-11 stsp
2417 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2418 d00136be 2019-03-26 stsp
2419 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2420 d00136be 2019-03-26 stsp switch (ch) {
2421 d00136be 2019-03-26 stsp default:
2422 d00136be 2019-03-26 stsp usage_add();
2423 d00136be 2019-03-26 stsp /* NOTREACHED */
2424 d00136be 2019-03-26 stsp }
2425 d00136be 2019-03-26 stsp }
2426 d00136be 2019-03-26 stsp
2427 d00136be 2019-03-26 stsp argc -= optind;
2428 d00136be 2019-03-26 stsp argv += optind;
2429 d00136be 2019-03-26 stsp
2430 723c305c 2019-05-11 jcs if (argc < 1)
2431 d00136be 2019-03-26 stsp usage_add();
2432 d00136be 2019-03-26 stsp
2433 723c305c 2019-05-11 jcs /* make sure each file exists before doing anything halfway */
2434 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2435 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2436 723c305c 2019-05-11 jcs if (path == NULL) {
2437 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2438 723c305c 2019-05-11 jcs goto done;
2439 723c305c 2019-05-11 jcs }
2440 1dd54920 2019-05-11 stsp free(path);
2441 d00136be 2019-03-26 stsp }
2442 d00136be 2019-03-26 stsp
2443 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2444 d00136be 2019-03-26 stsp if (cwd == NULL) {
2445 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2446 d00136be 2019-03-26 stsp goto done;
2447 d00136be 2019-03-26 stsp }
2448 723c305c 2019-05-11 jcs
2449 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2450 d00136be 2019-03-26 stsp if (error)
2451 d00136be 2019-03-26 stsp goto done;
2452 d00136be 2019-03-26 stsp
2453 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2454 031a5338 2019-03-26 stsp if (error != NULL)
2455 031a5338 2019-03-26 stsp goto done;
2456 031a5338 2019-03-26 stsp
2457 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2458 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2459 d00136be 2019-03-26 stsp if (error)
2460 d00136be 2019-03-26 stsp goto done;
2461 d00136be 2019-03-26 stsp
2462 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2463 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2464 723c305c 2019-05-11 jcs if (path == NULL) {
2465 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2466 723c305c 2019-05-11 jcs goto done;
2467 723c305c 2019-05-11 jcs }
2468 723c305c 2019-05-11 jcs
2469 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2470 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2471 1dd54920 2019-05-11 stsp if (error) {
2472 1dd54920 2019-05-11 stsp free(path);
2473 723c305c 2019-05-11 jcs goto done;
2474 1dd54920 2019-05-11 stsp }
2475 723c305c 2019-05-11 jcs }
2476 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2477 1dd54920 2019-05-11 stsp NULL, repo);
2478 d00136be 2019-03-26 stsp done:
2479 031a5338 2019-03-26 stsp if (repo)
2480 031a5338 2019-03-26 stsp got_repo_close(repo);
2481 d00136be 2019-03-26 stsp if (worktree)
2482 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2483 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2484 1dd54920 2019-05-11 stsp free((char *)pe->path);
2485 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2486 2ec1f75b 2019-03-26 stsp free(cwd);
2487 2ec1f75b 2019-03-26 stsp return error;
2488 2ec1f75b 2019-03-26 stsp }
2489 2ec1f75b 2019-03-26 stsp
2490 2ec1f75b 2019-03-26 stsp __dead static void
2491 2ec1f75b 2019-03-26 stsp usage_rm(void)
2492 2ec1f75b 2019-03-26 stsp {
2493 cc11e7e3 2019-06-04 stsp fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2494 2ec1f75b 2019-03-26 stsp exit(1);
2495 2ec1f75b 2019-03-26 stsp }
2496 2ec1f75b 2019-03-26 stsp
2497 2ec1f75b 2019-03-26 stsp static const struct got_error *
2498 2ec1f75b 2019-03-26 stsp cmd_rm(int argc, char *argv[])
2499 2ec1f75b 2019-03-26 stsp {
2500 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2501 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2502 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2503 17ed4618 2019-06-02 stsp char *cwd = NULL;
2504 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
2505 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2506 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
2507 2ec1f75b 2019-03-26 stsp
2508 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
2509 17ed4618 2019-06-02 stsp
2510 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2511 2ec1f75b 2019-03-26 stsp switch (ch) {
2512 2ec1f75b 2019-03-26 stsp case 'f':
2513 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2514 2ec1f75b 2019-03-26 stsp break;
2515 2ec1f75b 2019-03-26 stsp default:
2516 2ec1f75b 2019-03-26 stsp usage_add();
2517 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2518 2ec1f75b 2019-03-26 stsp }
2519 2ec1f75b 2019-03-26 stsp }
2520 2ec1f75b 2019-03-26 stsp
2521 2ec1f75b 2019-03-26 stsp argc -= optind;
2522 2ec1f75b 2019-03-26 stsp argv += optind;
2523 2ec1f75b 2019-03-26 stsp
2524 17ed4618 2019-06-02 stsp if (argc < 1)
2525 2ec1f75b 2019-03-26 stsp usage_rm();
2526 2ec1f75b 2019-03-26 stsp
2527 17ed4618 2019-06-02 stsp /* make sure each file exists before doing anything halfway */
2528 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2529 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2530 17ed4618 2019-06-02 stsp if (path == NULL) {
2531 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2532 17ed4618 2019-06-02 stsp goto done;
2533 17ed4618 2019-06-02 stsp }
2534 17ed4618 2019-06-02 stsp free(path);
2535 2ec1f75b 2019-03-26 stsp }
2536 2ec1f75b 2019-03-26 stsp
2537 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2538 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2539 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2540 2ec1f75b 2019-03-26 stsp goto done;
2541 2ec1f75b 2019-03-26 stsp }
2542 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2543 2ec1f75b 2019-03-26 stsp if (error)
2544 2ec1f75b 2019-03-26 stsp goto done;
2545 2ec1f75b 2019-03-26 stsp
2546 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2547 2af4a041 2019-05-11 jcs if (error)
2548 2ec1f75b 2019-03-26 stsp goto done;
2549 2ec1f75b 2019-03-26 stsp
2550 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2551 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2552 2ec1f75b 2019-03-26 stsp if (error)
2553 2ec1f75b 2019-03-26 stsp goto done;
2554 2ec1f75b 2019-03-26 stsp
2555 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2556 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2557 17ed4618 2019-06-02 stsp if (path == NULL) {
2558 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2559 17ed4618 2019-06-02 stsp goto done;
2560 17ed4618 2019-06-02 stsp }
2561 17ed4618 2019-06-02 stsp
2562 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
2563 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2564 17ed4618 2019-06-02 stsp if (error) {
2565 17ed4618 2019-06-02 stsp free(path);
2566 17ed4618 2019-06-02 stsp goto done;
2567 17ed4618 2019-06-02 stsp }
2568 17ed4618 2019-06-02 stsp }
2569 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
2570 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
2571 a129376b 2019-03-28 stsp if (error)
2572 a129376b 2019-03-28 stsp goto done;
2573 a129376b 2019-03-28 stsp done:
2574 a129376b 2019-03-28 stsp if (repo)
2575 a129376b 2019-03-28 stsp got_repo_close(repo);
2576 a129376b 2019-03-28 stsp if (worktree)
2577 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2578 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
2579 17ed4618 2019-06-02 stsp free((char *)pe->path);
2580 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
2581 a129376b 2019-03-28 stsp free(cwd);
2582 a129376b 2019-03-28 stsp return error;
2583 a129376b 2019-03-28 stsp }
2584 a129376b 2019-03-28 stsp
2585 a129376b 2019-03-28 stsp __dead static void
2586 a129376b 2019-03-28 stsp usage_revert(void)
2587 a129376b 2019-03-28 stsp {
2588 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2589 a129376b 2019-03-28 stsp exit(1);
2590 a129376b 2019-03-28 stsp }
2591 a129376b 2019-03-28 stsp
2592 a129376b 2019-03-28 stsp static void
2593 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2594 a129376b 2019-03-28 stsp {
2595 a129376b 2019-03-28 stsp while (path[0] == '/')
2596 a129376b 2019-03-28 stsp path++;
2597 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2598 a129376b 2019-03-28 stsp }
2599 a129376b 2019-03-28 stsp
2600 a129376b 2019-03-28 stsp static const struct got_error *
2601 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2602 a129376b 2019-03-28 stsp {
2603 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2604 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2605 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2606 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2607 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
2608 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2609 e20a8b6f 2019-06-04 stsp int ch, i;
2610 a129376b 2019-03-28 stsp
2611 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
2612 e20a8b6f 2019-06-04 stsp
2613 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2614 a129376b 2019-03-28 stsp switch (ch) {
2615 a129376b 2019-03-28 stsp default:
2616 a129376b 2019-03-28 stsp usage_revert();
2617 a129376b 2019-03-28 stsp /* NOTREACHED */
2618 a129376b 2019-03-28 stsp }
2619 a129376b 2019-03-28 stsp }
2620 a129376b 2019-03-28 stsp
2621 a129376b 2019-03-28 stsp argc -= optind;
2622 a129376b 2019-03-28 stsp argv += optind;
2623 a129376b 2019-03-28 stsp
2624 e20a8b6f 2019-06-04 stsp if (argc < 1)
2625 a129376b 2019-03-28 stsp usage_revert();
2626 a129376b 2019-03-28 stsp
2627 e20a8b6f 2019-06-04 stsp for (i = 0; i < argc; i++) {
2628 e20a8b6f 2019-06-04 stsp char *path = realpath(argv[i], NULL);
2629 e20a8b6f 2019-06-04 stsp if (path == NULL) {
2630 e20a8b6f 2019-06-04 stsp error = got_error_from_errno2("realpath", argv[i]);
2631 e20a8b6f 2019-06-04 stsp goto done;
2632 e20a8b6f 2019-06-04 stsp }
2633 e20a8b6f 2019-06-04 stsp
2634 e20a8b6f 2019-06-04 stsp got_path_strip_trailing_slashes(path);
2635 e20a8b6f 2019-06-04 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2636 e20a8b6f 2019-06-04 stsp if (error) {
2637 e20a8b6f 2019-06-04 stsp free(path);
2638 e20a8b6f 2019-06-04 stsp goto done;
2639 e20a8b6f 2019-06-04 stsp }
2640 a129376b 2019-03-28 stsp }
2641 a129376b 2019-03-28 stsp
2642 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
2643 a129376b 2019-03-28 stsp if (cwd == NULL) {
2644 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2645 a129376b 2019-03-28 stsp goto done;
2646 a129376b 2019-03-28 stsp }
2647 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
2648 2ec1f75b 2019-03-26 stsp if (error)
2649 2ec1f75b 2019-03-26 stsp goto done;
2650 a129376b 2019-03-28 stsp
2651 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2652 a129376b 2019-03-28 stsp if (error != NULL)
2653 a129376b 2019-03-28 stsp goto done;
2654 a129376b 2019-03-28 stsp
2655 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2656 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2657 a129376b 2019-03-28 stsp if (error)
2658 a129376b 2019-03-28 stsp goto done;
2659 a129376b 2019-03-28 stsp
2660 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
2661 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
2662 a129376b 2019-03-28 stsp if (error)
2663 a129376b 2019-03-28 stsp goto done;
2664 2ec1f75b 2019-03-26 stsp done:
2665 2ec1f75b 2019-03-26 stsp if (repo)
2666 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
2667 2ec1f75b 2019-03-26 stsp if (worktree)
2668 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
2669 2ec1f75b 2019-03-26 stsp free(path);
2670 d00136be 2019-03-26 stsp free(cwd);
2671 6bad629b 2019-02-04 stsp return error;
2672 c4296144 2019-05-09 stsp }
2673 c4296144 2019-05-09 stsp
2674 c4296144 2019-05-09 stsp __dead static void
2675 c4296144 2019-05-09 stsp usage_commit(void)
2676 c4296144 2019-05-09 stsp {
2677 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2678 c4296144 2019-05-09 stsp exit(1);
2679 33ad4cbe 2019-05-12 jcs }
2680 33ad4cbe 2019-05-12 jcs
2681 33ad4cbe 2019-05-12 jcs int
2682 e2ba3d07 2019-05-13 stsp spawn_editor(const char *editor, const char *file)
2683 33ad4cbe 2019-05-12 jcs {
2684 33ad4cbe 2019-05-12 jcs pid_t pid;
2685 33ad4cbe 2019-05-12 jcs sig_t sighup, sigint, sigquit;
2686 33ad4cbe 2019-05-12 jcs int st = -1;
2687 33ad4cbe 2019-05-12 jcs
2688 33ad4cbe 2019-05-12 jcs sighup = signal(SIGHUP, SIG_IGN);
2689 33ad4cbe 2019-05-12 jcs sigint = signal(SIGINT, SIG_IGN);
2690 33ad4cbe 2019-05-12 jcs sigquit = signal(SIGQUIT, SIG_IGN);
2691 33ad4cbe 2019-05-12 jcs
2692 33ad4cbe 2019-05-12 jcs switch (pid = fork()) {
2693 33ad4cbe 2019-05-12 jcs case -1:
2694 33ad4cbe 2019-05-12 jcs goto doneediting;
2695 33ad4cbe 2019-05-12 jcs case 0:
2696 e2ba3d07 2019-05-13 stsp execl(editor, editor, file, (char *)NULL);
2697 33ad4cbe 2019-05-12 jcs _exit(127);
2698 33ad4cbe 2019-05-12 jcs }
2699 33ad4cbe 2019-05-12 jcs
2700 33ad4cbe 2019-05-12 jcs while (waitpid(pid, &st, 0) == -1)
2701 33ad4cbe 2019-05-12 jcs if (errno != EINTR)
2702 33ad4cbe 2019-05-12 jcs break;
2703 33ad4cbe 2019-05-12 jcs
2704 33ad4cbe 2019-05-12 jcs doneediting:
2705 33ad4cbe 2019-05-12 jcs (void)signal(SIGHUP, sighup);
2706 33ad4cbe 2019-05-12 jcs (void)signal(SIGINT, sigint);
2707 33ad4cbe 2019-05-12 jcs (void)signal(SIGQUIT, sigquit);
2708 33ad4cbe 2019-05-12 jcs
2709 33ad4cbe 2019-05-12 jcs if (!WIFEXITED(st)) {
2710 33ad4cbe 2019-05-12 jcs errno = EINTR;
2711 33ad4cbe 2019-05-12 jcs return -1;
2712 33ad4cbe 2019-05-12 jcs }
2713 33ad4cbe 2019-05-12 jcs
2714 33ad4cbe 2019-05-12 jcs return WEXITSTATUS(st);
2715 33ad4cbe 2019-05-12 jcs }
2716 33ad4cbe 2019-05-12 jcs
2717 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
2718 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
2719 e2ba3d07 2019-05-13 stsp const char *editor;
2720 e0870e44 2019-05-13 stsp const char *worktree_path;
2721 76d98825 2019-06-03 stsp const char *branch_name;
2722 314a6357 2019-05-13 stsp const char *repo_path;
2723 e0870e44 2019-05-13 stsp char *logmsg_path;
2724 e2ba3d07 2019-05-13 stsp
2725 e2ba3d07 2019-05-13 stsp };
2726 e0870e44 2019-05-13 stsp
2727 33ad4cbe 2019-05-12 jcs static const struct got_error *
2728 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2729 33ad4cbe 2019-05-12 jcs void *arg)
2730 33ad4cbe 2019-05-12 jcs {
2731 76d98825 2019-06-03 stsp char *initial_content = NULL;
2732 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
2733 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
2734 e0870e44 2019-05-13 stsp char *template = NULL;
2735 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
2736 33ad4cbe 2019-05-12 jcs char buf[1024];
2737 33ad4cbe 2019-05-12 jcs struct stat st, st2;
2738 33ad4cbe 2019-05-12 jcs FILE *fp;
2739 33ad4cbe 2019-05-12 jcs size_t len;
2740 e0870e44 2019-05-13 stsp int fd, content_changed = 0;
2741 33ad4cbe 2019-05-12 jcs
2742 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
2743 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2744 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
2745 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
2746 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
2747 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
2748 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
2749 33ad4cbe 2019-05-12 jcs return NULL;
2750 33ad4cbe 2019-05-12 jcs }
2751 33ad4cbe 2019-05-12 jcs
2752 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2753 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
2754 76d98825 2019-06-03 stsp
2755 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
2756 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
2757 76d98825 2019-06-03 stsp a->branch_name) == -1)
2758 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
2759 e0870e44 2019-05-13 stsp
2760 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2761 793c30b5 2019-05-13 stsp if (err)
2762 e0870e44 2019-05-13 stsp goto done;
2763 33ad4cbe 2019-05-12 jcs
2764 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
2765 33ad4cbe 2019-05-12 jcs
2766 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
2767 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
2768 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
2769 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
2770 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
2771 33ad4cbe 2019-05-12 jcs }
2772 33ad4cbe 2019-05-12 jcs close(fd);
2773 33ad4cbe 2019-05-12 jcs
2774 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st) == -1) {
2775 638f9024 2019-05-13 stsp err = got_error_from_errno2("stat", a->logmsg_path);
2776 33ad4cbe 2019-05-12 jcs goto done;
2777 33ad4cbe 2019-05-12 jcs }
2778 33ad4cbe 2019-05-12 jcs
2779 e0870e44 2019-05-13 stsp if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2780 638f9024 2019-05-13 stsp err = got_error_from_errno("failed spawning editor");
2781 33ad4cbe 2019-05-12 jcs goto done;
2782 33ad4cbe 2019-05-12 jcs }
2783 33ad4cbe 2019-05-12 jcs
2784 e0870e44 2019-05-13 stsp if (stat(a->logmsg_path, &st2) == -1) {
2785 638f9024 2019-05-13 stsp err = got_error_from_errno("stat");
2786 33ad4cbe 2019-05-12 jcs goto done;
2787 33ad4cbe 2019-05-12 jcs }
2788 33ad4cbe 2019-05-12 jcs
2789 33ad4cbe 2019-05-12 jcs if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2790 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2791 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2792 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2793 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2794 33ad4cbe 2019-05-12 jcs "no changes made to commit message, aborting");
2795 33ad4cbe 2019-05-12 jcs goto done;
2796 33ad4cbe 2019-05-12 jcs }
2797 33ad4cbe 2019-05-12 jcs
2798 33ad4cbe 2019-05-12 jcs *logmsg = malloc(st2.st_size + 1);
2799 fcde04d9 2019-05-13 stsp if (*logmsg == NULL) {
2800 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
2801 fcde04d9 2019-05-13 stsp goto done;
2802 fcde04d9 2019-05-13 stsp }
2803 cc79381d 2019-05-22 stsp (*logmsg)[0] = '\0';
2804 33ad4cbe 2019-05-12 jcs len = 0;
2805 33ad4cbe 2019-05-12 jcs
2806 e0870e44 2019-05-13 stsp fp = fopen(a->logmsg_path, "r");
2807 d4592c7c 2019-05-22 stsp if (fp == NULL) {
2808 d4592c7c 2019-05-22 stsp err = got_error_from_errno("fopen");
2809 d4592c7c 2019-05-22 stsp goto done;
2810 d4592c7c 2019-05-22 stsp }
2811 33ad4cbe 2019-05-12 jcs while (fgets(buf, sizeof(buf), fp) != NULL) {
2812 e0870e44 2019-05-13 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
2813 e0870e44 2019-05-13 stsp content_changed = 1;
2814 33ad4cbe 2019-05-12 jcs if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2815 78527a0a 2019-05-22 stsp continue; /* remove comments and leading empty lines */
2816 33ad4cbe 2019-05-12 jcs len = strlcat(*logmsg, buf, st2.st_size);
2817 33ad4cbe 2019-05-12 jcs }
2818 33ad4cbe 2019-05-12 jcs fclose(fp);
2819 33ad4cbe 2019-05-12 jcs
2820 33ad4cbe 2019-05-12 jcs while (len > 0 && (*logmsg)[len - 1] == '\n') {
2821 33ad4cbe 2019-05-12 jcs (*logmsg)[len - 1] = '\0';
2822 33ad4cbe 2019-05-12 jcs len--;
2823 33ad4cbe 2019-05-12 jcs }
2824 33ad4cbe 2019-05-12 jcs
2825 e0870e44 2019-05-13 stsp if (len == 0 || !content_changed) {
2826 e0870e44 2019-05-13 stsp unlink(a->logmsg_path);
2827 e0870e44 2019-05-13 stsp free(a->logmsg_path);
2828 e0870e44 2019-05-13 stsp a->logmsg_path = NULL;
2829 33ad4cbe 2019-05-12 jcs err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2830 33ad4cbe 2019-05-12 jcs "commit message cannot be empty, aborting");
2831 33ad4cbe 2019-05-12 jcs goto done;
2832 33ad4cbe 2019-05-12 jcs }
2833 33ad4cbe 2019-05-12 jcs done:
2834 76d98825 2019-06-03 stsp free(initial_content);
2835 e0870e44 2019-05-13 stsp free(template);
2836 314a6357 2019-05-13 stsp
2837 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
2838 314a6357 2019-05-13 stsp if (err == NULL)
2839 314a6357 2019-05-13 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2840 33ad4cbe 2019-05-12 jcs return err;
2841 6bad629b 2019-02-04 stsp }
2842 c4296144 2019-05-09 stsp
2843 c4296144 2019-05-09 stsp static const struct got_error *
2844 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
2845 c4296144 2019-05-09 stsp {
2846 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
2847 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
2848 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
2849 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
2850 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
2851 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
2852 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
2853 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
2854 e2ba3d07 2019-05-13 stsp char *editor = NULL;
2855 c4296144 2019-05-09 stsp int ch;
2856 c4296144 2019-05-09 stsp
2857 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
2858 c4296144 2019-05-09 stsp switch (ch) {
2859 c4296144 2019-05-09 stsp case 'm':
2860 c4296144 2019-05-09 stsp logmsg = optarg;
2861 c4296144 2019-05-09 stsp break;
2862 c4296144 2019-05-09 stsp default:
2863 c4296144 2019-05-09 stsp usage_commit();
2864 c4296144 2019-05-09 stsp /* NOTREACHED */
2865 c4296144 2019-05-09 stsp }
2866 c4296144 2019-05-09 stsp }
2867 c4296144 2019-05-09 stsp
2868 c4296144 2019-05-09 stsp argc -= optind;
2869 c4296144 2019-05-09 stsp argv += optind;
2870 c4296144 2019-05-09 stsp
2871 c4296144 2019-05-09 stsp if (argc == 1) {
2872 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
2873 c4296144 2019-05-09 stsp if (path == NULL) {
2874 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2875 c4296144 2019-05-09 stsp goto done;
2876 c4296144 2019-05-09 stsp }
2877 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
2878 c4296144 2019-05-09 stsp } else if (argc != 0)
2879 c4296144 2019-05-09 stsp usage_commit();
2880 c4296144 2019-05-09 stsp
2881 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
2882 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
2883 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2884 35bd8fed 2019-05-09 stsp goto done;
2885 35bd8fed 2019-05-09 stsp }
2886 c4296144 2019-05-09 stsp
2887 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
2888 c4296144 2019-05-09 stsp if (cwd == NULL) {
2889 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2890 c4296144 2019-05-09 stsp goto done;
2891 c4296144 2019-05-09 stsp }
2892 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
2893 c4296144 2019-05-09 stsp if (error)
2894 c4296144 2019-05-09 stsp goto done;
2895 c4296144 2019-05-09 stsp
2896 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2897 c4296144 2019-05-09 stsp if (error != NULL)
2898 c4296144 2019-05-09 stsp goto done;
2899 c4296144 2019-05-09 stsp
2900 314a6357 2019-05-13 stsp /*
2901 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
2902 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
2903 314a6357 2019-05-13 stsp */
2904 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
2905 314a6357 2019-05-13 stsp error = get_editor(&editor);
2906 314a6357 2019-05-13 stsp else
2907 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2908 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2909 c4296144 2019-05-09 stsp if (error)
2910 c4296144 2019-05-09 stsp goto done;
2911 c4296144 2019-05-09 stsp
2912 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
2913 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
2914 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2915 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2916 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2917 76d98825 2019-06-03 stsp cl_arg.branch_name += 5;
2918 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2919 76d98825 2019-06-03 stsp cl_arg.branch_name += 6;
2920 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
2921 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
2922 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2923 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2924 e0870e44 2019-05-13 stsp if (error) {
2925 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2926 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
2927 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
2928 c4296144 2019-05-09 stsp goto done;
2929 e0870e44 2019-05-13 stsp }
2930 c4296144 2019-05-09 stsp
2931 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
2932 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
2933 e0870e44 2019-05-13 stsp
2934 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
2935 c4296144 2019-05-09 stsp if (error)
2936 c4296144 2019-05-09 stsp goto done;
2937 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
2938 c4296144 2019-05-09 stsp done:
2939 c4296144 2019-05-09 stsp if (repo)
2940 c4296144 2019-05-09 stsp got_repo_close(repo);
2941 c4296144 2019-05-09 stsp if (worktree)
2942 c4296144 2019-05-09 stsp got_worktree_close(worktree);
2943 c4296144 2019-05-09 stsp free(path);
2944 c4296144 2019-05-09 stsp free(cwd);
2945 c4296144 2019-05-09 stsp free(id_str);
2946 e2ba3d07 2019-05-13 stsp free(editor);
2947 234035bc 2019-06-01 stsp return error;
2948 234035bc 2019-06-01 stsp }
2949 234035bc 2019-06-01 stsp
2950 234035bc 2019-06-01 stsp __dead static void
2951 234035bc 2019-06-01 stsp usage_cherrypick(void)
2952 234035bc 2019-06-01 stsp {
2953 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2954 234035bc 2019-06-01 stsp exit(1);
2955 234035bc 2019-06-01 stsp }
2956 234035bc 2019-06-01 stsp
2957 234035bc 2019-06-01 stsp static const struct got_error *
2958 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
2959 234035bc 2019-06-01 stsp {
2960 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
2961 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
2962 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
2963 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
2964 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
2965 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
2966 234035bc 2019-06-01 stsp struct got_object_qid *pid;
2967 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
2968 234035bc 2019-06-01 stsp int ch, did_something = 0;
2969 234035bc 2019-06-01 stsp
2970 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2971 234035bc 2019-06-01 stsp switch (ch) {
2972 234035bc 2019-06-01 stsp default:
2973 234035bc 2019-06-01 stsp usage_cherrypick();
2974 234035bc 2019-06-01 stsp /* NOTREACHED */
2975 234035bc 2019-06-01 stsp }
2976 234035bc 2019-06-01 stsp }
2977 234035bc 2019-06-01 stsp
2978 234035bc 2019-06-01 stsp argc -= optind;
2979 234035bc 2019-06-01 stsp argv += optind;
2980 234035bc 2019-06-01 stsp
2981 234035bc 2019-06-01 stsp if (argc != 1)
2982 234035bc 2019-06-01 stsp usage_cherrypick();
2983 234035bc 2019-06-01 stsp
2984 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
2985 234035bc 2019-06-01 stsp if (cwd == NULL) {
2986 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
2987 234035bc 2019-06-01 stsp goto done;
2988 234035bc 2019-06-01 stsp }
2989 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
2990 234035bc 2019-06-01 stsp if (error)
2991 234035bc 2019-06-01 stsp goto done;
2992 234035bc 2019-06-01 stsp
2993 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2994 234035bc 2019-06-01 stsp if (error != NULL)
2995 234035bc 2019-06-01 stsp goto done;
2996 234035bc 2019-06-01 stsp
2997 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2998 234035bc 2019-06-01 stsp got_worktree_get_root_path(worktree), 0);
2999 234035bc 2019-06-01 stsp if (error)
3000 234035bc 2019-06-01 stsp goto done;
3001 234035bc 2019-06-01 stsp
3002 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3003 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3004 234035bc 2019-06-01 stsp if (error != NULL) {
3005 234035bc 2019-06-01 stsp struct got_reference *ref;
3006 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3007 234035bc 2019-06-01 stsp goto done;
3008 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3009 234035bc 2019-06-01 stsp if (error != NULL)
3010 234035bc 2019-06-01 stsp goto done;
3011 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3012 234035bc 2019-06-01 stsp got_ref_close(ref);
3013 234035bc 2019-06-01 stsp if (error != NULL)
3014 234035bc 2019-06-01 stsp goto done;
3015 234035bc 2019-06-01 stsp }
3016 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3017 234035bc 2019-06-01 stsp if (error)
3018 234035bc 2019-06-01 stsp goto done;
3019 234035bc 2019-06-01 stsp
3020 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3021 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3022 234035bc 2019-06-01 stsp if (error != NULL)
3023 234035bc 2019-06-01 stsp goto done;
3024 234035bc 2019-06-01 stsp
3025 234035bc 2019-06-01 stsp error = check_same_branch(commit_id, head_ref, repo);
3026 234035bc 2019-06-01 stsp if (error) {
3027 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3028 234035bc 2019-06-01 stsp goto done;
3029 234035bc 2019-06-01 stsp error = NULL;
3030 234035bc 2019-06-01 stsp } else {
3031 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3032 234035bc 2019-06-01 stsp goto done;
3033 234035bc 2019-06-01 stsp }
3034 234035bc 2019-06-01 stsp
3035 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3036 234035bc 2019-06-01 stsp if (error)
3037 234035bc 2019-06-01 stsp goto done;
3038 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3039 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3040 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3041 03415a1a 2019-06-02 stsp NULL);
3042 234035bc 2019-06-01 stsp if (error != NULL)
3043 234035bc 2019-06-01 stsp goto done;
3044 234035bc 2019-06-01 stsp
3045 234035bc 2019-06-01 stsp if (did_something)
3046 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3047 234035bc 2019-06-01 stsp done:
3048 234035bc 2019-06-01 stsp if (commit)
3049 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3050 234035bc 2019-06-01 stsp free(commit_id_str);
3051 234035bc 2019-06-01 stsp if (head_ref)
3052 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3053 234035bc 2019-06-01 stsp if (worktree)
3054 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3055 234035bc 2019-06-01 stsp if (repo)
3056 234035bc 2019-06-01 stsp got_repo_close(repo);
3057 c4296144 2019-05-09 stsp return error;
3058 c4296144 2019-05-09 stsp }
3059 5ef14e63 2019-06-02 stsp
3060 5ef14e63 2019-06-02 stsp __dead static void
3061 5ef14e63 2019-06-02 stsp usage_backout(void)
3062 5ef14e63 2019-06-02 stsp {
3063 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3064 5ef14e63 2019-06-02 stsp exit(1);
3065 5ef14e63 2019-06-02 stsp }
3066 5ef14e63 2019-06-02 stsp
3067 5ef14e63 2019-06-02 stsp static const struct got_error *
3068 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3069 5ef14e63 2019-06-02 stsp {
3070 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3071 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3072 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3073 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3074 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3075 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3076 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3077 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3078 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3079 5ef14e63 2019-06-02 stsp
3080 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3081 5ef14e63 2019-06-02 stsp switch (ch) {
3082 5ef14e63 2019-06-02 stsp default:
3083 5ef14e63 2019-06-02 stsp usage_backout();
3084 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3085 5ef14e63 2019-06-02 stsp }
3086 5ef14e63 2019-06-02 stsp }
3087 5ef14e63 2019-06-02 stsp
3088 5ef14e63 2019-06-02 stsp argc -= optind;
3089 5ef14e63 2019-06-02 stsp argv += optind;
3090 5ef14e63 2019-06-02 stsp
3091 5ef14e63 2019-06-02 stsp if (argc != 1)
3092 5ef14e63 2019-06-02 stsp usage_backout();
3093 5ef14e63 2019-06-02 stsp
3094 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3095 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3096 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3097 5ef14e63 2019-06-02 stsp goto done;
3098 5ef14e63 2019-06-02 stsp }
3099 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3100 5ef14e63 2019-06-02 stsp if (error)
3101 5ef14e63 2019-06-02 stsp goto done;
3102 5ef14e63 2019-06-02 stsp
3103 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3104 5ef14e63 2019-06-02 stsp if (error != NULL)
3105 5ef14e63 2019-06-02 stsp goto done;
3106 5ef14e63 2019-06-02 stsp
3107 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3108 5ef14e63 2019-06-02 stsp got_worktree_get_root_path(worktree), 0);
3109 5ef14e63 2019-06-02 stsp if (error)
3110 5ef14e63 2019-06-02 stsp goto done;
3111 5ef14e63 2019-06-02 stsp
3112 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3113 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3114 5ef14e63 2019-06-02 stsp if (error != NULL) {
3115 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3116 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3117 5ef14e63 2019-06-02 stsp goto done;
3118 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3119 5ef14e63 2019-06-02 stsp if (error != NULL)
3120 5ef14e63 2019-06-02 stsp goto done;
3121 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3122 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3123 5ef14e63 2019-06-02 stsp if (error != NULL)
3124 5ef14e63 2019-06-02 stsp goto done;
3125 5ef14e63 2019-06-02 stsp }
3126 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3127 5ef14e63 2019-06-02 stsp if (error)
3128 5ef14e63 2019-06-02 stsp goto done;
3129 5ef14e63 2019-06-02 stsp
3130 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3131 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3132 5ef14e63 2019-06-02 stsp if (error != NULL)
3133 5ef14e63 2019-06-02 stsp goto done;
3134 5ef14e63 2019-06-02 stsp
3135 5ef14e63 2019-06-02 stsp error = check_same_branch(commit_id, head_ref, repo);
3136 5ef14e63 2019-06-02 stsp if (error)
3137 5ef14e63 2019-06-02 stsp goto done;
3138 5ef14e63 2019-06-02 stsp
3139 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3140 5ef14e63 2019-06-02 stsp if (error)
3141 5ef14e63 2019-06-02 stsp goto done;
3142 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3143 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3144 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3145 5ef14e63 2019-06-02 stsp goto done;
3146 5ef14e63 2019-06-02 stsp }
3147 5ef14e63 2019-06-02 stsp
3148 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3149 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3150 5ef14e63 2019-06-02 stsp if (error != NULL)
3151 5ef14e63 2019-06-02 stsp goto done;
3152 5ef14e63 2019-06-02 stsp
3153 5ef14e63 2019-06-02 stsp if (did_something)
3154 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3155 5ef14e63 2019-06-02 stsp done:
3156 5ef14e63 2019-06-02 stsp if (commit)
3157 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3158 5ef14e63 2019-06-02 stsp free(commit_id_str);
3159 5ef14e63 2019-06-02 stsp if (head_ref)
3160 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3161 5ef14e63 2019-06-02 stsp if (worktree)
3162 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
3163 5ef14e63 2019-06-02 stsp if (repo)
3164 5ef14e63 2019-06-02 stsp got_repo_close(repo);
3165 5ef14e63 2019-06-02 stsp return error;
3166 5ef14e63 2019-06-02 stsp }