1 /*
2  *             Copyright Sönke Ludwig 2014.
3  *  Distributed under the Boost Software License, Version 1.0.
4  *     (See accompanying file LICENSE_1_0.txt or copy at
5  *           http://www.boost.org/LICENSE_1_0.txt)
6  */
7 module git.blob;
8 
9 import git.commit;
10 import git.object_;
11 import git.oid;
12 import git.repository;
13 import git.types;
14 import git.util;
15 import git.version_;
16 
17 import deimos.git2.blob;
18 import deimos.git2.errors;
19 import deimos.git2.types;
20 
21 import std.conv : to;
22 import std.exception : enforce;
23 import std.string : toStringz;
24 
25 
26 GitBlob lookupBlob(GitRepo repo, GitOid oid)
27 {
28 	git_blob* ret;
29 	require(git_blob_lookup(&ret, repo.cHandle, &oid._get_oid()) == 0);
30 	return GitBlob(repo, ret);
31 }
32 
33 GitBlob lookupBlobPrefix(GitRepo repo, GitOid oid, size_t oid_length)
34 {
35 	git_blob* ret;
36 	require(git_blob_lookup_prefix(&ret, repo.cHandle, &oid._get_oid(), oid_length) == 0);
37 	return GitBlob(repo, ret);
38 }
39 
40 GitOid createBlob(GitRepo repo, in ubyte[] buffer)
41 {
42 	GitOid ret;
43 	require(git_blob_create_frombuffer(&ret._get_oid(), repo.cHandle, buffer.ptr, buffer.length) == 0);
44 	return ret;
45 }
46 
47 /*GitOid createBlob(R)(GitRepo repo, R input_range)
48 	if (isInputRange!R)
49 {
50 	alias git_blob_chunk_cb = int function(char *content, size_t max_length, void *payload);
51 		int git_blob_create_fromchunks(
52 		git_oid *id,
53 		git_repository *repo,
54 		const(char)* hintpath,
55 		git_blob_chunk_cb callback,
56 		void *payload);
57 
58 }*/
59 
60 GitOid createBlobFromWorkDir(GitRepo repo, string relative_path)
61 {
62 	GitOid ret;
63 	require(git_blob_create_fromworkdir(&ret._get_oid(), repo.cHandle, relative_path.toStringz()) == 0);
64 	return ret;
65 }
66 
67 GitOid createBlobFromDisk(GitRepo repo, string path)
68 {
69 	GitOid ret;
70 	require(git_blob_create_fromdisk(&ret._get_oid(), repo.cHandle, path.toStringz()) == 0);
71 	return ret;
72 }
73 
74 
75 struct GitBlob {
76 	this(GitObject object)
77 	{
78 		enforce(object.type == GitType.blob, "GIT object is not a blob.");
79 		_object = object;
80 	}
81 
82 	package this(GitRepo repo, git_blob* blob)
83 	{
84 		_object = GitObject(repo, cast(git_object*)blob);
85 	}
86 
87 	@property GitRepo owner() { return _object.owner; }
88 	@property GitOid id() { return GitOid(*git_blob_id(this.cHandle)); }
89 
90 	@property const(ubyte)[] rawContent()
91 	{
92 		auto ptr = git_blob_rawcontent(this.cHandle);
93 		auto length = git_blob_rawsize(this.cHandle);
94 		return cast(const(ubyte)[])ptr[0 .. length];
95 	}
96 
97 	@property bool isBinary() { return requireBool(git_blob_is_binary(this.cHandle)); }
98 
99 	/*ubyte[] getFilteredContent()
100 	{
101 		int git_blob_filtered_content(git_buf *out_, git_blob *blob, const(char)* as_path, int check_for_binary_data);
102 	}*/
103 
104 	package @property inout(git_blob)* cHandle() inout { return cast(inout(git_blob)*)_object.cHandle; }
105 
106 	private GitObject _object;
107 }