Skip to content

Commit f06f009

Browse files
committed
git: primary_branch() via libgit
1 parent cbd3199 commit f06f009

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

git/libgit.v

+40
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ fn C.git_repository_open(repo voidptr, path &char) int
2727
fn C.git_libgit2_features()
2828
fn 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+
3035
struct C.git_repository {}
3136

3237
struct 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+
184224
pub 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 })

git/util.v

+2
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,11 @@ pub fn remove_git_extension_if_exists(git_repository_name string) string {
8585
return git_repository_name.trim_string_right('.git')
8686
}
8787

88+
/*
8889
fn get_branch_name_from_reference(value string) string {
8990
branch_query := r'refs/heads/(.*)'
9091
mut re := regex.regex_opt(branch_query) or { panic(err) }
9192
re.match_string(value)
9293
return re.get_group_by_id(value, 0)
9394
}
95+
*/

src/gitly.v

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ fn new_app() !&App {
7777
app.version = version
7878

7979
app.handle_static('src/static', true)!
80+
if !os.exists('avatars') {
81+
os.mkdir('avatars')!
82+
}
8083
app.handle_static('avatars', false)!
8184

8285
app.load_settings()

0 commit comments

Comments
 (0)