enhance: Git LFS support

This commit is contained in:
leo 2024-06-17 18:25:57 +08:00
parent e731807c91
commit 9a0b10bd9c
No known key found for this signature in database
GPG key ID: B528468E49CD0E58
24 changed files with 780 additions and 34 deletions

View file

@ -8,9 +8,9 @@ namespace SourceGit.Commands
{
var file = Path.Combine(repo, ".gitignore");
if (!File.Exists(file))
File.WriteAllLines(file, [ pattern ]);
File.WriteAllLines(file, [pattern]);
else
File.AppendAllLines(file, [ pattern ]);
File.AppendAllLines(file, [pattern]);
}
}
}

View file

@ -1,17 +1,22 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
namespace SourceGit.Commands
{
public class LFS
public partial class LFS
{
class PruneCmd : Command
[GeneratedRegex(@"^(.+)\s+(\w+)\s+\w+:(\d+)$")]
private static partial Regex REG_LOCK();
class SubCmd : Command
{
public PruneCmd(string repo, Action<string> onProgress)
public SubCmd(string repo, string args, Action<string> onProgress)
{
WorkingDirectory = repo;
Context = repo;
Args = "lfs prune";
Args = args;
TraitErrorAsOutput = true;
_outputHandler = onProgress;
}
@ -39,9 +44,73 @@ namespace SourceGit.Commands
return content.Contains("git lfs pre-push");
}
public bool Install()
{
return new SubCmd(_repo, $"lfs install", null).Exec();
}
public bool Track(string pattern, bool isFilenameMode = false)
{
var opt = isFilenameMode ? "--filename" : "";
return new SubCmd(_repo, $"lfs track {opt} \"{pattern}\"", null).Exec();
}
public void Fetch(Action<string> outputHandler)
{
new SubCmd(_repo, $"lfs fetch", outputHandler).Exec();
}
public void Pull(Action<string> outputHandler)
{
new SubCmd(_repo, $"lfs pull", outputHandler).Exec();
}
public void Prune(Action<string> outputHandler)
{
new PruneCmd(_repo, outputHandler).Exec();
new SubCmd(_repo, "lfs prune", outputHandler).Exec();
}
public List<Models.LFSLock> Locks()
{
var locks = new List<Models.LFSLock>();
var cmd = new SubCmd(_repo, "lfs locks", null);
var rs = cmd.ReadToEnd();
if (rs.IsSuccess)
{
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var match = REG_LOCK().Match(line);
if (match.Success)
{
locks.Add(new Models.LFSLock()
{
File = match.Groups[1].Value,
User = match.Groups[2].Value,
ID = long.Parse(match.Groups[3].Value),
});
}
}
}
return locks;
}
public bool Lock(string file)
{
return new SubCmd(_repo, $"lfs lock \"{file}\"", null).Exec();
}
public bool Unlock(string file, bool force)
{
var opt = force ? "-f" : "";
return new SubCmd(_repo, $"lfs unlock {opt} \"{file}\"", null).Exec();
}
public bool Unlock(long id, bool force)
{
var opt = force ? "-f" : "";
return new SubCmd(_repo, $"lfs unlock {opt} --id={id}", null).Exec();
}
private readonly string _repo;