-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathClone.cs
More file actions
164 lines (140 loc) · 5.03 KB
/
Clone.cs
File metadata and controls
164 lines (140 loc) · 5.03 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Threading.Tasks;
namespace SourceGit.ViewModels
{
public class Clone : Popup
{
[Required(ErrorMessage = "Remote URL is required")]
[CustomValidation(typeof(Clone), nameof(ValidateRemote))]
public string Remote
{
get => _remote;
set
{
if (SetProperty(ref _remote, value, true))
UseSSH = Models.Remote.IsSSH(value);
}
}
public bool UseSSH
{
get => _useSSH;
set => SetProperty(ref _useSSH, value);
}
public string SSHKey
{
get => _sshKey;
set => SetProperty(ref _sshKey, value);
}
[Required(ErrorMessage = "Parent folder is required")]
[CustomValidation(typeof(Clone), nameof(ValidateParentFolder))]
public string ParentFolder
{
get => _parentFolder;
set => SetProperty(ref _parentFolder, value, true);
}
public string Local
{
get => _local;
set => SetProperty(ref _local, value);
}
public string ExtraArgs
{
get => _extraArgs;
set => SetProperty(ref _extraArgs, value);
}
public bool InitAndUpdateSubmodules
{
get;
set;
} = true;
public Clone(string pageId)
{
_pageId = pageId;
var activeWorkspace = Preferences.Instance.GetActiveWorkspace();
_parentFolder = activeWorkspace?.DefaultCloneDir;
if (string.IsNullOrEmpty(ParentFolder))
_parentFolder = Preferences.Instance.GitDefaultCloneDir;
}
public static ValidationResult ValidateRemote(string remote, ValidationContext _)
{
if (!Models.Remote.IsValidURL(remote))
return new ValidationResult("Invalid remote repository URL format");
return ValidationResult.Success;
}
public static ValidationResult ValidateParentFolder(string folder, ValidationContext _)
{
if (!Directory.Exists(folder))
return new ValidationResult("Given path can NOT be found");
return ValidationResult.Success;
}
public override async Task<bool> Sure()
{
ProgressDescription = "Clone ...";
var log = new CommandLog("Clone");
Use(log);
var succ = await new Commands.Clone(_pageId, _parentFolder, _remote, _local, _useSSH ? _sshKey : "", _extraArgs)
.Use(log)
.ExecAsync();
if (!succ)
return false;
var path = _parentFolder;
if (!string.IsNullOrEmpty(_local))
{
path = Path.GetFullPath(Path.Combine(path, _local));
}
else
{
var name = Path.GetFileName(_remote)!;
if (name.EndsWith(".git", StringComparison.Ordinal))
name = name.Substring(0, name.Length - 4);
else if (name.EndsWith(".bundle", StringComparison.Ordinal))
name = name.Substring(0, name.Length - 7);
path = Path.GetFullPath(Path.Combine(path, name));
}
if (!Directory.Exists(path))
{
Models.Notification.Send(_pageId, $"Folder '{path}' can NOT be found", true);
return false;
}
if (_useSSH && !string.IsNullOrEmpty(_sshKey))
{
await new Commands.Config(path)
.Use(log)
.SetAsync("remote.origin.sshkey", _sshKey);
}
if (InitAndUpdateSubmodules)
{
var submodules = await new Commands.QueryUpdatableSubmodules(path, true).GetResultAsync();
if (submodules.Count > 0)
await new Commands.Submodule(path)
.Use(log)
.UpdateAsync(submodules, true);
}
log.Complete();
var node = Preferences.Instance.FindOrAddNodeByRepositoryPath(path, null, true);
await node.UpdateStatusAsync(false, null);
var launcher = App.GetLauncher();
LauncherPage page = null;
foreach (var one in launcher.Pages)
{
if (one.Node.Id == _pageId)
{
page = one;
break;
}
}
Welcome.Instance.Refresh();
launcher.OpenRepositoryInTab(node, page);
return true;
}
private string _pageId = string.Empty;
private string _remote = string.Empty;
private bool _useSSH = false;
private string _sshKey = string.Empty;
private string _parentFolder = string.Empty;
private string _local = string.Empty;
private string _extraArgs = string.Empty;
}
}