private bool ObjectExist (SHA1 id)
		{
			if (File.Exists (GetObjectFullPath (id)))
				return true;
			
			return false;
		}
示例#2
0
		public Commit (SHA1 treeId, SHA1 parentId, string authorName,
		               string commiterName, string authorEmail, string commiterEmail, 
		               string messageContent) : base (Type.Commit, Encoding.UTF8.GetBytes (authorName)) // TODO: ENCODE
		{
			tree = treeId;
			parent = parentId;
			
			author.Name = authorName;
			author.Email = authorEmail;
			
			commiter.Name = commiterName;
			commiter.Name = commiterEmail;
			
			message = messageContent;
		}
		public Object Get (SHA1 key)
		{
			if (cache == null)
				cache = new Dictionary<SHA1,Object>();
			
			if (cache.ContainsKey (key))
				return cache[key];
			
			if (ObjectExist (key)) {
				cache.Add (key, RetrieveObject (key));
			} else {
				throw new ArgumentException (String.Format ("The specified key {0} does not exist", key.ToHexString ()));
			}
			
			return cache[key];
		}
示例#4
0
		public TreeEntry (byte[] mode, string name, SHA1 id)
		{
			if (id.Bytes.Length != 20)
				throw new ArgumentException (String.Format
				                             ("The size of the SHA1 is incorrect, tree entry is invalid has to be 20 and is: ", 
				                              id.Bytes.Length));
			
			if (String.IsNullOrEmpty (name))
				throw new ArgumentException ("Name is null, tree entry is invalid");
			
			if (mode.Length < 5)
				throw new ArgumentException ("mode size is incorrect, tree entry is invalid, size has to be longer than 5 or 5");
			
			this.mode = new GitFileMode (mode);
			this.name = name;
			this.id = id;
		}
		public static void ViewCompressedFile (string filePath)
		{
			FileStream fs = File.Open (filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
			
			byte[] bytes = new byte[(int) fs.Length];
			
			fs.Read (bytes, 0, (int) fs.Length);
			
			byte[] content = ObjectStore.Decompress (bytes);
			
			SHA1 id = new SHA1 (content, true);
			
			Console.WriteLine ("ID: {0}", id.ToHexString ());
			Console.WriteLine ("Path {0}", filePath);
			
			Console.WriteLine ("Length: " + content.Length);
			
			foreach (char c in content) {
				if (c == '\n') {
					Console.WriteLine ("\\n");
					continue;
				}
				if (c == '\0')
					Console.Write ("[NULL]");
				Console.Write (c);
			}
		}
		public static void CheckoutTest (string hash, string objStorePath)
		{
			ObjectStore store = new ObjectStore (objStorePath);
			
			SHA1 id = new SHA1 (SHA1.FromHexString (hash), false);
			
			Console.WriteLine ("Hash: " + hash);
			Console.WriteLine ("Id:   " + id.ToHexString ());
			
			Console.WriteLine ("hash created");
			
			Tree tree = (Tree) store.Get (id);
			
			Console.WriteLine ("tree created No. entries: " + tree.Entries.Length);
			
			store.Checkout (Environment.CurrentDirectory, tree);
			
			Console.WriteLine ("Checkout done WIIIII!!!");
		}
		public static void ReadGitTree ()
		{
			FileStream fs = new FileStream ("c45099a4cd4ba61a32e1c61d987b73dc3b6146b9", FileMode.Open);
			Console.WriteLine ("SHA1 = c45099a4cd4ba61a32e1c61d987b73dc3b6146b9");
			
			BinaryReader br = new BinaryReader (fs);
			int len = (int)fs.Length;
			
			byte[] content = br.ReadBytes (len);
			
			Console.WriteLine ("First 2 bytes: {0} {1}", (int)content[0], (int)content[1]);
			Console.WriteLine ("Last 4 bytes: {0}{1}{2}{3}", (int)content[len - 4], (int)content[len - 3], 
			                   (int)content[len - 2], (int)content[len - 1]);
			
			byte[] deflated = Mono.Git.Core.ObjectStore.Decompress (content);
			
			int pos = 0;
			
			Console.Write ("header: ");
			
			for (; deflated[pos] != '\0'; pos++) {
				Console.Write ((char) deflated[pos]);
			}
			
			if (deflated[pos] == '\0') {
				Console.WriteLine ("[null]");
				pos++;
			}
			
			Console.WriteLine ("Content: ");
			
			byte[] deflatedNoHeader = new byte [deflated.Length - pos];
			
			Array.Copy (deflated, pos, deflatedNoHeader, 0, deflatedNoHeader.Length);
			
			pos = 0;
			int count = 0;
			
//			for (; pos < deflatedNoHeader.Length; pos++) {
//				if (deflatedNoHeader [pos] == '\0') {
//					count = 0;
//					Console.Write ("[#{0} null]", pos);
//					pos += 21;
//					if (pos > deflatedNoHeader.Length)
//						break;
//					byte[] bytes = new byte[20];
//					Array.Copy (deflatedNoHeader, pos, bytes, 0, 20);
//					SHA1 id = new SHA1 (bytes, true);
//					
//					Console.Write (id.ToHexString ());
//					continue;
//				}
//				if (deflatedNoHeader [pos] == '1' && deflatedNoHeader [pos + 1] == '0' && deflatedNoHeader [pos + 2] == '0') {
//					Console.Write ("\n#{0} count: {1}", pos, count);
//				}
//				Console.Write ((char) deflatedNoHeader [pos]);
//			}
			
			for (; pos < deflatedNoHeader.Length; pos++) {
				if (deflatedNoHeader [pos] == '\0') {
					break;
				}
				Console.Write ((char) deflatedNoHeader [pos]);
			}
			
			pos += 1;
			
			byte[] bytes = new byte[20];
			Array.Copy (deflatedNoHeader, pos, bytes, 0, 20);
			
			SHA1 id = new SHA1 (bytes, false);
			
			Console.WriteLine (" " + id.ToHexString ());
			
//			foreach (byte b in deflatedNoHeader) {
//				if (b == '\0')
//					Console.Write ("[null]");
//				Console.Write ((char) b);
//			}
		}
		public BlobTreeEntry (Tree myParent, SHA1 objId, string objName, bool exec) : 
			base (myParent, objId, objName)
		{
			SetExecutable (exec);
		}
示例#9
0
		public bool Equals (SHA1 o)
		{
			if (bytes == o.Bytes)
				return true;
			
			return bytes.Equals (o.Bytes);
		}
示例#10
0
		private void WriteBuffer (SHA1 id, byte[] content)
		{
			try {
				CreateObjectParent (id);
				
				FileStream fs = File.Open (GetObjectFullPath (id), FileMode.CreateNew);
				BinaryWriter bw = new BinaryWriter (fs);
				
				bw.Write (Compress (content));
				
				bw.Close ();
				fs.Close ();
			} catch (Exception e) {
				Console.WriteLine ("The file object you are trying to write already exist");
			}
		}
示例#11
0
		private Object RetrieveObject (SHA1 id)
		{
			byte[] bytes = RetrieveContent (id);
			
			return Object.DecodeObject (bytes);
		}
示例#12
0
		private byte[] RetrieveContent (SHA1 id)
		{
			FileStream fs = new FileStream (GetObjectFullPath (id), FileMode.Open, FileAccess.Read);
			BinaryReader br = new BinaryReader (fs);
			
			byte[] bytes = new byte[(int) fs.Length];
			fs.Read (bytes, 0, (int) fs.Length);
			bytes = Decompress (bytes);
			
			br.Close ();
			fs.Close ();
			
			return bytes;
		}
示例#13
0
		private void PersistObject (SHA1 id)
		{
			Object o = cache[id];
			
			FileStream fs = new FileStream(GetObjectFullPath (o.Id), FileMode.CreateNew, FileAccess.Write);
			BinaryWriter bw = new BinaryWriter (fs);
			
			bw.Write (Compress (o.Content));
			
			bw.Close ();
			fs.Close ();
		}
示例#14
0
		private byte[] ReadBuffer (SHA1 id)
		{
			try {
				FileStream fs = File.Open (GetObjectFullPath (id), FileMode.Open);
				BinaryReader br = new BinaryReader (fs);
				
				// I declared this here to close the stream and reader
				byte[] data = Decompress (br.ReadBytes ((int)fs.Length));
				
				br.Close ();
				fs.Close ();
				
				return data;
			} catch (FieldAccessException e) {
				Console.WriteLine ("The file object you are trying to read does not exist {0}", e);
			}
			
			return null;
		}
示例#15
0
		private void WriteBuffer (SHA1 id, byte[] content, bool overwrite)
		{
			if (overwrite) {
				File.Delete (GetObjectFullPath (id));
				WriteBuffer (id, content);
				return;
			}
			
			WriteBuffer (id, content);
		}
		public SymlinkTreeEntry (Tree myParent, SHA1 objId, string objName, bool exec) : 
			base (myParent, objId, objName)
		{
			mode = GitFileMode.Symlink;
		}
示例#17
0
		private void AddEntry (byte[] mode, string name, SHA1 id)
		{
			if (entries == null) {
				entries = new TreeEntry[0];
			}
			
			List<TreeEntry> entriesList = new List<TreeEntry> (entries);
			TreeEntry entry = new TreeEntry (mode, name, id);
			
			entriesList.Add (new TreeEntry (mode, name, id));
			
			entries = entriesList.ToArray ();
		}
示例#18
0
		private string GetObjectFullPath (SHA1 id)
		{
			return path.EndsWith ("/") ? (path + id.GetGitFileName ()) : (path + "/" + id.GetGitFileName ());
		}
示例#19
0
		public Object (Type type, byte[] content)
		{
			// FIXME: ok, if we got a 0 length content(a blob), what we can do?
			if (content.Length == 0 || content == null)
				return;
			
			byte[] header = CreateObjectHeader (type, content.Length.ToString ());
			this.content = new byte[header.Length + content.Length];
			
			// filling the content
			Array.Copy (header, 0, this.content, 0, header.Length); // Copying the header first
			Array.Copy (content, 0, this.content, header.Length, content.Length); // then the data
			
			Console.WriteLine ("Length: " + this.content.Length);
			foreach (char c in this.content) {
				if (c == '\n') {
					Console.WriteLine ("\\n");
					continue;
				}
				if (c == '\0')
					Console.Write ("[NULL]");
				Console.Write (c);
			}
			
			// initializing the id with the content
			id = new SHA1 (this.content, true);
			
			Console.WriteLine ("ID: " + id.ToHexString ());
		}
示例#20
0
		/// <summary>
		/// Create a object first parent... e.g. f4/47d5573b70cf042d036bfa62f55cdefccd9909
		/// f4 is the parent we create in this method
		/// </summary>
		/// <param name="id">
		/// A <see cref="SHA1"/>
		/// </param>
		private void CreateObjectParent (SHA1 id)
		{
			string fullPath = null;
			
			if (!path.EndsWith ("/"))
				fullPath = path + "/" + id.ToHexString (0, 1);
			else
				fullPath = path + id.ToHexString (0, 1);
			
			if (!Directory.Exists (fullPath))
				Directory.CreateDirectory (fullPath);
		}