Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 f42b1b34 2018-03-12 stsp * Copyright (c) 2018 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 f42b1b34 2018-03-12 stsp
23 5c860e29 2018-03-12 stsp #include <err.h>
24 5c860e29 2018-03-12 stsp #include <errno.h>
25 5c860e29 2018-03-12 stsp #include <locale.h>
26 99437157 2018-11-11 stsp #include <signal.h>
27 5c860e29 2018-03-12 stsp #include <stdio.h>
28 5c860e29 2018-03-12 stsp #include <stdlib.h>
29 5c860e29 2018-03-12 stsp #include <string.h>
30 5c860e29 2018-03-12 stsp #include <unistd.h>
31 c09a553d 2018-03-12 stsp #include <libgen.h>
32 c0768b0f 2018-06-10 stsp #include <time.h>
33 5c860e29 2018-03-12 stsp
34 f42b1b34 2018-03-12 stsp #include "got_error.h"
35 f42b1b34 2018-03-12 stsp #include "got_object.h"
36 5261c201 2018-04-01 stsp #include "got_reference.h"
37 f42b1b34 2018-03-12 stsp #include "got_repository.h"
38 c09a553d 2018-03-12 stsp #include "got_worktree.h"
39 79109fed 2018-03-27 stsp #include "got_diff.h"
40 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
41 404c43c4 2018-06-21 stsp #include "got_blame.h"
42 5c860e29 2018-03-12 stsp
43 5c860e29 2018-03-12 stsp #ifndef nitems
44 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 5c860e29 2018-03-12 stsp #endif
46 99437157 2018-11-11 stsp
47 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
48 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
49 99437157 2018-11-11 stsp
50 99437157 2018-11-11 stsp static void
51 99437157 2018-11-11 stsp catch_sigint(int signo)
52 99437157 2018-11-11 stsp {
53 99437157 2018-11-11 stsp sigint_received = 1;
54 99437157 2018-11-11 stsp }
55 99437157 2018-11-11 stsp
56 99437157 2018-11-11 stsp static void
57 99437157 2018-11-11 stsp catch_sigpipe(int signo)
58 99437157 2018-11-11 stsp {
59 99437157 2018-11-11 stsp sigpipe_received = 1;
60 99437157 2018-11-11 stsp }
61 5c860e29 2018-03-12 stsp
62 99437157 2018-11-11 stsp
63 5c860e29 2018-03-12 stsp struct cmd {
64 5c860e29 2018-03-12 stsp const char *cmd_name;
65 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
66 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
67 46a0db7d 2018-03-12 stsp const char *cmd_descr;
68 5c860e29 2018-03-12 stsp };
69 5c860e29 2018-03-12 stsp
70 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
71 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
72 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
73 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
74 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
75 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
76 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
77 5c860e29 2018-03-12 stsp
78 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
79 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
80 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
81 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
82 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
83 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
84 4ed7e80c 2018-05-20 stsp #ifdef notyet
85 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
86 4ed7e80c 2018-05-20 stsp #endif
87 5c860e29 2018-03-12 stsp
88 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
89 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
90 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
91 507dc3bb 2018-12-29 stsp { "update", cmd_update, usage_update,
92 507dc3bb 2018-12-29 stsp "update a work tree to a different commit" },
93 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
94 1b6b95a8 2018-03-12 stsp "show repository history" },
95 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
96 b00d56cd 2018-04-01 stsp "compare files and directories" },
97 404c43c4 2018-06-21 stsp { "blame", cmd_blame, usage_blame,
98 404c43c4 2018-06-21 stsp " show when lines in a file were changed" },
99 5de5890b 2018-10-18 stsp { "tree", cmd_tree, usage_tree,
100 5de5890b 2018-10-18 stsp " list files and directories in repository" },
101 f42b1b34 2018-03-12 stsp #ifdef notyet
102 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
103 1b6b95a8 2018-03-12 stsp "show modification status of files" },
104 f42b1b34 2018-03-12 stsp #endif
105 5c860e29 2018-03-12 stsp };
106 5c860e29 2018-03-12 stsp
107 5c860e29 2018-03-12 stsp int
108 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
109 5c860e29 2018-03-12 stsp {
110 5c860e29 2018-03-12 stsp struct cmd *cmd;
111 5c860e29 2018-03-12 stsp unsigned int i;
112 5c860e29 2018-03-12 stsp int ch;
113 1b6b95a8 2018-03-12 stsp int hflag = 0;
114 5c860e29 2018-03-12 stsp
115 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
116 5c860e29 2018-03-12 stsp
117 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
118 5c860e29 2018-03-12 stsp switch (ch) {
119 1b6b95a8 2018-03-12 stsp case 'h':
120 1b6b95a8 2018-03-12 stsp hflag = 1;
121 1b6b95a8 2018-03-12 stsp break;
122 5c860e29 2018-03-12 stsp default:
123 5c860e29 2018-03-12 stsp usage();
124 5c860e29 2018-03-12 stsp /* NOTREACHED */
125 5c860e29 2018-03-12 stsp }
126 5c860e29 2018-03-12 stsp }
127 5c860e29 2018-03-12 stsp
128 5c860e29 2018-03-12 stsp argc -= optind;
129 5c860e29 2018-03-12 stsp argv += optind;
130 1e70621d 2018-03-27 stsp optind = 0;
131 5c860e29 2018-03-12 stsp
132 5c860e29 2018-03-12 stsp if (argc <= 0)
133 5c860e29 2018-03-12 stsp usage();
134 5c860e29 2018-03-12 stsp
135 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
136 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
137 99437157 2018-11-11 stsp
138 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
139 d7d4f210 2018-03-12 stsp const struct got_error *error;
140 d7d4f210 2018-03-12 stsp
141 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
142 5c860e29 2018-03-12 stsp
143 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
144 5c860e29 2018-03-12 stsp continue;
145 5c860e29 2018-03-12 stsp
146 1b6b95a8 2018-03-12 stsp if (hflag)
147 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
148 1b6b95a8 2018-03-12 stsp
149 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
150 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
151 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
152 d7d4f210 2018-03-12 stsp return 1;
153 d7d4f210 2018-03-12 stsp }
154 d7d4f210 2018-03-12 stsp
155 d7d4f210 2018-03-12 stsp return 0;
156 5c860e29 2018-03-12 stsp }
157 5c860e29 2018-03-12 stsp
158 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
159 5c860e29 2018-03-12 stsp return 1;
160 5c860e29 2018-03-12 stsp }
161 5c860e29 2018-03-12 stsp
162 4ed7e80c 2018-05-20 stsp __dead static void
163 5c860e29 2018-03-12 stsp usage(void)
164 5c860e29 2018-03-12 stsp {
165 46a0db7d 2018-03-12 stsp int i;
166 46a0db7d 2018-03-12 stsp
167 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
168 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
169 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
170 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
171 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
172 46a0db7d 2018-03-12 stsp }
173 5c860e29 2018-03-12 stsp exit(1);
174 5c860e29 2018-03-12 stsp }
175 5c860e29 2018-03-12 stsp
176 4ed7e80c 2018-05-20 stsp __dead static void
177 c09a553d 2018-03-12 stsp usage_checkout(void)
178 c09a553d 2018-03-12 stsp {
179 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
180 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
181 c09a553d 2018-03-12 stsp exit(1);
182 92a684f4 2018-03-12 stsp }
183 92a684f4 2018-03-12 stsp
184 92a684f4 2018-03-12 stsp static void
185 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
186 92a684f4 2018-03-12 stsp {
187 92a684f4 2018-03-12 stsp char *worktree_path = arg;
188 92a684f4 2018-03-12 stsp
189 92a684f4 2018-03-12 stsp while (path[0] == '/')
190 92a684f4 2018-03-12 stsp path++;
191 92a684f4 2018-03-12 stsp
192 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
193 99437157 2018-11-11 stsp }
194 99437157 2018-11-11 stsp
195 99437157 2018-11-11 stsp static const struct got_error *
196 99437157 2018-11-11 stsp checkout_cancel(void *arg)
197 99437157 2018-11-11 stsp {
198 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
199 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
200 99437157 2018-11-11 stsp return NULL;
201 c09a553d 2018-03-12 stsp }
202 c09a553d 2018-03-12 stsp
203 4ed7e80c 2018-05-20 stsp static const struct got_error *
204 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
205 c09a553d 2018-03-12 stsp {
206 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
207 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
208 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
209 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
210 c09a553d 2018-03-12 stsp char *repo_path = NULL;
211 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
212 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
213 e5dc7198 2018-12-29 stsp int ch, same_path_prefix;
214 c09a553d 2018-03-12 stsp
215 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
216 0bb8a95e 2018-03-12 stsp switch (ch) {
217 0bb8a95e 2018-03-12 stsp case 'p':
218 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
219 0bb8a95e 2018-03-12 stsp break;
220 0bb8a95e 2018-03-12 stsp default:
221 0bb8a95e 2018-03-12 stsp usage();
222 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
223 0bb8a95e 2018-03-12 stsp }
224 0bb8a95e 2018-03-12 stsp }
225 0bb8a95e 2018-03-12 stsp
226 0bb8a95e 2018-03-12 stsp argc -= optind;
227 0bb8a95e 2018-03-12 stsp argv += optind;
228 0bb8a95e 2018-03-12 stsp
229 6715a751 2018-03-16 stsp #ifndef PROFILE
230 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
231 ad242220 2018-09-08 stsp == -1)
232 c09a553d 2018-03-12 stsp err(1, "pledge");
233 6715a751 2018-03-16 stsp #endif
234 0bb8a95e 2018-03-12 stsp if (argc == 1) {
235 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
236 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
237 76089277 2018-04-01 stsp if (repo_path == NULL)
238 76089277 2018-04-01 stsp return got_error_from_errno();
239 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
240 76089277 2018-04-01 stsp if (cwd == NULL) {
241 76089277 2018-04-01 stsp error = got_error_from_errno();
242 76089277 2018-04-01 stsp goto done;
243 76089277 2018-04-01 stsp }
244 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
245 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
246 5d7c1dab 2018-04-01 stsp else
247 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
248 76089277 2018-04-01 stsp if (base == NULL) {
249 76089277 2018-04-01 stsp error = got_error_from_errno();
250 76089277 2018-04-01 stsp goto done;
251 76089277 2018-04-01 stsp }
252 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
253 c09a553d 2018-03-12 stsp if (dotgit)
254 c09a553d 2018-03-12 stsp *dotgit = '\0';
255 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
256 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
257 c09a553d 2018-03-12 stsp free(cwd);
258 76089277 2018-04-01 stsp goto done;
259 c09a553d 2018-03-12 stsp }
260 c09a553d 2018-03-12 stsp free(cwd);
261 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
262 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
263 76089277 2018-04-01 stsp if (repo_path == NULL) {
264 76089277 2018-04-01 stsp error = got_error_from_errno();
265 76089277 2018-04-01 stsp goto done;
266 76089277 2018-04-01 stsp }
267 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
268 76089277 2018-04-01 stsp if (worktree_path == NULL) {
269 76089277 2018-04-01 stsp error = got_error_from_errno();
270 76089277 2018-04-01 stsp goto done;
271 76089277 2018-04-01 stsp }
272 c09a553d 2018-03-12 stsp } else
273 c09a553d 2018-03-12 stsp usage_checkout();
274 c09a553d 2018-03-12 stsp
275 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
276 c09a553d 2018-03-12 stsp if (error != NULL)
277 c09a553d 2018-03-12 stsp goto done;
278 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
279 c09a553d 2018-03-12 stsp if (error != NULL)
280 c09a553d 2018-03-12 stsp goto done;
281 c09a553d 2018-03-12 stsp
282 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
283 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
284 c09a553d 2018-03-12 stsp goto done;
285 c09a553d 2018-03-12 stsp
286 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
287 c09a553d 2018-03-12 stsp if (error != NULL)
288 c09a553d 2018-03-12 stsp goto done;
289 c09a553d 2018-03-12 stsp
290 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
291 e5dc7198 2018-12-29 stsp path_prefix);
292 e5dc7198 2018-12-29 stsp if (error != NULL)
293 e5dc7198 2018-12-29 stsp goto done;
294 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
295 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
296 49520a32 2018-12-29 stsp goto done;
297 49520a32 2018-12-29 stsp }
298 49520a32 2018-12-29 stsp
299 93a30277 2018-12-24 stsp error = got_worktree_checkout_files(worktree, repo,
300 99437157 2018-11-11 stsp checkout_progress, worktree_path, checkout_cancel, NULL);
301 c09a553d 2018-03-12 stsp if (error != NULL)
302 c09a553d 2018-03-12 stsp goto done;
303 c09a553d 2018-03-12 stsp
304 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
305 c09a553d 2018-03-12 stsp
306 c09a553d 2018-03-12 stsp done:
307 76089277 2018-04-01 stsp free(repo_path);
308 507dc3bb 2018-12-29 stsp free(worktree_path);
309 507dc3bb 2018-12-29 stsp return error;
310 507dc3bb 2018-12-29 stsp }
311 507dc3bb 2018-12-29 stsp
312 507dc3bb 2018-12-29 stsp __dead static void
313 507dc3bb 2018-12-29 stsp usage_update(void)
314 507dc3bb 2018-12-29 stsp {
315 507dc3bb 2018-12-29 stsp fprintf(stderr, "usage: %s update [-c commit] [worktree-path]\n",
316 507dc3bb 2018-12-29 stsp getprogname());
317 507dc3bb 2018-12-29 stsp exit(1);
318 507dc3bb 2018-12-29 stsp }
319 507dc3bb 2018-12-29 stsp
320 507dc3bb 2018-12-29 stsp static void
321 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
322 507dc3bb 2018-12-29 stsp {
323 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
324 507dc3bb 2018-12-29 stsp return;
325 507dc3bb 2018-12-29 stsp
326 507dc3bb 2018-12-29 stsp while (path[0] == '/')
327 507dc3bb 2018-12-29 stsp path++;
328 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
329 507dc3bb 2018-12-29 stsp }
330 507dc3bb 2018-12-29 stsp
331 507dc3bb 2018-12-29 stsp static const struct got_error *
332 be7061eb 2018-12-30 stsp check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
333 be7061eb 2018-12-30 stsp struct got_repository *repo)
334 be7061eb 2018-12-30 stsp {
335 be7061eb 2018-12-30 stsp const struct got_error *err;
336 be7061eb 2018-12-30 stsp struct got_reference *head_ref = NULL;
337 be7061eb 2018-12-30 stsp struct got_object_id *head_commit_id = NULL;
338 be7061eb 2018-12-30 stsp struct got_commit_graph *graph = NULL;
339 be7061eb 2018-12-30 stsp
340 be7061eb 2018-12-30 stsp head_ref = got_worktree_get_head_ref(worktree);
341 be7061eb 2018-12-30 stsp if (head_ref == NULL)
342 be7061eb 2018-12-30 stsp return got_error_from_errno();
343 be7061eb 2018-12-30 stsp
344 2944241d 2018-12-30 stsp /* TODO: Check the reflog. The head ref may have been rebased. */
345 be7061eb 2018-12-30 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
346 be7061eb 2018-12-30 stsp if (err)
347 be7061eb 2018-12-30 stsp goto done;
348 be7061eb 2018-12-30 stsp
349 be7061eb 2018-12-30 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
350 be7061eb 2018-12-30 stsp if (err)
351 be7061eb 2018-12-30 stsp goto done;
352 be7061eb 2018-12-30 stsp
353 be7061eb 2018-12-30 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
354 be7061eb 2018-12-30 stsp if (err)
355 be7061eb 2018-12-30 stsp goto done;
356 be7061eb 2018-12-30 stsp while (1) {
357 be7061eb 2018-12-30 stsp struct got_object_id *id;
358 be7061eb 2018-12-30 stsp
359 be7061eb 2018-12-30 stsp if (sigint_received || sigpipe_received)
360 be7061eb 2018-12-30 stsp break;
361 be7061eb 2018-12-30 stsp
362 be7061eb 2018-12-30 stsp err = got_commit_graph_iter_next(&id, graph);
363 be7061eb 2018-12-30 stsp if (err) {
364 be7061eb 2018-12-30 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
365 be7061eb 2018-12-30 stsp err = got_error(GOT_ERR_ANCESTRY);
366 be7061eb 2018-12-30 stsp break;
367 be7061eb 2018-12-30 stsp }
368 be7061eb 2018-12-30 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
369 be7061eb 2018-12-30 stsp break;
370 be7061eb 2018-12-30 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
371 be7061eb 2018-12-30 stsp if (err)
372 be7061eb 2018-12-30 stsp break;
373 be7061eb 2018-12-30 stsp else
374 be7061eb 2018-12-30 stsp continue;
375 be7061eb 2018-12-30 stsp }
376 be7061eb 2018-12-30 stsp if (id == NULL)
377 be7061eb 2018-12-30 stsp break;
378 be7061eb 2018-12-30 stsp if (got_object_id_cmp(id, commit_id) == 0)
379 be7061eb 2018-12-30 stsp break;
380 be7061eb 2018-12-30 stsp }
381 be7061eb 2018-12-30 stsp done:
382 be7061eb 2018-12-30 stsp if (head_ref)
383 be7061eb 2018-12-30 stsp got_ref_close(head_ref);
384 be7061eb 2018-12-30 stsp if (graph)
385 be7061eb 2018-12-30 stsp got_commit_graph_close(graph);
386 be7061eb 2018-12-30 stsp return err;
387 be7061eb 2018-12-30 stsp }
388 be7061eb 2018-12-30 stsp
389 be7061eb 2018-12-30 stsp static const struct got_error *
390 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
391 507dc3bb 2018-12-29 stsp {
392 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
393 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
394 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
395 507dc3bb 2018-12-29 stsp char *worktree_path = NULL;
396 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
397 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
398 507dc3bb 2018-12-29 stsp int ch;
399 507dc3bb 2018-12-29 stsp
400 507dc3bb 2018-12-29 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
401 507dc3bb 2018-12-29 stsp switch (ch) {
402 507dc3bb 2018-12-29 stsp case 'c':
403 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
404 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
405 9c4b8182 2019-01-02 stsp return got_error_from_errno();
406 507dc3bb 2018-12-29 stsp break;
407 507dc3bb 2018-12-29 stsp default:
408 507dc3bb 2018-12-29 stsp usage();
409 507dc3bb 2018-12-29 stsp /* NOTREACHED */
410 507dc3bb 2018-12-29 stsp }
411 507dc3bb 2018-12-29 stsp }
412 507dc3bb 2018-12-29 stsp
413 507dc3bb 2018-12-29 stsp argc -= optind;
414 507dc3bb 2018-12-29 stsp argv += optind;
415 507dc3bb 2018-12-29 stsp
416 507dc3bb 2018-12-29 stsp #ifndef PROFILE
417 507dc3bb 2018-12-29 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
418 507dc3bb 2018-12-29 stsp == -1)
419 507dc3bb 2018-12-29 stsp err(1, "pledge");
420 507dc3bb 2018-12-29 stsp #endif
421 507dc3bb 2018-12-29 stsp if (argc == 0) {
422 507dc3bb 2018-12-29 stsp worktree_path = getcwd(NULL, 0);
423 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
424 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
425 507dc3bb 2018-12-29 stsp goto done;
426 507dc3bb 2018-12-29 stsp }
427 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
428 507dc3bb 2018-12-29 stsp worktree_path = realpath(argv[0], NULL);
429 507dc3bb 2018-12-29 stsp if (worktree_path == NULL) {
430 507dc3bb 2018-12-29 stsp error = got_error_from_errno();
431 507dc3bb 2018-12-29 stsp goto done;
432 507dc3bb 2018-12-29 stsp }
433 507dc3bb 2018-12-29 stsp } else
434 507dc3bb 2018-12-29 stsp usage_update();
435 507dc3bb 2018-12-29 stsp
436 507dc3bb 2018-12-29 stsp error = got_worktree_open(&worktree, worktree_path);
437 507dc3bb 2018-12-29 stsp if (error != NULL)
438 507dc3bb 2018-12-29 stsp goto done;
439 507dc3bb 2018-12-29 stsp
440 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
441 507dc3bb 2018-12-29 stsp if (error != NULL)
442 507dc3bb 2018-12-29 stsp goto done;
443 507dc3bb 2018-12-29 stsp
444 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
445 507dc3bb 2018-12-29 stsp struct got_reference *head_ref;
446 507dc3bb 2018-12-29 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
447 507dc3bb 2018-12-29 stsp if (error != NULL)
448 507dc3bb 2018-12-29 stsp goto done;
449 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
450 9c4b8182 2019-01-02 stsp if (error != NULL)
451 9c4b8182 2019-01-02 stsp goto done;
452 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
453 507dc3bb 2018-12-29 stsp if (error != NULL)
454 507dc3bb 2018-12-29 stsp goto done;
455 507dc3bb 2018-12-29 stsp } else {
456 507dc3bb 2018-12-29 stsp error = got_object_resolve_id_str(&commit_id, repo,
457 507dc3bb 2018-12-29 stsp commit_id_str);
458 507dc3bb 2018-12-29 stsp if (error != NULL)
459 507dc3bb 2018-12-29 stsp goto done;
460 507dc3bb 2018-12-29 stsp }
461 35c965b2 2018-12-29 stsp
462 be7061eb 2018-12-30 stsp error = check_ancestry(worktree, commit_id, repo);
463 be7061eb 2018-12-30 stsp if (error != NULL)
464 be7061eb 2018-12-30 stsp goto done;
465 507dc3bb 2018-12-29 stsp
466 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
467 507dc3bb 2018-12-29 stsp commit_id) != 0) {
468 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
469 507dc3bb 2018-12-29 stsp commit_id);
470 507dc3bb 2018-12-29 stsp if (error)
471 507dc3bb 2018-12-29 stsp goto done;
472 507dc3bb 2018-12-29 stsp }
473 507dc3bb 2018-12-29 stsp
474 507dc3bb 2018-12-29 stsp error = got_worktree_checkout_files(worktree, repo,
475 507dc3bb 2018-12-29 stsp update_progress, NULL, checkout_cancel, NULL);
476 507dc3bb 2018-12-29 stsp if (error != NULL)
477 507dc3bb 2018-12-29 stsp goto done;
478 9c4b8182 2019-01-02 stsp
479 9c4b8182 2019-01-02 stsp printf("Updated to commit %s\n", commit_id_str);
480 507dc3bb 2018-12-29 stsp done:
481 c09a553d 2018-03-12 stsp free(worktree_path);
482 507dc3bb 2018-12-29 stsp free(commit_id);
483 9c4b8182 2019-01-02 stsp free(commit_id_str);
484 c09a553d 2018-03-12 stsp return error;
485 c09a553d 2018-03-12 stsp }
486 c09a553d 2018-03-12 stsp
487 f42b1b34 2018-03-12 stsp static const struct got_error *
488 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
489 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
490 5c860e29 2018-03-12 stsp {
491 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
492 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
493 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
494 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
495 79109fed 2018-03-27 stsp
496 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
497 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
498 79109fed 2018-03-27 stsp if (err)
499 79109fed 2018-03-27 stsp return err;
500 79109fed 2018-03-27 stsp
501 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
502 79f35eb3 2018-06-11 stsp if (qid != NULL) {
503 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
504 79109fed 2018-03-27 stsp
505 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
506 79109fed 2018-03-27 stsp if (err)
507 79109fed 2018-03-27 stsp return err;
508 79109fed 2018-03-27 stsp
509 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
510 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
511 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
512 0f2b3dca 2018-12-22 stsp if (err)
513 0f2b3dca 2018-12-22 stsp return err;
514 0f2b3dca 2018-12-22 stsp
515 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
516 79109fed 2018-03-27 stsp if (err)
517 79109fed 2018-03-27 stsp return err;
518 79109fed 2018-03-27 stsp }
519 79109fed 2018-03-27 stsp
520 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
521 0f2b3dca 2018-12-22 stsp if (err)
522 0f2b3dca 2018-12-22 stsp goto done;
523 0f2b3dca 2018-12-22 stsp
524 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
525 54156555 2018-12-24 stsp err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
526 0f2b3dca 2018-12-22 stsp done:
527 79109fed 2018-03-27 stsp if (tree1)
528 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
529 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
530 0f2b3dca 2018-12-22 stsp free(id_str1);
531 0f2b3dca 2018-12-22 stsp free(id_str2);
532 79109fed 2018-03-27 stsp return err;
533 79109fed 2018-03-27 stsp }
534 79109fed 2018-03-27 stsp
535 4bb494d5 2018-06-16 stsp static char *
536 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
537 6c281f94 2018-06-11 stsp {
538 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
539 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
540 6c281f94 2018-06-11 stsp if (p)
541 6c281f94 2018-06-11 stsp *p = '\0';
542 6c281f94 2018-06-11 stsp return s;
543 6c281f94 2018-06-11 stsp }
544 6c281f94 2018-06-11 stsp
545 79109fed 2018-03-27 stsp static const struct got_error *
546 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
547 c0cc5c62 2018-10-18 stsp struct got_repository *repo, int show_patch, int diff_context)
548 79109fed 2018-03-27 stsp {
549 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
550 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
551 6c281f94 2018-06-11 stsp char datebuf[26];
552 45d799e2 2018-12-23 stsp time_t committer_time;
553 45d799e2 2018-12-23 stsp const char *author, *committer;
554 5c860e29 2018-03-12 stsp
555 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
556 8bf5b3c9 2018-03-17 stsp if (err)
557 8bf5b3c9 2018-03-17 stsp return err;
558 788c352e 2018-06-16 stsp
559 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
560 832c249c 2018-06-10 stsp printf("commit %s\n", id_str);
561 832c249c 2018-06-10 stsp free(id_str);
562 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
563 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
564 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
565 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
566 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
567 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
568 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
569 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
570 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
571 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
572 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
573 3fe1abad 2018-06-10 stsp int n = 1;
574 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
575 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
576 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
577 a0603db2 2018-06-10 stsp if (err)
578 a0603db2 2018-06-10 stsp return err;
579 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
580 a0603db2 2018-06-10 stsp free(id_str);
581 a0603db2 2018-06-10 stsp }
582 a0603db2 2018-06-10 stsp }
583 832c249c 2018-06-10 stsp
584 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
585 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
586 832c249c 2018-06-10 stsp return got_error_from_errno();
587 8bf5b3c9 2018-03-17 stsp
588 621015ac 2018-07-23 stsp logmsg = logmsg0;
589 832c249c 2018-06-10 stsp do {
590 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
591 832c249c 2018-06-10 stsp if (line)
592 832c249c 2018-06-10 stsp printf(" %s\n", line);
593 832c249c 2018-06-10 stsp } while (line);
594 621015ac 2018-07-23 stsp free(logmsg0);
595 832c249c 2018-06-10 stsp
596 971751ac 2018-03-27 stsp if (show_patch) {
597 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
598 971751ac 2018-03-27 stsp if (err == 0)
599 971751ac 2018-03-27 stsp printf("\n");
600 971751ac 2018-03-27 stsp }
601 07862c20 2018-09-15 stsp
602 d82de447 2018-09-15 stsp fflush(stdout);
603 07862c20 2018-09-15 stsp return err;
604 f42b1b34 2018-03-12 stsp }
605 5c860e29 2018-03-12 stsp
606 f42b1b34 2018-03-12 stsp static const struct got_error *
607 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
608 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
609 15a94983 2018-12-23 stsp int first_parent_traversal)
610 f42b1b34 2018-03-12 stsp {
611 f42b1b34 2018-03-12 stsp const struct got_error *err;
612 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
613 372ccdbb 2018-06-10 stsp
614 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
615 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
616 f42b1b34 2018-03-12 stsp if (err)
617 f42b1b34 2018-03-12 stsp return err;
618 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
619 372ccdbb 2018-06-10 stsp if (err)
620 fcc85cad 2018-07-22 stsp goto done;
621 31cedeaf 2018-09-15 stsp while (1) {
622 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
623 372ccdbb 2018-06-10 stsp struct got_object_id *id;
624 8bf5b3c9 2018-03-17 stsp
625 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
626 84453469 2018-11-11 stsp break;
627 84453469 2018-11-11 stsp
628 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
629 372ccdbb 2018-06-10 stsp if (err) {
630 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
631 9ba79e04 2018-06-11 stsp err = NULL;
632 9ba79e04 2018-06-11 stsp break;
633 9ba79e04 2018-06-11 stsp }
634 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
635 372ccdbb 2018-06-10 stsp break;
636 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
637 372ccdbb 2018-06-10 stsp if (err)
638 372ccdbb 2018-06-10 stsp break;
639 372ccdbb 2018-06-10 stsp else
640 372ccdbb 2018-06-10 stsp continue;
641 7e665116 2018-04-02 stsp }
642 b43fbaa0 2018-06-11 stsp if (id == NULL)
643 7e665116 2018-04-02 stsp break;
644 b43fbaa0 2018-06-11 stsp
645 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
646 b43fbaa0 2018-06-11 stsp if (err)
647 fcc85cad 2018-07-22 stsp break;
648 c0cc5c62 2018-10-18 stsp err = print_commit(commit, id, repo, show_patch, diff_context);
649 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
650 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
651 7e665116 2018-04-02 stsp break;
652 31cedeaf 2018-09-15 stsp }
653 fcc85cad 2018-07-22 stsp done:
654 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
655 f42b1b34 2018-03-12 stsp return err;
656 f42b1b34 2018-03-12 stsp }
657 5c860e29 2018-03-12 stsp
658 4ed7e80c 2018-05-20 stsp __dead static void
659 6f3d1eb0 2018-03-12 stsp usage_log(void)
660 6f3d1eb0 2018-03-12 stsp {
661 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
662 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
663 6f3d1eb0 2018-03-12 stsp exit(1);
664 6f3d1eb0 2018-03-12 stsp }
665 6f3d1eb0 2018-03-12 stsp
666 4ed7e80c 2018-05-20 stsp static const struct got_error *
667 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
668 f42b1b34 2018-03-12 stsp {
669 f42b1b34 2018-03-12 stsp const struct got_error *error;
670 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
671 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
672 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
673 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
674 d142fc45 2018-04-01 stsp char *start_commit = NULL;
675 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
676 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
677 64a96a6d 2018-04-01 stsp const char *errstr;
678 5c860e29 2018-03-12 stsp
679 6715a751 2018-03-16 stsp #ifndef PROFILE
680 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
681 ad242220 2018-09-08 stsp == -1)
682 f42b1b34 2018-03-12 stsp err(1, "pledge");
683 6715a751 2018-03-16 stsp #endif
684 79109fed 2018-03-27 stsp
685 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
686 79109fed 2018-03-27 stsp switch (ch) {
687 79109fed 2018-03-27 stsp case 'p':
688 79109fed 2018-03-27 stsp show_patch = 1;
689 d142fc45 2018-04-01 stsp break;
690 d142fc45 2018-04-01 stsp case 'c':
691 d142fc45 2018-04-01 stsp start_commit = optarg;
692 64a96a6d 2018-04-01 stsp break;
693 c0cc5c62 2018-10-18 stsp case 'C':
694 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
695 4a8520aa 2018-10-18 stsp &errstr);
696 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
697 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
698 c0cc5c62 2018-10-18 stsp break;
699 64a96a6d 2018-04-01 stsp case 'l':
700 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
701 64a96a6d 2018-04-01 stsp if (errstr != NULL)
702 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
703 79109fed 2018-03-27 stsp break;
704 0ed6ed4c 2018-06-13 stsp case 'f':
705 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
706 a0603db2 2018-06-10 stsp break;
707 04ca23f4 2018-07-16 stsp case 'r':
708 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
709 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
710 04ca23f4 2018-07-16 stsp err(1, "-r option");
711 04ca23f4 2018-07-16 stsp break;
712 79109fed 2018-03-27 stsp default:
713 79109fed 2018-03-27 stsp usage();
714 79109fed 2018-03-27 stsp /* NOTREACHED */
715 79109fed 2018-03-27 stsp }
716 79109fed 2018-03-27 stsp }
717 79109fed 2018-03-27 stsp
718 79109fed 2018-03-27 stsp argc -= optind;
719 79109fed 2018-03-27 stsp argv += optind;
720 79109fed 2018-03-27 stsp
721 04ca23f4 2018-07-16 stsp if (argc == 0)
722 04ca23f4 2018-07-16 stsp path = strdup("");
723 04ca23f4 2018-07-16 stsp else if (argc == 1)
724 04ca23f4 2018-07-16 stsp path = strdup(argv[0]);
725 04ca23f4 2018-07-16 stsp else
726 6f3d1eb0 2018-03-12 stsp usage_log();
727 04ca23f4 2018-07-16 stsp if (path == NULL)
728 04ca23f4 2018-07-16 stsp return got_error_from_errno();
729 f42b1b34 2018-03-12 stsp
730 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
731 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
732 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
733 04ca23f4 2018-07-16 stsp goto done;
734 04ca23f4 2018-07-16 stsp }
735 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
736 04ca23f4 2018-07-16 stsp repo_path = strdup(cwd);
737 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
738 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
739 04ca23f4 2018-07-16 stsp goto done;
740 04ca23f4 2018-07-16 stsp }
741 04ca23f4 2018-07-16 stsp }
742 04ca23f4 2018-07-16 stsp
743 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
744 d7d4f210 2018-03-12 stsp if (error != NULL)
745 04ca23f4 2018-07-16 stsp goto done;
746 f42b1b34 2018-03-12 stsp
747 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
748 3235492e 2018-04-01 stsp struct got_reference *head_ref;
749 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
750 3235492e 2018-04-01 stsp if (error != NULL)
751 3235492e 2018-04-01 stsp return error;
752 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
753 3235492e 2018-04-01 stsp got_ref_close(head_ref);
754 3235492e 2018-04-01 stsp if (error != NULL)
755 3235492e 2018-04-01 stsp return error;
756 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
757 3235492e 2018-04-01 stsp } else {
758 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
759 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
760 3235492e 2018-04-01 stsp if (error == NULL) {
761 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
762 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
763 d1f2edc9 2018-06-13 stsp if (error != NULL)
764 d1f2edc9 2018-06-13 stsp return error;
765 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
766 d1f2edc9 2018-06-13 stsp if (error != NULL)
767 d1f2edc9 2018-06-13 stsp return error;
768 d1f2edc9 2018-06-13 stsp }
769 15a94983 2018-12-23 stsp if (commit == NULL) {
770 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id, repo,
771 d1f2edc9 2018-06-13 stsp start_commit);
772 d1f2edc9 2018-06-13 stsp if (error != NULL)
773 d1f2edc9 2018-06-13 stsp return error;
774 3235492e 2018-04-01 stsp }
775 3235492e 2018-04-01 stsp }
776 d7d4f210 2018-03-12 stsp if (error != NULL)
777 04ca23f4 2018-07-16 stsp goto done;
778 04ca23f4 2018-07-16 stsp
779 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
780 04ca23f4 2018-07-16 stsp if (error != NULL)
781 04ca23f4 2018-07-16 stsp goto done;
782 04ca23f4 2018-07-16 stsp if (in_repo_path) {
783 04ca23f4 2018-07-16 stsp free(path);
784 04ca23f4 2018-07-16 stsp path = in_repo_path;
785 04ca23f4 2018-07-16 stsp }
786 04ca23f4 2018-07-16 stsp
787 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
788 c0cc5c62 2018-10-18 stsp diff_context, limit, first_parent_traversal);
789 04ca23f4 2018-07-16 stsp done:
790 04ca23f4 2018-07-16 stsp free(path);
791 04ca23f4 2018-07-16 stsp free(repo_path);
792 04ca23f4 2018-07-16 stsp free(cwd);
793 f42b1b34 2018-03-12 stsp free(id);
794 ad242220 2018-09-08 stsp if (repo) {
795 ad242220 2018-09-08 stsp const struct got_error *repo_error;
796 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
797 ad242220 2018-09-08 stsp if (error == NULL)
798 ad242220 2018-09-08 stsp error = repo_error;
799 ad242220 2018-09-08 stsp }
800 8bf5b3c9 2018-03-17 stsp return error;
801 5c860e29 2018-03-12 stsp }
802 5c860e29 2018-03-12 stsp
803 4ed7e80c 2018-05-20 stsp __dead static void
804 3f8b7d6a 2018-04-01 stsp usage_diff(void)
805 3f8b7d6a 2018-04-01 stsp {
806 c0cc5c62 2018-10-18 stsp fprintf(stderr, "usage: %s diff [-C number] [repository-path] "
807 c0cc5c62 2018-10-18 stsp "object1 object2\n", getprogname());
808 3f8b7d6a 2018-04-01 stsp exit(1);
809 b00d56cd 2018-04-01 stsp }
810 b00d56cd 2018-04-01 stsp
811 4ed7e80c 2018-05-20 stsp static const struct got_error *
812 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
813 b00d56cd 2018-04-01 stsp {
814 b00d56cd 2018-04-01 stsp const struct got_error *error;
815 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
816 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
817 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
818 15a94983 2018-12-23 stsp char *id_str1 = NULL, *id_str2 = NULL;
819 15a94983 2018-12-23 stsp int type1, type2;
820 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
821 c0cc5c62 2018-10-18 stsp const char *errstr;
822 b00d56cd 2018-04-01 stsp
823 b00d56cd 2018-04-01 stsp #ifndef PROFILE
824 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
825 ad242220 2018-09-08 stsp == -1)
826 b00d56cd 2018-04-01 stsp err(1, "pledge");
827 b00d56cd 2018-04-01 stsp #endif
828 b00d56cd 2018-04-01 stsp
829 c0cc5c62 2018-10-18 stsp while ((ch = getopt(argc, argv, "C:")) != -1) {
830 b00d56cd 2018-04-01 stsp switch (ch) {
831 c0cc5c62 2018-10-18 stsp case 'C':
832 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
833 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
834 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
835 c0cc5c62 2018-10-18 stsp break;
836 b00d56cd 2018-04-01 stsp default:
837 b00d56cd 2018-04-01 stsp usage();
838 b00d56cd 2018-04-01 stsp /* NOTREACHED */
839 b00d56cd 2018-04-01 stsp }
840 b00d56cd 2018-04-01 stsp }
841 b00d56cd 2018-04-01 stsp
842 b00d56cd 2018-04-01 stsp argc -= optind;
843 b00d56cd 2018-04-01 stsp argv += optind;
844 b00d56cd 2018-04-01 stsp
845 b00d56cd 2018-04-01 stsp if (argc == 0) {
846 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
847 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
848 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
849 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
850 e1e3f570 2018-04-01 stsp return got_error_from_errno();
851 15a94983 2018-12-23 stsp id_str1 = argv[0];
852 15a94983 2018-12-23 stsp id_str2 = argv[1];
853 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
854 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
855 76089277 2018-04-01 stsp if (repo_path == NULL)
856 76089277 2018-04-01 stsp return got_error_from_errno();
857 15a94983 2018-12-23 stsp id_str1 = argv[1];
858 15a94983 2018-12-23 stsp id_str2 = argv[2];
859 b00d56cd 2018-04-01 stsp } else
860 b00d56cd 2018-04-01 stsp usage_diff();
861 b00d56cd 2018-04-01 stsp
862 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
863 76089277 2018-04-01 stsp free(repo_path);
864 b00d56cd 2018-04-01 stsp if (error != NULL)
865 b00d56cd 2018-04-01 stsp goto done;
866 b00d56cd 2018-04-01 stsp
867 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id1, repo, id_str1);
868 6402fb3c 2018-09-15 stsp if (error)
869 b00d56cd 2018-04-01 stsp goto done;
870 b00d56cd 2018-04-01 stsp
871 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&id2, repo, id_str2);
872 6402fb3c 2018-09-15 stsp if (error)
873 b00d56cd 2018-04-01 stsp goto done;
874 b00d56cd 2018-04-01 stsp
875 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
876 15a94983 2018-12-23 stsp if (error)
877 15a94983 2018-12-23 stsp goto done;
878 15a94983 2018-12-23 stsp
879 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
880 15a94983 2018-12-23 stsp if (error)
881 15a94983 2018-12-23 stsp goto done;
882 15a94983 2018-12-23 stsp
883 15a94983 2018-12-23 stsp if (type1 != type2) {
884 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
885 b00d56cd 2018-04-01 stsp goto done;
886 b00d56cd 2018-04-01 stsp }
887 b00d56cd 2018-04-01 stsp
888 15a94983 2018-12-23 stsp switch (type1) {
889 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
890 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
891 54156555 2018-12-24 stsp diff_context, repo, stdout);
892 b00d56cd 2018-04-01 stsp break;
893 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
894 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
895 54156555 2018-12-24 stsp diff_context, repo, stdout);
896 b00d56cd 2018-04-01 stsp break;
897 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
898 15a94983 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
899 15a94983 2018-12-23 stsp id_str2);
900 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
901 c0cc5c62 2018-10-18 stsp repo, stdout);
902 b00d56cd 2018-04-01 stsp break;
903 b00d56cd 2018-04-01 stsp default:
904 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
905 b00d56cd 2018-04-01 stsp }
906 b00d56cd 2018-04-01 stsp
907 b00d56cd 2018-04-01 stsp done:
908 15a94983 2018-12-23 stsp free(id1);
909 15a94983 2018-12-23 stsp free(id2);
910 ad242220 2018-09-08 stsp if (repo) {
911 ad242220 2018-09-08 stsp const struct got_error *repo_error;
912 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
913 ad242220 2018-09-08 stsp if (error == NULL)
914 ad242220 2018-09-08 stsp error = repo_error;
915 ad242220 2018-09-08 stsp }
916 b00d56cd 2018-04-01 stsp return error;
917 404c43c4 2018-06-21 stsp }
918 404c43c4 2018-06-21 stsp
919 404c43c4 2018-06-21 stsp __dead static void
920 404c43c4 2018-06-21 stsp usage_blame(void)
921 404c43c4 2018-06-21 stsp {
922 66bea077 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
923 404c43c4 2018-06-21 stsp getprogname());
924 404c43c4 2018-06-21 stsp exit(1);
925 b00d56cd 2018-04-01 stsp }
926 b00d56cd 2018-04-01 stsp
927 404c43c4 2018-06-21 stsp static const struct got_error *
928 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
929 404c43c4 2018-06-21 stsp {
930 404c43c4 2018-06-21 stsp const struct got_error *error;
931 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
932 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
933 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
934 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
935 404c43c4 2018-06-21 stsp int ch;
936 404c43c4 2018-06-21 stsp
937 404c43c4 2018-06-21 stsp #ifndef PROFILE
938 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
939 ad242220 2018-09-08 stsp == -1)
940 404c43c4 2018-06-21 stsp err(1, "pledge");
941 404c43c4 2018-06-21 stsp #endif
942 404c43c4 2018-06-21 stsp
943 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
944 404c43c4 2018-06-21 stsp switch (ch) {
945 404c43c4 2018-06-21 stsp case 'c':
946 404c43c4 2018-06-21 stsp commit_id_str = optarg;
947 404c43c4 2018-06-21 stsp break;
948 66bea077 2018-08-02 stsp case 'r':
949 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
950 66bea077 2018-08-02 stsp if (repo_path == NULL)
951 66bea077 2018-08-02 stsp err(1, "-r option");
952 66bea077 2018-08-02 stsp break;
953 404c43c4 2018-06-21 stsp default:
954 404c43c4 2018-06-21 stsp usage();
955 404c43c4 2018-06-21 stsp /* NOTREACHED */
956 404c43c4 2018-06-21 stsp }
957 404c43c4 2018-06-21 stsp }
958 404c43c4 2018-06-21 stsp
959 404c43c4 2018-06-21 stsp argc -= optind;
960 404c43c4 2018-06-21 stsp argv += optind;
961 404c43c4 2018-06-21 stsp
962 a39318fd 2018-08-02 stsp if (argc == 1)
963 404c43c4 2018-06-21 stsp path = argv[0];
964 a39318fd 2018-08-02 stsp else
965 404c43c4 2018-06-21 stsp usage_blame();
966 404c43c4 2018-06-21 stsp
967 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
968 66bea077 2018-08-02 stsp if (cwd == NULL) {
969 66bea077 2018-08-02 stsp error = got_error_from_errno();
970 66bea077 2018-08-02 stsp goto done;
971 66bea077 2018-08-02 stsp }
972 66bea077 2018-08-02 stsp if (repo_path == NULL) {
973 66bea077 2018-08-02 stsp repo_path = strdup(cwd);
974 66bea077 2018-08-02 stsp if (repo_path == NULL) {
975 66bea077 2018-08-02 stsp error = got_error_from_errno();
976 66bea077 2018-08-02 stsp goto done;
977 66bea077 2018-08-02 stsp }
978 66bea077 2018-08-02 stsp }
979 66bea077 2018-08-02 stsp
980 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
981 404c43c4 2018-06-21 stsp if (error != NULL)
982 404c43c4 2018-06-21 stsp goto done;
983 404c43c4 2018-06-21 stsp
984 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
985 66bea077 2018-08-02 stsp if (error != NULL)
986 66bea077 2018-08-02 stsp goto done;
987 66bea077 2018-08-02 stsp
988 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
989 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
990 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
991 404c43c4 2018-06-21 stsp if (error != NULL)
992 66bea077 2018-08-02 stsp goto done;
993 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
994 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
995 404c43c4 2018-06-21 stsp if (error != NULL)
996 66bea077 2018-08-02 stsp goto done;
997 404c43c4 2018-06-21 stsp } else {
998 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
999 15a94983 2018-12-23 stsp commit_id_str);
1000 404c43c4 2018-06-21 stsp if (error != NULL)
1001 66bea077 2018-08-02 stsp goto done;
1002 404c43c4 2018-06-21 stsp }
1003 404c43c4 2018-06-21 stsp
1004 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
1005 404c43c4 2018-06-21 stsp done:
1006 66bea077 2018-08-02 stsp free(in_repo_path);
1007 66bea077 2018-08-02 stsp free(repo_path);
1008 66bea077 2018-08-02 stsp free(cwd);
1009 404c43c4 2018-06-21 stsp free(commit_id);
1010 ad242220 2018-09-08 stsp if (repo) {
1011 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1012 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1013 ad242220 2018-09-08 stsp if (error == NULL)
1014 ad242220 2018-09-08 stsp error = repo_error;
1015 ad242220 2018-09-08 stsp }
1016 404c43c4 2018-06-21 stsp return error;
1017 5de5890b 2018-10-18 stsp }
1018 5de5890b 2018-10-18 stsp
1019 5de5890b 2018-10-18 stsp __dead static void
1020 5de5890b 2018-10-18 stsp usage_tree(void)
1021 5de5890b 2018-10-18 stsp {
1022 5de5890b 2018-10-18 stsp fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [-i] path\n",
1023 5de5890b 2018-10-18 stsp getprogname());
1024 5de5890b 2018-10-18 stsp exit(1);
1025 5de5890b 2018-10-18 stsp }
1026 5de5890b 2018-10-18 stsp
1027 5de5890b 2018-10-18 stsp
1028 5de5890b 2018-10-18 stsp static const struct got_error *
1029 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
1030 5de5890b 2018-10-18 stsp int show_ids, struct got_repository *repo)
1031 5de5890b 2018-10-18 stsp {
1032 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
1033 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
1034 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
1035 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
1036 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
1037 5de5890b 2018-10-18 stsp
1038 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1039 5de5890b 2018-10-18 stsp if (err)
1040 5de5890b 2018-10-18 stsp goto done;
1041 5de5890b 2018-10-18 stsp
1042 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
1043 5de5890b 2018-10-18 stsp if (err)
1044 5de5890b 2018-10-18 stsp goto done;
1045 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
1046 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
1047 5de5890b 2018-10-18 stsp while (te) {
1048 5de5890b 2018-10-18 stsp char *id = NULL;
1049 84453469 2018-11-11 stsp
1050 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1051 84453469 2018-11-11 stsp break;
1052 84453469 2018-11-11 stsp
1053 5de5890b 2018-10-18 stsp if (show_ids) {
1054 5de5890b 2018-10-18 stsp char *id_str;
1055 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
1056 5de5890b 2018-10-18 stsp if (err)
1057 5de5890b 2018-10-18 stsp goto done;
1058 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
1059 5de5890b 2018-10-18 stsp err = got_error_from_errno();
1060 5de5890b 2018-10-18 stsp free(id_str);
1061 5de5890b 2018-10-18 stsp goto done;
1062 5de5890b 2018-10-18 stsp }
1063 5de5890b 2018-10-18 stsp free(id_str);
1064 5de5890b 2018-10-18 stsp }
1065 5de5890b 2018-10-18 stsp printf("%s%s%s\n", id ? id : "",
1066 5de5890b 2018-10-18 stsp te->name, S_ISDIR(te->mode) ? "/" : "");
1067 5de5890b 2018-10-18 stsp te = SIMPLEQ_NEXT(te, entry);
1068 5de5890b 2018-10-18 stsp free(id);
1069 5de5890b 2018-10-18 stsp }
1070 5de5890b 2018-10-18 stsp done:
1071 5de5890b 2018-10-18 stsp if (tree)
1072 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
1073 5de5890b 2018-10-18 stsp free(tree_id);
1074 5de5890b 2018-10-18 stsp return err;
1075 404c43c4 2018-06-21 stsp }
1076 404c43c4 2018-06-21 stsp
1077 5de5890b 2018-10-18 stsp static const struct got_error *
1078 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
1079 5de5890b 2018-10-18 stsp {
1080 5de5890b 2018-10-18 stsp const struct got_error *error;
1081 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
1082 5de5890b 2018-10-18 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1083 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
1084 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
1085 5de5890b 2018-10-18 stsp int show_ids = 0;
1086 5de5890b 2018-10-18 stsp int ch;
1087 5de5890b 2018-10-18 stsp
1088 5de5890b 2018-10-18 stsp #ifndef PROFILE
1089 5de5890b 2018-10-18 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
1090 5de5890b 2018-10-18 stsp == -1)
1091 5de5890b 2018-10-18 stsp err(1, "pledge");
1092 5de5890b 2018-10-18 stsp #endif
1093 5de5890b 2018-10-18 stsp
1094 5de5890b 2018-10-18 stsp while ((ch = getopt(argc, argv, "c:r:i")) != -1) {
1095 5de5890b 2018-10-18 stsp switch (ch) {
1096 5de5890b 2018-10-18 stsp case 'c':
1097 5de5890b 2018-10-18 stsp commit_id_str = optarg;
1098 5de5890b 2018-10-18 stsp break;
1099 5de5890b 2018-10-18 stsp case 'r':
1100 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
1101 5de5890b 2018-10-18 stsp if (repo_path == NULL)
1102 5de5890b 2018-10-18 stsp err(1, "-r option");
1103 5de5890b 2018-10-18 stsp break;
1104 5de5890b 2018-10-18 stsp case 'i':
1105 5de5890b 2018-10-18 stsp show_ids = 1;
1106 5de5890b 2018-10-18 stsp break;
1107 5de5890b 2018-10-18 stsp default:
1108 5de5890b 2018-10-18 stsp usage();
1109 5de5890b 2018-10-18 stsp /* NOTREACHED */
1110 5de5890b 2018-10-18 stsp }
1111 5de5890b 2018-10-18 stsp }
1112 5de5890b 2018-10-18 stsp
1113 5de5890b 2018-10-18 stsp argc -= optind;
1114 5de5890b 2018-10-18 stsp argv += optind;
1115 5de5890b 2018-10-18 stsp
1116 5de5890b 2018-10-18 stsp if (argc == 1)
1117 5de5890b 2018-10-18 stsp path = argv[0];
1118 5de5890b 2018-10-18 stsp else if (argc > 1)
1119 5de5890b 2018-10-18 stsp usage_tree();
1120 5de5890b 2018-10-18 stsp else
1121 5de5890b 2018-10-18 stsp path = "/";
1122 5de5890b 2018-10-18 stsp
1123 5de5890b 2018-10-18 stsp cwd = getcwd(NULL, 0);
1124 5de5890b 2018-10-18 stsp if (cwd == NULL) {
1125 5de5890b 2018-10-18 stsp error = got_error_from_errno();
1126 5de5890b 2018-10-18 stsp goto done;
1127 5de5890b 2018-10-18 stsp }
1128 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1129 5de5890b 2018-10-18 stsp repo_path = strdup(cwd);
1130 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
1131 5de5890b 2018-10-18 stsp error = got_error_from_errno();
1132 5de5890b 2018-10-18 stsp goto done;
1133 5de5890b 2018-10-18 stsp }
1134 5de5890b 2018-10-18 stsp }
1135 5de5890b 2018-10-18 stsp
1136 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
1137 5de5890b 2018-10-18 stsp if (error != NULL)
1138 5de5890b 2018-10-18 stsp goto done;
1139 5de5890b 2018-10-18 stsp
1140 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1141 5de5890b 2018-10-18 stsp if (error != NULL)
1142 5de5890b 2018-10-18 stsp goto done;
1143 5de5890b 2018-10-18 stsp
1144 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
1145 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
1146 5de5890b 2018-10-18 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1147 5de5890b 2018-10-18 stsp if (error != NULL)
1148 5de5890b 2018-10-18 stsp goto done;
1149 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1150 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
1151 5de5890b 2018-10-18 stsp if (error != NULL)
1152 5de5890b 2018-10-18 stsp goto done;
1153 5de5890b 2018-10-18 stsp } else {
1154 15a94983 2018-12-23 stsp error = got_object_resolve_id_str(&commit_id, repo,
1155 15a94983 2018-12-23 stsp commit_id_str);
1156 5de5890b 2018-10-18 stsp if (error != NULL)
1157 5de5890b 2018-10-18 stsp goto done;
1158 5de5890b 2018-10-18 stsp }
1159 5de5890b 2018-10-18 stsp
1160 5de5890b 2018-10-18 stsp error = print_tree(in_repo_path, commit_id, show_ids, repo);
1161 5de5890b 2018-10-18 stsp done:
1162 5de5890b 2018-10-18 stsp free(in_repo_path);
1163 5de5890b 2018-10-18 stsp free(repo_path);
1164 5de5890b 2018-10-18 stsp free(cwd);
1165 5de5890b 2018-10-18 stsp free(commit_id);
1166 5de5890b 2018-10-18 stsp if (repo) {
1167 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
1168 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
1169 5de5890b 2018-10-18 stsp if (error == NULL)
1170 5de5890b 2018-10-18 stsp error = repo_error;
1171 5de5890b 2018-10-18 stsp }
1172 5de5890b 2018-10-18 stsp return error;
1173 5de5890b 2018-10-18 stsp }
1174 5de5890b 2018-10-18 stsp
1175 f42b1b34 2018-03-12 stsp #ifdef notyet
1176 4ed7e80c 2018-05-20 stsp static const struct got_error *
1177 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
1178 5c860e29 2018-03-12 stsp {
1179 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
1180 5c860e29 2018-03-12 stsp git_status_list *status;
1181 5c860e29 2018-03-12 stsp git_status_options statusopts;
1182 5c860e29 2018-03-12 stsp size_t i;
1183 5c860e29 2018-03-12 stsp
1184 5c860e29 2018-03-12 stsp git_libgit2_init();
1185 5c860e29 2018-03-12 stsp
1186 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
1187 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
1188 5c860e29 2018-03-12 stsp
1189 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
1190 5c860e29 2018-03-12 stsp errx(1, "bar repository");
1191 5c860e29 2018-03-12 stsp
1192 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
1193 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
1194 5c860e29 2018-03-12 stsp
1195 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
1196 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
1197 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
1198 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
1199 5c860e29 2018-03-12 stsp
1200 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
1201 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
1202 5c860e29 2018-03-12 stsp
1203 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
1204 5c860e29 2018-03-12 stsp const git_status_entry *se;
1205 5c860e29 2018-03-12 stsp
1206 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
1207 5c860e29 2018-03-12 stsp switch (se->status) {
1208 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
1209 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
1210 5c860e29 2018-03-12 stsp break;
1211 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
1212 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
1213 5c860e29 2018-03-12 stsp break;
1214 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
1215 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
1216 5c860e29 2018-03-12 stsp break;
1217 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
1218 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
1219 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
1220 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
1221 5c860e29 2018-03-12 stsp break;
1222 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
1223 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
1224 5c860e29 2018-03-12 stsp break;
1225 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
1226 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
1227 5c860e29 2018-03-12 stsp break;
1228 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
1229 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
1230 5c860e29 2018-03-12 stsp break;
1231 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
1232 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
1233 5c860e29 2018-03-12 stsp break;
1234 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
1235 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
1236 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
1237 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
1238 5c860e29 2018-03-12 stsp break;
1239 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
1240 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
1241 5c860e29 2018-03-12 stsp break;
1242 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
1243 5c860e29 2018-03-12 stsp default:
1244 5c860e29 2018-03-12 stsp break;
1245 5c860e29 2018-03-12 stsp }
1246 5c860e29 2018-03-12 stsp }
1247 5c860e29 2018-03-12 stsp
1248 5c860e29 2018-03-12 stsp git_status_list_free(status);
1249 5c860e29 2018-03-12 stsp git_repository_free(repo);
1250 5c860e29 2018-03-12 stsp git_libgit2_shutdown();
1251 5c860e29 2018-03-12 stsp
1252 5c860e29 2018-03-12 stsp return 0;
1253 5c860e29 2018-03-12 stsp }
1254 f42b1b34 2018-03-12 stsp #endif