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