mirror of
https://github.com/Theodor-Springmann-Stiftung/hamann-ausgabe-core.git
synced 2025-10-29 17:25:32 +00:00
Self-Hosted Git
This commit is contained in:
147
HaWeb/FileHelpers/GitService.cs
Normal file
147
HaWeb/FileHelpers/GitService.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
namespace HaWeb.FileHelpers;
|
||||
using LibGit2Sharp;
|
||||
using HaWeb.Models;
|
||||
|
||||
public class GitService : IGitService {
|
||||
private readonly string _repositoryPath;
|
||||
private readonly string _remoteName;
|
||||
private readonly string _branch;
|
||||
private readonly string _url;
|
||||
private readonly ILogger<GitService>? _logger;
|
||||
|
||||
public GitService(IConfiguration config, ILogger<GitService>? logger = null) {
|
||||
_logger = logger;
|
||||
_remoteName = "origin";
|
||||
_branch = config.GetValue<string>("RepositoryBranch") ?? "main";
|
||||
_url = config.GetValue<string>("RepositoryURL") ?? string.Empty;
|
||||
|
||||
var fileStoragePath = config.GetValue<string>("FileStoragePath") ?? throw new ArgumentException("FileStoragePath not configured");
|
||||
_repositoryPath = Path.Combine(fileStoragePath, "GIT");
|
||||
|
||||
// Ensure repository exists
|
||||
if (!Repository.IsValid(_repositoryPath)) {
|
||||
_logger?.LogWarning("Repository not found at {Path}, attempting to initialize/clone", _repositoryPath);
|
||||
InitializeRepository();
|
||||
}
|
||||
}
|
||||
|
||||
public GitState? GetGitState() {
|
||||
try {
|
||||
using var repo = new Repository(_repositoryPath);
|
||||
var headCommit = repo.Head.Tip;
|
||||
|
||||
if (headCommit == null) {
|
||||
_logger?.LogWarning("No commits found in repository");
|
||||
return null;
|
||||
}
|
||||
|
||||
return new GitState {
|
||||
Commit = headCommit.Sha,
|
||||
Branch = repo.Head.FriendlyName,
|
||||
URL = _url,
|
||||
PullTime = headCommit.Author.When.ToLocalTime().DateTime
|
||||
};
|
||||
}
|
||||
catch (Exception ex) {
|
||||
_logger?.LogError(ex, "Failed to get Git state");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Pull() {
|
||||
try {
|
||||
using var repo = new Repository(_repositoryPath);
|
||||
var oldCommitSha = repo.Head.Tip?.Sha;
|
||||
|
||||
// Configure pull options
|
||||
var options = new PullOptions {
|
||||
FetchOptions = new FetchOptions {
|
||||
CredentialsProvider = (_url, _user, _cred) => GetCredentials()
|
||||
}
|
||||
};
|
||||
|
||||
// Create signature for merge commit (if needed)
|
||||
var signature = new Signature(
|
||||
new Identity("HaWeb Service", "hawebservice@localhost"),
|
||||
DateTimeOffset.Now
|
||||
);
|
||||
|
||||
// Perform pull
|
||||
var result = Commands.Pull(repo, signature, options);
|
||||
|
||||
var newCommitSha = repo.Head.Tip?.Sha;
|
||||
var hasChanges = oldCommitSha != newCommitSha;
|
||||
|
||||
if (hasChanges) {
|
||||
_logger?.LogInformation("Pull successful: {OldCommit} -> {NewCommit}",
|
||||
oldCommitSha?[..7], newCommitSha?[..7]);
|
||||
} else {
|
||||
_logger?.LogInformation("Pull completed but no new changes");
|
||||
}
|
||||
|
||||
return hasChanges;
|
||||
}
|
||||
catch (Exception ex) {
|
||||
_logger?.LogError(ex, "Failed to pull from remote repository");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasChanged(string? previousCommitSha) {
|
||||
if (string.IsNullOrEmpty(previousCommitSha)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
var currentState = GetGitState();
|
||||
return currentState != null && currentState.Commit != previousCommitSha;
|
||||
}
|
||||
|
||||
private void InitializeRepository() {
|
||||
try {
|
||||
if (!Directory.Exists(_repositoryPath)) {
|
||||
Directory.CreateDirectory(_repositoryPath);
|
||||
}
|
||||
|
||||
// If there's a .git directory but it's invalid, try to reinitialize
|
||||
var gitDir = Path.Combine(_repositoryPath, ".git");
|
||||
if (Directory.Exists(gitDir)) {
|
||||
_logger?.LogWarning("Invalid .git directory found, attempting to use existing repository");
|
||||
return;
|
||||
}
|
||||
|
||||
// If URL is provided, clone; otherwise just initialize
|
||||
if (!string.IsNullOrEmpty(_url)) {
|
||||
_logger?.LogInformation("Cloning repository from {Url} to {Path}", _url, _repositoryPath);
|
||||
var cloneOptions = new CloneOptions();
|
||||
cloneOptions.FetchOptions.CredentialsProvider = (_url, _user, _cred) => GetCredentials();
|
||||
|
||||
// Clone with default branch, then checkout the specified branch if different
|
||||
Repository.Clone(_url, _repositoryPath, cloneOptions);
|
||||
_logger?.LogInformation("Repository cloned successfully");
|
||||
|
||||
// Checkout the specified branch if it's not the default
|
||||
using var repo = new Repository(_repositoryPath);
|
||||
var branch = repo.Branches[_branch] ?? repo.Branches[$"origin/{_branch}"];
|
||||
if (branch != null && branch.FriendlyName != repo.Head.FriendlyName) {
|
||||
Commands.Checkout(repo, branch);
|
||||
_logger?.LogInformation("Checked out branch {Branch}", _branch);
|
||||
}
|
||||
}
|
||||
else {
|
||||
_logger?.LogInformation("Initializing empty repository at {Path}", _repositoryPath);
|
||||
Repository.Init(_repositoryPath);
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
_logger?.LogError(ex, "Failed to initialize repository");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private Credentials? GetCredentials() {
|
||||
// For now, use default credentials (SSH agent, credential manager, etc.)
|
||||
// Can be extended to support username/password or personal access tokens
|
||||
// from configuration if needed
|
||||
return new DefaultCredentials();
|
||||
}
|
||||
}
|
||||
19
HaWeb/FileHelpers/IGitService.cs
Normal file
19
HaWeb/FileHelpers/IGitService.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace HaWeb.FileHelpers;
|
||||
|
||||
public interface IGitService {
|
||||
/// <summary>
|
||||
/// Gets the current Git state (commit SHA, branch, timestamp)
|
||||
/// </summary>
|
||||
GitState? GetGitState();
|
||||
|
||||
/// <summary>
|
||||
/// Pulls latest changes from the remote repository
|
||||
/// </summary>
|
||||
/// <returns>True if pull was successful and changes were detected, false otherwise</returns>
|
||||
bool Pull();
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the repository has a different commit than the provided SHA
|
||||
/// </summary>
|
||||
bool HasChanged(string? previousCommitSha);
|
||||
}
|
||||
@@ -18,4 +18,5 @@ public interface IXMLFileProvider {
|
||||
public bool HasChanged();
|
||||
public void DeleteHamannFile(string filename);
|
||||
public void Scan();
|
||||
public void Reload();
|
||||
}
|
||||
@@ -4,16 +4,15 @@ using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
using HaWeb.Models;
|
||||
using HaWeb.XMLParser;
|
||||
using System.Xml.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
// XMLProvider provides a wrapper around the available XML data on a FILE basis
|
||||
public class XMLFileProvider : IXMLFileProvider {
|
||||
private readonly IHaDocumentWrappper _Lib;
|
||||
private readonly IXMLInteractionService _XMLService;
|
||||
private readonly IGitService _GitService;
|
||||
|
||||
private IFileProvider _hamannFileProvider;
|
||||
private IFileProvider _bareRepositoryFileProvider;
|
||||
private IFileProvider _workingTreeFileProvider;
|
||||
|
||||
public event EventHandler<GitState?> FileChange;
|
||||
@@ -21,9 +20,6 @@ public class XMLFileProvider : IXMLFileProvider {
|
||||
public event EventHandler<XMLParsingState?> NewState;
|
||||
public event EventHandler NewData;
|
||||
|
||||
private string _Branch;
|
||||
private string _URL;
|
||||
|
||||
private List<IFileInfo>? _WorkingTreeFiles;
|
||||
private List<IFileInfo>? _HamannFiles;
|
||||
|
||||
@@ -31,24 +27,28 @@ public class XMLFileProvider : IXMLFileProvider {
|
||||
private System.Timers.Timer? _changeTokenTimer;
|
||||
|
||||
// Startup (LAST)
|
||||
public XMLFileProvider(IXMLInteractionService xmlservice, IHaDocumentWrappper _lib, IConfiguration config) {
|
||||
public XMLFileProvider(IXMLInteractionService xmlservice, IHaDocumentWrappper _lib, IGitService gitService, IConfiguration config) {
|
||||
// TODO: Test Read / Write Access
|
||||
_Lib = _lib;
|
||||
_XMLService = xmlservice;
|
||||
_GitService = gitService;
|
||||
|
||||
_Branch = config.GetValue<string>("RepositoryBranch");
|
||||
_URL = config.GetValue<string>("RepositoryURL");
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
||||
_hamannFileProvider = new PhysicalFileProvider(config.GetValue<string>("HamannFileStoreWindows"));
|
||||
_bareRepositoryFileProvider = new PhysicalFileProvider(config.GetValue<string>("BareRepositoryPathWindows"));
|
||||
_workingTreeFileProvider = new PhysicalFileProvider(config.GetValue<string>("WorkingTreePathWindows"));
|
||||
var fileStoragePath = config.GetValue<string>("FileStoragePath") ?? throw new ArgumentException("FileStoragePath not configured");
|
||||
|
||||
// Ensure directories exist
|
||||
var hamannPath = Path.Combine(fileStoragePath, "HAMANN");
|
||||
var gitPath = Path.Combine(fileStoragePath, "GIT");
|
||||
|
||||
if (!Directory.Exists(hamannPath)) {
|
||||
Directory.CreateDirectory(hamannPath);
|
||||
}
|
||||
else {
|
||||
_hamannFileProvider = new PhysicalFileProvider(config.GetValue<string>("HamannFileStoreLinux"));
|
||||
_bareRepositoryFileProvider = new PhysicalFileProvider(config.GetValue<string>("BareRepositoryPathLinux"));
|
||||
_workingTreeFileProvider = new PhysicalFileProvider(config.GetValue<string>("WorkingTreePathLinux"));
|
||||
if (!Directory.Exists(gitPath)) {
|
||||
Directory.CreateDirectory(gitPath);
|
||||
}
|
||||
|
||||
_hamannFileProvider = new PhysicalFileProvider(hamannPath);
|
||||
_workingTreeFileProvider = new PhysicalFileProvider(gitPath);
|
||||
|
||||
// Create File Lists; Here and in xmlservice, which does preliminary checking
|
||||
Scan();
|
||||
if (_WorkingTreeFiles != null && _WorkingTreeFiles.Any()) {
|
||||
@@ -57,7 +57,6 @@ public class XMLFileProvider : IXMLFileProvider {
|
||||
}
|
||||
_HamannFiles = _ScanHamannFiles();
|
||||
|
||||
_RegisterChangeToken();
|
||||
// Check if hamann file already is current working tree status
|
||||
// -> YES: Load up the file via _lib.SetLibrary();
|
||||
if (_IsAlreadyParsed()) {
|
||||
@@ -91,8 +90,6 @@ public class XMLFileProvider : IXMLFileProvider {
|
||||
}
|
||||
|
||||
public void ParseConfiguration(IConfiguration config) {
|
||||
_Branch = config.GetValue<string>("RepositoryBranch");
|
||||
|
||||
Scan();
|
||||
// Reset XMLInteractionService
|
||||
if (_WorkingTreeFiles != null && _WorkingTreeFiles.Any()) {
|
||||
@@ -158,7 +155,59 @@ public class XMLFileProvider : IXMLFileProvider {
|
||||
|
||||
public void Scan() {
|
||||
_WorkingTreeFiles = _ScanWorkingTreeFiles();
|
||||
_GitState = _ScanGitData();
|
||||
_GitState = _GitService.GetGitState();
|
||||
}
|
||||
|
||||
public void Reload() {
|
||||
Scan();
|
||||
|
||||
// Reset XMLInteractionService
|
||||
if (_WorkingTreeFiles != null && _WorkingTreeFiles.Any()) {
|
||||
var state = _XMLService.Collect(_WorkingTreeFiles, _XMLService.GetRootDefs());
|
||||
_XMLService.SetState(state);
|
||||
OnNewState(state);
|
||||
}
|
||||
|
||||
_HamannFiles = _ScanHamannFiles();
|
||||
_XMLService.SetSCCache(null);
|
||||
|
||||
// Check if hamann file already is current working tree status
|
||||
if (_IsAlreadyParsed()) {
|
||||
_Lib.SetLibrary(_HamannFiles!.First(), null, null);
|
||||
if (_Lib.GetLibrary() != null) {
|
||||
OnNewData();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to create a new file
|
||||
var created = _XMLService.TryCreate(_XMLService.GetState());
|
||||
if (created != null) {
|
||||
var file = SaveHamannFile(created, _hamannFileProvider.GetFileInfo("./").PhysicalPath, null);
|
||||
if (file != null) {
|
||||
_Lib.SetLibrary(file, created.Document, null);
|
||||
if (_Lib.GetLibrary() != null) {
|
||||
OnNewData();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// It failed, so use the last best File:
|
||||
if (_HamannFiles != null && _HamannFiles.Any()) {
|
||||
_Lib.SetLibrary(_HamannFiles.First(), null, null);
|
||||
if (_Lib.GetLibrary() != null) {
|
||||
OnNewData();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Use Fallback:
|
||||
var options = new HaWeb.Settings.HaDocumentOptions();
|
||||
if (_Lib.SetLibrary(null, null, null) == null) {
|
||||
throw new Exception("Die Fallback Hamann.xml unter " + options.HamannXMLFilePath + " kann nicht geparst werden.");
|
||||
}
|
||||
OnNewData();
|
||||
}
|
||||
|
||||
public IFileInfo? SaveHamannFile(XElement element, string basefilepath, ModelStateDictionary? ModelState) {
|
||||
@@ -191,7 +240,7 @@ public class XMLFileProvider : IXMLFileProvider {
|
||||
|
||||
public bool HasChanged() {
|
||||
if (_GitState == null) return true;
|
||||
var current = _ScanGitData();
|
||||
var current = _GitService.GetGitState();
|
||||
if (current != null && !String.Equals(current.Commit, _GitState.Commit)) {
|
||||
_GitState = current;
|
||||
return true;
|
||||
@@ -199,22 +248,6 @@ public class XMLFileProvider : IXMLFileProvider {
|
||||
return false;
|
||||
}
|
||||
|
||||
private GitState? _ScanGitData() {
|
||||
var head = _bareRepositoryFileProvider.GetFileInfo("refs/heads/" + _Branch);
|
||||
// TODO: Failsave reading from FIle
|
||||
try {
|
||||
return new GitState {
|
||||
URL = _URL,
|
||||
Branch = _Branch,
|
||||
PullTime = head.LastModified.ToLocalTime().DateTime,
|
||||
Commit = File.ReadAllText(head.PhysicalPath).Trim()
|
||||
};
|
||||
}
|
||||
catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Gets all XML Files
|
||||
private List<IFileInfo>? _ScanWorkingTreeFiles() {
|
||||
var files = _workingTreeFileProvider.GetDirectoryContents(string.Empty)!.Where(x => !x.IsDirectory && x.Name.EndsWith(".xml"))!.ToList();
|
||||
@@ -240,46 +273,6 @@ public class XMLFileProvider : IXMLFileProvider {
|
||||
return fhash == ghash;
|
||||
}
|
||||
|
||||
private void _RegisterChangeToken() {
|
||||
ChangeToken.OnChange(
|
||||
() => _bareRepositoryFileProvider.Watch("refs/heads/" + _Branch),
|
||||
async (state) => await this._InvokeChanged(state),
|
||||
this._ScanGitData()
|
||||
);
|
||||
}
|
||||
|
||||
private async Task _InvokeChanged(GitState? gitdata) {
|
||||
if (_changeTokenTimer != null) return;
|
||||
Console.WriteLine("FILECHANGE DETECTED, RELOAD");
|
||||
Scan();
|
||||
|
||||
OnFileChange(_ScanGitData());
|
||||
// Reset XMLInteractionService
|
||||
if (_WorkingTreeFiles != null && _WorkingTreeFiles.Any()) {
|
||||
var state = _XMLService.Collect(_WorkingTreeFiles, _XMLService.GetRootDefs());
|
||||
_XMLService.SetState(state);
|
||||
OnNewState(state);
|
||||
}
|
||||
|
||||
// -> Try to create a new file
|
||||
var created = _XMLService.TryCreate(_XMLService.GetState());
|
||||
if (created != null) {
|
||||
var file = SaveHamannFile(created, _hamannFileProvider.GetFileInfo("./").PhysicalPath, null);
|
||||
if (file != null) {
|
||||
var ret = _Lib.SetLibrary(file, created.Document, null);
|
||||
if (ret != null) OnNewData();
|
||||
}
|
||||
}
|
||||
|
||||
_XMLService.SetSCCache(null);
|
||||
_GitState = _ScanGitData();
|
||||
_changeTokenTimer = new(5000) { AutoReset = false, Enabled = true };
|
||||
_changeTokenTimer.Elapsed += this._OnElapsed;
|
||||
}
|
||||
|
||||
private void _OnElapsed(Object source, System.Timers.ElapsedEventArgs e) {
|
||||
_changeTokenTimer = null;
|
||||
}
|
||||
|
||||
protected virtual void OnFileChange(GitState? state) {
|
||||
EventHandler<GitState?> eh = FileChange;
|
||||
|
||||
Reference in New Issue
Block a user