Skip to content

Commit 31470b6

Browse files
authored
feat: add option to configure number of context lines in diffs (#22)
- Add a `diff_unified` option to generate diffs with a specific number of lines of context. - Modify `git.New()` to accept options to configure the `diff_unified` value. - Add a new file `git/options.go` that defines an `Option` interface and a `WithDiffUnified` function to set the `diff_unified` value. fix #21 fix #6
1 parent 52202da commit 31470b6

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

cmd/commit.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ var (
1919
commitLang string
2020
commitModel string
2121

22-
// disbale auot commit in Hook mode
23-
preview bool
22+
preview bool
23+
diffUnified int
2424
)
2525

2626
func init() {
2727
commitCmd.PersistentFlags().StringP("file", "f", ".git/COMMIT_EDITMSG", "commit message file")
2828
commitCmd.PersistentFlags().BoolVar(&preview, "preview", false, "preview commit message")
29+
commitCmd.PersistentFlags().IntVar(&diffUnified, "diff_unified", 3, "generate diffs with <n> lines of context, default is 3")
2930
commitCmd.PersistentFlags().StringVar(&commitModel, "model", "gpt-3.5-turbo", "select openai model")
3031
commitCmd.PersistentFlags().StringVar(&commitLang, "lang", "en", "summarizing language uses English by default")
3132
_ = viper.BindPFlag("output.file", commitCmd.PersistentFlags().Lookup("file"))
@@ -42,7 +43,13 @@ var commitCmd = &cobra.Command{
4243
return errors.New("To use CodeGPT, you must have git on your PATH")
4344
}
4445

45-
g := git.New()
46+
if diffUnified != 3 {
47+
viper.Set("git.diff_unified", diffUnified)
48+
}
49+
50+
g := git.New(
51+
git.WithDiffUnified(viper.GetInt("git.diff_unified")),
52+
)
4653
diff, err := g.DiffFiles()
4754
if err != nil {
4855
return err

cmd/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ func init() {
1818
configCmd.PersistentFlags().StringP("lang", "l", "en", "summarizing language uses English by default")
1919
configCmd.PersistentFlags().StringP("org_id", "o", "", "openai requesting organization")
2020
configCmd.PersistentFlags().StringP("proxy", "", "", "http proxy")
21+
configCmd.PersistentFlags().IntP("diff_unified", "", 3, "generate diffs with <n> lines of context, default is 3")
22+
2123
_ = viper.BindPFlag("openai.org_id", configCmd.PersistentFlags().Lookup("org_id"))
2224
_ = viper.BindPFlag("openai.api_key", configCmd.PersistentFlags().Lookup("api_key"))
2325
_ = viper.BindPFlag("openai.model", configCmd.PersistentFlags().Lookup("model"))
2426
_ = viper.BindPFlag("openai.proxy", configCmd.PersistentFlags().Lookup("proxy"))
2527
_ = viper.BindPFlag("output.lang", configCmd.PersistentFlags().Lookup("lang"))
28+
_ = viper.BindPFlag("git.diff_unified", configCmd.PersistentFlags().Lookup("diff_unified"))
2629
}
2730

2831
var configCmd = &cobra.Command{

commitlint.config.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
module.exports = {extends: ['@commitlint/config-conventional']}
1+
module.exports = {
2+
extends: ['@commitlint/config-conventional'],
3+
rules: {
4+
'body-max-line-length': [0, 'always', 'Infinity'],
5+
},
6+
};

git/git.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/exec"
77
"path"
8+
"strconv"
89
"strings"
910

1011
"github.com/appleboy/CodeGPT/util"
@@ -20,7 +21,10 @@ var excludeFromDiff = []string{
2021
"go.mod",
2122
}
2223

23-
type Command struct{}
24+
type Command struct {
25+
// Generate diffs with <n> lines of context instead of the usual three
26+
diffUnified int
27+
}
2428

2529
func (c *Command) excludeFiles() []string {
2630
newFileLists := []string{}
@@ -52,7 +56,7 @@ func (c *Command) diffFiles() *exec.Cmd {
5256
"--staged",
5357
"--ignore-all-space",
5458
"--diff-algorithm=minimal",
55-
"--function-context",
59+
"--unified=" + strconv.Itoa(c.diffUnified),
5660
}
5761

5862
args = append(args, c.excludeFiles()...)
@@ -151,6 +155,16 @@ func (c *Command) UninstallHook() error {
151155
return os.Remove(target)
152156
}
153157

154-
func New() *Command {
155-
return &Command{}
158+
func New(opts ...Option) *Command {
159+
cfg := &config{}
160+
161+
// Loop through each option
162+
for _, o := range opts {
163+
// Call the option giving the instantiated
164+
o.apply(cfg)
165+
}
166+
167+
return &Command{
168+
diffUnified: cfg.diffUnified,
169+
}
156170
}

git/options.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package git
2+
3+
// Option is an interface that specifies instrumentation configuration options.
4+
type Option interface {
5+
apply(*config)
6+
}
7+
8+
// optionFunc is a type of function that can be used to implement the Option interface.
9+
// It takes a pointer to a config struct and modifies it.
10+
type optionFunc func(*config)
11+
12+
// Ensure that optionFunc satisfies the Option interface.
13+
var _ Option = (*optionFunc)(nil)
14+
15+
// The apply method of optionFunc type is implemented here to modify the config struct based on the function passed.
16+
func (o optionFunc) apply(c *config) {
17+
o(c)
18+
}
19+
20+
// WithDiffUnified is a function that generate diffs with <n> lines of context instead of the usual three.
21+
func WithDiffUnified(val int) Option {
22+
return optionFunc(func(c *config) {
23+
c.diffUnified = val
24+
})
25+
}
26+
27+
// config is a struct that stores configuration options for the instrumentation.
28+
type config struct {
29+
diffUnified int
30+
}

0 commit comments

Comments
 (0)