Skip to content

Commit

Permalink
fix file extension alias handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mesuutt committed May 24, 2023
1 parent 4c097d3 commit ce11f3b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 10 additions & 6 deletions pkg/commit/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ func NewGenerator(configPath string) Generator {
}

func (f Generator) GenCommit(stat *FileStat) Commit {
ext := f.findRealExtension(stat)
ext := f.findExtensionAlias(stat.Ext)
filename := fmt.Sprintf("log.%s", ext)

// handle files without extension. eg: Makefile, Dockerfile etc
if !strings.HasPrefix(stat.Ext, ".") {
filename = stat.Ext
}

msg, err := f.buildMessage(stat)
msg, err := f.buildMessage(stat, ext)
if err != nil {
panic("TODO")
}
Expand All @@ -45,7 +45,7 @@ func (f Generator) GenCommit(stat *FileStat) Commit {
}
}

func (f Generator) buildMessage(stat *FileStat) (string, error) {
func (f Generator) buildMessage(stat *FileStat, ext string) (string, error) {
if f.conf == nil {
return fmt.Sprintf(defaultCommitFormat, stat.Insert, stat.Delete), nil
}
Expand All @@ -57,7 +57,7 @@ func (f Generator) buildMessage(stat *FileStat) (string, error) {
}

var commitMessage bytes.Buffer
fileExt := f.removeDot(stat.Ext)
fileExt := f.removeDot(ext)
now := time.Now()

commonVars := struct {
Expand Down Expand Up @@ -107,8 +107,12 @@ func (f Generator) buildMessage(stat *FileStat) (string, error) {
return fmt.Sprintf(defaultCommitFormat, stat.Insert, stat.Delete), nil
}

func (f Generator) findRealExtension(stat *FileStat) string {
searchExt := f.removeDot(stat.Ext)
func (f Generator) findExtensionAlias(ext string) string {
if f.conf == nil {
return ext
}

searchExt := f.removeDot(ext)

for typ, aliases := range f.conf.Aliases {
if typ != "default" {
Expand Down
4 changes: 3 additions & 1 deletion pkg/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func TestRepo_AddChange(t *testing.T) {
repoPath, _ := os.MkdirTemp("", "")
t.Cleanup(func() { os.RemoveAll(repoPath) })

repo := NewRepo(repoPath)
commitGen := commit.NewGenerator(filepath.Join(repoPath, "config.toml"))

repo := NewRepo(repoPath, commitGen)

now := time.Now()
tests := []struct {
Expand Down

0 comments on commit ce11f3b

Please sign in to comment.