private static RefDirectory.LooseSymbolicRef NewSymbolicRef(FileSnapshot snapshot
                                                                    , string name, string target)
        {
            Ref dst = new ObjectIdRef.Unpeeled(RefStorage.NEW, target, null);

            return(new RefDirectory.LooseSymbolicRef(snapshot, name, dst));
        }
Exemple #2
0
        public virtual void TestForceRemotesOrigin()
        {
            string  srcn = "refs/heads/*";
            string  dstn = "refs/remotes/origin/*";
            RefSpec rs   = new RefSpec("+" + srcn + ":" + dstn);

            NUnit.Framework.Assert.IsTrue(rs.IsForceUpdate());
            NUnit.Framework.Assert.IsTrue(rs.IsWildcard());
            NUnit.Framework.Assert.AreEqual(srcn, rs.GetSource());
            NUnit.Framework.Assert.AreEqual(dstn, rs.GetDestination());
            NUnit.Framework.Assert.AreEqual("+" + srcn + ":" + dstn, rs.ToString());
            NUnit.Framework.Assert.AreEqual(rs, new RefSpec(rs.ToString()));
            Ref     r;
            RefSpec expanded;

            r = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/heads/master", null);
            NUnit.Framework.Assert.IsTrue(rs.MatchSource(r));
            NUnit.Framework.Assert.IsFalse(rs.MatchDestination(r));
            expanded = rs.ExpandFromSource(r);
            NUnit.Framework.Assert.AreNotSame(rs, expanded);
            NUnit.Framework.Assert.IsTrue(expanded.IsForceUpdate());
            NUnit.Framework.Assert.IsFalse(expanded.IsWildcard());
            NUnit.Framework.Assert.AreEqual(r.GetName(), expanded.GetSource());
            NUnit.Framework.Assert.AreEqual("refs/remotes/origin/master", expanded.GetDestination
                                                ());
            r = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/remotes/origin/next", null);
            NUnit.Framework.Assert.IsFalse(rs.MatchSource(r));
            NUnit.Framework.Assert.IsTrue(rs.MatchDestination(r));
            r = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/tags/v1.0", null);
            NUnit.Framework.Assert.IsFalse(rs.MatchSource(r));
            NUnit.Framework.Assert.IsFalse(rs.MatchDestination(r));
        }
        /// <exception cref="System.IO.IOException"></exception>
        public override RefUpdate NewUpdate(string name, bool detach)
        {
            bool          detachingSymbolicRef = false;
            RefList <Ref> packed = GetPackedRefs();
            Ref           @ref   = ReadRef(name, packed);

            if (@ref != null)
            {
                @ref = Resolve(@ref, 0, null, null, packed);
            }
            if (@ref == null)
            {
                @ref = new ObjectIdRef.Unpeeled(RefStorage.NEW, name, null);
            }
            else
            {
                detachingSymbolicRef = detach && @ref.IsSymbolic();
                if (detachingSymbolicRef)
                {
                    @ref = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, name, @ref.GetObjectId());
                }
            }
            RefDirectoryUpdate refDirUpdate = new RefDirectoryUpdate(this, @ref);

            if (detachingSymbolicRef)
            {
                refDirUpdate.SetDetachingSymbolicRef();
            }
            return(refDirUpdate);
        }
        /// <summary>Create a reference update to write a temporary reference.</summary>
        /// <remarks>Create a reference update to write a temporary reference.</remarks>
        /// <returns>an update for a new temporary reference.</returns>
        /// <exception cref="System.IO.IOException">a temporary name cannot be allocated.</exception>
        internal virtual RefDirectoryUpdate NewTemporaryUpdate()
        {
            FilePath tmp  = FilePath.CreateTempFile("renamed_", "_ref", refsDir);
            string   name = Constants.R_REFS + tmp.GetName();
            Ref      @ref = new ObjectIdRef.Unpeeled(RefStorage.NEW, name, null);

            return(new RefDirectoryUpdate(this, @ref));
        }
Exemple #5
0
        public virtual void TestUpdateUpToDate()
        {
            RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9"
                                                      , "refs/heads/master", false, null, null);
            Ref @ref = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/heads/master", ObjectId
                                                .FromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));

            TestOneUpdateStatus(rru, @ref, RemoteRefUpdate.Status.UP_TO_DATE, null);
        }
Exemple #6
0
            /// <exception cref="NGit.Errors.TransportException"></exception>
            private Ref ReadRef(SortedDictionary <string, Ref> avail, string rn)
            {
                string s;
                string @ref = WalkRemoteObjectDatabase.ROOT_DIR + rn;

                try
                {
                    BufferedReader br = this.OpenReader(@ref);
                    try
                    {
                        s = br.ReadLine();
                    }
                    finally
                    {
                        br.Close();
                    }
                }
                catch (FileNotFoundException)
                {
                    return(null);
                }
                catch (IOException err)
                {
                    throw new TransportException(this.GetURI(), MessageFormat.Format(JGitText.Get().transportExceptionReadRef
                                                                                     , @ref), err);
                }
                if (s == null)
                {
                    throw new TransportException(this.GetURI(), MessageFormat.Format(JGitText.Get().transportExceptionEmptyRef
                                                                                     , rn));
                }
                if (s.StartsWith("ref: "))
                {
                    string target = Sharpen.Runtime.Substring(s, "ref: ".Length);
                    Ref    r      = avail.Get(target);
                    if (r == null)
                    {
                        r = this.ReadRef(avail, target);
                    }
                    if (r == null)
                    {
                        r = new ObjectIdRef.Unpeeled(RefStorage.NEW, target, null);
                    }
                    r = new SymbolicRef(rn, r);
                    avail.Put(r.GetName(), r);
                    return(r);
                }
                if (ObjectId.IsId(s))
                {
                    Ref r = new ObjectIdRef.Unpeeled(this.Loose(avail.Get(rn)), rn, ObjectId.FromString
                                                         (s));
                    avail.Put(r.GetName(), r);
                    return(r);
                }
                throw new TransportException(this.GetURI(), MessageFormat.Format(JGitText.Get().transportExceptionBadRef
                                                                                 , rn, s));
            }
Exemple #7
0
        public virtual void TestUpdateFastForward()
        {
            RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9"
                                                      , "refs/heads/master", false, null, null);
            Ref @ref = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/heads/master", ObjectId
                                                .FromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));

            TestOneUpdateStatus(rru, @ref, RemoteRefUpdate.Status.OK, true);
        }
        /// <exception cref="System.IO.IOException"></exception>
        private RefList <Ref> ParsePackedRefs(BufferedReader br)
        {
            RefListBuilder <Ref> all = new RefListBuilder <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      = Sharpen.Runtime.Substring(p, PACKED_REFS_HEADER.Length);
                        peeled = p.Contains(PACKED_REFS_PEELED);
                    }
                    continue;
                }
                if (p[0] == '^')
                {
                    if (last == null)
                    {
                        throw new IOException(JGitText.Get().peeledLineBeforeRef);
                    }
                    ObjectId id = ObjectId.FromString(Sharpen.Runtime.Substring(p, 1));
                    last = new ObjectIdRef.PeeledTag(RefStorage.PACKED, last.GetName(), last.GetObjectId
                                                         (), id);
                    all.Set(all.Size() - 1, last);
                    continue;
                }
                int         sp   = p.IndexOf(' ');
                ObjectId    id_1 = ObjectId.FromString(Sharpen.Runtime.Substring(p, 0, sp));
                string      name = Copy(p, sp + 1, p.Length);
                ObjectIdRef cur;
                if (peeled)
                {
                    cur = new ObjectIdRef.PeeledNonTag(RefStorage.PACKED, name, id_1);
                }
                else
                {
                    cur = new ObjectIdRef.Unpeeled(RefStorage.PACKED, name, id_1);
                }
                if (last != null && RefComparator.CompareTo(last, cur) > 0)
                {
                    needSort = true;
                }
                all.Add(cur);
                last = cur;
            }
            if (needSort)
            {
                all.Sort();
            }
            return(all.ToRefList());
        }
Exemple #9
0
        public virtual void TestUpdateNonFastForwardUnknownObject()
        {
            RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9"
                                                      , "refs/heads/master", false, null, null);
            Ref @ref = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/heads/master", ObjectId
                                                .FromString("0000000000000000000000000000000000000001"));

            TestOneUpdateStatus(rru, @ref, RemoteRefUpdate.Status.REJECTED_NONFASTFORWARD, null
                                );
        }
Exemple #10
0
            /// <exception cref="NGit.Errors.TransportException"></exception>
            private Ref ReadRef(SortedDictionary <string, Ref> avail, string path, string name
                                )
            {
                string line;

                try
                {
                    BufferedReader br = this.OpenReader(path);
                    try
                    {
                        line = br.ReadLine();
                    }
                    finally
                    {
                        br.Close();
                    }
                }
                catch (FileNotFoundException)
                {
                    return(null);
                }
                catch (IOException err)
                {
                    throw new TransportException("Cannot read " + this.objectsPath + "/" + path + ": "
                                                 + err.Message, err);
                }
                if (line == null)
                {
                    throw new TransportException("Empty ref: " + name);
                }
                if (line.StartsWith("ref: "))
                {
                    string target = Sharpen.Runtime.Substring(line, "ref: ".Length);
                    Ref    r      = avail.Get(target);
                    if (r == null)
                    {
                        r = this.ReadRef(avail, WalkRemoteObjectDatabase.ROOT_DIR + target, target);
                    }
                    if (r == null)
                    {
                        r = new ObjectIdRef.Unpeeled(RefStorage.NEW, target, null);
                    }
                    r = new SymbolicRef(name, r);
                    avail.Put(r.GetName(), r);
                    return(r);
                }
                if (ObjectId.IsId(line))
                {
                    Ref r = new ObjectIdRef.Unpeeled(this.Loose(avail.Get(name)), name, ObjectId.FromString
                                                         (line));
                    avail.Put(r.GetName(), r);
                    return(r);
                }
                throw new TransportException("Bad ref: " + name + ": " + line);
            }
Exemple #11
0
        public virtual void TestTrackingRefUpdateOnReject()
        {
            RemoteRefUpdate rru = new RemoteRefUpdate(db, "ac7e7e44c1885efb472ad54a78327d66bfc4ecef"
                                                      , "refs/heads/master", false, null, null);
            Ref @ref = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/heads/master", ObjectId
                                                .FromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
            PushResult result = TestOneUpdateStatus(rru, @ref, RemoteRefUpdate.Status.REJECTED_NONFASTFORWARD
                                                    , null);

            NUnit.Framework.Assert.IsTrue(result.GetTrackingRefUpdates().IsEmpty());
        }
Exemple #12
0
        public virtual void TestUpdateRejectedByConnection()
        {
            connectionUpdateStatus = RemoteRefUpdate.Status.REJECTED_OTHER_REASON;
            RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9"
                                                      , "refs/heads/master", false, null, null);
            Ref @ref = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/heads/master", ObjectId
                                                .FromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));

            TestOneUpdateStatus(rru, @ref, RemoteRefUpdate.Status.REJECTED_OTHER_REASON, null
                                );
        }
Exemple #13
0
        public virtual void TestUpdateUnexpectedRemoteVsForce()
        {
            RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9"
                                                      , "refs/heads/master", true, null, ObjectId.FromString("0000000000000000000000000000000000000001"
                                                                                                             ));
            Ref @ref = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/heads/master", ObjectId
                                                .FromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));

            TestOneUpdateStatus(rru, @ref, RemoteRefUpdate.Status.REJECTED_REMOTE_CHANGED, null
                                );
        }
Exemple #14
0
        public virtual void TestPullWithUri()
        {
            string   name     = "branch 'test' of http://egit.eclipse.org/jgit.git";
            ObjectId objectId = ObjectId.FromString("6db9c2ebf75590eef973081736730a9ea169a0c4"
                                                    );
            Ref    remoteBranch = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, name, objectId);
            Ref    master       = db.GetRef("refs/heads/master");
            string message      = formatter.Format(Arrays.AsList(remoteBranch), master);

            NUnit.Framework.Assert.AreEqual("Merge branch 'test' of http://egit.eclipse.org/jgit.git"
                                            , message);
        }
Exemple #15
0
        public virtual void TestCommit()
        {
            ObjectId objectId = ObjectId.FromString("6db9c2ebf75590eef973081736730a9ea169a0c4"
                                                    );
            Ref commit = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, objectId.GetName(), objectId
                                                  );
            Ref    master  = db.GetRef("refs/heads/master");
            string message = formatter.Format(Arrays.AsList(commit), master);

            NUnit.Framework.Assert.AreEqual("Merge commit '6db9c2ebf75590eef973081736730a9ea169a0c4'"
                                            , message);
        }
        /// <exception cref="System.IO.IOException"></exception>
        private void ReadPackedRefsImpl(IDictionary <string, Ref> avail, BufferedReader br
                                        )
        {
            Ref  last   = null;
            bool peeled = false;

            for (; ;)
            {
                string line = br.ReadLine();
                if (line == null)
                {
                    break;
                }
                if (line[0] == '#')
                {
                    if (line.StartsWith(RefDirectory.PACKED_REFS_HEADER))
                    {
                        line   = Sharpen.Runtime.Substring(line, RefDirectory.PACKED_REFS_HEADER.Length);
                        peeled = line.Contains(RefDirectory.PACKED_REFS_PEELED);
                    }
                    continue;
                }
                if (line[0] == '^')
                {
                    if (last == null)
                    {
                        throw new TransportException(JGitText.Get().peeledLineBeforeRef);
                    }
                    ObjectId id = ObjectId.FromString(Sharpen.Runtime.Substring(line, 1));
                    last = new ObjectIdRef.PeeledTag(RefStorage.PACKED, last.GetName(), last.GetObjectId
                                                         (), id);
                    avail.Put(last.GetName(), last);
                    continue;
                }
                int sp = line.IndexOf(' ');
                if (sp < 0)
                {
                    throw new TransportException(MessageFormat.Format(JGitText.Get().unrecognizedRef,
                                                                      line));
                }
                ObjectId id_1 = ObjectId.FromString(Sharpen.Runtime.Substring(line, 0, sp));
                string   name = Sharpen.Runtime.Substring(line, sp + 1);
                if (peeled)
                {
                    last = new ObjectIdRef.PeeledNonTag(RefStorage.PACKED, name, id_1);
                }
                else
                {
                    last = new ObjectIdRef.Unpeeled(RefStorage.PACKED, name, id_1);
                }
                avail.Put(last.GetName(), last);
            }
        }
Exemple #17
0
        public virtual void TestTrackingRefUpdateDisabled()
        {
            RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9"
                                                      , "refs/heads/master", false, null, null);
            Ref @ref = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/heads/master", ObjectId
                                                .FromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));

            refUpdates.AddItem(rru);
            advertisedRefs.AddItem(@ref);
            PushResult result = ExecutePush();

            NUnit.Framework.Assert.IsTrue(result.GetTrackingRefUpdates().IsEmpty());
        }
Exemple #18
0
        public virtual void TestTrackingRefUpdateEnabled()
        {
            RemoteRefUpdate rru = new RemoteRefUpdate(db, "2c349335b7f797072cf729c4f3bb0914ecb6dec9"
                                                      , "refs/heads/master", false, "refs/remotes/test/master", null);
            Ref @ref = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/heads/master", ObjectId
                                                .FromString("ac7e7e44c1885efb472ad54a78327d66bfc4ecef"));

            refUpdates.AddItem(rru);
            advertisedRefs.AddItem(@ref);
            PushResult        result = ExecutePush();
            TrackingRefUpdate tru    = result.GetTrackingRefUpdate("refs/remotes/test/master");

            NUnit.Framework.Assert.IsNotNull(tru);
            NUnit.Framework.Assert.AreEqual("refs/remotes/test/master", tru.GetLocalName());
            NUnit.Framework.Assert.AreEqual(RefUpdate.Result.NEW, tru.GetResult());
        }
Exemple #19
0
        public virtual void TestUpdateMixedCases()
        {
            RemoteRefUpdate rruOk = new RemoteRefUpdate(db, (string)null, "refs/heads/master"
                                                        , false, null, null);
            Ref refToChange = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, "refs/heads/master",
                                                       ObjectId.FromString("2c349335b7f797072cf729c4f3bb0914ecb6dec9"));
            RemoteRefUpdate rruReject = new RemoteRefUpdate(db, (string)null, "refs/heads/nonexisting"
                                                            , false, null, null);

            refUpdates.AddItem(rruOk);
            refUpdates.AddItem(rruReject);
            advertisedRefs.AddItem(refToChange);
            ExecutePush();
            NUnit.Framework.Assert.AreEqual(RemoteRefUpdate.Status.OK, rruOk.GetStatus());
            NUnit.Framework.Assert.AreEqual(true, rruOk.IsFastForward());
            NUnit.Framework.Assert.AreEqual(RemoteRefUpdate.Status.NON_EXISTING, rruReject.GetStatus
                                                ());
        }
Exemple #20
0
        public virtual void TestForceMasterMaster()
        {
            string  sn = "refs/heads/master";
            RefSpec rs = new RefSpec("+" + sn + ":" + sn);

            NUnit.Framework.Assert.IsTrue(rs.IsForceUpdate());
            NUnit.Framework.Assert.IsFalse(rs.IsWildcard());
            NUnit.Framework.Assert.AreEqual(sn, rs.GetSource());
            NUnit.Framework.Assert.AreEqual(sn, rs.GetDestination());
            NUnit.Framework.Assert.AreEqual("+" + sn + ":" + sn, rs.ToString());
            NUnit.Framework.Assert.AreEqual(rs, new RefSpec(rs.ToString()));
            Ref r = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, sn, null);

            NUnit.Framework.Assert.IsTrue(rs.MatchSource(r));
            NUnit.Framework.Assert.IsTrue(rs.MatchDestination(r));
            NUnit.Framework.Assert.AreSame(rs, rs.ExpandFromSource(r));
            r = new ObjectIdRef.Unpeeled(RefStorage.LOOSE, sn + "-and-more", null);
            NUnit.Framework.Assert.IsFalse(rs.MatchSource(r));
            NUnit.Framework.Assert.IsFalse(rs.MatchDestination(r));
        }
Exemple #21
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="NGit.Errors.PackProtocolException"></exception>
        private FetchConnection NewDumbConnection(InputStream @in)
        {
            TransportHttp.HttpObjectDB d  = new TransportHttp.HttpObjectDB(this, objectsUrl);
            BufferedReader             br = ToBufferedReader(@in);
            IDictionary <string, Ref>  refs;

            try
            {
                refs = d.ReadAdvertisedImpl(br);
            }
            finally
            {
                br.Close();
            }
            if (!refs.ContainsKey(Constants.HEAD))
            {
                // If HEAD was not published in the info/refs file (it usually
                // is not there) download HEAD by itself as a loose file and do
                // the resolution by hand.
                //
                HttpURLConnection conn = HttpOpen(new Uri(baseUrl, Constants.HEAD));
                int status             = HttpSupport.Response(conn);
                switch (status)
                {
                case HttpURLConnection.HTTP_OK:
                {
                    br = ToBufferedReader(OpenInputStream(conn));
                    try
                    {
                        string line = br.ReadLine();
                        if (line != null && line.StartsWith(RefDirectory.SYMREF))
                        {
                            string target = Sharpen.Runtime.Substring(line, RefDirectory.SYMREF.Length);
                            Ref    r      = refs.Get(target);
                            if (r == null)
                            {
                                r = new ObjectIdRef.Unpeeled(RefStorage.NEW, target, null);
                            }
                            r = new SymbolicRef(Constants.HEAD, r);
                            refs.Put(r.GetName(), r);
                        }
                        else
                        {
                            if (line != null && ObjectId.IsId(line))
                            {
                                Ref r = new ObjectIdRef.Unpeeled(RefStorage.NETWORK, Constants.HEAD, ObjectId.FromString
                                                                     (line));
                                refs.Put(r.GetName(), r);
                            }
                        }
                    }
                    finally
                    {
                        br.Close();
                    }
                    break;
                }

                case HttpURLConnection.HTTP_NOT_FOUND:
                {
                    break;
                }

                default:
                {
                    throw new TransportException(uri, MessageFormat.Format(JGitText.Get().cannotReadHEAD
                                                                           , status, conn.GetResponseMessage()));
                }
                }
            }
            WalkFetchConnection wfc = new WalkFetchConnection(this, d);

            wfc.Available(refs);
            return(wfc);
        }