Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom ref setting to pull git repository #302

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
"url" : "https://www.github.com/YourOrganization/RepoOne.git",
"enable-push-updates" : true
},
"RepoWithCustomRefs" : {
"url" : "https://www.github.com/YourOrganization/RepoOne.git",
"ref" : "develop"
},
"RepoIsGitHubWiki" : {
"url" : "https://github.com/YourOrganization/RepoWithWiki.wiki.git",
"url-pattern" : {
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Repo struct {
Vcs string `json:"vcs"`
VcsConfigMessage *SecretMessage `json:"vcs-config"`
UrlPattern *UrlPattern `json:"url-pattern"`
Ref string `json:"ref"`
ExcludeDotFiles bool `json:"exclude-dot-files"`
EnablePollUpdates *bool `json:"enable-poll-updates"`
EnablePushUpdates *bool `json:"enable-push-updates"`
Expand Down
4 changes: 2 additions & 2 deletions searcher/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func updateAndReindex(
defer lim.Release()

repo := s.Repo
newRev, err := wd.PullOrClone(vcsDir, repo.Url)
newRev, err := wd.PullOrClone(vcsDir, repo.Url, repo.Ref)

if err != nil {
log.Printf("vcs pull error (%s - %s): %s", name, repo.Url, err)
Expand Down Expand Up @@ -405,7 +405,7 @@ func newSearcher(
SpecialFiles: wd.SpecialFiles(),
}

rev, err := wd.PullOrClone(vcsDir, repo.Url)
rev, err := wd.PullOrClone(vcsDir, repo.Url, repo.Ref)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion vcs/bzr.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (g *BzrDriver) HeadRev(dir string) (string, error) {
return strings.TrimSpace(buf.String()), cmd.Wait()
}

func (g *BzrDriver) Pull(dir string) (string, error) {
func (g *BzrDriver) Pull(dir string, _ string) (string, error) {
cmd := exec.Command("bzr", "pull")
cmd.Dir = dir
out, err := cmd.CombinedOutput()
Expand Down
8 changes: 6 additions & 2 deletions vcs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,19 @@ func run(desc, dir, cmd string, args ...string) error {
return nil
}

func (g *GitDriver) Pull(dir string) (string, error) {
func (g *GitDriver) Pull(dir string, configRef string) (string, error) {
ref := configRef
if ref == "" {
ref = defaultRef
}
if err := run("git fetch", dir,
"git",
"fetch",
"--prune",
"--no-tags",
"--depth", "1",
"origin",
fmt.Sprintf("+%s:remotes/origin/%s", defaultRef, defaultRef)); err != nil {
fmt.Sprintf("+%s:remotes/origin/%s", ref, ref)); err != nil {
return "", err
}

Expand Down
2 changes: 1 addition & 1 deletion vcs/hg.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (g *MercurialDriver) HeadRev(dir string) (string, error) {
return strings.TrimSpace(buf.String()), cmd.Wait()
}

func (g *MercurialDriver) Pull(dir string) (string, error) {
func (g *MercurialDriver) Pull(dir string, _ string) (string, error) {
cmd := exec.Command("hg", "pull", "-u")
cmd.Dir = dir
err := cmd.Run()
Expand Down
2 changes: 1 addition & 1 deletion vcs/svn.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (g *SVNDriver) HeadRev(dir string) (string, error) {
return strings.TrimSpace(buf.String()), cmd.Wait()
}

func (g *SVNDriver) Pull(dir string) (string, error) {
func (g *SVNDriver) Pull(dir string, _ string) (string, error) {
cmd := exec.Command(
"svn",
"update",
Expand Down
6 changes: 3 additions & 3 deletions vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Driver interface {
Clone(dir, url string) (string, error)

// Pull new changes from the server and update the working directory.
Pull(dir string) (string, error)
Pull(dir string, ref string) (string, error)

// Return the revision at the head of the vcs directory.
HeadRev(dir string) (string, error)
Expand Down Expand Up @@ -69,9 +69,9 @@ func exists(path string) bool {

// A utility method that carries out the common operation of cloning
// if the working directory is absent and pulling otherwise.
func (w *WorkDir) PullOrClone(dir, url string) (string, error) {
func (w *WorkDir) PullOrClone(dir, url, ref string) (string, error) {
if exists(dir) {
return w.Pull(dir)
return w.Pull(dir, ref)
}
return w.Clone(dir, url)
}