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.getName())) { RefUpdate u1 = repo.UpdateRef(Constants.HEAD); u1.disableRefLog(); u1.link(branch.getName()); } 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(); }
/// <summary> /// Delete the ref. /// </summary> /// <param name="walk"> /// a RevWalk instance this delete command can borrow to perform /// the merge test. The walk will be reset to perform the test. /// </param> /// <returns>the result status of the delete.</returns> public RefUpdateResult delete(RevWalk.RevWalk walk) { string myName = Ref.getLeaf().getName(); if (myName.StartsWith(Constants.R_HEADS)) { Ref head = getRefDatabase().getRef(Constants.HEAD); while (head.isSymbolic()) { head = head.getTarget(); if (myName.Equals(head.getName())) { return(result = RefUpdateResult.REJECTED_CURRENT_BRANCH); } } } try { return(result = updateImpl(walk, new DeleteStore(this))); } catch (IOException) { result = RefUpdateResult.IO_FAILURE; throw; } }
public void stored(RefDirectoryUpdate update, long modified) { ObjectId target = update.getNewObjectId().Copy(); Ref leaf = update.getRef().getLeaf(); putLooseRef(new LooseUnpeeled(modified, leaf.getName(), target)); }
private Ref resolve(Ref @ref, int depth, string prefix, RefList <LooseRef> loose, RefList <Ref> packed) { if (@ref.isSymbolic()) { Ref dst = @ref.getTarget(); if (MAX_SYMBOLIC_REF_DEPTH <= depth) { return(null); // claim it doesn't exist } // If the cached value can be assumed to be current due to a // recent scan of the loose directory, use it. if (loose != null && dst.getName().StartsWith(prefix)) { int idx; if (0 <= (idx = loose.find(dst.getName()))) { dst = loose.get(idx); } else if (0 <= (idx = packed.find(dst.getName()))) { dst = packed.get(idx); } else { return(@ref); } } else { dst = readRef(dst.getName(), packed); if (dst == null) { return(@ref); } } dst = resolve(dst, depth + 1, prefix, loose, packed); if (dst == null) { return(null); } return(new SymbolicRef(@ref.getName(), dst)); } return(@ref); }
public override Ref peel(Ref @ref) { Ref leaf = @ref.getLeaf(); if (leaf.isPeeled() || leaf.getObjectId() == null) { return(@ref); } RevWalk.RevWalk rw = new RevWalk.RevWalk(getRepository()); RevObject obj = rw.parseAny(leaf.getObjectId()); ObjectIdRef newLeaf; if (obj is RevTag) { do { obj = rw.parseAny(((RevTag)obj).getObject()); } while (obj is RevTag); newLeaf = new PeeledTag(leaf.getStorage(), leaf .getName(), leaf.getObjectId(), obj.Copy()); } else { newLeaf = new PeeledNonTag(leaf.getStorage(), leaf .getName(), leaf.getObjectId()); } // Try to remember this peeling in the cache, so we don't have to do // it again in the future, but only if the reference is unchanged. if (leaf.getStorage().IsLoose) { RefList <LooseRef> curList = looseRefs.get(); int idx = curList.find(leaf.getName()); if (0 <= idx && curList.get(idx) == leaf) { LooseRef asPeeled = ((LooseRef)leaf).peel(newLeaf); RefList <LooseRef> newList = curList.set(idx, asPeeled); looseRefs.compareAndSet(curList, newList); } } return(recreate(@ref, newLeaf)); }
public void delete(RefDirectoryUpdate update) { Ref dst = update.getRef().getLeaf(); string name = dst.getName(); // Write the packed-refs file using an atomic update. We might // wind up reading it twice, before and after the lock, to ensure // we don't miss an edit made externally. PackedRefList packed = getPackedRefs(); if (packed.contains(name)) { var lck = new LockFile(packedRefsFile); if (!lck.Lock()) { throw new IOException("Cannot lock " + packedRefsFile); } try { PackedRefList cur = readPackedRefs(0, 0); int idx = cur.find(name); if (0 <= idx) { commitPackedRefs(lck, cur.remove(idx), packed); } } finally { lck.Unlock(); } } RefList <LooseRef> curLoose, newLoose; do { curLoose = looseRefs.get(); int idx = curLoose.find(name); if (idx < 0) { break; } newLoose = curLoose.remove(idx); } while (!looseRefs.compareAndSet(curLoose, newLoose)); int levels = levelsIn(name) - 2; delete(logFor(name), levels); if (dst.getStorage().IsLoose) { update.unlock(); delete(fileFor(name), levels); } modCnt.incrementAndGet(); fireRefsChanged(); }
private static Ref recreate(Ref old, ObjectIdRef leaf) { if (old.isSymbolic()) { Ref dst = recreate(old.getTarget(), leaf); return(new SymbolicRef(old.getName(), dst)); } return(leaf); }
public override string ToString() { var r = new StringBuilder(); r.Append("SymbolicRef["); Ref cur = this; while (cur.isSymbolic()) { r.Append(cur.getName()); r.Append(" -> "); cur = cur.getTarget(); } r.Append(cur.getName()); r.Append('='); r.Append(ObjectId.ToString(cur.getObjectId())); r.Append("]"); return(r.ToString()); }
/// <returns> /// true if the {@code Constants#HEAD} reference needs to be linked /// to the new destination name. /// </returns> protected bool needToUpdateHEAD() { Ref head = source.getRefDatabase().getRef(Constants.HEAD); if (head.isSymbolic()) { head = head.getTarget(); return(head.getName().Equals(source.getName())); } return(false); }
public void 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); } var r = new StringBuilder(); r.Append(ObjectId.ToString(oldId)); r.Append(' '); r.Append(ObjectId.ToString(newId)); r.Append(' '); r.Append(ident.ToExternalString()); r.Append('\t'); r.Append(msg); r.Append('\n'); byte[] rec = Constants.encode(r.ToString()); if (deref && @ref.isSymbolic()) { log(@ref.getName(), rec); log(@ref.getLeaf().getName(), rec); } else { log(@ref.getName(), rec); } }
/// <summary> /// Replace this reference with a symbolic reference to another reference. /// <para/> /// This exact reference (not its traversed leaf) is replaced with a symbolic /// reference to the requested name. /// </summary> /// <param name="target"> /// name of the new target for this reference. The new target name /// must be absolute, so it must begin with {@code refs/}. /// </param> /// <returns><see cref="RefUpdateResult.NEW"/> or <see cref="RefUpdateResult.FORCED"/> on success.</returns> public RefUpdateResult link(string target) { if (!target.StartsWith(Constants.R_REFS)) { throw new ArgumentException("Not " + Constants.R_REFS); } if (getRefDatabase().isNameConflicting(Name)) { return(RefUpdateResult.LOCK_FAILURE); } try { if (!tryLock(false)) { return(RefUpdateResult.LOCK_FAILURE); } Ref old = getRefDatabase().getRef(Name); if (old != null && old.isSymbolic()) { Ref dst = old.getTarget(); if (target.Equals(dst.getName())) { return(result = RefUpdateResult.NO_CHANGE); } } if (old != null && old.getObjectId() != null) { OldObjectId = (old.getObjectId()); } Ref dst2 = getRefDatabase().getRef(target); if (dst2 != null && dst2.getObjectId() != null) { NewObjectId = (dst2.getObjectId()); } return(result = doLink(target)); } catch (IOException) { result = RefUpdateResult.IO_FAILURE; throw; } finally { unlock(); } }
protected override bool tryLock(bool deref) { Ref dst = Ref; if (deref) { dst = dst.getLeaf(); } string name = dst.getName(); _lock = new LockFile(_database.fileFor(name)); if (_lock.Lock()) { dst = _database.getRef(name); OldObjectId = (dst != null ? dst.getObjectId() : null); return(true); } else { return(false); } }
private static RefList <Ref> parsePackedRefs(TextReader br) { var all = new RefList <Ref> .Builder <Ref>(); Ref last = null; bool peeled = false; bool needSort = false; string p; while ((p = br.ReadLine()) != null) { if (p[0] == '#') { if (p.StartsWith(PACKED_REFS_HEADER)) { p = p.Substring(PACKED_REFS_HEADER.Length); peeled = p.Contains(PACKED_REFS_PEELED); } continue; } if (p[0] == '^') { if (last == null) { throw new IOException("Peeled line before ref."); } ObjectId id = ObjectId.FromString(p.Substring(1)); last = new PeeledTag(Storage.Packed, last.getName(), last .getObjectId(), id); all.set(all.size() - 1, last); continue; } int sp = p.IndexOf(' '); ObjectId id2 = ObjectId.FromString(p.Slice(0, sp)); string name = copy(p, sp + 1, p.Length); ObjectIdRef cur; if (peeled) { cur = new PeeledNonTag(Storage.Packed, name, id2); } else { cur = new Unpeeled(Storage.Packed, name, id2); } if (last != null && RefComparator.compareTo(last, cur) > 0) { needSort = true; } all.add(cur); last = cur; } if (needSort) { all.sort(); } return(all.toRefList()); }