diff options
author | Aki <please@ignore.pl> | 2024-01-20 01:39:01 +0100 |
---|---|---|
committer | Aki <please@ignore.pl> | 2024-01-20 01:40:04 +0100 |
commit | ba708d4d01e050cc8d160fe1e4a241af26dd4f16 (patch) | |
tree | 69bdcbd122ac22987c66a5bf6f7860d7e8a7e245 | |
download | datever-ba708d4d01e050cc8d160fe1e4a241af26dd4f16.zip datever-ba708d4d01e050cc8d160fe1e4a241af26dd4f16.tar.gz datever-ba708d4d01e050cc8d160fe1e4a241af26dd4f16.tar.bz2 |
Implemented version based on number of commits on HEAD day
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 13 | ||||
-rw-r--r-- | datever.c | 65 |
3 files changed, 79 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..14e06db --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +datever diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5a5f407 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +CFLAGS=-Wall -Wextra -Wpedantic -O3 +CFLAGS+=`pkg-config --cflags libgit2` +LDLIBS+=`pkg-config --libs libgit2` + + +all: datever + + +clean: + rm -f datever + + +.PHONY: all clean diff --git a/datever.c b/datever.c new file mode 100644 index 0000000..a8c63df --- /dev/null +++ b/datever.c @@ -0,0 +1,65 @@ +#include <stdio.h> +#include <string.h> +#include <time.h> + +#include <git2.h> + + +static int must(const char* msg, int err); + + +int +main() +{ + git_libgit2_init(); + git_repository* repo = NULL; + must("open repository", git_repository_open_ext(&repo, NULL, GIT_REPOSITORY_OPEN_FROM_ENV, NULL)); + git_revwalk* walker = NULL; + must("walk revisions", git_revwalk_new(&walker, repo)); + must("find HEAD", git_revwalk_push_head(walker)); + git_oid oid; + char prev[37]; + prev[0] = 0; + char date[37]; + int count = 0; + while (0 == git_revwalk_next(&oid, walker)) { + git_commit* commit; + must("find commit", git_commit_lookup(&commit, repo, &oid)); + const git_time_t gt = git_commit_time(commit); + const struct tm* const tm = gmtime(>); + if (NULL == tm) { + perror("read time"); + exit(1); + } + snprintf( + date, + 37, + "%d%02d%02d", + tm->tm_year + 1900, + tm->tm_mon + 1, + tm->tm_mday); + if (0 != prev[0]) { + if (0 == strncmp(prev, date, 37)) + count++; + else break; // Last commit is not deallocated! + } + strcpy(prev, date); + git_commit_free(commit); + } + printf("%s.%d\n", date, count + 1); +} + + +/// If [[err]] is considered erroneuos, print [[msg]] and the last libgit2 error to standard error and then terminate. +int +must(const char* msg, const int err) +{ + if (0 > err) { + const git_error* const e = git_error_last(); + if (NULL != msg) + dprintf(2, "%s: ", msg); + dprintf(2, "%s\n", e->message); + exit(1); + } + return err; +} |