mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-22 04:34:59 +00:00
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
This commit is contained in:
parent
c939308e4c
commit
b26838ff68
10 changed files with 306 additions and 103 deletions
107
src/Native/OS.cs
107
src/Native/OS.cs
|
@ -2,12 +2,14 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
using Avalonia;
|
||||
|
||||
namespace SourceGit.Native
|
||||
{
|
||||
public static class OS
|
||||
public static partial class OS
|
||||
{
|
||||
public interface IBackend
|
||||
{
|
||||
|
@ -23,11 +25,51 @@ namespace SourceGit.Native
|
|||
void OpenWithDefaultEditor(string file);
|
||||
}
|
||||
|
||||
public static string DataDir { get; private set; } = string.Empty;
|
||||
public static string GitExecutable { get; set; } = string.Empty;
|
||||
public static string ShellOrTerminal { get; set; } = string.Empty;
|
||||
public static List<Models.ExternalTool> ExternalTools { get; set; } = [];
|
||||
public static string CustomPathEnv { get; set; } = string.Empty;
|
||||
public static string DataDir {
|
||||
get;
|
||||
private set;
|
||||
} = string.Empty;
|
||||
|
||||
public static string CustomPathEnv
|
||||
{
|
||||
get;
|
||||
set;
|
||||
} = string.Empty;
|
||||
|
||||
public static string GitExecutable
|
||||
{
|
||||
get => _gitExecutable;
|
||||
set
|
||||
{
|
||||
if (_gitExecutable != value)
|
||||
{
|
||||
_gitExecutable = value;
|
||||
UpdateGitVersion();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static string GitVersionString
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
} = string.Empty;
|
||||
|
||||
public static Version GitVersion
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
} = new Version(0, 0, 0);
|
||||
|
||||
public static string ShellOrTerminal {
|
||||
get;
|
||||
set;
|
||||
} = string.Empty;
|
||||
|
||||
public static List<Models.ExternalTool> ExternalTools {
|
||||
get;
|
||||
set;
|
||||
} = [];
|
||||
|
||||
static OS()
|
||||
{
|
||||
|
@ -123,6 +165,59 @@ namespace SourceGit.Native
|
|||
_backend.OpenWithDefaultEditor(file);
|
||||
}
|
||||
|
||||
private static void UpdateGitVersion()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_gitExecutable) || !File.Exists(_gitExecutable))
|
||||
{
|
||||
GitVersionString = string.Empty;
|
||||
GitVersion = new Version(0, 0, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
var start = new ProcessStartInfo();
|
||||
start.FileName = _gitExecutable;
|
||||
start.Arguments = "--version";
|
||||
start.UseShellExecute = false;
|
||||
start.CreateNoWindow = true;
|
||||
start.RedirectStandardOutput = true;
|
||||
start.RedirectStandardError = true;
|
||||
start.StandardOutputEncoding = Encoding.UTF8;
|
||||
start.StandardErrorEncoding = Encoding.UTF8;
|
||||
|
||||
var proc = new Process() { StartInfo = start };
|
||||
try
|
||||
{
|
||||
proc.Start();
|
||||
|
||||
var rs = proc.StandardOutput.ReadToEnd();
|
||||
proc.WaitForExit();
|
||||
if (proc.ExitCode == 0 && !string.IsNullOrWhiteSpace(rs))
|
||||
{
|
||||
GitVersionString = rs.Trim();
|
||||
|
||||
var match = REG_GIT_VERSION().Match(GitVersionString);
|
||||
if (match.Success)
|
||||
{
|
||||
var major = int.Parse(match.Groups[1].Value);
|
||||
var minor = int.Parse(match.Groups[2].Value);
|
||||
var build = int.Parse(match.Groups[3].Value);
|
||||
GitVersion = new Version(major, minor, build);
|
||||
GitVersionString = GitVersionString.Substring(11).Trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Ignore errors
|
||||
}
|
||||
|
||||
proc.Close();
|
||||
}
|
||||
|
||||
[GeneratedRegex(@"^git version[\s\w]*(\d+)\.(\d+)[\.\-](\d+).*$")]
|
||||
private static partial Regex REG_GIT_VERSION();
|
||||
|
||||
private static IBackend _backend = null;
|
||||
private static string _gitExecutable = string.Empty;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue