/// <summary>Core validation to be performed on all stashed commits</summary> /// <param name="commit"></param> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> private void ValidateStashedCommit(RevCommit commit) { NUnit.Framework.Assert.IsNotNull(commit); Ref stashRef = db.GetRef(Constants.R_STASH); NUnit.Framework.Assert.IsNotNull(stashRef); NUnit.Framework.Assert.AreEqual(commit, stashRef.GetObjectId()); NUnit.Framework.Assert.IsNotNull(commit.GetAuthorIdent()); NUnit.Framework.Assert.AreEqual(commit.GetAuthorIdent(), commit.GetCommitterIdent ()); NUnit.Framework.Assert.AreEqual(2, commit.ParentCount); // Load parents RevWalk walk = new RevWalk(db); try { foreach (RevCommit parent in commit.Parents) { walk.ParseBody(parent); } } finally { walk.Release(); } NUnit.Framework.Assert.AreEqual(1, commit.GetParent(1).ParentCount); NUnit.Framework.Assert.AreEqual(head, commit.GetParent(1).GetParent(0)); NUnit.Framework.Assert.IsFalse(commit.Tree.Equals(head.Tree), "Head tree matches stashed commit tree" ); NUnit.Framework.Assert.AreEqual(head, commit.GetParent(0)); NUnit.Framework.Assert.IsFalse(commit.GetFullMessage().Equals(commit.GetParent(1) .GetFullMessage())); }
public virtual void TestCommit() { Git git = new Git(db); revCommit = git.Commit().SetMessage("squash_me").Call(); Ref master = db.GetRef("refs/heads/master"); string message = msgFormatter.Format(Arrays.AsList(revCommit), master); NUnit.Framework.Assert.AreEqual("Squashed commit of the following:\n\ncommit " + revCommit.GetName() + "\nAuthor: " + revCommit.GetAuthorIdent().GetName() + " <" + revCommit.GetAuthorIdent().GetEmailAddress() + ">\nDate: " + dateFormatter. FormatDate(author) + "\n\n\tsquash_me\n", message); }
public Commit GetCommit(string commitId) { //commitId = repository.Resolve(commitId).Name; //return Commits.Where(c => c.Id.StartsWith(commitId)).FirstOrDefault(); var id = repository.Resolve(commitId); if (id == null) { return(null); } RevWalk walk = new RevWalk(repository); RevCommit commit = walk.ParseCommit(id); walk.Dispose(); return(commit == null || commit.Tree == null ? null : new Commit { Id = commit.Id.Name, ParentIds = commit.Parents.Select(p => p.Id.Name).ToList(), CommitDateRelative = RelativeDateFormatter.Format(commit.GetAuthorIdent().GetWhen()), CommitterName = commit.GetCommitterIdent().GetName(), CommitterEmail = commit.GetCommitterIdent().GetEmailAddress(), CommitDate = commit.GetCommitterIdent().GetWhen(), Message = commit.GetShortMessage(), }); }
public static PersonIdent author(this RevCommit revCommit) { if (revCommit.notNull()) { return(revCommit.GetAuthorIdent()); } return(null); }
private ListViewItem CreateListItem(RevCommit c) { var commitTime = c.GetAuthorIdent().GetWhen().ToLocalTime(); ListViewItem item = new ListViewItem($"{commitTime.ToShortDateString()} {commitTime.ToShortTimeString()}"); item.SubItems.Add(c.GetFullMessage()); item.Tag = c; return(item); }
private static Commit ToCommit(RevCommit commit, Repository repository) { return(new Commit ( repository, commit.Name, commit.GetShortMessage(), ToPerson(commit.GetAuthorIdent()), ToPerson(commit.GetCommitterIdent()), commit.GetCommitterIdent().GetWhen(), commit.Parents.Select(p => p.Name) )); }
public virtual void CommitAmendWithAuthorShouldUseIt() { Git git = new Git(db); WriteTrashFile("file1", "file1"); git.Add().AddFilepattern("file1").Call(); git.Commit().SetMessage("initial commit").Call(); RevCommit amended = git.Commit().SetAmend(true).SetAuthor("New Author", "*****@*****.**" ).SetMessage("amend commit").Call(); PersonIdent amendedAuthor = amended.GetAuthorIdent(); NUnit.Framework.Assert.AreEqual("New Author", amendedAuthor.GetName()); NUnit.Framework.Assert.AreEqual("*****@*****.**", amendedAuthor.GetEmailAddress ()); }
public static RevisionInfo ConvertToRevisionInfo(this RevCommit commit, Repository repository) { var authorIdent = commit.GetAuthorIdent(); return(new RevisionInfo { Author = authorIdent.GetName(), Comment = commit.GetComment(), Id = commit.Id.Name, Time = commit.GetCommitTime(), Entries = commit.GetEntriesEnc(repository), Email = authorIdent.GetEmailAddress(), TimeCreated = commit.GetAuthorTime() }); }
/// <exception cref="System.IO.IOException"></exception> private RebaseResult Stop(RevCommit commitToPick) { PersonIdent author = commitToPick.GetAuthorIdent(); string authorScript = ToAuthorScript(author); CreateFile(rebaseDir, AUTHOR_SCRIPT, authorScript); CreateFile(rebaseDir, MESSAGE, commitToPick.GetFullMessage()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); DiffFormatter df = new DiffFormatter(bos); df.SetRepository(repo); df.Format(commitToPick.GetParent(0), commitToPick); CreateFile(rebaseDir, PATCH, Sharpen.Runtime.GetStringForBytes(bos.ToByteArray(), Constants.CHARACTER_ENCODING)); CreateFile(rebaseDir, STOPPED_SHA, repo.NewObjectReader().Abbreviate(commitToPick ).Name); return(new RebaseResult(commitToPick)); }
public virtual void Test009_CreateCommitOldFormat() { ObjectId treeId = InsertTree(new TreeFormatter()); NGit.CommitBuilder c = new NGit.CommitBuilder(); c.Author = new PersonIdent(author, 1154236443000L, -4 * 60); c.Committer = new PersonIdent(committer, 1154236443000L, -4 * 60); c.Message = "A Commit\n"; c.TreeId = treeId; NUnit.Framework.Assert.AreEqual(treeId, c.TreeId); ObjectId actid = InsertCommit(c); ObjectId cmtid = ObjectId.FromString("9208b2459ea6609a5af68627cc031796d0d9329b"); NUnit.Framework.Assert.AreEqual(cmtid, actid); // Verify the commit we just wrote is in the correct format. ObjectDatabase odb = ((ObjectDirectory)db.ObjectDatabase); NUnit.Framework.Assert.IsTrue(odb is ObjectDirectory, "is ObjectDirectory"); XInputStream xis = new XInputStream(new FileInputStream(((ObjectDirectory)odb).FileFor (cmtid))); try { NUnit.Framework.Assert.AreEqual(unchecked ((int)(0x78)), xis.ReadUInt8()); NUnit.Framework.Assert.AreEqual(unchecked ((int)(0x9c)), xis.ReadUInt8()); NUnit.Framework.Assert.IsTrue(unchecked ((int)(0x789c)) % 31 == 0); } finally { xis.Close(); } // Verify we can read it. RevCommit c2 = ParseCommit(actid); NUnit.Framework.Assert.IsNotNull(c2); NUnit.Framework.Assert.AreEqual(c.Message, c2.GetFullMessage()); NUnit.Framework.Assert.AreEqual(c.TreeId, c2.Tree); NUnit.Framework.Assert.AreEqual(c.Author, c2.GetAuthorIdent()); NUnit.Framework.Assert.AreEqual(c.Committer, c2.GetCommitterIdent()); }
public virtual void CommitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime( ) { Git git = new Git(db); WriteTrashFile("file1", "file1"); git.Add().AddFilepattern("file1").Call(); string authorName = "First Author"; string authorEmail = "*****@*****.**"; DateTime authorDate = Sharpen.Extensions.CreateDate(1349621117000L); PersonIdent firstAuthor = new PersonIdent(authorName, authorEmail, authorDate, Sharpen.Extensions.GetTimeZone ("UTC")); git.Commit().SetMessage("initial commit").SetAuthor(firstAuthor).Call(); RevCommit amended = git.Commit().SetAmend(true).SetMessage("amend commit").Call(); PersonIdent amendedAuthor = amended.GetAuthorIdent(); NUnit.Framework.Assert.AreEqual(authorName, amendedAuthor.GetName()); NUnit.Framework.Assert.AreEqual(authorEmail, amendedAuthor.GetEmailAddress()); NUnit.Framework.Assert.AreEqual(authorDate.GetTime(), amendedAuthor.GetWhen().GetTime ()); }
public static MergeCommandResult MergeTrees(ProgressMonitor monitor, NGit.Repository repo, RevCommit srcBase, RevCommit srcCommit, string sourceDisplayName, bool commitResult) { RevCommit newHead; RevWalk revWalk = new RevWalk(repo); try { // get the head commit Ref headRef = repo.GetRef(Constants.HEAD); if (headRef == null) { throw new NoHeadException(JGitText.Get().commitOnRepoWithoutHEADCurrentlyNotSupported ); } RevCommit headCommit = revWalk.ParseCommit(headRef.GetObjectId()); ResolveMerger merger = (ResolveMerger)((ThreeWayMerger)MergeStrategy.RESOLVE.NewMerger (repo)); merger.SetWorkingTreeIterator(new FileTreeIterator(repo)); merger.SetBase(srcBase); bool noProblems; IDictionary <string, MergeResult <Sequence> > lowLevelResults = null; IDictionary <string, ResolveMerger.MergeFailureReason> failingPaths = null; IList <string> modifiedFiles = null; ResolveMerger resolveMerger = merger; resolveMerger.SetCommitNames(new string[] { "BASE", "HEAD", sourceDisplayName }); noProblems = merger.Merge(headCommit, srcCommit); lowLevelResults = resolveMerger.GetMergeResults(); modifiedFiles = resolveMerger.GetModifiedFiles(); failingPaths = resolveMerger.GetFailingPaths(); if (monitor != null) { monitor.Update(50); } if (noProblems) { if (modifiedFiles != null && modifiedFiles.Count == 0) { return(new MergeCommandResult(headCommit, null, new ObjectId[] { headCommit.Id, srcCommit .Id }, MergeStatus.ALREADY_UP_TO_DATE, MergeStrategy.RESOLVE, null, null)); } DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.Tree, repo.LockDirCache (), merger.GetResultTreeId()); dco.SetFailOnConflict(true); dco.Checkout(); if (commitResult) { newHead = new NGit.Api.Git(repo).Commit().SetMessage(srcCommit.GetFullMessage() ).SetAuthor(srcCommit.GetAuthorIdent()).Call(); return(new MergeCommandResult(newHead.Id, null, new ObjectId[] { headCommit.Id, srcCommit .Id }, MergeStatus.MERGED, MergeStrategy.RESOLVE, null, null)); } else { return(new MergeCommandResult(headCommit, null, new ObjectId[] { headCommit.Id, srcCommit .Id }, MergeStatus.MERGED, MergeStrategy.RESOLVE, null, null)); } } else { if (failingPaths != null) { return(new MergeCommandResult(null, merger.GetBaseCommit(0, 1), new ObjectId[] { headCommit.Id, srcCommit.Id }, MergeStatus.FAILED, MergeStrategy.RESOLVE, lowLevelResults , failingPaths, null)); } else { return(new MergeCommandResult(null, merger.GetBaseCommit(0, 1), new ObjectId[] { headCommit.Id, srcCommit.Id }, MergeStatus.CONFLICTING, MergeStrategy.RESOLVE, lowLevelResults , null)); } } } finally { revWalk.Release(); } }
/// <summary> /// Executes the /// <code>Cherry-Pick</code> /// command with all the options and /// parameters collected by the setter methods (e.g. /// <see cref="Include(NGit.Ref)">Include(NGit.Ref)</see> /// of /// this class. Each instance of this class should only be used for one /// invocation of the command. Don't call this method twice on an instance. /// </summary> /// <returns>the result of the cherry-pick</returns> /// <exception cref="NGit.Api.Errors.GitAPIException"></exception> public override CherryPickResult Call() { RevCommit newHead = null; IList <Ref> cherryPickedRefs = new List <Ref>(); CheckCallable(); RevWalk revWalk = new RevWalk(repo); try { // get the head commit Ref headRef = repo.GetRef(Constants.HEAD); if (headRef == null) { throw new NoHeadException(JGitText.Get().commitOnRepoWithoutHEADCurrentlyNotSupported ); } RevCommit headCommit = revWalk.ParseCommit(headRef.GetObjectId()); newHead = headCommit; // loop through all refs to be cherry-picked foreach (Ref src in commits) { // get the commit to be cherry-picked // handle annotated tags ObjectId srcObjectId = src.GetPeeledObjectId(); if (srcObjectId == null) { srcObjectId = src.GetObjectId(); } RevCommit srcCommit = revWalk.ParseCommit(srcObjectId); // get the parent of the commit to cherry-pick if (srcCommit.ParentCount != 1) { throw new MultipleParentsNotAllowedException(JGitText.Get().canOnlyCherryPickCommitsWithOneParent ); } RevCommit srcParent = srcCommit.GetParent(0); revWalk.ParseHeaders(srcParent); ResolveMerger merger = (ResolveMerger)((ThreeWayMerger)MergeStrategy.RESOLVE.NewMerger (repo)); merger.SetWorkingTreeIterator(new FileTreeIterator(repo)); merger.SetBase(srcParent.Tree); if (merger.Merge(headCommit, srcCommit)) { if (AnyObjectId.Equals(headCommit.Tree.Id, merger.GetResultTreeId())) { continue; } DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.Tree, repo.LockDirCache (), merger.GetResultTreeId()); dco.SetFailOnConflict(true); dco.Checkout(); newHead = new Git(GetRepository()).Commit().SetMessage(srcCommit.GetFullMessage() ).SetAuthor(srcCommit.GetAuthorIdent()).Call(); cherryPickedRefs.AddItem(src); } else { if (merger.Failed()) { return(new CherryPickResult(merger.GetFailingPaths())); } // there are merge conflicts string message = new MergeMessageFormatter().FormatWithConflicts(srcCommit.GetFullMessage (), merger.GetUnmergedPaths()); repo.WriteCherryPickHead(srcCommit.Id); repo.WriteMergeCommitMsg(message); return(CherryPickResult.CONFLICT); } } } catch (IOException e) { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().exceptionCaughtDuringExecutionOfCherryPickCommand , e), e); } finally { revWalk.Release(); } return(new CherryPickResult(newHead, cherryPickedRefs)); }
public static DateTime GetAuthorTime(this RevCommit commit) { return(commit.GetAuthorIdent().GetWhen()); }
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)); }