Skip to content

Commit aba04a0

Browse files
sagor999roboquat
authored andcommitted
run git command as gitpod user when using pvc
1 parent cebf1e0 commit aba04a0

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

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

Lines changed: 9 additions & 1 deletion
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"
@@ -197,7 +200,12 @@ func (c *Client) GitWithOutput(ctx context.Context, ignoreErr *string, subcomman
197200

198201
span.LogKV("args", fullArgs)
199202

200-
cmd := exec.Command("git", fullArgs...)
203+
cmdName := "git"
204+
if c.RunAsGitpodUser {
205+
cmdName = "sudo"
206+
fullArgs = append([]string{"-u", "gitpod", "git"}, fullArgs...)
207+
}
208+
cmd := exec.Command(cmdName, fullArgs...)
201209
cmd.Dir = c.Location
202210
cmd.Env = env
203211

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

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

79+
// make sure that folder itself is owned by gitpod user prior to doing git clone
80+
// this is needed as otherwise git clone will fail if the folder is owned by root
81+
if ws.RunAsGitpodUser {
82+
args := []string{"gitpod", ws.Location}
83+
cmd := exec.Command("chown", args...)
84+
res, cerr := cmd.CombinedOutput()
85+
if cerr != nil && !process.IsNotChildProcess(cerr) {
86+
err = git.OpFailedError{
87+
Args: args,
88+
ExecErr: cerr,
89+
Output: string(res),
90+
Subcommand: "chown",
91+
}
92+
return err
93+
}
94+
}
95+
7996
log.WithField("stage", "init").WithField("location", ws.Location).Debug("Running git clone on workspace")
8097
err = ws.Clone(ctx)
8198
if err != nil {

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

Lines changed: 2 additions & 1 deletion
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

0 commit comments

Comments
 (0)