added field clean bool for cleaning before checkout

This commit is contained in:
pasha1coil 2025-04-10 12:16:32 +03:00
parent 25266560d2
commit 49f17fef28
2 changed files with 26 additions and 0 deletions

@ -26,6 +26,10 @@ inputs:
description: 'Number of commits to fetch. 0 indicates all history for all branches and tags.' description: 'Number of commits to fetch. 0 indicates all history for all branches and tags.'
required: false required: false
default: 1 default: 1
clean:
description: 'Whether to execute `git clean -ffdx && git reset --hard HEAD` before fetching'
required: false
default: true
runs: runs:
using: 'go' using: 'go'

@ -16,8 +16,11 @@ type Config struct {
ServerURL string ServerURL string
CheckoutType CheckoutType CheckoutType CheckoutType
FetchDepth string FetchDepth string
Clean bool
} }
// todo lfs
type CheckoutType string type CheckoutType string
const ( const (
@ -30,6 +33,14 @@ func getConfig() Config {
if fetchDepth == "" || fetchDepth == "0" { if fetchDepth == "" || fetchDepth == "0" {
fetchDepth = "1" fetchDepth = "1"
} }
clean := os.Getenv("INPUT_CLEAN")
cleanBool := true
if clean == "0" || clean == "false" || clean == "f" {
cleanBool = false
}
return Config{ return Config{
Repository: os.Getenv("INPUT_REPOSITORY"), Repository: os.Getenv("INPUT_REPOSITORY"),
Ref: os.Getenv("INPUT_REF"), Ref: os.Getenv("INPUT_REF"),
@ -38,6 +49,7 @@ func getConfig() Config {
ServerURL: os.Getenv("INPUT_SERVERURL"), ServerURL: os.Getenv("INPUT_SERVERURL"),
CheckoutType: CheckoutType(os.Getenv("INPUT_CHECKOUTTYPE")), CheckoutType: CheckoutType(os.Getenv("INPUT_CHECKOUTTYPE")),
FetchDepth: fetchDepth, FetchDepth: fetchDepth,
Clean: cleanBool,
} }
} }
@ -57,6 +69,15 @@ func main() {
log.Fatal("field serverURL is required") log.Fatal("field serverURL is required")
} }
if cfg.Clean {
if _, err := os.Stat(cfg.Path); err == nil {
run("git", "-C", cfg.Path, "clean", "-ffdx")
run("git", "-C", cfg.Path, "reset", "--hard", "HEAD")
} else {
log.Println(fmt.Sprintf("not path-%s directory for cleaning", cfg.Path))
}
}
cloneURL := cfg.CheckoutType.GenerateCloneURL(cfg) cloneURL := cfg.CheckoutType.GenerateCloneURL(cfg)
fmt.Println("Cloning", cloneURL, "into", cfg.Path) fmt.Println("Cloning", cloneURL, "into", cfg.Path)
@ -70,6 +91,7 @@ func main() {
} }
func run(name string, args ...string) { func run(name string, args ...string) {
fmt.Println("git command: ", name, strings.Join(args, " "))
cmd := exec.Command(name, args...) cmd := exec.Command(name, args...)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr