Skip to content

Commit

Permalink
Support sha256 git repos (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 authored Sep 20, 2024
1 parent 13ade30 commit d59c372
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ var globalFlags = []cli.Flag{
Usage: "git clone ssh url",
EnvVars: []string{"PLUGIN_REMOTE_SSH", "CI_REPO_CLONE_SSH_URL"},
},
&cli.StringFlag{
Name: "object-format",
Usage: "specify the object format (hash) to be used on init. if not set it is autodetect by the commit sha.",
EnvVars: []string{"PLUGIN_OBJECT_FORMAT"},
},
&cli.StringFlag{
Name: "path",
Usage: "git clone path",
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func run(c *cli.Context) error {

plugin := Plugin{
Repo: Repo{
Clone: c.String("remote"),
CloneSSH: c.String("remote-ssh"),
Clone: c.String("remote"),
CloneSSH: c.String("remote-ssh"),
ObjectFormat: c.String("object-format"),
},
Pipeline: Pipeline{
Commit: c.String("sha"),
Expand Down
16 changes: 12 additions & 4 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,16 @@ func (p Plugin) Exec() error {
}
}

// autodetect object-format if not set
if p.Repo.ObjectFormat == "" {
p.Repo.ObjectFormat = "sha1"
if len(p.Pipeline.Commit) == 64 {
p.Repo.ObjectFormat = "sha256"
}
}

if isDirEmpty(filepath.Join(p.Pipeline.Path, ".git")) {
cmds = append(cmds, initGit(p.Config.Branch))
cmds = append(cmds, initGit(p.Config.Branch, p.Repo.ObjectFormat))
cmds = append(cmds, safeDirectory(p.Config.SafeDirectory))
if p.Config.UseSSH {
// If env var PLUGIN_USE_SSH is set to true, use SSH instead of HTTPS
Expand Down Expand Up @@ -217,11 +225,11 @@ func appendEnv(cmd *exec.Cmd, env ...string) *exec.Cmd {
}

// Creates an empty git repository.
func initGit(branch string) *exec.Cmd {
func initGit(branch, objectFormat string) *exec.Cmd {
if branch == "" {
return appendEnv(exec.Command("git", "init"), defaultEnvVars...)
return appendEnv(exec.Command("git", "init", "--object-format", objectFormat), defaultEnvVars...)
}
return appendEnv(exec.Command("git", "init", "-b", branch), defaultEnvVars...)
return appendEnv(exec.Command("git", "init", "--object-format", objectFormat, "-b", branch), defaultEnvVars...)
}

func safeDirectory(safeDirectory string) *exec.Cmd {
Expand Down
5 changes: 3 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (

type (
Repo struct {
Clone string
CloneSSH string
Clone string
CloneSSH string
ObjectFormat string
}

Pipeline struct {
Expand Down

0 comments on commit d59c372

Please sign in to comment.