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