-
-
Notifications
You must be signed in to change notification settings - Fork 166
/
ManagedVersionFile.cs
186 lines (163 loc) · 8.5 KB
/
ManagedVersionFile.cs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#nullable enable
namespace Nerdbank.GitVersioning.Managed
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Nerdbank.GitVersioning;
using Nerdbank.GitVersioning.ManagedGit;
using Newtonsoft.Json;
using Validation;
/// <summary>
/// Exposes queries and mutations on a version.json or version.txt file,
/// implemented in terms of our private managed git implementation.
/// </summary>
internal class ManagedVersionFile : VersionFile
{
/// <summary>
/// The filename of the version.txt file, as a byte array.
/// </summary>
private static readonly byte[] TxtFileNameBytes = Encoding.ASCII.GetBytes(TxtFileName);
/// <summary>
/// The filename of the version.json file, as a byte array.
/// </summary>
private static readonly byte[] JsonFileNameBytes = Encoding.ASCII.GetBytes(JsonFileName);
/// <summary>
/// Initializes a new instance of the <see cref="ManagedVersionFile"/> class.
/// </summary>
/// <param name="context"><inheritdoc /></param>
public ManagedVersionFile(GitContext context)
: base(context)
{
}
protected new ManagedGitContext Context => (ManagedGitContext)base.Context;
protected override VersionOptions? GetVersionCore(out string? actualDirectory) => this.GetVersion(this.Context.Commit!.Value, this.Context.RepoRelativeProjectDirectory, null, out actualDirectory);
/// <summary>
/// Reads the version.json file and returns the <see cref="VersionOptions"/> deserialized from it.
/// </summary>
/// <param name="commit">The commit to read from.</param>
/// <param name="repoRelativeProjectDirectory">The directory to consider when searching for the version.txt file.</param>
/// <param name="blobVersionCache">An optional blob cache for storing the raw parse results of a version.txt or version.json file (before any inherit merge operations are applied).</param>
/// <param name="actualDirectory">Receives the full path to the directory in which the version file was found.</param>
/// <returns>The version information read from the file.</returns>
internal VersionOptions? GetVersion(GitCommit commit, string repoRelativeProjectDirectory, Dictionary<GitObjectId, VersionOptions?>? blobVersionCache, out string? actualDirectory)
{
Stack<string> directories = new Stack<string>();
string? currentDirectory = repoRelativeProjectDirectory;
while (!string.IsNullOrEmpty(currentDirectory))
{
directories.Push(Path.GetFileName(currentDirectory));
currentDirectory = Path.GetDirectoryName(currentDirectory);
}
GitObjectId tree = commit.Tree;
string? searchDirectory = string.Empty;
string? parentDirectory = null;
VersionOptions? finalResult = null;
actualDirectory = null;
while (tree != GitObjectId.Empty)
{
var versionTxtBlob = this.Context.Repository.GetTreeEntry(tree, TxtFileNameBytes);
if (versionTxtBlob != GitObjectId.Empty)
{
if (blobVersionCache is null || !blobVersionCache.TryGetValue(versionTxtBlob, out VersionOptions? result))
{
result = TryReadVersionFile(new StreamReader(this.Context.Repository.GetObjectBySha(versionTxtBlob, "blob")!));
if (blobVersionCache is object)
{
result?.Freeze();
blobVersionCache.Add(versionTxtBlob, result);
}
}
if (result is object)
{
finalResult = result;
actualDirectory = Path.Combine(this.Context.WorkingTreePath, searchDirectory);
}
}
var versionJsonBlob = this.Context.Repository.GetTreeEntry(tree, JsonFileNameBytes);
if (versionJsonBlob != GitObjectId.Empty)
{
string? versionJsonContent = null;
if (blobVersionCache is null || !blobVersionCache.TryGetValue(versionJsonBlob, out VersionOptions? result))
{
using (var sr = new StreamReader(this.Context.Repository.GetObjectBySha(versionJsonBlob, "blob")!))
{
versionJsonContent = sr.ReadToEnd();
}
try
{
result = TryReadVersionJsonContent(versionJsonContent, searchDirectory);
}
catch (FormatException ex)
{
throw new FormatException(
$"Failure while reading {JsonFileName} from commit {this.Context.GitCommitId}. " +
"Fix this commit with rebase if this is an error, or review this doc on how to migrate to Nerdbank.GitVersioning: " +
"https://github.com/dotnet/Nerdbank.GitVersioning/blob/master/doc/migrating.md", ex);
}
if (blobVersionCache is object)
{
result?.Freeze();
blobVersionCache.Add(versionJsonBlob, result);
}
}
if (result?.Inherit ?? false)
{
if (parentDirectory is object)
{
result = this.GetVersion(commit, parentDirectory, blobVersionCache, out string? resultingDirectory);
if (result is object)
{
if (versionJsonContent is null)
{
// We reused a cache VersionOptions, but now we need the actual JSON string.
using (var sr = new StreamReader(this.Context.Repository.GetObjectBySha(versionJsonBlob, "blob")!))
{
versionJsonContent = sr.ReadToEnd();
}
}
if (result.IsFrozen)
{
result = new VersionOptions(result);
}
JsonConvert.PopulateObject(versionJsonContent, result, VersionOptions.GetJsonSettings(repoRelativeBaseDirectory: searchDirectory));
finalResult = result;
}
else
{
var candidatePath = Path.Combine(searchDirectory, JsonFileName);
throw new InvalidOperationException($"\"{candidatePath}\" inherits from a parent directory version.json file but none exists.");
}
}
else
{
var candidatePath = Path.Combine(searchDirectory, JsonFileName);
throw new InvalidOperationException($"\"{candidatePath}\" inherits from a parent directory version.json file but none exists.");
}
}
if (result is object)
{
actualDirectory = Path.Combine(this.Context.WorkingTreePath, searchDirectory);
finalResult = result;
}
}
if (directories.Count > 0)
{
var directoryName = directories.Pop();
tree = this.Context.Repository.GetTreeEntry(tree, GitRepository.Encoding.GetBytes(directoryName));
parentDirectory = searchDirectory;
searchDirectory = Path.Combine(searchDirectory, directoryName);
}
else
{
tree = GitObjectId.Empty;
parentDirectory = null;
searchDirectory = null;
break;
}
}
return finalResult;
}
}
}