add got(1) command code from mpi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
diff --git a/got/Makefile b/got/Makefile
new file mode 100644
index 0000000..0ce0ba7
--- /dev/null
+++ b/got/Makefile
@@ -0,0 +1,11 @@
+
+PROG= got
+SRCS= got.c
+
+CFLAGS+= -W -Wall -Wstrict-prototypes -Wunused-variable
+
+# libgit2
+CFLAGS+= -I/usr/local/include
+LDADD+= -L/usr/local/lib -lgit2
+
+.include <bsd.prog.mk>
diff --git a/got/got.1 b/got/got.1
new file mode 100644
index 0000000..ccb72f2
--- /dev/null
+++ b/got/got.1
@@ -0,0 +1,44 @@
+.\"
+.\" Copyright (c) 2017 Martin Pieuchot
+.\"
+.\" Permission to use, copy, modify, and distribute this software for any
+.\" purpose with or without fee is hereby granted, provided that the above
+.\" copyright notice and this permission notice appear in all copies.
+.\"
+.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+.\"
+.Dd $Mdocdate$
+.Dt GOT 1
+.Os
+.Sh NAME
+.Nm got
+.Nd simple version control system
+.Sh SYNOPSIS
+.Nm
+.Ar command
+.Op Ar arg ...
+.Sh DESCRIPTION
+The
+.Nm
+utility is used to manage
+.Xr git 5
+repositories.
+It prioritizes ease of use and simplicity over flexibility.
+.Pp
+The commands are as follows:
+.Bl -tag -width Ds
+.It Cm status
+Show current status of files.
+.It Cm log
+Display history of the repository.
+.El
+.Sh EXIT STATUS
+.Ex -std got
+.Sh SEE ALSO
+.Xr git 5
diff --git a/got/got.c b/got/got.c
new file mode 100644
index 0000000..c18fa2f
--- /dev/null
+++ b/got/got.c
@@ -0,0 +1,214 @@
+/*
+ * Copyright (c) 2017 Martin Pieuchot
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <err.h>
+#include <errno.h>
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <git2.h>
+
+#ifndef nitems
+#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
+#endif
+
+struct cmd {
+ const char *cmd_name;
+ int (*cmd_main)(int, char *[]);
+};
+
+__dead void usage(void);
+
+int cmd_log(int, char *[]);
+int cmd_status(int, char *[]);
+
+struct cmd got_commands[] = {
+ { "log", cmd_log },
+ { "status", cmd_status },
+};
+
+int
+main(int argc, char *argv[])
+{
+ struct cmd *cmd;
+ unsigned int i;
+ int ch;
+
+ setlocale(LC_ALL, "");
+
+ if (pledge("stdio rpath", NULL) == -1)
+ err(1, "pledge");
+
+ while ((ch = getopt(argc, argv, "")) != -1) {
+ switch (ch) {
+ default:
+ usage();
+ /* NOTREACHED */
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc <= 0)
+ usage();
+
+ for (i = 0; i < nitems(got_commands); i++) {
+ cmd = &got_commands[i];
+
+ if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
+ continue;
+
+ return got_commands[i].cmd_main(argc, argv);
+ /* NOTREACHED */
+ }
+
+ fprintf(stderr, "%s: unknown command -- %s\n", getprogname(), argv[0]);
+ return 1;
+}
+
+__dead void
+usage(void)
+{
+ fprintf(stderr, "usage: %s command [arg ...]\n", getprogname());
+ exit(1);
+}
+
+int
+cmd_log(int argc __unused, char *argv[] __unused)
+{
+ git_repository *repo = NULL;
+ git_revwalk *walker = NULL;
+ git_commit *commit = NULL;
+ git_oid oid;
+
+ git_libgit2_init();
+
+ if (git_repository_open_ext(&repo, ".", 0, NULL))
+ errx(1, "git_repository_open: %s", giterr_last()->message);
+
+ if (git_revwalk_new(&walker, repo))
+ errx(1, "git_revwalk_new: %s", giterr_last()->message);
+
+ if (git_revwalk_push_head(walker))
+ errx(1, "git_revwalk_push_head: %s", giterr_last()->message);
+
+ while (git_revwalk_next(&oid, walker) == 0) {
+ char buf[GIT_OID_HEXSZ + 1];
+ const git_signature *sig;
+
+ git_commit_free(commit);
+ if (git_commit_lookup(&commit, repo, &oid))
+ errx(1, "git_commit_lookup: %s", giterr_last()->message);
+
+ printf("-----------------------------------------------\n");
+ git_oid_tostr(buf, sizeof(buf), git_commit_id(commit));
+ printf("commit %s\n", buf);
+
+ if ((sig = git_commit_author(commit)) != NULL) {
+ printf("Author: %s <%s>\n", sig->name, sig->email);
+ }
+ printf("\n%s\n", git_commit_message_raw(commit));
+ }
+ git_commit_free(commit);
+
+ git_repository_free(repo);
+ git_libgit2_shutdown();
+
+ return 0;
+}
+
+int
+cmd_status(int argc __unused, char *argv[] __unused)
+{
+ git_repository *repo = NULL;
+ git_status_list *status;
+ git_status_options statusopts;
+ size_t i;
+
+ git_libgit2_init();
+
+ if (git_repository_open_ext(&repo, ".", 0, NULL))
+ errx(1, "git_repository_open: %s", giterr_last()->message);
+
+ if (git_repository_is_bare(repo))
+ errx(1, "bar repository");
+
+ if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
+ errx(1, "git_status_init_options: %s", giterr_last()->message);
+
+ statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
+ statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+ GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
+ GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
+
+ if (git_status_list_new(&status, repo, &statusopts))
+ errx(1, "git_status_list_new: %s", giterr_last()->message);
+
+ for (i = 0; i < git_status_list_entrycount(status); i++) {
+ const git_status_entry *se;
+
+ se = git_status_byindex(status, i);
+ switch (se->status) {
+ case GIT_STATUS_WT_NEW:
+ printf("? %s\n", se->index_to_workdir->new_file.path);
+ break;
+ case GIT_STATUS_WT_MODIFIED:
+ printf("M %s\n", se->index_to_workdir->new_file.path);
+ break;
+ case GIT_STATUS_WT_DELETED:
+ printf("R %s\n", se->index_to_workdir->new_file.path);
+ break;
+ case GIT_STATUS_WT_RENAMED:
+ printf("m %s -> %s\n",
+ se->index_to_workdir->old_file.path,
+ se->index_to_workdir->new_file.path);
+ break;
+ case GIT_STATUS_WT_TYPECHANGE:
+ printf("t %s\n", se->index_to_workdir->new_file.path);
+ break;
+ case GIT_STATUS_INDEX_NEW:
+ printf("A %s\n", se->head_to_index->new_file.path);
+ break;
+ case GIT_STATUS_INDEX_MODIFIED:
+ printf("M %s\n", se->head_to_index->old_file.path);
+ break;
+ case GIT_STATUS_INDEX_DELETED:
+ printf("R %s\n", se->head_to_index->old_file.path);
+ break;
+ case GIT_STATUS_INDEX_RENAMED:
+ printf("m %s -> %s\n",
+ se->head_to_index->old_file.path,
+ se->head_to_index->new_file.path);
+ break;
+ case GIT_STATUS_INDEX_TYPECHANGE:
+ printf("t %s\n", se->head_to_index->old_file.path);
+ break;
+ case GIT_STATUS_CURRENT:
+ default:
+ break;
+ }
+ }
+
+ git_status_list_free(status);
+ git_repository_free(repo);
+ git_libgit2_shutdown();
+
+ return 0;
+}