|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.IO;
|
| 4 | +using System.Text.RegularExpressions; |
3 | 5 |
|
4 | 6 | namespace SourceGit.Commands
|
5 | 7 | {
|
6 |
| - public class LFS |
| 8 | + public partial class LFS |
7 | 9 | {
|
8 |
| - class PruneCmd : Command |
| 10 | + [GeneratedRegex(@"^(.+)\s+(\w+)\s+\w+:(\d+)$")] |
| 11 | + private static partial Regex REG_LOCK(); |
| 12 | + |
| 13 | + class SubCmd : Command |
9 | 14 | {
|
10 |
| - public PruneCmd(string repo, Action<string> onProgress) |
| 15 | + public SubCmd(string repo, string args, Action<string> onProgress) |
11 | 16 | {
|
12 | 17 | WorkingDirectory = repo;
|
13 | 18 | Context = repo;
|
14 |
| - Args = "lfs prune"; |
| 19 | + Args = args; |
15 | 20 | TraitErrorAsOutput = true;
|
16 | 21 | _outputHandler = onProgress;
|
17 | 22 | }
|
@@ -39,9 +44,73 @@ public bool IsEnabled()
|
39 | 44 | return content.Contains("git lfs pre-push");
|
40 | 45 | }
|
41 | 46 |
|
| 47 | + public bool Install() |
| 48 | + { |
| 49 | + return new SubCmd(_repo, $"lfs install", null).Exec(); |
| 50 | + } |
| 51 | + |
| 52 | + public bool Track(string pattern, bool isFilenameMode = false) |
| 53 | + { |
| 54 | + var opt = isFilenameMode ? "--filename" : ""; |
| 55 | + return new SubCmd(_repo, $"lfs track {opt} \"{pattern}\"", null).Exec(); |
| 56 | + } |
| 57 | + |
| 58 | + public void Fetch(Action<string> outputHandler) |
| 59 | + { |
| 60 | + new SubCmd(_repo, $"lfs fetch", outputHandler).Exec(); |
| 61 | + } |
| 62 | + |
| 63 | + public void Pull(Action<string> outputHandler) |
| 64 | + { |
| 65 | + new SubCmd(_repo, $"lfs pull", outputHandler).Exec(); |
| 66 | + } |
| 67 | + |
42 | 68 | public void Prune(Action<string> outputHandler)
|
43 | 69 | {
|
44 |
| - new PruneCmd(_repo, outputHandler).Exec(); |
| 70 | + new SubCmd(_repo, "lfs prune", outputHandler).Exec(); |
| 71 | + } |
| 72 | + |
| 73 | + public List<Models.LFSLock> Locks() |
| 74 | + { |
| 75 | + var locks = new List<Models.LFSLock>(); |
| 76 | + var cmd = new SubCmd(_repo, "lfs locks", null); |
| 77 | + var rs = cmd.ReadToEnd(); |
| 78 | + if (rs.IsSuccess) |
| 79 | + { |
| 80 | + var lines = rs.StdOut.Split(new char[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries); |
| 81 | + foreach (var line in lines) |
| 82 | + { |
| 83 | + var match = REG_LOCK().Match(line); |
| 84 | + if (match.Success) |
| 85 | + { |
| 86 | + locks.Add(new Models.LFSLock() |
| 87 | + { |
| 88 | + File = match.Groups[1].Value, |
| 89 | + User = match.Groups[2].Value, |
| 90 | + ID = long.Parse(match.Groups[3].Value), |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return locks; |
| 97 | + } |
| 98 | + |
| 99 | + public bool Lock(string file) |
| 100 | + { |
| 101 | + return new SubCmd(_repo, $"lfs lock \"{file}\"", null).Exec(); |
| 102 | + } |
| 103 | + |
| 104 | + public bool Unlock(string file, bool force) |
| 105 | + { |
| 106 | + var opt = force ? "-f" : ""; |
| 107 | + return new SubCmd(_repo, $"lfs unlock {opt} \"{file}\"", null).Exec(); |
| 108 | + } |
| 109 | + |
| 110 | + public bool Unlock(long id, bool force) |
| 111 | + { |
| 112 | + var opt = force ? "-f" : ""; |
| 113 | + return new SubCmd(_repo, $"lfs unlock {opt} --id={id}", null).Exec(); |
45 | 114 | }
|
46 | 115 |
|
47 | 116 | private readonly string _repo;
|
|
0 commit comments