Exemplo n.º 1
0
        public void UpdateRef()
        {
            string @ref    = Path.Join("HEAD");
            string refPath = Path.Join("foo", ".ugit", @ref);

            byte[] data = "foobar".Encode();
            this.mockFileOperator.Setup(f => f.TryRead(refPath, out data)).Returns(true);
            this.mockFileOperator.Setup(f => f.Write(refPath, It.IsAny <byte[]>()));
            this.dataProvider.UpdateRef(@ref, RefValue.Create(false, "foo123bar"));
            this.mockFileOperator.VerifyAll();
        }
Exemplo n.º 2
0
 public void CreateCommitExceptionTest()
 {
     this.treeOperation.Setup(t => t.WriteTree()).Returns("tree-oid");
     this.dataProvider.Setup(d => d.GetRef("HEAD", true)).Returns(RefValue.Create(false, ""));
     this.treeOperation.Setup(d => d.GetTree("", "")).Returns(new Tree()
     {
     });
     this.treeOperation.Setup(d => d.GetIndexTree()).Returns(new Tree()
     {
     });
     _ = commitOperation.CreateCommit("hello foo");
 }
Exemplo n.º 3
0
        public void AllTest()
        {
            string prefix = Path.Join("refs", "tags");

            dataProviderMock.Setup(d => d.GetAllRefs(prefix, false)).Returns(new[] {
                ValueTuple.Create(Path.Join(prefix, "v1.0"), RefValue.Create(false, "oid1")),
                ValueTuple.Create(Path.Join(prefix, "v1.1"), RefValue.Create(false, "oid1.1")),
            });

            var tags = tagOperation.All.ToArray();

            CollectionAssert.AreEqual(new string[] { "v1.0", "v1.1" }, tags);
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public void Checkout(string name)
        {
            string oid    = this.dataProvider.GetOid(name);
            Commit commit = this.commitOperation.GetCommit(oid);

            this.treeOperation.ReadTree(commit.Tree, true);

            RefValue HEAD = this.branchOperation.IsBranch(name)
                ? RefValue.Create(true, Path.Join(Constants.Refs, Constants.Heads, name))
                : RefValue.Create(false, oid);

            this.dataProvider.UpdateRef(Constants.HEAD, HEAD, false);
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public void UpdateRef(string @ref, RefValue value, bool deref = true)
        {
            if (string.IsNullOrWhiteSpace(value.Value))
            {
                throw new UgitException("ref value could be null or empty");
            }

            @ref = this.GetRefInternal(@ref, deref).Item1;
            var val = value.Symbolic ? $"ref: {value.Value}" : value.Value;

            string filePath = Path.Join(this.GitDirFullPath, @ref);

            this.fileOperator.Write(filePath, val.Encode());
        }
Exemplo n.º 6
0
        public void UpdateRefDeref()
        {
            string @ref        = Path.Join("HEAD");
            string headRefPath = Path.Join("foo", ".ugit", @ref);

            byte[] headData      = $"ref: {Path.Join("refs", "heads", "master")}".Encode();
            string masterRefPath = Path.Join("foo", ".ugit", "refs", "heads", "master");

            byte[] masterData = "foobar123".Encode();
            this.mockFileOperator.Setup(f => f.TryRead(headRefPath, out headData)).Returns(true);
            this.mockFileOperator.Setup(f => f.TryRead(masterRefPath, out masterData)).Returns(true);
            this.mockFileOperator.Setup(f => f.Write(masterRefPath, It.IsAny <byte[]>()));
            this.dataProvider.UpdateRef(@ref, RefValue.Create(false, "foo123bar"));
            this.mockFileOperator.VerifyAll();
        }
Exemplo n.º 7
0
        /// <inheritdoc/>
        public void Merge(string other)
        {
            string head        = this.dataProvider.GetRef("HEAD").Value;
            var    headCommit  = this.commitOperation.GetCommit(head);
            string mergeBase   = this.GetMergeBase(other, head);
            var    otherCommit = this.commitOperation.GetCommit(other);

            if (mergeBase == head)
            {
                this.treeOperation.ReadTree(otherCommit.Tree, true);
                this.dataProvider.UpdateRef("HEAD", RefValue.Create(false, other));
                Console.WriteLine("Fast-forwad, no need to commit");
                return;
            }

            this.dataProvider.UpdateRef("MERGE_HEAD", RefValue.Create(false, other));
            this.ReadTreeMerged(headCommit.Tree, otherCommit.Tree, true);
            Console.WriteLine("Merged in working tree\nPlease commit");
        }
Exemplo n.º 8
0
        /// <inheritdoc/>
        public void Fetch()
        {
            var refs = this.remoteDataProvider.GetRefsMapping(RemoteRefsBase);

            foreach (var oid in this.remoteCommitOperation.GetObjectHistory(refs.Values))
            {
                this.FetchObjectIfMissing(oid);
            }

            foreach (var entry in refs)
            {
                string remoteName = entry.Key;
                string value      = entry.Value;
                string refName    = Path.GetRelativePath(RemoteRefsBase, remoteName);
                this.localDataProvider.UpdateRef(
                    Path.Join(LocalRefsBase, refName),
                    RefValue.Create(false, value));
            }
        }
Exemplo n.º 9
0
        private (string, RefValue) GetRefInternal(string @ref, bool deref)
        {
            var    refPath = Path.Join(this.GitDirFullPath, @ref);
            string value   = null;

            if (this.fileOperator.TryRead(refPath, out var data))
            {
                value = data.Decode();
            }

            bool symbolic = !string.IsNullOrWhiteSpace(value) && value.StartsWith("ref:");

            if (symbolic)
            {
                value = value.Split(":")[1].Trim();
                if (deref)
                {
                    return(this.GetRefInternal(value, true));
                }
            }

            return(ValueTuple.Create(@ref, RefValue.Create(symbolic, value)));
        }
Exemplo n.º 10
0
        /// <inheritdoc/>
        public void Push(string refName)
        {
            var remoteRefs = this.remoteDataProvider.GetRefsMapping(string.Empty);

            remoteRefs.TryGetValue(refName, out string remoteRef);
            string localRef = this.localDataProvider.GetRef(refName).Value;

            if (!string.IsNullOrEmpty(remoteRef) && !this.IsAncestorOf(localRef, remoteRef))
            {
                throw new UgitException("Could not push");
            }

            IEnumerable <string> knowRemoteRefs = remoteRefs.Values.Where(oid => this.localDataProvider.ObjectExist(oid));
            HashSet <string>     remoteObjects  = new HashSet <string>(this.localCommitOperation.GetObjectHistory(knowRemoteRefs));
            HashSet <string>     localObjects   = new HashSet <string>(this.localCommitOperation.GetObjectHistory(new[] { localRef }));
            IEnumerable <string> objectsToPush  = localObjects.Except(remoteObjects);

            foreach (var oid in objectsToPush)
            {
                this.PushObject(oid);
            }

            this.remoteDataProvider.UpdateRef(refName, RefValue.Create(false, localRef));
        }
Exemplo n.º 11
0
        public void MergeTest()
        {
            dataProvider.Setup(d => d.GetRef("HEAD", true)).Returns(RefValue.Create(false, "head-tree-oid"));
            commitOperation.Setup(c => c.GetCommit("head-tree-oid")).Returns(new Commit
            {
                Tree = "head-tree-oid",
            });

            commitOperation.Setup(c => c.GetCommit("other-oid")).Returns(new Commit
            {
                Tree = "other-tree-oid",
            });

            commitOperation.Setup(c => c.GetCommitHistory(It.IsAny <IEnumerable <string> >())).Returns <IEnumerable <string> >((oids) =>
            {
                if (oids.ToArray()[0] == "other-oid")
                {
                    return(new string[] { "other-oid", "parent-oid" });
                }
                if (oids.ToArray()[0] == "head-tree-oid")
                {
                    return(new string[] { "head-tree-oid", "parent-oid" });
                }
                return(Array.Empty <string>());
            });

            this.dataProvider.Setup(d => d.UpdateRef("MERGE_HEAD", It.Is <RefValue>(r => !r.Symbolic && r.Value == "other-oid"), true));
            this.dataProvider.Setup(d => d.Index).Returns(new Tree());
            this.treeOperation.Setup(d => d.GetTree("head-tree-oid", "")).Returns(
                new Tree()
            {
                { "foo.txt", "foo-oid" },
                { "bar.txt", "bar-oid" }
            });
            this.treeOperation.Setup(d => d.GetTree("other-tree-oid", "")).Returns(
                new Tree()
            {
                { "foo.txt", "foo-oid" },
            });
            this.diff.Setup(d => d.MergeTree(It.IsAny <Tree>(),
                                             It.IsAny <Tree>())).Returns <Tree, Tree>((tree1, tree2) =>
            {
                if (tree1.Count == 2 &&
                    tree1.ContainsKey("foo.txt") &&
                    tree1.ContainsKey("bar.txt") &&
                    tree2.Count == 1 &&
                    tree2.ContainsKey("foo.txt"))
                {
                    return(new Tree()
                    {
                        { "foo.txt", "foo-oid" },
                        { "bar.txt", "bar-oid" }
                    });
                }

                throw new Exception();
            });
            mergeOperation.Merge("other-oid");
            dataProvider.VerifyAll();
            treeOperation.VerifyAll();
            commitOperation.VerifyAll();
            diff.VerifyAll();
        }
Exemplo n.º 12
0
 /// <inheritdoc/>
 public void Init()
 {
     this.dataProvider.Init();
     this.dataProvider.UpdateRef(Constants.HEAD, RefValue.Create(true, Path.Join(Constants.Refs, Constants.Heads, Constants.Master)));
 }
Exemplo n.º 13
0
        /// <inheritdoc/>
        public void Create(string name, string oid)
        {
            string @ref = Path.Join(Constants.Refs, Constants.Heads, name);

            this.dataProvider.UpdateRef(@ref, RefValue.Create(false, oid));
        }
Exemplo n.º 14
0
 /// <inheritdoc/>
 public void Reset(string oid)
 {
     this.dataProvider.UpdateRef(Constants.HEAD, RefValue.Create(false, oid));
 }