public ITreeLine[] FileSystemScanSubFolder(string path) { var entries = new DirectoryInfo(path).EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly).ToArray(); var lines = new List <ITreeLine>(entries.OfType <FileInfo>() .Where(x => x.FullName != Path.Combine(CodeFolder, ".git")) .Select(x => new { Content = File.ReadAllText(x.FullName), x.FullName }) .Select(x => new BlobTreeLine(new Id(ByteHelper.ComputeSha(x.Content)), new BlobNode(x.Content), x.FullName.Substring(CodeFolder.Length + 1)))); lines.AddRange(entries.OfType <DirectoryInfo>() .Select(x => MakeTreeTreeLine(EnsurePathEndsInSlash(x.FullName)))); return(lines.ToArray()); }
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 ITreeLine[] FileSystemScanSubFolder(string path) { var entries = new DirectoryInfo(path).EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly).ToArray(); var tree = new List <ITreeLine>(); tree.AddRange(entries.OfType <FileInfo>() .Select(x => new { Content = File.ReadAllText(x.FullName), x.FullName }) .Select(x => new BlobTreeLine(new Id(ByteHelper.ComputeSha(x.Content)), new BlobNode(x.Content), x.FullName.Substring(CodeFolder.Length)))); tree.AddRange(entries.OfType <DirectoryInfo>() .Where(x => !x.FullName.EndsWith(KBGitFolderName)) .Select(x => MakeTreeTreeLine(x.FullName))); return(tree.ToArray()); }
/// <summary> /// Equivalent to "git hash-object -w <file>" /// </summary> public static Id HashObject(object o) => new Id(ByteHelper.ComputeSha(o));