MCPcopy
hub / github.com/kptdev/kpt / patchLocalPackage

Method patchLocalPackage

internal/util/update/gitpatch.go:113–155  ·  view source on GitHub ↗

patchLocalPackage will run 'git am' to patch the local package.

()

Source from the content-addressed store, hash-verified

111
112// patchLocalPackage will run 'git am' to patch the local package.
113func (u *GitPatchUpdater) patchLocalPackage() error {
114 g := gitutil.NewLocalGitRunner(u.UpdateOptions.PackagePath)
115
116 // add the cached update as an upstream so git can figure out how to do the
117 // 3-way merge when it looks for the commits in the patch file.
118 fmt.Fprintf(os.Stderr,
119 "fetching upstream updates locally staged at %q\n", u.gitRunner.RepoDir)
120 // TODO(pwittrock): consider fetching directly without adding using git fetch <path>
121 // and determine if there are any benefits in doing so over this approach.
122 if err := g.Run(
123 "remote", "add", alphaGitPatchRemote, u.gitRunner.RepoDir); err != nil {
124 return errors.Errorf("update failed: failure running git remote %q: %s %s",
125 err, g.Stderr.String(), g.Stdout.String())
126 }
127 defer func() {
128 // delete the remote when we are done
129 err := g.Run("remote", "remove", alphaGitPatchRemote)
130 if err != nil {
131 fmt.Fprintf(os.Stderr, "cleanup remote failed: %v\n", err)
132 }
133 }()
134 defaultRef, err := gitutil.DefaultRef(u.UpdateOptions.ToRepo)
135 if err != nil {
136 return err
137 }
138 if err := g.Run("fetch", alphaGitPatchRemote, defaultRef); err != nil {
139 return errors.Errorf("update failed: failure running git fetch %q: %s %s",
140 err, g.Stderr.String(), g.Stdout.String())
141 }
142
143 // run `git am` to apply the patch
144 fmt.Fprintf(os.Stderr,
145 "applying upstream updates using `git am -3 --directory %s`\n", u.PackagePath)
146 g.Stdin = &bytes.Buffer{}
147 if _, err := g.Stdin.WriteString(u.patch); err != nil {
148 return err
149 }
150 if err := g.Run("am", "-3", "--directory", u.PackagePath); err != nil {
151 return errors.Errorf("update failed: failure running git am: %q: %s %s",
152 err, g.Stderr.String(), g.Stdout.String())
153 }
154 return nil
155}
156
157// hardResetSourceFiles hard resets the repo to the commit we are updating from.
158// it also writes the Kptfile from the local package so that the patch correctly updates it

Callers 1

UpdateMethod · 0.95

Calls 3

NewLocalGitRunnerFunction · 0.92
RunMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected