|
1 |
| -using Avalonia.Collections; |
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +using Avalonia.Collections; |
| 6 | +using Avalonia.Threading; |
2 | 7 |
|
3 | 8 | namespace SourceGit.Models
|
4 | 9 | {
|
@@ -76,11 +81,11 @@ public bool CheckoutBranchOnCreateBranch
|
76 | 81 | set;
|
77 | 82 | } = true;
|
78 | 83 |
|
79 |
| - public AvaloniaList<string> Filters |
| 84 | + public AvaloniaList<Filter> HistoriesFilters |
80 | 85 | {
|
81 | 86 | get;
|
82 | 87 | set;
|
83 |
| - } = new AvaloniaList<string>(); |
| 88 | + } = new AvaloniaList<Filter>(); |
84 | 89 |
|
85 | 90 | public AvaloniaList<CommitTemplate> CommitTemplates
|
86 | 91 | {
|
@@ -148,6 +153,179 @@ public string PreferedOpenAIService
|
148 | 153 | set;
|
149 | 154 | } = "---";
|
150 | 155 |
|
| 156 | + public FilterMode GetHistoriesFilterMode(string pattern, FilterType type) |
| 157 | + { |
| 158 | + foreach (var filter in HistoriesFilters) |
| 159 | + { |
| 160 | + if (filter.Type != type) |
| 161 | + continue; |
| 162 | + |
| 163 | + if (filter.Pattern.Equals(pattern, StringComparison.Ordinal)) |
| 164 | + return filter.Mode; |
| 165 | + } |
| 166 | + |
| 167 | + return FilterMode.None; |
| 168 | + } |
| 169 | + |
| 170 | + public bool UpdateHistoriesFilter(string pattern, FilterType type, FilterMode mode) |
| 171 | + { |
| 172 | + for (int i = 0; i < HistoriesFilters.Count; i++) |
| 173 | + { |
| 174 | + var filter = HistoriesFilters[i]; |
| 175 | + if (filter.Type != type) |
| 176 | + continue; |
| 177 | + |
| 178 | + if (filter.Pattern.Equals(pattern, StringComparison.Ordinal)) |
| 179 | + { |
| 180 | + if (mode == FilterMode.None) |
| 181 | + { |
| 182 | + HistoriesFilters.RemoveAt(i); |
| 183 | + return true; |
| 184 | + } |
| 185 | + |
| 186 | + if (mode != filter.Mode) |
| 187 | + { |
| 188 | + filter.Mode = mode; |
| 189 | + return true; |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + if (mode != FilterMode.None) |
| 195 | + { |
| 196 | + HistoriesFilters.Add(new Filter(pattern, type, mode)); |
| 197 | + return true; |
| 198 | + } |
| 199 | + |
| 200 | + return false; |
| 201 | + } |
| 202 | + |
| 203 | + public string BuildHistoriesFilter() |
| 204 | + { |
| 205 | + var builder = new StringBuilder(); |
| 206 | + |
| 207 | + var excludedBranches = new List<string>(); |
| 208 | + var excludedRemotes = new List<string>(); |
| 209 | + var excludedTags = new List<string>(); |
| 210 | + var includedBranches = new List<string>(); |
| 211 | + var includedRemotes = new List<string>(); |
| 212 | + var includedTags = new List<string>(); |
| 213 | + foreach (var filter in HistoriesFilters) |
| 214 | + { |
| 215 | + if (filter.Type == FilterType.LocalBranch) |
| 216 | + { |
| 217 | + var name = filter.Pattern.Substring(11); |
| 218 | + var b = $"{name.Substring(0, name.Length - 1)}[{name[^1]}]"; |
| 219 | + |
| 220 | + if (filter.Mode == FilterMode.Included) |
| 221 | + includedBranches.Add(b); |
| 222 | + else if (filter.Mode == FilterMode.Excluded) |
| 223 | + excludedBranches.Add(b); |
| 224 | + } |
| 225 | + else if (filter.Type == FilterType.LocalBranchFolder) |
| 226 | + { |
| 227 | + if (filter.Mode == FilterMode.Included) |
| 228 | + includedBranches.Add($"{filter.Pattern.Substring(11)}/*"); |
| 229 | + else if (filter.Mode == FilterMode.Excluded) |
| 230 | + excludedBranches.Add($"{filter.Pattern.Substring(11)}/*"); |
| 231 | + } |
| 232 | + else if (filter.Type == FilterType.RemoteBranch) |
| 233 | + { |
| 234 | + var name = filter.Pattern.Substring(13); |
| 235 | + var r = $"{name.Substring(0, name.Length - 1)}[{name[^1]}]"; |
| 236 | + |
| 237 | + if (filter.Mode == FilterMode.Included) |
| 238 | + includedRemotes.Add(r); |
| 239 | + else if (filter.Mode == FilterMode.Excluded) |
| 240 | + excludedRemotes.Add(r); |
| 241 | + } |
| 242 | + else if (filter.Type == FilterType.RemoteBranchFolder) |
| 243 | + { |
| 244 | + if (filter.Mode == FilterMode.Included) |
| 245 | + includedRemotes.Add($"{filter.Pattern.Substring(13)}/*"); |
| 246 | + else if (filter.Mode == FilterMode.Excluded) |
| 247 | + excludedRemotes.Add($"{filter.Pattern.Substring(13)}/*"); |
| 248 | + } |
| 249 | + else if (filter.Type == FilterType.Tag) |
| 250 | + { |
| 251 | + var name = filter.Pattern; |
| 252 | + var t = $"{name.Substring(0, name.Length - 1)}[{name[^1]}]"; |
| 253 | + |
| 254 | + if (filter.Mode == FilterMode.Included) |
| 255 | + includedTags.Add(t); |
| 256 | + else if (filter.Mode == FilterMode.Excluded) |
| 257 | + excludedTags.Add(t); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + foreach (var b in excludedBranches) |
| 262 | + { |
| 263 | + builder.Append("--exclude="); |
| 264 | + builder.Append(b); |
| 265 | + builder.Append(' '); |
| 266 | + } |
| 267 | + |
| 268 | + if (includedBranches.Count > 0) |
| 269 | + { |
| 270 | + foreach (var b in includedBranches) |
| 271 | + { |
| 272 | + builder.Append("--branches="); |
| 273 | + builder.Append(b); |
| 274 | + builder.Append(' '); |
| 275 | + } |
| 276 | + } |
| 277 | + else if (excludedBranches.Count > 0 || (includedRemotes.Count == 0 && includedTags.Count == 0)) |
| 278 | + { |
| 279 | + builder.Append("--exclude=HEA[D] "); |
| 280 | + builder.Append("--branches "); |
| 281 | + } |
| 282 | + |
| 283 | + foreach (var r in excludedRemotes) |
| 284 | + { |
| 285 | + builder.Append("--exclude="); |
| 286 | + builder.Append(r); |
| 287 | + builder.Append(' '); |
| 288 | + } |
| 289 | + |
| 290 | + if (includedRemotes.Count > 0) |
| 291 | + { |
| 292 | + foreach (var r in includedRemotes) |
| 293 | + { |
| 294 | + builder.Append("--remotes="); |
| 295 | + builder.Append(r); |
| 296 | + builder.Append(' '); |
| 297 | + } |
| 298 | + } |
| 299 | + else if (excludedRemotes.Count > 0 || (includedBranches.Count == 0 && includedTags.Count == 0)) |
| 300 | + { |
| 301 | + builder.Append("--exclude=origin/HEA[D] "); |
| 302 | + builder.Append("--remotes "); |
| 303 | + } |
| 304 | + |
| 305 | + foreach (var t in excludedTags) |
| 306 | + { |
| 307 | + builder.Append("--exclude="); |
| 308 | + builder.Append(t); |
| 309 | + builder.Append(' '); |
| 310 | + } |
| 311 | + |
| 312 | + if (includedTags.Count > 0) |
| 313 | + { |
| 314 | + foreach (var t in includedTags) |
| 315 | + { |
| 316 | + builder.Append("--tags="); |
| 317 | + builder.Append(t); |
| 318 | + builder.Append(' '); |
| 319 | + } |
| 320 | + } |
| 321 | + else if (excludedTags.Count > 0 || (includedBranches.Count == 0 && includedRemotes.Count == 0)) |
| 322 | + { |
| 323 | + builder.Append("--tags "); |
| 324 | + } |
| 325 | + |
| 326 | + return builder.ToString(); |
| 327 | + } |
| 328 | + |
151 | 329 | public void PushCommitMessage(string message)
|
152 | 330 | {
|
153 | 331 | var existIdx = CommitMessages.IndexOf(message);
|
|
0 commit comments