2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
28 #include "got_error.h"
29 #include "got_object.h"
31 #include "got_repository.h"
34 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
39 const struct got_error *(*cmd_main)(int, char *[]);
40 void (*cmd_usage)(void);
41 const char *cmd_descr;
44 __dead void usage(void);
45 __dead void usage_log(void);
47 const struct got_error* cmd_log(int, char *[]);
48 const struct got_error* cmd_status(int, char *[]);
50 struct cmd got_commands[] = {
51 { "log", cmd_log, usage_log,
52 "show repository history" },
54 { "status", cmd_status, usage_status,
55 "show modification status of files" },
60 main(int argc, char *argv[])
67 setlocale(LC_ALL, "");
69 if (pledge("stdio rpath wpath cpath", NULL) == -1)
72 while ((ch = getopt(argc, argv, "h")) != -1) {
89 for (i = 0; i < nitems(got_commands); i++) {
90 const struct got_error *error;
92 cmd = &got_commands[i];
94 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
98 got_commands[i].cmd_usage();
100 error = got_commands[i].cmd_main(argc, argv);
102 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
109 fprintf(stderr, "%s: unknown command -- %s\n", getprogname(), argv[0]);
118 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
119 "Available commands:\n", getprogname());
120 for (i = 0; i < nitems(got_commands); i++) {
121 struct cmd *cmd = &got_commands[i];
122 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
127 static const struct got_error *
128 print_commit_object(struct got_object *, struct got_object_id *,
129 struct got_repository *);
131 static const struct got_error *
132 print_parent_commits(struct got_commit_object *commit,
133 struct got_repository *repo)
135 struct got_parent_id *pid;
136 const struct got_error *err = NULL;
137 struct got_object *obj;
139 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
140 err = got_object_open(&obj, repo, pid->id);
143 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
144 return got_error(GOT_ERR_OBJ_TYPE);
145 err = print_commit_object(obj, pid->id, repo);
146 got_object_close(obj);
152 static const struct got_error *
153 print_commit_object(struct got_object *obj, struct got_object_id *id,
154 struct got_repository *repo)
156 struct got_commit_object *commit;
158 const struct got_error *err;
160 err = got_object_commit_open(&commit, repo, obj);
164 err = got_object_id_str(&buf, id);
168 printf("-----------------------------------------------\n");
169 printf("commit: %s\n", buf);
170 printf("Author: %s\n", commit->author);
171 printf("\n%s\n", commit->logmsg);
175 err = print_parent_commits(commit, repo);
176 got_object_commit_close(commit);
183 fprintf(stderr, "usage: %s log [REPO_PATH]\n", getprogname());
187 const struct got_error *
188 cmd_log(int argc, char *argv[])
190 const struct got_error *error;
191 struct got_repository *repo;
192 struct got_reference *head_ref;
193 struct got_object_id *id;
194 struct got_object *obj;
195 char *repo_path = NULL;
197 if (pledge("stdio rpath wpath cpath", NULL) == -1)
201 repo_path = getcwd(NULL, 0);
202 if (repo_path == NULL)
204 } else if (argc == 2)
209 error = got_repo_open(&repo, repo_path);
212 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
215 error = got_ref_resolve(&id, repo, head_ref);
219 error = got_object_open(&obj, repo, id);
222 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
223 error = print_commit_object(obj, id, repo);
227 return got_error(GOT_ERR_OBJ_TYPE);
228 got_object_close(obj);
230 got_ref_close(head_ref);
231 got_repo_close(repo);
236 const struct got_error *
237 cmd_status(int argc __unused, char *argv[] __unused)
239 git_repository *repo = NULL;
240 git_status_list *status;
241 git_status_options statusopts;
246 if (git_repository_open_ext(&repo, ".", 0, NULL))
247 errx(1, "git_repository_open: %s", giterr_last()->message);
249 if (git_repository_is_bare(repo))
250 errx(1, "bar repository");
252 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
253 errx(1, "git_status_init_options: %s", giterr_last()->message);
255 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
256 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
257 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
258 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
260 if (git_status_list_new(&status, repo, &statusopts))
261 errx(1, "git_status_list_new: %s", giterr_last()->message);
263 for (i = 0; i < git_status_list_entrycount(status); i++) {
264 const git_status_entry *se;
266 se = git_status_byindex(status, i);
267 switch (se->status) {
268 case GIT_STATUS_WT_NEW:
269 printf("? %s\n", se->index_to_workdir->new_file.path);
271 case GIT_STATUS_WT_MODIFIED:
272 printf("M %s\n", se->index_to_workdir->new_file.path);
274 case GIT_STATUS_WT_DELETED:
275 printf("R %s\n", se->index_to_workdir->new_file.path);
277 case GIT_STATUS_WT_RENAMED:
278 printf("m %s -> %s\n",
279 se->index_to_workdir->old_file.path,
280 se->index_to_workdir->new_file.path);
282 case GIT_STATUS_WT_TYPECHANGE:
283 printf("t %s\n", se->index_to_workdir->new_file.path);
285 case GIT_STATUS_INDEX_NEW:
286 printf("A %s\n", se->head_to_index->new_file.path);
288 case GIT_STATUS_INDEX_MODIFIED:
289 printf("M %s\n", se->head_to_index->old_file.path);
291 case GIT_STATUS_INDEX_DELETED:
292 printf("R %s\n", se->head_to_index->old_file.path);
294 case GIT_STATUS_INDEX_RENAMED:
295 printf("m %s -> %s\n",
296 se->head_to_index->old_file.path,
297 se->head_to_index->new_file.path);
299 case GIT_STATUS_INDEX_TYPECHANGE:
300 printf("t %s\n", se->head_to_index->old_file.path);
302 case GIT_STATUS_CURRENT:
308 git_status_list_free(status);
309 git_repository_free(repo);
310 git_libgit2_shutdown();