-
Notifications
You must be signed in to change notification settings - Fork 66
Add SparseVerb --add #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using CommandLine; | ||
using Scalar.Common; | ||
using Scalar.Common.Git; | ||
using Scalar.Common.Http; | ||
using Scalar.Common.Tracing; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
|
||
namespace Scalar.CommandLine | ||
{ | ||
[Verb( | ||
SparseVerb.SparseVerbName, | ||
HelpText = @"Add to the list of folders that are included in git's sparse-checkout. | ||
Folders need to be relative to the repo's root directory.") | ||
] | ||
public class SparseVerb : ScalarVerb.ForExistingEnlistment | ||
{ | ||
private const string SparseVerbName = "sparse"; | ||
private const string FolderListSeparator = ";"; | ||
|
||
[Option( | ||
'a', | ||
"add", | ||
Required = false, | ||
Default = "", | ||
HelpText = "A semicolon-delimited list of repo root relative folders to include in the sparse-checkout. Wildcards are not supported.")] | ||
public string FoldersToAdd { get; set; } | ||
|
||
[Option( | ||
"add-stdin", | ||
Required = false, | ||
Default = false, | ||
HelpText = "Specify this flag to load folder list from stdin. Folders must be line-delimited and wildcards are not supported.")] | ||
public bool AddFromStdIn { get; set; } | ||
|
||
protected override string VerbName => SparseVerbName; | ||
|
||
protected override void Execute(ScalarEnlistment enlistment) | ||
{ | ||
using (JsonTracer tracer = new JsonTracer(ScalarConstants.ScalarEtwProviderName, SparseVerbName)) | ||
{ | ||
try | ||
{ | ||
CacheServerInfo cacheServer = CacheServerResolver.GetCacheServerFromConfig(enlistment); | ||
|
||
tracer.AddLogFileEventListener( | ||
ScalarEnlistment.GetNewScalarLogFileName(enlistment.ScalarLogsRoot, ScalarConstants.LogFileTypes.Sparse), | ||
EventLevel.Informational, | ||
Keywords.Any); | ||
tracer.WriteStartEvent( | ||
enlistment.EnlistmentRoot, | ||
enlistment.RepoUrl, | ||
cacheServer.Url, | ||
new EventMetadata | ||
{ | ||
{ "Unattended", this.Unattended }, | ||
{ "IsElevated", ScalarPlatform.Instance.IsElevated() }, | ||
{ "NamedPipeName", enlistment.NamedPipeName }, | ||
{ "ProcessID", Process.GetCurrentProcess().Id }, | ||
{ nameof(this.EnlistmentRootPathParameter), this.EnlistmentRootPathParameter }, | ||
{ nameof(this.FoldersToAdd), this.FoldersToAdd }, | ||
{ nameof(this.AddFromStdIn), this.AddFromStdIn }, | ||
}); | ||
|
||
if (!this.AddFromStdIn && string.IsNullOrWhiteSpace(this.FoldersToAdd)) | ||
{ | ||
this.ReportErrorAndExit(tracer, "You must specify folders to add with --add or --add-stdin"); | ||
} | ||
|
||
List<string> foldersToAdd = null; | ||
|
||
// Pre-fetch the blobs for the folders that will be added and | ||
// rely on PrefetchVerb for parsing the the list of folders | ||
ReturnCode result = this.Execute<PrefetchVerb>( | ||
enlistment, | ||
verb => | ||
{ | ||
verb.Commits = false; | ||
verb.ResolvedCacheServer = cacheServer; | ||
verb.SkipVersionCheck = false; | ||
verb.Files = string.Empty; | ||
verb.Folders = this.FoldersToAdd; | ||
verb.FoldersFromStdIn = this.AddFromStdIn; | ||
}, | ||
verb => | ||
{ | ||
foldersToAdd = verb.ParsedFoldersList; | ||
}); | ||
|
||
if (result != ReturnCode.Success) | ||
{ | ||
this.ReportErrorAndExit(tracer, "\r\nError during prefetch @ {0}", enlistment.EnlistmentRoot); | ||
} | ||
|
||
this.SparseCheckoutAdd(tracer, enlistment, foldersToAdd); | ||
} | ||
catch (Exception e) | ||
{ | ||
this.ReportErrorAndExit(tracer, "Failed to add folders to sparse-checkout {0}", e.Message); | ||
} | ||
} | ||
} | ||
|
||
private void SparseCheckoutAdd(ITracer tracer, ScalarEnlistment enlistment, List<string> foldersToAdd) | ||
{ | ||
GitProcess git = new GitProcess(enlistment); | ||
GitProcess.Result sparseResult = git.SparseCheckout(foldersToAdd); | ||
|
||
if (sparseResult.ExitCodeIsFailure) | ||
{ | ||
this.WriteMessage(tracer, $"Failed to add folders to sparse-checkout: {sparseResult.Errors}"); | ||
} | ||
} | ||
|
||
private void WriteMessage(ITracer tracer, string message) | ||
{ | ||
this.Output.WriteLine(message); | ||
tracer.RelatedEvent( | ||
EventLevel.Informational, | ||
SparseVerbName, | ||
new EventMetadata | ||
{ | ||
{ TracingConstants.MessageKey.InfoMessage, message } | ||
}); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.