/// <exception cref="System.IO.IOException"></exception> private void Commit(string commitMsg, PersonIdent author, PersonIdent committer) { NGit.CommitBuilder commit = new NGit.CommitBuilder(); commit.Author = author; commit.Committer = committer; commit.Message = commitMsg; ObjectInserter inserter = db.NewObjectInserter(); ObjectId id; try { commit.TreeId = inserter.Insert(new TreeFormatter()); id = inserter.Insert(commit); inserter.Flush(); } finally { inserter.Release(); } int nl = commitMsg.IndexOf('\n'); RefUpdate ru = db.UpdateRef(Constants.HEAD); ru.SetNewObjectId(id); ru.SetRefLogMessage("commit : " + ((nl == -1) ? commitMsg : Sharpen.Runtime.Substring (commitMsg, 0, nl)), false); ru.ForceUpdate(); }
public virtual void TestReadAllIncludingSymrefs() { ObjectId masterId = db.Resolve("refs/heads/master"); RefUpdate updateRef = db.UpdateRef("refs/remotes/origin/master"); updateRef.SetNewObjectId(masterId); updateRef.SetForceUpdate(true); updateRef.Update(); WriteSymref("refs/remotes/origin/HEAD", "refs/remotes/origin/master"); ObjectId r = db.Resolve("refs/remotes/origin/HEAD"); NUnit.Framework.Assert.AreEqual(masterId, r); IDictionary <string, Ref> allRefs = db.GetAllRefs(); Ref refHEAD = allRefs.Get("refs/remotes/origin/HEAD"); NUnit.Framework.Assert.IsNotNull(refHEAD); NUnit.Framework.Assert.AreEqual(masterId, refHEAD.GetObjectId()); NUnit.Framework.Assert.IsFalse(refHEAD.IsPeeled()); NUnit.Framework.Assert.IsNull(refHEAD.GetPeeledObjectId()); Ref refmaster = allRefs.Get("refs/remotes/origin/master"); NUnit.Framework.Assert.AreEqual(masterId, refmaster.GetObjectId()); NUnit.Framework.Assert.IsFalse(refmaster.IsPeeled()); NUnit.Framework.Assert.IsNull(refmaster.GetPeeledObjectId()); }
/// <param name="message"></param> /// <param name="ref"></param> /// <param name="rc"></param> public ConcurrentRefUpdateException(string message, Ref @ref, RefUpdate.Result rc ) : base((rc == null) ? message : message + ". " + MessageFormat.Format(JGitText .Get().refUpdateReturnCodeWas, rc)) { this.rc = rc; this.@ref = @ref; }
public virtual void ResolveUpstream() { Git git = new Git(db); WriteTrashFile("file.txt", "content"); git.Add().AddFilepattern("file.txt").Call(); RevCommit c1 = git.Commit().SetMessage("create file").Call(); WriteTrashFile("file2.txt", "content"); RefUpdate updateRemoteRef = db.UpdateRef("refs/remotes/origin/main"); updateRemoteRef.SetNewObjectId(c1); updateRemoteRef.Update(); ((FileBasedConfig)db.GetConfig()).SetString("branch", "master", "remote", "origin" ); ((FileBasedConfig)db.GetConfig()).SetString("branch", "master", "merge", "refs/heads/main" ); ((FileBasedConfig)db.GetConfig()).SetString("remote", "origin", "url", "git://example.com/here" ); ((FileBasedConfig)db.GetConfig()).SetString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*" ); git.Add().AddFilepattern("file2.txt").Call(); git.Commit().SetMessage("create file").Call(); NUnit.Framework.Assert.AreEqual("refs/remotes/origin/main", db.Simplify("@{upstream}" )); }
/// <exception cref="System.IO.IOException"></exception> protected internal virtual void CreateBranch(ObjectId objectId, string branchName ) { RefUpdate updateRef = db.UpdateRef(branchName); updateRef.SetNewObjectId(objectId); updateRef.Update(); }
/// <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); }
public virtual void TestReadLooseRef() { RefUpdate updateRef = db.UpdateRef("ref/heads/new"); updateRef.SetNewObjectId(db.Resolve("refs/heads/master")); RefUpdate.Result update = updateRef.Update(); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, update); Ref @ref = db.GetRef("ref/heads/new"); NUnit.Framework.Assert.AreEqual(RefStorage.LOOSE, @ref.GetStorage()); }
/// <summary>Initialize a new rename operation.</summary> /// <remarks>Initialize a new rename operation.</remarks> /// <param name="src">operation to read and delete the source.</param> /// <param name="dst">operation to create (or overwrite) the destination.</param> protected internal RefRename(RefUpdate src, RefUpdate dst) { source = src; destination = dst; string cmd = string.Empty; if (source.GetName().StartsWith(Constants.R_HEADS) && destination.GetName().StartsWith (Constants.R_HEADS)) { cmd = "Branch: "; } SetRefLogMessage(cmd + "renamed " + Repository.ShortenRefName(source.GetName()) + " to " + Repository.ShortenRefName(destination.GetName())); }
public virtual void TestReadSimplePackedRefSameRepo() { Ref @ref = db.GetRef("refs/heads/master"); ObjectId pid = db.Resolve("refs/heads/master^"); NUnit.Framework.Assert.AreEqual(RefStorage.PACKED, @ref.GetStorage()); RefUpdate updateRef = db.UpdateRef("refs/heads/master"); updateRef.SetNewObjectId(pid); updateRef.SetForceUpdate(true); RefUpdate.Result update = updateRef.Update(); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, update); @ref = db.GetRef("refs/heads/master"); NUnit.Framework.Assert.AreEqual(RefStorage.LOOSE, @ref.GetStorage()); }
public virtual void TestParseNonGitDescribe() { ObjectId id = Id("49322bb17d3acc9146f98c97d078513228bbf3c0"); RefUpdate ru = db.UpdateRef("refs/heads/foo-g032c"); ru.SetNewObjectId(id); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, ru.Update()); NUnit.Framework.Assert.AreEqual(id, db.Resolve("refs/heads/foo-g032c")); NUnit.Framework.Assert.AreEqual(id, db.Resolve("foo-g032c")); ru = db.UpdateRef("refs/heads/foo-g032c-dev"); ru.SetNewObjectId(id); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, ru.Update()); NUnit.Framework.Assert.AreEqual(id, db.Resolve("refs/heads/foo-g032c-dev")); NUnit.Framework.Assert.AreEqual(id, db.Resolve("foo-g032c-dev")); }
/// <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); }
/// <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 virtual void TestReadSymRefToLoosePacked() { ObjectId pid = db.Resolve("refs/heads/master^"); RefUpdate updateRef = db.UpdateRef("refs/heads/master"); updateRef.SetNewObjectId(pid); updateRef.SetForceUpdate(true); RefUpdate.Result update = updateRef.Update(); NUnit.Framework.Assert.AreEqual(RefUpdate.Result.FORCED, update); // internal WriteSymref("HEAD", "refs/heads/master"); Ref @ref = db.GetRef("HEAD"); NUnit.Framework.Assert.AreEqual(RefStorage.LOOSE, @ref.GetStorage()); @ref = @ref.GetTarget(); NUnit.Framework.Assert.AreEqual("refs/heads/master", @ref.GetName()); NUnit.Framework.Assert.AreEqual(RefStorage.LOOSE, @ref.GetStorage()); }
/// <exception cref="System.IO.IOException"></exception> private void WriteSymref(string src, string dst) { RefUpdate u = db.UpdateRef(src); switch (u.Link(dst)) { case RefUpdate.Result.NEW: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { break; } default: { NUnit.Framework.Assert.Fail("link " + src + " to " + dst); break; } } }
/// <summary>Execute this batch update.</summary> /// <remarks> /// Execute this batch update. /// <p> /// The default implementation of this method performs a sequential reference /// update over each reference. /// </remarks> /// <param name="walk"> /// a RevWalk to parse tags in case the storage system wants to /// store them pre-peeled, a common performance optimization. /// </param> /// <param name="update">progress monitor to receive update status on.</param> /// <exception cref="System.IO.IOException"> /// the database is unable to accept the update. Individual /// command status must be tested to determine if there is a /// partial failure, or a total failure. /// </exception> public virtual void Execute(RevWalk walk, ProgressMonitor update) { update.BeginTask(JGitText.Get().updatingReferences, commands.Count); foreach (ReceiveCommand cmd in commands) { try { update.Update(1); if (cmd.GetResult() == ReceiveCommand.Result.NOT_ATTEMPTED) { cmd.UpdateType(walk); RefUpdate ru = NewUpdate(cmd); switch (cmd.GetType()) { case ReceiveCommand.Type.DELETE: { cmd.SetResult(ru.Delete(walk)); continue; goto case ReceiveCommand.Type.CREATE; } case ReceiveCommand.Type.CREATE: case ReceiveCommand.Type.UPDATE: case ReceiveCommand.Type.UPDATE_NONFASTFORWARD: { cmd.SetResult(ru.Update(walk)); continue; } } } } catch (IOException err) { cmd.SetResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, MessageFormat.Format(JGitText .Get().lockError, err.Message)); } } update.EndTask(); }
/// <summary>Create a new RefUpdate copying the batch settings.</summary> /// <remarks>Create a new RefUpdate copying the batch settings.</remarks> /// <param name="cmd">specific command the update should be created to copy.</param> /// <returns>a single reference update command.</returns> /// <exception cref="System.IO.IOException"> /// the reference database cannot make a new update object for /// the given reference. /// </exception> protected internal virtual RefUpdate NewUpdate(ReceiveCommand cmd) { RefUpdate ru = refdb.NewUpdate(cmd.GetRefName(), false); if (IsRefLogDisabled()) { ru.DisableRefLog(); } else { ru.SetRefLogIdent(refLogIdent); ru.SetRefLogMessage(refLogMessage, refLogIncludeResult); } switch (cmd.GetType()) { case ReceiveCommand.Type.DELETE: { if (!ObjectId.ZeroId.Equals(cmd.GetOldId())) { ru.SetExpectedOldObjectId(cmd.GetOldId()); } ru.SetForceUpdate(true); return(ru); } case ReceiveCommand.Type.CREATE: case ReceiveCommand.Type.UPDATE: case ReceiveCommand.Type.UPDATE_NONFASTFORWARD: default: { ru.SetForceUpdate(IsAllowNonFastForwards()); ru.SetExpectedOldObjectId(cmd.GetOldId()); ru.SetNewObjectId(cmd.GetNewId()); return(ru); break; } } }
/// <summary>Write the given ref update to the ref's log</summary> /// <param name="update"></param> /// <param name="msg"></param> /// <param name="deref"></param> /// <returns>this writer</returns> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> public virtual NGit.Storage.File.ReflogWriter Log(RefUpdate update, string msg, bool deref) { ObjectId oldId = update.GetOldObjectId(); ObjectId newId = update.GetNewObjectId(); Ref @ref = update.GetRef(); PersonIdent ident = update.GetRefLogIdent(); if (ident == null) { ident = new PersonIdent(parent); } else { ident = new PersonIdent(ident); } byte[] rec = Encode(oldId, newId, ident, msg); if (deref && @ref.IsSymbolic()) { Log(@ref.GetName(), rec); Log(@ref.GetLeaf().GetName(), rec); } else { Log(@ref.GetName(), rec); } return this; }
internal Store(RefUpdate _enclosing) { this._enclosing = _enclosing; }
/// <exception cref="System.IO.IOException"></exception> internal abstract RefUpdate.Result Execute(RefUpdate.Result status);
/// <exception cref="System.IO.IOException"></exception> private RefUpdate.Result UpdateImpl(RevWalk walk, RefUpdate.Store store) { RevObject newObj; RevObject oldObj; if (GetRefDatabase().IsNameConflicting(GetName())) { return RefUpdate.Result.LOCK_FAILURE; } try { if (!TryLock(true)) { return RefUpdate.Result.LOCK_FAILURE; } if (expValue != null) { ObjectId o; o = oldValue != null ? oldValue : ObjectId.ZeroId; if (!AnyObjectId.Equals(expValue, o)) { return RefUpdate.Result.LOCK_FAILURE; } } if (oldValue == null) { return store.Execute(RefUpdate.Result.NEW); } newObj = SafeParse(walk, newValue); oldObj = SafeParse(walk, oldValue); if (newObj == oldObj && !detachingSymbolicRef) { return store.Execute(RefUpdate.Result.NO_CHANGE); } if (newObj is RevCommit && oldObj is RevCommit) { if (walk.IsMergedInto((RevCommit)oldObj, (RevCommit)newObj)) { return store.Execute(RefUpdate.Result.FAST_FORWARD); } } if (IsForceUpdate()) { return store.Execute(RefUpdate.Result.FORCED); } return RefUpdate.Result.REJECTED; } finally { Unlock(); } }
/// <exception cref="System.IO.IOException"></exception> internal override RefUpdate.Result Execute(RefUpdate.Result status) { return this._enclosing.DoDelete(status); }
public _Store_540(RefUpdate _enclosing) : base(_enclosing) { this._enclosing = _enclosing; }
/// <exception cref="System.IO.IOException"></exception> protected internal override RefUpdate.Result DoUpdate(RefUpdate.Result status) { WriteConfig wc = database.GetRepository().GetConfig().Get(WriteConfig.KEY); Lock.SetFSync(wc.GetFSyncRefFiles()); Lock.SetNeedStatInformation(true); Lock.Write(GetNewObjectId()); string msg = GetRefLogMessage(); if (msg != null) { if (IsRefLogIncludingResult()) { string strResult = ToResultString(status); if (strResult != null) { if (msg.Length > 0) { msg = msg + ": " + strResult; } else { msg = strResult; } } } database.Log(this, msg, true); } if (!Lock.Commit()) { return RefUpdate.Result.LOCK_FAILURE; } database.Stored(this, Lock.GetCommitSnapshot()); return status; }
private bool LinkHEAD(RefUpdate target) { try { RefUpdate u = ((RefDirectoryUpdate)refdb.NewUpdate(Constants.HEAD, false)); u.DisableRefLog(); switch (u.Link(target.GetName())) { case RefUpdate.Result.NEW: case RefUpdate.Result.FORCED: case RefUpdate.Result.NO_CHANGE: { return true; } default: { return false; break; } } } catch (IOException) { return false; } }
internal virtual void SetResult(RefUpdate.Result result) { this.result = result; }
private bool RenameLog(RefUpdate src, RefUpdate dst) { FilePath srcLog = refdb.GetLogWriter().LogFor(src.GetName()); FilePath dstLog = refdb.GetLogWriter().LogFor(dst.GetName()); if (!srcLog.Exists()) { return true; } if (!Rename(srcLog, dstLog)) { return false; } try { int levels = RefDirectory.LevelsIn(src.GetName()) - 2; RefDirectory.Delete(srcLog, levels); return true; } catch (IOException) { Rename(dstLog, srcLog); return false; } }
/// <exception cref="System.IO.IOException"></exception> private void Delete(RefUpdate @ref, RefUpdate.Result expected) { Delete(@ref, expected, true, true); }
private string ToResultString(RefUpdate.Result status) { switch (status) { case RefUpdate.Result.FORCED: { return "forced-update"; } case RefUpdate.Result.FAST_FORWARD: { return "fast forward"; } case RefUpdate.Result.NEW: { return "created"; } default: { return null; break; } } }
public override void SetResult(RefUpdate.Result status) { this._enclosing.result = status; base.SetResult(status); }
/// <param name="desiredResult"></param> /// <returns> /// /// <code>result</code> /// </returns> /// <exception cref="System.IO.IOException">System.IO.IOException</exception> protected internal abstract RefUpdate.Result DoDelete(RefUpdate.Result desiredResult );
/// <exception cref="System.IO.IOException"></exception> protected internal override RefUpdate.Result DoDelete(RefUpdate.Result status) { if (GetRef().GetLeaf().GetStorage() != RefStorage.NEW) { database.Delete(this); } return status; }
/// <exception cref="System.IO.IOException"></exception> internal override RefUpdate.Result Execute(RefUpdate.Result status) { if (status == RefUpdate.Result.NO_CHANGE) { return status; } return this._enclosing.DoUpdate(status); }
/// <exception cref="System.IO.IOException"></exception> private void Delete(RefUpdate @ref, RefUpdate.Result expected, bool exists, bool removed) { NUnit.Framework.Assert.AreEqual(exists, db.GetAllRefs().ContainsKey(@ref.GetName())); NUnit.Framework.Assert.AreEqual(expected, @ref.Delete()); NUnit.Framework.Assert.AreEqual(!removed, db.GetAllRefs().ContainsKey(@ref.GetName())); }
/// <summary>Set the result of this command.</summary> /// <remarks>Set the result of this command.</remarks> /// <param name="r">the new result code for this command.</param> public virtual void SetResult(RefUpdate.Result r) { switch (r) { case RefUpdate.Result.NOT_ATTEMPTED: { SetResult(ReceiveCommand.Result.NOT_ATTEMPTED); break; } case RefUpdate.Result.LOCK_FAILURE: case RefUpdate.Result.IO_FAILURE: { SetResult(ReceiveCommand.Result.LOCK_FAILURE); break; } case RefUpdate.Result.NO_CHANGE: case RefUpdate.Result.NEW: case RefUpdate.Result.FORCED: case RefUpdate.Result.FAST_FORWARD: { SetResult(ReceiveCommand.Result.OK); break; } case RefUpdate.Result.REJECTED: { SetResult(ReceiveCommand.Result.REJECTED_NONFASTFORWARD); break; } case RefUpdate.Result.REJECTED_CURRENT_BRANCH: { SetResult(ReceiveCommand.Result.REJECTED_CURRENT_BRANCH); break; } default: { SetResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, r.ToString()); break; break; } } }
/// <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; }