summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2016-10-28 07:02:26 (GMT)
committerWerner Almesberger <werner@almesberger.net>2016-10-28 07:02:26 (GMT)
commit3f81407dd9a82fddbe130f660e8760e22fe6d287 (patch)
treec7c754f97952eedfa4a943abf74e13710ca67269
parent812bd145ddb39f6fbab53196652ad47520beffd2 (diff)
downloadeeshow-3f81407dd9a82fddbe130f660e8760e22fe6d287.zip
eeshow-3f81407dd9a82fddbe130f660e8760e22fe6d287.tar.gz
eeshow-3f81407dd9a82fddbe130f660e8760e22fe6d287.tar.bz2
file/: retrieve file mtime or commit time
-rw-r--r--file/file.c38
-rw-r--r--file/file.h2
-rw-r--r--file/git-file.c28
-rw-r--r--file/git-file.h2
4 files changed, 53 insertions, 17 deletions
diff --git a/file/file.c b/file/file.c
index 628e47f..ff220cb 100644
--- a/file/file.c
+++ b/file/file.c
@@ -14,7 +14,10 @@
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
+#include <unistd.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include "misc/util.h"
#include "misc/diag.h"
@@ -44,6 +47,19 @@ bool file_oid_eq(const void *a, const void *b)
}
+static void get_time(struct file *file)
+{
+ struct stat st;
+
+ if (fstat(fileno(file->file), &st)) {
+ diag_perror("fstat");
+ file->mtime = 0;
+ } else {
+ file->mtime = st.st_mtime;
+ }
+}
+
+
bool file_cat(const struct file *file, void *user, const char *line)
{
printf("%s\n", line);
@@ -97,6 +113,7 @@ static bool try_related(struct file *file)
free((char *) file->name);
file->name = tmp;
+ get_time(file);
return 1;
}
@@ -140,18 +157,20 @@ static void *open_vcs(struct file *file)
file->related ? file->related->vcs : NULL);
if (file->vcs) {
free(tmp);
- return file->vcs;
+ } else {
+ progress(2, "could not open %s:%s", tmp, colon + 1);
+ return NULL;
}
- progress(2, "could not open %s:%s", tmp, colon + 1);
- return NULL;
} else {
file->vcs = vcs_git_open(NULL, file->name,
file->related ? file->related->vcs : NULL);
- if (file->vcs)
- return file->vcs;
- progress(2, "could not open %s", file->name);
- return NULL;
+ if (!file->vcs) {
+ progress(2, "could not open %s", file->name);
+ return NULL;
+ }
}
+ file->mtime = vcs_git_time(file->vcs);
+ return file->vcs;
}
@@ -188,6 +207,7 @@ bool file_open(struct file *file, const char *name, const struct file *related)
file->file = fopen(name, "r");
if (file->file) {
+ get_time(file);
progress(1, "reading %s", name);
return 1;
}
@@ -249,8 +269,10 @@ bool file_open_revision(struct file *file, const char *rev, const char *name,
file_init(file, name, related);
file->vcs = vcs_git_open(rev, name, related ? related->vcs : NULL);
- if (file->vcs)
+ if (file->vcs) {
+ file->mtime = vcs_git_time(file->vcs);
return 1;
+ }
progress(2, "could not open %s at %s", name, rev);
return 0;
}
diff --git a/file/file.h b/file/file.h
index ff7ea65..d4aa7f7 100644
--- a/file/file.h
+++ b/file/file.h
@@ -15,6 +15,7 @@
#include <stdbool.h>
#include <stdio.h>
+#include <time.h>
struct file {
@@ -22,6 +23,7 @@ struct file {
void *vcs; /* VCS descriptor or NULL */
const char *name; /* name/designator given to file_open */
unsigned lineno;
+ time_t mtime; /* modification time */
const struct file *related; /* NULL if not related to anything */
};
diff --git a/file/git-file.c b/file/git-file.c
index 149143c..6cd8a07 100644
--- a/file/git-file.c
+++ b/file/git-file.c
@@ -15,6 +15,7 @@
#include <stdio.h>
#include <unistd.h>
#include <string.h>
+#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -33,6 +34,7 @@ struct vcs_git {
const struct vcs_git *related;
git_repository *repo;
+ git_commit *commit;
git_tree *tree;
git_object *obj;
@@ -90,23 +92,17 @@ static git_repository *select_repo(const char *path)
}
-static git_tree *pick_revision(git_repository *repo, const char *revision)
+static git_commit *pick_revision(git_repository *repo, const char *revision)
{
- git_commit *commit;
git_object *obj;
- git_tree *tree;
if (git_revparse_single(&obj, repo, revision))
pfatal_git(git_repository_path(repo));
if (git_object_type(obj) != GIT_OBJ_COMMIT)
fatal("%s: not a commit", revision);
- commit = (git_commit *) obj;
-
- if (git_commit_tree(&tree, commit))
- pfatal_git(revision);
- return tree;
+ return (git_commit *) obj;
}
@@ -469,7 +465,10 @@ struct vcs_git *vcs_git_open(const char *revision, const char *name,
if (!revision)
revision = "HEAD";
- vcs_git->tree = pick_revision(vcs_git->repo, revision);
+ vcs_git->commit = pick_revision(vcs_git->repo, revision);
+
+ if (git_commit_tree(&vcs_git->tree, vcs_git->commit))
+ pfatal_git(revision);
if (!access_file_data(vcs_git, name))
goto fail;
@@ -482,6 +481,17 @@ fail:
}
+/* ----- Commit time ------------------------------------------------------- */
+
+
+time_t vcs_git_time(void *ctx)
+{
+ const struct vcs_git *vcs_git = ctx;
+
+ return git_commit_time(vcs_git->commit);
+}
+
+
/* ----- Read -------------------------------------------------------------- */
diff --git a/file/git-file.h b/file/git-file.h
index 4369b0e..d2008ee 100644
--- a/file/git-file.h
+++ b/file/git-file.h
@@ -14,6 +14,7 @@
#define FILE_GIT_FILE_H
#include <stdbool.h>
+#include <time.h>
/*
@@ -37,6 +38,7 @@ bool vcs_git_oid_eq(const void *a, const void *b);
struct vcs_git *vcs_git_open(const char *revision, const char *name,
const struct vcs_git *related);
+time_t vcs_git_time(void *ctx);
bool vcs_git_read(void *ctx, struct file *file,
bool (*parse)(const struct file *file, void *user, const char *line),
void *user);