Ejemplo n.º 1
0
        /// <summary>
        /// Returns all the files at a specific commit by listing the file tree
        /// </summary>
        /// <param name="tree">The file tree</param>
        /// <param name="currentPath">The initial file path. (Default is "")</param>
        /// <param name="repo">The repository containing the file tree</param>
        /// <returns>List of nodes that are in the file tree.</returns>
        public IReadOnlyList <GitFileInfo> ListTree(LibGit2Sharp.Tree tree, string currentPath, LibGit2Sharp.Repository repo)
        {
            var files = new List <GitFileInfo>();

            foreach (var node in tree)
            {
                var nodePath = string.IsNullOrWhiteSpace(currentPath) ?
                               node.Path :
                               Path.Combine(currentPath, node.Path);

                if (node.TargetType == LibGit2Sharp.TreeEntryTargetType.Tree)
                {
                    files.AddRange(
                        ListTree(
                            (LibGit2Sharp.Tree)repo.Lookup(node.Target.Id),
                            nodePath,
                            repo));
                    continue;
                }

                // Only handle files
                if (node.TargetType != LibGit2Sharp.TreeEntryTargetType.Blob)
                {
                    continue;
                }

                var blobSize = ((LibGit2Sharp.Blob)node.Target).Size;
                var nodeInfo = new GitFileInfo(nodePath, blobSize);
                files.Add(nodeInfo);
            }

            return(files);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Retrives the tree changes for the given commit.
        /// </summary>
        /// <returns></returns>
        public static LibGit2Sharp.TreeChanges GetTreeChangesForCommit(LibGit2Sharp.Repository repo, Commit commit)
        {
            // Retrieve the Tree for this commit.
            LibGit2Sharp.Tree thisTree = ((LibGit2Sharp.Commit)repo.Lookup(commit.ObjectId)).Tree;

            // Retrieve the Tree for the previous commit (parent).
            // TODO: What about Merge commits?
            LibGit2Sharp.Tree parentTree = ((LibGit2Sharp.Commit)repo.Lookup(commit.Parents.ElementAt(0).ObjectId)).Tree;

            // Take the diff.
            return(repo.Diff.Compare(parentTree, thisTree));
        }