public static ObjectHash WriteFixedTree(string vcsPath, Tree tree) { var resultingTreeLines = new List <Tree.TreeLine>(); bool fixRequired = false; foreach (var treeLine in tree.Lines) { if (!treeLine.IsDirectory()) { resultingTreeLines.Add(treeLine); continue; } var childTree = GitObjectFactory.ReadTree(vcsPath, treeLine.Hash); var fixedTreeHash = WriteFixedTree(vcsPath, childTree); resultingTreeLines.Add(new Tree.TreeLine(treeLine.TextBytes, fixedTreeHash)); if (fixedTreeHash != childTree.Hash) { fixRequired = true; } } if (fixRequired || Tree.HasDuplicateLines(resultingTreeLines)) { tree = Tree.GetFixedTree(resultingTreeLines); HashContent.WriteObject(vcsPath, tree); } return(tree.Hash); }
private static ObjectHash RewriteRef(string vcsPath, string hash, string refName, Dictionary <ObjectHash, ObjectHash> rewrittenCommits) { var gitObject = GitObjectFactory.ReadGitObject(vcsPath, new ObjectHash(hash)); if (gitObject.Type == GitObjectType.Commit) { var path = Path.Combine(vcsPath, refName); var correctedHash = GetRewrittenCommitHash(new ObjectHash(hash), rewrittenCommits); Directory.CreateDirectory(Path.GetDirectoryName(path)); File.WriteAllText(path, correctedHash.ToString()); return(correctedHash); } if (gitObject.Type == GitObjectType.Tag) { var tag = (Tag)gitObject; if (tag.PointsToTree) { // Do not touch tags pointing to trees right now as this is not properly implemented yet return(tag.Hash); } var rewrittenObjectHash = tag.PointsToTag ? RewriteRef(vcsPath, tag.Object, "", rewrittenCommits) : GetRewrittenCommitHash(new ObjectHash(tag.Object), rewrittenCommits); // points to commit var rewrittenTag = tag.WithNewObject(rewrittenObjectHash.ToString()); HashContent.WriteObject(vcsPath, rewrittenTag); var path = Path.Combine(vcsPath, "refs/tags", rewrittenTag.TagName); File.WriteAllText(path, rewrittenTag.Hash.ToString()); return(rewrittenTag.Hash); } throw new NotImplementedException(); }