forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepositorySettings.cs
More file actions
188 lines (162 loc) · 4.86 KB
/
RepositorySettings.cs
File metadata and controls
188 lines (162 loc) · 4.86 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Avalonia.Collections;
namespace SourceGit.Models
{
public class RepositorySettings
{
public string DefaultRemote
{
get;
set;
} = string.Empty;
public int PreferredMergeMode
{
get;
set;
} = 0;
public string ConventionalTypesOverride
{
get;
set;
} = string.Empty;
public bool EnableAutoFetch
{
get;
set;
} = false;
public int AutoFetchInterval
{
get;
set;
} = 10;
public bool AskBeforeAutoUpdatingSubmodules
{
get;
set;
} = false;
public string PreferredOpenAIService
{
get;
set;
} = "---";
public AvaloniaList<CommitTemplate> CommitTemplates
{
get;
set;
} = [];
public AvaloniaList<string> CommitMessages
{
get;
set;
} = [];
public AvaloniaList<CustomAction> CustomActions
{
get;
set;
} = [];
public static RepositorySettings Get(string gitCommonDir)
{
var fileInfo = new FileInfo(Path.Combine(gitCommonDir, "sourcegit.settings"));
var fullpath = fileInfo.FullName;
if (_cache.TryGetValue(fullpath, out var setting))
return setting;
if (!File.Exists(fullpath))
{
setting = new();
}
else
{
try
{
using var stream = File.OpenRead(fullpath);
setting = JsonSerializer.Deserialize(stream, JsonCodeGen.Default.RepositorySettings);
}
catch
{
setting = new();
}
}
// Serialize setting again to make sure there are no unnecessary whitespaces.
Task.Run(() =>
{
var formatted = JsonSerializer.Serialize(setting, JsonCodeGen.Default.RepositorySettings);
setting._orgHash = HashContent(formatted);
});
setting._file = fullpath;
_cache.Add(fullpath, setting);
return setting;
}
public async Task SaveAsync()
{
try
{
var content = JsonSerializer.Serialize(this, JsonCodeGen.Default.RepositorySettings);
var hash = HashContent(content);
if (!hash.Equals(_orgHash, StringComparison.Ordinal))
{
await File.WriteAllTextAsync(_file, content);
_orgHash = hash;
}
}
catch
{
// Ignore save errors
}
}
public void PushCommitMessage(string message)
{
message = message.Trim().ReplaceLineEndings("\n");
var existIdx = CommitMessages.IndexOf(message);
if (existIdx == 0)
return;
if (existIdx > 0)
{
CommitMessages.Move(existIdx, 0);
return;
}
if (CommitMessages.Count > 9)
CommitMessages.RemoveRange(9, CommitMessages.Count - 9);
CommitMessages.Insert(0, message);
}
public CustomAction AddNewCustomAction()
{
var act = new CustomAction() { Name = "Unnamed Action" };
CustomActions.Add(act);
return act;
}
public void RemoveCustomAction(CustomAction act)
{
if (act != null)
CustomActions.Remove(act);
}
public void MoveCustomActionUp(CustomAction act)
{
var idx = CustomActions.IndexOf(act);
if (idx > 0)
CustomActions.Move(idx - 1, idx);
}
public void MoveCustomActionDown(CustomAction act)
{
var idx = CustomActions.IndexOf(act);
if (idx < CustomActions.Count - 1)
CustomActions.Move(idx + 1, idx);
}
private static string HashContent(string source)
{
var hash = MD5.HashData(Encoding.Default.GetBytes(source));
var builder = new StringBuilder(hash.Length * 2);
foreach (var c in hash)
builder.Append(c.ToString("x2"));
return builder.ToString();
}
private static Dictionary<string, RepositorySettings> _cache = new();
private string _file = string.Empty;
private string _orgHash = string.Empty;
}
}