Skip to content

Commit 79d46f4

Browse files
[supervisor] Don't install git msg annotation hook in headless workspaces (#20548)
* [supervisor] Don't install git msg annotation hook in headless workspaces * chown hooks to gitpod user * Wait for content ready with git commit hooks * Move hook setup back inside `configureGit` * Clean up chowns
1 parent 58e1f55 commit 79d46f4

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

components/supervisor/pkg/supervisor/supervisor.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ func Run(options ...RunOption) {
189189
return
190190
}
191191

192+
var (
193+
ideReady = newIDEReadyState(&cfg.IDE)
194+
desktopIdeReady *ideReadyState = nil
195+
196+
cstate = NewInMemoryContentState(cfg.RepoRoot)
197+
gitpodService serverapi.APIInterface
198+
199+
notificationService = NewNotificationService()
200+
)
201+
192202
endpoint, host, err := cfg.GitpodAPIEndpoint()
193203
if err != nil {
194204
log.WithError(err).Fatal("cannot find Gitpod API endpoint")
@@ -210,7 +220,7 @@ func Run(options ...RunOption) {
210220
}
211221
symlinkBinaries(cfg)
212222

213-
configureGit(cfg)
223+
configureGit(cfg, cstate.ContentReady())
214224

215225
telemetry := analytics.NewFromEnvironment()
216226
defer telemetry.Close()
@@ -260,16 +270,6 @@ func Run(options ...RunOption) {
260270
internalPorts = append(internalPorts, debugProxyPort)
261271
}
262272

263-
var (
264-
ideReady = newIDEReadyState(&cfg.IDE)
265-
desktopIdeReady *ideReadyState = nil
266-
267-
cstate = NewInMemoryContentState(cfg.RepoRoot)
268-
gitpodService serverapi.APIInterface
269-
270-
notificationService = NewNotificationService()
271-
)
272-
273273
if !opts.RunGP {
274274
gitpodService = serverapi.NewServerApiService(ctx, &serverapi.ServiceConfig{
275275
Host: host,
@@ -804,7 +804,7 @@ func symlinkBinaries(cfg *Config) {
804804
}
805805
}
806806

807-
func configureGit(cfg *Config) {
807+
func configureGit(cfg *Config, contentReady <-chan struct{}) {
808808
settings := [][]string{
809809
{"push.default", "simple"},
810810
{"alias.lg", "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"},
@@ -818,13 +818,6 @@ func configureGit(cfg *Config) {
818818
settings = append(settings, []string{"user.email", cfg.GitEmail})
819819
}
820820

821-
if cfg.CommitAnnotationEnabled {
822-
err := setupGitMessageHook(filepath.Join(cfg.RepoRoot, ".git", "hooks"))
823-
if err != nil {
824-
log.WithError(err).Error("cannot setup git message hook")
825-
}
826-
}
827-
828821
for _, s := range settings {
829822
cmd := exec.Command("git", append([]string{"config", "--global"}, s...)...)
830823
cmd = runAsGitpodUser(cmd)
@@ -835,6 +828,15 @@ func configureGit(cfg *Config) {
835828
log.WithError(err).WithField("args", s).Warn("git config error")
836829
}
837830
}
831+
832+
go func() {
833+
<-contentReady
834+
if cfg.CommitAnnotationEnabled && !cfg.isHeadless() {
835+
if err := setupGitMessageHook(filepath.Join(cfg.RepoRoot, ".git", "hooks")); err != nil {
836+
log.WithError(err).Error("cannot setup git message hook")
837+
}
838+
}
839+
}()
838840
}
839841

840842
const hookContent = `#!/bin/sh
@@ -847,14 +849,22 @@ func setupGitMessageHook(path string) error {
847849
}
848850

849851
fn := filepath.Join(path, "prepare-commit-msg")
850-
// do not override existing hooks. Relevant for workspaces based off of prebuilds, which might already have a hook.
852+
// do not override existing hooks
851853
if _, err := os.Stat(fn); err == nil {
852854
return nil
853855
}
854856
if err := os.WriteFile(fn, []byte(hookContent), 0755); err != nil {
855857
return err
856858
}
857859

860+
// Change ownership of both path and file to the gitpod user
861+
if err := os.Chown(path, gitpodUID, gitpodGID); err != nil {
862+
return err
863+
}
864+
if err := os.Chown(fn, gitpodUID, gitpodGID); err != nil {
865+
return err
866+
}
867+
858868
return nil
859869
}
860870

0 commit comments

Comments
 (0)