summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--datever.c31
1 files changed, 29 insertions, 2 deletions
diff --git a/datever.c b/datever.c
index 626899b..2e90be7 100644
--- a/datever.c
+++ b/datever.c
@@ -14,8 +14,10 @@ struct datever
static int must(const char* msg, int err);
+static int is_dirty(git_repository* repo);
static struct datever find_latest(git_repository* repo);
static struct datever datever_init();
+static struct datever datever_today();
static struct datever datever_from_time(time_t time);
static int datever_is_valid(const struct datever* ver);
static int datever_compare(const struct datever* lhs, const struct datever* rhs);
@@ -29,6 +31,7 @@ main()
must("open repository", git_repository_open_ext(&repo, NULL, GIT_REPOSITORY_OPEN_FROM_ENV, NULL));
const struct datever ver = find_latest(repo);
printf("%d%02d%02d.%d\n", ver.year, ver.month, ver.day, ver.revision);
+ git_libgit2_shutdown();
}
@@ -48,6 +51,23 @@ must(const char* const msg, const int err)
}
+int
+is_dirty(git_repository* repo)
+{
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
+ git_status_options statopts = GIT_STATUS_OPTIONS_INIT;
+#pragma GCC diagnostic pop
+ statopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
+ statopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED;
+ git_status_list* status;
+ must("retrieve status", git_status_list_new(&status, repo, &statopts));
+ const int count = git_status_list_entrycount(status);
+ git_status_list_free(status);
+ return count > 0;
+}
+
+
struct datever
find_latest(git_repository* const repo)
{
@@ -56,14 +76,14 @@ find_latest(git_repository* const repo)
must("find HEAD", git_revwalk_push_head(walker));
git_oid oid;
git_commit* commit;
- struct datever prev = datever_init();
+ struct datever prev = is_dirty(repo) ? datever_today() : datever_init();
struct datever ver = datever_init();
while (0 == git_revwalk_next(&oid, walker)) {
must("find commit", git_commit_lookup(&commit, repo, &oid));
ver = datever_from_time(git_commit_time(commit));
if (datever_is_valid(&prev)) {
if (0 == datever_compare(&prev, &ver))
- ver.revision++;
+ ver.revision += prev.revision;
else break;
}
prev = ver;
@@ -88,6 +108,13 @@ datever_init()
struct datever
+datever_today()
+{
+ return datever_from_time(time(NULL));
+}
+
+
+struct datever
datever_from_time(const time_t time)
{
const struct tm* tm = gmtime(&time);