This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathconfig.go
More file actions
70 lines (60 loc) · 2.33 KB
/
config.go
File metadata and controls
70 lines (60 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package git
import (
"context"
"math/rand"
"os/exec"
"strconv"
"strings"
"syscall"
"time"
"github.com/sourcegraph/log"
"github.com/sourcegraph/sourcegraph/cmd/gitserver/internal/common"
"github.com/sourcegraph/sourcegraph/cmd/gitserver/internal/executil"
"github.com/sourcegraph/sourcegraph/cmd/gitserver/internal/gitserverfs"
"github.com/sourcegraph/sourcegraph/internal/wrexec"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
func ConfigGet(rcf *wrexec.RecordingCommandFactory, reposDir string, dir common.GitDir, key string) (string, error) {
cmd := exec.Command("git", "config", "--get", key)
dir.Set(cmd)
wrappedCmd := rcf.WrapWithRepoName(context.Background(), log.NoOp(), gitserverfs.RepoNameFromDir(reposDir, dir), cmd)
out, err := wrappedCmd.Output()
if err != nil {
// Exit code 1 means the key is not set.
var e *exec.ExitError
if errors.As(err, &e) && e.Sys().(syscall.WaitStatus).ExitStatus() == 1 {
return "", nil
}
return "", errors.Wrapf(executil.WrapCmdError(cmd, err), "failed to get git config %s", key)
}
return strings.TrimSpace(string(out)), nil
}
func ConfigSet(rcf *wrexec.RecordingCommandFactory, reposDir string, dir common.GitDir, key, value string) error {
cmd := exec.Command("git", "config", key, value)
dir.Set(cmd)
wrappedCmd := rcf.WrapWithRepoName(context.Background(), log.NoOp(), gitserverfs.RepoNameFromDir(reposDir, dir), cmd)
err := wrappedCmd.Run()
if err != nil {
return errors.Wrapf(executil.WrapCmdError(cmd, err), "failed to set git config %s", key)
}
return nil
}
func ConfigUnset(rcf *wrexec.RecordingCommandFactory, reposDir string, dir common.GitDir, key string) error {
rand.Seed(time.Now().UnixNano())
// Generate a random number between 0 and 10
randomNum := rand.Intn(10) + 1
randomNumString := strconv.Itoa(randomNum)
cmd := exec.Command("git", "config", "--unset-all", key, randomNumString)
dir.Set(cmd)
wrappedCmd := rcf.WrapWithRepoName(context.Background(), log.NoOp(), gitserverfs.RepoNameFromDir(reposDir, dir), cmd)
out, err := wrappedCmd.CombinedOutput()
if err != nil {
// Exit code 5 means the key is not set.
var e *exec.ExitError
if errors.As(err, &e) && e.Sys().(syscall.WaitStatus).ExitStatus() == 5 {
return nil
}
return errors.Wrapf(executil.WrapCmdError(cmd, err), "failed to unset git config %s: %s", key, string(out))
}
return nil
}