public virtual void TestMergeEmptyBranches() { Git git = new Git(db); git.Commit().SetMessage("initial commit").Call(); RefUpdate r = db.UpdateRef("refs/heads/side"); r.SetNewObjectId(db.Resolve(Constants.HEAD)); NUnit.Framework.Assert.AreEqual(r.ForceUpdate(), RefUpdate.Result.NEW); RevCommit second = git.Commit().SetMessage("second commit").SetCommitter(committer ).Call(); db.UpdateRef(Constants.HEAD).Link("refs/heads/side"); RevCommit firstSide = git.Commit().SetMessage("first side commit").SetAuthor(author ).Call(); Write(new FilePath(db.Directory, Constants.MERGE_HEAD), ObjectId.ToString(db.Resolve ("refs/heads/master"))); Write(new FilePath(db.Directory, Constants.MERGE_MSG), "merging"); RevCommit commit = git.Commit().Call(); RevCommit[] parents = commit.Parents; NUnit.Framework.Assert.AreEqual(parents[0], firstSide); NUnit.Framework.Assert.AreEqual(parents[1], second); NUnit.Framework.Assert.IsTrue(parents.Length == 2); }
/// <exception cref="NGit.Api.Errors.JGitInternalException"></exception> /// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="System.IO.IOException"></exception> private void Checkout(Repository repo, FetchResult result) { if (branch.StartsWith(Constants.R_HEADS)) { RefUpdate head = repo.UpdateRef(Constants.HEAD); head.DisableRefLog(); head.Link(branch); } Ref head_1 = result.GetAdvertisedRef(branch); if (head_1 == null || head_1.GetObjectId() == null) { return; } // throw exception? RevCommit commit = ParseCommit(repo, head_1); bool detached = !head_1.GetName().StartsWith(Constants.R_HEADS); RefUpdate u = repo.UpdateRef(Constants.HEAD, detached); u.SetNewObjectId(commit.Id); u.ForceUpdate(); if (!bare) { DirCache dc = repo.LockDirCache(); DirCacheCheckout co = new DirCacheCheckout(repo, dc, commit.Tree); co.Checkout(); } }
private void doCheckout(GitSharp.Core.Ref branch) { if (branch == null) { throw new ArgumentNullException("branch", "Cannot checkout; no HEAD advertised by remote"); } var repo = Repository._internal_repo; if (!Constants.HEAD.Equals(branch.Name)) { repo.WriteSymref(Constants.HEAD, branch.Name); } GitSharp.Core.Commit commit = repo.MapCommit(branch.ObjectId); RefUpdate u = repo.UpdateRef(Constants.HEAD); u.NewObjectId = commit.CommitId; u.ForceUpdate(); GitIndex index = new GitIndex(repo); GitSharp.Core.Tree tree = commit.TreeEntry; WorkDirCheckout co = new WorkDirCheckout(repo, repo.WorkingDirectory, index, tree); co.checkout(); index.write(); }
/// <exception cref="System.IO.IOException"></exception> private void CheckoutCommit(RevCommit commit) { try { RevCommit head = walk.ParseCommit(repo.Resolve(Constants.HEAD)); DirCacheCheckout dco = new DirCacheCheckout(repo, head.Tree, repo.LockDirCache(), commit.Tree); dco.SetFailOnConflict(true); dco.Checkout(); // update the HEAD RefUpdate refUpdate = repo.UpdateRef(Constants.HEAD, true); refUpdate.SetExpectedOldObjectId(head); refUpdate.SetNewObjectId(commit); RefUpdate.Result res = refUpdate.ForceUpdate(); switch (res) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.NO_CHANGE: case RefUpdate.Result.FORCED: { break; } default: { throw new IOException("Could not rewind to upstream commit"); } } } finally { walk.Release(); monitor.EndTask(); } }
public virtual void Test028_LockPackedRef() { WriteTrashFile(".git/packed-refs", "7f822839a2fe9760f386cbbbcb3f92c5fe81def7 refs/heads/foobar" ); WriteTrashFile(".git/HEAD", "ref: refs/heads/foobar\n"); BUG_WorkAroundRacyGitIssues("packed-refs"); BUG_WorkAroundRacyGitIssues("HEAD"); ObjectId resolve = db.Resolve("HEAD"); NUnit.Framework.Assert.AreEqual("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", resolve .Name); RefUpdate lockRef = db.UpdateRef("HEAD"); ObjectId newId = ObjectId.FromString("07f822839a2fe9760f386cbbbcb3f92c5fe81def"); lockRef.SetNewObjectId(newId); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, lockRef.ForceUpdate()); NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "refs/heads/foobar").Exists ()); NUnit.Framework.Assert.AreEqual(newId, db.Resolve("refs/heads/foobar")); // Again. The ref already exists RefUpdate lockRef2 = db.UpdateRef("HEAD"); ObjectId newId2 = ObjectId.FromString("7f822839a2fe9760f386cbbbcb3f92c5fe81def7"); lockRef2.SetNewObjectId(newId2); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, lockRef2.ForceUpdate()); NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "refs/heads/foobar").Exists ()); NUnit.Framework.Assert.AreEqual(newId2, db.Resolve("refs/heads/foobar")); }
public void test028_LockPackedRef() { writeTrashFile(".git/packed-refs", "7f822839a2fe9760f386cbbbcb3f92c5fe81def7 refs/heads/foobar"); writeTrashFile(".git/HEAD", "ref: refs/heads/foobar\n"); ObjectId resolve = db.Resolve("HEAD"); Assert.AreEqual("7f822839a2fe9760f386cbbbcb3f92c5fe81def7", resolve.Name); RefUpdate lockRef = db.UpdateRef("HEAD"); ObjectId newId = ObjectId.FromString("07f822839a2fe9760f386cbbbcb3f92c5fe81def"); lockRef.NewObjectId = newId; Assert.AreEqual(RefUpdate.RefUpdateResult.Forced, lockRef.ForceUpdate()); Assert.IsTrue(new FileInfo(db.Directory.FullName + "/refs/heads/foobar").Exists); Assert.AreEqual(newId, db.Resolve("refs/heads/foobar")); // Again. The ref already exists RefUpdate lockRef2 = db.UpdateRef("HEAD"); ObjectId newId2 = ObjectId.FromString("7f822839a2fe9760f386cbbbcb3f92c5fe81def7"); lockRef2.NewObjectId = newId2; Assert.AreEqual(RefUpdate.RefUpdateResult.Forced, lockRef2.ForceUpdate()); Assert.IsTrue(new FileInfo(db.Directory.FullName + "/refs/heads/foobar").Exists); Assert.AreEqual(newId2, db.Resolve("refs/heads/foobar")); }
public void testTwoSuccessiveCommitsLinkedToHead() { var repo = createBareRepository(); var workingDirectory = repo.WorkingDirectory; repo.Create(); var objectWriter = new ObjectWriter(repo); FileInfo project1_a_txt = writeTrashFile(Path.Combine(workingDirectory.FullName, "Project-1/A.txt"), "A.txt - first version\n"); FileInfo project1_b_txt = writeTrashFile(Path.Combine(workingDirectory.FullName, "Project-1/B.txt"), "B.txt - first version\n"); var tree = new Core.Tree(repo); Core.Tree projectTree = tree.AddTree("Project-1"); addFile(projectTree, project1_a_txt, objectWriter); projectTree.Id = (objectWriter.WriteTree(projectTree)); addFile(projectTree, project1_b_txt, objectWriter); projectTree.Id = (objectWriter.WriteTree(projectTree)); tree.Id = (objectWriter.WriteTree(tree)); var commit = new Core.Commit(repo) { Author = new PersonIdent(author, (0L), 60), Committer = new PersonIdent(committer, (0L), 60), Message = "Foo\n\nMessage", TreeEntry = tree }; commit.Save(); var commitId = commit.CommitId; FileInfo project1_b_v2_txt = writeTrashFile(Path.Combine(workingDirectory.FullName, "Project-1/B.txt"), "B.txt - second version\n"); tree = new Core.Tree(repo); projectTree = tree.AddTree("Project-1"); addFile(projectTree, project1_a_txt, objectWriter); projectTree.Id = (objectWriter.WriteTree(projectTree)); addFile(projectTree, project1_b_v2_txt, objectWriter); projectTree.Id = (objectWriter.WriteTree(projectTree)); tree.Id = (objectWriter.WriteTree(tree)); commit = new Core.Commit(repo) { Author = new PersonIdent(author, (0L), 60), Committer = new PersonIdent(committer, (0L), 60), Message = "Modified", ParentIds = new[] { commitId }, TreeEntry = tree }; commit.Save(); commitId = commit.CommitId; RefUpdate lck = repo.UpdateRef("refs/heads/master"); Assert.IsNotNull(lck, "obtained lock"); lck.NewObjectId = commitId; Assert.AreEqual(RefUpdate.RefUpdateResult.New, lck.ForceUpdate()); }
public virtual void RepositoryWithDifferentRevCheckedOutSubmodule() { ObjectId id = ObjectId.FromString("abcd1234abcd1234abcd1234abcd1234abcd1234"); string path = "sub"; DirCache cache = db.LockDirCache(); DirCacheEditor editor = cache.Editor(); editor.Add(new _PathEdit_317(id, path)); editor.Commit(); string url = "git://server/repo.git"; StoredConfig config = ((FileBasedConfig)db.GetConfig()); config.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants. CONFIG_KEY_URL, url); config.Save(); FileBasedConfig modulesConfig = new FileBasedConfig(new FilePath(db.WorkTree, Constants .DOT_GIT_MODULES), db.FileSystem); modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants .CONFIG_KEY_PATH, path); modulesConfig.SetString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, ConfigConstants .CONFIG_KEY_URL, url); modulesConfig.Save(); Repository subRepo = Git.Init().SetBare(false).SetDirectory(new FilePath(db.WorkTree , path)).Call().GetRepository(); NUnit.Framework.Assert.IsNotNull(subRepo); RefUpdate update = subRepo.UpdateRef(Constants.HEAD, true); update.SetNewObjectId(ObjectId.FromString("aaaa0000aaaa0000aaaa0000aaaa0000aaaa0000" )); update.ForceUpdate(); SubmoduleStatusCommand command = new SubmoduleStatusCommand(db); IDictionary <string, SubmoduleStatus> statuses = command.Call(); NUnit.Framework.Assert.IsNotNull(statuses); NUnit.Framework.Assert.AreEqual(1, statuses.Count); KeyValuePair <string, SubmoduleStatus> module = statuses.EntrySet().Iterator().Next (); NUnit.Framework.Assert.IsNotNull(module); NUnit.Framework.Assert.AreEqual(path, module.Key); SubmoduleStatus status = module.Value; NUnit.Framework.Assert.IsNotNull(status); NUnit.Framework.Assert.AreEqual(path, status.GetPath()); NUnit.Framework.Assert.AreEqual(id, status.GetIndexId()); NUnit.Framework.Assert.AreEqual(update.GetNewObjectId(), status.GetHeadId()); NUnit.Framework.Assert.AreEqual(SubmoduleStatusType.REV_CHECKED_OUT, status.GetType ()); }
/// <exception cref="System.Exception"></exception> private Git SetUpRepoWithRemote() { Repository remoteRepository = CreateWorkRepository(); Git remoteGit = new Git(remoteRepository); // commit something WriteTrashFile("Test.txt", "Hello world"); remoteGit.Add().AddFilepattern("Test.txt").Call(); initialCommit = remoteGit.Commit().SetMessage("Initial commit").Call(); WriteTrashFile("Test.txt", "Some change"); remoteGit.Add().AddFilepattern("Test.txt").Call(); secondCommit = remoteGit.Commit().SetMessage("Second commit").Call(); // create a master branch RefUpdate rup = remoteRepository.UpdateRef("refs/heads/master"); rup.SetNewObjectId(initialCommit.Id); rup.ForceUpdate(); Repository localRepository = CreateWorkRepository(); Git localGit = new Git(localRepository); StoredConfig config = localRepository.GetConfig(); RemoteConfig rc = new RemoteConfig(config, "origin"); rc.AddURI(new URIish(remoteRepository.Directory.GetPath())); rc.AddFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*")); rc.Update(config); config.Save(); FetchResult res = localGit.Fetch().SetRemote("origin").Call(); NUnit.Framework.Assert.IsFalse(res.GetTrackingRefUpdates().IsEmpty()); rup = localRepository.UpdateRef("refs/heads/master"); rup.SetNewObjectId(initialCommit.Id); rup.ForceUpdate(); rup = localRepository.UpdateRef(Constants.HEAD); rup.Link("refs/heads/master"); rup.SetNewObjectId(initialCommit.Id); rup.Update(); return(localGit); }
/// <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.IO.IOException"></exception> private void UpdateStashRef(ObjectId commitId, PersonIdent refLogIdent, string refLogMessage ) { Ref currentRef = repo.GetRef(@ref); RefUpdate refUpdate = repo.UpdateRef(@ref); refUpdate.SetNewObjectId(commitId); refUpdate.SetRefLogIdent(refLogIdent); refUpdate.SetRefLogMessage(refLogMessage, false); if (currentRef != null) { refUpdate.SetExpectedOldObjectId(currentRef.GetObjectId()); } else { refUpdate.SetExpectedOldObjectId(ObjectId.ZeroId); } refUpdate.ForceUpdate(); }
private void commit(Core.Tree t, string commitMsg, PersonIdent author, PersonIdent committer) { Core.Commit commit = new Core.Commit(db); commit.Author = (author); commit.Committer = (committer); commit.Message = (commitMsg); commit.TreeEntry = (t); //ObjectWriter writer = new ObjectWriter(db); //commit.CommitId = (writer.WriteCommit(commit)); commit.Save(); int nl = commitMsg.IndexOf('\n'); RefUpdate ru = db.UpdateRef(Constants.HEAD); ru.NewObjectId = (commit.CommitId); ru.SetRefLogMessage("commit : " + ((nl == -1) ? commitMsg : commitMsg.Slice(0, nl)), false); ru.ForceUpdate(); }
/// <exception cref="NGit.Errors.MissingObjectException"></exception> /// <exception cref="NGit.Errors.IncorrectObjectTypeException"></exception> /// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Api.Errors.GitAPIException"></exception> private void Checkout(Repository clonedRepo, FetchResult result) { Ref head = result.GetAdvertisedRef(branch); if (branch.Equals(Constants.HEAD)) { Ref foundBranch = FindBranchToCheckout(result); if (foundBranch != null) { head = foundBranch; } } if (head == null || head.GetObjectId() == null) { return; } // throw exception? if (head.GetName().StartsWith(Constants.R_HEADS)) { RefUpdate newHead = clonedRepo.UpdateRef(Constants.HEAD); newHead.DisableRefLog(); newHead.Link(head.GetName()); AddMergeConfig(clonedRepo, head); } RevCommit commit = ParseCommit(clonedRepo, head); bool detached = !head.GetName().StartsWith(Constants.R_HEADS); RefUpdate u = clonedRepo.UpdateRef(Constants.HEAD, detached); u.SetNewObjectId(commit.Id); u.ForceUpdate(); if (!bare) { DirCache dc = clonedRepo.LockDirCache(); DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc, commit.Tree); co.Checkout(); if (cloneSubmodules) { CloneSubmodules(clonedRepo); } } }
/// <exception cref="System.IO.IOException"></exception> private void UpdateHead(string headName, RevCommit newHead) { // point the previous head (if any) to the new commit if (headName.StartsWith(Constants.R_REFS)) { RefUpdate rup = repo.UpdateRef(headName); rup.SetNewObjectId(newHead); RefUpdate.Result res = rup.ForceUpdate(); switch (res) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { throw new JGitInternalException("Updating HEAD failed"); } } rup = repo.UpdateRef(Constants.HEAD); res = rup.Link(headName); switch (res) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { throw new JGitInternalException("Updating HEAD failed"); } } } }
/// <exception cref="NGit.Api.Errors.RefAlreadyExistsException"> /// when trying to create (without force) a branch with a name /// that already exists /// </exception> /// <exception cref="NGit.Api.Errors.RefNotFoundException">if the start point can not be found /// </exception> /// <exception cref="NGit.Api.Errors.InvalidRefNameException"> /// if the provided name is <code>null</code> or otherwise /// invalid /// </exception> /// <returns>the newly created branch</returns> /// <exception cref="NGit.Api.Errors.GitAPIException"></exception> public override Ref Call() { CheckCallable(); ProcessOptions(); RevWalk revWalk = new RevWalk(repo); try { Ref refToCheck = repo.GetRef(name); bool exists = refToCheck != null && refToCheck.GetName().StartsWith(Constants.R_HEADS ); if (!force && exists) { throw new RefAlreadyExistsException(MessageFormat.Format(JGitText.Get().refAlreadyExists1 , name)); } ObjectId startAt = GetStartPoint(); string startPointFullName = null; if (startPoint != null) { Ref baseRef = repo.GetRef(startPoint); if (baseRef != null) { startPointFullName = baseRef.GetName(); } } // determine whether we are based on a commit, // a branch, or a tag and compose the reflog message string refLogMessage; string baseBranch = string.Empty; if (startPointFullName == null) { string baseCommit; if (startCommit != null) { baseCommit = startCommit.GetShortMessage(); } else { RevCommit commit = revWalk.ParseCommit(repo.Resolve(startPoint)); baseCommit = commit.GetShortMessage(); } if (exists) { refLogMessage = "branch: Reset start-point to commit " + baseCommit; } else { refLogMessage = "branch: Created from commit " + baseCommit; } } else { if (startPointFullName.StartsWith(Constants.R_HEADS) || startPointFullName.StartsWith (Constants.R_REMOTES)) { baseBranch = startPointFullName; if (exists) { refLogMessage = "branch: Reset start-point to branch " + startPointFullName; } else { // TODO refLogMessage = "branch: Created from branch " + baseBranch; } } else { startAt = revWalk.Peel(revWalk.ParseAny(startAt)); if (exists) { refLogMessage = "branch: Reset start-point to tag " + startPointFullName; } else { refLogMessage = "branch: Created from tag " + startPointFullName; } } } RefUpdate updateRef = repo.UpdateRef(Constants.R_HEADS + name); updateRef.SetNewObjectId(startAt); updateRef.SetRefLogMessage(refLogMessage, false); RefUpdate.Result updateResult; if (exists && force) { updateResult = updateRef.ForceUpdate(); } else { updateResult = updateRef.Update(); } SetCallable(false); bool ok = false; switch (updateResult) { case RefUpdate.Result.NEW: { ok = !exists; break; } case RefUpdate.Result.NO_CHANGE: case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: { ok = exists; break; } default: { break; break; } } if (!ok) { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().createBranchUnexpectedResult , updateResult.ToString())); } Ref result = repo.GetRef(name); if (result == null) { throw new JGitInternalException(JGitText.Get().createBranchFailedUnknownReason); } if (baseBranch.Length == 0) { return(result); } // if we are based on another branch, see // if we need to configure upstream configuration: first check // whether the setting was done explicitly bool doConfigure; if (upstreamMode == CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM || upstreamMode == CreateBranchCommand.SetupUpstreamMode.TRACK) { // explicitly set to configure doConfigure = true; } else { if (upstreamMode == CreateBranchCommand.SetupUpstreamMode.NOTRACK) { // explicitly set to not configure doConfigure = false; } else { // if there was no explicit setting, check the configuration string autosetupflag = repo.GetConfig().GetString(ConfigConstants.CONFIG_BRANCH_SECTION , null, ConfigConstants.CONFIG_KEY_AUTOSETUPMERGE); if ("false".Equals(autosetupflag)) { doConfigure = false; } else { if ("always".Equals(autosetupflag)) { doConfigure = true; } else { // in this case, the default is to configure // only in case the base branch was a remote branch doConfigure = baseBranch.StartsWith(Constants.R_REMOTES); } } } } if (doConfigure) { StoredConfig config = repo.GetConfig(); string[] tokens = baseBranch.RegexSplit("/", 4); bool isRemote = tokens[1].Equals("remotes"); if (isRemote) { // refs/remotes/<remote name>/<branch> string remoteName = tokens[2]; string branchName = tokens[3]; config.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, name, ConfigConstants.CONFIG_KEY_REMOTE , remoteName); config.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, name, ConfigConstants.CONFIG_KEY_MERGE , Constants.R_HEADS + branchName); } else { // set "." as remote config.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, name, ConfigConstants.CONFIG_KEY_REMOTE , "."); config.SetString(ConfigConstants.CONFIG_BRANCH_SECTION, name, ConfigConstants.CONFIG_KEY_MERGE , baseBranch); } config.Save(); } return(result); } catch (IOException ioe) { throw new JGitInternalException(ioe.Message, ioe); } finally { revWalk.Release(); } }
// TODO not implemented yet // TODO not implemented yet /// <summary> /// Executes the /// <code>Reset</code> /// command. 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 Ref after reset</returns> /// <exception cref="System.IO.IOException"></exception> public override Ref Call() { CheckCallable(); Ref r; RevCommit commit; try { RepositoryState state = repo.GetRepositoryState(); bool merging = state.Equals(RepositoryState.MERGING) || state.Equals(RepositoryState .MERGING_RESOLVED); bool cherryPicking = state.Equals(RepositoryState.CHERRY_PICKING) || state.Equals (RepositoryState.CHERRY_PICKING_RESOLVED); // resolve the ref to a commit ObjectId commitId; try { commitId = repo.Resolve(@ref); } catch (IOException e) { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().cannotRead, @ref ), e); } RevWalk rw = new RevWalk(repo); try { commit = rw.ParseCommit(commitId); } catch (IOException e) { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().cannotReadCommit , commitId.ToString()), e); } finally { rw.Release(); } if (!filepaths.IsEmpty()) { // reset [commit] -- paths ResetIndexForPaths(commit); SetCallable(false); return(repo.GetRef(Constants.HEAD)); } // write the ref RefUpdate ru = repo.UpdateRef(Constants.HEAD); ru.SetNewObjectId(commitId); string refName = Repository.ShortenRefName(@ref); string message = refName + ": updating " + Constants.HEAD; //$NON-NLS-1$ ru.SetRefLogMessage(message, false); if (ru.ForceUpdate() == RefUpdate.Result.LOCK_FAILURE) { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().cannotLock, ru .GetName())); } switch (mode) { case ResetCommand.ResetType.HARD: { CheckoutIndex(commit); break; } case ResetCommand.ResetType.MIXED: { ResetIndex(commit); break; } case ResetCommand.ResetType.SOFT: { // do nothing, only the ref was changed break; } case ResetCommand.ResetType.KEEP: case ResetCommand.ResetType.MERGE: { // TODO // TODO throw new NotSupportedException(); } } if (mode != ResetCommand.ResetType.SOFT) { if (merging) { ResetMerge(); } else { if (cherryPicking) { ResetCherryPick(); } } } SetCallable(false); r = ru.GetRef(); } catch (IOException e) { throw new JGitInternalException(JGitText.Get().exceptionCaughtDuringExecutionOfResetCommand , e); } return(r); }
public virtual void TestRenameLocalBranch() { // null newName not allowed try { git.BranchRename().Call(); } catch (InvalidRefNameException) { } // expected // invalid newName not allowed try { git.BranchRename().SetNewName("In va lid").Call(); } catch (InvalidRefNameException) { } // expected // not existing name not allowed try { git.BranchRename().SetOldName("notexistingbranch").SetNewName("newname").Call(); } catch (RefNotFoundException) { } // expected // create some branch CreateBranch(git, "existing", false, "master", null); // a local branch Ref branch = CreateBranch(git, "fromMasterForRename", false, "master", null); NUnit.Framework.Assert.AreEqual(Constants.R_HEADS + "fromMasterForRename", branch .GetName()); Ref renamed = git.BranchRename().SetOldName("fromMasterForRename").SetNewName("newName" ).Call(); NUnit.Framework.Assert.AreEqual(Constants.R_HEADS + "newName", renamed.GetName()); try { git.BranchRename().SetOldName(renamed.GetName()).SetNewName("existing").Call(); NUnit.Framework.Assert.Fail("Should have failed"); } catch (RefAlreadyExistsException) { } // expected try { git.BranchRename().SetNewName("In va lid").Call(); NUnit.Framework.Assert.Fail("Rename with invalid ref name should fail"); } catch (InvalidRefNameException) { } // expected // rename without old name and detached head not allowed RefUpdate rup = git.GetRepository().UpdateRef(Constants.HEAD, true); rup.SetNewObjectId(initialCommit); rup.ForceUpdate(); try { git.BranchRename().SetNewName("detached").Call(); } catch (DetachedHeadException) { } }
/// <summary> /// Executes the /// <code>Rebase</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. Don't call /// this method twice on an instance. /// </summary> /// <returns>an object describing the result of this command</returns> /// <exception cref="NGit.Api.Errors.NoHeadException"></exception> /// <exception cref="NGit.Api.Errors.RefNotFoundException"></exception> /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception> /// <exception cref="NGit.Api.Errors.GitAPIException"></exception> public override RebaseResult Call() { RevCommit newHead = null; bool lastStepWasForward = false; CheckCallable(); CheckParameters(); try { switch (operation) { case RebaseCommand.Operation.ABORT: { try { return(Abort()); } catch (IOException ioe) { throw new JGitInternalException(ioe.Message, ioe); } goto case RebaseCommand.Operation.SKIP; } case RebaseCommand.Operation.SKIP: case RebaseCommand.Operation.CONTINUE: { // fall through string upstreamCommitName = ReadFile(rebaseDir, ONTO); this.upstreamCommit = walk.ParseCommit(repo.Resolve(upstreamCommitName)); break; } case RebaseCommand.Operation.BEGIN: { RebaseResult res = InitFilesAndRewind(); if (res != null) { return(res); } break; } } if (monitor.IsCancelled()) { return(Abort()); } if (this.operation == RebaseCommand.Operation.CONTINUE) { newHead = ContinueRebase(); } if (this.operation == RebaseCommand.Operation.SKIP) { newHead = CheckoutCurrentHead(); } ObjectReader or = repo.NewObjectReader(); IList <RebaseCommand.Step> steps = LoadSteps(); foreach (RebaseCommand.Step step in steps) { PopSteps(1); ICollection <ObjectId> ids = or.Resolve(step.commit); if (ids.Count != 1) { throw new JGitInternalException("Could not resolve uniquely the abbreviated object ID" ); } RevCommit commitToPick = walk.ParseCommit(ids.Iterator().Next()); if (monitor.IsCancelled()) { return(new RebaseResult(commitToPick)); } monitor.BeginTask(MessageFormat.Format(JGitText.Get().applyingCommit, commitToPick .GetShortMessage()), ProgressMonitor.UNKNOWN); // if the first parent of commitToPick is the current HEAD, // we do a fast-forward instead of cherry-pick to avoid // unnecessary object rewriting newHead = TryFastForward(commitToPick); lastStepWasForward = newHead != null; if (!lastStepWasForward) { // TODO if the content of this commit is already merged here // we should skip this step in order to avoid confusing // pseudo-changed newHead = new Git(repo).CherryPick().Include(commitToPick).Call(); } monitor.EndTask(); if (newHead == null) { return(Stop(commitToPick)); } } if (newHead != null) { // point the previous head (if any) to the new commit string headName = ReadFile(rebaseDir, HEAD_NAME); if (headName.StartsWith(Constants.R_REFS)) { RefUpdate rup = repo.UpdateRef(headName); rup.SetNewObjectId(newHead); RefUpdate.Result res_1 = rup.ForceUpdate(); switch (res_1) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { throw new JGitInternalException("Updating HEAD failed"); } } rup = repo.UpdateRef(Constants.HEAD); res_1 = rup.Link(headName); switch (res_1) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { throw new JGitInternalException("Updating HEAD failed"); } } } FileUtils.Delete(rebaseDir, FileUtils.RECURSIVE); if (lastStepWasForward) { return(new RebaseResult(RebaseResult.Status.FAST_FORWARD)); } return(new RebaseResult(RebaseResult.Status.OK)); } return(new RebaseResult(RebaseResult.Status.UP_TO_DATE)); } catch (IOException ioe) { throw new JGitInternalException(ioe.Message, ioe); } }
/// <exception cref="NGit.Api.Errors.RefAlreadyExistsException"> /// when trying to create (without force) a branch with a name /// that already exists /// </exception> /// <exception cref="NGit.Api.Errors.RefNotFoundException">if the start point or branch can not be found /// </exception> /// <exception cref="NGit.Api.Errors.InvalidRefNameException"> /// if the provided name is <code>null</code> or otherwise /// invalid /// </exception> /// <returns>the newly created branch</returns> /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception> public override Ref Call() { CheckCallable(); ProcessOptions(); try { if (createBranch) { Git git = new Git(repo); CreateBranchCommand command = git.BranchCreate(); command.SetName(name); command.SetStartPoint(GetStartPoint().Name); if (upstreamMode != null) { command.SetUpstreamMode(upstreamMode); } command.Call(); } Ref headRef = repo.GetRef(Constants.HEAD); string refLogMessage = "checkout: moving from " + headRef.GetTarget().GetName(); ObjectId branch = repo.Resolve(name); if (branch == null) { throw new RefNotFoundException(MessageFormat.Format(JGitText.Get().refNotResolved , name)); } RevWalk revWalk = new RevWalk(repo); AnyObjectId headId = headRef.GetObjectId(); RevCommit headCommit = headId == null ? null : revWalk.ParseCommit(headId); RevCommit newCommit = revWalk.ParseCommit(branch); RevTree headTree = headCommit == null ? null : headCommit.Tree; DirCacheCheckout dco = new DirCacheCheckout(repo, headTree, repo.LockDirCache(), newCommit.Tree); dco.SetFailOnConflict(true); try { dco.Checkout(); } catch (NGit.Errors.CheckoutConflictException e) { status = new CheckoutResult(CheckoutResult.Status.CONFLICTS, dco.GetConflicts()); throw; } Ref @ref = repo.GetRef(name); if (@ref != null && [email protected]().StartsWith(Constants.R_HEADS)) { @ref = null; } RefUpdate refUpdate = repo.UpdateRef(Constants.HEAD, @ref == null); refUpdate.SetForceUpdate(force); refUpdate.SetRefLogMessage(refLogMessage + " to " + newCommit.GetName(), false); RefUpdate.Result updateResult; if (@ref != null) { updateResult = refUpdate.Link(@ref.GetName()); } else { refUpdate.SetNewObjectId(newCommit); updateResult = refUpdate.ForceUpdate(); } SetCallable(false); bool ok = false; switch (updateResult) { case RefUpdate.Result.NEW: { ok = true; break; } case RefUpdate.Result.NO_CHANGE: case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.FORCED: { ok = true; break; } default: { break; break; } } if (!ok) { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().checkoutUnexpectedResult , updateResult.ToString())); } if (!dco.GetToBeDeleted().IsEmpty()) { status = new CheckoutResult(CheckoutResult.Status.NONDELETED, dco.GetToBeDeleted( )); } else { status = CheckoutResult.OK_RESULT; } return(@ref); } catch (IOException ioe) { throw new JGitInternalException(ioe.Message, ioe); } finally { if (status == null) { status = CheckoutResult.ERROR_RESULT; } } }
/// <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); } }
public virtual void CommitSubmoduleUpdate() { Git git = new Git(db); WriteTrashFile("file.txt", "content"); git.Add().AddFilepattern("file.txt").Call(); RevCommit commit = git.Commit().SetMessage("create file").Call(); WriteTrashFile("file.txt", "content2"); git.Add().AddFilepattern("file.txt").Call(); RevCommit commit2 = git.Commit().SetMessage("edit file").Call(); SubmoduleAddCommand command = new SubmoduleAddCommand(db); string path = "sub"; command.SetPath(path); string uri = db.Directory.ToURI().ToString(); command.SetURI(uri); Repository repo = command.Call(); NUnit.Framework.Assert.IsNotNull(repo); AddRepoToClose(repo); SubmoduleWalk generator = SubmoduleWalk.ForIndex(db); NUnit.Framework.Assert.IsTrue(generator.Next()); NUnit.Framework.Assert.AreEqual(path, generator.GetPath()); NUnit.Framework.Assert.AreEqual(commit2, generator.GetObjectId()); NUnit.Framework.Assert.AreEqual(uri, generator.GetModulesUrl()); NUnit.Framework.Assert.AreEqual(path, generator.GetModulesPath()); NUnit.Framework.Assert.AreEqual(uri, generator.GetConfigUrl()); Repository subModRepo = generator.GetRepository(); AddRepoToClose(subModRepo); NUnit.Framework.Assert.IsNotNull(subModRepo); NUnit.Framework.Assert.AreEqual(commit2, repo.Resolve(Constants.HEAD)); RevCommit submoduleAddCommit = git.Commit().SetMessage("submodule add").SetOnly(path ).Call(); NUnit.Framework.Assert.IsNotNull(submoduleAddCommit); RefUpdate update = repo.UpdateRef(Constants.HEAD); update.SetNewObjectId(commit); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, update.ForceUpdate()); RevCommit submoduleEditCommit = git.Commit().SetMessage("submodule add").SetOnly( path).Call(); NUnit.Framework.Assert.IsNotNull(submoduleEditCommit); TreeWalk walk = new TreeWalk(db); walk.AddTree(submoduleAddCommit.Tree); walk.AddTree(submoduleEditCommit.Tree); walk.Filter = TreeFilter.ANY_DIFF; IList <DiffEntry> diffs = DiffEntry.Scan(walk); NUnit.Framework.Assert.AreEqual(1, diffs.Count); DiffEntry subDiff = diffs[0]; NUnit.Framework.Assert.AreEqual(FileMode.GITLINK, subDiff.GetOldMode()); NUnit.Framework.Assert.AreEqual(FileMode.GITLINK, subDiff.GetNewMode()); NUnit.Framework.Assert.AreEqual(commit2, subDiff.GetOldId().ToObjectId()); NUnit.Framework.Assert.AreEqual(commit, subDiff.GetNewId().ToObjectId()); NUnit.Framework.Assert.AreEqual(path, subDiff.GetNewPath()); NUnit.Framework.Assert.AreEqual(path, subDiff.GetOldPath()); }
/// <summary>Execute the SubmoduleUpdateCommand command.</summary> /// <remarks>Execute the SubmoduleUpdateCommand command.</remarks> /// <returns>a collection of updated submodule paths</returns> /// <exception cref="NGit.Api.Errors.ConcurrentRefUpdateException">NGit.Api.Errors.ConcurrentRefUpdateException /// </exception> /// <exception cref="NGit.Api.Errors.CheckoutConflictException">NGit.Api.Errors.CheckoutConflictException /// </exception> /// <exception cref="NGit.Api.Errors.InvalidMergeHeadsException">NGit.Api.Errors.InvalidMergeHeadsException /// </exception> /// <exception cref="NGit.Api.Errors.InvalidConfigurationException">NGit.Api.Errors.InvalidConfigurationException /// </exception> /// <exception cref="NGit.Api.Errors.NoHeadException">NGit.Api.Errors.NoHeadException /// </exception> /// <exception cref="NGit.Api.Errors.NoMessageException">NGit.Api.Errors.NoMessageException /// </exception> /// <exception cref="NGit.Api.Errors.RefNotFoundException">NGit.Api.Errors.RefNotFoundException /// </exception> /// <exception cref="NGit.Api.Errors.WrongRepositoryStateException">NGit.Api.Errors.WrongRepositoryStateException /// </exception> /// <exception cref="NGit.Api.Errors.GitAPIException">NGit.Api.Errors.GitAPIException /// </exception> public override ICollection <string> Call() { CheckCallable(); try { SubmoduleWalk generator = SubmoduleWalk.ForIndex(repo); if (!paths.IsEmpty()) { generator.SetFilter(PathFilterGroup.CreateFromStrings(paths)); } IList <string> updated = new AList <string>(); while (generator.Next()) { // Skip submodules not registered in .gitmodules file if (generator.GetModulesPath() == null) { continue; } // Skip submodules not registered in parent repository's config string url = generator.GetConfigUrl(); if (url == null) { continue; } Repository submoduleRepo = generator.GetRepository(); // Clone repository is not present if (submoduleRepo == null) { CloneCommand clone = Git.CloneRepository(); Configure(clone); clone.SetURI(url); clone.SetDirectory(generator.GetDirectory()); if (monitor != null) { clone.SetProgressMonitor(monitor); } submoduleRepo = clone.Call().GetRepository(); } try { RevWalk walk = new RevWalk(submoduleRepo); RevCommit commit = walk.ParseCommit(generator.GetObjectId()); string update = generator.GetConfigUpdate(); if (ConfigConstants.CONFIG_KEY_MERGE.Equals(update)) { MergeCommand merge = new MergeCommand(submoduleRepo); merge.Include(commit); merge.Call(); } else { if (ConfigConstants.CONFIG_KEY_REBASE.Equals(update)) { RebaseCommand rebase = new RebaseCommand(submoduleRepo); rebase.SetUpstream(commit); rebase.Call(); } else { // Checkout commit referenced in parent repository's // index as a detached HEAD DirCacheCheckout co = new DirCacheCheckout(submoduleRepo, submoduleRepo.LockDirCache (), commit.Tree); co.SetFailOnConflict(true); co.Checkout(); RefUpdate refUpdate = submoduleRepo.UpdateRef(Constants.HEAD, true); refUpdate.SetNewObjectId(commit); refUpdate.ForceUpdate(); } } } finally { submoduleRepo.Close(); } updated.AddItem(generator.GetPath()); } return(updated); } catch (IOException e) { throw new JGitInternalException(e.Message, e); } catch (ConfigInvalidException e) { throw new InvalidConfigurationException(e.Message, e); } }