Blame


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