Skip to content

[supervisor] Don't install git msg annotation hook in headless workspaces #20548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions components/supervisor/pkg/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ func Run(options ...RunOption) {
return
}

var (
ideReady = newIDEReadyState(&cfg.IDE)
desktopIdeReady *ideReadyState = nil

cstate = NewInMemoryContentState(cfg.RepoRoot)
gitpodService serverapi.APIInterface

notificationService = NewNotificationService()
)

endpoint, host, err := cfg.GitpodAPIEndpoint()
if err != nil {
log.WithError(err).Fatal("cannot find Gitpod API endpoint")
Expand All @@ -210,7 +220,7 @@ func Run(options ...RunOption) {
}
symlinkBinaries(cfg)

configureGit(cfg)
configureGit(cfg, cstate.ContentReady())

telemetry := analytics.NewFromEnvironment()
defer telemetry.Close()
Expand Down Expand Up @@ -260,16 +270,6 @@ func Run(options ...RunOption) {
internalPorts = append(internalPorts, debugProxyPort)
}

var (
ideReady = newIDEReadyState(&cfg.IDE)
desktopIdeReady *ideReadyState = nil

cstate = NewInMemoryContentState(cfg.RepoRoot)
gitpodService serverapi.APIInterface

notificationService = NewNotificationService()
)

if !opts.RunGP {
gitpodService = serverapi.NewServerApiService(ctx, &serverapi.ServiceConfig{
Host: host,
Expand Down Expand Up @@ -804,7 +804,7 @@ func symlinkBinaries(cfg *Config) {
}
}

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

if cfg.CommitAnnotationEnabled {
err := setupGitMessageHook(filepath.Join(cfg.RepoRoot, ".git", "hooks"))
if err != nil {
log.WithError(err).Error("cannot setup git message hook")
}
}

for _, s := range settings {
cmd := exec.Command("git", append([]string{"config", "--global"}, s...)...)
cmd = runAsGitpodUser(cmd)
Expand All @@ -835,6 +828,15 @@ func configureGit(cfg *Config) {
log.WithError(err).WithField("args", s).Warn("git config error")
}
}

go func() {
<-contentReady
if cfg.CommitAnnotationEnabled && !cfg.isHeadless() {
if err := setupGitMessageHook(filepath.Join(cfg.RepoRoot, ".git", "hooks")); err != nil {
log.WithError(err).Error("cannot setup git message hook")
}
}
}()
}

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

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

// Change ownership of both path and file to the gitpod user
if err := os.Chown(path, gitpodUID, gitpodGID); err != nil {
return err
}
if err := os.Chown(fn, gitpodUID, gitpodGID); err != nil {
return err
}

return nil
}

Expand Down
Loading