@@ -27,6 +27,11 @@ fn C.git_repository_open(repo voidptr, path &char) int
27
27
fn C.git_libgit2_features ()
28
28
fn C.git_commit_lookup (voidptr , voidptr , & C.git_oid) int
29
29
30
+ fn C.git_repository_free (repo & C.git_repository)
31
+ fn C.git_repository_head (out && C.git_reference, repo & C.git_repository) int
32
+
33
+ fn C.git_reference_free (ref & C.git_reference)
34
+
30
35
struct C.git_repository {}
31
36
32
37
struct C.git_commit {}
@@ -181,6 +186,41 @@ pub fn (r &Repo) current_branch() string {
181
186
return branch.after ('refs/heads/' )
182
187
}
183
188
189
+ pub fn (repo &Repo) primary_branch () string {
190
+ err := C.git_repository_open (& repo.obj, repo.path.str)
191
+ if err != 0 {
192
+ return ''
193
+ }
194
+ defer {
195
+ C.git_repository_free (repo.obj)
196
+ }
197
+
198
+ // Get HEAD reference
199
+ head_ref := & C.git_reference (unsafe { nil })
200
+ err_head := C.git_repository_head (& head_ref, repo.obj)
201
+ if err_head != 0 {
202
+ return ''
203
+ }
204
+ defer {
205
+ C.git_reference_free (head_ref)
206
+ }
207
+
208
+ // Get symbolic target
209
+ symbolic_ref := C.git_reference_symbolic_target (head_ref)
210
+ if symbolic_ref == unsafe { nil } {
211
+ return ''
212
+ }
213
+
214
+ // Convert to V string and extract branch name
215
+ branch := unsafe { cstring_to_vstring (symbolic_ref) }
216
+ return get_branch_name_from_reference (branch)
217
+ }
218
+
219
+ // Assuming this helper function exists elsewhere in your code
220
+ fn get_branch_name_from_reference (ref string ) string {
221
+ return ref.after ('refs/heads/' )
222
+ }
223
+
184
224
pub fn (r &Repo) show_file_blob (branch string , file_path string ) ! string {
185
225
mut blob := & C.git_blob (unsafe { nil })
186
226
mut branch_ref := & C.git_reference (unsafe { nil })
0 commit comments