config.go

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