Skip to content

Commit

Permalink
use quoted regexp instead of git fixed-value (#20030)
Browse files Browse the repository at this point in the history
Backport #20029
  • Loading branch information
wxiaoguang authored Jun 19, 2022
1 parent a180d94 commit 8733f4b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -337,7 +338,7 @@ func configSetNonExist(key, value string) error {
}

func configAddNonExist(key, value string) error {
_, _, err := NewCommand(DefaultContext, "config", "--fixed-value", "--get", key, value).RunStdString(nil)
_, _, err := NewCommand(DefaultContext, "config", "--get", key, regexp.QuoteMeta(value)).RunStdString(nil)
if err == nil {
// already exist
return nil
Expand All @@ -357,7 +358,7 @@ func configUnsetAll(key, value string) error {
_, _, err := NewCommand(DefaultContext, "config", "--get", key).RunStdString(nil)
if err == nil {
// exist, need to remove
_, _, err = NewCommand(DefaultContext, "config", "--global", "--fixed-value", "--unset-all", key, value).RunStdString(nil)
_, _, err = NewCommand(DefaultContext, "config", "--global", "--unset-all", key, regexp.QuoteMeta(value)).RunStdString(nil)
if err != nil {
return fmt.Errorf("failed to unset git global config %s, err: %w", key, err)
}
Expand Down
6 changes: 6 additions & 0 deletions modules/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,10 @@ func TestGitConfig(t *testing.T) {

assert.NoError(t, configUnsetAll("test.key-b", "val-2b"))
assert.False(t, gitConfigContains("key-b = val-2b"))

assert.NoError(t, configSet("test.key-x", "*"))
assert.True(t, gitConfigContains("key-x = *"))
assert.NoError(t, configSetNonExist("test.key-x", "*"))
assert.NoError(t, configUnsetAll("test.key-x", "*"))
assert.False(t, gitConfigContains("key-x = *"))
}

0 comments on commit 8733f4b

Please sign in to comment.