• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kubernetes-sigs / kubebuilder / 17173341912

23 Aug 2025 08:26AM UTC coverage: 70.428% (-0.06%) from 70.492%
17173341912

Pull #5039

github

camilamacedo86
(fix): ensure that by default is used merge.conflictStyle=merge to facilitate the review
Pull Request #5039: 🐛 (alpha update): ensure that by default is used merge.conflictStyle=merge to facilitate the review

2 of 7 new or added lines in 1 file covered. (28.57%)

3096 of 4396 relevant lines covered (70.43%)

14.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.38
/pkg/cli/alpha/update.go
1
/*
2
Copyright 2025 The Kubernetes Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package alpha
18

19
import (
20
        "fmt"
21
        "log"
22

23
        "github.com/spf13/cobra"
24

25
        "sigs.k8s.io/kubebuilder/v4/pkg/cli/alpha/internal/update"
26
)
27

28
// NewUpdateCommand creates and returns a new Cobra command for updating Kubebuilder projects.
29
func NewUpdateCommand() *cobra.Command {
2✔
30
        opts := update.Update{}
2✔
31
        var gitCfg []string
2✔
32
        updateCmd := &cobra.Command{
2✔
33
                Use:   "update",
2✔
34
                Short: "Update your project to a newer version (3-way merge; squash by default)",
2✔
35
                Long: `Upgrade your project scaffold using a 3-way merge while preserving your code.
2✔
36

2✔
37
The updater uses four temporary branches during the run:
2✔
38
  • ancestor : clean scaffold from the starting version (--from-version)
2✔
39
  • original : snapshot of your current project (--from-branch)
2✔
40
  • upgrade  : scaffold generated with the target version (--to-version)
2✔
41
  • merge    : result of merging original into upgrade (conflicts possible)
2✔
42

2✔
43
Output branch & history:
2✔
44
  • Default: SQUASH the merge result into ONE commit on:
2✔
45
        kubebuilder-update-from-<from-version>-to-<to-version>
2✔
46
  • --show-commits: keep full history (not compatible with --restore-path).
2✔
47

2✔
48
Conflicts:
2✔
49
  • Default: stop on conflicts and leave the merge branch for manual resolution.
2✔
50
  • --force: commit with conflict markers so automation can proceed.
2✔
51

2✔
52
Other options:
2✔
53
  • --restore-path: restore paths from base when squashing (e.g., CI configs).
2✔
54
  • --output-branch: override the output branch name.
2✔
55
  • --push: push the output branch to 'origin' after the update.
2✔
56
  • --git-config: pass per-invocation Git config as -c key=value (repeatable). When not set,
2✔
57
      defaults are set to improve detection during merges.
2✔
58

2✔
59
Defaults:
2✔
60
  • --from-version / --to-version: resolved from PROJECT and the latest release if unset.
2✔
61
  • --from-branch: defaults to 'main' if not specified.`,
2✔
62
                Example: `
2✔
63
  # Update from the version in PROJECT to the latest, stop on conflicts
2✔
64
  kubebuilder alpha update
2✔
65

2✔
66
  # Update from a specific version to latest
2✔
67
  kubebuilder alpha update --from-version v4.6.0
2✔
68

2✔
69
  # Update from v4.5.0 to v4.7.0 and keep conflict markers (automation-friendly)
2✔
70
  kubebuilder alpha update --from-version v4.5.0 --to-version v4.7.0 --force
2✔
71

2✔
72
  # Keep full commit history instead of squashing
2✔
73
  kubebuilder alpha update --from-version v4.5.0 --to-version v4.7.0 --force --show-commits
2✔
74

2✔
75
  # Squash while preserving CI workflows from base (e.g., main)
2✔
76
  kubebuilder alpha update --force --restore-path .github/workflows
2✔
77

2✔
78
  # Show commits into a custom output branch name
2✔
79
  kubebuilder alpha update --force --show-commits --output-branch my-update-branch
2✔
80

2✔
81
  # Run update and push the output branch to origin (works with or without --show-commits)
2✔
82
  kubebuilder alpha update --from-version v4.6.0 --to-version v4.7.0 --force --push
2✔
83

2✔
84
  # Add extra Git configs (no need to re-specify defaults)
2✔
85
  kubebuilder alpha update --git-config merge.conflictStyle=diff3 --git-config rerere.enabled=true
2✔
86
                                          
2✔
87
  # Disable Git config defaults completely, use only custom configs
2✔
88
  kubebuilder alpha update --git-config disable --git-config rerere.enabled=true`,
2✔
89
                PreRunE: func(_ *cobra.Command, _ []string) error {
2✔
90
                        if opts.ShowCommits && len(opts.RestorePath) > 0 {
×
91
                                return fmt.Errorf("the --restore-path flag is not supported with --show-commits")
×
92
                        }
×
93

94
                        // Defaults always on unless "disable" is present anywhere
NEW
95
                        defaults := []string{
×
NEW
96
                                "merge.renameLimit=999999",
×
NEW
97
                                "diff.renameLimit=999999",
×
NEW
98
                                "merge.conflictStyle=merge",
×
NEW
99
                        }
×
100

×
101
                        hasDisable := false
×
102
                        filtered := make([]string, 0, len(gitCfg))
×
103
                        for _, v := range gitCfg {
×
104
                                if v == "disable" {
×
105
                                        hasDisable = true
×
106
                                        continue
×
107
                                }
108
                                filtered = append(filtered, v)
×
109
                        }
110

111
                        if hasDisable {
×
112
                                // no defaults; only user-provided configs (excluding "disable")
×
113
                                opts.GitConfig = filtered
×
114
                        } else {
×
115
                                // defaults + user configs (user can override by repeating keys)
×
116
                                opts.GitConfig = append(defaults, filtered...)
×
117
                        }
×
118

119
                        if err := opts.Prepare(); err != nil {
×
120
                                return fmt.Errorf("failed to prepare update: %w", err)
×
121
                        }
×
122
                        return opts.Validate()
×
123
                },
124
                Run: func(_ *cobra.Command, _ []string) {
×
125
                        if err := opts.Update(); err != nil {
×
126
                                log.Fatalf("Update failed: %s", err)
×
127
                        }
×
128
                },
129
        }
130

131
        updateCmd.Flags().StringVar(&opts.FromVersion, "from-version", "",
2✔
132
                "binary release version to upgrade from. Should match the version used to init the project and be "+
2✔
133
                        "a valid release version, e.g., v4.6.0. If not set, it defaults to the version specified in the PROJECT file.")
2✔
134
        updateCmd.Flags().StringVar(&opts.ToVersion, "to-version", "",
2✔
135
                "binary release version to upgrade to. Should be a valid release version, e.g., v4.7.0. "+
2✔
136
                        "If not set, it defaults to the latest release version available in the project repository.")
2✔
137
        updateCmd.Flags().StringVar(&opts.FromBranch, "from-branch", "",
2✔
138
                "Git branch to use as current state of the project for the update.")
2✔
139
        updateCmd.Flags().BoolVar(&opts.Force, "force", false,
2✔
140
                "Force the update even if conflicts occur. Conflicted files will include conflict markers, and a "+
2✔
141
                        "commit will be created automatically. Ideal for automation (e.g., cronjobs, CI).")
2✔
142
        updateCmd.Flags().BoolVar(&opts.ShowCommits, "show-commits", false,
2✔
143
                "If set, the update will keep the full history instead of squashing into a single commit.")
2✔
144
        updateCmd.Flags().StringArrayVar(&opts.RestorePath, "restore-path", nil,
2✔
145
                "Paths to preserve from the base branch (repeatable). Not supported with --show-commits.")
2✔
146
        updateCmd.Flags().StringVar(&opts.OutputBranch, "output-branch", "",
2✔
147
                "Override the default output branch name (default: kubebuilder-update-from-<from-version>-to-<to-version>).")
2✔
148
        updateCmd.Flags().BoolVar(&opts.Push, "push", false,
2✔
149
                "Push the output branch to the remote repository after the update.")
2✔
150
        updateCmd.Flags().BoolVar(&opts.OpenGhIssue, "open-gh-issue", false,
2✔
151
                "Create a GitHub issue with a pre-filled checklist and compare link after the update completes (requires `gh`).")
2✔
152
        updateCmd.Flags().StringArrayVar(
2✔
153
                &gitCfg,
2✔
154
                "git-config",
2✔
155
                nil,
2✔
156
                "Per-invocation Git config (repeatable). "+
2✔
157
                        "Defaults: -c merge.renameLimit=999999 -c diff.renameLimit=999999 -c merge.conflictStyle=merge. "+
2✔
158
                        "Your configs are applied on top. To disable defaults, include `--git-config disable`")
2✔
159
        return updateCmd
2✔
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc