func()

in pkg/release/repository.go [90:197]


func (r *Repo) CheckState(expOrg, expRepo, expRev string, nomock bool) error {
	logrus.Info("Verifying repository state")

	dirty, err := r.repo.IsDirty()
	if err != nil {
		return errors.Wrap(err, "checking if repository is dirty")
	}
	if dirty {
		return errors.New(
			"repository is dirty, please commit and push your changes",
		)
	}
	logrus.Info("Repository is in clean state")

	branch, err := r.repo.CurrentBranch()
	if err != nil {
		return errors.Wrap(err, "retrieving current branch")
	}

	head, err := r.repo.Head()
	if err != nil {
		return errors.Wrap(err, "retrieving repository HEAD")
	}

	logrus.Infof("Repo head is: %s", head)

	rev, err := r.repo.RevParse(expRev)
	if err != nil {
		return errors.Wrapf(err, "retrieving rev-parse for %s", expRev)
	}

	if rev != head {
		return errors.Errorf("revision %q expected but got %q", head, rev)
	}

	if nomock && !(expOrg == DefaultToolOrg && expRepo == DefaultToolRepo && expRev == DefaultToolRef) {
		return errors.New("disallow using anything other than kubernetes/release:master with nomock flag")
	}

	logrus.Infof("Found matching revision %q", expRev)

	// Verify the remote
	remotes, err := r.repo.Remotes()
	if err != nil {
		return errors.Wrap(err, "retrieving repository remotes")
	}
	var foundRemote *git.Remote
	for _, remote := range remotes {
		for _, url := range remote.URLs() {
			if strings.Contains(url, filepath.Join(expOrg, expRepo)) {
				foundRemote = remote
				break
			}
		}
		if foundRemote != nil {
			break
		}
	}
	if foundRemote == nil {
		return errors.Errorf(
			"unable to find remote matching organization %q and repository %q",
			expOrg, expRepo,
		)
	}
	logrus.Infof(
		"Found matching organization %q and repository %q in remote: %s (%s)",
		expOrg,
		expRepo,
		foundRemote.Name(),
		strings.Join(foundRemote.URLs(), ", "),
	)

	logrus.Info("Verifying remote HEAD commit")
	args := []string{
		"--heads",
		foundRemote.Name(),
		fmt.Sprintf("refs/heads/%s", branch),
	}
	if branch == "" {
		args = []string{
			"--tags",
			"--heads",
			foundRemote.Name(),
			fmt.Sprintf("refs/tags/%s^{}", expRev),
		}
	}
	lsRemoteOut, err := r.repo.LsRemote(args...)
	if err != nil {
		return errors.Wrap(err, "getting remote HEAD")
	}
	fields := strings.Fields(lsRemoteOut)
	if len(fields) < 1 {
		return errors.Errorf("unexpected output: %s", lsRemoteOut)
	}
	commit := fields[0]
	logrus.Infof("Got remote commit: %s", commit)

	logrus.Info("Verifying that remote commit is equal to the local one")
	if head != commit {
		return errors.Errorf(
			"Local HEAD (%s) is not equal to latest remote commit (%s)",
			head, commit,
		)
	}
	logrus.Info("Repository is up-to-date")

	return nil
}