Skip to content

Commit 0cdce52

Browse files
sagor999roboquat
authored andcommitted
[ws-daemon] update git safe directory at init workspace phase
1 parent 3359591 commit 0cdce52

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

components/ws-daemon/pkg/content/service.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ func (s *WorkspaceService) InitWorkspace(ctx context.Context, req *api.InitWorks
257257
}
258258
}
259259

260+
err = workspace.UpdateGitSafeDirectory(ctx)
261+
if err != nil {
262+
log.WithError(err).WithField("workspaceId", req.Id).Warn("cannot update git safe directory")
263+
}
264+
260265
// Tell the world we're done
261266
err = workspace.MarkInitDone(ctx)
262267
if err != nil {

components/ws-daemon/pkg/internal/session/workspace.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,28 @@ func (s *Workspace) SetGitStatus(status *csapi.GitStatus) error {
257257
return s.persist()
258258
}
259259

260+
func (s *Workspace) UpdateGitSafeDirectory(ctx context.Context) (err error) {
261+
loc := s.Location
262+
if loc == "" {
263+
log.WithField("loc", loc).WithFields(s.OWI()).Debug("not updating Git safe directory of FWB workspace")
264+
return nil
265+
}
266+
267+
loc = filepath.Join(loc, s.CheckoutLocation)
268+
if !git.IsWorkingCopy(loc) {
269+
log.WithField("loc", loc).WithField("checkout location", s.CheckoutLocation).WithFields(s.OWI()).Warn("did not find a Git working copy - not updating safe directory")
270+
return nil
271+
}
272+
273+
c := git.Client{Location: loc}
274+
275+
err = c.Git(ctx, "config", "--global", "--add", "safe.directory", loc)
276+
if err != nil {
277+
log.WithError(err).WithFields(s.OWI()).Warn("cannot add safe directory into git global config")
278+
}
279+
return err
280+
}
281+
260282
// UpdateGitStatus attempts to update the LastGitStatus from the workspace's local working copy.
261283
func (s *Workspace) UpdateGitStatus(ctx context.Context) (res *csapi.GitStatus, err error) {
262284
loc := s.Location
@@ -281,12 +303,6 @@ func (s *Workspace) UpdateGitStatus(ctx context.Context) (res *csapi.GitStatus,
281303

282304
c := git.Client{Location: loc}
283305

284-
err = c.Git(ctx, "config", "--global", "--add", "safe.directory", loc)
285-
if err != nil {
286-
log.WithError(err).WithFields(s.OWI()).Warn("cannot persist latest Git status")
287-
err = nil
288-
}
289-
290306
stat, err := c.Status(ctx)
291307
if err != nil {
292308
return nil, err

0 commit comments

Comments
 (0)