forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApply.cs
More file actions
118 lines (95 loc) · 3.36 KB
/
Copy pathApply.cs
File metadata and controls
118 lines (95 loc) · 3.36 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
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Threading.Tasks;
using Avalonia.Input.Platform;
namespace SourceGit.ViewModels
{
public class Apply : Popup
{
[CustomValidation(typeof(Apply), nameof(ValidatePatchFile))]
public string PatchFile
{
get => _patchFile;
set => SetProperty(ref _patchFile, value, true);
}
public bool IgnoreWhiteSpace
{
get => _ignoreWhiteSpace;
set => SetProperty(ref _ignoreWhiteSpace, value);
}
public Models.ApplyWhiteSpaceMode SelectedWhiteSpaceMode
{
get;
set;
}
public bool FromClipboard
{
get => _fromClipboard;
set => SetProperty(ref _fromClipboard, value);
}
public bool ThreeWayMerge
{
get;
set;
}
public IClipboard Clipboard
{
get;
set;
} = null;
public Apply(Repository repo)
{
_repo = repo;
SelectedWhiteSpaceMode = Models.ApplyWhiteSpaceMode.Supported[0];
}
public static ValidationResult ValidatePatchFile(string file, ValidationContext ctx)
{
if (ctx.ObjectInstance is not Apply apply)
return new ValidationResult("Invalid object instance!!!");
if (apply.FromClipboard)
return ValidationResult.Success;
if (string.IsNullOrEmpty(file))
return new ValidationResult("Please select a patch file!!!");
if (!File.Exists(file))
return new ValidationResult($"File '{file}' can NOT be found!!!");
return ValidationResult.Success;
}
public override async Task<bool> Sure()
{
using var lockWatcher = _repo.LockWatcher();
ProgressDescription = "Apply patch...";
var finalPatchFile = _patchFile;
if (_fromClipboard)
{
if (Clipboard == null)
{
_repo.SendNotification("Clipboard service is not available!!!", true);
return false;
}
var content = await Clipboard.TryGetTextAsync();
if (string.IsNullOrEmpty(content) || !content.StartsWith("diff --git ", StringComparison.Ordinal))
{
_repo.SendNotification("There's no valid patch content in clipboard!!!", true);
return false;
}
finalPatchFile = Path.GetTempFileName();
File.WriteAllText(finalPatchFile, content);
}
var log = _repo.CreateLog("Apply Patch");
Use(log);
var extra = ThreeWayMerge ? "--3way" : string.Empty;
var succ = await new Commands.Apply(_repo.FullPath, finalPatchFile, _ignoreWhiteSpace, SelectedWhiteSpaceMode.Arg, extra)
.Use(log)
.ExecAsync();
log.Complete();
if (_fromClipboard)
File.Delete(finalPatchFile);
return succ;
}
private readonly Repository _repo = null;
private string _patchFile = string.Empty;
private bool _fromClipboard = false;
private bool _ignoreWhiteSpace = true;
}
}