Skip to content

JerryBian/gitstore-dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A dotnet library leverage Git repository as store.

master

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.

Usage

Install from NuGet

dotnet add package GitStore

This library supports .NET 7 and version onwards.

Basic usage

Provide required option parameters manually.

var option = new GitStoreOption
{
    Branch = "unit-test",
    Author = "test_committer",
    Password = "",
    CommitterEmail = "test_committer@test.com",
    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.

Dependency injection / Options pattern

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.

License

MIT