-
Notifications
You must be signed in to change notification settings - Fork 32
Also write the used link-index to the filesystem and add it as part of the new CheckoutResult record #1316
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
reakaleek
merged 7 commits into
main
from
feature/create-and-upload-link-index-snapshot
May 28, 2025
Merged
Also write the used link-index to the filesystem and add it as part of the new CheckoutResult record #1316
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5f92101
Also write the used link-index to the filesystem and add it as part o…
reakaleek f39834d
Refactor git operations
reakaleek 8ef5f22
Fix naming
reakaleek 09e1432
Refactor writing linkRegistrySnapshot to AssemblerRepositorySourcer m…
reakaleek 42a048b
Fix method signature
reakaleek 42ad14f
Update src/tooling/docs-assembler/Sourcing/RepositorySourcesFetcher.cs
reakaleek 6c2f39d
Use context's FileSystem
reakaleek 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System.IO.Abstractions; | ||
| using Elastic.Documentation.Diagnostics; | ||
| using ProcNet; | ||
|
|
||
| namespace Documentation.Assembler.Sourcing; | ||
|
|
||
|
|
||
| public interface IGitRepository | ||
| { | ||
| void Init(); | ||
| string GetCurrentCommit(); | ||
| void GitAddOrigin(string origin); | ||
| bool IsInitialized(); | ||
| void Pull(string branch); | ||
| void Fetch(string reference); | ||
| void EnableSparseCheckout(string folder); | ||
| void DisableSparseCheckout(); | ||
| void Checkout(string reference); | ||
| } | ||
|
|
||
| // This git repository implementation is optimized for pull and fetching single commits. | ||
| // It uses `git pull --depth 1` and `git fetch --depth 1` to minimize the amount of data transferred. | ||
| public class SingleCommitOptimizedGitRepository(DiagnosticsCollector collector, IDirectoryInfo workingDirectory) : IGitRepository | ||
| { | ||
| public string GetCurrentCommit() => Capture("git", "rev-parse", "HEAD"); | ||
|
|
||
| public void Init() => ExecIn("git", "init"); | ||
| public bool IsInitialized() => Directory.Exists(Path.Combine(workingDirectory.FullName, ".git")); | ||
| public void Pull(string branch) => ExecIn("git", "pull", "--depth", "1", "--allow-unrelated-histories", "--no-ff", "origin", branch); | ||
| public void Fetch(string reference) => ExecIn("git", "fetch", "--no-tags", "--prune", "--no-recurse-submodules", "--depth", "1", "origin", reference); | ||
| public void EnableSparseCheckout(string folder) => ExecIn("git", "sparse-checkout", "set", folder); | ||
| public void DisableSparseCheckout() => ExecIn("git", "sparse-checkout", "disable"); | ||
| public void Checkout(string reference) => ExecIn("git", "checkout", "--force", reference); | ||
|
|
||
| public void GitAddOrigin(string origin) => ExecIn("git", "remote", "add", "origin", origin); | ||
|
|
||
| private void ExecIn(string binary, params string[] args) | ||
| { | ||
| var arguments = new ExecArguments(binary, args) | ||
| { | ||
| WorkingDirectory = workingDirectory.FullName, | ||
| Environment = new Dictionary<string, string> | ||
| { | ||
| // Disable git editor prompts: | ||
| // There are cases where `git pull` would prompt for an editor to write a commit message. | ||
| // This env variable prevents that. | ||
| { "GIT_EDITOR", "true" } | ||
| }, | ||
| }; | ||
| var result = Proc.Exec(arguments); | ||
| if (result != 0) | ||
| collector.EmitError("", $"Exit code: {result} while executing {binary} {string.Join(" ", args)} in {workingDirectory}"); | ||
| } | ||
| private string Capture(string binary, params string[] args) | ||
| { | ||
| // Try 10 times to capture the output of the command, if it fails, we'll throw an exception on the last try | ||
| Exception? e = null; | ||
| for (var i = 0; i <= 9; i++) | ||
| { | ||
| try | ||
| { | ||
| return CaptureOutput(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| if (ex is not null) | ||
| e = ex; | ||
| } | ||
| } | ||
|
|
||
| if (e is not null) | ||
| collector.EmitError("", "failure capturing stdout", e); | ||
|
|
||
| return string.Empty; | ||
|
|
||
| string CaptureOutput() | ||
| { | ||
| var arguments = new StartArguments(binary, args) | ||
| { | ||
| WorkingDirectory = workingDirectory.FullName, | ||
| Timeout = TimeSpan.FromSeconds(3), | ||
| WaitForExit = TimeSpan.FromSeconds(3), | ||
| ConsoleOutWriter = NoopConsoleWriter.Instance | ||
| }; | ||
| var result = Proc.Start(arguments); | ||
| var line = result.ExitCode != 0 | ||
| ? throw new Exception($"Exit code is not 0. Received {result.ExitCode} from {binary}: {workingDirectory}") | ||
| : result.ConsoleOut.FirstOrDefault()?.Line ?? throw new Exception($"No output captured for {binary}: {workingDirectory}"); | ||
| return line; | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
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.