Add support for blob files Blob files can now be loaded from the repository like all the other base Git types. Signed-off-by: Vicent Marti <tanoku@gmail.com>
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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
diff --git a/src/blob.c b/src/blob.c
new file mode 100644
index 0000000..77f5ff8
--- /dev/null
+++ b/src/blob.c
@@ -0,0 +1,146 @@
+/*
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License, version 2,
+ * as published by the Free Software Foundation.
+ *
+ * In addition to the permissions in the GNU General Public License,
+ * the authors give you unlimited permission to link the compiled
+ * version of this file into combinations with other programs,
+ * and to distribute those combinations without any restriction
+ * coming from the use of this file. (The General Public License
+ * restrictions do apply in other respects; for example, they cover
+ * modification of the file, and distribution when not linked into
+ * a combined executable.)
+ *
+ * This file is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; see the file COPYING. If not, write to
+ * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#include "git/common.h"
+#include "git/odb.h"
+#include "git/repository.h"
+
+#include "common.h"
+#include "blob.h"
+
+int git_blob_rawcontent(git_blob *blob, void *buffer, size_t len)
+{
+ int error;
+
+ assert(blob && buffer);
+
+ if (blob->content.data != NULL) {
+
+ if (len + 1 < blob->content.len)
+ return GIT_ENOMEM;
+
+ memcpy(buffer, blob->content.data, blob->content.len);
+ ((char *)buffer)[blob->content.len] = 0;
+
+ } else {
+
+ if (len + 1 < blob->object.source.raw.len)
+ return GIT_ENOMEM;
+
+ if ((error = git_object__source_open((git_object *)blob)) < 0)
+ return error;
+
+ memcpy(buffer, blob->object.source.raw.data, blob->object.source.raw.len);
+ ((char *)buffer)[blob->object.source.raw.len] = 0;
+
+ git_object__source_close((git_object *)blob);
+ }
+
+ return GIT_SUCCESS;
+}
+
+int git_blob_rawsize(git_blob *blob)
+{
+ assert(blob);
+
+ if (blob->content.data != NULL)
+ return blob->content.len;
+
+ return blob->object.source.raw.len;
+}
+
+void git_blob__free(git_blob *blob)
+{
+ gitfo_free_buf(&blob->content);
+}
+
+int git_blob__parse(git_blob *blob)
+{
+ assert(blob);
+ return GIT_SUCCESS;
+}
+
+int git_blob__writeback(git_blob *blob, git_odb_source *src)
+{
+ assert(blob->object.modified);
+
+ if (blob->content.data == NULL)
+ return GIT_EMISSINGOBJDATA;
+
+ return git__source_write(src, blob->content.data, blob->content.len);
+}
+
+int git_blob_set_rawcontent(git_blob *blob, const void *buffer, size_t len)
+{
+ assert(blob && buffer);
+
+ blob->object.modified = 1;
+
+ if (blob->content.data != NULL)
+ gitfo_free_buf(&blob->content);
+
+ blob->content.data = git__malloc(len);
+ blob->content.len = len;
+
+ if (blob->content.data == NULL)
+ return GIT_ENOMEM;
+
+ memcpy(blob->content.data, buffer, len);
+
+ return GIT_SUCCESS;
+}
+
+int git_blob_set_rawcontent_fromfile(git_blob *blob, const char *filename)
+{
+ assert(blob && filename);
+ blob->object.modified = 1;
+
+ if (blob->content.data != NULL)
+ gitfo_free_buf(&blob->content);
+
+ return gitfo_read_file(&blob->content, filename);
+}
+
+int git_blob_writefile(git_oid *written_id, git_repository *repo, const char *path)
+{
+ int error;
+ git_blob *blob;
+
+ if (gitfo_exists(path) < 0)
+ return GIT_ENOTFOUND;
+
+ if ((error = git_blob_new(&blob, repo)) < 0)
+ return error;
+
+ if ((error = git_blob_set_rawcontent_fromfile(blob, path)) < 0)
+ return error;
+
+ if ((error = git_object_write((git_object *)blob)) < 0)
+ return error;
+
+ git_oid_cpy(written_id, git_object_id((git_object *)blob));
+ return GIT_SUCCESS;
+}
+
diff --git a/src/blob.h b/src/blob.h
new file mode 100644
index 0000000..db37d2c
--- /dev/null
+++ b/src/blob.h
@@ -0,0 +1,17 @@
+#ifndef INCLUDE_blob_h__
+#define INCLUDE_blob_h__
+
+#include "git/blob.h"
+#include "repository.h"
+#include "fileops.h"
+
+struct git_blob {
+ git_object object;
+ gitfo_buf content;
+};
+
+void git_blob__free(git_blob *blob);
+int git_blob__parse(git_blob *blob);
+int git_blob__writeback(git_blob *blob, git_odb_source *src);
+
+#endif
diff --git a/src/git/blob.h b/src/git/blob.h
new file mode 100644
index 0000000..23afd64
--- /dev/null
+++ b/src/git/blob.h
@@ -0,0 +1,100 @@
+#ifndef INCLUDE_git_blob_h__
+#define INCLUDE_git_blob_h__
+
+#include "common.h"
+#include "oid.h"
+
+/**
+ * @file git/blob.h
+ * @brief Git blob load and write routines
+ * @defgroup git_blob Git blob load and write routines
+ * @ingroup Git
+ * @{
+ */
+GIT_BEGIN_DECL
+
+/** In-memory representation of a blob object. */
+typedef struct git_blob git_blob;
+
+/**
+ * Lookup a blob object from a repository.
+ * The generated blob object is owned by the revision
+ * repo and shall not be freed by the user.
+ *
+ * @param blob pointer to the looked up blob
+ * @param repo the repo to use when locating the blob.
+ * @param id identity of the blob to locate.
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_blob_lookup(git_blob **blob, git_repository *repo, const git_oid *id);
+
+/**
+ * Create a new in-memory git_blob.
+ *
+ * The blob object must be manually filled using
+ * the 'set_rawcontent' methods before it can
+ * be written back to disk.
+ *
+ * @param blob pointer to the new blob
+ * @param repo The repository where the object will reside
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_blob_new(git_blob **blob, git_repository *repo);
+
+/**
+ * Fill a blob with the contents inside
+ * the pointed file.
+ *
+ * @param blob pointer to the new blob
+ * @param filename name of the file to read
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_blob_set_rawcontent_fromfile(git_blob *blob, const char *filename);
+
+/**
+ * Fill a blob with the contents inside
+ * the pointed buffer
+ *
+ * @param blob pointer to the blob
+ * @param buffer buffer with the contents for the blob
+ * @param len size of the buffer
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_blob_set_rawcontent(git_blob *blob, const void *buffer, size_t len);
+
+/**
+ * Read the raw content of a blob.
+ *
+ * A copy of the raw content is stored on the buffer passed
+ * to the function. If the buffer is not long enough,
+ * the method will fail.
+ *
+ * @param blob pointer to the blob
+ * @param buffer buffer to fill with contents
+ * @param len size of the buffer
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_blob_rawcontent(git_blob *blob, void *buffer, size_t len);
+
+/**
+ * Get the size in bytes of the contents of a blob
+ *
+ * @param blob pointer to the blob
+ * @return size on bytes
+ */
+GIT_EXTERN(int) git_blob_rawsize(git_blob *blob);
+
+/**
+ * Read a file from the working folder of a repository
+ * and write it to the Object Database as a loose blob,
+ * if such doesn't exist yet.
+ *
+ * @param written_id return the id of the written blob
+ * @param repo repository where the blob will be written
+ * @param path file from which the blob will be created
+ */
+GIT_EXTERN(int) git_blob_writefile(git_oid *written_id, git_repository *repo, const char *path);
+
+/** @} */
+GIT_END_DECL
+#endif
diff --git a/src/repository.c b/src/repository.c
index 8cef182..4beb3e0 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -28,6 +28,7 @@
#include "repository.h"
#include "commit.h"
#include "tag.h"
+#include "blob.h"
#include "fileops.h"
static const int default_table_size = 32;
@@ -39,7 +40,7 @@ static const size_t object_sizes[] = {
0,
sizeof(git_commit),
sizeof(git_tree),
- sizeof(git_object), /* TODO: sizeof(git_blob) */
+ sizeof(git_blob),
sizeof(git_tag)
};
@@ -371,6 +372,10 @@ int git_object_write(git_object *object)
error = git_tag__writeback((git_tag *)object, source);
break;
+ case GIT_OBJ_BLOB:
+ error = git_blob__writeback((git_blob *)object, source);
+ break;
+
default:
error = GIT_ERROR;
break;
@@ -404,6 +409,10 @@ void git_object_free(git_object *object)
git_tag__free((git_tag *)object);
break;
+ case GIT_OBJ_BLOB:
+ git_blob__free((git_blob *)object);
+ break;
+
default:
free(object);
break;
@@ -524,8 +533,10 @@ int git_repository_lookup(git_object **object_out, git_repository *repo, const g
break;
case GIT_OBJ_BLOB:
+ error = git_blob__parse((git_blob *)object);
+ break;
+
default:
- /* blobs get no parsing */
break;
}
@@ -550,5 +561,6 @@ int git_repository_lookup(git_object **object_out, git_repository *repo, const g
GIT_NEWOBJECT_TEMPLATE(commit, COMMIT)
GIT_NEWOBJECT_TEMPLATE(tag, TAG)
GIT_NEWOBJECT_TEMPLATE(tree, TREE)
+GIT_NEWOBJECT_TEMPLATE(blob, BLOB)