using System; using System.Collections.Generic; using System.IO; namespace LibGit2Sharp { /// /// The archiving method needs to be passed an inheritor of this class, which will then be used /// to provide low-level archiving facilities (tar, zip, ...). /// /// /// /// public abstract class ArchiverBase { /// /// Override this method to perform operations before the archiving of each entry of the tree takes place. /// /// The tree that will be archived /// The ObjectId of the commit being archived, or null if there is no commit. /// The modification time that will be used for the files in the archive. public virtual void BeforeArchiving(Tree tree, ObjectId oid, DateTimeOffset modificationTime) { } /// /// Override this method to perform operations after the archiving of each entry of the tree took place. /// /// The tree that was archived /// The ObjectId of the commit being archived, or null if there is no commit. /// The modification time that was used for the files in the archive. public virtual void AfterArchiving(Tree tree, ObjectId oid, DateTimeOffset modificationTime) { } internal void OrchestrateArchiving(Tree tree, ObjectId oid, DateTimeOffset modificationTime) { BeforeArchiving(tree, oid, modificationTime); ArchiveTree(tree, "", modificationTime); AfterArchiving(tree, oid, modificationTime); } private void ArchiveTree(IEnumerable tree, string path, DateTimeOffset modificationTime) { foreach (var entry in tree) { AddTreeEntry(Path.Combine(path, entry.Name), entry, modificationTime); // Recurse if we have subtrees if (entry.Mode == Mode.Directory) { ArchiveTree((Tree)entry.Target, Path.Combine(path, entry.Name), modificationTime); } } } /// /// Implements the archiving of a TreeEntry in a given format. /// /// The path of the entry in the archive. /// The entry to archive. /// The datetime the entry was last modified. protected abstract void AddTreeEntry(string path, TreeEntry entry, DateTimeOffset modificationTime); } }