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