@@ -27,6 +27,11 @@ fn C.git_repository_open(repo voidptr, path &char) int
2727fn C.git_libgit2_features ()
2828fn C.git_commit_lookup (voidptr , voidptr , & C.git_oid) int
2929
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+
3035struct C.git_repository {}
3136
3237struct C.git_commit {}
@@ -181,6 +186,41 @@ pub fn (r &Repo) current_branch() string {
181186 return branch.after ('refs/heads/' )
182187}
183188
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+
184224pub fn (r &Repo) show_file_blob (branch string , file_path string ) ! string {
185225 mut blob := & C.git_blob (unsafe { nil })
186226 mut branch_ref := & C.git_reference (unsafe { nil })
0 commit comments