예제 #1
0
        /// <exception cref="System.IO.IOException"></exception>
        private void CommitNoteMap(RevWalk walk, NoteMap map, RevCommit notesCommit, ObjectInserter
                                   inserter, string msg)
        {
            // commit the note
            NGit.CommitBuilder builder = new NGit.CommitBuilder();
            builder.TreeId    = map.WriteTree(inserter);
            builder.Author    = new PersonIdent(repo);
            builder.Committer = builder.Author;
            builder.Message   = msg;
            if (notesCommit != null)
            {
                builder.SetParentIds(notesCommit);
            }
            ObjectId commit = inserter.Insert(builder);

            inserter.Flush();
            RefUpdate refUpdate = repo.UpdateRef(notesRef);

            if (notesCommit != null)
            {
                refUpdate.SetExpectedOldObjectId(notesCommit);
            }
            else
            {
                refUpdate.SetExpectedOldObjectId(ObjectId.ZeroId);
            }
            refUpdate.SetNewObjectId(commit);
            refUpdate.Update(walk);
        }
예제 #2
0
 public static ObjectId CreateCommit(NGit.Repository rep, string message, IList <ObjectId> parents, ObjectId indexTreeId, PersonIdent author, PersonIdent committer)
 {
     try {
         ObjectInserter odi = rep.NewObjectInserter();
         try {
             // Create a Commit object, populate it and write it
             NGit.CommitBuilder commit = new NGit.CommitBuilder();
             commit.Committer = committer;
             commit.Author    = author;
             commit.Message   = message;
             commit.SetParentIds(parents);
             commit.TreeId = indexTreeId;
             ObjectId commitId = odi.Insert(commit);
             odi.Flush();
             return(commitId);
         } finally {
             odi.Release();
         }
     } catch (UnmergedPathException) {
         // since UnmergedPathException is a subclass of IOException
         // which should not be wrapped by a JGitInternalException we
         // have to catch and re-throw it here
         throw;
     } catch (IOException e) {
         throw new JGitInternalException("Commit failed", e);
     }
 }
예제 #3
0
 /// <exception cref="System.Exception"></exception>
 public virtual RevCommit Create()
 {
     if (this.self == null)
     {
         this._enclosing.Tick(this.tick);
         NGit.CommitBuilder c;
         c = new NGit.CommitBuilder();
         c.SetParentIds(this.parents);
         this._enclosing.SetAuthorAndCommitter(c);
         c.Message = this.message;
         ObjectId commitId;
         try
         {
             c.TreeId = this.tree.WriteTree(this._enclosing.inserter);
             commitId = this._enclosing.inserter.Insert(c);
             this._enclosing.inserter.Flush();
         }
         finally
         {
             this._enclosing.inserter.Release();
         }
         this.self = this._enclosing.pool.LookupCommit(commitId);
         if (this.branch != null)
         {
             this.branch.Update(this.self);
         }
     }
     return(this.self);
 }
예제 #4
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);
        }
예제 #5
0
 public virtual void Test026_CreateCommitMultipleparents()
 {
     ObjectId treeId;
     ObjectInserter oi = db.NewObjectInserter();
     try
     {
         ObjectId blobId = oi.Insert(Constants.OBJ_BLOB, Sharpen.Runtime.GetBytesForString
             ("and this is the data in me\n", Constants.CHARSET.Name()));
         TreeFormatter fmt = new TreeFormatter();
         fmt.Append("i-am-a-file", FileMode.REGULAR_FILE, blobId);
         treeId = oi.Insert(fmt);
         oi.Flush();
     }
     finally
     {
         oi.Release();
     }
     NUnit.Framework.Assert.AreEqual(ObjectId.FromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"
         ), treeId);
     NGit.CommitBuilder c1 = new NGit.CommitBuilder();
     c1.Author = new PersonIdent(author, 1154236443000L, -4 * 60);
     c1.Committer = new PersonIdent(committer, 1154236443000L, -4 * 60);
     c1.Message = "A Commit\n";
     c1.TreeId = treeId;
     NUnit.Framework.Assert.AreEqual(treeId, c1.TreeId);
     ObjectId actid1 = InsertCommit(c1);
     ObjectId cmtid1 = ObjectId.FromString("803aec4aba175e8ab1d666873c984c0308179099");
     NUnit.Framework.Assert.AreEqual(cmtid1, actid1);
     NGit.CommitBuilder c2 = new NGit.CommitBuilder();
     c2.Author = new PersonIdent(author, 1154236443000L, -4 * 60);
     c2.Committer = new PersonIdent(committer, 1154236443000L, -4 * 60);
     c2.Message = "A Commit 2\n";
     c2.TreeId = treeId;
     NUnit.Framework.Assert.AreEqual(treeId, c2.TreeId);
     c2.SetParentIds(actid1);
     ObjectId actid2 = InsertCommit(c2);
     ObjectId cmtid2 = ObjectId.FromString("95d068687c91c5c044fb8c77c5154d5247901553");
     NUnit.Framework.Assert.AreEqual(cmtid2, actid2);
     RevCommit rm2 = ParseCommit(cmtid2);
     NUnit.Framework.Assert.AreNotSame(c2, rm2);
     // assert the parsed objects is not from the
     // cache
     NUnit.Framework.Assert.AreEqual(c2.Author, rm2.GetAuthorIdent());
     NUnit.Framework.Assert.AreEqual(actid2, rm2.Id);
     NUnit.Framework.Assert.AreEqual(c2.Message, rm2.GetFullMessage());
     NUnit.Framework.Assert.AreEqual(c2.TreeId, rm2.Tree.Id);
     NUnit.Framework.Assert.AreEqual(1, rm2.ParentCount);
     NUnit.Framework.Assert.AreEqual(actid1, rm2.GetParent(0));
     NGit.CommitBuilder c3 = new NGit.CommitBuilder();
     c3.Author = new PersonIdent(author, 1154236443000L, -4 * 60);
     c3.Committer = new PersonIdent(committer, 1154236443000L, -4 * 60);
     c3.Message = "A Commit 3\n";
     c3.TreeId = treeId;
     NUnit.Framework.Assert.AreEqual(treeId, c3.TreeId);
     c3.SetParentIds(actid1, actid2);
     ObjectId actid3 = InsertCommit(c3);
     ObjectId cmtid3 = ObjectId.FromString("ce6e1ce48fbeeb15a83f628dc8dc2debefa066f4");
     NUnit.Framework.Assert.AreEqual(cmtid3, actid3);
     RevCommit rm3 = ParseCommit(cmtid3);
     NUnit.Framework.Assert.AreNotSame(c3, rm3);
     // assert the parsed objects is not from the
     // cache
     NUnit.Framework.Assert.AreEqual(c3.Author, rm3.GetAuthorIdent());
     NUnit.Framework.Assert.AreEqual(actid3, rm3.Id);
     NUnit.Framework.Assert.AreEqual(c3.Message, rm3.GetFullMessage());
     NUnit.Framework.Assert.AreEqual(c3.TreeId, rm3.Tree.Id);
     NUnit.Framework.Assert.AreEqual(2, rm3.ParentCount);
     NUnit.Framework.Assert.AreEqual(actid1, rm3.GetParent(0));
     NUnit.Framework.Assert.AreEqual(actid2, rm3.GetParent(1));
     NGit.CommitBuilder c4 = new NGit.CommitBuilder();
     c4.Author = new PersonIdent(author, 1154236443000L, -4 * 60);
     c4.Committer = new PersonIdent(committer, 1154236443000L, -4 * 60);
     c4.Message = "A Commit 4\n";
     c4.TreeId = treeId;
     NUnit.Framework.Assert.AreEqual(treeId, c3.TreeId);
     c4.SetParentIds(actid1, actid2, actid3);
     ObjectId actid4 = InsertCommit(c4);
     ObjectId cmtid4 = ObjectId.FromString("d1fca9fe3fef54e5212eb67902c8ed3e79736e27");
     NUnit.Framework.Assert.AreEqual(cmtid4, actid4);
     RevCommit rm4 = ParseCommit(cmtid4);
     NUnit.Framework.Assert.AreNotSame(c4, rm3);
     // assert the parsed objects is not from the
     // cache
     NUnit.Framework.Assert.AreEqual(c4.Author, rm4.GetAuthorIdent());
     NUnit.Framework.Assert.AreEqual(actid4, rm4.Id);
     NUnit.Framework.Assert.AreEqual(c4.Message, rm4.GetFullMessage());
     NUnit.Framework.Assert.AreEqual(c4.TreeId, rm4.Tree.Id);
     NUnit.Framework.Assert.AreEqual(3, rm4.ParentCount);
     NUnit.Framework.Assert.AreEqual(actid1, rm4.GetParent(0));
     NUnit.Framework.Assert.AreEqual(actid2, rm4.GetParent(1));
     NUnit.Framework.Assert.AreEqual(actid3, rm4.GetParent(2));
 }
예제 #6
0
		public static ObjectId CreateCommit (NGit.Repository rep, string message, IList<ObjectId> parents, ObjectId indexTreeId, PersonIdent author, PersonIdent committer)
		{
			try {
				ObjectInserter odi = rep.NewObjectInserter ();
				try {
					// Create a Commit object, populate it and write it
					NGit.CommitBuilder commit = new NGit.CommitBuilder ();
					commit.Committer = committer;
					commit.Author = author;
					commit.Message = message;
					commit.SetParentIds (parents);
					commit.TreeId = indexTreeId;
					ObjectId commitId = odi.Insert (commit);
					odi.Flush ();
					return commitId;
				} finally {
					odi.Release ();
				}
			} catch (UnmergedPathException) {
				// since UnmergedPathException is a subclass of IOException
				// which should not be wrapped by a JGitInternalException we
				// have to catch and re-throw it here
				throw;
			} catch (IOException e) {
				throw new JGitInternalException ("Commit failed", e);
			}
		}
예제 #7
0
        /// <summary>
        /// Executes the
        /// <code>commit</code>
        /// command with all the options and parameters
        /// collected by the setter methods of this class. Each instance of this
        /// class should only be used for one invocation of the command (means: one
        /// call to
        /// <see cref="Call()">Call()</see>
        /// )
        /// </summary>
        /// <returns>
        /// a
        /// <see cref="NGit.Revwalk.RevCommit">NGit.Revwalk.RevCommit</see>
        /// object representing the successful commit.
        /// </returns>
        /// <exception cref="NGit.Api.Errors.NoHeadException">when called on a git repo without a HEAD reference
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.NoMessageException">when called without specifying a commit message
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.UnmergedPathsException">when the current index contained unmerged paths (conflicts)
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.ConcurrentRefUpdateException">
        /// when HEAD or branch ref is updated concurrently by someone
        /// else
        /// </exception>
        /// <exception cref="NGit.Api.Errors.WrongRepositoryStateException">when repository is not in the right state for committing
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.GitAPIException"></exception>
        public override RevCommit Call()
        {
            CheckCallable();
            RepositoryState state = repo.GetRepositoryState();

            if (!state.CanCommit())
            {
                throw new WrongRepositoryStateException(MessageFormat.Format(JGitText.Get().cannotCommitOnARepoWithState
                                                                             , state.Name()));
            }
            ProcessOptions(state);
            try
            {
                if (all && !repo.IsBare && repo.WorkTree != null)
                {
                    Git git = new Git(repo);
                    try
                    {
                        git.Add().AddFilepattern(".").SetUpdate(true).Call();
                    }
                    catch (NoFilepatternException e)
                    {
                        // should really not happen
                        throw new JGitInternalException(e.Message, e);
                    }
                }
                Ref head = repo.GetRef(Constants.HEAD);
                if (head == null)
                {
                    throw new NoHeadException(JGitText.Get().commitOnRepoWithoutHEADCurrentlyNotSupported
                                              );
                }
                // determine the current HEAD and the commit it is referring to
                ObjectId headId = repo.Resolve(Constants.HEAD + "^{commit}");
                if (headId == null && amend)
                {
                    throw new WrongRepositoryStateException(JGitText.Get().commitAmendOnInitialNotPossible
                                                            );
                }
                if (headId != null)
                {
                    if (amend)
                    {
                        RevCommit   previousCommit = new RevWalk(repo).ParseCommit(headId);
                        RevCommit[] p = previousCommit.Parents;
                        for (int i = 0; i < p.Length; i++)
                        {
                            parents.Add(0, p[i].Id);
                        }
                        if (author == null)
                        {
                            author = previousCommit.GetAuthorIdent();
                        }
                    }
                    else
                    {
                        parents.Add(0, headId);
                    }
                }
                // lock the index
                DirCache index = repo.LockDirCache();
                try
                {
                    if (!only.IsEmpty())
                    {
                        index = CreateTemporaryIndex(headId, index);
                    }
                    ObjectInserter odi = repo.NewObjectInserter();
                    try
                    {
                        // Write the index as tree to the object database. This may
                        // fail for example when the index contains unmerged paths
                        // (unresolved conflicts)
                        ObjectId indexTreeId = index.WriteTree(odi);
                        if (insertChangeId)
                        {
                            InsertChangeId(indexTreeId);
                        }
                        // Create a Commit object, populate it and write it
                        NGit.CommitBuilder commit = new NGit.CommitBuilder();
                        commit.Committer = committer;
                        commit.Author    = author;
                        commit.Message   = message;
                        commit.SetParentIds(parents);
                        commit.TreeId = indexTreeId;
                        ObjectId commitId = odi.Insert(commit);
                        odi.Flush();
                        RevWalk revWalk = new RevWalk(repo);
                        try
                        {
                            RevCommit revCommit = revWalk.ParseCommit(commitId);
                            RefUpdate ru        = repo.UpdateRef(Constants.HEAD);
                            ru.SetNewObjectId(commitId);
                            if (reflogComment != null)
                            {
                                ru.SetRefLogMessage(reflogComment, false);
                            }
                            else
                            {
                                string prefix = amend ? "commit (amend): " : "commit: ";
                                ru.SetRefLogMessage(prefix + revCommit.GetShortMessage(), false);
                            }
                            if (headId != null)
                            {
                                ru.SetExpectedOldObjectId(headId);
                            }
                            else
                            {
                                ru.SetExpectedOldObjectId(ObjectId.ZeroId);
                            }
                            RefUpdate.Result rc = ru.ForceUpdate();
                            switch (rc)
                            {
                            case RefUpdate.Result.NEW:
                            case RefUpdate.Result.FORCED:
                            case RefUpdate.Result.FAST_FORWARD:
                            {
                                SetCallable(false);
                                if (state == RepositoryState.MERGING_RESOLVED)
                                {
                                    // Commit was successful. Now delete the files
                                    // used for merge commits
                                    repo.WriteMergeCommitMsg(null);
                                    repo.WriteMergeHeads(null);
                                }
                                else
                                {
                                    if (state == RepositoryState.CHERRY_PICKING_RESOLVED)
                                    {
                                        repo.WriteMergeCommitMsg(null);
                                        repo.WriteCherryPickHead(null);
                                    }
                                }
                                return(revCommit);
                            }

                            case RefUpdate.Result.REJECTED:
                            case RefUpdate.Result.LOCK_FAILURE:
                            {
                                throw new ConcurrentRefUpdateException(JGitText.Get().couldNotLockHEAD, ru.GetRef
                                                                           (), rc);
                            }

                            default:
                            {
                                throw new JGitInternalException(MessageFormat.Format(JGitText.Get().updatingRefFailed
                                                                                     , Constants.HEAD, commitId.ToString(), rc));
                            }
                            }
                        }
                        finally
                        {
                            revWalk.Release();
                        }
                    }
                    finally
                    {
                        odi.Release();
                    }
                }
                finally
                {
                    index.Unlock();
                }
            }
            catch (UnmergedPathException e)
            {
                throw new UnmergedPathsException(e);
            }
            catch (IOException e)
            {
                throw new JGitInternalException(JGitText.Get().exceptionCaughtDuringExecutionOfCommitCommand
                                                , e);
            }
        }
예제 #8
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;
		}
예제 #9
0
			/// <exception cref="System.Exception"></exception>
			public virtual RevCommit Create()
			{
				if (this.self == null)
				{
					this._enclosing.Tick(this.tick);
					NGit.CommitBuilder c;
					c = new NGit.CommitBuilder();
					c.SetParentIds(this.parents);
					this._enclosing.SetAuthorAndCommitter(c);
					c.Message = this.message;
					ObjectId commitId;
					try
					{
						if (this.topLevelTree != null)
						{
							c.TreeId = this.topLevelTree;
						}
						else
						{
							c.TreeId = this.tree.WriteTree(this._enclosing.inserter);
						}
						commitId = this._enclosing.inserter.Insert(c);
						this._enclosing.inserter.Flush();
					}
					finally
					{
						this._enclosing.inserter.Release();
					}
					this.self = this._enclosing.pool.LookupCommit(commitId);
					if (this.branch != null)
					{
						this.branch.Update(this.self);
					}
				}
				return this.self;
			}
예제 #10
0
        public virtual void Test026_CreateCommitMultipleparents()
        {
            ObjectId       treeId;
            ObjectInserter oi = db.NewObjectInserter();

            try
            {
                ObjectId blobId = oi.Insert(Constants.OBJ_BLOB, Sharpen.Runtime.GetBytesForString
                                                ("and this is the data in me\n", Constants.CHARSET.Name()));
                TreeFormatter fmt = new TreeFormatter();
                fmt.Append("i-am-a-file", FileMode.REGULAR_FILE, blobId);
                treeId = oi.Insert(fmt);
                oi.Flush();
            }
            finally
            {
                oi.Release();
            }
            NUnit.Framework.Assert.AreEqual(ObjectId.FromString("00b1f73724f493096d1ffa0b0f1f1482dbb8c936"
                                                                ), treeId);
            NGit.CommitBuilder c1 = new NGit.CommitBuilder();
            c1.Author    = new PersonIdent(author, 1154236443000L, -4 * 60);
            c1.Committer = new PersonIdent(committer, 1154236443000L, -4 * 60);
            c1.Message   = "A Commit\n";
            c1.TreeId    = treeId;
            NUnit.Framework.Assert.AreEqual(treeId, c1.TreeId);
            ObjectId actid1 = InsertCommit(c1);
            ObjectId cmtid1 = ObjectId.FromString("803aec4aba175e8ab1d666873c984c0308179099");

            NUnit.Framework.Assert.AreEqual(cmtid1, actid1);
            NGit.CommitBuilder c2 = new NGit.CommitBuilder();
            c2.Author    = new PersonIdent(author, 1154236443000L, -4 * 60);
            c2.Committer = new PersonIdent(committer, 1154236443000L, -4 * 60);
            c2.Message   = "A Commit 2\n";
            c2.TreeId    = treeId;
            NUnit.Framework.Assert.AreEqual(treeId, c2.TreeId);
            c2.SetParentIds(actid1);
            ObjectId actid2 = InsertCommit(c2);
            ObjectId cmtid2 = ObjectId.FromString("95d068687c91c5c044fb8c77c5154d5247901553");

            NUnit.Framework.Assert.AreEqual(cmtid2, actid2);
            RevCommit rm2 = ParseCommit(cmtid2);

            NUnit.Framework.Assert.AreNotSame(c2, rm2);
            // assert the parsed objects is not from the
            // cache
            NUnit.Framework.Assert.AreEqual(c2.Author, rm2.GetAuthorIdent());
            NUnit.Framework.Assert.AreEqual(actid2, rm2.Id);
            NUnit.Framework.Assert.AreEqual(c2.Message, rm2.GetFullMessage());
            NUnit.Framework.Assert.AreEqual(c2.TreeId, rm2.Tree.Id);
            NUnit.Framework.Assert.AreEqual(1, rm2.ParentCount);
            NUnit.Framework.Assert.AreEqual(actid1, rm2.GetParent(0));
            NGit.CommitBuilder c3 = new NGit.CommitBuilder();
            c3.Author    = new PersonIdent(author, 1154236443000L, -4 * 60);
            c3.Committer = new PersonIdent(committer, 1154236443000L, -4 * 60);
            c3.Message   = "A Commit 3\n";
            c3.TreeId    = treeId;
            NUnit.Framework.Assert.AreEqual(treeId, c3.TreeId);
            c3.SetParentIds(actid1, actid2);
            ObjectId actid3 = InsertCommit(c3);
            ObjectId cmtid3 = ObjectId.FromString("ce6e1ce48fbeeb15a83f628dc8dc2debefa066f4");

            NUnit.Framework.Assert.AreEqual(cmtid3, actid3);
            RevCommit rm3 = ParseCommit(cmtid3);

            NUnit.Framework.Assert.AreNotSame(c3, rm3);
            // assert the parsed objects is not from the
            // cache
            NUnit.Framework.Assert.AreEqual(c3.Author, rm3.GetAuthorIdent());
            NUnit.Framework.Assert.AreEqual(actid3, rm3.Id);
            NUnit.Framework.Assert.AreEqual(c3.Message, rm3.GetFullMessage());
            NUnit.Framework.Assert.AreEqual(c3.TreeId, rm3.Tree.Id);
            NUnit.Framework.Assert.AreEqual(2, rm3.ParentCount);
            NUnit.Framework.Assert.AreEqual(actid1, rm3.GetParent(0));
            NUnit.Framework.Assert.AreEqual(actid2, rm3.GetParent(1));
            NGit.CommitBuilder c4 = new NGit.CommitBuilder();
            c4.Author    = new PersonIdent(author, 1154236443000L, -4 * 60);
            c4.Committer = new PersonIdent(committer, 1154236443000L, -4 * 60);
            c4.Message   = "A Commit 4\n";
            c4.TreeId    = treeId;
            NUnit.Framework.Assert.AreEqual(treeId, c3.TreeId);
            c4.SetParentIds(actid1, actid2, actid3);
            ObjectId actid4 = InsertCommit(c4);
            ObjectId cmtid4 = ObjectId.FromString("d1fca9fe3fef54e5212eb67902c8ed3e79736e27");

            NUnit.Framework.Assert.AreEqual(cmtid4, actid4);
            RevCommit rm4 = ParseCommit(cmtid4);

            NUnit.Framework.Assert.AreNotSame(c4, rm3);
            // assert the parsed objects is not from the
            // cache
            NUnit.Framework.Assert.AreEqual(c4.Author, rm4.GetAuthorIdent());
            NUnit.Framework.Assert.AreEqual(actid4, rm4.Id);
            NUnit.Framework.Assert.AreEqual(c4.Message, rm4.GetFullMessage());
            NUnit.Framework.Assert.AreEqual(c4.TreeId, rm4.Tree.Id);
            NUnit.Framework.Assert.AreEqual(3, rm4.ParentCount);
            NUnit.Framework.Assert.AreEqual(actid1, rm4.GetParent(0));
            NUnit.Framework.Assert.AreEqual(actid2, rm4.GetParent(1));
            NUnit.Framework.Assert.AreEqual(actid3, rm4.GetParent(2));
        }