1package git
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7
8 gcfg "github.com/go-git/go-git/v5/plumbing/format/config"
9)
10
11// Config returns the repository Git configuration.
12func (r *Repository) Config() (*gcfg.Config, error) {
13 cp := filepath.Join(r.Path, "config")
14 f, err := os.Open(cp)
15 if err != nil {
16 return nil, fmt.Errorf("failed to open git config file: %w", err)
17 }
18
19 defer f.Close()
20 d := gcfg.NewDecoder(f)
21 cfg := gcfg.New()
22 if err := d.Decode(cfg); err != nil {
23 return nil, fmt.Errorf("failed to decode git config: %w", err)
24 }
25
26 return cfg, nil
27}
28
29// SetConfig sets the repository Git configuration.
30func (r *Repository) SetConfig(cfg *gcfg.Config) error {
31 cp := filepath.Join(r.Path, "config")
32 f, err := os.Create(cp)
33 if err != nil {
34 return fmt.Errorf("failed to create git config file: %w", err)
35 }
36
37 defer f.Close()
38 e := gcfg.NewEncoder(f)
39 if err := e.Encode(cfg); err != nil {
40 return fmt.Errorf("failed to encode git config: %w", err)
41 }
42 return nil
43}