Exemple #1
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.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();
        }
Exemple #3
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);
            }
            /// <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());
            }
Exemple #6
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);
            }
Exemple #8
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();
        }
        /// <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();
            }
        }