Skip to content

Commit 98744c8

Browse files
authored
Add sparse checkout support (#772)
1 parent 7747c71 commit 98744c8

File tree

16 files changed

+139
-2
lines changed

16 files changed

+139
-2
lines changed

src/Microsoft.Build.Tasks.Git.UnitTests/GitRepositoryTests.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,32 @@ public void OpenRepository_WorkingDirectorySpecifiedInConfig()
209209
Assert.Equal(workingDir2.Path, repository.WorkingDirectory);
210210
}
211211

212+
[Fact]
213+
public void OpenRepository_SparseV2()
214+
{
215+
using var temp = new TempRoot();
216+
217+
var workingDir = temp.CreateDirectory();
218+
var gitDir = workingDir.CreateDirectory(".git");
219+
220+
gitDir.CreateFile("HEAD");
221+
gitDir.CreateFile("config").WriteAllText(@"
222+
[core]
223+
repositoryformatversion = 1
224+
[extensions]
225+
worktreeConfig = true");
226+
227+
Assert.True(GitRepository.TryFindRepository(gitDir.Path, out var location));
228+
Assert.Equal(gitDir.Path, location.CommonDirectory);
229+
Assert.Equal(gitDir.Path, location.GitDirectory);
230+
Assert.Null(location.WorkingDirectory);
231+
232+
var repository = GitRepository.OpenRepository(location, GitEnvironment.Empty);
233+
Assert.Equal(gitDir.Path, repository.CommonDirectory);
234+
Assert.Equal(gitDir.Path, repository.GitDirectory);
235+
Assert.Null(repository.WorkingDirectory);
236+
}
237+
212238
[Fact]
213239
public void OpenRepository_VersionNotSupported()
214240
{
@@ -223,7 +249,32 @@ public void OpenRepository_VersionNotSupported()
223249
gitDir.CreateDirectory("refs").CreateDirectory("heads").CreateFile("master").WriteAllText("0000000000000000000000000000000000000000");
224250
gitDir.CreateDirectory("objects");
225251

226-
gitDir.CreateFile("config").WriteAllText("[core]repositoryformatversion = 1");
252+
gitDir.CreateFile("config").WriteAllText("[core]repositoryformatversion = 2");
253+
254+
var src = workingDir.CreateDirectory("src");
255+
256+
Assert.Throws<NotSupportedException>(() => GitRepository.OpenRepository(src.Path, new GitEnvironment(homeDir.Path)));
257+
}
258+
259+
[Fact]
260+
public void OpenRepository_V2ExtensionNotSupported()
261+
{
262+
using var temp = new TempRoot();
263+
264+
var homeDir = temp.CreateDirectory();
265+
266+
var workingDir = temp.CreateDirectory();
267+
var gitDir = workingDir.CreateDirectory(".git");
268+
269+
gitDir.CreateFile("HEAD").WriteAllText("ref: refs/heads/master");
270+
gitDir.CreateDirectory("refs").CreateDirectory("heads").CreateFile("master").WriteAllText("0000000000000000000000000000000000000000");
271+
gitDir.CreateDirectory("objects");
272+
273+
gitDir.CreateFile("config").WriteAllText(@"
274+
[core]
275+
repositoryformatversion = 1
276+
[extensions]
277+
newExtension = true");
227278

228279
var src = workingDir.CreateDirectory("src");
229280

src/Microsoft.Build.Tasks.Git/GitDataReader/GitRepository.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.Build.Tasks.Git
1313
{
1414
internal sealed class GitRepository
1515
{
16-
private const int SupportedGitRepoFormatVersion = 0;
16+
private const int SupportedGitRepoFormatVersion = 1;
1717

1818
private const string CommonDirFileName = "commondir";
1919
private const string GitDirName = ".git";
@@ -23,6 +23,8 @@ internal sealed class GitRepository
2323
internal const string GitHeadFileName = "HEAD";
2424

2525
private const string GitModulesFileName = ".gitmodules";
26+
private static readonly string[] KnownExtensions = new[] { "noop", "preciousObjects", "partialclone", "worktreeConfig" };
27+
2628

2729
public GitConfig Config { get; }
2830

@@ -124,6 +126,22 @@ public static GitRepository OpenRepository(GitRepositoryLocation location, GitEn
124126
throw new NotSupportedException(string.Format(Resources.UnsupportedRepositoryVersion, versionStr, SupportedGitRepoFormatVersion));
125127
}
126128

129+
if (version == 1)
130+
{
131+
// When reading the core.repositoryformatversion variable, a git implementation which supports version 1 MUST
132+
// also read any configuration keys found in the extensions section of the configuration file.
133+
//
134+
// If a version-1 repository specifies any extensions.* keys that the running git has not implemented, the operation MUST NOT proceed.
135+
// Similarly, if the value of any known key is not understood by the implementation,the operation MUST NOT proceed.
136+
foreach (var variable in config.Variables)
137+
{
138+
if (variable.Key.SectionNameEquals("extensions") && !KnownExtensions.Contains(variable.Key.VariableName, StringComparer.OrdinalIgnoreCase))
139+
{
140+
throw new NotSupportedException(string.Format(Resources.UnsupportedRepositoryExtension, variable.Key.VariableName, string.Join(", ", KnownExtensions)));
141+
}
142+
}
143+
}
144+
127145
return new GitRepository(environment, config, location.GitDirectory, location.CommonDirectory, workingDirectory);
128146
}
129147

src/Microsoft.Build.Tasks.Git/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@
129129
<data name="UnsupportedRepositoryVersion" xml:space="preserve">
130130
<value>Unsupported repository version {0}. Only versions up to {1} are supported.</value>
131131
</data>
132+
<data name="UnsupportedRepositoryExtension" xml:space="preserve">
133+
<value>Unsupported repository extension {0}. Only {1} are supported.</value>
134+
</data>
132135
<data name="PathSpecifiedInFileIsNotAbsolute" xml:space="preserve">
133136
<value>Path specified in file '{0}' is not absolute: '{1}'.</value>
134137
</data>

src/Microsoft.Build.Tasks.Git/xlf/Resources.cs.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
<target state="translated">Nejde najít úložiště s pracovním adresářem, který obsahuje adresář {0}.</target>
118118
<note />
119119
</trans-unit>
120+
<trans-unit id="UnsupportedRepositoryExtension">
121+
<source>Unsupported repository extension {0}. Only {1} are supported.</source>
122+
<target state="new">Unsupported repository extension {0}. Only {1} are supported.</target>
123+
<note />
124+
</trans-unit>
120125
<trans-unit id="UnsupportedRepositoryVersion">
121126
<source>Unsupported repository version {0}. Only versions up to {1} are supported.</source>
122127
<target state="translated">Nepodporovaná verze úložiště: {0}. Podporují se jenom verze do {1}.</target>

src/Microsoft.Build.Tasks.Git/xlf/Resources.de.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
<target state="translated">Das Repository mit dem Arbeitsverzeichnis, welches das Verzeichnis "{0}" enthält, wurde nicht gefunden.</target>
118118
<note />
119119
</trans-unit>
120+
<trans-unit id="UnsupportedRepositoryExtension">
121+
<source>Unsupported repository extension {0}. Only {1} are supported.</source>
122+
<target state="new">Unsupported repository extension {0}. Only {1} are supported.</target>
123+
<note />
124+
</trans-unit>
120125
<trans-unit id="UnsupportedRepositoryVersion">
121126
<source>Unsupported repository version {0}. Only versions up to {1} are supported.</source>
122127
<target state="translated">Nicht unterstützte Repositoryversion {0}. Nur Versionen bis {1} werden unterstützt.</target>

src/Microsoft.Build.Tasks.Git/xlf/Resources.es.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
<target state="translated">No se puede encontrar el repositorio con el directorio de trabajo que contiene el directorio "{0}".</target>
118118
<note />
119119
</trans-unit>
120+
<trans-unit id="UnsupportedRepositoryExtension">
121+
<source>Unsupported repository extension {0}. Only {1} are supported.</source>
122+
<target state="new">Unsupported repository extension {0}. Only {1} are supported.</target>
123+
<note />
124+
</trans-unit>
120125
<trans-unit id="UnsupportedRepositoryVersion">
121126
<source>Unsupported repository version {0}. Only versions up to {1} are supported.</source>
122127
<target state="translated">Versión de repositorio {0} no admitida. Solo se admiten las versiones hasta {1}.</target>

src/Microsoft.Build.Tasks.Git/xlf/Resources.fr.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
<target state="translated">Impossible de localiser le dépôt avec le répertoire de travail contenant le répertoire '{0}'.</target>
118118
<note />
119119
</trans-unit>
120+
<trans-unit id="UnsupportedRepositoryExtension">
121+
<source>Unsupported repository extension {0}. Only {1} are supported.</source>
122+
<target state="new">Unsupported repository extension {0}. Only {1} are supported.</target>
123+
<note />
124+
</trans-unit>
120125
<trans-unit id="UnsupportedRepositoryVersion">
121126
<source>Unsupported repository version {0}. Only versions up to {1} are supported.</source>
122127
<target state="translated">La version du dépôt {0} n'est pas prise en charge. Seules les versions jusqu'à {1} sont prises en charge.</target>

src/Microsoft.Build.Tasks.Git/xlf/Resources.it.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
<target state="translated">Non è possibile individuare il repository con la directory di lavoro che contiene la directory '{0}'.</target>
118118
<note />
119119
</trans-unit>
120+
<trans-unit id="UnsupportedRepositoryExtension">
121+
<source>Unsupported repository extension {0}. Only {1} are supported.</source>
122+
<target state="new">Unsupported repository extension {0}. Only {1} are supported.</target>
123+
<note />
124+
</trans-unit>
120125
<trans-unit id="UnsupportedRepositoryVersion">
121126
<source>Unsupported repository version {0}. Only versions up to {1} are supported.</source>
122127
<target state="translated">La versione {0} del repository non è supportata. Sono supportate solo le versioni fino alla {1}.</target>

src/Microsoft.Build.Tasks.Git/xlf/Resources.ja.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
<target state="translated">ディレクトリ '{0}' を含む作業ディレクトリがあるリポジトリが見つかりません。</target>
118118
<note />
119119
</trans-unit>
120+
<trans-unit id="UnsupportedRepositoryExtension">
121+
<source>Unsupported repository extension {0}. Only {1} are supported.</source>
122+
<target state="new">Unsupported repository extension {0}. Only {1} are supported.</target>
123+
<note />
124+
</trans-unit>
120125
<trans-unit id="UnsupportedRepositoryVersion">
121126
<source>Unsupported repository version {0}. Only versions up to {1} are supported.</source>
122127
<target state="translated">サポートされていないリポジトリ バージョン {0}。{1} までのバージョンのみがサポートされています。</target>

src/Microsoft.Build.Tasks.Git/xlf/Resources.ko.xlf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
<target state="translated">디렉터리 '{0}'이(가) 포함된 작업 디렉터리로 리포지토리를 찾을 수 없습니다.</target>
118118
<note />
119119
</trans-unit>
120+
<trans-unit id="UnsupportedRepositoryExtension">
121+
<source>Unsupported repository extension {0}. Only {1} are supported.</source>
122+
<target state="new">Unsupported repository extension {0}. Only {1} are supported.</target>
123+
<note />
124+
</trans-unit>
120125
<trans-unit id="UnsupportedRepositoryVersion">
121126
<source>Unsupported repository version {0}. Only versions up to {1} are supported.</source>
122127
<target state="translated">{0}은(는) 지원되지 않는 리포지토리 버전입니다. 버전은 최대 {1}까지만 지원됩니다.</target>

0 commit comments

Comments
 (0)