Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.8
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down Expand Up @@ -33,5 +33,9 @@ let package = Package(
name: "SwiftGitXTests",
dependencies: ["SwiftGitX"]
)
],
swiftLanguageModes: [
.v5,
.v6
]
)
13 changes: 11 additions & 2 deletions Sources/SwiftGitX/Collections/BranchCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ public enum BranchCollectionError: Error {

/// A collection of branches and their operations.
public struct BranchCollection: Sequence {
private let repositoryPointer: OpaquePointer
private var repositoryPointer: OpaquePointer {
get {
repositoryPointerProtector.read { $0 }
}
set {
repositoryPointerProtector.write(newValue)
}
}

private let repositoryPointerProtector: Protected<OpaquePointer>

init(repositoryPointer: OpaquePointer) {
self.repositoryPointer = repositoryPointer
self.repositoryPointerProtector = Protected(repositoryPointer)
}

// * I am not sure calling `git_error_last()` from a computed property is safe.
Expand Down
15 changes: 12 additions & 3 deletions Sources/SwiftGitX/Collections/ConfigCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ import libgit2
// ? Should we use actor?
/// A collection of configurations and their operations.
public struct ConfigCollection {
private let repositoryPointer: OpaquePointer?
private var repositoryPointer: OpaquePointer? {
get {
repositoryPointerProtector.read { $0 }
}
set {
repositoryPointerProtector.write(newValue)
}
}

private let repositoryPointerProtector: Protected<OpaquePointer?>

/// Init for repository configurations.
init(repositoryPointer: OpaquePointer) {
self.repositoryPointer = repositoryPointer
self.repositoryPointerProtector = Protected(repositoryPointer)
}

/// Init for global configurations.
init() {
repositoryPointer = nil
self.repositoryPointerProtector = Protected(nil)
}

/// The default branch name of the repository
Expand Down
13 changes: 11 additions & 2 deletions Sources/SwiftGitX/Collections/IndexCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ public enum IndexError: Error {

/// A collection of index operations.
struct IndexCollection {
private let repositoryPointer: OpaquePointer
private var repositoryPointer: OpaquePointer {
get {
repositoryPointerProtector.read { $0 }
}
set {
repositoryPointerProtector.write(newValue)
}
}

private let repositoryPointerProtector: Protected<OpaquePointer>

init(repositoryPointer: OpaquePointer) {
self.repositoryPointer = repositoryPointer
self.repositoryPointerProtector = Protected(repositoryPointer)
}

/// The error message from the last failed operation.
Expand Down
13 changes: 11 additions & 2 deletions Sources/SwiftGitX/Collections/ReferenceCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,19 @@ public enum ReferenceCollectionError: Error {

/// A collection of references and their operations.
public struct ReferenceCollection: Sequence {
private let repositoryPointer: OpaquePointer
private var repositoryPointer: OpaquePointer {
get {
repositoryPointerProtector.read { $0 }
}
set {
repositoryPointerProtector.write(newValue)
}
}

private let repositoryPointerProtector: Protected<OpaquePointer>

init(repositoryPointer: OpaquePointer) {
self.repositoryPointer = repositoryPointer
self.repositoryPointerProtector = Protected(repositoryPointer)
}

// * I am not sure calling `git_error_last()` from a computed property is safe.
Expand Down
13 changes: 11 additions & 2 deletions Sources/SwiftGitX/Collections/RemoteCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ public enum RemoteCollectionError: Error, Equatable {

/// A collection of remotes and their operations.
public struct RemoteCollection: Sequence {
private let repositoryPointer: OpaquePointer
private var repositoryPointer: OpaquePointer {
get {
repositoryPointerProtector.read { $0 }
}
set {
repositoryPointerProtector.write(newValue)
}
}

private let repositoryPointerProtector: Protected<OpaquePointer>

init(repositoryPointer: OpaquePointer) {
self.repositoryPointer = repositoryPointer
self.repositoryPointerProtector = Protected(repositoryPointer)
}

// * I am not sure calling `git_error_last()` from a computed property is safe.
Expand Down
13 changes: 11 additions & 2 deletions Sources/SwiftGitX/Collections/StashCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ public enum StashCollectionError: Error, Equatable {

/// A collection of stashes and their operations.
public struct StashCollection: Sequence {
private let repositoryPointer: OpaquePointer
private var repositoryPointer: OpaquePointer {
get {
repositoryPointerProtector.read { $0 }
}
set {
repositoryPointerProtector.write(newValue)
}
}

private let repositoryPointerProtector: Protected<OpaquePointer>

init(repositoryPointer: OpaquePointer) {
self.repositoryPointer = repositoryPointer
self.repositoryPointerProtector = Protected(repositoryPointer)
}

private var errorMessage: String {
Expand Down
13 changes: 11 additions & 2 deletions Sources/SwiftGitX/Collections/TagCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ public enum TagCollectionError: Error {

/// A collection of tags and their operations.
public struct TagCollection: Sequence {
private var repositoryPointer: OpaquePointer
private var repositoryPointer: OpaquePointer {
get {
repositoryPointerProtector.read { $0 }
}
set {
repositoryPointerProtector.write(newValue)
}
}

private let repositoryPointerProtector: Protected<OpaquePointer>

init(repositoryPointer: OpaquePointer) {
self.repositoryPointer = repositoryPointer
self.repositoryPointerProtector = Protected(repositoryPointer)
}

// * I am not sure calling `git_error_last()` from a computed property is safe.
Expand Down
181 changes: 181 additions & 0 deletions Sources/SwiftGitX/Helpers/Protected.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
//
// Protected.swift
//
// Copyright (c) 2014-2020 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

import Foundation

private protocol Lock: Sendable {
func lock()
func unlock()
}

extension Lock {
/// Executes a closure returning a value while acquiring the lock.
///
/// - Parameter closure: The closure to run.
///
/// - Returns: The value the closure generated.
func around<T>(_ closure: () throws -> T) rethrows -> T {
lock(); defer { unlock() }
return try closure()
}

/// Execute a closure while acquiring the lock.
///
/// - Parameter closure: The closure to run.
func around(_ closure: () throws -> Void) rethrows {
lock(); defer { unlock() }
try closure()
}
}

#if canImport(Darwin)
// Number of Apple engineers who insisted on inspecting this: 5
/// An `os_unfair_lock` wrapper.
final class UnfairLock: Lock, @unchecked Sendable {
private let unfairLock: os_unfair_lock_t

init() {
unfairLock = .allocate(capacity: 1)
unfairLock.initialize(to: os_unfair_lock())
}

deinit {
unfairLock.deinitialize(count: 1)
unfairLock.deallocate()
}

fileprivate func lock() {
os_unfair_lock_lock(unfairLock)
}

fileprivate func unlock() {
os_unfair_lock_unlock(unfairLock)
}
}

#elseif canImport(Foundation)
extension NSLock: Lock {}
#else
#error("This platform needs a Lock-conforming type without Foundation.")
#endif

/// A thread-safe wrapper around a value.
@dynamicMemberLookup
final class Protected<Value> {
#if canImport(Darwin)
private let lock = UnfairLock()
#elseif canImport(Foundation)
private let lock = NSLock()
#else
#error("This platform needs a Lock-conforming type without Foundation.")
#endif
#if compiler(>=6)
private nonisolated(unsafe) var value: Value
#else
private var value: Value
#endif

init(_ value: Value) {
self.value = value
}

/// Synchronously read or transform the contained value.
///
/// - Parameter closure: The closure to execute.
///
/// - Returns: The return value of the closure passed.
func read<U>(_ closure: (Value) throws -> U) rethrows -> U {
try lock.around { try closure(self.value) }
}

/// Synchronously modify the protected value.
///
/// - Parameter closure: The closure to execute.
///
/// - Returns: The modified value.
@discardableResult
func write<U>(_ closure: (inout Value) throws -> U) rethrows -> U {
try lock.around { try closure(&self.value) }
}

/// Synchronously update the protected value.
///
/// - Parameter value: The `Value`.
func write(_ value: Value) {
write { $0 = value }
}

subscript<Property>(dynamicMember keyPath: WritableKeyPath<Value, Property>) -> Property {
get { lock.around { value[keyPath: keyPath] } }
set { lock.around { value[keyPath: keyPath] = newValue } }
}

subscript<Property>(dynamicMember keyPath: KeyPath<Value, Property>) -> Property {
lock.around { value[keyPath: keyPath] }
}
}

#if compiler(>=6)
extension Protected: Sendable {}
#else
extension Protected: @unchecked Sendable {}
#endif

/*
extension Protected where Value == Request.MutableState {
/// Attempts to transition to the passed `State`.
///
/// - Parameter state: The `State` to attempt transition to.
///
/// - Returns: Whether the transition occurred.
func attemptToTransitionTo(_ state: Request.State) -> Bool {
lock.around {
guard value.state.canTransitionTo(state) else { return false }

value.state = state

return true
}
}

/// Perform a closure while locked with the provided `Request.State`.
///
/// - Parameter perform: The closure to perform while locked.
func withState(perform: (Request.State) -> Void) {
lock.around { perform(value.state) }
}
}
*/

extension Protected: Equatable where Value: Equatable {
static func ==(lhs: Protected<Value>, rhs: Protected<Value>) -> Bool {
lhs.read { left in rhs.read { right in left == right }}
}
}

extension Protected: Hashable where Value: Hashable {
func hash(into hasher: inout Hasher) {
read { hasher.combine($0) }
}
}
2 changes: 2 additions & 0 deletions Sources/SwiftGitX/Models/Diff/StatusEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public struct StatusEntry: LibGit2RawRepresentable {
}
}

extension StatusEntry.Status: Sendable {}

private extension StatusEntry.Status {
// We use this instead of direct dictionary because this makes sure the result is ordered.
static let statusMapping: [(key: StatusEntry.Status, value: git_status_t)] = [
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftGitX/Models/Options/CloneOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ public struct CloneOptions {
return options
}
}

extension CloneOptions: Sendable {}
2 changes: 2 additions & 0 deletions Sources/SwiftGitX/Models/Options/DiffOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ public struct DiffOption: OptionSet {
public static let workingTree = DiffOption(rawValue: 1 << 0)
public static let index = DiffOption(rawValue: 1 << 1)
}

extension DiffOption: Sendable {}
2 changes: 2 additions & 0 deletions Sources/SwiftGitX/Models/Types/ObjectType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public enum ObjectType: LibGit2RawRepresentable {
}
}

extension ObjectType: Sendable {}

private extension ObjectType {
static let objectTypeMapping: [ObjectType: git_object_t] = [
.any: GIT_OBJECT_ANY,
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftGitX/Objects/OID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ public extension OID {
withUnsafeBytes(of: raw.id) { hasher.combine(bytes: $0) }
}
}

extension OID: Sendable {}
Loading