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