reader: a generic way to read files from repos Similar to the `git_iterator` interface, the `git_reader` interface will allow us to read file contents from an arbitrary repository-backed data source (trees, index, or working directory).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
diff --git a/src/reader.c b/src/reader.c
new file mode 100644
index 0000000..754b90a
--- /dev/null
+++ b/src/reader.c
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "reader.h"
+
+#include "fileops.h"
+#include "blob.h"
+
+#include "git2/tree.h"
+#include "git2/blob.h"
+#include "git2/index.h"
+#include "git2/repository.h"
+
+/* tree reader */
+
+typedef struct {
+ git_reader reader;
+ git_tree *tree;
+} tree_reader;
+
+static int tree_reader_read(
+ git_buf *out,
+ git_reader *_reader,
+ const char *filename)
+{
+ tree_reader *reader = (tree_reader *)_reader;
+ git_tree_entry *tree_entry = NULL;
+ git_blob *blob = NULL;
+ int error;
+
+ if ((error = git_tree_entry_bypath(&tree_entry, reader->tree, filename)) < 0 ||
+ (error = git_blob_lookup(&blob, git_tree_owner(reader->tree), git_tree_entry_id(tree_entry))) < 0 ||
+ (error = git_buf_set(out, git_blob_rawcontent(blob), git_blob_rawsize(blob))) < 0)
+ goto done;
+
+done:
+ git_blob_free(blob);
+ git_tree_entry_free(tree_entry);
+ return error;
+}
+
+static void tree_reader_free(git_reader *_reader)
+{
+ GIT_UNUSED(_reader);
+}
+
+int git_reader_for_tree(git_reader **out, git_tree *tree)
+{
+ tree_reader *reader;
+
+ assert(out && tree);
+
+ reader = git__calloc(1, sizeof(tree_reader));
+ GITERR_CHECK_ALLOC(reader);
+
+ reader->reader.read = tree_reader_read;
+ reader->reader.free = tree_reader_free;
+ reader->tree = tree;
+
+ *out = (git_reader *)reader;
+ return 0;
+}
+
+/* workdir reader */
+
+typedef struct {
+ git_reader reader;
+ git_repository *repo;
+} workdir_reader;
+
+static int workdir_reader_read(
+ git_buf *out,
+ git_reader *_reader,
+ const char *filename)
+{
+ workdir_reader *reader = (workdir_reader *)_reader;
+ git_buf path = GIT_BUF_INIT;
+ int error;
+
+ if ((error = git_buf_joinpath(&path,
+ git_repository_workdir(reader->repo), filename)) < 0)
+ goto done;
+
+ /* TODO: should we read the filtered data? */
+ error = git_futils_readbuffer(out, path.ptr);
+
+done:
+ git_buf_dispose(&path);
+ return error;
+}
+
+static void workdir_reader_free(git_reader *_reader)
+{
+ GIT_UNUSED(_reader);
+}
+
+int git_reader_for_workdir(git_reader **out, git_repository *repo)
+{
+ workdir_reader *reader;
+
+ assert(out && repo);
+
+ reader = git__calloc(1, sizeof(workdir_reader));
+ GITERR_CHECK_ALLOC(reader);
+
+ reader->reader.read = workdir_reader_read;
+ reader->reader.free = workdir_reader_free;
+ reader->repo = repo;
+
+ *out = (git_reader *)reader;
+ return 0;
+}
+
+/* index reader */
+
+typedef struct {
+ git_reader reader;
+ git_repository *repo;
+ git_index *index;
+} index_reader;
+
+static int index_reader_read(
+ git_buf *out,
+ git_reader *_reader,
+ const char *filename)
+{
+ index_reader *reader = (index_reader *)_reader;
+ const git_index_entry *entry;
+ git_blob *blob;
+ int error;
+
+ if ((entry = git_index_get_bypath(reader->index, filename, 0)) == NULL)
+ return GIT_ENOTFOUND;
+
+ if ((error = git_blob_lookup(&blob, reader->repo, &entry->id)) < 0)
+ goto done;
+
+ error = git_blob__getbuf(out, blob);
+
+done:
+ git_blob_free(blob);
+ return error;
+}
+
+static void index_reader_free(git_reader *_reader)
+{
+ GIT_UNUSED(_reader);
+}
+
+int git_reader_for_index(
+ git_reader **out,
+ git_repository *repo,
+ git_index *index)
+{
+ index_reader *reader;
+ int error;
+
+ assert(out && repo);
+
+ reader = git__calloc(1, sizeof(index_reader));
+ GITERR_CHECK_ALLOC(reader);
+
+ reader->reader.read = index_reader_read;
+ reader->reader.free = index_reader_free;
+ reader->repo = repo;
+
+ if (index) {
+ reader->index = index;
+ } else {
+ error = git_repository_index__weakptr(&reader->index, repo);
+
+ if (error < 0) {
+ git__free(reader);
+ return error;
+ }
+ }
+
+ *out = (git_reader *)reader;
+ return 0;
+}
+
+/* generic */
+
+int git_reader_read(git_buf *out, git_reader *reader, const char *filename)
+{
+ assert(out && reader && filename);
+
+ return reader->read(out, reader, filename);
+}
+
+void git_reader_free(git_reader *reader)
+{
+ if (!reader)
+ return;
+
+ reader->free(reader);
+ git__free(reader);
+}
diff --git a/src/reader.h b/src/reader.h
new file mode 100644
index 0000000..18c7f59
--- /dev/null
+++ b/src/reader.h
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+#ifndef INCLUDE_reader_h__
+#define INCLUDE_reader_h__
+
+#include "common.h"
+
+typedef struct git_reader git_reader;
+
+/*
+ * The `git_reader` structure is a generic interface for reading the
+ * contents of a file by its name, and implementations are provided
+ * for reading out of a tree, the index, and the working directory.
+ *
+ * Note that the reader implementation is meant to have a short
+ * lifecycle and does not increase the refcount of the object that
+ * it's reading. Callers should ensure that they do not use a
+ * reader after disposing the underlying object that it reads.
+ */
+struct git_reader {
+ int (*read)(git_buf *out, git_reader *reader, const char *filename);
+ void (*free)(git_reader *reader);
+};
+
+/**
+ * Create a `git_reader` that will allow random access to the given
+ * tree. Paths requested via `git_reader_read` will be rooted at this
+ * tree, callers are not expected to recurse through tree lookups. Thus,
+ * you can request to read `/src/foo.c` and the tree provided to this
+ * function will be searched to find another tree named `src`, which
+ * will then be opened to find `foo.c`.
+ *
+ * @param out The reader for the given tree
+ * @param tree The tree object to read
+ * @return 0 on success, or an error code < 0
+ */
+extern int git_reader_for_tree(
+ git_reader **out,
+ git_tree *tree);
+
+/**
+ * Create a `git_reader` that will allow random access to the given
+ * index, or the repository's index.
+ *
+ * @param out The reader for the given index
+ * @param repo The repository containing the index
+ * @param index The index to read, or NULL to use the repository's index
+ * @return 0 on success, or an error code < 0
+ */
+extern int git_reader_for_index(
+ git_reader **out,
+ git_repository *repo,
+ git_index *index);
+
+/**
+ * Create a `git_reader` that will allow random access to the given
+ * repository's working directory. Note that the contents are read
+ * in repository format, meaning any workdir -> odb filters are
+ * applied.
+ *
+ * If `validate_index` is set to true, reads of files will hash the
+ * on-disk contents and ensure that the resulting object ID matches
+ * the repository's index. This ensures that the working directory
+ * is unmodified from the index contents.
+ *
+ * @param out The reader for the given working directory
+ * @param repo The repository containing the working directory
+ * @param validate_index If true, the working directory contents will
+ * be compared to the index contents during read to ensure that
+ * the working directory is unmodified.
+ * @return 0 on success, or an error code < 0
+ */
+extern int git_reader_for_workdir(
+ git_reader **out,
+ git_repository *repo);
+
+/**
+ * Read the given filename from the reader and populate the given buffer
+ * with the contents and the given oid with the object ID.
+ *
+ * @param out The buffer to populate with the file contents
+ * @param out_id The oid to populate with the object ID
+ * @param reader The reader to read
+ * @param filename The filename to read from the reader
+ */
+extern int git_reader_read(
+ git_buf *out,
+ git_reader *reader,
+ const char *filename);
+
+/**
+ * Free the given reader and any associated objects.
+ *
+ * @param reader The reader to free
+ */
+extern void git_reader_free(git_reader *reader);
+
+#endif