diff --git a/plugins/buildpacks/functions.go b/plugins/buildpacks/functions.go index 1845efae8..47acbbc78 100644 --- a/plugins/buildpacks/functions.go +++ b/plugins/buildpacks/functions.go @@ -64,15 +64,25 @@ func validBuildpackURL(buildpack string) (string, error) { return buildpack, fmt.Errorf("Invalid buildpack specified: %v", buildpack) } -func checkoutBareGitRepo(sourceWorkDir string) (string, error) { +func checkoutBareGitRepo(sourceWorkDir string, branch string) (string, error) { tmpdir, err := os.MkdirTemp("", "bare-git-checkout") if err != nil { return "", fmt.Errorf("Unable to create temporary directory: %s", err.Error()) } + args := []string{ + "--git-dir=" + sourceWorkDir, + "--work-tree=" + tmpdir, + "checkout", "-f", + } + + if branch != "" { + args = append(args, branch) + } + result, err := common.CallExecCommand(common.ExecCommandInput{ Command: "git", - Args: []string{"--git-dir=" + sourceWorkDir, "--work-tree=" + tmpdir, "checkout", "-f"}, + Args: args, }) if err != nil { diff --git a/plugins/buildpacks/src/commands/commands.go b/plugins/buildpacks/src/commands/commands.go index c2238a5a4..51eb30a15 100644 --- a/plugins/buildpacks/src/commands/commands.go +++ b/plugins/buildpacks/src/commands/commands.go @@ -20,7 +20,7 @@ Additional commands:` helpContent = ` buildpacks:add [--index 1] , Add new app buildpack while inserting into list of buildpacks if necessary buildpacks:clear , Clear all buildpacks set on the app - buildpacks:detect , Detect the buildpack for an app + buildpacks:detect [--branch ], Detect the buildpack for an app (if not specified, the branch flag defaults to the deploy branch) buildpacks:list , List all buildpacks for an app buildpacks:remove , Remove a buildpack set on the app buildpacks:report [] [], Displays a buildpack report for one or more apps diff --git a/plugins/buildpacks/src/subcommands/subcommands.go b/plugins/buildpacks/src/subcommands/subcommands.go index d1c596bcc..9062e80fa 100644 --- a/plugins/buildpacks/src/subcommands/subcommands.go +++ b/plugins/buildpacks/src/subcommands/subcommands.go @@ -32,9 +32,13 @@ func main() { err = buildpacks.CommandClear(appName) case "detect": args := flag.NewFlagSet("buildpacks:detect", flag.ExitOnError) + branch := args.String("branch", "", "--branch: the branch to detect against (defaults to the deploy branch)") args.Parse(os.Args[2:]) appName := args.Arg(0) - err = buildpacks.CommandDetect(appName) + if *branch == "" { + *branch = common.PropertyGet("git", appName, "deploy-branch") + } + err = buildpacks.CommandDetect(appName, *branch) case "list": args := flag.NewFlagSet("buildpacks:list", flag.ExitOnError) args.Parse(os.Args[2:]) diff --git a/plugins/buildpacks/subcommands.go b/plugins/buildpacks/subcommands.go index 86255e628..079de2091 100644 --- a/plugins/buildpacks/subcommands.go +++ b/plugins/buildpacks/subcommands.go @@ -32,13 +32,13 @@ func CommandClear(appName string) error { } // CommandDetect implements buildpacks:detect -func CommandDetect(appName string) error { +func CommandDetect(appName string, branch string) (err error) { if err := common.VerifyAppName(appName); err != nil { return err } workDir := common.AppRoot(appName) - checkedOutDir, err := checkoutBareGitRepo(workDir) + checkedOutDir, err := checkoutBareGitRepo(workDir, branch) if err != nil { return err }