-
Notifications
You must be signed in to change notification settings - Fork 502
Expand file tree
/
Copy pathRepositorySettings.cs
More file actions
156 lines (136 loc) · 4.09 KB
/
Copy pathRepositorySettings.cs
File metadata and controls
156 lines (136 loc) · 4.09 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
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 EnableRecursiveWhenAutoUpdatingSubmodules
{
get;
set;
} = true;
public bool AskBeforeAutoUpdatingSubmodules
{
get;
set;
} = false;
public string PreferredOpenAIService
{
get;
set;
} = "---";
public AvaloniaList<CommitTemplate> CommitTemplates
{
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 void Save()
{
try
{
var content = JsonSerializer.Serialize(this, JsonCodeGen.Default.RepositorySettings);
var hash = HashContent(content);
if (!hash.Equals(_orgHash, StringComparison.Ordinal))
{
var tmpfile = $"{_file}.tmp";
File.WriteAllText(tmpfile, content);
File.Move(tmpfile, _file, true);
_orgHash = hash;
}
}
catch
{
// Ignore save errors
}
}
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));
return Convert.ToHexStringLower(hash);
}
private static Dictionary<string, RepositorySettings> _cache = new();
private string _file = string.Empty;
private string _orgHash = string.Empty;
}
}