Git repository item path (#757) (#768)

add wrapper for `git_repository_item_path`

(cherry picked from commit a4d202ed7b)

Co-authored-by: Vladimir Buzuev <44682889+vladimir-buzuev@users.noreply.github.com>
This commit is contained in:
lhchavez 2021-04-04 08:28:09 -07:00 committed by GitHub
parent 275690e61a
commit bcfd445806
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -735,3 +735,37 @@ func (r *Repository) RemoveMessage() error {
} }
return nil return nil
} }
type RepositoryItem int
const (
RepositoryItemGitDir RepositoryItem = C.GIT_REPOSITORY_ITEM_GITDIR
RepositoryItemWorkDir RepositoryItem = C.GIT_REPOSITORY_ITEM_WORKDIR
RepositoryItemCommonDir RepositoryItem = C.GIT_REPOSITORY_ITEM_COMMONDIR
RepositoryItemIndex RepositoryItem = C.GIT_REPOSITORY_ITEM_INDEX
RepositoryItemObjects RepositoryItem = C.GIT_REPOSITORY_ITEM_OBJECTS
RepositoryItemRefs RepositoryItem = C.GIT_REPOSITORY_ITEM_REFS
RepositoryItemPackedRefs RepositoryItem = C.GIT_REPOSITORY_ITEM_PACKED_REFS
RepositoryItemRemotes RepositoryItem = C.GIT_REPOSITORY_ITEM_REMOTES
RepositoryItemConfig RepositoryItem = C.GIT_REPOSITORY_ITEM_CONFIG
RepositoryItemInfo RepositoryItem = C.GIT_REPOSITORY_ITEM_INFO
RepositoryItemHooks RepositoryItem = C.GIT_REPOSITORY_ITEM_HOOKS
RepositoryItemLogs RepositoryItem = C.GIT_REPOSITORY_ITEM_LOGS
RepositoryItemModules RepositoryItem = C.GIT_REPOSITORY_ITEM_MODULES
RepositoryItemWorkTrees RepositoryItem = C.GIT_REPOSITORY_ITEM_WORKTREES
)
func (r *Repository) ItemPath(item RepositoryItem) (string, error) {
var c_buf C.git_buf
defer C.git_buf_free(&c_buf)
runtime.LockOSThread()
defer runtime.UnlockOSThread()
ret := C.git_repository_item_path(&c_buf, r.ptr, C.git_repository_item_t(item))
runtime.KeepAlive(r)
if ret < 0 {
return "", MakeGitError(ret)
}
return C.GoString(c_buf.ptr), nil
}

View File

@ -92,3 +92,14 @@ func TestRepositorySetConfig(t *testing.T) {
t.Fatal("result must be true") t.Fatal("result must be true")
} }
} }
func TestRepositoryItemPath(t *testing.T) {
repo := createTestRepo(t)
defer cleanupTestRepo(t, repo)
gitDir, err := repo.ItemPath(RepositoryItemGitDir)
checkFatal(t, err)
if gitDir == "" {
t.Error("expected not empty gitDir")
}
}