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