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