Skip to content

Commit

Permalink
implement branch pull
Browse files Browse the repository at this point in the history
  • Loading branch information
sters committed Mar 1, 2021
1 parent 4e7b5ea commit 63e02c7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
10 changes: 6 additions & 4 deletions onstatic/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,18 @@ func handlePull(res http.ResponseWriter, req *http.Request) {
return
}

reponame := getHashedDirectoryName(
strings.TrimSpace(req.Header.Get(repoKey)),
)
reponame := strings.TrimSpace(req.Header.Get(repoKey))
reponame = getHashedDirectoryName(reponame)
branchName := strings.TrimSpace(req.Header.Get(branchKey))

repo, err := loadLocalRepository(reponame)
if err != nil {
zap.L().Error("failed to load repo", zap.Error(err))
res.WriteHeader(http.StatusServiceUnavailable)
return
}

if err := doGitPull(repo); err != nil {
if err := doGitPull(repo, branchName); err != nil {
zap.L().Error("failed to gitpull", zap.Error(err))
res.WriteHeader(http.StatusServiceUnavailable)
return
Expand Down Expand Up @@ -185,6 +186,7 @@ func hasIgnoreSuffix(p string) bool {
const (
validateKey = "X-ONSTATIC-KEY"
repoKey = "X-ONSTATIC-REPONAME"
branchKey = "X-ONSTATIC-BRANCH-NAME"
)

func validate(res http.ResponseWriter, req *http.Request) bool {
Expand Down
15 changes: 12 additions & 3 deletions onstatic/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"gopkg.in/src-d/go-billy.v4/util"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/config"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/cache"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
"gopkg.in/src-d/go-git.v4/storage/filesystem"
Expand Down Expand Up @@ -190,13 +191,14 @@ func configureOriginRepository(repo *git.Repository, originURL string) error {
Name: originName,
URLs: []string{originURL},
})

if err != nil {
return failure.Wrap(err)
}
return nil
}

func doGitPull(repo *git.Repository) error {
func doGitPull(repo *git.Repository, branchName string) error {
w, err := repo.Worktree()
if err != nil {
return failure.Wrap(err)
Expand All @@ -211,10 +213,17 @@ func doGitPull(repo *git.Repository) error {
return failure.Wrap(err)
}

err = w.Pull(&git.PullOptions{
opt := &git.PullOptions{
RemoteName: originName,
Auth: auth,
})
Force: true,
}

if branchName != "" {
opt.ReferenceName = plumbing.NewBranchReferenceName(branchName)
}

err = w.Pull(opt)
if err != nil {
return failure.Wrap(err)
}
Expand Down

0 comments on commit 63e02c7

Please sign in to comment.