Пример #1
0
        public static GitTreeWriter AsWriter(this GitTree tree)
        {
            if (tree is null)
            {
                throw new ArgumentNullException(nameof(tree));
            }

            var gtw = GitTreeWriter.CreateEmpty();

            foreach (var v in tree)
            {
                switch (v.ElementType)
                {
                case GitTreeElementType.File:
                case GitTreeElementType.FileExecutable:
                case GitTreeElementType.Directory:
                    gtw.Add(v.Name, v.GitObject.AsLazy());
                    break;

                case GitTreeElementType.SymbolicLink:
                case GitTreeElementType.GitCommitLink:
                    throw new NotImplementedException();
                }
            }
            gtw.PutId(tree.Id); // TODO: Cleanup
            return(gtw);
        }
Пример #2
0
        public void Add <TGitObject>(string name, IGitLazy <TGitObject> item, GitTreeElementType?setType = null)
            where TGitObject : GitObject
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            else if (item is null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            if (typeof(TGitObject) == typeof(GitObject))
            {
                typeof(GitTreeWriter).GetMethod(nameof(Add)) !.MakeGenericMethod(item.Type.AsType()).Invoke(this, new object?[] { name, item, setType });
                return;
            }

            if (IsValidName(name))
            {
                if (_items.ContainsKey(name))
                {
                    throw new ArgumentOutOfRangeException(nameof(name), $"Entry with name '{name}' already exists");
                }

                _items.Add(name, new Item <TGitObject>(name, item, setType));
            }
            else if (name.Contains('/', StringComparison.Ordinal))
            {
                var           p  = name.Split('/');
                GitTreeWriter tw = this;

                foreach (var si in p.Take(p.Length - 1))
                {
                    if (tw._items.TryGetValue(si, out var v))
                    {
                        if (v.Writer is GitTreeWriter subTw)
                        {
                            tw = subTw;
                        }
                        else if (v.Lazy is GitTree)
                        {
                            tw = (GitTreeWriter)v.EnsureWriter();
                        }
                        else
                        {
                            throw new InvalidOperationException();
                        }

                        tw.Updated();
                    }
                    else
                    {
                        var stw = GitTreeWriter.CreateEmpty();
                        tw.Add(si, stw, null);
                        tw = stw;
                    }
                }

                tw.Add(p.Last(), item, setType);
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(name), name, "Invalid name");
            }

            Id = null;
        }