2 * Copyright (C) 2009, Henon <meinrad.recheis@gmail.com>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 using System
.Collections
.Generic
;
42 using GitSharp
.Core
.Util
;
43 using ObjectId
= GitSharp
.Core
.ObjectId
;
44 using CoreRef
= GitSharp
.Core
.Ref
;
45 using CoreCommit
= GitSharp
.Core
.Commit
;
46 using CoreTree
= GitSharp
.Core
.Tree
;
47 using CoreTag
= GitSharp
.Core
.Tag
;
53 /// Represents a specific version of the content of a file tracked by git. Using a Blob you can access the contents of
54 /// any git object as string or byte array. For tracked files (leaves of a git tree) it returns the content of the file. For git objects
55 /// such as Commit, Tag or Tree the Blob API may be used to inspect the the uncompressed internal representation.
57 /// To open a git object instantiate a Blob with the object's Hash or another valid reference (see Ref).
58 /// <code>var b=new Blob(repo, "e287f54");</code>
60 /// Note, that new Blob( ...) does not create a new blob in the repository but rather constructs the object to manipulate an existing blob.
62 /// Advanced: To create a new Blob inside the repository you can use the static <see cref="Create(GitSharp.Repository,byte[])"/> function, however, you are advised to use
63 /// higher level functionality to create new revisions of files, i.e. by using the <see cref="Commit"/> API, for instance <see cref="Commit.Create(string,GitSharp.Commit,GitSharp.Tree)"/>.
65 public class Blob
: AbstractObject
68 internal Blob(Repository repo
, ObjectId id
)
73 internal Blob(Repository repo
, ObjectId id
, byte[] blob
)
80 /// Create a Blob object which represents an existing blob in the git repository
82 /// <param name="repo">The repository which owns the object to load</param>
83 /// <param name="hash">The SHA1 Hash of the object to load</param>
84 public Blob(Repository repo
, string hash
) : base(repo
, hash
) { }
89 /// Get the uncompressed contents of this Blob as string. This assumes that the contents are encoded in UTF8.
97 return RawParseUtils
.decode(RawData
);
102 /// Get the uncompressed original encoded raw data of the Blob as byte array. This is useful if the contents of the blob are encoded in some legacy encoding instead of UTF8.
104 public byte[] RawData
110 var loader
= _repo
._internal_repo
.OpenBlob(_id
);
113 _blob
= loader
.Bytes
;
119 public override string ToString()
121 return "Blob[" + ShortHash
+ "]";
125 /// Create a new Blob containing the given string data as content. The string will be encoded as UTF8
127 /// <param name="repo"></param>
128 /// <param name="content">string to be stored in the blob</param>
129 /// <returns></returns>
130 public static Blob
Create(Repository repo
, string content
)
132 return Create(repo
, content
, Encoding
.UTF8
);
136 /// Create a new Blob containing the given string data as content. The string will be encoded by the submitted encoding
138 /// <param name="repo"></param>
139 /// <param name="content">string to be stored in the blob</param>
140 /// <param name="encoding"></param>
141 /// <returns></returns>
142 public static Blob
Create(Repository repo
, string content
, Encoding encoding
)
144 return Create(repo
, encoding
.GetBytes(content
));
148 /// Create a new Blob containing the contents of the given file.
150 /// <param name="repo"></param>
151 /// <param name="path">Path to the file that should be stored in the blob</param>
152 /// <returns></returns>
153 public static Blob
CreateFromFile(Repository repo
, string path
)
155 if (new FileInfo(path
).Exists
== false)
156 throw new ArgumentException("File does not exist", "path");
157 return Create(repo
, File
.ReadAllBytes(path
));
161 /// Create a new Blob containing exactly the raw bytes given (before compression).
163 /// <param name="repo"></param>
164 /// <param name="content">Uncompressed, encoded raw data to be stored in the blob</param>
165 /// <returns></returns>
166 public static Blob
Create(Repository repo
, byte[] content
)
168 var db
= repo
._internal_repo
;
169 var id
= new GitSharp
.Core
.ObjectWriter(db
).WriteBlob(content
);
170 return new Blob(repo
, id
, content
);