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