2016-08-02 17:00:08 +03:00
|
|
|
/*
|
2016-08-18 03:07:13 +03:00
|
|
|
* file/git-file.c - Open and read a file from git version control system
|
2016-08-02 17:00:08 +03:00
|
|
|
*
|
|
|
|
* Written 2016 by Werner Almesberger
|
|
|
|
* Copyright 2016 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE /* for get_current_dir_name */
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <git2.h>
|
|
|
|
|
2016-08-18 03:37:15 +03:00
|
|
|
#include "misc/util.h"
|
|
|
|
#include "misc/diag.h"
|
2016-08-18 03:07:13 +03:00
|
|
|
#include "file/file.h"
|
|
|
|
#include "file/git-util.h"
|
|
|
|
#include "file/git-file.h"
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
|
2016-08-02 21:36:18 +03:00
|
|
|
struct vcs_git {
|
2016-08-03 01:13:28 +03:00
|
|
|
const char *name;
|
|
|
|
const char *revision;
|
|
|
|
const struct vcs_git *related;
|
|
|
|
|
|
|
|
git_repository *repo;
|
|
|
|
git_tree *tree;
|
2016-08-10 19:16:23 +03:00
|
|
|
git_object *obj;
|
2016-08-03 01:13:28 +03:00
|
|
|
|
2016-08-02 21:36:18 +03:00
|
|
|
const void *data;
|
|
|
|
unsigned size;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-08-10 19:16:23 +03:00
|
|
|
/* ----- 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 -------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2016-08-02 17:00:08 +03:00
|
|
|
static git_repository *select_repo(const char *path)
|
|
|
|
{
|
|
|
|
git_repository *repo = NULL;
|
|
|
|
char *tmp = stralloc(path);
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we can't find a repo, this may be due to the file or directory
|
|
|
|
* the path points to not existing in the currently checked-out tree.
|
|
|
|
* So we trim off elements until we find a repository.
|
|
|
|
*/
|
|
|
|
while (1) {
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(3, "trying \"%s\"\n", tmp);
|
2016-08-02 17:00:08 +03:00
|
|
|
if (!git_repository_open_ext(&repo, *tmp ? tmp : "/",
|
|
|
|
GIT_REPOSITORY_OPEN_CROSS_FS, NULL))
|
|
|
|
break;
|
|
|
|
slash = strrchr(tmp, '/');
|
|
|
|
if (!slash)
|
|
|
|
break;
|
|
|
|
*slash = 0;
|
|
|
|
}
|
|
|
|
free(tmp);
|
|
|
|
return repo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static git_tree *pick_revision(git_repository *repo, const char *revision)
|
|
|
|
{
|
|
|
|
git_commit *commit;
|
|
|
|
git_object *obj;
|
|
|
|
git_tree *tree;
|
|
|
|
|
|
|
|
if (git_revparse_single(&obj, repo, revision)) {
|
|
|
|
const git_error *e = giterr_last();
|
|
|
|
|
2016-08-11 23:19:32 +03:00
|
|
|
fatal("%s: %s\n", git_repository_path(repo), e->message);
|
2016-08-02 17:00:08 +03:00
|
|
|
}
|
|
|
|
|
2016-08-11 23:19:32 +03:00
|
|
|
if (git_object_type(obj) != GIT_OBJ_COMMIT)
|
|
|
|
fatal("%s: not a commit\n", revision);
|
2016-08-02 17:00:08 +03:00
|
|
|
commit = (git_commit *) obj;
|
|
|
|
|
|
|
|
if (git_commit_tree(&tree, commit)) {
|
|
|
|
const git_error *e = giterr_last();
|
|
|
|
|
2016-08-11 23:19:32 +03:00
|
|
|
fatal("%s: %s\n", revision, e->message);
|
2016-08-02 17:00:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *canonical_path_into_repo(const char *repo_dir, const char *path)
|
|
|
|
{
|
|
|
|
struct stat repo_st, path_st;
|
|
|
|
char *tmp, *tmp2, *slash, *tail, *real;
|
|
|
|
char *to;
|
|
|
|
const char *end, *from;
|
|
|
|
|
|
|
|
/* identify inode of repo root */
|
|
|
|
|
2016-08-12 16:49:59 +03:00
|
|
|
if (stat(repo_dir, &repo_st) < 0)
|
|
|
|
diag_pfatal(repo_dir);
|
2016-08-11 23:19:32 +03:00
|
|
|
if (!S_ISDIR(repo_st.st_mode))
|
|
|
|
fatal("%s: not a directory\n", repo_dir);
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
/* convert relative paths to absolute */
|
|
|
|
|
|
|
|
if (*path == '/') {
|
|
|
|
tmp = stralloc(path);
|
|
|
|
} else {
|
|
|
|
char *cwd = get_current_dir_name();
|
|
|
|
|
|
|
|
tmp = alloc_size(strlen(cwd) + 1 + strlen(path) + 1);
|
|
|
|
sprintf(tmp, "%s/%s", cwd, path);
|
|
|
|
free(cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove trailing / */
|
|
|
|
|
|
|
|
slash = strrchr(tmp, '/');
|
|
|
|
if (slash && slash != tmp && !slash[1])
|
|
|
|
*slash = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If path does point to inexistent object, separate into the part that
|
|
|
|
* is valid on the current system and the tail containing dead things.
|
|
|
|
*/
|
|
|
|
end = tail = strchr(tmp, 0);
|
|
|
|
|
|
|
|
while (1) {
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(3, "probing \"%s\" tail \"%s\"\n", tmp, tail);
|
2016-08-02 17:00:08 +03:00
|
|
|
if (stat(tmp, &path_st) == 0)
|
|
|
|
break;
|
2016-08-11 23:19:32 +03:00
|
|
|
if (!tmp[1])
|
|
|
|
fatal("%s: cannot resolve\n", path);
|
2016-08-02 17:00:08 +03:00
|
|
|
slash = strrchr(tmp, '/');
|
|
|
|
if (tail != end)
|
|
|
|
tail[-1] = '/';
|
|
|
|
tail = slash + 1;
|
|
|
|
*slash = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove . and .. from tail */
|
|
|
|
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(3, "input tail \"%s\"\n", tail);
|
2016-08-02 17:00:08 +03:00
|
|
|
from = to = tail;
|
|
|
|
while (1) {
|
|
|
|
if (!strncmp(from, "./", 2)) {
|
|
|
|
from += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!strcmp(from, "."))
|
|
|
|
break;
|
|
|
|
if (strncmp(from, "../", 3) && strcmp(from, "..")) {
|
|
|
|
while (*from) {
|
|
|
|
*to++ = *from++;
|
|
|
|
if (from[-1] == '/')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!*from)
|
|
|
|
break;
|
|
|
|
}
|
2016-08-11 23:19:32 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We have something like this:
|
|
|
|
* /home/repo/dead/../../foo
|
|
|
|
*/
|
|
|
|
if (to == tail)
|
|
|
|
fatal("%s: can't climb out of dead path\n", path);
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We have something like
|
|
|
|
* "foo/" -> ""
|
|
|
|
* or
|
|
|
|
* "foo/bar/" -> "foo/"
|
|
|
|
* where "to" points to the end.
|
|
|
|
*/
|
|
|
|
to--;
|
|
|
|
while (to != tail && to[-1] != '/')
|
|
|
|
to--;
|
|
|
|
}
|
|
|
|
*to = 0;
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(3, "output tail \"%s\"\n", tail);
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
/* resolve all symlinks */
|
|
|
|
|
|
|
|
real = realpath(tmp, NULL);
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(3, "realpath(\"%s\") = \"%s\"\n", tmp, real);
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
/* append tail */
|
|
|
|
|
|
|
|
if (*tail) {
|
|
|
|
tmp2 = alloc_size(strlen(real) + 1 + strlen(tail) + 1);
|
|
|
|
sprintf(tmp2, "%s/%s", real, tail);
|
|
|
|
free(real);
|
|
|
|
} else {
|
|
|
|
tmp2 = real;
|
|
|
|
}
|
|
|
|
free(tmp);
|
|
|
|
tmp = tmp2;
|
|
|
|
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(2, "full object path \"%s\"\n", tmp);
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
/* find which part of our path is inside the repo */
|
|
|
|
|
|
|
|
end = tail = strchr(tmp, 0);
|
|
|
|
while (1) {
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(3, "trying \"%s\" tail \"%s\"\n", tmp, tail);
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
if (stat(tmp, &path_st) == 0 &&
|
|
|
|
path_st.st_dev == repo_st.st_dev &&
|
|
|
|
path_st.st_ino == repo_st.st_ino)
|
|
|
|
break;
|
|
|
|
|
2016-08-07 22:13:28 +03:00
|
|
|
slash = strrchr(tmp, '/');
|
|
|
|
|
2016-08-02 17:00:08 +03:00
|
|
|
/* "this cannot happen" */
|
2016-08-11 23:19:32 +03:00
|
|
|
if (tail == tmp || !slash)
|
|
|
|
fatal("divergent paths:\nrepo \"%s\"\nobject \"%s\"\n",
|
2016-08-07 22:13:28 +03:00
|
|
|
repo_dir, tail);
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
if (tail != end)
|
|
|
|
tail[-1] = '/';
|
|
|
|
tail = slash + 1;
|
|
|
|
*slash = 0;
|
|
|
|
}
|
|
|
|
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(2, "path in repo \"%s\"\n", tail);
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
tmp2 = stralloc(tail);
|
|
|
|
free(tmp);
|
|
|
|
return tmp2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static git_tree_entry *find_file(git_repository *repo, git_tree *tree,
|
|
|
|
const char *path)
|
|
|
|
{
|
|
|
|
git_tree_entry *entry;
|
2016-08-16 09:14:50 +03:00
|
|
|
char *repo_path = stralloc(git_repository_workdir(repo));
|
|
|
|
/* use workdir, not path, for submodules */
|
2016-08-02 17:00:08 +03:00
|
|
|
char *slash, *canon_path;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
/* remove trailing / from repo_path */
|
|
|
|
slash = strrchr(repo_path, '/');
|
|
|
|
if (slash && slash != repo_path && !slash[1])
|
|
|
|
*slash = 0;
|
|
|
|
|
|
|
|
len = strlen(repo_path);
|
|
|
|
if (len >= 5 && !strcmp(repo_path + len - 5, "/.git"))
|
|
|
|
repo_path[len == 5 ? 1 : len - 5] = 0;
|
|
|
|
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(2, "repo dir \"%s\"\n", repo_path);
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
canon_path = canonical_path_into_repo(repo_path, path);
|
2016-08-06 07:15:49 +03:00
|
|
|
free(repo_path);
|
2016-08-02 17:00:08 +03:00
|
|
|
|
|
|
|
if (git_tree_entry_bypath(&entry, tree, canon_path)) {
|
|
|
|
const git_error *e = giterr_last();
|
|
|
|
|
2016-08-11 23:19:32 +03:00
|
|
|
error("%s: %s\n", path, e->message);
|
2016-08-06 07:15:49 +03:00
|
|
|
free(canon_path);
|
|
|
|
return NULL;
|
2016-08-02 17:00:08 +03:00
|
|
|
}
|
|
|
|
free(canon_path);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-10 19:16:23 +03:00
|
|
|
static const void *get_data(struct vcs_git *vcs_git, git_tree_entry *entry,
|
2016-08-02 17:00:08 +03:00
|
|
|
unsigned *size)
|
|
|
|
{
|
2016-08-10 19:16:23 +03:00
|
|
|
git_repository *repo =vcs_git->repo;
|
2016-08-02 17:00:08 +03:00
|
|
|
git_object *obj;
|
|
|
|
git_blob *blob;
|
|
|
|
|
2016-08-11 23:19:32 +03:00
|
|
|
if (git_tree_entry_type(entry) != GIT_OBJ_BLOB)
|
|
|
|
fatal("entry is not a blob\n");
|
2016-08-02 17:00:08 +03:00
|
|
|
if (git_tree_entry_to_object(&obj, repo, entry)) {
|
|
|
|
const git_error *e = giterr_last();
|
|
|
|
|
2016-08-11 23:19:32 +03:00
|
|
|
fatal("%s\n", e->message);
|
2016-08-02 17:00:08 +03:00
|
|
|
}
|
2016-08-10 19:16:23 +03:00
|
|
|
vcs_git->obj = obj;
|
2016-08-02 17:00:08 +03:00
|
|
|
|
2016-08-03 01:13:28 +03:00
|
|
|
if (verbose > 2) {
|
2016-08-05 01:25:31 +03:00
|
|
|
git_buf buf = { 0 };
|
2016-08-03 01:13:28 +03:00
|
|
|
|
|
|
|
if (git_object_short_id(&buf, obj)) {
|
|
|
|
const git_error *e = giterr_last();
|
|
|
|
|
2016-08-11 23:19:32 +03:00
|
|
|
fatal("%s\n", e->message);
|
2016-08-03 01:13:28 +03:00
|
|
|
}
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(3, "object %s\n", buf.ptr);
|
2016-08-03 01:13:28 +03:00
|
|
|
git_buf_free(&buf);
|
|
|
|
}
|
2016-08-02 17:00:08 +03:00
|
|
|
blob = (git_blob *) obj;
|
|
|
|
*size = git_blob_rawsize(blob);
|
|
|
|
return git_blob_rawcontent(blob);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool send_line(const char *s, unsigned len,
|
2016-08-02 21:36:18 +03:00
|
|
|
bool (*parse)(const struct file *file, void *user, const char *line),
|
|
|
|
void *user, const struct file *file)
|
2016-08-02 17:00:08 +03:00
|
|
|
{
|
|
|
|
char *tmp = alloc_size(len + 1);
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
memcpy(tmp, s, len);
|
|
|
|
tmp[len] = 0;
|
2016-08-02 21:36:18 +03:00
|
|
|
res = parse(file, user, tmp);
|
2016-08-02 17:00:08 +03:00
|
|
|
free(tmp);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-06 07:15:49 +03:00
|
|
|
static bool access_file_data(struct vcs_git *vcs_git, const char *name)
|
2016-08-03 01:21:43 +03:00
|
|
|
{
|
|
|
|
git_tree_entry *entry;
|
|
|
|
|
|
|
|
entry = find_file(vcs_git->repo, vcs_git->tree, name);
|
2016-08-06 07:15:49 +03:00
|
|
|
if (!entry)
|
|
|
|
return 0;
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(1, "reading %s\n", name);
|
2016-08-03 01:21:43 +03:00
|
|
|
|
2016-08-10 19:16:23 +03:00
|
|
|
vcs_git->data = get_data(vcs_git, entry, &vcs_git->size);
|
2016-08-06 07:15:49 +03:00
|
|
|
return 1;
|
2016-08-03 01:21:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-03 01:13:28 +03:00
|
|
|
static bool related_same_repo(struct vcs_git *vcs_git)
|
|
|
|
{
|
2016-08-03 01:21:43 +03:00
|
|
|
const struct vcs_git *related = vcs_git->related;
|
|
|
|
|
|
|
|
vcs_git->repo = related->repo;
|
|
|
|
vcs_git->tree = related->tree;
|
|
|
|
|
2016-08-06 07:15:49 +03:00
|
|
|
return access_file_data(vcs_git, vcs_git->name);
|
2016-08-03 01:13:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool related_other_repo(struct vcs_git *vcs_git)
|
|
|
|
{
|
2016-08-10 22:03:16 +03:00
|
|
|
static bool shown = 0;
|
|
|
|
|
2016-08-03 01:13:28 +03:00
|
|
|
/* @@@ find revision <= date of revision in related */
|
2016-08-10 22:03:16 +03:00
|
|
|
if (!shown)
|
2016-08-22 11:02:09 +03:00
|
|
|
warning("related_other_repo is not yet implemented");
|
2016-08-10 22:03:16 +03:00
|
|
|
shown = 1;
|
2016-08-03 01:13:28 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool related_only_repo(struct vcs_git *vcs_git)
|
|
|
|
{
|
|
|
|
const struct vcs_git *related = vcs_git->related;
|
|
|
|
char *tmp;
|
|
|
|
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(2, "trying graft \"%s\" \"%s\"\n",
|
|
|
|
related->name, vcs_git->name);
|
2016-08-03 01:13:28 +03:00
|
|
|
tmp = file_graft_relative(related->name, vcs_git->name);
|
|
|
|
if (!tmp)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
vcs_git->repo = related->repo;
|
|
|
|
vcs_git->tree = related->tree;
|
|
|
|
|
2016-08-06 07:15:49 +03:00
|
|
|
if (!access_file_data(vcs_git, tmp)) {
|
|
|
|
free(tmp);
|
|
|
|
return 0;
|
|
|
|
}
|
2016-08-03 01:13:28 +03:00
|
|
|
|
|
|
|
free((char *) vcs_git->name);
|
|
|
|
vcs_git->name = tmp;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool try_related(struct vcs_git *vcs_git)
|
|
|
|
{
|
|
|
|
if (!vcs_git->related)
|
|
|
|
return 0;
|
|
|
|
if (vcs_git->revision)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
vcs_git->repo = select_repo(vcs_git->name);
|
|
|
|
if (vcs_git->repo) {
|
|
|
|
if (!strcmp(git_repository_path(vcs_git->related->repo),
|
|
|
|
git_repository_path(vcs_git->repo)))
|
|
|
|
return related_same_repo(vcs_git);
|
|
|
|
else
|
|
|
|
return related_other_repo(vcs_git);
|
|
|
|
}
|
|
|
|
|
|
|
|
return related_only_repo(vcs_git);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-05 01:40:29 +03:00
|
|
|
struct vcs_git *vcs_git_open(const char *revision, const char *name,
|
|
|
|
const struct vcs_git *related)
|
|
|
|
{
|
|
|
|
struct vcs_git *vcs_git = alloc_type(struct vcs_git);
|
|
|
|
|
2016-08-05 14:56:08 +03:00
|
|
|
git_init_once();
|
2016-08-02 17:00:08 +03:00
|
|
|
|
2016-08-03 01:13:28 +03:00
|
|
|
vcs_git->name = stralloc(name);
|
|
|
|
vcs_git->revision = revision ? stralloc(revision) : NULL;
|
|
|
|
vcs_git->related = related;
|
|
|
|
|
|
|
|
if (try_related(vcs_git))
|
|
|
|
return vcs_git;
|
|
|
|
|
|
|
|
vcs_git->repo = select_repo(name);
|
|
|
|
if (!vcs_git->repo) {
|
2016-08-11 23:19:32 +03:00
|
|
|
error("%s: not found\n", name);
|
2016-08-06 07:15:49 +03:00
|
|
|
goto fail;
|
2016-08-02 17:00:08 +03:00
|
|
|
}
|
2016-08-11 23:00:44 +03:00
|
|
|
progress(2, "using repository %s\n",
|
|
|
|
git_repository_path(vcs_git->repo));
|
2016-08-02 17:00:08 +03:00
|
|
|
|
2016-08-03 01:13:28 +03:00
|
|
|
if (!revision)
|
|
|
|
revision = "HEAD";
|
|
|
|
vcs_git->tree = pick_revision(vcs_git->repo, revision);
|
2016-08-02 21:36:18 +03:00
|
|
|
|
2016-08-06 07:15:49 +03:00
|
|
|
if (!access_file_data(vcs_git, name))
|
|
|
|
goto fail;
|
2016-08-02 21:36:18 +03:00
|
|
|
|
|
|
|
return vcs_git;
|
2016-08-06 07:15:49 +03:00
|
|
|
|
|
|
|
fail:
|
|
|
|
vcs_git_close(vcs_git);
|
|
|
|
return 0;
|
2016-08-02 21:36:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-10 19:16:23 +03:00
|
|
|
/* ----- Read -------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2016-08-06 02:42:42 +03:00
|
|
|
bool vcs_git_read(void *ctx, struct file *file,
|
2016-08-05 01:25:31 +03:00
|
|
|
bool (*parse)(const struct file *file, void *user, const char *line),
|
2016-08-02 21:36:18 +03:00
|
|
|
void *user)
|
|
|
|
{
|
|
|
|
const struct vcs_git *vcs_git = ctx;
|
|
|
|
const char *end = vcs_git->data + vcs_git->size;
|
|
|
|
const char *p = vcs_git->data;
|
|
|
|
const char *nl;
|
|
|
|
|
|
|
|
while (p != end) {
|
|
|
|
nl = memchr(p, '\n', end - p);
|
|
|
|
file->lineno++;
|
2016-08-06 02:42:42 +03:00
|
|
|
if (!nl)
|
|
|
|
return send_line(p, end - p, parse, user, file);
|
2016-08-02 21:36:18 +03:00
|
|
|
if (!send_line(p, nl - p, parse, user, file))
|
2016-08-06 02:42:42 +03:00
|
|
|
return 0;
|
2016-08-02 21:36:18 +03:00
|
|
|
p = nl + 1;
|
|
|
|
}
|
2016-08-06 02:42:42 +03:00
|
|
|
return 1;
|
2016-08-02 21:36:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-10 19:16:23 +03:00
|
|
|
/* ----- Close ------------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2016-08-02 21:36:18 +03:00
|
|
|
void vcs_git_close(void *ctx)
|
|
|
|
{
|
2016-08-03 01:13:28 +03:00
|
|
|
struct vcs_git *vcs_git = ctx;
|
|
|
|
|
|
|
|
free((char *) vcs_git->name);
|
|
|
|
free((char *) vcs_git->revision);
|
|
|
|
free(vcs_git);
|
2016-08-02 17:00:08 +03:00
|
|
|
}
|