Pairing of a name and the ObjectId it currently has. A ref in Git is (more or less) a variable that holds a single object identifier. The object identifier can be any valid Git object (blob, tree, commit, annotated tag, ...). The ref name has the attributes of the ref that was asked for as well as the ref it was resolved to for symbolic refs plus the object id it points to and (for tags) the peeled target object id, i.e. the tag resolved recursively until a non-tag object is referenced.
Esempio n. 1
0
        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();
        }
Esempio n. 2
0
        private static GitSharp.Core.Ref guessHEAD(FetchResult result)
        {
            GitSharp.Core.Ref        idHEAD        = result.GetAdvertisedRef(Constants.HEAD);
            List <GitSharp.Core.Ref> availableRefs = new List <GitSharp.Core.Ref>();

            GitSharp.Core.Ref head = null;

            foreach (GitSharp.Core.Ref r in result.AdvertisedRefs)
            {
                string n = r.Name;
                if (!n.StartsWith(Constants.R_HEADS))
                {
                    continue;
                }
                availableRefs.Add(r);
                if (idHEAD == null || head != null)
                {
                    continue;
                }

                if (r.ObjectId.Equals(idHEAD.ObjectId))
                {
                    head = r;
                }
            }
            availableRefs.Sort(RefComparator.INSTANCE);
            if (idHEAD != null && head == null)
            {
                head = idHEAD;
            }
            return(head);
        }
Esempio n. 3
0
 public RefUpdate(RefDatabase refDb, Ref r, FileInfo f)
 {
     _db = refDb;
     _ref = r;
     OldObjectId = r.ObjectId;
     _looseFile = f;
     _refLogMessage = string.Empty;
     Result = RefUpdateResult.NotAttempted;
 }
Esempio n. 4
0
 public void testPushResult()
 {
     RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
                                               "refs/heads/master", false, "refs/remotes/test/master", null);
     Ref @ref = new Ref(Ref.Storage.Loose, "refs/heads/master", ObjectId.FromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
     refUpdates.Add(rru);
     advertisedRefs.Add(@ref);
     PushResult result = executePush();
     Assert.AreEqual(1, result.TrackingRefUpdates.Count);
     Assert.AreEqual(1, result.AdvertisedRefs.Count);
     Assert.AreEqual(1, result.RemoteUpdates.Count);
     Assert.IsNotNull(result.GetTrackingRefUpdate("refs/remotes/test/master"));
     Assert.IsNotNull(result.GetAdvertisedRef("refs/heads/master"));
     Assert.IsNotNull(result.GetRemoteUpdate("refs/heads/master"));
 }
Esempio n. 5
0
        public override void Execute()
        {
            if (Source.Length <= 0)
            {
                throw new ArgumentNullException("Repository", "fatal: You must specify a repository to clone.");
            }

            URIish source = new URIish(Source);

            if (Mirror)
            {
                Bare = true;
            }
            if (Bare)
            {
                if (OriginName != null)
                {
                    throw new ArgumentException("Bare+Origin", "--bare and --origin " + OriginName + " options are incompatible.");
                }
                NoCheckout = true;
            }
            if (OriginName == null)
            {
                OriginName = "origin";
            }

            var repo = new GitSharp.Core.Repository(new DirectoryInfo(ActualDirectory));

            repo.Create(Bare);
            repo.Config.setBoolean("core", null, "bare", Bare);
            repo.Config.save();
            Repository = new Repository(repo);
            if (!Quiet)
            {
                OutputStream.WriteLine("Initialized empty Git repository in " + repo.Directory.FullName);
                OutputStream.Flush();
            }

            saveRemote(source);
            FetchResult r = runFetch();

            GitSharp.Core.Ref branch = guessHEAD(r);
            if (!NoCheckout)
            {
                doCheckout(branch);
            }
        }
Esempio n. 6
0
        public void testDeleteMaster()
        {
            string sn = "refs/heads/master";
            RefSpec rs = new RefSpec(":" + sn);
            Assert.IsFalse(rs.Force);
            Assert.IsFalse(rs.Wildcard);
            Assert.AreEqual(sn, rs.Destination);
            Assert.IsNull(rs.Source);
            Assert.AreEqual(":" + sn, rs.ToString());
            Assert.AreEqual(rs, new RefSpec(rs.ToString()));

            Ref r = new Ref(Ref.Storage.Loose, sn, null);
            Assert.IsFalse(rs.MatchSource(r));
            Assert.IsTrue(rs.MatchDestination(r));
            Assert.AreSame(rs, rs.ExpandFromSource(r));

            r = new Ref(Ref.Storage.Loose, sn + "-and-more", null);
            Assert.IsFalse(rs.MatchSource(r));
            Assert.IsFalse(rs.MatchDestination(r));
        }
Esempio n. 7
0
        private static GitSharp.Core.Ref guessHEAD(FetchResult result)
        {
            // Some transports allow us to see where HEAD points to. If that is not so,
            // we'll have to guess.
            GitSharp.Core.Ref head = result.GetAdvertisedRef(Constants.HEAD);
            if (head != null)
            {
                return(head);
            }

            var availableHeads = result.AdvertisedRefs.Where(r => r.Name.StartsWith(Constants.R_HEADS));

            // master is our preferred guess, so if it's advertised, return that.
            GitSharp.Core.Ref guessedHead = result.GetAdvertisedRef(Constants.R_HEADS + Constants.MASTER);
            if (guessedHead == null && availableHeads.Count() > 0)
            {
                // if master is not advertised, return any other head.
                guessedHead = availableHeads.First();
            }

            return(guessedHead);
        }
Esempio n. 8
0
 public void testUpdateMixedCases()
 {
     RemoteRefUpdate rruOk = new RemoteRefUpdate(db, null, "refs/heads/master", false, null, null);
     Ref refToChange = new Ref(Ref.Storage.Loose, "refs/heads/master", ObjectId.FromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
     RemoteRefUpdate rruReject = new RemoteRefUpdate(db, null, "refs/heads/nonexisting", false, null, null);
     refUpdates.Add(rruOk);
     refUpdates.Add(rruReject);
     advertisedRefs.Add(refToChange);
     executePush();
     Assert.AreEqual(RemoteRefUpdate.UpdateStatus.OK, rruOk.Status);
     Assert.AreEqual(true, rruOk.FastForward);
     Assert.AreEqual(RemoteRefUpdate.UpdateStatus.NON_EXISTING, rruReject.Status);
 }
Esempio n. 9
0
        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());
        }
Esempio n. 10
0
 public void testUpdateUpToDate()
 {
     RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9", "refs/heads/master", false, null, null);
     Ref @ref = new Ref(Ref.Storage.Loose, "refs/heads/master", ObjectId.FromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
     testOneUpdateStatus(rru, @ref, RemoteRefUpdate.UpdateStatus.UP_TO_DATE, null);
 }
Esempio n. 11
0
		internal Branch(Repository repo, CoreRef @ref)
			: this(repo, GetBranchName (@ref.Name))
		{
			_ref_name = GetRefName (@ref.Name);
		}
Esempio n. 12
0
 internal Branch(Repository repo, CoreRef @ref)
     : this(repo, GetBranchName(@ref.Name))
 {
     _ref_name = GetRefName(@ref.Name);
 }
Esempio n. 13
0
        /// <summary>
        /// Do it.
        /// </summary>
        public override void Execute()
        {
            if (Source.Length <= 0)
            {
                throw new ArgumentException("fatal: You must specify a repository to clone.");
            }

            if (Directory != null && GitDirectory != null)
            {
                throw new ArgumentException("conflicting usage of --git-dir and arguments");
            }

            var source = new URIish(Source);

            if (Directory == null)
            {
                try
                {
                    Directory = source.getHumanishName();
                }
                catch (InvalidOperationException e)
                {
                    throw new ArgumentException("cannot guess local name from " + source, e);
                }
            }

            if (GitDirectory == null)
            {
                GitDirectory = Path.Combine(Directory, Constants.DOT_GIT);
            }

            if (Mirror)
            {
                Bare = true;
            }
            if (Bare)
            {
                if (OriginName != null)
                {
                    throw new ArgumentException("Bare+Origin", "--bare and --origin " + OriginName + " options are incompatible.");
                }
                NoCheckout = true;
            }
            if (OriginName == null)
            {
                OriginName = Constants.DEFAULT_REMOTE_NAME;
            }

            if (System.IO.Directory.Exists(Directory) && System.IO.Directory.GetFileSystemEntries(Directory).Length != 0)
            {
                throw new InvalidOperationException(string.Format("destination path '{0}' already exists and is not an empty directory.", new DirectoryInfo(Directory).FullName));
            }

            var repo = new Core.Repository(new DirectoryInfo(GitDirectory));

            repo.Create(Bare);
            repo.Config.setBoolean("core", null, "bare", Bare);
            repo.Config.save();
            Repository = new Repository(repo);
            if (!Quiet)
            {
                OutputStream.WriteLine("Initialized empty Git repository in " + repo.Directory.FullName);
                OutputStream.Flush();
            }

            saveRemote(source);

            FetchResult r;

            try
            {
                r = runFetch();
            }
            catch (NoRemoteRepositoryException)
            {
                Repository.Dispose();
                throw;
            }
            GitSharp.Core.Ref branch = guessHEAD(r);
            if (!NoCheckout)
            {
                doCheckout(branch);
            }
        }
Esempio n. 14
0
 ///	<summary>
 /// An set of update operations for renaming a ref
 ///	</summary>
 ///	<param name="fromRef"> Old ref name </param>
 ///	<param name="toRef"> New ref name </param>
 ///	<returns> a RefUpdate operation to rename a ref </returns>
 ///	<exception cref="IOException"> </exception>
 public RefRename NewRename(string fromRef, string toRef)
 {
     RefreshPackedRefs();
     Ref f = ReadRefBasic(fromRef, 0);
     var t = new Ref(Ref.Storage.New, toRef, null);
     var refUpdateFrom = new RefUpdate(this, f, FileForRef(f.Name));
     var refUpdateTo = new RefUpdate(this, t, FileForRef(t.Name));
     return new RefRename(refUpdateTo, refUpdateFrom);
 }
Esempio n. 15
0
 public void testUpdateNonFastForwardUnknownObject()
 {
     RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
                                               "refs/heads/master", false, null, null);
     Ref @ref = new Ref(Ref.Storage.Loose, "refs/heads/master", ObjectId.FromString("0000000000000000000000000000000000000001"));
     testOneUpdateStatus(rru, @ref, RemoteRefUpdate.UpdateStatus.REJECTED_NONFASTFORWARD, null);
 }
Esempio n. 16
0
 public void testUpdateUnexpectedRemoteVsForce()
 {
     RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
                                   "refs/heads/master", true, null,
                                   ObjectId.FromString("0000000000000000000000000000000000000001"));
     Ref @ref = new Ref(Ref.Storage.Loose, "refs/heads/master", ObjectId.FromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
     testOneUpdateStatus(rru, @ref, RemoteRefUpdate.UpdateStatus.REJECTED_REMOTE_CHANGED, null);
 }
Esempio n. 17
0
 internal Commit(Repository repo, CoreRef @ref)
     : base(repo, @ref.ObjectId)
 {
 }
Esempio n. 18
0
        public void testForceRemotesOrigin()
        {
            string srcn = "refs/heads/*";
            string dstn = "refs/remotes/origin/*";
            RefSpec rs = new RefSpec("+" + srcn + ":" + dstn);
            Assert.IsTrue(rs.Force);
            Assert.IsTrue(rs.Wildcard);
            Assert.AreEqual(srcn, rs.Source);
            Assert.AreEqual(dstn, rs.Destination);
            Assert.AreEqual("+" + srcn + ":" + dstn, rs.ToString());
            Assert.AreEqual(rs, new RefSpec(rs.ToString()));

            Ref r;
            RefSpec expanded;

            r = new Ref(Ref.Storage.Loose, "refs/heads/master", null);
            Assert.IsTrue(rs.MatchSource(r));
            Assert.IsFalse(rs.MatchDestination(r));
            expanded = rs.ExpandFromSource(r);
            Assert.AreNotSame(rs, expanded);
            Assert.IsTrue(expanded.Force);
            Assert.IsFalse(expanded.Wildcard);
            Assert.AreEqual(r.Name, expanded.Source);
            Assert.AreEqual("refs/remotes/origin/master", expanded.Destination);

            r = new Ref(Ref.Storage.Loose, "refs/remotes/origin/next", null);
            Assert.IsFalse(rs.MatchSource(r));
            Assert.IsTrue(rs.MatchDestination(r));

            r = new Ref(Ref.Storage.Loose, "refs/tags/v1.0", null);
            Assert.IsFalse(rs.MatchSource(r));
            Assert.IsFalse(rs.MatchDestination(r));
        }
Esempio n. 19
0
 internal Ref(Repository repo, CoreRef @ref)
     : this(repo, @ref.Name)
 {
 }
Esempio n. 20
0
 public void testTrackingRefUpdateDisabled()
 {
     RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9", "refs/heads/master", false, null, null);
     Ref @ref = new Ref(Ref.Storage.Loose, "refs/heads/master", ObjectId.FromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
     refUpdates.Add(rru);
     advertisedRefs.Add(@ref);
     PushResult result = executePush();
     Assert.IsTrue(result.TrackingRefUpdates.Count == 0);
 }
Esempio n. 21
0
 public Entry(Entry <T> n, Ref <T> r)
 {
     Next = n;
     Ref  = r;
 }
Esempio n. 22
0
 public void testUpdateRejectedByConnection()
 {
     connectionUpdateStatus = RemoteRefUpdate.UpdateStatus.REJECTED_OTHER_REASON;
     RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9",
                                               "refs/heads/master", false, null, null);
     Ref @ref = new Ref(Ref.Storage.Loose, "refs/heads/master", ObjectId.FromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));
     testOneUpdateStatus(rru, @ref, RemoteRefUpdate.UpdateStatus.REJECTED_OTHER_REASON, null);
 }
 protected RefUpdate(Ref @ref)
 {
     _ref = @ref;
     oldValue = @ref.getObjectId();
     refLogMessage = "";
 }
Esempio n. 24
0
 internal Ref(Repository repo, CoreRef @ref)
     : this(repo, @ref.Name)
 {
 }
Esempio n. 25
0
        private void printRefUpdateResult(URIish uri, OperationResult result, RemoteRefUpdate rru)
        {
            if (!shownUri)
            {
                shownUri = true;
                OutputStream.WriteLine("To " + uri);
            }

            string remoteName = rru.RemoteName;
            string srcRef     = rru.IsDelete ? null : rru.SourceRef;

            switch (rru.Status)
            {
            case RemoteRefUpdate.UpdateStatus.OK:
            {
                if (rru.IsDelete)
                {
                    printUpdateLine('-', "[deleted]", null, remoteName, null);
                }
                else
                {
                    GitSharp.Core.Ref oldRef = result.GetAdvertisedRef(remoteName);
                    if (oldRef == null)
                    {
                        string summary = remoteName.StartsWith(Constants.R_TAGS) ? "[new tag]" : "[new branch]";
                        printUpdateLine('*', summary, srcRef, remoteName, null);
                    }
                    else
                    {
                        bool   fastForward = rru.FastForward;
                        char   flag        = fastForward ? ' ' : '+';
                        string summary     = oldRef.ObjectId.Abbreviate(Repository._internal_repo).name() +
                                             (fastForward ? ".." : "...") +
                                             rru.NewObjectId.Abbreviate(Repository._internal_repo).name();
                        string message = fastForward ? null : "forced update";
                        printUpdateLine(flag, summary, srcRef, remoteName, message);
                    }
                }
                break;
            }

            case RemoteRefUpdate.UpdateStatus.NON_EXISTING:
                printUpdateLine('X', "[no match]", null, remoteName, null);
                break;

            case RemoteRefUpdate.UpdateStatus.REJECTED_NODELETE:
                printUpdateLine('!', "[rejected]", null, remoteName, "remote side does not support deleting refs");
                break;

            case RemoteRefUpdate.UpdateStatus.REJECTED_NONFASTFORWARD:
                printUpdateLine('!', "[rejected]", srcRef, remoteName, "non-fast forward");
                break;

            case RemoteRefUpdate.UpdateStatus.REJECTED_REMOTE_CHANGED:
            {
                string message = "remote ref object changed - is not expected one " +
                                 rru.ExpectedOldObjectId.Abbreviate(Repository._internal_repo).name();
                printUpdateLine('!', "[rejected]", srcRef, remoteName, message);
                break;
            }

            case RemoteRefUpdate.UpdateStatus.REJECTED_OTHER_REASON:
                printUpdateLine('!', "[rejected]", srcRef, remoteName, rru.Message);
                break;

            case RemoteRefUpdate.UpdateStatus.UP_TO_DATE:
                if (Verbose)
                {
                    printUpdateLine('=', "[up to date]", srcRef, remoteName, null);
                }
                break;

            case RemoteRefUpdate.UpdateStatus.NOT_ATTEMPTED:
            case RemoteRefUpdate.UpdateStatus.AWAITING_REPORT:
                printUpdateLine('?', "[unexpected push-process behavior]", srcRef, remoteName, rru.Message);
                break;
            }
        }
Esempio n. 26
0
		internal Commit(Repository repo, CoreRef @ref)
			: base(repo, @ref.ObjectId)
		{
		}
Esempio n. 27
0
 public void testUpdateNonFastForwardForced()
 {
     RemoteRefUpdate rru = new RemoteRefUpdate(db, "ac7e7e44c1885efb472ad54a78327d66bfc4ecef",
                                   "refs/heads/master", true, null, null);
     Ref @ref = new Ref(Ref.Storage.Loose, "refs/heads/master", ObjectId.FromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
     testOneUpdateStatus(rru, @ref, RemoteRefUpdate.UpdateStatus.OK, false);
 }
Esempio n. 28
0
 public Ref Peel(Ref pRef)
 {
     return _refDb.Peel(pRef);
 }
Esempio n. 29
0
 /// <summary>
 /// Create a command to update, create or delete a ref in this repository.
 /// </summary>
 /// <param name="name">name of the ref the caller wants to modify.</param>
 /// <param name="detach">true to detach the ref, i.e. replace symref with object ref</param>
 /// <returns>An update command. The caller must finish populating this command  and then invoke one of the update methods to actually make a change.</returns>
 public RefUpdate NewUpdate(string name, bool detach)
 {
     RefreshPackedRefs();
     Ref r = ReadRefBasic(name, 0);
     if (r == null)
     {
         r = new Ref(Ref.Storage.New, name, null);
     }
     else if (detach)
     {
         r = new Ref(Ref.Storage.New, name, r.ObjectId);
     }
Esempio n. 30
0
 private PushResult testOneUpdateStatus(RemoteRefUpdate rru, Ref advertisedRef, RemoteRefUpdate.UpdateStatus expectedStatus, bool? fastForward)
 {
     refUpdates.Add(rru);
     if (advertisedRef != null)
         advertisedRefs.Add(advertisedRef);
     PushResult result = executePush();
     Assert.AreEqual(expectedStatus, rru.Status);
     if (fastForward.HasValue)
         Assert.AreEqual(fastForward.Value, rru.FastForward);
     return result;
 }
Esempio n. 31
0
        }

        /// <summary>
        /// Returns the object that this object points to if this is a commit.
        /// </summary>
        /// <param name="dref">The ref.</param>
        /// <returns></returns>
        internal Ref Peel(Ref dref)
        {
            if (dref.Peeled) return dref;

            ObjectId peeled = null;
            try
            {
                Tag target = (Repository.MapObject(dref.ObjectId, dref.Name) as Tag);

                while (target != null)
                {
                    peeled = target.Id;

                    if (target.TagType == Constants.TYPE_TAG)
                    {
                        target = (Repository.MapObject(target.Id, dref.Name) as Tag);
                    }
                    else
                    {
                        break;
                    }
                }
            }
            catch (IOException)
            {
                // Ignore a read error.  Callers will also get the same error
                // if they try to use the result of getPeeledObjectId.
            }
Esempio n. 32
0
        }

        internal void Stored(string origName, string name, ObjectId id, DateTime time)
        {
            lock (locker)
            {
                _looseRefs[name] = new Ref(Ref.Storage.Loose, name, name, id);
                _looseRefsMTime[name] = time;
                SetModified();
            }
Esempio n. 33
0
 /// <summary>
 /// Create a new ref pairing.
 /// </summary>
 /// <param name="refName">name of this ref.</param>
 /// <param name="target">the ref we reference and derive our value from.</param>
 public SymbolicRef(string refName, Ref target)
 {
     _name = refName;
     _target = target;
 }
Esempio n. 34
0
        }

        private Ref ReadRefBasic(String origName, string name, int depth)
        {
            // Prefer loose ref to packed ref as the loose
            // file can be more up-to-date than a packed one.
            //
            Ref @ref;
            _looseRefs.TryGetValue(name, out @ref);
            FileInfo loose = FileForRef(name);
            loose.Refresh();
            DateTime mtime = loose.Exists ? loose.LastWriteTime : DateTime.MinValue;	// [ammachado] If the file does not exists, LastWriteTimes returns '1600-12-31 22:00:00'

            if (@ref != null)
            {
                DateTime cachedLastModified;
                if (_looseRefsMTime.TryGetValue(name, out cachedLastModified) && cachedLastModified == mtime)
                {
                    return _packedRefs.ContainsKey(origName) ?
                        new Ref(Ref.Storage.LoosePacked, origName, @ref.ObjectId, @ref.PeeledObjectId, @ref.Peeled)
                        : @ref;
                }

                _looseRefs.Remove(origName);
                _looseRefsMTime.Remove(origName);
            }

            if (!loose.Exists)
            {
                // File does not exist.
                // Try packed cache.
                //
                _packedRefs.TryGetValue(name, out @ref);
                if (@ref != null && [email protected](origName))
                {
                    @ref = new Ref(Ref.Storage.LoosePacked, origName, name, @ref.ObjectId);
                }
                return @ref;
            }

            string line = null;
            try
            {
                DateTime cachedLastModified;
                if (_looseRefsMTime.TryGetValue(name, out cachedLastModified) && cachedLastModified == mtime)
                {
                    _looseSymRefs.TryGetValue(name, out line);
                }

                if (string.IsNullOrEmpty(line))
                {
                    line = ReadLine(loose);
                    _looseRefsMTime[name] = mtime;
                    _looseSymRefs[name] = line;
                }
            }
            catch (FileNotFoundException)
            {
                return _packedRefs[name];
            }

            if (string.IsNullOrEmpty(line))
            {
                _looseRefs.Remove(origName);
                _looseRefsMTime.Remove(origName);
                return new Ref(Ref.Storage.Loose, origName, name, null);
            }

            if (line.StartsWith("ref: "))
            {
                if (depth >= 5)
                {
                    throw new IOException("Exceeded maximum ref depth of " + depth + " at " + name + ".  Circular reference?");
                }

                string target = line.Substring("ref: ".Length);
                Ref r = ReadRefBasic(target, depth + 1);
                DateTime cachedMtime;
                if (_looseRefsMTime.TryGetValue(name, out cachedMtime) && cachedMtime != mtime)
                {
                    SetModified();
                }
                _looseRefsMTime[name] = mtime;

                if (r == null)
                {
                    return new Ref(Ref.Storage.Loose, origName, target, null);
                }

                if (!origName.Equals(r.Name))
                {
                    r = new Ref(Ref.Storage.LoosePacked, origName, r.Name, r.ObjectId, r.PeeledObjectId, true);
                }

                return r;
            }

            SetModified();

            ObjectId id;
            try
            {
                id = ObjectId.FromString(line);
            }
            catch (ArgumentException)
            {
                throw new IOException("Not a ref: " + name + ": " + line);
            }

            Ref.Storage storage = _packedRefs.ContainsKey(name) ? Ref.Storage.LoosePacked : Ref.Storage.Loose;
            @ref = new Ref(storage, name, id);
            _looseRefs[name] = @ref;
            _looseRefsMTime[name] = mtime;

            if (!origName.Equals(name))
            {
                @ref = new Ref(Ref.Storage.Loose, origName, name, id);
                _looseRefs[origName] = @ref;
            }
Esempio n. 35
0
 public Ref Peel(Ref pRef)
 {
     try
     {
         return _refDb.peel(pRef);
     }
     catch (IOException)
     {
         // Historical accident; if the reference cannot be peeled due
         // to some sort of repository access problem we claim that the
         // same as if the reference was not an annotated tag.
         return pRef;
     }
 }
Esempio n. 36
0
        }

        private void RefreshPackedRefs()
        {
            lock(locker)
            {
                _packedRefsFile.Refresh();
                if (!_packedRefsFile.Exists) return;

                DateTime currTime = _packedRefsFile.LastWriteTime;
                long currLen = currTime == DateTime.MinValue ? 0 : _packedRefsFile.Length;
                if (currTime == _packedRefsLastModified && currLen == _packedRefsLength) return;

                if (currTime == DateTime.MinValue)
                {
                    _packedRefsLastModified = DateTime.MinValue;
                    _packedRefsLength = 0;
                    _packedRefs = new Dictionary<string, Ref>();
                    return;
                }

                var newPackedRefs = new Dictionary<string, Ref>();
                try
                {
                    using (var b = OpenReader(_packedRefsFile))
                    {
                        string p;
                        Ref last = null;
                        while ((p = b.ReadLine()) != null)
                        {
                            if (p[0] == '#') continue;

                            if (p[0] == '^')
                            {
                                if (last == null)
                                {
                                    throw new IOException("Peeled line before ref.");
                                }

                                ObjectId id = ObjectId.FromString(p.Substring(1));
                                last = new Ref(Ref.Storage.Packed, last.Name, last.Name, last.ObjectId, id, true);
                                newPackedRefs.put(last.Name,  last);
                                continue;
                            }

                            int sp = p.IndexOf(' ');
                            ObjectId id2 = ObjectId.FromString(p.Slice(0, sp));
                            string name = p.Substring(sp + 1);
                            last = new Ref(Ref.Storage.Packed, name, name, id2);
                            newPackedRefs.Add(last.Name, last);
                        }
                    }

                    _packedRefsLastModified = currTime;
                    _packedRefsLength = currLen;
                    _packedRefs = newPackedRefs;
                    SetModified();
                }
                catch (FileNotFoundException)
                {
                    // Ignore it and leave the new map empty.
                    //
                    _packedRefsLastModified = DateTime.MinValue;
                    _packedRefsLength = 0;
                    _packedRefs = newPackedRefs;
                }
                catch (IOException e)
                {
                    throw new GitException("Cannot read packed refs", e);
                }
Esempio n. 37
0
 /// <summary>
 /// Peel a possibly unpeeled reference by traversing the annotated tags.
 /// <para/>
 /// If the reference cannot be peeled (as it does not refer to an annotated
 /// tag) the peeled id stays null, but <see cref="Ref.IsPeeled"/> will be true.
 /// <para/>
 /// Implementors should check <see cref="Ref.IsPeeled"/> before performing any
 /// additional work effort.
 /// </summary>
 /// <param name="ref">The reference to peel</param>
 /// <returns>
 /// {@code ref} if {@code ref.isPeeled()} is true; otherwise a new
 /// Ref object representing the same data as Ref, but isPeeled() will
 /// be true and getPeeledObjectId() will contain the peeled object
 /// (or null).
 /// </returns>
 public abstract Ref peel(Ref @ref);
Esempio n. 38
0
 public LooseSymbolicRef(long mtime, string refName, Ref target)
     : base(refName, target)
 {
     _lastModified = mtime;
 }
Esempio n. 39
0
 public RefDirectoryUpdate(RefDirectory r, Ref @ref)
     : base(@ref)
 {
     _database = r;
 }
Esempio n. 40
0
 public void testTrackingRefUpdateOnReject()
 {
     RemoteRefUpdate rru = new RemoteRefUpdate(db, "ac7e7e44c1885efb472ad54a78327d66bfc4ecef", "refs/heads/master", false, null, null);
     Ref @ref = new Ref(Ref.Storage.Loose, "refs/heads/master", ObjectId.FromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
     PushResult result = testOneUpdateStatus(rru, @ref, RemoteRefUpdate.UpdateStatus.REJECTED_NONFASTFORWARD, null);
     Assert.IsTrue(result.TrackingRefUpdates.Count == 0);
 }