2020-04-18 23:09:03 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2020-04-29 22:02:15 -04:00
|
|
|
"github.com/go-git/go-git/v5"
|
2020-04-18 23:09:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// TeaRepo is a go-git Repository, with an extended high level interface.
|
|
|
|
type TeaRepo struct {
|
|
|
|
*git.Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
// RepoForWorkdir tries to open the git repository in the local directory
|
|
|
|
// for reading or modification.
|
|
|
|
func RepoForWorkdir() (*TeaRepo, error) {
|
2020-12-11 04:07:29 -05:00
|
|
|
return RepoFromPath("")
|
2020-04-18 23:09:03 -04:00
|
|
|
}
|
2020-07-17 12:36:48 -04:00
|
|
|
|
|
|
|
// RepoFromPath tries to open the git repository by path
|
|
|
|
func RepoFromPath(path string) (*TeaRepo, error) {
|
2020-12-11 04:07:29 -05:00
|
|
|
if len(path) == 0 {
|
|
|
|
path = "./"
|
|
|
|
}
|
2020-07-17 12:36:48 -04:00
|
|
|
repo, err := git.PlainOpenWithOptions(path, &git.PlainOpenOptions{
|
|
|
|
DetectDotGit: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &TeaRepo{repo}, nil
|
|
|
|
}
|