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