示例#1
0
        public bool Remove(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (IsValidName(name))
            {
                if (_items.Remove(name))
                {
                    Updated();
                    return(true);
                }
                return(false);
            }
            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
                    {
                        return(false);
                    }
                }

                return(tw.Remove(p.Last()));
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(name), name, "Invalid name");
            }
        }
示例#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;
        }