sourcegit/src/Commands/Worktree.cs

119 lines
3.5 KiB
C#
Raw Normal View History

2024-06-27 03:25:16 -07:00
using System;
using System.Collections.Generic;
namespace SourceGit.Commands
{
2024-07-14 09:30:31 -07:00
public class Worktree : Command
2024-06-27 03:25:16 -07:00
{
public Worktree(string repo)
{
WorkingDirectory = repo;
Context = repo;
}
public List<Models.Worktree> List()
{
Args = "worktree list --porcelain";
var rs = ReadToEnd();
var worktrees = new List<Models.Worktree>();
var last = null as Models.Worktree;
if (rs.IsSuccess)
{
2024-07-14 09:30:31 -07:00
var lines = rs.StdOut.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
2024-06-27 03:25:16 -07:00
foreach (var line in lines)
{
if (line.StartsWith("worktree ", StringComparison.Ordinal))
{
last = new Models.Worktree() { FullPath = line.Substring(9).Trim() };
worktrees.Add(last);
}
else if (line.StartsWith("bare", StringComparison.Ordinal))
{
2024-07-14 09:30:31 -07:00
last!.IsBare = true;
2024-06-27 03:25:16 -07:00
}
else if (line.StartsWith("HEAD ", StringComparison.Ordinal))
{
2024-07-14 09:30:31 -07:00
last!.Head = line.Substring(5).Trim();
2024-06-27 03:25:16 -07:00
}
else if (line.StartsWith("branch ", StringComparison.Ordinal))
{
2024-07-14 09:30:31 -07:00
last!.Branch = line.Substring(7).Trim();
2024-06-27 03:25:16 -07:00
}
else if (line.StartsWith("detached", StringComparison.Ordinal))
{
2024-07-14 09:30:31 -07:00
last!.IsDetached = true;
2024-06-27 03:25:16 -07:00
}
else if (line.StartsWith("locked", StringComparison.Ordinal))
{
2024-07-14 09:30:31 -07:00
last!.IsLocked = true;
2024-06-27 03:25:16 -07:00
}
}
}
return worktrees;
}
public bool Add(string fullpath, string name, bool createNew, string tracking, Action<string> outputHandler)
2024-06-27 03:25:16 -07:00
{
Args = "worktree add ";
if (!string.IsNullOrEmpty(tracking))
Args += "--track ";
if (!string.IsNullOrEmpty(name))
{
if (createNew)
Args += $"-b {name} ";
else
Args += $"-B {name} ";
}
2024-06-27 03:25:16 -07:00
Args += $"\"{fullpath}\" ";
if (!string.IsNullOrEmpty(tracking))
Args += tracking;
_outputHandler = outputHandler;
return Exec();
}
public bool Prune(Action<string> outputHandler)
{
Args = "worktree prune -v";
_outputHandler = outputHandler;
return Exec();
}
public bool Lock(string fullpath)
2024-06-27 03:25:16 -07:00
{
Args = $"worktree lock \"{fullpath}\"";
2024-06-27 03:25:16 -07:00
return Exec();
}
public bool Unlock(string fullpath)
{
Args = $"worktree unlock \"{fullpath}\"";
return Exec();
}
public bool Remove(string fullpath, bool force, Action<string> outputHandler)
{
if (force)
Args = $"worktree remove -f \"{fullpath}\"";
else
Args = $"worktree remove \"{fullpath}\"";
_outputHandler = outputHandler;
return Exec();
}
protected override void OnReadline(string line)
{
_outputHandler?.Invoke(line);
}
private Action<string> _outputHandler = null;
}
}