2019-02-21 15:06:18 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package gitsave
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-02-22 14:01:29 +00:00
|
|
|
"agola.io/agola/internal/errors"
|
2019-07-01 09:40:20 +00:00
|
|
|
"agola.io/agola/internal/util"
|
2019-02-21 15:06:18 +00:00
|
|
|
|
2022-02-21 08:40:18 +00:00
|
|
|
"github.com/gofrs/uuid"
|
2022-02-21 17:07:58 +00:00
|
|
|
"github.com/rs/zerolog"
|
2019-02-21 15:06:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-06-11 13:04:01 +00:00
|
|
|
gitIndexFile = "index"
|
|
|
|
defaultRefsPrefix = "refs/gitsave"
|
2019-02-21 15:06:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func copyFile(src, dest string) error {
|
|
|
|
srcf, err := os.Open(src)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
defer srcf.Close()
|
|
|
|
|
|
|
|
destf, err := os.Create(dest)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
defer destf.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(destf, srcf)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func fileExists(path string) (bool, error) {
|
|
|
|
_, err := os.Stat(path)
|
2022-03-02 12:23:32 +00:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
2022-02-22 14:01:29 +00:00
|
|
|
return false, errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
2022-03-02 12:23:32 +00:00
|
|
|
return !errors.Is(err, os.ErrNotExist), nil
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GitDir returns the git dir relative to the working dir
|
|
|
|
func GitDir() (string, error) {
|
|
|
|
git := &util.Git{}
|
|
|
|
lines, err := git.OutputLines(context.Background(), nil, "rev-parse", "--git-dir")
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
if len(lines) != 1 {
|
|
|
|
return "", errors.Errorf("received %d lines, expected one line", len(lines))
|
|
|
|
}
|
2022-02-22 14:01:29 +00:00
|
|
|
return lines[0], errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func currentGitBranch() (string, error) {
|
|
|
|
git := &util.Git{}
|
|
|
|
lines, err := git.OutputLines(context.Background(), nil, "symbolic-ref", "--short", "HEAD")
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
if len(lines) != 1 {
|
|
|
|
return "", errors.Errorf("received %d lines, expected one line", len(lines))
|
|
|
|
}
|
2022-02-22 14:01:29 +00:00
|
|
|
return lines[0], errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// gitDir returns the git dir relative to the working dir
|
|
|
|
func gitWriteTree(indexPath string) (string, error) {
|
|
|
|
git := &util.Git{Env: []string{"GIT_INDEX_FILE=" + indexPath}}
|
|
|
|
lines, err := git.OutputLines(context.Background(), nil, "write-tree")
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
if len(lines) != 1 {
|
|
|
|
return "", errors.Errorf("received %d lines, expected one line", len(lines))
|
|
|
|
}
|
2022-02-22 14:01:29 +00:00
|
|
|
return lines[0], errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func gitCommitTree(message, treeSHA string) (string, error) {
|
|
|
|
git := &util.Git{}
|
|
|
|
lines, err := git.OutputLines(context.Background(), nil, "commit-tree", "-m", message, treeSHA)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
if len(lines) != 1 {
|
|
|
|
return "", errors.Errorf("received %d lines, expected one line", len(lines))
|
|
|
|
}
|
2022-02-22 14:01:29 +00:00
|
|
|
return lines[0], errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func gitUpdateRef(message, ref, commitSHA string) error {
|
|
|
|
git := &util.Git{}
|
|
|
|
_, err := git.Output(context.Background(), nil, "update-ref", "-m", message, ref, commitSHA)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 22:01:17 +00:00
|
|
|
func gitUpdateFiles(indexPath string) error {
|
|
|
|
git := &util.Git{Env: []string{"GIT_INDEX_FILE=" + indexPath}}
|
|
|
|
_, err := git.Output(context.Background(), nil, "add", "-u")
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 22:01:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 15:06:18 +00:00
|
|
|
func gitAddUntrackedFiles(indexPath string) error {
|
|
|
|
git := &util.Git{Env: []string{"GIT_INDEX_FILE=" + indexPath}}
|
|
|
|
_, err := git.Output(context.Background(), nil, "add", ".")
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func gitAddIgnoredFiles(indexPath string) error {
|
|
|
|
git := &util.Git{Env: []string{"GIT_INDEX_FILE=" + indexPath}}
|
|
|
|
_, err := git.Output(context.Background(), nil, "add", "-f", "-A", ".")
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GitAddRemote(configPath, name, url string) error {
|
|
|
|
git := &util.Git{}
|
|
|
|
_, err := git.Output(context.Background(), nil, "remote", "add", name, url)
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GitPush(configPath, remote, branch string) error {
|
|
|
|
git := &util.Git{}
|
|
|
|
_, err := git.Output(context.Background(), nil, "push", remote, branch, "-f")
|
2022-02-22 14:01:29 +00:00
|
|
|
return errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GitSaveConfig struct {
|
|
|
|
AddUntracked bool
|
|
|
|
AddIgnored bool
|
2019-06-11 13:04:01 +00:00
|
|
|
RefsPrefix string
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GitSave struct {
|
2022-02-21 17:07:58 +00:00
|
|
|
log zerolog.Logger
|
2019-06-11 13:04:01 +00:00
|
|
|
conf *GitSaveConfig
|
|
|
|
refsPrefix string
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
func NewGitSave(log zerolog.Logger, conf *GitSaveConfig) *GitSave {
|
2019-06-11 13:04:01 +00:00
|
|
|
refsPrefix := conf.RefsPrefix
|
|
|
|
if refsPrefix == "" {
|
|
|
|
refsPrefix = defaultRefsPrefix
|
|
|
|
}
|
|
|
|
return &GitSave{
|
2022-02-21 17:07:58 +00:00
|
|
|
log: log,
|
2019-06-11 13:04:01 +00:00
|
|
|
conf: conf,
|
|
|
|
refsPrefix: refsPrefix,
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *GitSave) RefsPrefix() string {
|
|
|
|
return s.refsPrefix
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save adds files to the provided index, creates a tree and a commit pointing to
|
|
|
|
// that tree, finally it creates a branch poiting to that commit
|
|
|
|
// Save will use the current worktree index if available to speed the index generation
|
2019-06-11 13:04:34 +00:00
|
|
|
func (s *GitSave) Save(message, branchName string) (string, error) {
|
2019-02-21 15:06:18 +00:00
|
|
|
gitdir, err := GitDir()
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 08:40:18 +00:00
|
|
|
tmpIndexPath := filepath.Join(gitdir, "gitsave-index-"+uuid.Must(uuid.NewV4()).String())
|
2019-02-21 22:01:17 +00:00
|
|
|
defer os.Remove(tmpIndexPath)
|
|
|
|
|
2019-02-21 15:06:18 +00:00
|
|
|
indexPath := filepath.Join(gitdir, gitIndexFile)
|
|
|
|
|
|
|
|
curBranch, err := currentGitBranch()
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
indexExists, err := fileExists(indexPath)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if indexExists {
|
|
|
|
// copy current git index to a temporary index
|
|
|
|
if err := copyFile(indexPath, tmpIndexPath); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Info().Msgf("created temporary index: %s", tmpIndexPath)
|
2019-02-21 15:06:18 +00:00
|
|
|
// read the current branch tree information into the index
|
|
|
|
git := &util.Git{Env: []string{"GIT_INDEX_FILE=" + tmpIndexPath}}
|
|
|
|
_, err = git.Output(context.Background(), nil, "read-tree", curBranch)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Info().Msgf("index %s does not exist", indexPath)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Info().Msgf("updating files already in the index")
|
2019-02-21 22:01:17 +00:00
|
|
|
if err := gitUpdateFiles(tmpIndexPath); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 22:01:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 15:06:18 +00:00
|
|
|
if s.conf.AddUntracked {
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Info().Msgf("adding untracked files")
|
2019-02-21 15:06:18 +00:00
|
|
|
if err := gitAddUntrackedFiles(tmpIndexPath); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.conf.AddIgnored {
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Info().Msgf("adding ignored files")
|
2019-02-21 15:06:18 +00:00
|
|
|
if err := gitAddIgnoredFiles(tmpIndexPath); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Info().Msgf("writing tree file")
|
2019-02-21 15:06:18 +00:00
|
|
|
treeSHA, err := gitWriteTree(tmpIndexPath)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Info().Msgf("tree: %s", treeSHA)
|
2019-02-21 15:06:18 +00:00
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Info().Msgf("committing tree")
|
2019-02-21 22:01:17 +00:00
|
|
|
commitSHA, err := gitCommitTree(message, treeSHA)
|
2019-02-21 15:06:18 +00:00
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Info().Msgf("commit: %s", commitSHA)
|
2019-02-21 15:06:18 +00:00
|
|
|
|
2022-02-21 17:07:58 +00:00
|
|
|
s.log.Info().Msgf("updating ref")
|
2019-06-11 13:04:01 +00:00
|
|
|
if err = gitUpdateRef("git-save", filepath.Join(s.refsPrefix, branchName), commitSHA); err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return "", errors.WithStack(err)
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 13:04:34 +00:00
|
|
|
return commitSHA, nil
|
2019-02-21 15:06:18 +00:00
|
|
|
}
|