Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ArchiverBase.cs « LibGit2Sharp - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8268c107553c4c307fd107608cbb14f435134e37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections.Generic;
using System.IO;

namespace LibGit2Sharp
{
    /// <summary>
    /// 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, ...).
    /// <para>
    ///   <see cref="ObjectDatabase.Archive(LibGit2Sharp.Commit,LibGit2Sharp.ArchiverBase)"/>
    /// </para>
    /// </summary>
    public abstract class ArchiverBase
    {
        /// <summary>
        /// Override this method to perform operations before the archiving of each entry of the tree takes place.
        /// </summary>
        /// <param name="tree">The tree that will be archived</param>
        /// <param name="oid">The ObjectId of the commit being archived, or null if there is no commit.</param>
        /// <param name="modificationTime">The modification time that will be used for the files in the archive.</param>
        public virtual void BeforeArchiving(Tree tree, ObjectId oid, DateTimeOffset modificationTime)
        { }

        /// <summary>
        /// Override this method to perform operations after the archiving of each entry of the tree took place.
        /// </summary>
        /// <param name="tree">The tree that was archived</param>
        /// <param name="oid">The ObjectId of the commit being archived, or null if there is no commit.</param>
        /// <param name="modificationTime">The modification time that was used for the files in the archive.</param>
        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<TreeEntry> 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);
                }
            }
        }

        /// <summary>
        /// Implements the archiving of a TreeEntry in a given format.
        /// </summary>
        /// <param name="path">The path of the entry in the archive.</param>
        /// <param name="entry">The entry to archive.</param>
        /// <param name="modificationTime">The datetime the entry was last modified.</param>
        protected abstract void AddTreeEntry(string path, TreeEntry entry, DateTimeOffset modificationTime);
    }
}