Skip to content

Commit 10cd971

Browse files
committed
Enhancement and Code Improvements
1 parent 991824f commit 10cd971

File tree

63 files changed

+157
-126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+157
-126
lines changed

Sources/Version-Control/Base/Actions/GitHub/GitHubActions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// GitHubActions.swift
33
//
44
//
5-
// Created by Tihan-Nico Paxton on 2023/09/25.
5+
// Created by Nanashi Li on 2023/09/25.
66
//
77

88
import Foundation

Sources/Version-Control/Base/Commands/Apply.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public struct Apply {
3535
) throws {
3636
// If the file was a rename we have to recreate that rename since we've
3737
// just blown away the index.
38-
if file.status.kind == .renamed {
38+
if file.status?.kind == .renamed {
3939
if let renamedFile = file.status as? CopiedOrRenamedFileStatus {
4040
if renamedFile.kind == .renamed {
4141
try GitShell().git(args: ["add", "--u", "--", renamedFile.oldPath],

Sources/Version-Control/Base/Commands/Cherry-Pick.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public struct CherryPick {
225225
progressCallback: ((MultiCommitOperationProgress) -> Void)? = nil
226226
) throws -> CherryPickResult {
227227
// Only stage files related to cherry-pick
228-
let trackedFiles = files.filter { $0.status.kind != .untracked }
228+
let trackedFiles = files.filter { $0.status?.kind != .untracked }
229229

230230
// Apply conflict resolutions
231231
for (path, resolution) in manualResolutions {

Sources/Version-Control/Base/Commands/Config.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ public struct Config {
9393
/// - Note:
9494
/// This function retrieves a global Git configuration value using the `getConfigValueInPath`
9595
/// function with appropriate parameters.
96-
public func getGlobalConfigValue(name: String) throws -> String? {
96+
public func getGlobalConfigValue(
97+
path: URL,
98+
name: String
99+
) throws -> String? {
97100
return try getConfigValueInPath(name: name,
98-
path: nil,
101+
path: path,
99102
onlyLocal: false,
100103
type: nil)
101104
}
@@ -134,9 +137,12 @@ public struct Config {
134137
/// - Note:
135138
/// This function retrieves and interprets a global Git configuration value as a boolean by
136139
/// using the `getConfigValueInPath` function with appropriate parameters.
137-
public func getGlobalBooleanConfigValue(name: String) throws -> Bool? {
140+
public func getGlobalBooleanConfigValue(
141+
path: URL,
142+
name: String
143+
) throws -> Bool? {
138144
let value = try getConfigValueInPath(name: name,
139-
path: nil,
145+
path: path,
140146
onlyLocal: false,
141147
type: Bool.self)
142148
return value == nil ? nil : (value != nil) != false
@@ -181,18 +187,18 @@ public struct Config {
181187
/// - If `onlyLocal` is `true`, the global configuration is not considered.
182188
/// - The `type` parameter is optional and can be used to specify the expected type of the configuration value.
183189
public func getConfigValueInPath(name: String,
184-
path: URL?,
190+
path: URL,
185191
onlyLocal: Bool = false,
186192
type: Any?) throws -> String? {
187193

188194
var gitCommand: String
189195

190196
var flags = ["config", "-z"]
191197

192-
if path == nil {
193-
flags.append("--global")
194-
} else if onlyLocal {
198+
if onlyLocal {
195199
flags.append("--local")
200+
} else {
201+
flags.append("--global")
196202
}
197203

198204
if let type = type {
@@ -203,7 +209,7 @@ public struct Config {
203209

204210
let result = try GitShell().git(
205211
args: flags,
206-
path: path ?? URL(string: "")!,
212+
path: path,
207213
name: #function,
208214
options: IGitExecutionOptions(
209215
successExitCodes: Set([0, 1])

Sources/Version-Control/Base/Commands/Diff.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,13 @@ public struct GitDiff { // swiftlint:disable:this type_body_length
432432
] + (hideWhitespaceInDiff ? ["-w"] : [])
433433

434434
var successExitCodes: Set<Int> = [0]
435-
let isSubmodule = file.status.submoduleStatus != nil
435+
let isSubmodule = file.status?.submoduleStatus != nil
436436

437437
// If the file is new or untracked, and it's not a submodule, use `--no-index`
438-
if !isSubmodule && (file.status.kind == .new || file.status.kind == .untracked) {
438+
if !isSubmodule && (file.status?.kind == .new || file.status?.kind == .untracked) {
439439
args += ["--no-index", "--", "/dev/null", file.path]
440440
successExitCodes.insert(1) // Exit code 1 is also considered a success in this context
441-
} else if file.status.kind == .renamed {
441+
} else if file.status?.kind == .renamed {
442442
args += ["--", file.path]
443443
} else {
444444
args += ["HEAD", "--", file.path]
@@ -489,25 +489,25 @@ public struct GitDiff { // swiftlint:disable:this type_body_length
489489
var current: DiffImage?
490490
var previous: DiffImage?
491491

492-
if file.status.kind != .conflicted {
493-
if file.status.kind != .deleted {
492+
if file.status?.kind != .conflicted {
493+
if file.status?.kind != .deleted {
494494
current = try getWorkingDirectoryImage(directoryURL: directoryURL, file: file)
495495
}
496496

497-
if file.status.kind != .new && file.status.kind != .untracked {
497+
if file.status?.kind != .new && file.status?.kind != .untracked {
498498
previous = try getBlobImage(directoryURL: directoryURL,
499499
path: FileUtils().getOldPathOrDefault(file: file),
500500
commitish: "HEAD")
501501
}
502502
}
503503

504-
if file.status.kind != .deleted {
504+
if file.status?.kind != .deleted {
505505
current = try getBlobImage(directoryURL: directoryURL,
506506
path: file.path,
507507
commitish: oldestCommitish)
508508
}
509509

510-
if file.status.kind != .new && file.status.kind != .untracked {
510+
if file.status?.kind != .new && file.status?.kind != .untracked {
511511
previous = try getBlobImage(directoryURL: directoryURL,
512512
path: FileUtils().getOldPathOrDefault(file: file),
513513
commitish: "\(oldestCommitish)^")
@@ -650,8 +650,8 @@ public struct GitDiff { // swiftlint:disable:this type_body_length
650650
var newSHA: String?
651651

652652
if status.commitChanged ||
653-
file.status.kind == .new ||
654-
file.status.kind == .deleted {
653+
file.status?.kind == .new ||
654+
file.status?.kind == .deleted {
655655
let lines = buffer.split(separator: "\n")
656656
let baseRegex = "Subproject commit ([^-]+)(-dirty)?$"
657657
guard let oldSHARegex = try? NSRegularExpression(pattern: "-" + baseRegex),
@@ -697,11 +697,11 @@ public struct GitDiff { // swiftlint:disable:this type_body_length
697697
file: FileChange,
698698
oldestCommitish: String,
699699
lineEndingChange: LineEndingsChange?) throws -> IDiff {
700-
if file.status.submoduleStatus != nil {
700+
if file.status?.submoduleStatus != nil {
701701
return try buildSubmoduleDiff(buffer: buffer,
702702
directoryURL: directoryURL,
703703
file: file,
704-
status: file.status.submoduleStatus!)
704+
status: (file.status?.submoduleStatus)!)
705705
}
706706

707707
if !isValidBuffer(buffer) {

Sources/Version-Control/Base/Commands/GitLog.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,16 @@ public struct GitLog {
511511
linesAdded += added == "-" ? 0 : Int(added)!
512512
linesDeleted += deleted == "-" ? 0 : Int(deleted)!
513513

514-
if isCopyOrRename(status: files[numStatCount].status) {
514+
guard let fileStatus = files[numStatCount].status else {
515+
let missingFileStatusError = NSError(
516+
domain: "com.auroraeditor.fileitem",
517+
code: 1001,
518+
userInfo: [NSLocalizedDescriptionKey: "File status is missing"]
519+
)
520+
throw missingFileStatusError
521+
}
522+
523+
if isCopyOrRename(status: fileStatus) {
515524
number += 2
516525
}
517526
numStatCount += 1

Sources/Version-Control/Base/Commands/Interpret-Trailers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public struct Trailer: Codable, ITrailer, Hashable {
1919
public var token: String = ""
2020
public var value: String = ""
2121

22-
init(token: String, value: String) {
22+
public init(token: String, value: String) {
2323
self.token = token
2424
self.value = value
2525
}

Sources/Version-Control/Base/Commands/Rebase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public struct Rebase {
203203
progressCallback: ((MultiCommitOperationProgress) -> Void)?,
204204
gitEditor: String = ":") async throws -> RebaseResult {
205205

206-
let trackedFiles = files.filter { $0.status.kind != .untracked }
206+
let trackedFiles = files.filter { $0.status?.kind != .untracked }
207207

208208
// Apply conflict resolutions
209209
for (path, resolution) in manualResolutions {
@@ -228,7 +228,7 @@ public struct Rebase {
228228
return .aborted
229229
}
230230

231-
let trackedFilesAfter = status.workingDirectory.files.filter { $0.status.kind != .untracked }
231+
let trackedFilesAfter = status.workingDirectory.files.filter { $0.status?.kind != .untracked }
232232

233233
var options = IGitExecutionOptions(env: ["GIT_EDITOR": gitEditor],
234234
expectedErrors: [.RebaseConflicts, .UnresolvedConflicts])

Sources/Version-Control/Base/Commands/Stage.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ public struct GitStage {
1818
func stageManualConflictResolution(directoryURL: URL,
1919
file: WorkingDirectoryFileChange,
2020
manualResolution: ManualConflictResolution) throws {
21-
if !isConflictedFileStatus(file.status) {
21+
guard let fileStatus = file.status else {
22+
print("File status is nil")
23+
return
24+
}
25+
if !isConflictedFileStatus(fileStatus) {
2226
print("Tried to manually resolve unconflicted file (\(file.path))")
2327
return
2428
}
2529

26-
guard let conflictedStatus = file.status as? ConflictsWithMarkers else {
30+
guard let conflictedStatus = fileStatus as? ConflictsWithMarkers else {
2731
print("Failed to cast to ConflictsWithMarkers")
2832
return
2933
}

Sources/Version-Control/Base/Commands/Update-Index.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public struct UpdateIndex {
140140
normal.append(file.path)
141141

142142
guard let fileStatus = file.status as? CopiedOrRenamedFileStatus else {
143-
if file.status.kind == .deleted {
143+
if file.status?.kind == .deleted {
144144
deletedFiles.append(file.path)
145145
}
146146
continue

Sources/Version-Control/Base/Core/Parsers/PatchFormatterParser.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ struct PatchFormatterParser {
4747
/// The `formatPatchHeader` helper function is used to generate the appropriate header based on the file's status.
4848

4949
func formatPatchHeaderForFile(file: WorkingDirectoryFileChange) -> String {
50-
switch file.status.kind {
50+
switch file.status?.kind {
5151
case .new, .untracked:
5252
return formatPatchHeader(fromPath: nil, toPath: file.path)
5353
case .renamed, .deleted, .modified, .copied, .conflicted:
5454
return formatPatchHeader(fromPath: file.path, toPath: file.path)
55+
default:
56+
return ""
5557
}
5658
}
5759

@@ -263,7 +265,7 @@ struct PatchFormatterParser {
263265
}
264266

265267
anyAdditionsOrDeletions = true
266-
} else if file.status.kind == .new || file.status.kind == .untracked {
268+
} else if file.status?.kind == .new || file.status?.kind == .untracked {
267269
// Unselected lines in new files are ignored
268270
continue
269271
} else if line.type == .add {

Sources/Version-Control/Base/Core/ProcessError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// ProcessError.swift
33
//
44
//
5-
// Created by Tihan-Nico Paxton on 2024/07/02.
5+
// Created by Nanashi Li on 2024/07/02.
66
//
77

88
// Process Specific Errors

Sources/Version-Control/Base/Models/Files/AppFileStatus.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
public enum GitStatusEntry: String {
10+
public enum GitStatusEntry: String, Codable {
1111
case modified = "M"
1212
case added = "A"
1313
case deleted = "D"
@@ -19,28 +19,28 @@ public enum GitStatusEntry: String {
1919
case updatedButUnmerged = "U"
2020
}
2121

22-
public enum AppFileStatusKind {
23-
case new
24-
case modified
25-
case deleted
26-
case copied
27-
case renamed
28-
case conflicted
29-
case untracked
22+
public enum AppFileStatusKind: String, Codable {
23+
case new = "New"
24+
case modified = "Modified"
25+
case deleted = "Deleted"
26+
case copied = "Copied"
27+
case renamed = "Renamed"
28+
case conflicted = "Conflicted"
29+
case untracked = "Untracked"
3030
}
3131

32-
public struct SubmoduleStatus {
32+
public struct SubmoduleStatus: Codable {
3333
let commitChanged: Bool
3434
let modifiedChanges: Bool
3535
let untrackedChanges: Bool
3636
}
3737

38-
public struct PlainFileStatus: AppFileStatus {
38+
public struct PlainFileStatus: AppFileStatus, Codable {
3939
public var kind: AppFileStatusKind
4040
public var submoduleStatus: SubmoduleStatus?
4141
}
4242

43-
public struct CopiedOrRenamedFileStatus: AppFileStatus {
43+
public struct CopiedOrRenamedFileStatus: AppFileStatus, Codable {
4444
public var kind: AppFileStatusKind
4545
let oldPath: String
4646
public var submoduleStatus: SubmoduleStatus?
@@ -49,14 +49,14 @@ public struct CopiedOrRenamedFileStatus: AppFileStatus {
4949
// MARK: - Conflicted
5050
public protocol ConflictedFileStatus: AppFileStatus {}
5151

52-
public struct ConflictsWithMarkers: ConflictedFileStatus {
52+
public struct ConflictsWithMarkers: ConflictedFileStatus, Codable {
5353
public var kind: AppFileStatusKind
5454
let entry: TextConflictEntry
5555
let conflictMarkerCount: Int
5656
public var submoduleStatus: SubmoduleStatus?
5757
}
5858

59-
public struct ManualConflict: ConflictedFileStatus {
59+
public struct ManualConflict: ConflictedFileStatus, Codable {
6060
public var kind: AppFileStatusKind
6161
let entry: ManualConflictEntry
6262
public var submoduleStatus: SubmoduleStatus?
@@ -74,17 +74,17 @@ public func isManualConflict(_ conflictedFileStatus: ConflictedFileStatus) -> Bo
7474
return conflictedFileStatus is ManualConflict
7575
}
7676

77-
public struct UntrackedFileStatus: AppFileStatus {
77+
public struct UntrackedFileStatus: AppFileStatus, Codable {
7878
public var kind: AppFileStatusKind
7979
public var submoduleStatus: SubmoduleStatus?
8080
}
8181

82-
public protocol AppFileStatus {
82+
public protocol AppFileStatus: Codable {
8383
var kind: AppFileStatusKind { get set }
8484
var submoduleStatus: SubmoduleStatus? { get set }
8585
}
8686

87-
public enum UnmergedEntrySummary: String {
87+
public enum UnmergedEntrySummary: String, Codable {
8888
case AddedByUs = "added-by-us"
8989
case DeletedByUs = "deleted-by-us"
9090
case AddedByThem = "added-by-them"
@@ -94,14 +94,14 @@ public enum UnmergedEntrySummary: String {
9494
case BothModified = "both-modified"
9595
}
9696

97-
public struct ManualConflictDetails {
97+
public struct ManualConflictDetails: Codable {
9898
let submoduleStatus: SubmoduleStatus?
9999
let action: UnmergedEntrySummary
100100
let us: GitStatusEntry
101101
let them: GitStatusEntry
102102
}
103103

104-
public struct TextConflictDetails {
104+
public struct TextConflictDetails: Codable {
105105
let action: UnmergedEntrySummary
106106
let us: GitStatusEntry
107107
let them: GitStatusEntry
@@ -116,13 +116,13 @@ protocol FileEntry {
116116

117117
protocol UnmergedEntry {}
118118

119-
public struct TextConflictEntry: FileEntry, UnmergedEntry {
119+
public struct TextConflictEntry: Codable, FileEntry, UnmergedEntry {
120120
let kind: String = "conflicted"
121121
let submoduleStatus: SubmoduleStatus?
122122
let details: TextConflictDetails
123123
}
124124

125-
public struct ManualConflictEntry: FileEntry, UnmergedEntry {
125+
public struct ManualConflictEntry: Codable, FileEntry, UnmergedEntry {
126126
let kind: String = "conflicted"
127127
let submoduleStatus: SubmoduleStatus?
128128
let details: ManualConflictDetails

0 commit comments

Comments
 (0)