|
| 1 | +using GVFS.Common.FileSystem; |
| 2 | +using GVFS.Common.Git; |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | + |
| 7 | +namespace GVFS.Common |
| 8 | +{ |
| 9 | + public class EnlistmentHydrationSummary |
| 10 | + { |
| 11 | + public int HydratedFileCount { get; private set; } |
| 12 | + public int TotalFileCount { get; private set; } |
| 13 | + public int HydratedFolderCount { get; private set; } |
| 14 | + public int TotalFolderCount { get; private set; } |
| 15 | + |
| 16 | + public bool IsValid |
| 17 | + { |
| 18 | + get |
| 19 | + { |
| 20 | + return HydratedFileCount >= 0 |
| 21 | + && HydratedFolderCount >= 0 |
| 22 | + && TotalFileCount >= HydratedFileCount |
| 23 | + && TotalFolderCount >= HydratedFolderCount; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public string ToMessage() |
| 28 | + { |
| 29 | + if (!IsValid) |
| 30 | + { |
| 31 | + return "Error calculating hydration. Run 'gvfs health' for details."; |
| 32 | + } |
| 33 | + |
| 34 | + int fileHydrationPercent = TotalFileCount == 0 ? 0 : (100 * HydratedFileCount) / TotalFileCount; |
| 35 | + int folderHydrationPercent = TotalFolderCount == 0 ? 0 : ((100 * HydratedFolderCount) / TotalFolderCount); |
| 36 | + return $"{fileHydrationPercent}% of files and {folderHydrationPercent}% of folders hydrated. Run 'gvfs health' for details."; |
| 37 | + } |
| 38 | + |
| 39 | + public static EnlistmentHydrationSummary CreateSummary( |
| 40 | + GVFSEnlistment enlistment, |
| 41 | + PhysicalFileSystem fileSystem) |
| 42 | + { |
| 43 | + try |
| 44 | + { |
| 45 | + /* Getting all the file paths from git index is slow and we only need the total count, |
| 46 | + * so we read the index file header instead of calling GetPathsFromGitIndex */ |
| 47 | + int totalFileCount = GetIndexFileCount(enlistment, fileSystem); |
| 48 | + |
| 49 | + /* Getting all the directories is also slow, but not as slow as reading the entire index, |
| 50 | + * GetTotalPathCount caches the count so this is only slow occasionally, |
| 51 | + * and the GitStatusCache manager also calls this to ensure it is updated frequently. */ |
| 52 | + int totalFolderCount = GetHeadTreeCount(enlistment, fileSystem); |
| 53 | + |
| 54 | + EnlistmentPathData pathData = new EnlistmentPathData(); |
| 55 | + |
| 56 | + /* FUTURE: These could be optimized to only deal with counts instead of full path lists */ |
| 57 | + pathData.LoadPlaceholdersFromDatabase(enlistment); |
| 58 | + pathData.LoadModifiedPaths(enlistment); |
| 59 | + |
| 60 | + int hydratedFileCount = pathData.ModifiedFilePaths.Count + pathData.PlaceholderFilePaths.Count; |
| 61 | + int hydratedFolderCount = pathData.ModifiedFolderPaths.Count + pathData.PlaceholderFolderPaths.Count; |
| 62 | + return new EnlistmentHydrationSummary() |
| 63 | + { |
| 64 | + HydratedFileCount = hydratedFileCount, |
| 65 | + HydratedFolderCount = hydratedFolderCount, |
| 66 | + TotalFileCount = totalFileCount, |
| 67 | + TotalFolderCount = totalFolderCount, |
| 68 | + }; |
| 69 | + } |
| 70 | + catch |
| 71 | + { |
| 72 | + return new EnlistmentHydrationSummary() |
| 73 | + { |
| 74 | + HydratedFileCount = -1, |
| 75 | + HydratedFolderCount = -1, |
| 76 | + TotalFileCount = -1, |
| 77 | + TotalFolderCount = -1, |
| 78 | + }; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Get the total number of files in the index. |
| 84 | + /// </summary> |
| 85 | + internal static int GetIndexFileCount(GVFSEnlistment enlistment, PhysicalFileSystem fileSystem) |
| 86 | + { |
| 87 | + string indexPath = Path.Combine(enlistment.WorkingDirectoryBackingRoot, GVFSConstants.DotGit.Index); |
| 88 | + using (var indexFile = fileSystem.OpenFileStream(indexPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, callFlushFileBuffers: false)) |
| 89 | + { |
| 90 | + if (indexFile.Length < 12) |
| 91 | + { |
| 92 | + return -1; |
| 93 | + } |
| 94 | + /* The number of files in the index is a big-endian integer from |
| 95 | + * the 4 bytes at offsets 8-11 of the index file. */ |
| 96 | + indexFile.Position = 8; |
| 97 | + var bytes = new byte[4]; |
| 98 | + indexFile.Read( |
| 99 | + bytes, // Destination buffer |
| 100 | + offset: 0, // Offset in destination buffer, not in indexFile |
| 101 | + count: 4); |
| 102 | + if (BitConverter.IsLittleEndian) |
| 103 | + { |
| 104 | + Array.Reverse(bytes); |
| 105 | + } |
| 106 | + int count = BitConverter.ToInt32(bytes, 0); |
| 107 | + return count; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Get the total number of trees in the repo at HEAD. |
| 113 | + /// </summary> |
| 114 | + /// <remarks> |
| 115 | + /// This is used as the denominator in displaying percentage of hydrated |
| 116 | + /// directories as part of git status pre-command hook. |
| 117 | + /// It can take several seconds to calculate, so we cache it near the git status cache. |
| 118 | + /// </remarks> |
| 119 | + /// <returns> |
| 120 | + /// The number of subtrees at HEAD, which may be 0. |
| 121 | + /// Will return 0 if unsuccessful. |
| 122 | + /// </returns> |
| 123 | + internal static int GetHeadTreeCount(GVFSEnlistment enlistment, PhysicalFileSystem fileSystem) |
| 124 | + { |
| 125 | + var gitProcess = enlistment.CreateGitProcess(); |
| 126 | + var headResult = gitProcess.GetHeadTreeId(); |
| 127 | + if (headResult.ExitCodeIsFailure) |
| 128 | + { |
| 129 | + return 0; |
| 130 | + } |
| 131 | + var headSha = headResult.Output.Trim(); |
| 132 | + var cacheFile = Path.Combine( |
| 133 | + enlistment.DotGVFSRoot, |
| 134 | + GVFSConstants.DotGVFS.GitStatusCache.TreeCount); |
| 135 | + |
| 136 | + // Load from cache if cache matches current HEAD. |
| 137 | + if (fileSystem.FileExists(cacheFile)) |
| 138 | + { |
| 139 | + try |
| 140 | + { |
| 141 | + var lines = fileSystem.ReadLines(cacheFile).ToArray(); |
| 142 | + if (lines.Length == 2 |
| 143 | + && lines[0] == headSha |
| 144 | + && int.TryParse(lines[1], out int cachedCount)) |
| 145 | + { |
| 146 | + return cachedCount; |
| 147 | + } |
| 148 | + } |
| 149 | + catch |
| 150 | + { |
| 151 | + // Ignore errors reading the cache |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + int totalPathCount = 0; |
| 156 | + GitProcess.Result folderResult = gitProcess.LsTree( |
| 157 | + GVFSConstants.DotGit.HeadName, |
| 158 | + line => totalPathCount++, |
| 159 | + recursive: true, |
| 160 | + showDirectories: true); |
| 161 | + try |
| 162 | + { |
| 163 | + fileSystem.CreateDirectory(Path.GetDirectoryName(cacheFile)); |
| 164 | + fileSystem.WriteAllText(cacheFile, $"{headSha}\n{totalPathCount}"); |
| 165 | + } |
| 166 | + catch |
| 167 | + { |
| 168 | + // Ignore errors writing the cache |
| 169 | + } |
| 170 | + |
| 171 | + return totalPathCount; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments