Skip to content

Commit 4db2bde

Browse files
committed
improve AddOptionFormat to avoid low-level mistakes
1 parent 01d79d0 commit 4db2bde

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

modules/git/command.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ func (c *Command) AddOptionFormat(opt string, args ...any) *Command {
142142
c.brokenArgs = append(c.brokenArgs, opt)
143143
return c
144144
}
145-
145+
// a quick check to make sure the format string matches the number of arguments, to find low-level mistakes ASAP
146+
if strings.Count(strings.ReplaceAll(opt, "%%", ""), "%") != len(args) {
147+
c.brokenArgs = append(c.brokenArgs, opt)
148+
return c
149+
}
146150
s := fmt.Sprintf(opt, args...)
147151
c.args = append(c.args, s)
148152
return c

modules/git/repo_archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (repo *Repository) CreateArchive(ctx context.Context, format ArchiveType, t
5959
if usePrefix {
6060
cmd.AddOptionFormat("--prefix=%s", filepath.Base(strings.TrimSuffix(repo.Path, ".git"))+"/")
6161
}
62-
cmd.AddOptionFormat("--format=%s" + format.String())
62+
cmd.AddOptionFormat("--format=%s", format.String())
6363
cmd.AddDynamicArguments(commitID)
6464

6565
var stderr strings.Builder

modules/git/repo_commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (repo *Repository) searchCommits(id SHA1, opts SearchCommitsOptions) ([]*Co
123123
c.AddOptionFormat("--after=%s", opts.After)
124124
}
125125
if len(opts.Before) > 0 {
126-
c.AddOptionFormat("--before=%s" + opts.Before)
126+
c.AddOptionFormat("--before=%s", opts.Before)
127127
}
128128
}
129129

0 commit comments

Comments
 (0)