forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorktree.cs
More file actions
111 lines (97 loc) · 3.72 KB
/
Copy pathWorktree.cs
File metadata and controls
111 lines (97 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace SourceGit.Commands
{
public class Worktree : Command
{
public Worktree(string repo)
{
WorkingDirectory = repo;
Context = repo;
}
public async Task<List<Models.Worktree>> ReadAllAsync()
{
Args = "worktree list --porcelain";
var rs = await ReadToEndAsync().ConfigureAwait(false);
var worktrees = new List<Models.Worktree>();
Models.Worktree last = null;
if (rs.IsSuccess)
{
var lines = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
if (line.StartsWith("worktree ", StringComparison.Ordinal))
{
last = new Models.Worktree() { FullPath = line.Substring(9).Trim() };
worktrees.Add(last);
continue;
}
if (last == null)
continue;
if (line.StartsWith("bare", StringComparison.Ordinal))
{
last.IsBare = true;
}
else if (line.StartsWith("HEAD ", StringComparison.Ordinal))
{
last.Head = line.Substring(5).Trim();
}
else if (line.StartsWith("branch ", StringComparison.Ordinal))
{
last.Branch = line.Substring(7).Trim();
}
else if (line.StartsWith("detached", StringComparison.Ordinal))
{
last.IsDetached = true;
}
else if (line.StartsWith("locked", StringComparison.Ordinal))
{
last.IsLocked = true;
}
}
}
return worktrees;
}
public async Task<bool> AddAsync(string fullpath, string name, bool createNew, string tracking)
{
var builder = new StringBuilder(1024);
builder.Append("worktree add ");
if (!string.IsNullOrEmpty(tracking))
builder.Append("--track ");
if (!string.IsNullOrEmpty(name))
builder.Append(createNew ? "-b " : "-B ").Append(name).Append(' ');
builder.Append(fullpath.Quoted()).Append(' ');
if (!string.IsNullOrEmpty(tracking))
builder.Append(tracking);
else if (!string.IsNullOrEmpty(name) && !createNew)
builder.Append(name);
Args = builder.ToString();
return await ExecAsync().ConfigureAwait(false);
}
public async Task<bool> PruneAsync()
{
Args = "worktree prune -v";
return await ExecAsync().ConfigureAwait(false);
}
public async Task<bool> LockAsync(string fullpath)
{
Args = $"worktree lock {fullpath.Quoted()}";
return await ExecAsync().ConfigureAwait(false);
}
public async Task<bool> UnlockAsync(string fullpath)
{
Args = $"worktree unlock {fullpath.Quoted()}";
return await ExecAsync().ConfigureAwait(false);
}
public async Task<bool> RemoveAsync(string fullpath, bool force)
{
if (force)
Args = $"worktree remove -f {fullpath.Quoted()}";
else
Args = $"worktree remove {fullpath.Quoted()}";
return await ExecAsync().ConfigureAwait(false);
}
}
}