Skip to content

Improve Signature Model with Public Initializer and Better API#8

Merged
ibrahimcetin merged 1 commit into
mainfrom
improve-signature
Nov 27, 2025
Merged

Improve Signature Model with Public Initializer and Better API#8
ibrahimcetin merged 1 commit into
mainfrom
improve-signature

Conversation

@ibrahimcetin

Copy link
Copy Markdown
Owner

This PR enhances the Signature model by adding a public initializer and improving the overall API design, making it easier for consumers to create custom signatures for commits, tags, and other Git operations.

Usage

// Create a custom signature
let signature = Signature(
    name: "John Doe",
    email: "john@example.com",
    date: Date(),
    timezone: TimeZone(identifier: "UTC")!
)

// Or use now and current for date and timezone
let simpleSignature = Signature(name: "Jane Doe", email: "jane@example.com")

// Get default signature from repository config
let defaultSig = try Signature.default(in: repository)

…signature method

- Introduced a new initializer for the Signature struct to allow setting name, email, date, and timezone.
- Added a raw property to convert Signature to a git_signature.
- Updated the default signature method to accept a Repository object instead of an OpaquePointer.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR enhances the Signature model by adding a public initializer to enable custom signature creation and improving the default(in:) API to accept a Repository parameter instead of OpaquePointer, making the API more user-friendly.

  • Added public initializer with default parameters for date and timezone
  • Introduced internal raw computed property for converting Signature to git_signature
  • Enhanced default(in:) method with public overload accepting Repository type
  • Added .signature operation type to SwiftGitXError.Operation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +69 to +71
public static func `default`(in repository: Repository) throws(SwiftGitXError) -> Signature {
try `default`(in: repository.pointer)
}

Copilot AI Nov 27, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new public API for default(in:) that accepts a Repository parameter lacks test coverage. Consider adding a test that verifies the method correctly retrieves the default signature from repository configuration.

Copilot uses AI. Check for mistakes.
)
return (pointer, status)
}

Copilot AI Nov 27, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Critical memory leak: The signaturePointer allocated by git_signature_new is never freed with git_signature_free. This will leak memory every time the raw property is accessed.

The pointer should be freed after accessing pointee:

var raw: git_signature {
    get throws(SwiftGitXError) {
        let signaturePointer = try git(operation: .signature) {
            var pointer: UnsafeMutablePointer<git_signature>?
            let status = git_signature_new(
                &pointer,
                name,
                email,
                git_time_t(date.timeIntervalSince1970),
                Int32(timezone.secondsFromGMT() / 60)
            )
            return (pointer, status)
        }
        defer { git_signature_free(signaturePointer) }
        
        return signaturePointer.pointee
    }
}

This pattern is consistently used elsewhere in the codebase (see ObjectFactory.makeSignaturePointer usage in StashCollection.swift:114 and TagCollection.swift:89).

Suggested change
defer { git_signature_free(signaturePointer) }

Copilot uses AI. Check for mistakes.
}
}

extension SwiftGitXError.Operation {

Copilot AI Nov 27, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new operation type .signature should have documentation explaining when it's used. Following the pattern in the codebase, operation extensions typically define all related operations together. Consider adding a documentation comment:

extension SwiftGitXError.Operation {
    /// Operation for signature-related errors (creation, validation, etc.).
    public static let signature = Self(rawValue: "signature")
}
Suggested change
extension SwiftGitXError.Operation {
extension SwiftGitXError.Operation {
/// Operation for signature-related errors (creation, validation, etc.).

Copilot uses AI. Check for mistakes.
/// - date: The date of the action. Defaults to the current date.
/// - timezone: The timezone of the author. Defaults to the system timezone.
///
/// If `date` and `timezone` are not provided, the current date and system timezone are used.

Copilot AI Nov 27, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence is redundant with the parameter documentation above. The parameter descriptions with "Defaults to..." already convey this information. Consider removing this line to avoid repetition.

Suggested change
/// If `date` and `timezone` are not provided, the current date and system timezone are used.

Copilot uses AI. Check for mistakes.
Comment on lines +28 to +33
public init(name: String, email: String, date: Date = Date(), timezone: TimeZone = .current) {
self.name = name
self.email = email
self.date = date
self.timezone = timezone
}

Copilot AI Nov 27, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new public initializer lacks test coverage. Consider adding tests to verify:

  1. Signature creation with all parameters
  2. Signature creation with default date and timezone
  3. Signature equality and hashing
  4. That the signature can be used in operations like commits and tags

Other collections in the codebase (BranchCollection, TagCollection, etc.) have comprehensive test coverage in the Tests/SwiftGitXTests/CollectionTests/ directory.

Copilot uses AI. Check for mistakes.
@ibrahimcetin ibrahimcetin merged commit 4f5a8c0 into main Nov 27, 2025
7 checks passed
@ibrahimcetin ibrahimcetin deleted the improve-signature branch November 29, 2025 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants