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>
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.
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.
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>
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>
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"
41 5c860e29 2018-03-12 stsp #ifndef nitems
42 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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;
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);
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 *[]);
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" },
82 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
84 5c860e29 2018-03-12 stsp struct cmd *cmd;
85 5c860e29 2018-03-12 stsp unsigned int i;
87 1b6b95a8 2018-03-12 stsp int hflag = 0;
89 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
91 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
92 5c860e29 2018-03-12 stsp switch (ch) {
98 5c860e29 2018-03-12 stsp /* NOTREACHED */
102 5c860e29 2018-03-12 stsp argc -= optind;
103 5c860e29 2018-03-12 stsp argv += optind;
104 1e70621d 2018-03-27 stsp optind = 0;
106 5c860e29 2018-03-12 stsp if (argc <= 0)
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;
112 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
114 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
118 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
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);
129 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
133 4ed7e80c 2018-05-20 stsp __dead static void
134 5c860e29 2018-03-12 stsp usage(void)
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);
147 4ed7e80c 2018-05-20 stsp __dead static void
148 c09a553d 2018-03-12 stsp usage_checkout(void)
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());
155 92a684f4 2018-03-12 stsp static void
156 92a684f4 2018-03-12 stsp checkout_progress(void *arg, const char *path)
158 92a684f4 2018-03-12 stsp char *worktree_path = arg;
160 92a684f4 2018-03-12 stsp while (path[0] == '/')
163 92a684f4 2018-03-12 stsp printf("A %s/%s\n", worktree_path, path);
166 4ed7e80c 2018-05-20 stsp static const struct got_error *
167 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
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 = "";
178 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
179 0bb8a95e 2018-03-12 stsp switch (ch) {
181 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
185 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
189 0bb8a95e 2018-03-12 stsp argc -= optind;
190 0bb8a95e 2018-03-12 stsp argv += optind;
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");
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();
206 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
207 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
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();
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();
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();
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();
235 c09a553d 2018-03-12 stsp usage_checkout();
237 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
238 c09a553d 2018-03-12 stsp if (error != NULL)
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)
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)
248 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
249 c09a553d 2018-03-12 stsp if (error != NULL)
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)
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");
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;
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)
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;
275 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, commit->tree_id);
277 79109fed 2018-03-27 stsp return err;
279 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree2, repo, obj);
280 79109fed 2018-03-27 stsp got_object_close(obj);
282 79109fed 2018-03-27 stsp return err;
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;
288 79f35eb3 2018-06-11 stsp err = got_object_open(&obj, repo, qid->id);
290 79109fed 2018-03-27 stsp return err;
292 79109fed 2018-03-27 stsp err = got_object_commit_open(&pcommit, repo, obj);
293 79109fed 2018-03-27 stsp got_object_close(obj);
295 79109fed 2018-03-27 stsp return err;
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);
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);
304 79109fed 2018-03-27 stsp return err;
307 79109fed 2018-03-27 stsp err = got_diff_tree(tree1, tree2, repo, stdout);
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;
314 4bb494d5 2018-06-16 stsp static char *
315 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
317 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
318 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
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)
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;
333 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
335 8bf5b3c9 2018-03-17 stsp return err;
337 788c352e 2018-06-16 stsp author_time = mktime(&commit->tm_author);
338 788c352e 2018-06-16 stsp committer_time = mktime(&commit->tm_committer);
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;
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);
355 3fe1abad 2018-06-10 stsp if (commit->nparents > 1) {
356 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
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);
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);
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();
372 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
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);
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");
384 79109fed 2018-03-27 stsp return err;
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 04ca23f4 2018-07-16 stsp struct got_repository *repo, char *path, int show_patch, int limit,
390 0ed6ed4c 2018-06-13 stsp int first_parent_traversal)
392 f42b1b34 2018-03-12 stsp const struct got_error *err;
393 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
394 04ca23f4 2018-07-16 stsp int ncommits, found_obj = 0;
395 6b765bce 2018-07-22 stsp int is_root_path = (strcmp(path, "/") == 0);
397 0ed6ed4c 2018-06-13 stsp err = got_commit_graph_open(&graph, root_id, first_parent_traversal,
400 f42b1b34 2018-03-12 stsp return err;
401 372ccdbb 2018-06-10 stsp err = got_commit_graph_iter_start(graph, root_id);
405 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
406 372ccdbb 2018-06-10 stsp struct got_object_id *id;
408 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
410 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
411 9ba79e04 2018-06-11 stsp err = NULL;
414 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
416 372ccdbb 2018-06-10 stsp err = got_commit_graph_fetch_commits(&ncommits,
417 372ccdbb 2018-06-10 stsp graph, 1, repo);
423 b43fbaa0 2018-06-11 stsp if (id == NULL)
426 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
429 fcc85cad 2018-07-22 stsp if (!is_root_path) {
430 04ca23f4 2018-07-16 stsp struct got_object *obj;
431 04ca23f4 2018-07-16 stsp struct got_object_qid *pid;
432 e456239b 2018-07-16 stsp int changed = 0;
434 04ca23f4 2018-07-16 stsp err = got_object_open_by_path(&obj, repo, id, path);
436 fcc85cad 2018-07-22 stsp got_object_commit_close(commit);
437 04ca23f4 2018-07-16 stsp if (err->code == GOT_ERR_NO_OBJ && found_obj) {
439 fcc85cad 2018-07-22 stsp * History of the path stops here
440 fcc85cad 2018-07-22 stsp * on the current commit's branch.
441 fcc85cad 2018-07-22 stsp * Keep logging on other branches.
443 04ca23f4 2018-07-16 stsp err = NULL;
448 04ca23f4 2018-07-16 stsp found_obj = 1;
450 04ca23f4 2018-07-16 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
451 04ca23f4 2018-07-16 stsp if (pid != NULL) {
452 04ca23f4 2018-07-16 stsp struct got_object *pobj;
453 04ca23f4 2018-07-16 stsp err = got_object_open_by_path(&pobj, repo,
454 04ca23f4 2018-07-16 stsp pid->id, path);
456 04ca23f4 2018-07-16 stsp if (err->code != GOT_ERR_NO_OBJ) {
457 04ca23f4 2018-07-16 stsp got_object_close(obj);
458 fcc85cad 2018-07-22 stsp got_object_commit_close(commit);
461 04ca23f4 2018-07-16 stsp err = NULL;
462 04ca23f4 2018-07-16 stsp changed = 1;
464 55ad411b 2018-07-16 stsp changed = (got_object_id_cmp(
465 04ca23f4 2018-07-16 stsp got_object_get_id(obj),
466 55ad411b 2018-07-16 stsp got_object_get_id(pobj)) != 0);
467 04ca23f4 2018-07-16 stsp got_object_close(pobj);
470 fcc85cad 2018-07-22 stsp got_object_close(obj);
471 04ca23f4 2018-07-16 stsp if (!changed) {
472 04ca23f4 2018-07-16 stsp got_object_commit_close(commit);
476 1fd6d7ea 2018-06-13 stsp err = print_commit(commit, id, repo, show_patch);
477 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
478 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
480 372ccdbb 2018-06-10 stsp } while (ncommits > 0);
482 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
483 f42b1b34 2018-03-12 stsp return err;
486 4ed7e80c 2018-05-20 stsp __dead static void
487 6f3d1eb0 2018-03-12 stsp usage_log(void)
489 6238ee32 2018-06-13 stsp fprintf(stderr, "usage: %s log [-c commit] [-f] [ -l N ] [-p] "
490 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
494 4ed7e80c 2018-05-20 stsp static const struct got_error *
495 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
497 f42b1b34 2018-03-12 stsp const struct got_error *error;
498 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
499 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
500 d1f2edc9 2018-06-13 stsp struct got_object *obj = NULL;
501 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
502 d142fc45 2018-04-01 stsp char *start_commit = NULL;
504 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
505 64a96a6d 2018-04-01 stsp const char *errstr;
507 6715a751 2018-03-16 stsp #ifndef PROFILE
508 442a3ddc 2018-04-23 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
509 f42b1b34 2018-03-12 stsp err(1, "pledge");
512 04ca23f4 2018-07-16 stsp while ((ch = getopt(argc, argv, "pc:l:fr:")) != -1) {
513 79109fed 2018-03-27 stsp switch (ch) {
515 79109fed 2018-03-27 stsp show_patch = 1;
518 d142fc45 2018-04-01 stsp start_commit = optarg;
521 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
522 64a96a6d 2018-04-01 stsp if (errstr != NULL)
523 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
526 0ed6ed4c 2018-06-13 stsp first_parent_traversal = 1;
529 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
530 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
531 04ca23f4 2018-07-16 stsp err(1, "-r option");
535 79109fed 2018-03-27 stsp /* NOTREACHED */
539 79109fed 2018-03-27 stsp argc -= optind;
540 79109fed 2018-03-27 stsp argv += optind;
542 04ca23f4 2018-07-16 stsp if (argc == 0)
543 04ca23f4 2018-07-16 stsp path = strdup("");
544 04ca23f4 2018-07-16 stsp else if (argc == 1)
545 04ca23f4 2018-07-16 stsp path = strdup(argv[0]);
547 6f3d1eb0 2018-03-12 stsp usage_log();
548 04ca23f4 2018-07-16 stsp if (path == NULL)
549 04ca23f4 2018-07-16 stsp return got_error_from_errno();
551 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
552 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
553 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
556 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
557 04ca23f4 2018-07-16 stsp repo_path = strdup(cwd);
558 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
559 04ca23f4 2018-07-16 stsp error = got_error_from_errno();
564 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
565 d7d4f210 2018-03-12 stsp if (error != NULL)
568 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
569 3235492e 2018-04-01 stsp struct got_reference *head_ref;
570 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
571 3235492e 2018-04-01 stsp if (error != NULL)
572 3235492e 2018-04-01 stsp return error;
573 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
574 3235492e 2018-04-01 stsp got_ref_close(head_ref);
575 3235492e 2018-04-01 stsp if (error != NULL)
576 3235492e 2018-04-01 stsp return error;
577 3235492e 2018-04-01 stsp error = got_object_open(&obj, repo, id);
579 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
580 d1f2edc9 2018-06-13 stsp error = got_ref_open(&ref, repo, start_commit);
581 3235492e 2018-04-01 stsp if (error == NULL) {
582 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
583 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
584 d1f2edc9 2018-06-13 stsp if (error != NULL)
585 d1f2edc9 2018-06-13 stsp return error;
586 d1f2edc9 2018-06-13 stsp error = got_object_open(&obj, repo, id);
587 d1f2edc9 2018-06-13 stsp if (error != NULL)
588 d1f2edc9 2018-06-13 stsp return error;
590 d1f2edc9 2018-06-13 stsp if (obj == NULL) {
591 d1f2edc9 2018-06-13 stsp error = got_object_open_by_id_str(&obj, repo,
592 d1f2edc9 2018-06-13 stsp start_commit);
593 d1f2edc9 2018-06-13 stsp if (error != NULL)
594 d1f2edc9 2018-06-13 stsp return error;
595 3235492e 2018-04-01 stsp id = got_object_get_id(obj);
596 3235492e 2018-04-01 stsp if (id == NULL)
597 3235492e 2018-04-01 stsp error = got_error_from_errno();
600 d7d4f210 2018-03-12 stsp if (error != NULL)
602 04ca23f4 2018-07-16 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
603 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
607 04ca23f4 2018-07-16 stsp error = got_repo_map_path(&in_repo_path, repo, path);
608 04ca23f4 2018-07-16 stsp if (error != NULL)
610 04ca23f4 2018-07-16 stsp if (in_repo_path) {
611 04ca23f4 2018-07-16 stsp free(path);
612 04ca23f4 2018-07-16 stsp path = in_repo_path;
615 04ca23f4 2018-07-16 stsp error = print_commits(obj, id, repo, path, show_patch,
616 04ca23f4 2018-07-16 stsp limit, first_parent_traversal);
618 04ca23f4 2018-07-16 stsp free(path);
619 04ca23f4 2018-07-16 stsp free(repo_path);
622 04ca23f4 2018-07-16 stsp got_object_close(obj);
625 04ca23f4 2018-07-16 stsp got_repo_close(repo);
626 8bf5b3c9 2018-03-17 stsp return error;
629 4ed7e80c 2018-05-20 stsp __dead static void
630 3f8b7d6a 2018-04-01 stsp usage_diff(void)
632 3f8b7d6a 2018-04-01 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
633 3f8b7d6a 2018-04-01 stsp getprogname());
637 4ed7e80c 2018-05-20 stsp static const struct got_error *
638 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
640 b00d56cd 2018-04-01 stsp const struct got_error *error;
641 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
642 b00d56cd 2018-04-01 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
643 b00d56cd 2018-04-01 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
644 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
645 b00d56cd 2018-04-01 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
648 b00d56cd 2018-04-01 stsp #ifndef PROFILE
649 442a3ddc 2018-04-23 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
650 b00d56cd 2018-04-01 stsp err(1, "pledge");
653 b00d56cd 2018-04-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
654 b00d56cd 2018-04-01 stsp switch (ch) {
657 b00d56cd 2018-04-01 stsp /* NOTREACHED */
661 b00d56cd 2018-04-01 stsp argc -= optind;
662 b00d56cd 2018-04-01 stsp argv += optind;
664 b00d56cd 2018-04-01 stsp if (argc == 0) {
665 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
666 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
667 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
668 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
669 e1e3f570 2018-04-01 stsp return got_error_from_errno();
670 3f8b7d6a 2018-04-01 stsp obj_id_str1 = argv[0];
671 3f8b7d6a 2018-04-01 stsp obj_id_str2 = argv[1];
672 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
673 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
674 76089277 2018-04-01 stsp if (repo_path == NULL)
675 76089277 2018-04-01 stsp return got_error_from_errno();
676 b00d56cd 2018-04-01 stsp obj_id_str1 = argv[1];
677 b00d56cd 2018-04-01 stsp obj_id_str2 = argv[2];
679 b00d56cd 2018-04-01 stsp usage_diff();
681 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
682 76089277 2018-04-01 stsp free(repo_path);
683 b00d56cd 2018-04-01 stsp if (error != NULL)
686 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
687 b00d56cd 2018-04-01 stsp if (error == NULL) {
688 b00d56cd 2018-04-01 stsp id1 = got_object_get_id(obj1);
689 b00d56cd 2018-04-01 stsp if (id1 == NULL)
690 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
692 b00d56cd 2018-04-01 stsp if (error != NULL)
695 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
696 b00d56cd 2018-04-01 stsp if (error == NULL) {
697 b00d56cd 2018-04-01 stsp id2 = got_object_get_id(obj2);
698 b00d56cd 2018-04-01 stsp if (id2 == NULL)
699 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
701 b00d56cd 2018-04-01 stsp if (error != NULL)
704 b00d56cd 2018-04-01 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
705 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
709 b00d56cd 2018-04-01 stsp switch (got_object_get_type(obj1)) {
710 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
711 11528a82 2018-05-19 stsp error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
713 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
714 11528a82 2018-05-19 stsp error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
716 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
717 11528a82 2018-05-19 stsp error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
720 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
725 b00d56cd 2018-04-01 stsp got_object_close(obj1);
727 b00d56cd 2018-04-01 stsp got_object_close(obj2);
733 b00d56cd 2018-04-01 stsp got_repo_close(repo);
734 b00d56cd 2018-04-01 stsp return error;
737 404c43c4 2018-06-21 stsp __dead static void
738 404c43c4 2018-06-21 stsp usage_blame(void)
740 404c43c4 2018-06-21 stsp fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
741 404c43c4 2018-06-21 stsp getprogname());
745 404c43c4 2018-06-21 stsp static const struct got_error *
746 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
748 404c43c4 2018-06-21 stsp const struct got_error *error;
749 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
750 404c43c4 2018-06-21 stsp char *repo_path = NULL;
751 404c43c4 2018-06-21 stsp char *path = NULL;
752 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
753 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
756 404c43c4 2018-06-21 stsp #ifndef PROFILE
757 404c43c4 2018-06-21 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
758 404c43c4 2018-06-21 stsp err(1, "pledge");
761 404c43c4 2018-06-21 stsp while ((ch = getopt(argc, argv, "c:")) != -1) {
762 404c43c4 2018-06-21 stsp switch (ch) {
764 404c43c4 2018-06-21 stsp commit_id_str = optarg;
768 404c43c4 2018-06-21 stsp /* NOTREACHED */
772 404c43c4 2018-06-21 stsp argc -= optind;
773 404c43c4 2018-06-21 stsp argv += optind;
775 404c43c4 2018-06-21 stsp if (argc == 0) {
776 404c43c4 2018-06-21 stsp usage_blame();
777 404c43c4 2018-06-21 stsp } else if (argc == 1) {
778 404c43c4 2018-06-21 stsp repo_path = getcwd(NULL, 0);
779 404c43c4 2018-06-21 stsp if (repo_path == NULL)
780 404c43c4 2018-06-21 stsp return got_error_from_errno();
781 404c43c4 2018-06-21 stsp path = argv[0];
782 404c43c4 2018-06-21 stsp } else if (argc == 2) {
783 404c43c4 2018-06-21 stsp repo_path = realpath(argv[0], NULL);
784 404c43c4 2018-06-21 stsp if (repo_path == NULL)
785 404c43c4 2018-06-21 stsp return got_error_from_errno();
786 404c43c4 2018-06-21 stsp path = argv[1];
788 404c43c4 2018-06-21 stsp usage_blame();
790 404c43c4 2018-06-21 stsp error = got_repo_open(&repo, repo_path);
791 404c43c4 2018-06-21 stsp free(repo_path);
792 404c43c4 2018-06-21 stsp if (error != NULL)
795 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
796 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
797 404c43c4 2018-06-21 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
798 404c43c4 2018-06-21 stsp if (error != NULL)
799 404c43c4 2018-06-21 stsp return error;
800 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
801 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
802 404c43c4 2018-06-21 stsp if (error != NULL)
803 404c43c4 2018-06-21 stsp return error;
805 404c43c4 2018-06-21 stsp struct got_object *obj;
806 404c43c4 2018-06-21 stsp error = got_object_open_by_id_str(&obj, repo, commit_id_str);
807 404c43c4 2018-06-21 stsp if (error != NULL)
808 404c43c4 2018-06-21 stsp return error;
809 404c43c4 2018-06-21 stsp commit_id = got_object_get_id(obj);
810 404c43c4 2018-06-21 stsp if (commit_id == NULL)
811 404c43c4 2018-06-21 stsp error = got_error_from_errno();
812 404c43c4 2018-06-21 stsp got_object_close(obj);
815 404c43c4 2018-06-21 stsp error = got_blame(path, commit_id, repo, stdout);
817 404c43c4 2018-06-21 stsp free(commit_id);
819 404c43c4 2018-06-21 stsp got_repo_close(repo);
820 404c43c4 2018-06-21 stsp return error;
823 f42b1b34 2018-03-12 stsp #ifdef notyet
824 4ed7e80c 2018-05-20 stsp static const struct got_error *
825 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
827 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
828 5c860e29 2018-03-12 stsp git_status_list *status;
829 5c860e29 2018-03-12 stsp git_status_options statusopts;
832 5c860e29 2018-03-12 stsp git_libgit2_init();
834 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
835 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
837 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
838 5c860e29 2018-03-12 stsp errx(1, "bar repository");
840 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
841 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
843 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
844 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
845 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
846 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
848 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
849 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
851 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
852 5c860e29 2018-03-12 stsp const git_status_entry *se;
854 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
855 5c860e29 2018-03-12 stsp switch (se->status) {
856 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
857 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
859 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
860 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
862 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
863 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
865 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
866 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
867 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
868 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
870 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
871 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
873 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
874 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
876 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
877 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
879 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
880 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
882 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
883 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
884 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
885 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
887 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
888 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
890 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
896 5c860e29 2018-03-12 stsp git_status_list_free(status);
897 5c860e29 2018-03-12 stsp git_repository_free(repo);
898 5c860e29 2018-03-12 stsp git_libgit2_shutdown();