<commit_before>// Copyright 2012 <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package git

// #cgo pkg-config: libgit2
// #include <git2.h>
import "C"

import (
	"unsafe"
)

// Commit represents a git commit.
type Commit struct {
	commit *C.struct_git_commit
}

// Free is used to deallocate a commit object.
func (c *Commit) Free() {
	C.git_commit_free(c.commit)
}

// Id returns the hash of the commit.
func (c *Commit) Id() string {
	oid := C.git_commit_id(c.commit)
	defer C.free(unsafe.Pointer(oid))
	id := C.git_oid_allocfmt(oid)
	defer C.free(unsafe.Pointer(id))
	return C.GoString(id)
}

// Repository is the basic type of the git package, it represents a git
// repository.
type Repository struct {
	repository *C.struct_git_repository
}

// InitRepository inits a new repository.
//
// If the path does not exist, it will be created.
//
// Returns an instance of Repository, or an error in case of failure.
func InitRepository(path string, bare bool) (*Repository, error) {
	var cbare C.unsigned = 0
	if bare {
		cbare = 1
	}
	repo := new(Repository)
	cpath := C.CString(path)
	defer C.free(unsafe.Pointer(cpath))
	if C.git_repository_init(&repo.repository, cpath, cbare) != C.GIT_OK {
		return nil, lastErr()
	}
	return repo, nil
}

// OpenRepository opens a repository by its path.
//
// Returns an error in case of failure (e.g.: if the path does not exist; if
// it exists but is not a git repository or if the path exists, is a git
// repository, but the user does not have access to it).
func OpenRepository(path string) (*Repository, error) {
	repo := new(Repository)
	cpath := C.CString(path)
	defer C.free(unsafe.Pointer(cpath))
	if C.git_repository_open(&repo.repository, cpath) != C.GIT_OK {
		return nil, lastErr()
	}
	return repo, nil
}

// Config returns a Config instance, representing the configuration of the
// repository.
func (r *Repository) Config() (*Config, error) {
	conf := new(Config)
	if C.git_repository_config(&conf.config, r.repository) != C.GIT_OK {
		return nil, lastErr()
	}
	return conf, nil
}

// Free is used to deallocate the repository. It should be called to finish the
// repository. It's a good practice to use it with the defer statement:
//
//     repo, err := git.GetRepository("/path/to/repository")
//     // check error
//     defer repo.Free()
//     // use repo
func (r *Repository) Free() {
	C.git_repository_free(r.repository)
}

// Head returns the commit at the head of the repository.
func (r *Repository) Head() (*Commit, error) {
	var reference *C.struct_git_reference
	if C.git_repository_head(&reference, r.repository) != C.GIT_OK {
		return nil, lastErr()
	}
	defer C.git_reference_free(reference)
	c := new(Commit)
	if C.git_commit_lookup(&c.commit, r.repository, C.git_reference_oid(reference)) != C.GIT_OK {
		return nil, lastErr()
	}
	return c, nil
}

// Config represents the configuration of a git repository.
//
// You can use it retrieve or define configurations of the repository.
type Config struct {
	config *C.struct_git_config
}

// Free is used to deallocate the Config instance. It should be called to
// finish the instance. You can use it with the defer statement:
//
//     // get repository instance
//     config, err := repo.Config()
//     // check error
//     defer config.Free()
func (c *Config) Free() {
	C.git_config_free(c.config)
}
<commit_msg>repository: improve doc for OpenRepository function<commit_after>// Copyright 2012 <NAME>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package git

// #cgo pkg-config: libgit2
// #include <git2.h>
import "C"

import (
	"unsafe"
)

// Commit represents a git commit.
type Commit struct {
	commit *C.struct_git_commit
}

// Free is used to deallocate a commit object.
func (c *Commit) Free() {
	C.git_commit_free(c.commit)
}

// Id returns the hash of the commit.
func (c *Commit) Id() string {
	oid := C.git_commit_id(c.commit)
	defer C.free(unsafe.Pointer(oid))
	id := C.git_oid_allocfmt(oid)
	defer C.free(unsafe.Pointer(id))
	return C.GoString(id)
}

// Repository is the basic type of the git package, it represents a git
// repository.
type Repository struct {
	repository *C.struct_git_repository
}

// InitRepository inits a new repository.
//
// If the path does not exist, it will be created.
//
// Returns an instance of Repository, or an error in case of failure.
func InitRepository(path string, bare bool) (*Repository, error) {
	var cbare C.unsigned = 0
	if bare {
		cbare = 1
	}
	repo := new(Repository)
	cpath := C.CString(path)
	defer C.free(unsafe.Pointer(cpath))
	if C.git_repository_init(&repo.repository, cpath, cbare) != C.GIT_OK {
		return nil, lastErr()
	}
	return repo, nil
}

// OpenRepository opens a repository by its path.
//
// Returns an error in case of failure (e.g.: if the path does not exist; if
// it exists but is not a git repository or if the path exists, is a git
// repository, but the user does not have access to it).
func OpenRepository(path string) (*Repository, error) {
	repo := new(Repository)
	cpath := C.CString(path)
	defer C.free(unsafe.Pointer(cpath))
	if C.git_repository_open(&repo.repository, cpath) != C.GIT_OK {
		return nil, lastErr()
	}
	return repo, nil
}

// Config returns a Config instance, representing the configuration of the
// repository.
func (r *Repository) Config() (*Config, error) {
	conf := new(Config)
	if C.git_repository_config(&conf.config, r.repository) != C.GIT_OK {
		return nil, lastErr()
	}
	return conf, nil
}

// Free is used to deallocate the repository. It should be called to finish the
// repository. It's a good practice to use it with the defer statement:
//
//     repo, err := git.GetRepository("/path/to/repository")
//     // check error
//     defer repo.Free()
//     // use repo
func (r *Repository) Free() {
	C.git_repository_free(r.repository)
}

// Head returns the commit at the head of the repository.
func (r *Repository) Head() (*Commit, error) {
	var reference *C.struct_git_reference
	if C.git_repository_head(&reference, r.repository) != C.GIT_OK {
		return nil, lastErr()
	}
	defer C.git_reference_free(reference)
	c := new(Commit)
	if C.git_commit_lookup(&c.commit, r.repository, C.git_reference_oid(reference)) != C.GIT_OK {
		return nil, lastErr()
	}
	return c, nil
}

// Config represents the configuration of a git repository.
//
// You can use it to retrieve or to define settings on the repository.
type Config struct {
	config *C.struct_git_config
}

// Free is used to deallocate the Config instance. It should be called to
// finish the instance. You can use it with the defer statement:
//
//     // get repository instance
//     config, err := repo.Config()
//     // check error
//     defer config.Free()
func (c *Config) Free() {
	C.git_config_free(c.config)
}

{'fraction_non_alphanumeric': 0.11927536231884057, 'fraction_numerical': 0.002318840579710145, 'mean_word_length': 3.522280471821756, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3888216', 'n_tokens_mistral': 2260, 'n_tokens_neox': 2215, 'n_words': 811}