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

Improve how we use git in eext and improve the code's testing facilities. #152

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
refactor "cloning" of the repository
There was a lot of boiler plate that was hard to follow, so this patch
introduces a simple list of git commands to run. It should also yield
potentially better error messages in case of a failure.
  • Loading branch information
mkisielewski-arista committed Jan 8, 2025
commit fb7990f635e123804c9a54bdfd06c159e1393cca
44 changes: 17 additions & 27 deletions impl/create_srpm_from_git.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,31 @@ func getRpmNameFromSpecFile(repo, pkg string, isPkgSubdirInRepo bool) (string, e
}

// Create a lightweight git repo containing only the `revision` pulled from
// the repository specified in `srcURL`
// the repository specified by `srcURL`
// We aren't using 'git clone' since it is slow for large repos.
// This method is faster and pulls only the necessary changes.
func cloneGitRepo(pkg, srcURL, revision, targetDir string) (string, error) {
// Cloning the git repo to a temporary directory
git_commands := [][]string{
{"init"},
{"remote", "add", "origin", srcURL},
{"fetch", "--tags"},
{"fetch", "origin", revision},
{"reset", "--hard", "FETCH_HEAD"},
}

cloneDir, err := os.MkdirTemp(targetDir, pkg)
if err != nil {
return "", fmt.Errorf("error while creating tempDir for %s, %s", pkg, err)
}
// Init the dir as a git repo
err = util.RunSystemCmdInDir(cloneDir, "git", "init")
if err != nil {
return "", fmt.Errorf("git init at %s failed: %s", cloneDir, err)
}
// Add the srcURL as the origin for the repo
err = util.RunSystemCmdInDir(cloneDir, "git", "remote", "add", "origin", srcURL)
if err != nil {
return "", fmt.Errorf("adding %s as git remote failed: %s", srcURL, err)
}
// Fetch repo tags, for user inputs revision as TAG
err = util.RunSystemCmdInDir(cloneDir, "git", "fetch", "--tags")
if err != nil {
return "", fmt.Errorf("fetching tags failed for %s: %s", pkg, err)
}
// Fetch the code changes for the provided revision
err = util.RunSystemCmdInDir(cloneDir, "git", "fetch", "origin", revision)
if err != nil {
return "", fmt.Errorf("fetching revision %s failed for %s: %s", revision, pkg, err)
}
// Pull code to repo at provided revision
err = util.RunSystemCmdInDir(cloneDir, "git", "reset", "--hard", "FETCH_HEAD")
if err != nil {
return "", fmt.Errorf("fetching HEAD at %s failed: %s", revision, err)
}
for _, git_command := range(git_commands) {
err := util.RunSystemCmdInDir(cloneDir, "git", git_command...)
if err != nil {
return "", fmt.Errorf("Failed to obtain `%s` revision from `%s` for " +
"package `%s`.\nThe command `git %s` failed: %s",
revision, srcURL, pkg, strings.Join(git_command, " "), err)

}
}
return cloneDir, nil
}

Expand Down