Skip to content

Commit cbb3750

Browse files
committed
wip
1 parent 549dc05 commit cbb3750

File tree

5 files changed

+36
-35
lines changed

5 files changed

+36
-35
lines changed

components/content-service/pkg/git/git.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ func (c *Client) GitWithOutput(ctx context.Context, ignoreErr *string, subcomman
199199

200200
cmdName := "git"
201201
if sudo {
202-
cmdName = "sudo -u gitpod git"
202+
cmdName = "sudo"
203+
fullArgs = append([]string{"-u", "gitpod", "git"}, fullArgs...)
203204
}
204205
cmd := exec.Command(cmdName, fullArgs...)
205206
cmd.Dir = c.Location
@@ -223,8 +224,8 @@ func (c *Client) GitWithOutput(ctx context.Context, ignoreErr *string, subcomman
223224
}
224225

225226
// Git executes git using the client configuration
226-
func (c *Client) Git(ctx context.Context, subcommand string, args ...string) (err error) {
227-
_, err = c.GitWithOutput(ctx, nil, subcommand, true, args...)
227+
func (c *Client) Git(ctx context.Context, subcommand string, sudo bool, args ...string) (err error) {
228+
_, err = c.GitWithOutput(ctx, nil, subcommand, sudo, args...)
228229
if err != nil {
229230
return err
230231
}
@@ -328,7 +329,7 @@ func (c *Client) Status(ctx context.Context) (res *Status, err error) {
328329
}
329330

330331
// Clone runs git clone
331-
func (c *Client) Clone(ctx context.Context) (err error) {
332+
func (c *Client) Clone(ctx context.Context, sudo bool) (err error) {
332333
err = os.MkdirAll(c.Location, 0775)
333334
if err != nil {
334335
log.WithError(err).Error("cannot create clone location")
@@ -349,7 +350,7 @@ func (c *Client) Clone(ctx context.Context) (err error) {
349350

350351
args = append(args, ".")
351352

352-
return c.Git(ctx, "clone", args...)
353+
return c.Git(ctx, "clone", sudo, args...)
353354
}
354355

355356
// UpdateRemote performs a git fetch on the upstream remote URI
@@ -361,11 +362,11 @@ func (c *Client) UpdateRemote(ctx context.Context) (err error) {
361362

362363
// fetch upstream
363364
if c.UpstreamRemoteURI != "" {
364-
if err := c.Git(ctx, "remote", "add", "upstream", c.UpstreamRemoteURI); err != nil {
365+
if err := c.Git(ctx, "remote", true, "add", "upstream", c.UpstreamRemoteURI); err != nil {
365366
return err
366367
}
367368
// fetch
368-
if err := c.Git(ctx, "fetch", "upstream"); err != nil {
369+
if err := c.Git(ctx, "fetch", true, "upstream"); err != nil {
369370
return err
370371
}
371372
}
@@ -381,7 +382,7 @@ func (c *Client) UpdateSubmodules(ctx context.Context) (err error) {
381382

382383
// checkout submodules
383384
// git submodule update --init --recursive
384-
if err := c.Git(ctx, "submodule", "update", "--init", "--recursive"); err != nil {
385+
if err := c.Git(ctx, "submodule", true, "update", "--init", "--recursive"); err != nil {
385386
return err
386387
}
387388
return nil

components/content-service/pkg/git/git_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestGitStatus(t *testing.T) {
3030
{
3131
"no commits",
3232
func(ctx context.Context, c *Client) error {
33-
if err := c.Git(ctx, "init"); err != nil {
33+
if err := c.Git(ctx, "init", false); err != nil {
3434
return err
3535
}
3636
return nil
@@ -111,7 +111,7 @@ func TestGitStatus(t *testing.T) {
111111
if err := os.WriteFile(filepath.Join(c.Location, "first-file"), []byte("foobar"), 0755); err != nil {
112112
return err
113113
}
114-
if err := c.Git(ctx, "commit", "-a", "-m", "foo"); err != nil {
114+
if err := c.Git(ctx, "commit", false, "-a", "-m", "foo"); err != nil {
115115
return err
116116
}
117117
return nil
@@ -132,13 +132,13 @@ func TestGitStatus(t *testing.T) {
132132
if err := initFromRemote(ctx, c); err != nil {
133133
return err
134134
}
135-
if err := c.Git(ctx, "checkout", "-b", "otherbranch"); err != nil {
135+
if err := c.Git(ctx, "checkout", false, "-b", "otherbranch"); err != nil {
136136
return err
137137
}
138138
if err := os.WriteFile(filepath.Join(c.Location, "first-file"), []byte("foobar"), 0755); err != nil {
139139
return err
140140
}
141-
if err := c.Git(ctx, "commit", "-a", "-m", "foo"); err != nil {
141+
if err := c.Git(ctx, "commit", false, "-a", "-m", "foo"); err != nil {
142142
return err
143143
}
144144
return nil
@@ -239,7 +239,7 @@ func TestGitStatusFromFiles(t *testing.T) {
239239
{
240240
"no commits",
241241
func(ctx context.Context, c *Client) error {
242-
if err := c.Git(ctx, "init"); err != nil {
242+
if err := c.Git(ctx, "init", false); err != nil {
243243
return err
244244
}
245245
return nil
@@ -320,7 +320,7 @@ func TestGitStatusFromFiles(t *testing.T) {
320320
if err := os.WriteFile(filepath.Join(c.Location, "first-file"), []byte("foobar"), 0755); err != nil {
321321
return err
322322
}
323-
if err := c.Git(ctx, "commit", "-a", "-m", "foo"); err != nil {
323+
if err := c.Git(ctx, "commit", false, "-a", "-m", "foo"); err != nil {
324324
return err
325325
}
326326
return nil
@@ -341,13 +341,13 @@ func TestGitStatusFromFiles(t *testing.T) {
341341
if err := initFromRemote(ctx, c); err != nil {
342342
return err
343343
}
344-
if err := c.Git(ctx, "checkout", "-b", "otherbranch"); err != nil {
344+
if err := c.Git(ctx, "checkout", false, "-b", "otherbranch"); err != nil {
345345
return err
346346
}
347347
if err := os.WriteFile(filepath.Join(c.Location, "first-file"), []byte("foobar"), 0755); err != nil {
348348
return err
349349
}
350-
if err := c.Git(ctx, "commit", "-a", "-m", "foo"); err != nil {
350+
if err := c.Git(ctx, "commit", false, "-a", "-m", "foo"); err != nil {
351351
return err
352352
}
353353
return nil
@@ -499,33 +499,33 @@ func initFromRemote(ctx context.Context, c *Client) error {
499499
if err != nil {
500500
return xerrors.Errorf("cannot add remote: %w", err)
501501
}
502-
if err := remote.Git(ctx, "init"); err != nil {
502+
if err := remote.Git(ctx, "init", false); err != nil {
503503
return err
504504
}
505-
if err := remote.Git(ctx, "config", "--local", "user.email", "[email protected]"); err != nil {
505+
if err := remote.Git(ctx, "config", false, "--local", "user.email", "[email protected]"); err != nil {
506506
return err
507507
}
508-
if err := remote.Git(ctx, "config", "--local", "user.name", "foo bar"); err != nil {
508+
if err := remote.Git(ctx, "config", false, "--local", "user.name", "foo bar"); err != nil {
509509
return err
510510
}
511511
if err := os.WriteFile(filepath.Join(remote.Location, "first-file"), []byte{}, 0755); err != nil {
512512
return err
513513
}
514-
if err := remote.Git(ctx, "add", "first-file"); err != nil {
514+
if err := remote.Git(ctx, "add", false, "first-file"); err != nil {
515515
return err
516516
}
517-
if err := remote.Git(ctx, "commit", "-m", "foo"); err != nil {
517+
if err := remote.Git(ctx, "commit", false, "-m", "foo"); err != nil {
518518
return err
519519
}
520520

521521
c.RemoteURI = remote.Location
522-
if err := c.Clone(ctx); err != nil {
522+
if err := c.Clone(ctx, false); err != nil {
523523
return err
524524
}
525-
if err := c.Git(ctx, "config", "--local", "user.email", "[email protected]"); err != nil {
525+
if err := c.Git(ctx, "config", false, "--local", "user.email", "[email protected]"); err != nil {
526526
return err
527527
}
528-
if err := c.Git(ctx, "config", "--local", "user.name", "foo bar"); err != nil {
528+
if err := c.Git(ctx, "config", false, "--local", "user.name", "foo bar"); err != nil {
529529
return err
530530
}
531531

components/content-service/pkg/initializer/git.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (ws *GitInitializer) Run(ctx context.Context, mappings []archive.IDMapping)
7777
}
7878

7979
log.WithField("stage", "init").WithField("location", ws.Location).Debug("Running git clone on workspace")
80-
err = ws.Clone(ctx)
80+
err = ws.Clone(ctx, true)
8181
if err != nil {
8282
if strings.Contains(err.Error(), "Access denied") {
8383
err = &backoff.PermanentError{
@@ -88,12 +88,12 @@ func (ws *GitInitializer) Run(ctx context.Context, mappings []archive.IDMapping)
8888
return err
8989
}
9090

91-
err = ws.Git(ctx, "config", "--replace-all", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
91+
err = ws.Git(ctx, "config", true, "--replace-all", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
9292
if err != nil {
9393
log.WithError(err).WithField("location", ws.Location).Error("cannot configure fecth behavior")
9494
}
9595

96-
err = ws.Git(ctx, "config", "--replace-all", "checkout.defaultRemote", "origin")
96+
err = ws.Git(ctx, "config", true, "--replace-all", "checkout.defaultRemote", "origin")
9797
if err != nil {
9898
log.WithError(err).WithField("location", ws.Location).Error("cannot configure checkout defaultRemote")
9999
}
@@ -193,30 +193,30 @@ func (ws *GitInitializer) realizeCloneTarget(ctx context.Context) (err error) {
193193
//
194194
// We don't recurse submodules because callers realizeCloneTarget() are expected to update submodules explicitly,
195195
// and deal with any error appropriately (i.e. emit a warning rather than fail).
196-
if err := ws.Git(ctx, "fetch", "--depth=1", "origin", "--recurse-submodules=no", ws.CloneTarget); err != nil {
196+
if err := ws.Git(ctx, "fetch", true, "--depth=1", "origin", "--recurse-submodules=no", ws.CloneTarget); err != nil {
197197
log.WithError(err).WithField("remoteURI", ws.RemoteURI).WithField("branch", ws.CloneTarget).Error("Cannot fetch remote branch")
198198
return err
199199
}
200200

201-
if err := ws.Git(ctx, "checkout", "-B", branchName, "origin/"+ws.CloneTarget); err != nil {
201+
if err := ws.Git(ctx, "checkout", true, "-B", branchName, "origin/"+ws.CloneTarget); err != nil {
202202
log.WithError(err).WithField("remoteURI", ws.RemoteURI).WithField("branch", branchName).Error("Cannot fetch remote branch")
203203
return err
204204
}
205205
case LocalBranch:
206206
// checkout local branch based on remote HEAD
207-
if err := ws.Git(ctx, "checkout", "-B", ws.CloneTarget, "origin/HEAD", "--no-track"); err != nil {
207+
if err := ws.Git(ctx, "checkout", true, "-B", ws.CloneTarget, "origin/HEAD", "--no-track"); err != nil {
208208
return err
209209
}
210210
case RemoteCommit:
211211
// We did a shallow clone before, hence need to fetch the commit we are about to check out.
212212
// Because we don't want to make the "git fetch" mechanism in supervisor more complicated,
213213
// we'll just fetch the 20 commits right away.
214-
if err := ws.Git(ctx, "fetch", "origin", ws.CloneTarget, "--depth=20"); err != nil {
214+
if err := ws.Git(ctx, "fetch", true, "origin", ws.CloneTarget, "--depth=20"); err != nil {
215215
return err
216216
}
217217

218218
// checkout specific commit
219-
if err := ws.Git(ctx, "checkout", ws.CloneTarget); err != nil {
219+
if err := ws.Git(ctx, "checkout", true, ws.CloneTarget); err != nil {
220220
return err
221221
}
222222
default:

components/content-service/pkg/initializer/initializer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ func EnsureCleanDotGitpodDirectory(ctx context.Context, wspath string) error {
453453
Location: wspath,
454454
}
455455
mv = func(src, dst string) error {
456-
return c.Git(ctx, "mv", src, dst)
456+
return c.Git(ctx, "mv", true, src, dst)
457457
}
458458
} else {
459459
mv = os.Rename

components/content-service/pkg/initializer/prebuild.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@ func runGitInit(ctx context.Context, gInit *GitInitializer) (err error) {
143143
// If any of these cleanup operations fail that's no reason to fail ws initialization.
144144
// It just results in a slightly degraded state.
145145
if didStash {
146-
err = gInit.Git(ctx, "stash", "pop")
146+
err = gInit.Git(ctx, "stash", true, "pop")
147147
if err != nil {
148148
// If restoring the stashed changes produces merge conflicts on the new Git ref, simply
149149
// throw them away (they'll remain in the stash, but are likely outdated anyway).
150-
_ = gInit.Git(ctx, "reset", "--hard")
150+
_ = gInit.Git(ctx, "reset", true, "--hard")
151151
}
152152
}
153153

0 commit comments

Comments
 (0)