Skip to content

Commit 522bee5

Browse files
Merge pull request #82 from swift-developer-tools/feature/refs-advanced
Add Refs (Advanced) bindings
2 parents 749ef94 + f0620be commit 522bee5

5 files changed

Lines changed: 211 additions & 9 deletions

File tree

Sources/SwiftLibgit2/ODB/ODB-Functions.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,6 @@ public func gitODBWrite(
545545

546546

547547

548-
// TODO: Replace `GIT_STREAM_WRONLY` in documentation.
549548
/// Opens a stream to write an object into the given object database.
550549
/// - Parameters:
551550
/// - out: The pointer in which to store the stream.
@@ -557,9 +556,9 @@ public func gitODBWrite(
557556
///
558557
/// ## Discussion
559558
///
560-
/// The type of the resulting stream will be `GIT_STREAM_WRONLY`, and it
561-
/// will not be effective until ``gitODBStreamFinalizeWrite(out:stream:)``
562-
/// is successfully called.
559+
/// The type of the resulting stream will be ``GitODBStreamT/gitStreamWROnly``,
560+
/// and it will not be effective until
561+
/// ``gitODBStreamFinalizeWrite(out:stream:)`` is successfully called.
563562
///
564563
/// ## C Equivalent
565564
///
@@ -719,7 +718,6 @@ public func gitODBStreamFree(
719718

720719

721720

722-
// TODO: Replace `GIT_STREAM_WRONLY` in documentation.
723721
/// Opens a stream to read the specified object from the given object database.
724722
/// - Parameters:
725723
/// - out: The pointer in which to store the stream.
@@ -732,8 +730,8 @@ public func gitODBStreamFree(
732730
///
733731
/// ## Discussion
734732
///
735-
/// The type of the resulting stream will be `GIT_STREAM_RONLY`, and will
736-
/// have `read()` and `free()` methods.
733+
/// The type of the resulting stream will be ``GitODBStreamT/gitStreamWROnly``,
734+
/// and will have `read()` and `free()` methods.
737735
///
738736
/// - Note: Most backends do not support streaming reads, since the objects are
739737
/// stored as compressed/delta blobs. Use ``gitODBRead(obj:db:id:)`` instead.

Sources/SwiftLibgit2/RefDB/RefDB-Functions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import CLibgit2
1111

1212

1313

14-
// TODO: Replace `git_refdb_set_backend()` in documentation.
1514
/// Creates a new reference database with no backends.
1615
/// - Parameters:
1716
/// - out: The pointer in which to store the reference database. The
@@ -23,7 +22,8 @@ import CLibgit2
2322
/// ## Discussion
2423
///
2524
/// Before the ODB can be used for reading or writing, a custom database
26-
/// backend must be manually added by calling `git_refdb_set_backend()`.
25+
/// backend must be manually added by calling
26+
/// ``gitRefDBSetBackend(refDB:backend:)``.
2727
///
2828
/// ## C Equivalent
2929
///
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the swift-libgit2 open source project.
4+
//
5+
// Copyright (c) Margins Technologies LLC.
6+
// Licensed under the Apache License, Version 2.0.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
import CLibgit2
11+
12+
13+
14+
/// Creates a new direct reference using the given name and ID.
15+
/// - Parameters:
16+
/// - name: The reference name to use.
17+
/// - oid: The reference ID to use.
18+
/// - peel: The ID of the first non-tag object to use.
19+
/// - Returns: The new direct reference using the given name and ID.
20+
///
21+
/// ## C Equivalent
22+
///
23+
/// [`git_reference__alloc()`](https://libgit2.org/docs/reference/main/sys/refs/git_reference__alloc.html)
24+
public func gitReferenceAlloc(
25+
name : String,
26+
oid : GitOID,
27+
peel : GitOID?
28+
) -> OpaquePointer?
29+
{
30+
return try? oid.withCValue
31+
{
32+
cOID in
33+
34+
return try peel.withOptionalCValue
35+
{
36+
cPeel in
37+
38+
return git_reference__alloc(
39+
name,
40+
cOID,
41+
cPeel
42+
)
43+
}
44+
}
45+
}
46+
47+
48+
49+
/// Creates a new symbolic reference using the given name and target.
50+
/// - Parameters:
51+
/// - name: The reference name to use.
52+
/// - target: The reference target to use.
53+
/// - Returns: The new direct reference using the given name and target.
54+
///
55+
/// ## C Equivalent
56+
///
57+
/// [`git_reference__alloc_symbolic()`](https://libgit2.org/docs/reference/main/sys/refs/git_reference__alloc_symbolic.html)
58+
public func gitReferenceAllocSymbolic(
59+
name : String,
60+
target : String
61+
) -> OpaquePointer?
62+
{
63+
return git_reference__alloc_symbolic(
64+
name,
65+
target
66+
)
67+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Refs (Advanced)
2+
3+
Low-level reference creation.
4+
5+
## Topics
6+
7+
### Functions
8+
9+
- ``gitReferenceAlloc(name:oid:peel:)``
10+
- ``gitReferenceAllocSymbolic(name:target:)``
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the swift-libgit2 open source project.
4+
//
5+
// Copyright (c) Margins Technologies LLC.
6+
// Licensed under the Apache License, Version 2.0.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
import CLibgit2
11+
import XCTest
12+
@testable import SwiftLibgit2
13+
14+
15+
16+
final class RefsAdvancedTests: XCTestCaseStopOnFail
17+
{
18+
func testGitReferenceAlloc() throws
19+
{
20+
try Repository.withRepository
21+
{
22+
repository in
23+
24+
var directRefPointer: OpaquePointer? = nil
25+
26+
defer
27+
{
28+
gitReferenceFree(ref: directRefPointer)
29+
}
30+
31+
32+
33+
let refName : String = "HEAD"
34+
let refOID : GitOID = repository.headOID
35+
36+
directRefPointer = gitReferenceAlloc(
37+
name: refName,
38+
oid: refOID,
39+
peel: refOID
40+
)
41+
42+
guard let directRefPointer: OpaquePointer = directRefPointer
43+
else
44+
{
45+
XCTFail("The direct reference pointer was nil.")
46+
return
47+
}
48+
49+
50+
51+
let directRefName: String?
52+
= gitReferenceName(ref: directRefPointer)
53+
54+
XCTAssertNotNil(directRefName)
55+
XCTAssertEqual(directRefName, refName)
56+
57+
58+
59+
let directRefType: GitReferenceT?
60+
= gitReferenceType(ref: directRefPointer)
61+
62+
XCTAssertNotNil(directRefType)
63+
XCTAssertEqual(directRefType, .gitReferenceDirect)
64+
65+
66+
67+
let directTargetOID: GitOID?
68+
= gitReferenceTarget(ref: directRefPointer)
69+
70+
XCTAssertNotNil(directTargetOID)
71+
XCTAssertEqual(directTargetOID, refOID)
72+
}
73+
}
74+
75+
76+
77+
func testGitReferenceAllocSymbolic() throws
78+
{
79+
var symbolicRefPointer: OpaquePointer? = nil
80+
81+
defer
82+
{
83+
gitReferenceFree(ref: symbolicRefPointer)
84+
}
85+
86+
87+
88+
let refName : String = "refs/heads/symbolic-test"
89+
let refTargetName : String = "HEAD"
90+
91+
symbolicRefPointer = gitReferenceAllocSymbolic(
92+
name: refName,
93+
target: refTargetName
94+
)
95+
96+
guard let symbolicRefPointer: OpaquePointer = symbolicRefPointer
97+
else
98+
{
99+
XCTFail("The symbolic reference pointer was nil.")
100+
return
101+
}
102+
103+
104+
105+
let symbolicRefName: String?
106+
= gitReferenceName(ref: symbolicRefPointer)
107+
108+
XCTAssertNotNil(symbolicRefName)
109+
XCTAssertEqual(symbolicRefName, refName)
110+
111+
112+
113+
let symbolicRefType: GitReferenceT?
114+
= gitReferenceType(ref: symbolicRefPointer)
115+
116+
XCTAssertNotNil(symbolicRefType)
117+
XCTAssertEqual(symbolicRefType, .gitReferenceSymbolic)
118+
119+
120+
121+
let symbolicTarget: String?
122+
= gitReferenceSymbolicTarget(ref: symbolicRefPointer)
123+
124+
XCTAssertNotNil(symbolicTarget)
125+
XCTAssertEqual(symbolicTarget, refTargetName)
126+
}
127+
}

0 commit comments

Comments
 (0)