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>
20 5c860e29 2018-03-12 stsp #include <err.h>
21 5c860e29 2018-03-12 stsp #include <errno.h>
22 5c860e29 2018-03-12 stsp #include <locale.h>
23 5c860e29 2018-03-12 stsp #include <stdio.h>
24 5c860e29 2018-03-12 stsp #include <stdlib.h>
25 5c860e29 2018-03-12 stsp #include <string.h>
26 5c860e29 2018-03-12 stsp #include <unistd.h>
28 f42b1b34 2018-03-12 stsp #include "got_error.h"
29 f42b1b34 2018-03-12 stsp #include "got_object.h"
30 f42b1b34 2018-03-12 stsp #include "got_refs.h"
31 f42b1b34 2018-03-12 stsp #include "got_repository.h"
33 5c860e29 2018-03-12 stsp #ifndef nitems
34 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
37 5c860e29 2018-03-12 stsp struct cmd {
38 5c860e29 2018-03-12 stsp const char *cmd_name;
39 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
40 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
41 46a0db7d 2018-03-12 stsp const char *cmd_descr;
44 5c860e29 2018-03-12 stsp __dead void usage(void);
45 6f3d1eb0 2018-03-12 stsp __dead void usage_log(void);
47 d7d4f210 2018-03-12 stsp const struct got_error* cmd_log(int, char *[]);
48 d7d4f210 2018-03-12 stsp const struct got_error* cmd_status(int, char *[]);
50 5c860e29 2018-03-12 stsp struct cmd got_commands[] = {
51 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
52 1b6b95a8 2018-03-12 stsp "show repository history" },
53 f42b1b34 2018-03-12 stsp #ifdef notyet
54 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
55 1b6b95a8 2018-03-12 stsp "show modification status of files" },
60 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
62 5c860e29 2018-03-12 stsp struct cmd *cmd;
63 5c860e29 2018-03-12 stsp unsigned int i;
65 1b6b95a8 2018-03-12 stsp int hflag = 0;
67 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
69 f42b1b34 2018-03-12 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
70 5c860e29 2018-03-12 stsp err(1, "pledge");
72 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
73 5c860e29 2018-03-12 stsp switch (ch) {
79 5c860e29 2018-03-12 stsp /* NOTREACHED */
83 5c860e29 2018-03-12 stsp argc -= optind;
84 5c860e29 2018-03-12 stsp argv += optind;
86 5c860e29 2018-03-12 stsp if (argc <= 0)
89 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
90 d7d4f210 2018-03-12 stsp const struct got_error *error;
92 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
94 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
98 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
100 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
101 d7d4f210 2018-03-12 stsp if (error) {
102 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
109 5c860e29 2018-03-12 stsp fprintf(stderr, "%s: unknown command -- %s\n", getprogname(), argv[0]);
113 5c860e29 2018-03-12 stsp __dead void
114 5c860e29 2018-03-12 stsp usage(void)
118 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
119 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
120 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
121 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
122 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
127 f42b1b34 2018-03-12 stsp static const struct got_error *
128 f42b1b34 2018-03-12 stsp print_commit_object(struct got_object *, struct got_object_id *,
129 f42b1b34 2018-03-12 stsp struct got_repository *);
131 f42b1b34 2018-03-12 stsp static const struct got_error *
132 f42b1b34 2018-03-12 stsp print_parent_commits(struct got_commit_object *commit,
133 f42b1b34 2018-03-12 stsp struct got_repository *repo)
135 f42b1b34 2018-03-12 stsp struct got_parent_id *pid;
136 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
137 f42b1b34 2018-03-12 stsp struct got_object *obj;
139 f42b1b34 2018-03-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
140 f42b1b34 2018-03-12 stsp err = got_object_open(&obj, repo, pid->id);
141 f42b1b34 2018-03-12 stsp if (err != NULL)
142 f42b1b34 2018-03-12 stsp return err;
143 f42b1b34 2018-03-12 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
144 f42b1b34 2018-03-12 stsp return got_error(GOT_ERR_OBJ_TYPE);
145 f42b1b34 2018-03-12 stsp err = print_commit_object(obj, pid->id, repo);
146 f42b1b34 2018-03-12 stsp got_object_close(obj);
149 f42b1b34 2018-03-12 stsp return err;
152 f42b1b34 2018-03-12 stsp static const struct got_error *
153 f42b1b34 2018-03-12 stsp print_commit_object(struct got_object *obj, struct got_object_id *id,
154 f42b1b34 2018-03-12 stsp struct got_repository *repo)
156 f42b1b34 2018-03-12 stsp struct got_commit_object *commit;
158 f42b1b34 2018-03-12 stsp const struct got_error *err;
160 f42b1b34 2018-03-12 stsp err = got_object_commit_open(&commit, repo, obj);
162 f42b1b34 2018-03-12 stsp return err;
164 f42b1b34 2018-03-12 stsp err = got_object_id_str(&buf, id);
166 f42b1b34 2018-03-12 stsp return err;
168 f42b1b34 2018-03-12 stsp printf("-----------------------------------------------\n");
169 f42b1b34 2018-03-12 stsp printf("commit: %s\n", buf);
170 f42b1b34 2018-03-12 stsp printf("Author: %s\n", commit->author);
171 f42b1b34 2018-03-12 stsp printf("\n%s\n", commit->logmsg);
175 f42b1b34 2018-03-12 stsp err = print_parent_commits(commit, repo);
176 f42b1b34 2018-03-12 stsp got_object_commit_close(commit);
177 f42b1b34 2018-03-12 stsp return err;
180 6f3d1eb0 2018-03-12 stsp __dead void
181 6f3d1eb0 2018-03-12 stsp usage_log(void)
183 6f3d1eb0 2018-03-12 stsp fprintf(stderr, "usage: %s log [REPO_PATH]\n", getprogname());
187 d7d4f210 2018-03-12 stsp const struct got_error *
188 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
190 f42b1b34 2018-03-12 stsp const struct got_error *error;
191 f42b1b34 2018-03-12 stsp struct got_repository *repo;
192 f42b1b34 2018-03-12 stsp struct got_reference *head_ref;
193 f42b1b34 2018-03-12 stsp struct got_object_id *id;
194 f42b1b34 2018-03-12 stsp struct got_object *obj;
195 6f3d1eb0 2018-03-12 stsp char *repo_path = NULL;
197 f42b1b34 2018-03-12 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
198 f42b1b34 2018-03-12 stsp err(1, "pledge");
200 6f3d1eb0 2018-03-12 stsp if (argc == 1) {
201 6f3d1eb0 2018-03-12 stsp repo_path = getcwd(NULL, 0);
202 6f3d1eb0 2018-03-12 stsp if (repo_path == NULL)
203 6f3d1eb0 2018-03-12 stsp err(1, "getcwd");
204 6f3d1eb0 2018-03-12 stsp } else if (argc == 2)
205 6f3d1eb0 2018-03-12 stsp repo_path = argv[1];
207 6f3d1eb0 2018-03-12 stsp usage_log();
209 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
210 d7d4f210 2018-03-12 stsp if (error != NULL)
211 d7d4f210 2018-03-12 stsp return error;
212 d7d4f210 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
213 d7d4f210 2018-03-12 stsp if (error != NULL)
214 d7d4f210 2018-03-12 stsp return error;
215 f42b1b34 2018-03-12 stsp error = got_ref_resolve(&id, repo, head_ref);
216 d7d4f210 2018-03-12 stsp if (error != NULL)
217 d7d4f210 2018-03-12 stsp return error;
219 f42b1b34 2018-03-12 stsp error = got_object_open(&obj, repo, id);
220 d7d4f210 2018-03-12 stsp if (error != NULL)
221 d7d4f210 2018-03-12 stsp return error;
222 f42b1b34 2018-03-12 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
223 f42b1b34 2018-03-12 stsp error = print_commit_object(obj, id, repo);
225 d7d4f210 2018-03-12 stsp return error;
227 d7d4f210 2018-03-12 stsp return got_error(GOT_ERR_OBJ_TYPE);
228 f42b1b34 2018-03-12 stsp got_object_close(obj);
230 f42b1b34 2018-03-12 stsp got_ref_close(head_ref);
231 f42b1b34 2018-03-12 stsp got_repo_close(repo);
232 d7d4f210 2018-03-12 stsp return NULL;
235 f42b1b34 2018-03-12 stsp #ifdef notyet
236 d7d4f210 2018-03-12 stsp const struct got_error *
237 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
239 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
240 5c860e29 2018-03-12 stsp git_status_list *status;
241 5c860e29 2018-03-12 stsp git_status_options statusopts;
244 5c860e29 2018-03-12 stsp git_libgit2_init();
246 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
247 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
249 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
250 5c860e29 2018-03-12 stsp errx(1, "bar repository");
252 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
253 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
255 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
256 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
257 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
258 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
260 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
261 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
263 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
264 5c860e29 2018-03-12 stsp const git_status_entry *se;
266 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
267 5c860e29 2018-03-12 stsp switch (se->status) {
268 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
269 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
271 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
272 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
274 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
275 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
277 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
278 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
279 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
280 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
282 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
283 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
285 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
286 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
288 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
289 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
291 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
292 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
294 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
295 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
296 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
297 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
299 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
300 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
302 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
308 5c860e29 2018-03-12 stsp git_status_list_free(status);
309 5c860e29 2018-03-12 stsp git_repository_free(repo);
310 5c860e29 2018-03-12 stsp git_libgit2_shutdown();