-
Notifications
You must be signed in to change notification settings - Fork 385
Expand file tree
/
Copy pathCompareCommandPalette.cs
More file actions
114 lines (94 loc) · 2.94 KB
/
CompareCommandPalette.cs
File metadata and controls
114 lines (94 loc) · 2.94 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
using System;
using System.Collections.Generic;
namespace SourceGit.ViewModels
{
public class CompareCommandPalette : ICommandPalette
{
public object BasedOn
{
get => _basedOn;
}
public object CompareTo
{
get => _compareTo;
set => SetProperty(ref _compareTo, value);
}
public List<object> Refs
{
get => _refs;
private set => SetProperty(ref _refs, value);
}
public string Filter
{
get => _filter;
set
{
if (SetProperty(ref _filter, value))
UpdateRefs();
}
}
public CompareCommandPalette(Repository repo, object basedOn)
{
_repo = repo;
_basedOn = basedOn ?? repo.CurrentBranch;
UpdateRefs();
}
public void ClearFilter()
{
Filter = string.Empty;
}
public void Launch()
{
_refs.Clear();
Close();
if (_compareTo != null)
App.ShowWindow(new Compare(_repo, _basedOn, _compareTo));
}
private void UpdateRefs()
{
var refs = new List<object>();
foreach (var b in _repo.Branches)
{
if (b == _basedOn)
continue;
if (string.IsNullOrEmpty(_filter) || b.FriendlyName.Contains(_filter, StringComparison.OrdinalIgnoreCase))
refs.Add(b);
}
foreach (var t in _repo.Tags)
{
if (t == _basedOn)
continue;
if (string.IsNullOrEmpty(_filter) || t.Name.Contains(_filter, StringComparison.OrdinalIgnoreCase))
refs.Add(t);
}
refs.Sort((l, r) =>
{
if (l is Models.Branch lb)
{
if (r is Models.Branch rb)
{
if (lb.IsLocal == rb.IsLocal)
return Models.NumericSort.Compare(lb.FriendlyName, rb.FriendlyName);
return lb.IsLocal ? -1 : 1;
}
return -1;
}
if (r is Models.Branch)
return 1;
return Models.NumericSort.Compare((l as Models.Tag).Name, (r as Models.Tag).Name);
});
var autoSelected = _compareTo;
if (refs.Count == 0)
autoSelected = null;
else if (_compareTo == null || !refs.Contains(_compareTo))
autoSelected = refs[0];
Refs = refs;
CompareTo = autoSelected;
}
private Repository _repo;
private object _basedOn = null;
private object _compareTo = null;
private List<object> _refs = [];
private string _filter;
}
}