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

Verify repo checkout for branch names #56

Merged
merged 1 commit into from
Feb 28, 2020
Merged
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
66 changes: 54 additions & 12 deletions sdk/repo/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/coreos/pkg/capnslog"

Expand Down Expand Up @@ -134,6 +135,32 @@ func isSHA1(s string) bool {
return err == nil && len(b) == sha1.Size
}

func (r *repo) projectBranches(p Project) ([]string, error) {
git := exec.Command("git", "show", "-s", "--pretty=%D", "HEAD")
git.Dir = filepath.Join(r.root, p.Path)
git.Stderr = os.Stderr
out, err := git.Output()
if err != nil {
return nil, err
}

var branches []string

for _, val := range strings.Split(string(out), ",") {
ref := strings.TrimSpace(val)

if strings.HasPrefix(ref, "HEAD") || strings.HasPrefix(ref, "tag:") {
// Skip "tag: tagname", "HEAD", and "HEAD -> branchname"
continue
}

parts := strings.Split(ref, "/")
branches = append(branches, parts[len(parts)-1])
}

return branches, nil
}

func (r *repo) projectHEAD(p Project) (string, error) {
git := exec.Command("git", "rev-list", "--max-count=1", "HEAD")
git.Dir = filepath.Join(r.root, p.Path)
Expand Down Expand Up @@ -175,23 +202,38 @@ func VerifySync(name string) error {

var result error
for _, project := range manifest.Projects {
if !isSHA1(project.Revision) {
if isSHA1(project.Revision) {
rev, err := manifest.projectHEAD(project)
if err != nil {
return err
}

if rev != project.Revision {
plog.Errorf("Project dir %s at %s, expected %s",
project.Path, rev, project.Revision)
result = VerifyError
}
} else if strings.HasPrefix(project.Revision, "refs/heads/") {
branches, err := manifest.projectBranches(project)
if err != nil {
return err
}

for _, branch := range branches {
branchRef := "refs/heads/" + branch

if branchRef != project.Revision {
plog.Errorf("Project dir %s at %s, expected %s",
project.Path, branchRef, project.Revision)
result = VerifyError
}
}
} else {
plog.Errorf("Cannot verify %s revision %s in %s",
project.Name, project.Revision, manifest.name)
return Unimplemented
}

rev, err := manifest.projectHEAD(project)
if err != nil {
return err
}

if rev != project.Revision {
plog.Errorf("Project dir %s at %s, expected %s",
project.Path, rev, project.Revision)
result = VerifyError
}

if err := manifest.projectIsClean(project); err != nil {
plog.Errorf("Project dir %s is not clean, git diff %v",
project.Path, err)
Expand Down