Skip to content

Commit 95faa2d

Browse files
committed
wip
1 parent 3d952f3 commit 95faa2d

File tree

6 files changed

+68
-49
lines changed

6 files changed

+68
-49
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ type Client struct {
9292

9393
// UpstreamCloneURI is the fork upstream of a repository
9494
UpstreamRemoteURI string
95+
96+
// if true will run git command as gitpod user (should be executed as root that has access to sudo in this case)
97+
RunAsGitpodUser bool
9598
}
9699

97100
// Status describes the status of a Git repo/working copy akin to "git status"
@@ -147,7 +150,7 @@ func (e OpFailedError) Error() string {
147150

148151
// GitWithOutput starts git and returns the stdout of the process. This function returns once git is started,
149152
// not after it finishd. Once the returned reader returned io.EOF, the command is finished.
150-
func (c *Client) GitWithOutput(ctx context.Context, ignoreErr *string, subcommand string, sudo bool, args ...string) (out []byte, err error) {
153+
func (c *Client) GitWithOutput(ctx context.Context, ignoreErr *string, subcommand string, args ...string) (out []byte, err error) {
151154
//nolint:staticcheck,ineffassign
152155
span, ctx := opentracing.StartSpanFromContext(ctx, fmt.Sprintf("git.%s", subcommand))
153156
defer func() {
@@ -198,7 +201,7 @@ func (c *Client) GitWithOutput(ctx context.Context, ignoreErr *string, subcomman
198201
span.LogKV("args", fullArgs)
199202

200203
cmdName := "git"
201-
if sudo {
204+
if c.RunAsGitpodUser {
202205
cmdName = "sudo"
203206
fullArgs = append([]string{"-u", "gitpod", "git"}, fullArgs...)
204207
}
@@ -225,8 +228,8 @@ func (c *Client) GitWithOutput(ctx context.Context, ignoreErr *string, subcomman
225228
}
226229

227230
// Git executes git using the client configuration
228-
func (c *Client) Git(ctx context.Context, subcommand string, sudo bool, args ...string) (err error) {
229-
_, err = c.GitWithOutput(ctx, nil, subcommand, sudo, args...)
231+
func (c *Client) Git(ctx context.Context, subcommand string, args ...string) (err error) {
232+
_, err = c.GitWithOutput(ctx, nil, subcommand, args...)
230233
if err != nil {
231234
return err
232235
}
@@ -283,7 +286,7 @@ func GitStatusFromFiles(ctx context.Context, loc string) (res *Status, err error
283286

284287
// Status runs git status
285288
func (c *Client) Status(ctx context.Context) (res *Status, err error) {
286-
gitout, err := c.GitWithOutput(ctx, nil, "status", false, "--porcelain=v2", "--branch", "-uall")
289+
gitout, err := c.GitWithOutput(ctx, nil, "status", "--porcelain=v2", "--branch", "-uall")
287290
if err != nil {
288291
return nil, err
289292
}
@@ -293,7 +296,7 @@ func (c *Client) Status(ctx context.Context) (res *Status, err error) {
293296
}
294297

295298
unpushedCommits := make([]string, 0)
296-
gitout, err = c.GitWithOutput(ctx, &errNoCommitsYet, "log", false, "--pretty=%h: %s", "--branches", "--not", "--remotes")
299+
gitout, err = c.GitWithOutput(ctx, &errNoCommitsYet, "log", "--pretty=%h: %s", "--branches", "--not", "--remotes")
297300
if err != nil && !strings.Contains(err.Error(), errNoCommitsYet) {
298301
return nil, err
299302
}
@@ -314,7 +317,7 @@ func (c *Client) Status(ctx context.Context) (res *Status, err error) {
314317
}
315318

316319
latestCommit := ""
317-
gitout, err = c.GitWithOutput(ctx, &errNoCommitsYet, "log", false, "--pretty=%H", "-n", "1")
320+
gitout, err = c.GitWithOutput(ctx, &errNoCommitsYet, "log", "--pretty=%H", "-n", "1")
318321
if err != nil && !strings.Contains(err.Error(), errNoCommitsYet) {
319322
return nil, err
320323
}
@@ -330,7 +333,7 @@ func (c *Client) Status(ctx context.Context) (res *Status, err error) {
330333
}
331334

332335
// Clone runs git clone
333-
func (c *Client) Clone(ctx context.Context, sudo bool) (err error) {
336+
func (c *Client) Clone(ctx context.Context) (err error) {
334337
err = os.MkdirAll(c.Location, 0775)
335338
if err != nil {
336339
log.WithError(err).Error("cannot create clone location")
@@ -351,7 +354,7 @@ func (c *Client) Clone(ctx context.Context, sudo bool) (err error) {
351354

352355
args = append(args, ".")
353356

354-
return c.Git(ctx, "clone", sudo, args...)
357+
return c.Git(ctx, "clone", args...)
355358
}
356359

357360
// UpdateRemote performs a git fetch on the upstream remote URI
@@ -363,11 +366,11 @@ func (c *Client) UpdateRemote(ctx context.Context) (err error) {
363366

364367
// fetch upstream
365368
if c.UpstreamRemoteURI != "" {
366-
if err := c.Git(ctx, "remote", true, "add", "upstream", c.UpstreamRemoteURI); err != nil {
369+
if err := c.Git(ctx, "remote", "add", "upstream", c.UpstreamRemoteURI); err != nil {
367370
return err
368371
}
369372
// fetch
370-
if err := c.Git(ctx, "fetch", true, "upstream"); err != nil {
373+
if err := c.Git(ctx, "fetch", "upstream"); err != nil {
371374
return err
372375
}
373376
}
@@ -383,7 +386,7 @@ func (c *Client) UpdateSubmodules(ctx context.Context) (err error) {
383386

384387
// checkout submodules
385388
// git submodule update --init --recursive
386-
if err := c.Git(ctx, "submodule", true, "update", "--init", "--recursive"); err != nil {
389+
if err := c.Git(ctx, "submodule", "update", "--init", "--recursive"); err != nil {
387390
return err
388391
}
389392
return nil

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

Lines changed: 19 additions & 19 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", false); err != nil {
33+
if err := c.Git(ctx, "init"); 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", false, "-a", "-m", "foo"); err != nil {
114+
if err := c.Git(ctx, "commit", "-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", false, "-b", "otherbranch"); err != nil {
135+
if err := c.Git(ctx, "checkout", "-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", false, "-a", "-m", "foo"); err != nil {
141+
if err := c.Git(ctx, "commit", "-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", false); err != nil {
242+
if err := c.Git(ctx, "init"); 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", false, "-a", "-m", "foo"); err != nil {
323+
if err := c.Git(ctx, "commit", "-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", false, "-b", "otherbranch"); err != nil {
344+
if err := c.Git(ctx, "checkout", "-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", false, "-a", "-m", "foo"); err != nil {
350+
if err := c.Git(ctx, "commit", "-a", "-m", "foo"); err != nil {
351351
return err
352352
}
353353
return nil
@@ -416,7 +416,7 @@ func TestGitStatusFromFiles(t *testing.T) {
416416
return
417417
}
418418

419-
gitout, err := client.GitWithOutput(ctx, nil, "status", false, "--porcelain=v2", "--branch", "-uall")
419+
gitout, err := client.GitWithOutput(ctx, nil, "status", "--porcelain=v2", "--branch", "-uall")
420420
if err != nil {
421421
t.Errorf("error calling GitWithOutput: %v", err)
422422
return
@@ -426,7 +426,7 @@ func TestGitStatusFromFiles(t *testing.T) {
426426
return
427427
}
428428

429-
gitout, err = client.GitWithOutput(ctx, &errNoCommitsYet, "log", false, "--pretty=%h: %s", "--branches", "--not", "--remotes")
429+
gitout, err = client.GitWithOutput(ctx, &errNoCommitsYet, "log", "--pretty=%h: %s", "--branches", "--not", "--remotes")
430430
if err != nil {
431431
t.Errorf("error calling GitWithOutput: %v", err)
432432
return
@@ -436,7 +436,7 @@ func TestGitStatusFromFiles(t *testing.T) {
436436
return
437437
}
438438

439-
gitout, err = client.GitWithOutput(ctx, &errNoCommitsYet, "log", false, "--pretty=%H", "-n", "1")
439+
gitout, err = client.GitWithOutput(ctx, &errNoCommitsYet, "log", "--pretty=%H", "-n", "1")
440440
if err != nil && !strings.Contains(err.Error(), "fatal: your current branch 'master' does not have any commits yet") {
441441
t.Errorf("error calling GitWithOutput: %v", err)
442442
return
@@ -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", false); err != nil {
502+
if err := remote.Git(ctx, "init"); err != nil {
503503
return err
504504
}
505-
if err := remote.Git(ctx, "config", false, "--local", "user.email", "[email protected]"); err != nil {
505+
if err := remote.Git(ctx, "config", "--local", "user.email", "[email protected]"); err != nil {
506506
return err
507507
}
508-
if err := remote.Git(ctx, "config", false, "--local", "user.name", "foo bar"); err != nil {
508+
if err := remote.Git(ctx, "config", "--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", false, "first-file"); err != nil {
514+
if err := remote.Git(ctx, "add", "first-file"); err != nil {
515515
return err
516516
}
517-
if err := remote.Git(ctx, "commit", false, "-m", "foo"); err != nil {
517+
if err := remote.Git(ctx, "commit", "-m", "foo"); err != nil {
518518
return err
519519
}
520520

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

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

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

79-
args := []string{"-R", "-L", "gitpod", ws.Location}
79+
// make sure that folder itself is owned by gitpod user prior to doing git clone
80+
args := []string{"gitpod", ws.Location}
8081
cmd := exec.Command("chown", args...)
8182
res, cerr := cmd.CombinedOutput()
8283
if cerr != nil && !process.IsNotChildProcess(cerr) {
@@ -89,8 +90,22 @@ func (ws *GitInitializer) Run(ctx context.Context, mappings []archive.IDMapping)
8990
return err
9091
}
9192

93+
// test
94+
args = []string{"gitpod", "/workspace"}
95+
cmd = exec.Command("chown", args...)
96+
res, cerr = cmd.CombinedOutput()
97+
if cerr != nil && !process.IsNotChildProcess(cerr) {
98+
err = git.OpFailedError{
99+
Args: args,
100+
ExecErr: cerr,
101+
Output: string(res),
102+
Subcommand: "chown",
103+
}
104+
return err
105+
}
106+
92107
log.WithField("stage", "init").WithField("location", ws.Location).Debug("Running git clone on workspace")
93-
err = ws.Clone(ctx, true)
108+
err = ws.Clone(ctx)
94109
if err != nil {
95110
if strings.Contains(err.Error(), "Access denied") {
96111
err = &backoff.PermanentError{
@@ -101,12 +116,12 @@ func (ws *GitInitializer) Run(ctx context.Context, mappings []archive.IDMapping)
101116
return err
102117
}
103118

104-
err = ws.Git(ctx, "config", true, "--replace-all", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
119+
err = ws.Git(ctx, "config", "--replace-all", "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*")
105120
if err != nil {
106121
log.WithError(err).WithField("location", ws.Location).Error("cannot configure fecth behavior")
107122
}
108123

109-
err = ws.Git(ctx, "config", true, "--replace-all", "checkout.defaultRemote", "origin")
124+
err = ws.Git(ctx, "config", "--replace-all", "checkout.defaultRemote", "origin")
110125
if err != nil {
111126
log.WithError(err).WithField("location", ws.Location).Error("cannot configure checkout defaultRemote")
112127
}
@@ -187,14 +202,14 @@ func (ws *GitInitializer) realizeCloneTarget(ctx context.Context) (err error) {
187202
switch ws.TargetMode {
188203
case RemoteBranch:
189204
// confirm the value of the default branch name using rev-parse
190-
gitout, _ := ws.GitWithOutput(ctx, nil, "rev-parse", true, "--abbrev-ref", "origin/HEAD")
205+
gitout, _ := ws.GitWithOutput(ctx, nil, "rev-parse", "--abbrev-ref", "origin/HEAD")
191206
defaultBranch := strings.TrimSpace(strings.Replace(string(gitout), "origin/", "", -1))
192207

193208
branchName := ws.CloneTarget
194209

195210
// we already cloned the git repository but we need to check CloneTarget exists
196211
// to avoid calling fetch from a non-existing branch
197-
gitout, err := ws.GitWithOutput(ctx, nil, "ls-remote", true, "--exit-code", "origin", ws.CloneTarget)
212+
gitout, err := ws.GitWithOutput(ctx, nil, "ls-remote", "--exit-code", "origin", ws.CloneTarget)
198213
if err != nil || len(gitout) == 0 {
199214
log.WithField("remoteURI", ws.RemoteURI).WithField("branch", ws.CloneTarget).Warnf("Invalid default branch name. Changing to %v", defaultBranch)
200215
ws.CloneTarget = defaultBranch
@@ -206,35 +221,35 @@ func (ws *GitInitializer) realizeCloneTarget(ctx context.Context) (err error) {
206221
//
207222
// We don't recurse submodules because callers realizeCloneTarget() are expected to update submodules explicitly,
208223
// and deal with any error appropriately (i.e. emit a warning rather than fail).
209-
if err := ws.Git(ctx, "fetch", true, "--depth=1", "origin", "--recurse-submodules=no", ws.CloneTarget); err != nil {
224+
if err := ws.Git(ctx, "fetch", "--depth=1", "origin", "--recurse-submodules=no", ws.CloneTarget); err != nil {
210225
log.WithError(err).WithField("remoteURI", ws.RemoteURI).WithField("branch", ws.CloneTarget).Error("Cannot fetch remote branch")
211226
return err
212227
}
213228

214-
if err := ws.Git(ctx, "checkout", true, "-B", branchName, "origin/"+ws.CloneTarget); err != nil {
229+
if err := ws.Git(ctx, "checkout", "-B", branchName, "origin/"+ws.CloneTarget); err != nil {
215230
log.WithError(err).WithField("remoteURI", ws.RemoteURI).WithField("branch", branchName).Error("Cannot fetch remote branch")
216231
return err
217232
}
218233
case LocalBranch:
219234
// checkout local branch based on remote HEAD
220-
if err := ws.Git(ctx, "checkout", true, "-B", ws.CloneTarget, "origin/HEAD", "--no-track"); err != nil {
235+
if err := ws.Git(ctx, "checkout", "-B", ws.CloneTarget, "origin/HEAD", "--no-track"); err != nil {
221236
return err
222237
}
223238
case RemoteCommit:
224239
// We did a shallow clone before, hence need to fetch the commit we are about to check out.
225240
// Because we don't want to make the "git fetch" mechanism in supervisor more complicated,
226241
// we'll just fetch the 20 commits right away.
227-
if err := ws.Git(ctx, "fetch", true, "origin", ws.CloneTarget, "--depth=20"); err != nil {
242+
if err := ws.Git(ctx, "fetch", "origin", ws.CloneTarget, "--depth=20"); err != nil {
228243
return err
229244
}
230245

231246
// checkout specific commit
232-
if err := ws.Git(ctx, "checkout", true, ws.CloneTarget); err != nil {
247+
if err := ws.Git(ctx, "checkout", ws.CloneTarget); err != nil {
233248
return err
234249
}
235250
default:
236251
// update to remote HEAD
237-
if _, err := ws.GitWithOutput(ctx, nil, "reset", true, "--hard", "origin/HEAD"); err != nil {
252+
if _, err := ws.GitWithOutput(ctx, nil, "reset", "--hard", "origin/HEAD"); err != nil {
238253
var giterr git.OpFailedError
239254
if errors.As(err, &giterr) && strings.Contains(giterr.Output, "unknown revision or path not in the working tree") {
240255
// 'git reset --hard origin/HEAD' returns a non-zero exit code if origin does not have a single commit (empty repository).

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,11 @@ func newGitInitializer(ctx context.Context, loc string, req *csapi.GitInitialize
261261
Config: req.Config.CustomConfig,
262262
AuthMethod: authMethod,
263263
AuthProvider: authProvider,
264+
RunAsGitpodUser: forceGitpodUser,
264265
},
265266
TargetMode: targetMode,
266267
CloneTarget: req.CloneTaget,
267-
Chown: forceGitpodUser,
268+
Chown: false,
268269
}, nil
269270
}
270271

@@ -453,7 +454,7 @@ func EnsureCleanDotGitpodDirectory(ctx context.Context, wspath string) error {
453454
Location: wspath,
454455
}
455456
mv = func(src, dst string) error {
456-
return c.Git(ctx, "mv", true, src, dst)
457+
return c.Git(ctx, "mv", src, dst)
457458
}
458459
} else {
459460
mv = os.Rename

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func runGitInit(ctx context.Context, gInit *GitInitializer) (err error) {
116116
)
117117
defer tracing.FinishSpan(span, &err)
118118
if git.IsWorkingCopy(gInit.Location) {
119-
out, err := gInit.GitWithOutput(ctx, nil, "stash", true, "push", "--no-include-untracked")
119+
out, err := gInit.GitWithOutput(ctx, nil, "stash", "push", "--no-include-untracked")
120120
if err != nil {
121121
var giterr git.OpFailedError
122122
if errors.As(err, &giterr) && strings.Contains(giterr.Output, "You do not have the initial commit yet") {
@@ -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", true, "pop")
146+
err = gInit.Git(ctx, "stash", "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", true, "--hard")
150+
_ = gInit.Git(ctx, "reset", "--hard")
151151
}
152152
}
153153

0 commit comments

Comments
 (0)