Skip to content

Commit 3b93ac5

Browse files
author
Antonio Salinas
committed
refactor: invert with-git flag
1 parent a0c86f5 commit 3b93ac5

6 files changed

Lines changed: 50 additions & 14 deletions

File tree

internal/app/azldev/cmds/component/build.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type ComponentBuildOptions struct {
2929

3030
ContinueOnError bool
3131
NoCheck bool
32-
WithGitRepo bool
32+
WithoutGitRepo bool
3333
SourcePackageOnly bool
3434
BuildEnvPolicy BuildEnvPreservePolicy
3535

@@ -131,8 +131,8 @@ builds can consume.`,
131131
cmd.Flags().BoolVarP(&options.ContinueOnError, "continue-on-error", "k", false,
132132
"Continue building when some components fail")
133133
cmd.Flags().BoolVar(&options.NoCheck, "no-check", false, "Skip package %check tests")
134-
cmd.Flags().BoolVar(&options.WithGitRepo, "with-git", false,
135-
"Create a dist-git repository with synthetic commit history (requires a project git repository)")
134+
cmd.Flags().BoolVar(&options.WithoutGitRepo, "without-git", false,
135+
"Skip creating a dist-git repository with synthetic commit history")
136136
cmd.Flags().BoolVar(&options.SourcePackageOnly, "srpm-only", false, "Build SRPM (source RPM) *only*")
137137
cmd.Flags().Var(&options.BuildEnvPolicy, "preserve-buildenv",
138138
fmt.Sprintf("Preserve build environment {%s, %s, %s}",
@@ -252,7 +252,7 @@ func BuildComponent(
252252
}, &err)
253253

254254
var preparerOpts []sources.PreparerOption
255-
if options.WithGitRepo {
255+
if !options.WithoutGitRepo {
256256
preparerOpts = append(preparerOpts, sources.WithGitRepo(env, env.LockReader()))
257257
}
258258

internal/app/azldev/cmds/component/build_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ func TestNewBuildCommand(t *testing.T) {
2323
assert.Equal(t, "build", cmd.Use)
2424
assert.NotNil(t, cmd.RunE)
2525
}
26+
27+
withoutGitFlag := cmd.Flags().Lookup("without-git")
28+
require.NotNil(t, withoutGitFlag, "--without-git flag should be registered")
29+
assert.Equal(t, "false", withoutGitFlag.DefValue, "dist-git flow should be enabled by default")
30+
assert.Contains(t, withoutGitFlag.Usage, "dist-git")
31+
32+
// Legacy --with-git flag must NOT exist.
33+
withGitFlag := cmd.Flags().Lookup("with-git")
34+
assert.Nil(t, withGitFlag, "--with-git flag must not be registered")
2635
}
2736

2837
func TestNewBuildCommand_MockConfigOptFlag(t *testing.T) {

internal/app/azldev/cmds/component/preparesources.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import (
1919
type PrepareSourcesOptions struct {
2020
ComponentFilter components.ComponentFilter
2121

22-
OutputDir string
23-
SkipOverlays bool
24-
WithGitRepo bool
25-
Force bool
26-
AllowNoHashes bool
22+
OutputDir string
23+
SkipOverlays bool
24+
WithoutGitRepo bool
25+
Force bool
26+
AllowNoHashes bool
2727
}
2828

2929
func prepareOnAppInit(_ *azldev.App, sourceCmd *cobra.Command) {
@@ -68,8 +68,8 @@ Only one component may be selected at a time.`,
6868
_ = cmd.MarkFlagDirname("output-dir")
6969

7070
cmd.Flags().BoolVar(&options.SkipOverlays, "skip-overlays", false, "skip applying overlays to prepared sources")
71-
cmd.Flags().BoolVar(&options.WithGitRepo, "with-git", false,
72-
"Create a dist-git repository with synthetic commit history (requires a project git repository)")
71+
cmd.Flags().BoolVar(&options.WithoutGitRepo, "without-git", false,
72+
"Disable dist-git repository creation (enabled by default)")
7373
cmd.Flags().BoolVar(&options.Force, "force", false, "delete and recreate the output directory if it already exists")
7474
cmd.Flags().BoolVar(&options.AllowNoHashes, "allow-no-hashes", false,
7575
"compute missing hashes by downloading source files from their origin")
@@ -121,13 +121,13 @@ func PrepareComponentSources(env *azldev.Env, options *PrepareSourcesOptions) er
121121
return err
122122
}
123123

124-
if options.SkipOverlays && options.WithGitRepo {
125-
slog.Warn("--with-git has no effect when --skip-overlays is set; " +
124+
if options.SkipOverlays && !options.WithoutGitRepo {
125+
slog.Warn("dist-git flow has no effect when '--skip-overlays' is set; " +
126126
"synthetic history requires overlays to be applied")
127127
}
128128

129129
var preparerOpts []sources.PreparerOption
130-
if options.WithGitRepo {
130+
if !options.WithoutGitRepo {
131131
preparerOpts = append(preparerOpts, sources.WithGitRepo(env, env.LockReader()))
132132
}
133133

internal/app/azldev/cmds/component/preparesources_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ func TestNewPrepareSourcesCmd(t *testing.T) {
2828
require.NotNil(t, allowNoHashesFlag, "--allow-no-hashes flag should be registered")
2929
assert.Equal(t, "false", allowNoHashesFlag.DefValue)
3030
assert.Contains(t, allowNoHashesFlag.Usage, "compute missing hashes")
31+
32+
withoutGitFlag := cmd.Flags().Lookup("without-git")
33+
require.NotNil(t, withoutGitFlag, "--without-git flag should be registered")
34+
assert.Equal(t, "false", withoutGitFlag.DefValue, "dist-git flow should be enabled by default")
35+
assert.Contains(t, withoutGitFlag.Usage, "dist-git")
36+
37+
// Legacy --with-git flag must NOT exist.
38+
withGitFlag := cmd.Flags().Lookup("with-git")
39+
assert.Nil(t, withGitFlag, "--with-git flag must not be registered")
3140
}
3241

3342
func TestPrepareSourcesCmd_NoMatch(t *testing.T) {

scenario/component_build_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func TestBuildingLocalComponent(t *testing.T) {
3434
project := projecttest.NewDynamicTestProject(
3535
projecttest.AddSpec(spec),
3636
projecttest.UseTestDefaultConfigs(),
37+
projecttest.WithGitRepo(),
3738
)
3839

3940
// Run the build with test default configs copied into the container.
@@ -76,6 +77,7 @@ func TestBuildingLocalComponentFromCheckedInFiles(t *testing.T) {
7677
// Include test default configs to get distro and mock configurations.
7778
project := projecttest.NewTemplatedTestProject(t, "testdata/simple",
7879
projecttest.TemplatedUseTestDefaultConfigs(),
80+
projecttest.TemplatedWithGitRepo(),
7981
)
8082

8183
// Run the build with test default configs copied into the container.
@@ -121,6 +123,7 @@ func TestBuildingUpstreamComponent(t *testing.T) {
121123
project := projecttest.NewDynamicTestProject(
122124
projecttest.AddComponent(&projectconfig.ComponentConfig{Name: testComponentName}),
123125
projecttest.UseTestDefaultConfigs(),
126+
projecttest.WithGitRepo(),
124127
)
125128

126129
// Run the build with test default configs copied into the container.

scenario/internal/projecttest/templatetestproject.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type TemplatedTestProjectOption func(*templatedTestProject)
2323
type templatedTestProject struct {
2424
templateDir string
2525
useTestDefaultConfigs bool
26+
initGitRepo bool
2627
}
2728

2829
// TemplatedUseTestDefaultConfigs configures the templated project to include the test default configs.
@@ -35,6 +36,15 @@ func TemplatedUseTestDefaultConfigs() TemplatedTestProjectOption {
3536
}
3637
}
3738

39+
// TemplatedWithGitRepo initializes the project directory as a git repository with an
40+
// initial commit containing all project files. Required for commands that use synthetic
41+
// history (e.g., [component build], [component render]).
42+
func TemplatedWithGitRepo() TemplatedTestProjectOption {
43+
return func(p *templatedTestProject) {
44+
p.initGitRepo = true
45+
}
46+
}
47+
3848
func NewTemplatedTestProject(
3949
t *testing.T,
4050
templateDir string,
@@ -97,6 +107,11 @@ func (p *templatedTestProject) Serialize(t *testing.T, projectDir string) {
97107
require.NoError(t, afero.WriteFile(osFS, configFilePath, modifiedBytes, fileperms.PublicFile),
98108
"failed to write config file with test default configs")
99109
}
110+
111+
// Initialize a git repo if requested.
112+
if p.initGitRepo {
113+
initProjectGitRepo(t, projectDir)
114+
}
100115
}
101116

102117
type notDryRun struct{}

0 commit comments

Comments
 (0)