/// <exception cref="System.InvalidOperationException"></exception> /// <exception cref="System.IO.IOException"></exception> private void PrescanTwoTrees(ObjectId head, ObjectId merge) { DirCache dc = db.LockDirCache(); try { dco = new DirCacheCheckout(db, head, dc, merge); dco.PreScanTwoTrees(); } finally { dc.Unlock(); } }
/// <exception cref="System.IO.IOException"></exception> public override void Checkout() { DirCache dc = db.LockDirCache(); try { dco = new DirCacheCheckout(db, theHead.GetId(), dc, theMerge.GetId()); dco.Checkout(); } finally { dc.Unlock(); } }
/// <exception cref="System.InvalidOperationException"></exception> /// <exception cref="System.IO.IOException"></exception> public override void PrescanTwoTrees(Tree head, Tree merge) { DirCache dc = db.LockDirCache(); try { dco = new DirCacheCheckout(db, head.GetId(), dc, merge.GetId()); dco.PreScanTwoTrees(); } finally { dc.Unlock(); } }
/// <exception cref="System.InvalidOperationException"></exception> /// <exception cref="System.IO.IOException"></exception> private void CheckoutCommit(RevCommit commit) { RevWalk walk = new RevWalk(db); RevCommit head = walk.ParseCommit(db.Resolve(Constants.HEAD)); DirCacheCheckout dco = new DirCacheCheckout(db, head.Tree, db.LockDirCache(), commit .Tree); dco.SetFailOnConflict(true); dco.Checkout(); walk.Release(); // update the HEAD RefUpdate refUpdate = db.UpdateRef(Constants.HEAD, true); refUpdate.SetNewObjectId(commit); refUpdate.ForceUpdate(); }
/// <exception cref="System.InvalidOperationException"></exception> /// <exception cref="System.IO.IOException"></exception> protected internal virtual void CheckoutBranch(string branchName) { RevWalk walk = new RevWalk(db); RevCommit head = walk.ParseCommit(db.Resolve(Constants.HEAD)); RevCommit branch = walk.ParseCommit(db.Resolve(branchName)); DirCacheCheckout dco = new DirCacheCheckout(db, head.Tree.Id, db.LockDirCache(), branch.Tree.Id); dco.SetFailOnConflict(true); dco.Checkout(); walk.Release(); // update the HEAD RefUpdate refUpdate = db.UpdateRef(Constants.HEAD); refUpdate.Link(branchName); }
public void SwitchToBranch (IProgressMonitor monitor, string branch) { monitor.BeginTask (GettextCatalog.GetString ("Switching to branch {0}", branch), GitService.StashUnstashWhenSwitchingBranches ? 4 : 2); // Get a list of files that are different in the target branch IEnumerable<DiffEntry> statusList = GitUtil.GetChangedFiles (RootRepository, branch); StashCollection stashes = null; Stash stash = null; if (GitService.StashUnstashWhenSwitchingBranches) { stashes = GitUtil.GetStashes (RootRepository); // Remove the stash for this branch, if exists string currentBranch = GetCurrentBranch (); stash = GetStashForBranch (stashes, currentBranch); if (stash != null) stashes.Remove (stash); // Create a new stash for the branch. This allows switching branches // without losing local changes using (var gm = new GitMonitor (monitor)) stash = stashes.Create (gm, GetStashName (currentBranch)); monitor.Step (1); } // Switch to the target branch DirCache dc = RootRepository.LockDirCache (); try { RevWalk rw = new RevWalk (RootRepository); ObjectId branchHeadId = RootRepository.Resolve (branch); if (branchHeadId == null) throw new InvalidOperationException ("Branch head commit not found"); RevCommit branchCommit = rw.ParseCommit (branchHeadId); DirCacheCheckout checkout = new DirCacheCheckout (RootRepository, null, dc, branchCommit.Tree); checkout.Checkout (); RefUpdate u = RootRepository.UpdateRef(Constants.HEAD); u.Link ("refs/heads/" + branch); monitor.Step (1); } catch { dc.Unlock (); if (GitService.StashUnstashWhenSwitchingBranches) { // If something goes wrong, restore the work tree status using (var gm = new GitMonitor (monitor)) stash.Apply (gm); stashes.Remove (stash); } throw; } // Restore the branch stash if (GitService.StashUnstashWhenSwitchingBranches) { stash = GetStashForBranch (stashes, branch); if (stash != null) { using (var gm = new GitMonitor (monitor)) stash.Apply (gm); stashes.Remove (stash); } monitor.Step (1); } // Notify file changes NotifyFileChanges (monitor, statusList); if (BranchSelectionChanged != null) BranchSelectionChanged (this, EventArgs.Empty); monitor.EndTask (); }
public static MergeCommandResult MergeTrees (NGit.Repository repo, RevCommit srcBase, RevCommit srcCommit, string sourceDisplayName, bool commitResult) { RevCommit newHead = null; 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)); // CherryPick command sets the working tree, but this should not be necessary, and when setting it // untracked files are deleted during the merge // merger.SetWorkingTreeIterator(new FileTreeIterator(repo)); merger.SetBase(srcBase); bool noProblems; IDictionary<string, MergeResult<NGit.Diff.Sequence>> lowLevelResults = null; IDictionary<string, ResolveMerger.MergeFailureReason> failingPaths = null; IList<string> modifiedFiles = null; ResolveMerger 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 (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 , 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(); } }
public static FileRepository Clone (string targetLocalPath, string url, IProgressMonitor monitor) { FileRepository repo = Init (targetLocalPath, url, monitor); // Fetch string remoteName = "origin"; string branch = Constants.R_HEADS + "master"; Transport tn = Transport.Open (repo, remoteName); FetchResult r; try { r = tn.Fetch(new GitMonitor (monitor), null); } finally { tn.Close (); } // Create the master branch // branch is like 'Constants.R_HEADS + branchName', we need only // the 'branchName' part String branchName = branch.Substring (Constants.R_HEADS.Length); NGit.Api.Git git = new NGit.Api.Git (repo); git.BranchCreate ().SetName (branchName).SetUpstreamMode (CreateBranchCommand.SetupUpstreamMode.TRACK).SetStartPoint ("origin/master").Call (); // Checkout DirCache dc = repo.LockDirCache (); try { RevWalk rw = new RevWalk (repo); ObjectId remCommitId = repo.Resolve (remoteName + "/" + branchName); RevCommit remCommit = rw.ParseCommit (remCommitId); DirCacheCheckout co = new DirCacheCheckout (repo, null, dc, remCommit.Tree); co.Checkout (); } catch { dc.Unlock (); throw; } return repo; }
public static void Checkout (NGit.Repository repo, RevCommit commit, string working_directory) { DirCacheCheckout co = new DirCacheCheckout (repo, null, repo.ReadDirCache (), commit.Tree); co.Checkout (); }
public static void HardReset (NGit.Repository repo, ObjectId newHead) { DirCache dc = null; try { // Reset head to upstream RefUpdate ru = repo.UpdateRef (Constants.HEAD); ru.SetNewObjectId (newHead); ru.SetForceUpdate (true); RefUpdate.Result rc = ru.Update (); switch (rc) { case RefUpdate.Result.NO_CHANGE: case RefUpdate.Result.NEW: case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: break; case RefUpdate.Result.REJECTED: case RefUpdate.Result.LOCK_FAILURE: throw new ConcurrentRefUpdateException (JGitText.Get ().couldNotLockHEAD, ru.GetRef (), rc); default: throw new JGitInternalException ("Reference update failed: " + rc); } dc = repo.LockDirCache (); RevWalk rw = new RevWalk (repo); RevCommit c = rw.ParseCommit (newHead); DirCacheCheckout checkout = new DirCacheCheckout (repo, null, dc, c.Tree); checkout.Checkout (); } catch { if (dc != null) dc.Unlock (); throw; } }
/// <exception cref="NGit.Errors.NoWorkTreeException"></exception> /// <exception cref="NGit.Errors.CorruptObjectException"></exception> /// <exception cref="System.IO.IOException"></exception> private DirCacheCheckout ResetHard(RevCommit commit) { DirCacheCheckout dc; dc = new DirCacheCheckout(db, null, db.LockDirCache(), commit.Tree); dc.SetFailOnConflict(true); NUnit.Framework.Assert.IsTrue(dc.Checkout()); return dc; }
/// <exception cref="System.IO.IOException"></exception> private void Checkout() { DirCache dc = db.LockDirCache(); try { dco = new DirCacheCheckout(db, theHead, dc, theMerge); dco.Checkout(); } finally { dc.Unlock(); } }
/// <exception cref="System.InvalidOperationException"></exception> /// <exception cref="System.IO.IOException"></exception> private void CheckoutBranch(string branchName) { RevWalk walk = new RevWalk(db); RevCommit head = walk.ParseCommit(db.Resolve(Constants.HEAD)); RevCommit branch = walk.ParseCommit(db.Resolve(branchName)); DirCacheCheckout dco = new DirCacheCheckout(db, head.Tree, db.LockDirCache(), branch .Tree); dco.SetFailOnConflict(true); NUnit.Framework.Assert.IsTrue(dco.Checkout()); walk.Release(); // update the HEAD RefUpdate refUpdate = db.UpdateRef(Constants.HEAD); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, refUpdate.Link(branchName )); }
public static FileRepository Clone (string targetLocalPath, string url, IProgressMonitor monitor) { // Initialize InitCommand ci = new InitCommand (); ci.SetDirectory (targetLocalPath); var git = ci.Call (); FileRepository repo = (FileRepository) git.GetRepository (); string branch = Constants.R_HEADS + "master"; string remoteName = "origin"; RefUpdate head = repo.UpdateRef (Constants.HEAD); head.DisableRefLog (); head.Link (branch); RemoteConfig remoteConfig = new RemoteConfig (repo.GetConfig (), remoteName); remoteConfig.AddURI (new URIish (url)); string dst = Constants.R_REMOTES + remoteConfig.Name; RefSpec wcrs = new RefSpec(); wcrs = wcrs.SetForceUpdate (true); wcrs = wcrs.SetSourceDestination (Constants.R_HEADS + "*", dst + "/*"); remoteConfig.AddFetchRefSpec (wcrs); // we're setting up for a clone with a checkout repo.GetConfig().SetBoolean ("core", null, "bare", false); remoteConfig.Update (repo.GetConfig()); repo.GetConfig().Save(); // Fetch Transport tn = Transport.Open (repo, remoteName); FetchResult r; try { r = tn.Fetch(new GitMonitor (monitor), null); } finally { tn.Close (); } // Create the master branch // branch is like 'Constants.R_HEADS + branchName', we need only // the 'branchName' part String branchName = branch.Substring (Constants.R_HEADS.Length); git.BranchCreate ().SetName (branchName).SetUpstreamMode (CreateBranchCommand.SetupUpstreamMode.TRACK).SetStartPoint ("origin/master").Call (); // Checkout DirCache dc = repo.LockDirCache (); try { RevWalk rw = new RevWalk (repo); ObjectId remCommitId = repo.Resolve (remoteName + "/" + branchName); RevCommit remCommit = rw.ParseCommit (remCommitId); DirCacheCheckout co = new DirCacheCheckout (repo, null, dc, remCommit.Tree); co.Checkout (); } catch { dc.Unlock (); throw; } return repo; }