public Id Commit(string message, string author, DateTime now, params Fileinfo[] fileinfo) { var blobsInCommit = fileinfo.Select(x => new { file = x, blobid = new Id(ByteHelper.ComputeSha(x.Content)), blob = new BlobNode(x.Content) }).ToArray(); var treeNode = new TreeNode(blobsInCommit.Select(x => new BlobTreeLine(x.blobid, x.blob, x.file.Path)).ToArray()); var treeNodeId = Id.HashObject(treeNode); var parentCommitId = Hd.Head.GetId(Hd); var isFirstCommit = parentCommitId == null; var commit = new CommitNode { Time = now, Tree = treeNode, TreeId = treeNodeId, Author = author, Message = message, Parents = isFirstCommit ? new Id[0] : new[] { parentCommitId }, }; if (!Hd.Trees.ContainsKey(treeNodeId)) { Hd.Trees.Add(treeNodeId, treeNode); } foreach (var blob in blobsInCommit.Where(x => !Hd.Blobs.ContainsKey(x.blobid))) { Hd.Blobs.Add(blob.blobid, blob.blob); } var commitId = Id.HashObject(commit); Hd.Commits.Add(commitId, commit); if (Hd.Head.IsDetachedHead()) { Hd.Head.Update(commitId, Hd); } else { Hd.Branches[Hd.Head.Branch].Tip = commitId; } return(commitId); }
public Id Commit(string message, string author, DateTime now) { var composite = FileSystemScanFolder(CodeFolder); composite.Visit(x => { if (x is TreeTreeLine t) { Hd.Trees.TryAdd(t.Id, t.Tree); } if (x is BlobTreeLine b) { Hd.Blobs.TryAdd(b.Id, b.Blob); } }); var parentCommitId = Hd.Head.GetId(Hd); var isFirstCommit = parentCommitId == null; var commit = new CommitNode { Time = now, Tree = composite.Tree, TreeId = composite.Id, Author = author, Message = message, Parents = isFirstCommit ? new Id[0] : new[] { parentCommitId }, }; var commitId = Id.HashObject(commit); Hd.Commits.Add(commitId, commit); if (Hd.Head.IsDetachedHead()) { Hd.Head.Update(commitId, Hd); } else { Hd.Branches[Hd.Head.Branch].Tip = commitId; } return(commitId); }