Esempio n. 1
0
        /// <exception cref="System.IO.IOException"></exception>
        private void Commit(string commitMsg, PersonIdent author, PersonIdent committer)
        {
            NGit.CommitBuilder commit = new NGit.CommitBuilder();
            commit.Author    = author;
            commit.Committer = committer;
            commit.Message   = commitMsg;
            ObjectInserter inserter = db.NewObjectInserter();
            ObjectId       id;

            try
            {
                commit.TreeId = inserter.Insert(new TreeFormatter());
                id            = inserter.Insert(commit);
                inserter.Flush();
            }
            finally
            {
                inserter.Release();
            }
            int       nl = commitMsg.IndexOf('\n');
            RefUpdate ru = db.UpdateRef(Constants.HEAD);

            ru.SetNewObjectId(id);
            ru.SetRefLogMessage("commit : " + ((nl == -1) ? commitMsg : Sharpen.Runtime.Substring
                                                   (commitMsg, 0, nl)), false);
            ru.ForceUpdate();
        }
Esempio n. 2
0
        /// <summary>Insert this tree and obtain its ObjectId.</summary>
        /// <remarks>Insert this tree and obtain its ObjectId.</remarks>
        /// <param name="ins">the inserter to store the tree.</param>
        /// <returns>computed ObjectId of the tree</returns>
        /// <exception cref="System.IO.IOException">the tree could not be stored.</exception>
        public virtual ObjectId InsertTo(ObjectInserter ins)
        {
            if (buf != null)
            {
                return(ins.Insert(Constants.OBJ_TREE, buf, 0, ptr));
            }
            long len = overflowBuffer.Length();

            return(ins.Insert(Constants.OBJ_TREE, len, overflowBuffer.OpenInputStream()));
        }
Esempio n. 3
0
        /// <exception cref="System.IO.IOException"></exception>
        private Tree BuildTree(Dictionary <string, string> headEntries)
        {
            Tree tree = new Tree(db);

            if (headEntries == null)
            {
                return(tree);
            }
            FileTreeEntry  fileEntry;
            Tree           parent;
            ObjectInserter oi = db.NewObjectInserter();

            try
            {
                foreach (KeyValuePair <string, string> e in headEntries.EntrySet())
                {
                    fileEntry = tree.AddFile(e.Key);
                    fileEntry.SetId(GenSha1(e.Value));
                    parent = fileEntry.GetParent();
                    while (parent != null)
                    {
                        parent.SetId(oi.Insert(Constants.OBJ_TREE, parent.Format()));
                        parent = parent.GetParent();
                    }
                }
                oi.Flush();
            }
            finally
            {
                oi.Release();
            }
            return(tree);
        }
        /// <exception cref="System.IO.FileNotFoundException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        internal static DirCacheEditor.PathEdit Add(Repository db, FilePath workdir, string
                                                    path)
        {
            ObjectInserter inserter = db.NewObjectInserter();
            FilePath       f        = new FilePath(workdir, path);
            ObjectId       id       = inserter.Insert(Constants.OBJ_BLOB, IOUtil.ReadFully(f));

            return(new _PathEdit_81(f, id, path));
        }
Esempio n. 5
0
        /// <exception cref="System.IO.IOException"></exception>
        private ObjectId InsertTree(Tree tree)
        {
            ObjectInserter oi = db.NewObjectInserter();

            try
            {
                ObjectId id = oi.Insert(Constants.OBJ_TREE, tree.Format());
                oi.Flush();
                return(id);
            }
            finally
            {
                oi.Release();
            }
        }
            /// <summary>
            /// Update this index entry with stat and SHA-1 information if it looks
            /// like the file has been modified in the workdir.
            /// </summary>
            /// <remarks>
            /// Update this index entry with stat and SHA-1 information if it looks
            /// like the file has been modified in the workdir.
            /// </remarks>
            /// <param name="f">file in work dir</param>
            /// <returns>true if a change occurred</returns>
            /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
            public virtual bool Update(FilePath f)
            {
                long lm       = f.LastModified() * 1000000L;
                bool modified = this.mtime != lm;

                this.mtime = lm;
                if (this.size != f.Length())
                {
                    modified = true;
                }
                if (this._enclosing.Config_filemode())
                {
                    if (this._enclosing.File_canExecute(f) != FileMode.EXECUTABLE_FILE.Equals(this.mode
                                                                                              ))
                    {
                        this.mode = FileMode.EXECUTABLE_FILE.GetBits();
                        modified  = true;
                    }
                }
                if (modified)
                {
                    this.size = (int)f.Length();
                    ObjectInserter oi = this._enclosing.db.NewObjectInserter();
                    try
                    {
                        InputStream @in = new FileInputStream(f);
                        try
                        {
                            ObjectId newsha1 = oi.Insert(Constants.OBJ_BLOB, f.Length(), @in);
                            oi.Flush();
                            if (!newsha1.Equals(this.sha1))
                            {
                                modified = true;
                            }
                            this.sha1 = newsha1;
                        }
                        finally
                        {
                            @in.Close();
                        }
                    }
                    finally
                    {
                        oi.Release();
                    }
                }
                return(modified);
            }
Esempio n. 7
0
 /// <exception cref="System.IO.IOException"></exception>
 private void BuildIndex(Dictionary <string, string> indexEntries)
 {
     dirCache = new DirCache(db.GetIndexFile(), db.FileSystem);
     if (indexEntries != null)
     {
         NUnit.Framework.Assert.IsTrue(dirCache.Lock());
         DirCacheEditor editor = dirCache.Editor();
         foreach (KeyValuePair <string, string> e in indexEntries.EntrySet())
         {
             WriteTrashFile(e.Key, e.Value);
             ObjectInserter inserter = db.NewObjectInserter();
             ObjectId       id       = inserter.Insert(Constants.OBJ_BLOB, Constants.Encode(e.Value));
             editor.Add(new DirCacheEditor.DeletePath(e.Key));
             editor.Add(new _PathEdit_287(id, e.Key));
         }
         NUnit.Framework.Assert.IsTrue(editor.Commit());
     }
 }
            /// <exception cref="System.IO.IOException"></exception>
            internal Entry(GitIndex _enclosing, byte[] key, FilePath f, int stage, byte[] newContent
                           )
            {
                this._enclosing = _enclosing;
                this.ctime      = f.LastModified() * 1000000L;
                this.mtime      = this.ctime;
                // we use same here
                this.dev = -1;
                this.ino = -1;
                if (this._enclosing.Config_filemode() && this._enclosing.File_canExecute(f))
                {
                    this.mode = FileMode.EXECUTABLE_FILE.GetBits();
                }
                else
                {
                    this.mode = FileMode.REGULAR_FILE.GetBits();
                }
                this.uid  = -1;
                this.gid  = -1;
                this.size = newContent.Length;
                ObjectInserter inserter = this._enclosing.db.NewObjectInserter();

                try
                {
                    InputStream @in = new FileInputStream(f);
                    try
                    {
                        this.sha1 = inserter.Insert(Constants.OBJ_BLOB, newContent);
                    }
                    finally
                    {
                        @in.Close();
                    }
                    inserter.Flush();
                }
                finally
                {
                    inserter.Release();
                }
                this.name  = key;
                this.flags = (short)((stage << 12) | this.name.Length);
                // TODO: fix flags
                this.stages = (1 >> this.GetStage());
            }
Esempio n. 9
0
        internal virtual ObjectId GenSha1(string data)
        {
            ObjectInserter w = db.NewObjectInserter();

            try
            {
                ObjectId id = w.Insert(Constants.OBJ_BLOB, Sharpen.Runtime.GetBytesForString(data
                                                                                             ));
                w.Flush();
                return(id);
            }
            catch (IOException e)
            {
                NUnit.Framework.Assert.Fail(e.ToString());
            }
            finally
            {
                w.Release();
            }
            return(null);
        }
            /// <summary>
            /// Update this index entry with stat and SHA-1 information if it looks
            /// like the file has been modified in the workdir.
            /// </summary>
            /// <remarks>
            /// Update this index entry with stat and SHA-1 information if it looks
            /// like the file has been modified in the workdir.
            /// </remarks>
            /// <param name="f">file in work dir</param>
            /// <param name="newContent">the new content of the file</param>
            /// <returns>true if a change occurred</returns>
            /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
            public virtual bool Update(FilePath f, byte[] newContent)
            {
                bool modified = false;

                this.size = newContent.Length;
                ObjectInserter oi = this._enclosing.db.NewObjectInserter();

                try
                {
                    ObjectId newsha1 = oi.Insert(Constants.OBJ_BLOB, newContent);
                    oi.Flush();
                    if (!newsha1.Equals(this.sha1))
                    {
                        modified = true;
                    }
                    this.sha1 = newsha1;
                }
                finally
                {
                    oi.Release();
                }
                return(modified);
            }
Esempio n. 11
0
        /// <summary>Resets the index to represent exactly some filesystem content.</summary>
        /// <remarks>
        /// Resets the index to represent exactly some filesystem content. E.g. the
        /// following call will replace the index with the working tree content:
        /// <p>
        /// <code>resetIndex(new FileSystemIterator(db))</code>
        /// <p>
        /// This method can be used by testcases which first prepare a new commit
        /// somewhere in the filesystem (e.g. in the working-tree) and then want to
        /// have an index which matches their prepared content.
        /// </remarks>
        /// <param name="treeItr">
        /// a
        /// <see cref="NGit.Treewalk.FileTreeIterator">NGit.Treewalk.FileTreeIterator</see>
        /// which determines which files should
        /// go into the new index
        /// </param>
        /// <exception cref="System.IO.FileNotFoundException">System.IO.FileNotFoundException
        ///     </exception>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        protected internal virtual void ResetIndex(FileTreeIterator treeItr)
        {
            ObjectInserter  inserter = db.NewObjectInserter();
            DirCacheBuilder builder  = db.LockDirCache().Builder();
            DirCacheEntry   dce;

            while (!treeItr.Eof)
            {
                long len = treeItr.GetEntryLength();
                dce              = new DirCacheEntry(treeItr.EntryPathString);
                dce.FileMode     = treeItr.EntryFileMode;
                dce.LastModified = treeItr.GetEntryLastModified();
                dce.SetLength((int)len);
                FileInputStream @in = new FileInputStream(treeItr.GetEntryFile());
                dce.SetObjectId(inserter.Insert(Constants.OBJ_BLOB, len, @in));
                @in.Close();
                builder.Add(dce);
                treeItr.Next(1);
            }
            builder.Commit();
            inserter.Flush();
            inserter.Release();
        }
Esempio n. 12
0
        /// <exception cref="System.IO.IOException"></exception>
        public virtual Note Merge(Note @base, Note ours, Note theirs, ObjectReader reader
			, ObjectInserter inserter)
        {
            if (ours == null)
            {
                return theirs;
            }
            if (theirs == null)
            {
                return ours;
            }
            if (ours.GetData().Equals(theirs.GetData()))
            {
                return ours;
            }
            ObjectLoader lo = reader.Open(ours.GetData());
            ObjectLoader lt = reader.Open(theirs.GetData());
            UnionInputStream union = new UnionInputStream(lo.OpenStream(), lt.OpenStream());
            ObjectId noteData = inserter.Insert(Constants.OBJ_BLOB, lo.GetSize() + lt.GetSize
                (), union);
            return new Note(ours, noteData);
        }
Esempio n. 13
0
		/// <summary>Attach a note to an object.</summary>
		/// <remarks>
		/// Attach a note to an object.
		/// If no note exists, a new note is stored. If a note already exists for the
		/// given object, it is replaced (or removed).
		/// </remarks>
		/// <param name="noteOn">
		/// the object to attach the note to. This same ObjectId can later
		/// be used as an argument to
		/// <see cref="Get(NGit.AnyObjectId)">Get(NGit.AnyObjectId)</see>
		/// or
		/// <see cref="GetCachedBytes(NGit.AnyObjectId, int)">GetCachedBytes(NGit.AnyObjectId, int)
		/// 	</see>
		/// to read back the
		/// <code>noteData</code>
		/// .
		/// </param>
		/// <param name="noteData">
		/// text to store in the note. The text will be UTF-8 encoded when
		/// stored in the repository. If null the note will be deleted, if
		/// the empty string a note with the empty string will be stored.
		/// </param>
		/// <param name="ins">
		/// inserter to write the encoded
		/// <code>noteData</code>
		/// out as a blob.
		/// The caller must ensure the inserter is flushed before the
		/// updated note map is made available for reading.
		/// </param>
		/// <exception cref="System.IO.IOException">the note data could not be stored in the repository.
		/// 	</exception>
		public virtual void Set(AnyObjectId noteOn, string noteData, ObjectInserter ins)
		{
			ObjectId dataId;
			if (noteData != null)
			{
				byte[] dataUTF8 = Constants.Encode(noteData);
				dataId = ins.Insert(Constants.OBJ_BLOB, dataUTF8);
			}
			else
			{
				dataId = null;
			}
			Set(noteOn, dataId);
		}
Esempio n. 14
0
		/// <summary>Insert this tree and obtain its ObjectId.</summary>
		/// <remarks>Insert this tree and obtain its ObjectId.</remarks>
		/// <param name="ins">the inserter to store the tree.</param>
		/// <returns>computed ObjectId of the tree</returns>
		/// <exception cref="System.IO.IOException">the tree could not be stored.</exception>
		public virtual ObjectId InsertTo(ObjectInserter ins)
		{
			if (buf != null)
			{
				return ins.Insert(Constants.OBJ_TREE, buf, 0, ptr);
			}
			long len = overflowBuffer.Length();
			return ins.Insert(Constants.OBJ_TREE, len, overflowBuffer.OpenInputStream());
		}
        /// <summary>Construct and write tree out of index.</summary>
        /// <remarks>Construct and write tree out of index.</remarks>
        /// <returns>SHA-1 of the constructed tree</returns>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        public virtual ObjectId WriteTree()
        {
            CheckWriteOk();
            ObjectInserter inserter = db.NewObjectInserter();

            try
            {
                Tree         current = new Tree(db);
                Stack <Tree> trees   = new Stack <Tree>();
                trees.Push(current);
                string[] prevName = new string[0];
                foreach (GitIndex.Entry e in entries.Values)
                {
                    if (e.GetStage() != 0)
                    {
                        continue;
                    }
                    string[] newName = SplitDirPath(e.GetName());
                    int      c       = LongestCommonPath(prevName, newName);
                    while (c < trees.Count - 1)
                    {
                        current.SetId(inserter.Insert(Constants.OBJ_TREE, current.Format()));
                        trees.Pop();
                        current = trees.IsEmpty() ? null : (Tree)trees.Peek();
                    }
                    while (trees.Count < newName.Length)
                    {
                        if (!current.ExistsTree(newName[trees.Count - 1]))
                        {
                            current = new Tree(current, Constants.Encode(newName[trees.Count - 1]));
                            current.GetParent().AddEntry(current);
                            trees.Push(current);
                        }
                        else
                        {
                            current = (Tree)current.FindTreeMember(newName[trees.Count - 1]);
                            trees.Push(current);
                        }
                    }
                    FileTreeEntry ne = new FileTreeEntry(current, e.sha1, Constants.Encode(newName[newName
                                                                                                   .Length - 1]), (e.mode & FileMode.EXECUTABLE_FILE.GetBits()) == FileMode.EXECUTABLE_FILE
                                                         .GetBits());
                    current.AddEntry(ne);
                }
                while (!trees.IsEmpty())
                {
                    current.SetId(inserter.Insert(Constants.OBJ_TREE, current.Format()));
                    trees.Pop();
                    if (!trees.IsEmpty())
                    {
                        current = trees.Peek();
                    }
                }
                inserter.Flush();
                return(current.GetId());
            }
            finally
            {
                inserter.Release();
            }
        }
Esempio n. 16
0
		/// <exception cref="System.Exception"></exception>
		private ObjectId Commit(ObjectInserter odi, DirCache treeB, ObjectId[] parentIds)
		{
			NGit.CommitBuilder c = new NGit.CommitBuilder();
			c.TreeId = treeB.WriteTree(odi);
			c.Author = new PersonIdent("A U Thor", "a.u.thor", 1L, 0);
			c.Committer = c.Author;
			c.SetParentIds(parentIds);
			c.Message = "Tree " + c.TreeId.Name;
			ObjectId id = odi.Insert(c);
			odi.Flush();
			return id;
		}
Esempio n. 17
0
        /// <exception cref="System.IO.IOException"></exception>
        private DirCacheEntry AddEntryToBuilder(string path, FilePath file, ObjectInserter
			 newObjectInserter, DirCacheBuilder builder, int stage)
        {
            FileInputStream inputStream = new FileInputStream(file);
            ObjectId id = newObjectInserter.Insert(Constants.OBJ_BLOB, file.Length(), inputStream
                );
            inputStream.Close();
            DirCacheEntry entry = new DirCacheEntry(path, stage);
            entry.SetObjectId(id);
            entry.FileMode = FileMode.REGULAR_FILE;
            entry.LastModified = file.LastModified();
            entry.SetLength((int)file.Length());
            builder.Add(entry);
            return entry;
        }