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 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
194 ad242220 2018-09-08 stsp == -1)
195 c09a553d 2018-03-12 stsp err(1, "pledge");
196 6715a751 2018-03-16 stsp #endif
197 0bb8a95e 2018-03-12 stsp if (argc == 1) {
198 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
199 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
200 76089277 2018-04-01 stsp if (repo_path == NULL)
201 76089277 2018-04-01 stsp return got_error_from_errno();
202 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
203 76089277 2018-04-01 stsp if (cwd == NULL) {
204 76089277 2018-04-01 stsp error = got_error_from_errno();
205 76089277 2018-04-01 stsp goto done;
206 76089277 2018-04-01 stsp }
207 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
208 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
209 5d7c1dab 2018-04-01 stsp else
210 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
211 76089277 2018-04-01 stsp if (base == NULL) {
212 76089277 2018-04-01 stsp error = got_error_from_errno();
213 76089277 2018-04-01 stsp goto done;
214 76089277 2018-04-01 stsp }
215 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
216 c09a553d 2018-03-12 stsp if (dotgit)
217 c09a553d 2018-03-12 stsp *dotgit = '\0';
218 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
219 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
220 c09a553d 2018-03-12 stsp free(cwd);
221 76089277 2018-04-01 stsp goto done;
222 c09a553d 2018-03-12 stsp }
223 c09a553d 2018-03-12 stsp free(cwd);
224 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
225 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
226 76089277 2018-04-01 stsp if (repo_path == NULL) {
227 76089277 2018-04-01 stsp error = got_error_from_errno();
228 76089277 2018-04-01 stsp goto done;
229 76089277 2018-04-01 stsp }
230 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
231 76089277 2018-04-01 stsp if (worktree_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 c09a553d 2018-03-12 stsp } else
236 c09a553d 2018-03-12 stsp usage_checkout();
237 c09a553d 2018-03-12 stsp
238 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
239 c09a553d 2018-03-12 stsp if (error != NULL)
240 c09a553d 2018-03-12 stsp goto done;
241 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
242 c09a553d 2018-03-12 stsp if (error != NULL)
243 c09a553d 2018-03-12 stsp goto done;
244 c09a553d 2018-03-12 stsp
245 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
246 c09a553d 2018-03-12 stsp if (error != NULL)
247 c09a553d 2018-03-12 stsp goto done;
248 c09a553d 2018-03-12 stsp
249 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
250 c09a553d 2018-03-12 stsp if (error != NULL)
251 c09a553d 2018-03-12 stsp goto done;
252 c09a553d 2018-03-12 stsp
253 92a684f4 2018-03-12 stsp error = got_worktree_checkout_files(worktree, head_ref, repo,
254 92a684f4 2018-03-12 stsp checkout_progress, 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 b65ae19a 2018-04-24 stsp printf("Checked out %s\n", worktree_path);
259 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
260 c09a553d 2018-03-12 stsp
261 c09a553d 2018-03-12 stsp done:
262 76089277 2018-04-01 stsp free(repo_path);
263 c09a553d 2018-03-12 stsp free(worktree_path);
264 c09a553d 2018-03-12 stsp return error;
265 c09a553d 2018-03-12 stsp }
266 c09a553d 2018-03-12 stsp
267 f42b1b34 2018-03-12 stsp static const struct got_error *
268 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
269 f42b1b34 2018-03-12 stsp struct got_repository *repo)
270 5c860e29 2018-03-12 stsp {
271 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
272 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
273 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
274 79109fed 2018-03-27 stsp
275 117e771c 2018-07-23 stsp err = got_object_open_as_tree(&tree2, 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 79f35eb3 2018-06-11 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
280 79f35eb3 2018-06-11 stsp if (qid != NULL) {
281 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
282 79109fed 2018-03-27 stsp
283 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
284 79109fed 2018-03-27 stsp if (err)
285 79109fed 2018-03-27 stsp return err;
286 79109fed 2018-03-27 stsp
287 117e771c 2018-07-23 stsp err = got_object_open_as_tree(&tree1, repo, pcommit->tree_id);
288 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
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
293 f6861a81 2018-09-13 stsp err = got_diff_tree(tree1, tree2, "", "", repo, stdout);
294 79109fed 2018-03-27 stsp if (tree1)
295 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
296 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
297 79109fed 2018-03-27 stsp return err;
298 79109fed 2018-03-27 stsp }
299 79109fed 2018-03-27 stsp
300 4bb494d5 2018-06-16 stsp static char *
301 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
302 6c281f94 2018-06-11 stsp {
303 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
304 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
305 6c281f94 2018-06-11 stsp if (p)
306 6c281f94 2018-06-11 stsp *p = '\0';
307 6c281f94 2018-06-11 stsp return s;
308 6c281f94 2018-06-11 stsp }
309 6c281f94 2018-06-11 stsp
310 79109fed 2018-03-27 stsp static const struct got_error *
311 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
312 1fd6d7ea 2018-06-13 stsp struct got_repository *repo, int show_patch)
313 79109fed 2018-03-27 stsp {
314 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
315 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
316 6c281f94 2018-06-11 stsp char datebuf[26];
317 788c352e 2018-06-16 stsp time_t author_time, committer_time;
318 5c860e29 2018-03-12 stsp
319 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
320 8bf5b3c9 2018-03-17 stsp if (err)
321 8bf5b3c9 2018-03-17 stsp return err;
322 788c352e 2018-06-16 stsp
323 788c352e 2018-06-16 stsp author_time = mktime(&commit->tm_author);
324 788c352e 2018-06-16 stsp committer_time = mktime(&commit->tm_committer);
325 788c352e 2018-06-16 stsp #if 0
326 788c352e 2018-06-16 stsp /* This would express the date in committer's timezone. */
327 788c352e 2018-06-16 stsp author_time += commit->tm_author.tm_gmtoff;
328 788c352e 2018-06-16 stsp committer_time += commit->tm_committer.tm_gmtoff;
329 788c352e 2018-06-16 stsp #endif
330 5c860e29 2018-03-12 stsp
331 8bf5b3c9 2018-03-17 stsp printf("-----------------------------------------------\n");
332 832c249c 2018-06-10 stsp printf("commit %s\n", id_str);
333 832c249c 2018-06-10 stsp free(id_str);
334 0dcf3e9c 2018-09-15 stsp printf("from: %s\n", commit->author);
335 dab5fe87 2018-09-14 stsp datestr = get_datestr(&committer_time, datebuf);
336 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
337 dab5fe87 2018-09-14 stsp if (strcmp(commit->author, commit->committer) != 0)
338 0dcf3e9c 2018-09-15 stsp printf("via: %s\n", commit->committer);
339 3fe1abad 2018-06-10 stsp if (commit->nparents > 1) {
340 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
341 3fe1abad 2018-06-10 stsp int n = 1;
342 79f35eb3 2018-06-11 stsp SIMPLEQ_FOREACH(qid, &commit->parent_ids, entry) {
343 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
344 a0603db2 2018-06-10 stsp if (err)
345 a0603db2 2018-06-10 stsp return err;
346 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
347 a0603db2 2018-06-10 stsp free(id_str);
348 a0603db2 2018-06-10 stsp }
349 a0603db2 2018-06-10 stsp }
350 832c249c 2018-06-10 stsp
351 621015ac 2018-07-23 stsp logmsg0 = strdup(commit->logmsg);
352 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
353 832c249c 2018-06-10 stsp return got_error_from_errno();
354 8bf5b3c9 2018-03-17 stsp
355 621015ac 2018-07-23 stsp logmsg = logmsg0;
356 832c249c 2018-06-10 stsp do {
357 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
358 832c249c 2018-06-10 stsp if (line)
359 832c249c 2018-06-10 stsp printf(" %s\n", line);
360 832c249c 2018-06-10 stsp } while (line);
361 621015ac 2018-07-23 stsp free(logmsg0);
362 832c249c 2018-06-10 stsp
363 971751ac 2018-03-27 stsp if (show_patch) {
364 79109fed 2018-03-27 stsp err = print_patch(commit, id, repo);
365 971751ac 2018-03-27 stsp if (err == 0)
366 971751ac 2018-03-27 stsp printf("\n");
367 971751ac 2018-03-27 stsp }
368 79109fed 2018-03-27 stsp
369 79109fed 2018-03-27 stsp return err;
370 f2e80f4b 2018-09-15 stsp }
371 f2e80f4b 2018-09-15 stsp
372 f2e80f4b 2018-09-15 stsp static const struct got_error *
373 f2e80f4b 2018-09-15 stsp detect_change(int *changed, struct got_object_id *commit_id,
374 f2e80f4b 2018-09-15 stsp struct got_object *obj, const char *path, struct got_repository *repo)
375 f2e80f4b 2018-09-15 stsp {
376 f2e80f4b 2018-09-15 stsp const struct got_error *err = NULL;
377 f2e80f4b 2018-09-15 stsp struct got_object_id *id, *pid;
378 f2e80f4b 2018-09-15 stsp struct got_object *pobj;
379 f2e80f4b 2018-09-15 stsp
380 f2e80f4b 2018-09-15 stsp err = got_object_open_by_path(&pobj, repo, commit_id, path);
381 f2e80f4b 2018-09-15 stsp if (err) {
382 f2e80f4b 2018-09-15 stsp if (err->code != GOT_ERR_NO_OBJ)
383 f2e80f4b 2018-09-15 stsp return err;
384 f2e80f4b 2018-09-15 stsp *changed = 1;
385 f2e80f4b 2018-09-15 stsp return NULL;
386 f2e80f4b 2018-09-15 stsp }
387 f2e80f4b 2018-09-15 stsp
388 f2e80f4b 2018-09-15 stsp id = got_object_get_id(obj);
389 f2e80f4b 2018-09-15 stsp if (id == NULL)
390 f2e80f4b 2018-09-15 stsp return got_error_from_errno();
391 f2e80f4b 2018-09-15 stsp pid = got_object_get_id(pobj);
392 f2e80f4b 2018-09-15 stsp if (pid == NULL) {
393 f2e80f4b 2018-09-15 stsp err = got_error_from_errno();
394 f2e80f4b 2018-09-15 stsp free(id);
395 f2e80f4b 2018-09-15 stsp return err;
396 f2e80f4b 2018-09-15 stsp }
397 f2e80f4b 2018-09-15 stsp
398 f2e80f4b 2018-09-15 stsp *changed = (got_object_id_cmp(id, pid) != 0);
399 f2e80f4b 2018-09-15 stsp got_object_close(pobj);
400 f2e80f4b 2018-09-15 stsp free(id);
401 f2e80f4b 2018-09-15 stsp free(pid);
402 f2e80f4b 2018-09-15 stsp return NULL;
403 f42b1b34 2018-03-12 stsp }
404 5c860e29 2018-03-12 stsp
405 f42b1b34 2018-03-12 stsp static const struct got_error *
406 8bf5b3c9 2018-03-17 stsp print_commits(struct got_object *root_obj, struct got_object_id *root_id,
407 04ca23f4 2018-07-16 stsp struct got_repository *repo, char *path, int show_patch, int limit,
408 0ed6ed4c 2018-06-13 stsp int first_parent_traversal)
409 f42b1b34 2018-03-12 stsp {
410 f42b1b34 2018-03-12 stsp const struct got_error *err;
411 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
412 04ca23f4 2018-07-16 stsp int ncommits, found_obj = 0;
413 6b765bce 2018-07-22 stsp int is_root_path = (strcmp(path, "/") == 0);
414 372ccdbb 2018-06-10 stsp
415 0ed6ed4c 2018-06-13 stsp err = got_commit_graph_open(&graph, root_id, first_parent_traversal,
416 0ed6ed4c 2018-06-13 stsp repo);
417 f42b1b34 2018-03-12 stsp if (err)
418 f42b1b34 2018-03-12 stsp return err;
419 372ccdbb 2018-06-10 stsp err = got_commit_graph_iter_start(graph, root_id);
420 372ccdbb 2018-06-10 stsp if (err)
421 fcc85cad 2018-07-22 stsp goto done;
422 372ccdbb 2018-06-10 stsp do {
423 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
424 372ccdbb 2018-06-10 stsp struct got_object_id *id;
425 8bf5b3c9 2018-03-17 stsp
426 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
427 372ccdbb 2018-06-10 stsp if (err) {
428 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
429 9ba79e04 2018-06-11 stsp err = NULL;
430 9ba79e04 2018-06-11 stsp break;
431 9ba79e04 2018-06-11 stsp }
432 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
433 372ccdbb 2018-06-10 stsp break;
434 372ccdbb 2018-06-10 stsp err = got_commit_graph_fetch_commits(&ncommits,
435 372ccdbb 2018-06-10 stsp graph, 1, repo);
436 372ccdbb 2018-06-10 stsp if (err)
437 372ccdbb 2018-06-10 stsp break;
438 372ccdbb 2018-06-10 stsp else
439 372ccdbb 2018-06-10 stsp continue;
440 7e665116 2018-04-02 stsp }
441 b43fbaa0 2018-06-11 stsp if (id == NULL)
442 7e665116 2018-04-02 stsp break;
443 b43fbaa0 2018-06-11 stsp
444 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
445 b43fbaa0 2018-06-11 stsp if (err)
446 fcc85cad 2018-07-22 stsp break;
447 fcc85cad 2018-07-22 stsp if (!is_root_path) {
448 04ca23f4 2018-07-16 stsp struct got_object *obj;
449 04ca23f4 2018-07-16 stsp struct got_object_qid *pid;
450 e456239b 2018-07-16 stsp int changed = 0;
451 04ca23f4 2018-07-16 stsp
452 04ca23f4 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
453 04ca23f4 2018-07-16 stsp if (err) {
454 fcc85cad 2018-07-22 stsp got_object_commit_close(commit);
455 04ca23f4 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ && found_obj) {
456 04ca23f4 2018-07-16 stsp /*
457 fcc85cad 2018-07-22 stsp * History of the path stops here
458 fcc85cad 2018-07-22 stsp * on the current commit's branch.
459 fcc85cad 2018-07-22 stsp * Keep logging on other branches.
460 04ca23f4 2018-07-16 stsp */
461 04ca23f4 2018-07-16 stsp err = NULL;
462 fcc85cad 2018-07-22 stsp continue;
463 04ca23f4 2018-07-16 stsp }
464 04ca23f4 2018-07-16 stsp break;
465 04ca23f4 2018-07-16 stsp }
466 04ca23f4 2018-07-16 stsp found_obj = 1;
467 04ca23f4 2018-07-16 stsp
468 04ca23f4 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
469 f2e80f4b 2018-09-15 stsp if (pid) {
470 f2e80f4b 2018-09-15 stsp err = detect_change(&changed, pid->id, obj,
471 f2e80f4b 2018-09-15 stsp path, repo);
472 04ca23f4 2018-07-16 stsp if (err) {
473 f2e80f4b 2018-09-15 stsp got_object_close(obj);
474 f2e80f4b 2018-09-15 stsp got_object_commit_close(commit);
475 f2e80f4b 2018-09-15 stsp break;
476 04ca23f4 2018-07-16 stsp }
477 04ca23f4 2018-07-16 stsp }
478 fcc85cad 2018-07-22 stsp got_object_close(obj);
479 04ca23f4 2018-07-16 stsp if (!changed) {
480 04ca23f4 2018-07-16 stsp got_object_commit_close(commit);
481 04ca23f4 2018-07-16 stsp continue;
482 04ca23f4 2018-07-16 stsp }
483 04ca23f4 2018-07-16 stsp }
484 1fd6d7ea 2018-06-13 stsp err = print_commit(commit, id, repo, show_patch);
485 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
486 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
487 7e665116 2018-04-02 stsp break;
488 372ccdbb 2018-06-10 stsp } while (ncommits > 0);
489 fcc85cad 2018-07-22 stsp done:
490 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
491 f42b1b34 2018-03-12 stsp return err;
492 f42b1b34 2018-03-12 stsp }
493 5c860e29 2018-03-12 stsp
494 4ed7e80c 2018-05-20 stsp __dead static void
495 6f3d1eb0 2018-03-12 stsp usage_log(void)
496 6f3d1eb0 2018-03-12 stsp {
497 6238ee32 2018-06-13 stsp fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
498 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
499 6f3d1eb0 2018-03-12 stsp exit(1);
500 6f3d1eb0 2018-03-12 stsp }
501 6f3d1eb0 2018-03-12 stsp
502 4ed7e80c 2018-05-20 stsp static const struct got_error *
503 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
504 f42b1b34 2018-03-12 stsp {
505 f42b1b34 2018-03-12 stsp const struct got_error *error;
506 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
507 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
508 d1f2edc9 2018-06-13 stsp struct got_object *obj = NULL;
509 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
510 d142fc45 2018-04-01 stsp char *start_commit = NULL;
511 79109fed 2018-03-27 stsp int ch;
512 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
513 64a96a6d 2018-04-01 stsp const char *errstr;
514 5c860e29 2018-03-12 stsp
515 6715a751 2018-03-16 stsp #ifndef PROFILE
516 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
517 ad242220 2018-09-08 stsp == -1)
518 f42b1b34 2018-03-12 stsp err(1, "pledge");
519 6715a751 2018-03-16 stsp #endif
520 79109fed 2018-03-27 stsp
521 04ca23f4 2018-07-16 stsp while ((ch = getopt(argc, argv, "pc:l:fr:")) != -1) {
522 79109fed 2018-03-27 stsp switch (ch) {
523 79109fed 2018-03-27 stsp case 'p':
524 79109fed 2018-03-27 stsp show_patch = 1;
525 d142fc45 2018-04-01 stsp break;
526 d142fc45 2018-04-01 stsp case 'c':
527 d142fc45 2018-04-01 stsp start_commit = optarg;
528 64a96a6d 2018-04-01 stsp break;
529 64a96a6d 2018-04-01 stsp case 'l':
530 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
531 64a96a6d 2018-04-01 stsp if (errstr != NULL)
532 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
533 79109fed 2018-03-27 stsp break;
534 0ed6ed4c 2018-06-13 stsp case 'f':
535 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
536 a0603db2 2018-06-10 stsp break;
537 04ca23f4 2018-07-16 stsp case 'r':
538 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
539 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
540 04ca23f4 2018-07-16 stsp err(1, "-r option");
541 04ca23f4 2018-07-16 stsp break;
542 79109fed 2018-03-27 stsp default:
543 79109fed 2018-03-27 stsp usage();
544 79109fed 2018-03-27 stsp /* NOTREACHED */
545 79109fed 2018-03-27 stsp }
546 79109fed 2018-03-27 stsp }
547 79109fed 2018-03-27 stsp
548 79109fed 2018-03-27 stsp argc -= optind;
549 79109fed 2018-03-27 stsp argv += optind;
550 79109fed 2018-03-27 stsp
551 04ca23f4 2018-07-16 stsp if (argc == 0)
552 04ca23f4 2018-07-16 stsp path = strdup("");
553 04ca23f4 2018-07-16 stsp else if (argc == 1)
554 04ca23f4 2018-07-16 stsp path = strdup(argv[0]);
555 04ca23f4 2018-07-16 stsp else
556 6f3d1eb0 2018-03-12 stsp usage_log();
557 04ca23f4 2018-07-16 stsp if (path == NULL)
558 04ca23f4 2018-07-16 stsp return got_error_from_errno();
559 f42b1b34 2018-03-12 stsp
560 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
561 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
562 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
563 04ca23f4 2018-07-16 stsp goto done;
564 04ca23f4 2018-07-16 stsp }
565 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
566 04ca23f4 2018-07-16 stsp repo_path = strdup(cwd);
567 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
568 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
569 04ca23f4 2018-07-16 stsp goto done;
570 04ca23f4 2018-07-16 stsp }
571 04ca23f4 2018-07-16 stsp }
572 04ca23f4 2018-07-16 stsp
573 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
574 d7d4f210 2018-03-12 stsp if (error != NULL)
575 04ca23f4 2018-07-16 stsp goto done;
576 f42b1b34 2018-03-12 stsp
577 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
578 3235492e 2018-04-01 stsp struct got_reference *head_ref;
579 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
580 3235492e 2018-04-01 stsp if (error != NULL)
581 3235492e 2018-04-01 stsp return error;
582 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
583 3235492e 2018-04-01 stsp got_ref_close(head_ref);
584 3235492e 2018-04-01 stsp if (error != NULL)
585 3235492e 2018-04-01 stsp return error;
586 3235492e 2018-04-01 stsp error = got_object_open(&obj, repo, id);
587 3235492e 2018-04-01 stsp } else {
588 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
589 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
590 3235492e 2018-04-01 stsp if (error == NULL) {
591 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
592 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
593 d1f2edc9 2018-06-13 stsp if (error != NULL)
594 d1f2edc9 2018-06-13 stsp return error;
595 d1f2edc9 2018-06-13 stsp error = got_object_open(&obj, repo, id);
596 d1f2edc9 2018-06-13 stsp if (error != NULL)
597 d1f2edc9 2018-06-13 stsp return error;
598 d1f2edc9 2018-06-13 stsp }
599 d1f2edc9 2018-06-13 stsp if (obj == NULL) {
600 d1f2edc9 2018-06-13 stsp error = got_object_open_by_id_str(&obj, repo,
601 d1f2edc9 2018-06-13 stsp start_commit);
602 d1f2edc9 2018-06-13 stsp if (error != NULL)
603 d1f2edc9 2018-06-13 stsp return error;
604 3235492e 2018-04-01 stsp id = got_object_get_id(obj);
605 3235492e 2018-04-01 stsp if (id == NULL)
606 3235492e 2018-04-01 stsp error = got_error_from_errno();
607 3235492e 2018-04-01 stsp }
608 3235492e 2018-04-01 stsp }
609 d7d4f210 2018-03-12 stsp if (error != NULL)
610 04ca23f4 2018-07-16 stsp goto done;
611 04ca23f4 2018-07-16 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
612 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
613 04ca23f4 2018-07-16 stsp goto done;
614 04ca23f4 2018-07-16 stsp }
615 04ca23f4 2018-07-16 stsp
616 04ca23f4 2018-07-16 stsp error = got_repo_map_path(&in_repo_path, repo, path);
617 04ca23f4 2018-07-16 stsp if (error != NULL)
618 04ca23f4 2018-07-16 stsp goto done;
619 04ca23f4 2018-07-16 stsp if (in_repo_path) {
620 04ca23f4 2018-07-16 stsp free(path);
621 04ca23f4 2018-07-16 stsp path = in_repo_path;
622 04ca23f4 2018-07-16 stsp }
623 04ca23f4 2018-07-16 stsp
624 04ca23f4 2018-07-16 stsp error = print_commits(obj, id, repo, path, show_patch,
625 04ca23f4 2018-07-16 stsp limit, first_parent_traversal);
626 04ca23f4 2018-07-16 stsp done:
627 04ca23f4 2018-07-16 stsp free(path);
628 04ca23f4 2018-07-16 stsp free(repo_path);
629 04ca23f4 2018-07-16 stsp free(cwd);
630 04ca23f4 2018-07-16 stsp if (obj)
631 04ca23f4 2018-07-16 stsp got_object_close(obj);
632 f42b1b34 2018-03-12 stsp free(id);
633 ad242220 2018-09-08 stsp if (repo) {
634 ad242220 2018-09-08 stsp const struct got_error *repo_error;
635 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
636 ad242220 2018-09-08 stsp if (error == NULL)
637 ad242220 2018-09-08 stsp error = repo_error;
638 ad242220 2018-09-08 stsp }
639 8bf5b3c9 2018-03-17 stsp return error;
640 5c860e29 2018-03-12 stsp }
641 5c860e29 2018-03-12 stsp
642 4ed7e80c 2018-05-20 stsp __dead static void
643 3f8b7d6a 2018-04-01 stsp usage_diff(void)
644 3f8b7d6a 2018-04-01 stsp {
645 3f8b7d6a 2018-04-01 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
646 3f8b7d6a 2018-04-01 stsp getprogname());
647 3f8b7d6a 2018-04-01 stsp exit(1);
648 b00d56cd 2018-04-01 stsp }
649 b00d56cd 2018-04-01 stsp
650 4ed7e80c 2018-05-20 stsp static const struct got_error *
651 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
652 b00d56cd 2018-04-01 stsp {
653 b00d56cd 2018-04-01 stsp const struct got_error *error;
654 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
655 b00d56cd 2018-04-01 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
656 b00d56cd 2018-04-01 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
657 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
658 b00d56cd 2018-04-01 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
659 b00d56cd 2018-04-01 stsp int ch;
660 b00d56cd 2018-04-01 stsp
661 b00d56cd 2018-04-01 stsp #ifndef PROFILE
662 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
663 ad242220 2018-09-08 stsp == -1)
664 b00d56cd 2018-04-01 stsp err(1, "pledge");
665 b00d56cd 2018-04-01 stsp #endif
666 b00d56cd 2018-04-01 stsp
667 b00d56cd 2018-04-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
668 b00d56cd 2018-04-01 stsp switch (ch) {
669 b00d56cd 2018-04-01 stsp default:
670 b00d56cd 2018-04-01 stsp usage();
671 b00d56cd 2018-04-01 stsp /* NOTREACHED */
672 b00d56cd 2018-04-01 stsp }
673 b00d56cd 2018-04-01 stsp }
674 b00d56cd 2018-04-01 stsp
675 b00d56cd 2018-04-01 stsp argc -= optind;
676 b00d56cd 2018-04-01 stsp argv += optind;
677 b00d56cd 2018-04-01 stsp
678 b00d56cd 2018-04-01 stsp if (argc == 0) {
679 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
680 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
681 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
682 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
683 e1e3f570 2018-04-01 stsp return got_error_from_errno();
684 3f8b7d6a 2018-04-01 stsp obj_id_str1 = argv[0];
685 3f8b7d6a 2018-04-01 stsp obj_id_str2 = argv[1];
686 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
687 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
688 76089277 2018-04-01 stsp if (repo_path == NULL)
689 76089277 2018-04-01 stsp return got_error_from_errno();
690 b00d56cd 2018-04-01 stsp obj_id_str1 = argv[1];
691 b00d56cd 2018-04-01 stsp obj_id_str2 = argv[2];
692 b00d56cd 2018-04-01 stsp } else
693 b00d56cd 2018-04-01 stsp usage_diff();
694 b00d56cd 2018-04-01 stsp
695 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
696 76089277 2018-04-01 stsp free(repo_path);
697 b00d56cd 2018-04-01 stsp if (error != NULL)
698 b00d56cd 2018-04-01 stsp goto done;
699 b00d56cd 2018-04-01 stsp
700 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
701 b00d56cd 2018-04-01 stsp if (error == NULL) {
702 b00d56cd 2018-04-01 stsp id1 = got_object_get_id(obj1);
703 b00d56cd 2018-04-01 stsp if (id1 == NULL)
704 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
705 b00d56cd 2018-04-01 stsp }
706 b00d56cd 2018-04-01 stsp if (error != NULL)
707 b00d56cd 2018-04-01 stsp goto done;
708 b00d56cd 2018-04-01 stsp
709 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
710 b00d56cd 2018-04-01 stsp if (error == NULL) {
711 b00d56cd 2018-04-01 stsp id2 = got_object_get_id(obj2);
712 b00d56cd 2018-04-01 stsp if (id2 == NULL)
713 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
714 b00d56cd 2018-04-01 stsp }
715 b00d56cd 2018-04-01 stsp if (error != NULL)
716 b00d56cd 2018-04-01 stsp goto done;
717 b00d56cd 2018-04-01 stsp
718 b00d56cd 2018-04-01 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
719 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
720 b00d56cd 2018-04-01 stsp goto done;
721 b00d56cd 2018-04-01 stsp }
722 b00d56cd 2018-04-01 stsp
723 b00d56cd 2018-04-01 stsp switch (got_object_get_type(obj1)) {
724 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
725 f6861a81 2018-09-13 stsp error = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
726 f6861a81 2018-09-13 stsp repo, stdout);
727 b00d56cd 2018-04-01 stsp break;
728 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
729 f6861a81 2018-09-13 stsp error = got_diff_objects_as_trees(obj1, obj2, "", "", repo,
730 f6861a81 2018-09-13 stsp stdout);
731 b00d56cd 2018-04-01 stsp break;
732 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
733 11528a82 2018-05-19 stsp error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
734 b00d56cd 2018-04-01 stsp break;
735 b00d56cd 2018-04-01 stsp default:
736 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
737 b00d56cd 2018-04-01 stsp }
738 b00d56cd 2018-04-01 stsp
739 b00d56cd 2018-04-01 stsp done:
740 b00d56cd 2018-04-01 stsp if (obj1)
741 b00d56cd 2018-04-01 stsp got_object_close(obj1);
742 b00d56cd 2018-04-01 stsp if (obj2)
743 b00d56cd 2018-04-01 stsp got_object_close(obj2);
744 b00d56cd 2018-04-01 stsp if (id1)
745 b00d56cd 2018-04-01 stsp free(id1);
746 b00d56cd 2018-04-01 stsp if (id2)
747 b00d56cd 2018-04-01 stsp free(id2);
748 ad242220 2018-09-08 stsp if (repo) {
749 ad242220 2018-09-08 stsp const struct got_error *repo_error;
750 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
751 ad242220 2018-09-08 stsp if (error == NULL)
752 ad242220 2018-09-08 stsp error = repo_error;
753 ad242220 2018-09-08 stsp }
754 b00d56cd 2018-04-01 stsp return error;
755 404c43c4 2018-06-21 stsp }
756 404c43c4 2018-06-21 stsp
757 404c43c4 2018-06-21 stsp __dead static void
758 404c43c4 2018-06-21 stsp usage_blame(void)
759 404c43c4 2018-06-21 stsp {
760 66bea077 2018-08-02 stsp fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
761 404c43c4 2018-06-21 stsp getprogname());
762 404c43c4 2018-06-21 stsp exit(1);
763 b00d56cd 2018-04-01 stsp }
764 b00d56cd 2018-04-01 stsp
765 404c43c4 2018-06-21 stsp static const struct got_error *
766 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
767 404c43c4 2018-06-21 stsp {
768 404c43c4 2018-06-21 stsp const struct got_error *error;
769 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
770 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
771 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
772 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
773 404c43c4 2018-06-21 stsp int ch;
774 404c43c4 2018-06-21 stsp
775 404c43c4 2018-06-21 stsp #ifndef PROFILE
776 ad242220 2018-09-08 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd", NULL)
777 ad242220 2018-09-08 stsp == -1)
778 404c43c4 2018-06-21 stsp err(1, "pledge");
779 404c43c4 2018-06-21 stsp #endif
780 404c43c4 2018-06-21 stsp
781 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
782 404c43c4 2018-06-21 stsp switch (ch) {
783 404c43c4 2018-06-21 stsp case 'c':
784 404c43c4 2018-06-21 stsp commit_id_str = optarg;
785 404c43c4 2018-06-21 stsp break;
786 66bea077 2018-08-02 stsp case 'r':
787 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
788 66bea077 2018-08-02 stsp if (repo_path == NULL)
789 66bea077 2018-08-02 stsp err(1, "-r option");
790 66bea077 2018-08-02 stsp break;
791 404c43c4 2018-06-21 stsp default:
792 404c43c4 2018-06-21 stsp usage();
793 404c43c4 2018-06-21 stsp /* NOTREACHED */
794 404c43c4 2018-06-21 stsp }
795 404c43c4 2018-06-21 stsp }
796 404c43c4 2018-06-21 stsp
797 404c43c4 2018-06-21 stsp argc -= optind;
798 404c43c4 2018-06-21 stsp argv += optind;
799 404c43c4 2018-06-21 stsp
800 a39318fd 2018-08-02 stsp if (argc == 1)
801 404c43c4 2018-06-21 stsp path = argv[0];
802 a39318fd 2018-08-02 stsp else
803 404c43c4 2018-06-21 stsp usage_blame();
804 404c43c4 2018-06-21 stsp
805 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
806 66bea077 2018-08-02 stsp if (cwd == NULL) {
807 66bea077 2018-08-02 stsp error = got_error_from_errno();
808 66bea077 2018-08-02 stsp goto done;
809 66bea077 2018-08-02 stsp }
810 66bea077 2018-08-02 stsp if (repo_path == NULL) {
811 66bea077 2018-08-02 stsp repo_path = strdup(cwd);
812 66bea077 2018-08-02 stsp if (repo_path == NULL) {
813 66bea077 2018-08-02 stsp error = got_error_from_errno();
814 66bea077 2018-08-02 stsp goto done;
815 66bea077 2018-08-02 stsp }
816 66bea077 2018-08-02 stsp }
817 66bea077 2018-08-02 stsp
818 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
819 404c43c4 2018-06-21 stsp if (error != NULL)
820 404c43c4 2018-06-21 stsp goto done;
821 404c43c4 2018-06-21 stsp
822 66bea077 2018-08-02 stsp error = got_repo_map_path(&in_repo_path, repo, path);
823 66bea077 2018-08-02 stsp if (error != NULL)
824 66bea077 2018-08-02 stsp goto done;
825 66bea077 2018-08-02 stsp
826 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
827 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
828 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
829 404c43c4 2018-06-21 stsp if (error != NULL)
830 66bea077 2018-08-02 stsp goto done;
831 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
832 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
833 404c43c4 2018-06-21 stsp if (error != NULL)
834 66bea077 2018-08-02 stsp goto done;
835 404c43c4 2018-06-21 stsp } else {
836 404c43c4 2018-06-21 stsp struct got_object *obj;
837 404c43c4 2018-06-21 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
838 404c43c4 2018-06-21 stsp if (error != NULL)
839 66bea077 2018-08-02 stsp goto done;
840 404c43c4 2018-06-21 stsp commit_id = got_object_get_id(obj);
841 404c43c4 2018-06-21 stsp if (commit_id == NULL)
842 404c43c4 2018-06-21 stsp error = got_error_from_errno();
843 404c43c4 2018-06-21 stsp got_object_close(obj);
844 404c43c4 2018-06-21 stsp }
845 404c43c4 2018-06-21 stsp
846 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
847 404c43c4 2018-06-21 stsp done:
848 66bea077 2018-08-02 stsp free(in_repo_path);
849 66bea077 2018-08-02 stsp free(repo_path);
850 66bea077 2018-08-02 stsp free(cwd);
851 404c43c4 2018-06-21 stsp free(commit_id);
852 ad242220 2018-09-08 stsp if (repo) {
853 ad242220 2018-09-08 stsp const struct got_error *repo_error;
854 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
855 ad242220 2018-09-08 stsp if (error == NULL)
856 ad242220 2018-09-08 stsp error = repo_error;
857 ad242220 2018-09-08 stsp }
858 404c43c4 2018-06-21 stsp return error;
859 404c43c4 2018-06-21 stsp }
860 404c43c4 2018-06-21 stsp
861 f42b1b34 2018-03-12 stsp #ifdef notyet
862 4ed7e80c 2018-05-20 stsp static const struct got_error *
863 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
864 5c860e29 2018-03-12 stsp {
865 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
866 5c860e29 2018-03-12 stsp git_status_list *status;
867 5c860e29 2018-03-12 stsp git_status_options statusopts;
868 5c860e29 2018-03-12 stsp size_t i;
869 5c860e29 2018-03-12 stsp
870 5c860e29 2018-03-12 stsp git_libgit2_init();
871 5c860e29 2018-03-12 stsp
872 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
873 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
874 5c860e29 2018-03-12 stsp
875 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
876 5c860e29 2018-03-12 stsp errx(1, "bar repository");
877 5c860e29 2018-03-12 stsp
878 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
879 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
880 5c860e29 2018-03-12 stsp
881 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
882 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
883 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
884 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
885 5c860e29 2018-03-12 stsp
886 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
887 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
888 5c860e29 2018-03-12 stsp
889 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
890 5c860e29 2018-03-12 stsp const git_status_entry *se;
891 5c860e29 2018-03-12 stsp
892 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
893 5c860e29 2018-03-12 stsp switch (se->status) {
894 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
895 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
896 5c860e29 2018-03-12 stsp break;
897 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
898 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
899 5c860e29 2018-03-12 stsp break;
900 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
901 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
902 5c860e29 2018-03-12 stsp break;
903 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
904 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
905 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
906 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
907 5c860e29 2018-03-12 stsp break;
908 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
909 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
910 5c860e29 2018-03-12 stsp break;
911 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
912 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
913 5c860e29 2018-03-12 stsp break;
914 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
915 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
916 5c860e29 2018-03-12 stsp break;
917 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
918 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
919 5c860e29 2018-03-12 stsp break;
920 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
921 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
922 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
923 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
924 5c860e29 2018-03-12 stsp break;
925 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
926 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
927 5c860e29 2018-03-12 stsp break;
928 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
929 5c860e29 2018-03-12 stsp default:
930 5c860e29 2018-03-12 stsp break;
931 5c860e29 2018-03-12 stsp }
932 5c860e29 2018-03-12 stsp }
933 5c860e29 2018-03-12 stsp
934 5c860e29 2018-03-12 stsp git_status_list_free(status);
935 5c860e29 2018-03-12 stsp git_repository_free(repo);
936 5c860e29 2018-03-12 stsp git_libgit2_shutdown();
937 5c860e29 2018-03-12 stsp
938 5c860e29 2018-03-12 stsp return 0;
939 5c860e29 2018-03-12 stsp }
940 f42b1b34 2018-03-12 stsp #endif