Skip to content

Commit 22dcfe0

Browse files
committed
feat: add RestoreGit to restore git identity from snapshot
1 parent 262e633 commit 22dcfe0

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

internal/snapshot/capture.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package snapshot
22

33
import (
4+
"fmt"
45
"os"
56
"os/exec"
67
"path/filepath"
@@ -9,6 +10,7 @@ import (
910
"time"
1011

1112
"github.com/openbootdotdev/openboot/internal/macos"
13+
"github.com/openbootdotdev/openboot/internal/system"
1214
)
1315

1416
// Capture orchestrates a full environment snapshot.
@@ -406,6 +408,29 @@ func CaptureGit() (*GitSnapshot, error) {
406408
return snap, nil
407409
}
408410

411+
// RestoreGit writes global git user.name and user.email from a snapshot, skipping values already configured.
412+
func RestoreGit(git GitSnapshot) error {
413+
existingName, existingEmail := system.GetExistingGitConfig()
414+
415+
if git.UserName == "" && git.UserEmail == "" {
416+
return nil
417+
}
418+
419+
if existingName == "" && git.UserName != "" {
420+
if err := system.RunCommand("git", "config", "--global", "user.name", git.UserName); err != nil {
421+
return fmt.Errorf("failed to restore git user.name: %w", err)
422+
}
423+
}
424+
425+
if existingEmail == "" && git.UserEmail != "" {
426+
if err := system.RunCommand("git", "config", "--global", "user.email", git.UserEmail); err != nil {
427+
return fmt.Errorf("failed to restore git user.email: %w", err)
428+
}
429+
}
430+
431+
return nil
432+
}
433+
409434
var devToolCommands = []struct {
410435
name string
411436
args []string

0 commit comments

Comments
 (0)