public void testRenameRefNameColission1avoided() { // setup ObjectId rb = db.Resolve("refs/heads/b"); db.WriteSymref(Constants.HEAD, "refs/heads/a"); RefUpdate updateRef = db.UpdateRef("refs/heads/a"); updateRef.NewObjectId = rb; updateRef.SetRefLogMessage("Setup", false); Assert.AreEqual(RefUpdate.RefUpdateResult.FastForward, updateRef.Update()); ObjectId oldHead = db.Resolve(Constants.HEAD); Assert.IsTrue(rb.Equals(oldHead)); // assumption for this test RefLogWriter.WriteReflog(db, rb, rb, "Just a message", "refs/heads/a"); Assert.IsTrue(new FileInfo(Path.Combine(db.Directory.FullName, "logs/refs/heads/a")).Exists, "internal check, we have a log"); // Now this is our test RefRename renameRef = db.RenameRef("refs/heads/a", "refs/heads/a/b"); RefUpdate.RefUpdateResult result = renameRef.Rename(); Assert.AreEqual(RefUpdate.RefUpdateResult.Renamed, result); Assert.IsNull(db.Resolve("refs/heads/a")); Assert.AreEqual(rb, db.Resolve("refs/heads/a/b")); Assert.AreEqual(3, db.ReflogReader("a/b").getReverseEntries().Count); Assert.AreEqual("Branch: renamed a to a/b", db.ReflogReader("a/b") .getReverseEntries()[0].getComment()); Assert.AreEqual("Just a message", db.ReflogReader("a/b") .getReverseEntries()[1].getComment()); Assert.AreEqual("Setup", db.ReflogReader("a/b").getReverseEntries() [2].getComment()); // same thing was logged to HEAD Assert.AreEqual("Branch: renamed a to a/b", db.ReflogReader("HEAD") .getReverseEntries()[0].getComment()); }
private void Execute(ReceiveCommand cmd) { try { RefUpdate ru = db.UpdateRef(cmd.getRefName()); ru.RefLogIdent = getRefLogIdent(); switch (cmd.getType()) { case ReceiveCommand.Type.DELETE: if (!ObjectId.ZeroId.Equals(cmd.getOldId())) { ru.ExpectedOldObjectId = cmd.getOldId(); } ru.IsForceUpdate = true; Status(cmd, ru.Delete(walk)); break; case ReceiveCommand.Type.CREATE: case ReceiveCommand.Type.UPDATE: case ReceiveCommand.Type.UPDATE_NONFASTFORWARD: ru.IsForceUpdate = isAllowNonFastForwards(); ru.ExpectedOldObjectId = cmd.getOldId(); ru.NewObjectId = cmd.getNewId(); ru.SetRefLogMessage("push", true); Status(cmd, ru.Update(walk)); break; } } catch (IOException err) { cmd.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, "lock error: " + err.Message); } }
public virtual void TestDeleteLooseAndItsDirectory() { ObjectId pid = db.Resolve("refs/heads/c^"); RefUpdate updateRef = db.UpdateRef("refs/heads/z/c"); updateRef.SetNewObjectId(pid); updateRef.SetForceUpdate(true); updateRef.SetRefLogMessage("new test ref", false); RefUpdate.Result update = updateRef.Update(); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, update); // internal NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, Constants.R_HEADS + "z") .Exists()); NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "logs/refs/heads/z").Exists ()); // The real test here RefUpdate updateRef2 = db.UpdateRef("refs/heads/z/c"); updateRef2.SetForceUpdate(true); RefUpdate.Result delete = updateRef2.Delete(); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, delete); NUnit.Framework.Assert.IsNull(db.Resolve("refs/heads/z/c")); NUnit.Framework.Assert.IsFalse(new FilePath(db.Directory, Constants.R_HEADS + "z" ).Exists()); NUnit.Framework.Assert.IsFalse(new FilePath(db.Directory, "logs/refs/heads/z").Exists ()); }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Api.Errors.ConcurrentRefUpdateException"></exception> private void UpdateHead(StringBuilder refLogMessage, ObjectId newHeadId, ObjectId oldHeadID) { RefUpdate refUpdate = repo.UpdateRef(Constants.HEAD); refUpdate.SetNewObjectId(newHeadId); refUpdate.SetRefLogMessage(refLogMessage.ToString(), false); refUpdate.SetExpectedOldObjectId(oldHeadID); RefUpdate.Result rc = refUpdate.Update(); switch (rc) { case RefUpdate.Result.NEW: case RefUpdate.Result.FAST_FORWARD: { return; } case RefUpdate.Result.REJECTED: case RefUpdate.Result.LOCK_FAILURE: { throw new ConcurrentRefUpdateException(JGitText.Get().couldNotLockHEAD, refUpdate .GetRef(), rc); } default: { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().updatingRefFailed , Constants.HEAD, newHeadId.ToString(), rc)); } } }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Api.Errors.JGitInternalException"></exception> private RevCommit TryFastForward(string headName, RevCommit oldCommit, RevCommit newCommit) { bool tryRebase = false; foreach (RevCommit parentCommit in newCommit.Parents) { if (parentCommit.Equals(oldCommit)) { tryRebase = true; } } if (!tryRebase) { return(null); } CheckoutCommand co = new CheckoutCommand(repo); try { co.SetName(newCommit.Name).Call(); if (headName.StartsWith(Constants.R_HEADS)) { RefUpdate rup = repo.UpdateRef(headName); rup.SetExpectedOldObjectId(oldCommit); rup.SetNewObjectId(newCommit); rup.SetRefLogMessage("Fast-foward from " + oldCommit.Name + " to " + newCommit.Name , false); RefUpdate.Result res = rup.Update(walk); switch (res) { case RefUpdate.Result.FAST_FORWARD: case RefUpdate.Result.NO_CHANGE: case RefUpdate.Result.FORCED: { break; } default: { throw new IOException("Could not fast-forward"); } } } return(newCommit); } catch (RefAlreadyExistsException e) { throw new JGitInternalException(e.Message, e); } catch (RefNotFoundException e) { throw new JGitInternalException(e.Message, e); } catch (InvalidRefNameException e) { throw new JGitInternalException(e.Message, e); } }
public TrackingRefUpdate(Repository db, string localName, string remoteName, bool forceUpdate, AnyObjectId nv, string msg) { RemoteName = remoteName; update = db.UpdateRef(localName); update.IsForceUpdate = forceUpdate; update.NewObjectId = nv.Copy(); update.SetRefLogMessage(msg, true); }
/// <exception cref="System.IO.IOException"></exception> internal TrackingRefUpdate(Repository db, string localName, string remoteName, bool forceUpdate, AnyObjectId nv, string msg) { this.remoteName = remoteName; update = db.UpdateRef(localName); update.SetForceUpdate(forceUpdate); update.SetNewObjectId(nv); update.SetRefLogMessage(msg, true); }
/// <summary>Construct remote ref update request by providing an update specification. /// </summary> /// <remarks> /// Construct remote ref update request by providing an update specification. /// Object is created with default /// <see cref="Status.NOT_ATTEMPTED">Status.NOT_ATTEMPTED</see> /// status and no /// message. /// </remarks> /// <param name="localDb">local repository to push from.</param> /// <param name="srcRef"> /// source revision to label srcId with. If null srcId.name() will /// be used instead. /// </param> /// <param name="srcId"> /// The new object that the caller wants remote ref to be after /// update. Use null or /// <see cref="NGit.ObjectId.ZeroId()">NGit.ObjectId.ZeroId()</see> /// for delete /// request. /// </param> /// <param name="remoteName"> /// full name of a remote ref to update, e.g. "refs/heads/master" /// (no wildcard, no short name). /// </param> /// <param name="forceUpdate"> /// true when caller want remote ref to be updated regardless /// whether it is fast-forward update (old object is ancestor of /// new object). /// </param> /// <param name="localName"> /// optional full name of a local stored tracking branch, to /// update after push, e.g. "refs/remotes/zawir/dirty" (no /// wildcard, no short name); null if no local tracking branch /// should be updated. /// </param> /// <param name="expectedOldObjectId"> /// optional object id that caller is expecting, requiring to be /// advertised by remote side before update; update will take /// place ONLY if remote side advertise exactly this expected id; /// null if caller doesn't care what object id remote side /// advertise. Use /// <see cref="NGit.ObjectId.ZeroId()">NGit.ObjectId.ZeroId()</see> /// when expecting no /// remote ref with this name. /// </param> /// <exception cref="System.IO.IOException"> /// when I/O error occurred during creating /// <see cref="TrackingRefUpdate">TrackingRefUpdate</see> /// for local tracking branch or srcRef /// can't be resolved to any object. /// </exception> /// <exception cref="System.ArgumentException">if some required parameter was null</exception> public RemoteRefUpdate(Repository localDb, string srcRef, ObjectId srcId, string remoteName, bool forceUpdate, string localName, ObjectId expectedOldObjectId) { if (remoteName == null) { throw new ArgumentException(JGitText.Get().remoteNameCantBeNull); } if (srcId == null && srcRef != null) { throw new IOException(MessageFormat.Format(JGitText.Get().sourceRefDoesntResolveToAnyObject , srcRef)); } if (srcRef != null) { this.srcRef = srcRef; } else { if (srcId != null && !srcId.Equals(ObjectId.ZeroId)) { this.srcRef = srcId.Name; } else { this.srcRef = null; } } if (srcId != null) { this.newObjectId = srcId; } else { this.newObjectId = ObjectId.ZeroId; } this.remoteName = remoteName; this.forceUpdate = forceUpdate; if (localName != null && localDb != null) { localUpdate = localDb.UpdateRef(localName); localUpdate.SetForceUpdate(true); localUpdate.SetRefLogMessage("push", true); localUpdate.SetNewObjectId(newObjectId); trackingRefUpdate = new TrackingRefUpdate(true, remoteName, localName, localUpdate .GetOldObjectId() != null ? localUpdate.GetOldObjectId() : ObjectId.ZeroId, newObjectId ); } else { trackingRefUpdate = null; } this.localDb = localDb; this.expectedOldObjectId = expectedOldObjectId; this.status = RemoteRefUpdate.Status.NOT_ATTEMPTED; }
public TrackingRefUpdate(Repository db, string localName, string remoteName, bool forceUpdate, AnyObjectId nv, string msg) { if (db == null) throw new System.ArgumentNullException ("db"); if (nv == null) throw new System.ArgumentNullException ("nv"); RemoteName = remoteName; update = db.UpdateRef(localName); update.IsForceUpdate = forceUpdate; update.NewObjectId = nv.Copy(); update.SetRefLogMessage(msg, true); }
/// <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(); }
/// <summary>Execute this command during a receive-pack session.</summary> /// <remarks> /// Execute this command during a receive-pack session. /// <p> /// Sets the status of the command as a side effect. /// </remarks> /// <param name="rp">receive-pack session.</param> /// <since>2.0</since> public virtual void Execute(BaseReceivePack rp) { try { RefUpdate ru = rp.GetRepository().UpdateRef(GetRefName()); ru.SetRefLogIdent(rp.GetRefLogIdent()); switch (GetType()) { case ReceiveCommand.Type.DELETE: { if (!ObjectId.ZeroId.Equals(GetOldId())) { // We can only do a CAS style delete if the client // didn't bork its delete request by sending the // wrong zero id rather than the advertised one. // ru.SetExpectedOldObjectId(GetOldId()); } ru.SetForceUpdate(true); SetResult(ru.Delete(rp.GetRevWalk())); break; } case ReceiveCommand.Type.CREATE: case ReceiveCommand.Type.UPDATE: case ReceiveCommand.Type.UPDATE_NONFASTFORWARD: { ru.SetForceUpdate(rp.IsAllowNonFastForwards()); ru.SetExpectedOldObjectId(GetOldId()); ru.SetNewObjectId(GetNewId()); ru.SetRefLogMessage("push", true); SetResult(ru.Update(rp.GetRevWalk())); break; } } } catch (IOException err) { Reject(err); } }
public virtual void TestRenameRefNameColission1avoided() { // setup ObjectId rb = db.Resolve("refs/heads/b"); WriteSymref(Constants.HEAD, "refs/heads/a"); RefUpdate updateRef = db.UpdateRef("refs/heads/a"); updateRef.SetNewObjectId(rb); updateRef.SetRefLogMessage("Setup", false); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FAST_FORWARD, updateRef.Update() ); ObjectId oldHead = db.Resolve(Constants.HEAD); NUnit.Framework.Assert.AreEqual(oldHead, rb); // assumption for this test WriteReflog(db, rb, "Just a message", "refs/heads/a"); NUnit.Framework.Assert.IsTrue(new FilePath(db.Directory, "logs/refs/heads/a").Exists (), "internal check, we have a log"); // Now this is our test RefRename renameRef = db.RenameRef("refs/heads/a", "refs/heads/a/b"); RefUpdate.Result result = renameRef.Rename(); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.RENAMED, result); NUnit.Framework.Assert.IsNull(db.Resolve("refs/heads/a")); NUnit.Framework.Assert.AreEqual(rb, db.Resolve("refs/heads/a/b")); NUnit.Framework.Assert.AreEqual(3, db.GetReflogReader("a/b").GetReverseEntries(). Count); NUnit.Framework.Assert.AreEqual("Branch: renamed a to a/b", db.GetReflogReader("a/b" ).GetReverseEntries()[0].GetComment()); NUnit.Framework.Assert.AreEqual("Just a message", db.GetReflogReader("a/b").GetReverseEntries ()[1].GetComment()); NUnit.Framework.Assert.AreEqual("Setup", db.GetReflogReader("a/b").GetReverseEntries ()[2].GetComment()); // same thing was logged to HEAD NUnit.Framework.Assert.AreEqual("Branch: renamed a to a/b", db.GetReflogReader("HEAD" ).GetReverseEntries()[0].GetComment()); }
public void testDeleteLooseAndItsDirectory() { ObjectId pid = db.Resolve("refs/heads/c^"); RefUpdate updateRef = db.UpdateRef("refs/heads/z/c"); updateRef.NewObjectId = pid; updateRef.IsForceUpdate = true; updateRef.SetRefLogMessage("new test ref", false); RefUpdate.RefUpdateResult update = updateRef.Update(); Assert.AreEqual(RefUpdate.RefUpdateResult.New, update); // internal Assert.IsTrue(new DirectoryInfo(Path.Combine(db.Directory.FullName, Constants.R_HEADS + "z")).Exists); Assert.IsTrue(new DirectoryInfo(Path.Combine(db.Directory.FullName, "logs/refs/heads/z")).Exists); // The real test here RefUpdate updateRef2 = db.UpdateRef("refs/heads/z/c"); updateRef2.IsForceUpdate = true; RefUpdate.RefUpdateResult delete = updateRef2.Delete(); Assert.AreEqual(RefUpdate.RefUpdateResult.Forced, delete); Assert.IsNull(db.Resolve("refs/heads/z/c")); Assert.IsFalse(new DirectoryInfo(Path.Combine(db.Directory.FullName, Constants.R_HEADS + "z")).Exists); Assert.IsFalse(new DirectoryInfo(Path.Combine(db.Directory.FullName, "logs/refs/heads/z")).Exists); }
/// <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); }
/// <exception cref="NGit.Api.Errors.NotMergedException"> /// when trying to delete a branch which has not been merged into /// the currently checked out branch without force /// </exception> /// <exception cref="NGit.Api.Errors.CannotDeleteCurrentBranchException">NGit.Api.Errors.CannotDeleteCurrentBranchException /// </exception> /// <returns>the list with the (full) names of the deleted branches</returns> /// <exception cref="NGit.Api.Errors.GitAPIException"></exception> public override IList <string> Call() { CheckCallable(); IList <string> result = new AList <string>(); if (branchNames.IsEmpty()) { return(result); } try { string currentBranch = repo.GetFullBranch(); if (!force) { // check if the branches to be deleted // are all merged into the current branch RevWalk walk = new RevWalk(repo); RevCommit tip = walk.ParseCommit(repo.Resolve(Constants.HEAD)); foreach (string branchName in branchNames) { if (branchName == null) { continue; } Ref currentRef = repo.GetRef(branchName); if (currentRef == null) { continue; } RevCommit @base = walk.ParseCommit(repo.Resolve(branchName)); if (!walk.IsMergedInto(@base, tip)) { throw new NotMergedException(); } } } SetCallable(false); foreach (string branchName_1 in branchNames) { if (branchName_1 == null) { continue; } Ref currentRef = repo.GetRef(branchName_1); if (currentRef == null) { continue; } string fullName = currentRef.GetName(); if (fullName.Equals(currentBranch)) { throw new CannotDeleteCurrentBranchException(MessageFormat.Format(JGitText.Get(). cannotDeleteCheckedOutBranch, branchName_1)); } RefUpdate update = repo.UpdateRef(fullName); update.SetRefLogMessage("branch deleted", false); update.SetForceUpdate(true); RefUpdate.Result deleteResult = update.Delete(); bool ok = true; switch (deleteResult) { case RefUpdate.Result.IO_FAILURE: case RefUpdate.Result.LOCK_FAILURE: case RefUpdate.Result.REJECTED: { ok = false; break; } default: { break; break; } } if (ok) { result.AddItem(fullName); if (fullName.StartsWith(Constants.R_HEADS)) { string shortenedName = Sharpen.Runtime.Substring(fullName, Constants.R_HEADS.Length ); // remove upstream configuration if any StoredConfig cfg = repo.GetConfig(); cfg.UnsetSection(ConfigConstants.CONFIG_BRANCH_SECTION, shortenedName); cfg.Save(); } } else { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().deleteBranchUnexpectedResult , deleteResult.ToString())); } } return(result); } 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>Merge</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 merge</returns> /// <exception cref="NGit.Api.Errors.NoHeadException"></exception> /// <exception cref="NGit.Api.Errors.ConcurrentRefUpdateException"></exception> /// <exception cref="NGit.Api.Errors.CheckoutConflictException"></exception> /// <exception cref="NGit.Api.Errors.InvalidMergeHeadsException"></exception> /// <exception cref="NGit.Api.Errors.WrongRepositoryStateException"></exception> /// <exception cref="NGit.Api.Errors.NoMessageException"></exception> public override MergeCommandResult Call() { CheckCallable(); if (commits.Count != 1) { throw new InvalidMergeHeadsException(commits.IsEmpty() ? JGitText.Get().noMergeHeadSpecified : MessageFormat.Format(JGitText.Get().mergeStrategyDoesNotSupportHeads, mergeStrategy .GetName(), Sharpen.Extensions.ValueOf(commits.Count))); } RevWalk revWalk = null; try { Ref head = repo.GetRef(Constants.HEAD); if (head == null) { throw new NoHeadException(JGitText.Get().commitOnRepoWithoutHEADCurrentlyNotSupported ); } StringBuilder refLogMessage = new StringBuilder("merge "); // Check for FAST_FORWARD, ALREADY_UP_TO_DATE revWalk = new RevWalk(repo); // we know for now there is only one commit Ref @ref = commits[0]; refLogMessage.Append(@ref.GetName()); // handle annotated tags ObjectId objectId = @ref.GetPeeledObjectId(); if (objectId == null) { objectId = @ref.GetObjectId(); } RevCommit srcCommit = revWalk.LookupCommit(objectId); ObjectId headId = head.GetObjectId(); if (headId == null) { revWalk.ParseHeaders(srcCommit); DirCacheCheckout dco = new DirCacheCheckout(repo, repo.LockDirCache(), srcCommit. Tree); dco.SetFailOnConflict(true); dco.Checkout(); RefUpdate refUpdate = repo.UpdateRef(head.GetTarget().GetName()); refUpdate.SetNewObjectId(objectId); refUpdate.SetExpectedOldObjectId(null); refUpdate.SetRefLogMessage("initial pull", false); if (refUpdate.Update() != RefUpdate.Result.NEW) { throw new NoHeadException(JGitText.Get().commitOnRepoWithoutHEADCurrentlyNotSupported ); } SetCallable(false); return(new MergeCommandResult(srcCommit, srcCommit, new ObjectId[] { null, srcCommit }, MergeStatus.FAST_FORWARD, mergeStrategy, null, null)); } RevCommit headCommit = revWalk.LookupCommit(headId); if (revWalk.IsMergedInto(srcCommit, headCommit)) { SetCallable(false); return(new MergeCommandResult(headCommit, srcCommit, new ObjectId[] { headCommit, srcCommit }, MergeStatus.ALREADY_UP_TO_DATE, mergeStrategy, null, null)); } else { if (revWalk.IsMergedInto(headCommit, srcCommit)) { // FAST_FORWARD detected: skip doing a real merge but only // update HEAD refLogMessage.Append(": " + MergeStatus.FAST_FORWARD); DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.Tree, repo.LockDirCache (), srcCommit.Tree); dco.SetFailOnConflict(true); dco.Checkout(); UpdateHead(refLogMessage, srcCommit, headId); SetCallable(false); return(new MergeCommandResult(srcCommit, srcCommit, new ObjectId[] { headCommit, srcCommit }, MergeStatus.FAST_FORWARD, mergeStrategy, null, null)); } else { repo.WriteMergeCommitMsg(new MergeMessageFormatter().Format(commits, head)); repo.WriteMergeHeads(Arrays.AsList(@ref.GetObjectId())); ThreeWayMerger merger = (ThreeWayMerger)mergeStrategy.NewMerger(repo); bool noProblems; IDictionary <string, MergeResult <NGit.Diff.Sequence> > lowLevelResults = null; IDictionary <string, ResolveMerger.MergeFailureReason> failingPaths = null; if (merger is ResolveMerger) { ResolveMerger resolveMerger = (ResolveMerger)merger; resolveMerger.SetCommitNames(new string[] { "BASE", "HEAD", @ref.GetName() }); resolveMerger.SetWorkingTreeIterator(new FileTreeIterator(repo)); noProblems = merger.Merge(headCommit, srcCommit); lowLevelResults = resolveMerger.GetMergeResults(); failingPaths = resolveMerger.GetFailingPaths(); } else { noProblems = merger.Merge(headCommit, srcCommit); } if (noProblems) { DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.Tree, repo.LockDirCache (), merger.GetResultTreeId()); dco.SetFailOnConflict(true); dco.Checkout(); RevCommit newHead = new Git(GetRepository()).Commit().Call(); return(new MergeCommandResult(newHead.Id, null, new ObjectId[] { headCommit.Id, srcCommit .Id }, MergeStatus.MERGED, mergeStrategy, null, null)); } else { if (failingPaths != null) { repo.WriteMergeCommitMsg(null); repo.WriteMergeHeads(null); return(new MergeCommandResult(null, merger.GetBaseCommit(0, 1), new ObjectId[] { headCommit.Id, srcCommit.Id }, MergeStatus.FAILED, mergeStrategy, lowLevelResults , null)); } else { return(new MergeCommandResult(null, merger.GetBaseCommit(0, 1), new ObjectId[] { headCommit.Id, srcCommit.Id }, MergeStatus.CONFLICTING, mergeStrategy, lowLevelResults , null)); } } } } } catch (IOException e) { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().exceptionCaughtDuringExecutionOfMergeCommand , e), e); } finally { if (revWalk != null) { revWalk.Release(); } } }
/// <exception cref="System.IO.IOException"></exception> protected internal override RefUpdate.Result DoRename() { if (source.GetRef().IsSymbolic()) { return(RefUpdate.Result.IO_FAILURE); } // not supported objId = source.GetOldObjectId(); updateHEAD = NeedToUpdateHEAD(); tmp = refdb.NewTemporaryUpdate(); RevWalk rw = new RevWalk(refdb.GetRepository()); try { // First backup the source so its never unreachable. tmp.SetNewObjectId(objId); tmp.SetForceUpdate(true); tmp.DisableRefLog(); switch (tmp.Update(rw)) { case RefUpdate.Result.NEW: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { return(tmp.GetResult()); break; } } // Save the source's log under the temporary name, we must do // this before we delete the source, otherwise we lose the log. if (!RenameLog(source, tmp)) { return(RefUpdate.Result.IO_FAILURE); } // If HEAD has to be updated, link it now to destination. // We have to link before we delete, otherwise the delete // fails because its the current branch. RefUpdate dst = destination; if (updateHEAD) { if (!LinkHEAD(destination)) { RenameLog(tmp, source); return(RefUpdate.Result.LOCK_FAILURE); } // Replace the update operation so HEAD will log the rename. dst = ((RefDirectoryUpdate)refdb.NewUpdate(Constants.HEAD, false)); dst.SetRefLogIdent(destination.GetRefLogIdent()); dst.SetRefLogMessage(destination.GetRefLogMessage(), false); } // Delete the source name so its path is free for replacement. source.SetExpectedOldObjectId(objId); source.SetForceUpdate(true); source.DisableRefLog(); if (source.Delete(rw) != RefUpdate.Result.FORCED) { RenameLog(tmp, source); if (updateHEAD) { LinkHEAD(source); } return(source.GetResult()); } // Move the log to the destination. if (!RenameLog(tmp, destination)) { RenameLog(tmp, source); source.SetExpectedOldObjectId(ObjectId.ZeroId); source.SetNewObjectId(objId); source.Update(rw); if (updateHEAD) { LinkHEAD(source); } return(RefUpdate.Result.IO_FAILURE); } // Create the destination, logging the rename during the creation. dst.SetExpectedOldObjectId(ObjectId.ZeroId); dst.SetNewObjectId(objId); if (dst.Update(rw) != RefUpdate.Result.NEW) { // If we didn't create the destination we have to undo // our work. Put the log back and restore source. if (RenameLog(destination, tmp)) { RenameLog(tmp, source); } source.SetExpectedOldObjectId(ObjectId.ZeroId); source.SetNewObjectId(objId); source.Update(rw); if (updateHEAD) { LinkHEAD(source); } return(dst.GetResult()); } return(RefUpdate.Result.RENAMED); } finally { // Always try to free the temporary name. try { refdb.Delete(tmp); } catch (IOException) { FileUtils.Delete(refdb.FileFor(tmp.GetName())); } rw.Release(); } }
/// <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); } }
/// <summary> /// Executes the /// <code>tag</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.Ref">NGit.Ref</see> /// a ref pointing to a tag /// </returns> /// <exception cref="NGit.Api.Errors.NoHeadException">when called on a git repo without a HEAD reference /// </exception> /// <since>2.0</since> /// <exception cref="NGit.Api.Errors.GitAPIException"></exception> /// <exception cref="NGit.Api.Errors.ConcurrentRefUpdateException"></exception> /// <exception cref="NGit.Api.Errors.InvalidTagNameException"></exception> public override Ref Call() { CheckCallable(); RepositoryState state = repo.GetRepositoryState(); ProcessOptions(state); try { // create the tag object TagBuilder newTag = new TagBuilder(); newTag.SetTag(name); newTag.SetMessage(message); newTag.SetTagger(tagger); // if no id is set, we should attempt to use HEAD if (id == null) { ObjectId objectId = repo.Resolve(Constants.HEAD + "^{commit}"); if (objectId == null) { throw new NoHeadException(JGitText.Get().tagOnRepoWithoutHEADCurrentlyNotSupported ); } newTag.SetObjectId(objectId, Constants.OBJ_COMMIT); } else { newTag.SetObjectId(id); } // write the tag object ObjectInserter inserter = repo.NewObjectInserter(); try { ObjectId tagId = inserter.Insert(newTag); inserter.Flush(); RevWalk revWalk = new RevWalk(repo); try { string refName = Constants.R_TAGS + newTag.GetTag(); RefUpdate tagRef = repo.UpdateRef(refName); tagRef.SetNewObjectId(tagId); tagRef.SetForceUpdate(forceUpdate); tagRef.SetRefLogMessage("tagged " + name, false); RefUpdate.Result updateResult = tagRef.Update(revWalk); switch (updateResult) { case RefUpdate.Result.NEW: case RefUpdate.Result.FORCED: { return(repo.GetRef(refName)); } case RefUpdate.Result.LOCK_FAILURE: { throw new ConcurrentRefUpdateException(JGitText.Get().couldNotLockHEAD, tagRef.GetRef (), updateResult); } default: { throw new JGitInternalException(MessageFormat.Format(JGitText.Get().updatingRefFailed , refName, newTag.ToString(), updateResult)); } } } finally { revWalk.Release(); } } finally { inserter.Release(); } } catch (IOException e) { throw new JGitInternalException(JGitText.Get().exceptionCaughtDuringExecutionOfTagCommand , e); } }