Skip to content

Commit b26838f

Browse files
committed
refactor: git version related commands
* use `--pathspec-from-file=<FILE>` in `git add` command if git >= 2.25.0 * use `--pathspec-from-file=<FILE>` in `git stash push` command if git >= 2.26.0 * use `--staged` in `git stash push` command only if git >= 2.35.0
1 parent c939308 commit b26838f

File tree

10 files changed

+305
-102
lines changed

10 files changed

+305
-102
lines changed

src/Commands/Add.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,12 @@ public Add(string repo, List<Models.Change> changes)
2727
}
2828
Args = builder.ToString();
2929
}
30+
31+
public Add(string repo, string pathspecFromFile)
32+
{
33+
WorkingDirectory = repo;
34+
Context = repo;
35+
Args = $"add --pathspec-from-file=\"{pathspecFromFile}\"";
36+
}
3037
}
3138
}

src/Commands/Stash.cs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,52 @@ public bool Push(string message)
1717
return Exec();
1818
}
1919

20-
public bool Push(List<Models.Change> changes, string message, bool onlyStaged, bool keepIndex)
20+
public bool Push(string message, List<Models.Change> changes, bool keepIndex)
2121
{
2222
var builder = new StringBuilder();
2323
builder.Append("stash push ");
24-
if (onlyStaged)
25-
builder.Append("--staged ");
2624
if (keepIndex)
2725
builder.Append("--keep-index ");
2826
builder.Append("-m \"");
2927
builder.Append(message);
3028
builder.Append("\" -- ");
3129

32-
if (onlyStaged)
33-
{
34-
foreach (var c in changes)
35-
builder.Append($"\"{c.Path}\" ");
36-
}
37-
else
38-
{
39-
var needAdd = new List<Models.Change>();
40-
foreach (var c in changes)
41-
{
42-
builder.Append($"\"{c.Path}\" ");
30+
foreach (var c in changes)
31+
builder.Append($"\"{c.Path}\" ");
4332

44-
if (c.WorkTree == Models.ChangeState.Added || c.WorkTree == Models.ChangeState.Untracked)
45-
{
46-
needAdd.Add(c);
47-
if (needAdd.Count > 10)
48-
{
49-
new Add(WorkingDirectory, needAdd).Exec();
50-
needAdd.Clear();
51-
}
52-
}
53-
}
54-
if (needAdd.Count > 0)
55-
{
56-
new Add(WorkingDirectory, needAdd).Exec();
57-
needAdd.Clear();
58-
}
59-
}
33+
Args = builder.ToString();
34+
return Exec();
35+
}
36+
37+
public bool Push(string message, string pathspecFromFile, bool keepIndex)
38+
{
39+
var builder = new StringBuilder();
40+
builder.Append("stash push --pathspec-from-file=\"");
41+
builder.Append(pathspecFromFile);
42+
builder.Append("\" ");
43+
if (keepIndex)
44+
builder.Append("--keep-index ");
45+
builder.Append("-m \"");
46+
builder.Append(message);
47+
builder.Append("\"");
6048

6149
Args = builder.ToString();
6250
return Exec();
6351
}
6452

53+
public bool PushOnlyStaged(string message, bool keepIndex)
54+
{
55+
var builder = new StringBuilder();
56+
builder.Append("stash push --staged ");
57+
if (keepIndex)
58+
builder.Append("--keep-index ");
59+
builder.Append("-m \"");
60+
builder.Append(message);
61+
builder.Append("\"");
62+
Args = builder.ToString();
63+
return Exec();
64+
}
65+
6566
public bool Apply(string name)
6667
{
6768
Args = $"stash apply -q {name}";

src/Commands/Version.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Converters/StringConverters.cs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System;
22
using System.Globalization;
3-
using System.Text.RegularExpressions;
43

54
using Avalonia.Data.Converters;
65
using Avalonia.Styling;
76

87
namespace SourceGit.Converters
98
{
10-
public static partial class StringConverters
9+
public static class StringConverters
1110
{
1211
public class ToLocaleConverter : IValueConverter
1312
{
@@ -68,22 +67,6 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
6867
public static readonly FuncValueConverter<string, string> ToShortSHA =
6968
new FuncValueConverter<string, string>(v => v == null ? string.Empty : (v.Length > 10 ? v.Substring(0, 10) : v));
7069

71-
public static readonly FuncValueConverter<string, bool> UnderRecommendGitVersion =
72-
new(v =>
73-
{
74-
var match = REG_GIT_VERSION().Match(v ?? "");
75-
if (match.Success)
76-
{
77-
var major = int.Parse(match.Groups[1].Value);
78-
var minor = int.Parse(match.Groups[2].Value);
79-
var build = int.Parse(match.Groups[3].Value);
80-
81-
return new Version(major, minor, build) < MINIMAL_GIT_VERSION;
82-
}
83-
84-
return true;
85-
});
86-
8770
public static readonly FuncValueConverter<string, string> TrimRefsPrefix =
8871
new FuncValueConverter<string, string>(v =>
8972
{
@@ -95,10 +78,5 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
9578
return v.Substring(13);
9679
return v;
9780
});
98-
99-
[GeneratedRegex(@"^[\s\w]*(\d+)\.(\d+)[\.\-](\d+).*$")]
100-
private static partial Regex REG_GIT_VERSION();
101-
102-
private static readonly Version MINIMAL_GIT_VERSION = new Version(2, 23, 0);
10381
}
10482
}

src/Models/GitVersions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace SourceGit.Models
2+
{
3+
public static class GitVersions
4+
{
5+
/// <summary>
6+
/// The minimal version of Git that required by this app.
7+
/// </summary>
8+
public static readonly System.Version MINIMAL = new System.Version(2, 23, 0);
9+
10+
/// <summary>
11+
/// The minimal version of Git that supports the `add` command with the `--pathspec-from-file` option.
12+
/// </summary>
13+
public static readonly System.Version ADD_WITH_PATHSPECFILE = new System.Version(2, 25, 0);
14+
15+
/// <summary>
16+
/// The minimal version of Git that supports the `stash` command with the `--pathspec-from-file` option.
17+
/// </summary>
18+
public static readonly System.Version STASH_WITH_PATHSPECFILE = new System.Version(2, 26, 0);
19+
20+
/// <summary>
21+
/// The minimal version of Git that supports the `stash` command with the `--staged` option.
22+
/// </summary>
23+
public static readonly System.Version STASH_ONLY_STAGED = new System.Version(2, 35, 0);
24+
}
25+
}

src/Native/OS.cs

Lines changed: 101 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5+
using System.Text;
6+
using System.Text.RegularExpressions;
57

68
using Avalonia;
79

810
namespace SourceGit.Native
911
{
10-
public static class OS
12+
public static partial class OS
1113
{
1214
public interface IBackend
1315
{
@@ -23,11 +25,51 @@ public interface IBackend
2325
void OpenWithDefaultEditor(string file);
2426
}
2527

26-
public static string DataDir { get; private set; } = string.Empty;
27-
public static string GitExecutable { get; set; } = string.Empty;
28-
public static string ShellOrTerminal { get; set; } = string.Empty;
29-
public static List<Models.ExternalTool> ExternalTools { get; set; } = [];
30-
public static string CustomPathEnv { get; set; } = string.Empty;
28+
public static string DataDir {
29+
get;
30+
private set;
31+
} = string.Empty;
32+
33+
public static string CustomPathEnv
34+
{
35+
get;
36+
set;
37+
} = string.Empty;
38+
39+
public static string GitExecutable
40+
{
41+
get => _gitExecutable;
42+
set
43+
{
44+
if (_gitExecutable != value)
45+
{
46+
_gitExecutable = value;
47+
UpdateGitVersion();
48+
}
49+
}
50+
}
51+
52+
public static string GitVersionString
53+
{
54+
get;
55+
private set;
56+
} = string.Empty;
57+
58+
public static Version GitVersion
59+
{
60+
get;
61+
private set;
62+
} = new Version(0, 0, 0);
63+
64+
public static string ShellOrTerminal {
65+
get;
66+
set;
67+
} = string.Empty;
68+
69+
public static List<Models.ExternalTool> ExternalTools {
70+
get;
71+
set;
72+
} = [];
3173

3274
static OS()
3375
{
@@ -123,6 +165,59 @@ public static void OpenWithDefaultEditor(string file)
123165
_backend.OpenWithDefaultEditor(file);
124166
}
125167

168+
private static void UpdateGitVersion()
169+
{
170+
if (string.IsNullOrEmpty(_gitExecutable) || !File.Exists(_gitExecutable))
171+
{
172+
GitVersionString = string.Empty;
173+
GitVersion = new Version(0, 0, 0);
174+
return;
175+
}
176+
177+
var start = new ProcessStartInfo();
178+
start.FileName = _gitExecutable;
179+
start.Arguments = "--version";
180+
start.UseShellExecute = false;
181+
start.CreateNoWindow = true;
182+
start.RedirectStandardOutput = true;
183+
start.RedirectStandardError = true;
184+
start.StandardOutputEncoding = Encoding.UTF8;
185+
start.StandardErrorEncoding = Encoding.UTF8;
186+
187+
var proc = new Process() { StartInfo = start };
188+
try
189+
{
190+
proc.Start();
191+
192+
var rs = proc.StandardOutput.ReadToEnd();
193+
proc.WaitForExit();
194+
if (proc.ExitCode == 0 && !string.IsNullOrWhiteSpace(rs))
195+
{
196+
GitVersionString = rs.Trim();
197+
198+
var match = REG_GIT_VERSION().Match(GitVersionString);
199+
if (match.Success)
200+
{
201+
var major = int.Parse(match.Groups[1].Value);
202+
var minor = int.Parse(match.Groups[2].Value);
203+
var build = int.Parse(match.Groups[3].Value);
204+
GitVersion = new Version(major, minor, build);
205+
GitVersionString = GitVersionString.Substring(11).Trim();
206+
}
207+
}
208+
}
209+
catch
210+
{
211+
// Ignore errors
212+
}
213+
214+
proc.Close();
215+
}
216+
217+
[GeneratedRegex(@"^git version[\s\w]*(\d+)\.(\d+)[\.\-](\d+).*$")]
218+
private static partial Regex REG_GIT_VERSION();
219+
126220
private static IBackend _backend = null;
221+
private static string _gitExecutable = string.Empty;
127222
}
128223
}

0 commit comments

Comments
 (0)