Repository: libgit2/git2go
Pull request #758 (2021-04-03 23:50:07)

add wrapper for git_config_open_default


Patch:
diff --git a/Build_bundled_static.go b/Build_bundled_static.go
index 4d10d45..65e31ac 100644
--- a/Build_bundled_static.go
+++ b/Build_bundled_static.go
@@ -4,7 +4,7 @@ package git
 
 /*
 #cgo windows CFLAGS: -I${SRCDIR}/static-build/install/include/
-#cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp
+#cgo windows LDFLAGS: -L${SRCDIR}/static-build/install/lib/ -lgit2 -lwinhttp -lws2_32 -lole32 -lrpcrt4 -lcrypt32
 #cgo !windows pkg-config: --static ${SRCDIR}/static-build/install/lib/pkgconfig/libgit2.pc
 #cgo CFLAGS: -DLIBGIT2_STATIC
 #include <git2.h>
diff --git a/config.go b/config.go
index 7260089..062df05 100644
--- a/config.go
+++ b/config.go
@@ -450,3 +450,17 @@ func ConfigFindProgramdata() (string, error) {
 
 	return C.GoString(buf.ptr), nil
 }
+
+// OpenDefault opens the default config according to git rules
+func OpenDefault() (*Config, error) {
+	runtime.LockOSThread()
+	defer runtime.UnlockOSThread()
+
+	config := new(Config)
+
+	if ret := C.git_config_open_default(&config.ptr); ret < 0 {
+		return nil, MakeGitError(ret)
+	}
+
+	return config, nil
+}
diff --git a/repository.go b/repository.go
index a6eb7f1..cb82fc3 100644
--- a/repository.go
+++ b/repository.go
@@ -739,5 +739,39 @@ func (r *Repository) RemoveMessage() error {
 	if cErr < 0 {
 		return MakeGitError(cErr)
 	}
-	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_dispose(&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
 }


Test patch:
diff --git a/config_test.go b/config_test.go
index 398236e..f942dcb 100644
--- a/config_test.go
+++ b/config_test.go
@@ -107,3 +107,13 @@ func TestConfigLookups(t *testing.T) {
 		test(c, t)
 	}
 }
+
+func TestOpenDefault(t *testing.T) {
+
+	c, err := OpenDefault()
+	if err != nil {
+		t.Errorf("OpenDefault error: '%v'. Expected none\n", err)
+		return
+	}
+	defer c.Free()
+}
diff --git a/repository_test.go b/repository_test.go
index 5a0f920..e403aa9 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -92,3 +92,14 @@ func TestRepositorySetConfig(t *testing.T) {
 		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")
+	}
+}