-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorktreeManager.cs
More file actions
48 lines (40 loc) · 1.24 KB
/
Copy pathWorktreeManager.cs
File metadata and controls
48 lines (40 loc) · 1.24 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
using System.Diagnostics;
namespace DevTree;
static class WorktreeManager
{
public static void CreateWorktree(string branch)
{
var worktreePath = GetWorktreePath(branch);
Directory.CreateDirectory(Path.Combine(worktreePath, ".devcontainer"));
RunGitCommand($"worktree add {worktreePath} --checkout -b {branch}");
}
public static void RemoveWorktree(string branch)
{
var worktreePath = GetWorktreePath(branch);
RunGitCommand($"worktree remove {worktreePath} --force");
Directory.Delete(worktreePath, recursive: true);
}
public static string GetWorktreePath(string branch)
{
return Path.Combine(Environment.CurrentDirectory, "worktrees", branch);
}
private static void RunGitCommand(string args)
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "git",
Arguments = args,
RedirectStandardOutput = true,
UseShellExecute = false
}
};
process.Start();
process.WaitForExit();
if (process.ExitCode != 0)
{
throw new Exception($"Git command failed: git {args}");
}
}
}