|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | + |
| 6 | +namespace LibGit2Sharp.Core |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Represents a file-related log of commits beyond renames. |
| 10 | + /// </summary> |
| 11 | + internal class FileHistory : IEnumerable<LogEntry> |
| 12 | + { |
| 13 | + #region Fields |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// The allowed commit sort strategies. |
| 17 | + /// </summary> |
| 18 | + private static readonly List<CommitSortStrategies> AllowedSortStrategies = new List<CommitSortStrategies> |
| 19 | + { |
| 20 | + CommitSortStrategies.Topological, |
| 21 | + CommitSortStrategies.Time, |
| 22 | + CommitSortStrategies.Topological | CommitSortStrategies.Time |
| 23 | + }; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The repository. |
| 27 | + /// </summary> |
| 28 | + private readonly Repository _repo; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// The file's path relative to the repository's root. |
| 32 | + /// </summary> |
| 33 | + private readonly string _path; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// The filter to be used in querying the commit log. |
| 37 | + /// </summary> |
| 38 | + private readonly CommitFilter _queryFilter; |
| 39 | + |
| 40 | + #endregion |
| 41 | + |
| 42 | + #region Constructors |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Initializes a new instance of the <see cref="FileHistory"/> class. |
| 46 | + /// The commits will be enumerated in reverse chronological order. |
| 47 | + /// </summary> |
| 48 | + /// <param name="repo">The repository.</param> |
| 49 | + /// <param name="path">The file's path relative to the repository's root.</param> |
| 50 | + /// <exception cref="ArgumentNullException">If any of the parameters is null.</exception> |
| 51 | + internal FileHistory(Repository repo, string path) |
| 52 | + : this(repo, path, new CommitFilter()) |
| 53 | + { } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Initializes a new instance of the <see cref="FileHistory"/> class. |
| 57 | + /// The given <see cref="CommitFilter"/> instance specifies the commit |
| 58 | + /// sort strategies and range of commits to be considered. |
| 59 | + /// Only the time (corresponding to <code>--date-order</code>) and topological |
| 60 | + /// (coresponding to <code>--topo-order</code>) sort strategies are supported. |
| 61 | + /// </summary> |
| 62 | + /// <param name="repo">The repository.</param> |
| 63 | + /// <param name="path">The file's path relative to the repository's root.</param> |
| 64 | + /// <param name="queryFilter">The filter to be used in querying the commit log.</param> |
| 65 | + /// <exception cref="ArgumentNullException">If any of the parameters is null.</exception> |
| 66 | + /// <exception cref="ArgumentException">When an unsupported commit sort strategy is specified.</exception> |
| 67 | + internal FileHistory(Repository repo, string path, CommitFilter queryFilter) |
| 68 | + { |
| 69 | + Ensure.ArgumentNotNull(repo, "repo"); |
| 70 | + Ensure.ArgumentNotNull(path, "path"); |
| 71 | + Ensure.ArgumentNotNull(queryFilter, "queryFilter"); |
| 72 | + |
| 73 | + // Ensure the commit sort strategy makes sense. |
| 74 | + if (!AllowedSortStrategies.Contains(queryFilter.SortBy)) |
| 75 | + throw new ArgumentException( |
| 76 | + "Unsupported sort strategy. Only 'Topological', 'Time', or 'Topological | Time' are allowed.", |
| 77 | + "queryFilter"); |
| 78 | + |
| 79 | + _repo = repo; |
| 80 | + _path = path; |
| 81 | + _queryFilter = queryFilter; |
| 82 | + } |
| 83 | + |
| 84 | + #endregion |
| 85 | + |
| 86 | + #region IEnumerable<LogEntry> Members |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Gets the <see cref="IEnumerator{LogEntry}"/> that enumerates the |
| 90 | + /// <see cref="LogEntry"/> instances representing the file's history, |
| 91 | + /// including renames (as in <code>git log --follow</code>). |
| 92 | + /// </summary> |
| 93 | + /// <returns>A <see cref="IEnumerator{LogEntry}"/>.</returns> |
| 94 | + public IEnumerator<LogEntry> GetEnumerator() |
| 95 | + { |
| 96 | + return FullHistory(_repo, _path, _queryFilter).GetEnumerator(); |
| 97 | + } |
| 98 | + |
| 99 | + IEnumerator IEnumerable.GetEnumerator() |
| 100 | + { |
| 101 | + return GetEnumerator(); |
| 102 | + } |
| 103 | + |
| 104 | + #endregion |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Gets the relevant commits in which the given file was created, changed, or renamed. |
| 108 | + /// </summary> |
| 109 | + /// <param name="repo">The repository.</param> |
| 110 | + /// <param name="path">The file's path relative to the repository's root.</param> |
| 111 | + /// <param name="filter">The filter to be used in querying the commits log.</param> |
| 112 | + /// <returns>A collection of <see cref="LogEntry"/> instances.</returns> |
| 113 | + private static IEnumerable<LogEntry> FullHistory(IRepository repo, string path, CommitFilter filter) |
| 114 | + { |
| 115 | + var map = new Dictionary<Commit, string>(); |
| 116 | + |
| 117 | + foreach (var currentCommit in repo.Commits.QueryBy(filter)) |
| 118 | + { |
| 119 | + var currentPath = map.Keys.Count > 0 ? map[currentCommit] : path; |
| 120 | + var currentTreeEntry = currentCommit.Tree[currentPath]; |
| 121 | + |
| 122 | + if (currentTreeEntry == null) |
| 123 | + { |
| 124 | + yield break; |
| 125 | + } |
| 126 | + |
| 127 | + var parentCount = currentCommit.Parents.Count(); |
| 128 | + if (parentCount == 0) |
| 129 | + { |
| 130 | + yield return new LogEntry { Path = currentPath, Commit = currentCommit }; |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + DetermineParentPaths(repo, currentCommit, currentPath, map); |
| 135 | + |
| 136 | + if (parentCount != 1) |
| 137 | + { |
| 138 | + continue; |
| 139 | + } |
| 140 | + |
| 141 | + var parentCommit = currentCommit.Parents.Single(); |
| 142 | + var parentPath = map[parentCommit]; |
| 143 | + var parentTreeEntry = parentCommit.Tree[parentPath]; |
| 144 | + |
| 145 | + if (parentTreeEntry == null || |
| 146 | + parentTreeEntry.Target.Id != currentTreeEntry.Target.Id || |
| 147 | + parentPath != currentPath) |
| 148 | + { |
| 149 | + yield return new LogEntry { Path = currentPath, Commit = currentCommit }; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private static void DetermineParentPaths(IRepository repo, Commit currentCommit, string currentPath, IDictionary<Commit, string> map) |
| 156 | + { |
| 157 | + foreach (var parentCommit in currentCommit.Parents.Where(parentCommit => !map.ContainsKey(parentCommit))) |
| 158 | + { |
| 159 | + map.Add(parentCommit, ParentPath(repo, currentCommit, currentPath, parentCommit)); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private static string ParentPath(IRepository repo, Commit currentCommit, string currentPath, Commit parentCommit) |
| 164 | + { |
| 165 | + var treeChanges = repo.Diff.Compare<TreeChanges>(parentCommit.Tree, currentCommit.Tree); |
| 166 | + var treeEntryChanges = treeChanges.FirstOrDefault(c => c.Path == currentPath); |
| 167 | + return treeEntryChanges != null && treeEntryChanges.Status == ChangeKind.Renamed |
| 168 | + ? treeEntryChanges.OldPath |
| 169 | + : currentPath; |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments