diff --git a/eeshow/file.c b/eeshow/file.c index d2718c6..6910899 100644 --- a/eeshow/file.c +++ b/eeshow/file.c @@ -22,6 +22,24 @@ #include "file.h" +void *file_oid(const struct file *file) +{ + if (!file->vcs) + return NULL; + return vcs_git_get_oid(file->vcs); +} + + +bool file_oid_eq(const void *a, const void *b) +{ + /* + * If both a and b are NULL, we don't have revision data and thus + * can't tell if they're identical. + */ + return a && b && vcs_git_oid_eq(a, b); +} + + bool file_cat(const struct file *file, void *user, const char *line) { printf("%s\n", line); diff --git a/eeshow/file.h b/eeshow/file.h index 10d012d..4d5f886 100644 --- a/eeshow/file.h +++ b/eeshow/file.h @@ -26,6 +26,9 @@ struct file { }; +void *file_oid(const struct file *file); +bool file_oid_eq(const void *a, const void *b); + bool file_cat(const struct file *file, void *user, const char *line); char *file_graft_relative(const char *base, const char *name); diff --git a/eeshow/git-file.c b/eeshow/git-file.c index 26ff1c6..78d4a9b 100644 --- a/eeshow/git-file.c +++ b/eeshow/git-file.c @@ -34,12 +34,36 @@ struct vcs_git { git_repository *repo; git_tree *tree; + git_object *obj; const void *data; unsigned size; }; +/* ----- OID matching ------------------------------------------------------ */ + + +void *vcs_git_get_oid(const void *ctx) +{ + const struct vcs_git *vcs_git = ctx; + struct git_oid *new; + + new = alloc_type(git_oid); + git_oid_cpy(new, git_object_id(vcs_git->obj)); + return new; +} + + +bool vcs_git_oid_eq(const void *a, const void *b) +{ + return !git_oid_cmp(a, b); +} + + +/* ----- Open -------------------------------------------------------------- */ + + static git_repository *select_repo(const char *path) { git_repository *repo = NULL; @@ -298,9 +322,10 @@ static git_tree_entry *find_file(git_repository *repo, git_tree *tree, } -static const void *get_data(git_repository *repo, git_tree_entry *entry, +static const void *get_data(struct vcs_git *vcs_git, git_tree_entry *entry, unsigned *size) { + git_repository *repo =vcs_git->repo; git_object *obj; git_blob *blob; @@ -314,6 +339,7 @@ static const void *get_data(git_repository *repo, git_tree_entry *entry, fprintf(stderr, "%s\n", e->message); exit(1); } + vcs_git->obj = obj; if (verbose > 2) { git_buf buf = { 0 }; @@ -358,7 +384,7 @@ static bool access_file_data(struct vcs_git *vcs_git, const char *name) if (verbose) fprintf(stderr, "reading %s\n", name); - vcs_git->data = get_data(vcs_git->repo, entry, &vcs_git->size); + vcs_git->data = get_data(vcs_git, entry, &vcs_git->size); return 1; } @@ -467,6 +493,9 @@ fail: } +/* ----- Read -------------------------------------------------------------- */ + + bool vcs_git_read(void *ctx, struct file *file, bool (*parse)(const struct file *file, void *user, const char *line), void *user) @@ -489,6 +518,9 @@ bool vcs_git_read(void *ctx, struct file *file, } +/* ----- Close ------------------------------------------------------------- */ + + void vcs_git_close(void *ctx) { struct vcs_git *vcs_git = ctx; diff --git a/eeshow/git-file.h b/eeshow/git-file.h index 7faba3a..973aedb 100644 --- a/eeshow/git-file.h +++ b/eeshow/git-file.h @@ -32,6 +32,9 @@ struct file; void vcs_git_init(void); +void *vcs_git_get_oid(const void *ctx); /* mallocs */ +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); bool vcs_git_read(void *ctx, struct file *file,