A dotnet library leverage Git repository as store.
Note: it's not a replacement of Git operations, actually it only implements git clone
and git push
. Basic idea is clone remoting git repository to local and store data as text or bytes, commit and push to remote. The core operations are file reads and writes.
Install from NuGet
dotnet add package GitStore
This library supports .NET 7 and version onwards.
Provide required option parameters manually.
var option = new GitStoreOption
{
Branch = "unit-test",
Author = "test_committer",
Password = "",
CommitterEmail = "[email protected]",
UserName = <Personal Access Token>,
LocalDirectory = Path.Combine(Path.GetTempPath(), "gitstore_unittest"),
RemoteGitUrl = "https://github.com/JerryBian/gitstore-dotnet"
};
var store = new GitStore(Options.Create(option));
await store.PullFromRemoteAsync();
var content = await store.GetTextAsync(Path.Combine(option.LocalDirectory, "dummy.txt"));
Assert.Equal("test", content);
var path1 = Path.Combine(option.LocalDirectory, "data", Path.GetRandomFileName());
var content1 = Guid.NewGuid().ToString();
await store.InsertOrUpdateAsync(path1, content1);
var path2 = Path.Combine(option.LocalDirectory, "data", Path.GetRandomFileName());
var content2 = Guid.NewGuid().ToByteArray();
await store.InsertOrUpdateAsync(path2, content2);
await store.PushToRemoteAsync("commit by GitStore.");
await store.PullFromRemoteAsync();
Assert.Equal(File.ReadAllText(path1), content1);
Assert.True(content2.SequenceEqual(File.ReadAllBytes(path2)));
For GitHub repository, you can request PAT here here. Please note you cannot use traditional username/password method to operate GitHub anymore, see the announcement.
For modern applications, you can register GitStore via DI.
var builder = Host.CreateApplicationBuilder();
builder.Services.AddGitStore();
using IHost host = builder.Build();
var store = host.Services.GetRequiredService<IGitStore>();
var option = host.Services.GetRequiredService<IOptions<GitStoreOption>>().Value;
await store.PullFromRemoteAsync();
The option parameters are configured as standard way.
For example, set environment variable as GitStore__Branch=master
.