예제 #1
0
        public static GitCommitWriter AsWriter(this GitCommit commit)
        {
            if (commit is null)
            {
                throw new ArgumentNullException(nameof(commit));
            }

            var cw = GitCommitWriter.CreateFromTree(commit.Tree.AsWriter());

            cw.Parents = commit.Parents.Select(x => (x ?? throw new InvalidOperationException()).AsWriter()).ToArray();


            cw.PutId(commit.Id); // TODO: Cleanup

            return(cw);
        }
예제 #2
0
        private static async ValueTask <GitCommitWriter> FillCommit(string message, Bucket source, Dictionary <string, GitId> marks, GitRepository repo)
        {
            List <GitCommit>?parents = null;

            GitCommitWriter gcw = GitCommitWriter.Create();

            gcw.Message = message;
            GitTree?tree = null;

            while (true)
            {
                var(bb, eol) = await source.ReadUntilEolFullAsync(BucketEol.LF).ConfigureAwait(false);

                if (bb.IsEof)
                {
                    return(gcw);
                }

                bb = bb.Trim(eol);
                if (bb.IsEmpty)
                {
                    break;
                }

                if (bb.StartsWithASCII("from "))
                {
                    string mark = bb.Slice(5).ToUTF8String();

                    var c = repo.Commits[marks[mark]] !;
                    parents ??= new();
                    parents.Insert(0, c);
                    tree     = c.Tree;
                    gcw.Tree = c.Tree.AsWriter();
                }
                else if (bb.StartsWithASCII("merge "))
                {
                    string mark = bb.Slice(6).ToUTF8String();

                    parents ??= new();
                    parents.Add(repo.Commits[marks[mark]] !);
                }
                else if (bb.StartsWithASCII("M "))
                {
                    var items = bb.Slice(2).ToUTF8String().Split(new[] { ' ' }, 3);

                    GitTreeElementType?fileType = (GitTreeElementType)Convert.ToInt32(items[0], 8);
                    string             mark     = items[1];
                    string             name     = items[2];
                    var b = repo.Blobs[marks[mark]] !;

#if NET6_0_OR_GREATER
                    if (!Enum.IsDefined <GitTreeElementType>(fileType.Value))
                    {
                        fileType = null;
                    }
#else
                    if (!Enum.IsDefined(typeof(GitTreeElementType), fileType))
                    {
                        fileType = null;
                    }
#endif

                    if (tree?.AllFiles.TryGet(name, out _) ?? false)
                    {
                        gcw.Tree.Replace(name, b, fileType);
                    }
                    else
                    {
                        gcw.Tree.Add(name, b, fileType);
                    }
                }
                else if (bb.StartsWithASCII("D "))
                {
                    string name = bb.Slice(2).ToUTF8String();
                    gcw.Tree.Remove(name);
                }
            }

            gcw.Parents = parents !;
            return(gcw);
        }