isacikgoz / gitbatch

manage your git repositories in one place
MIT License
1.54k stars 53 forks source link

Error when loading stashed items on a detached head #42

Closed ObviouslyGreen closed 5 years ago

ObviouslyGreen commented 5 years ago

For some reason I had a stashed commit for a repo in a detached state i.e. stash@{0}: WIP on (no branch): test


goroutine 34 [running]:
github.com/isacikgoz/gitbatch/pkg/git.(*RepoEntity).loadStashedItems(0xc000382600, 0x0, 0x0)
    /Users/shibo/Code/go/src/github.com/isacikgoz/gitbatch/pkg/git/cmd-stash.go:57 +0x69c
github.com/isacikgoz/gitbatch/pkg/git.(*RepoEntity).loadComponents(0xc000382600, 0x1, 0xc000382600, 0x0)
    /Users/shibo/Code/go/src/github.com/isacikgoz/gitbatch/pkg/git/repository.go:119 +0x92
github.com/isacikgoz/gitbatch/pkg/git.InitializeRepo(0xc0000a8f00, 0x2f, 0xc0000abe90, 0x0, 0x0)
    /Users/shibo/Code/go/src/github.com/isacikgoz/gitbatch/pkg/git/repository.go:104 +0x7d
github.com/isacikgoz/gitbatch/pkg/git.LoadRepositoryEntities.func1(0xc0000abe90, 0xc0000abe88, 0xc0000bde20, 0xc0000a8f00, 0x2f)
    /Users/shibo/Code/go/src/github.com/isacikgoz/gitbatch/pkg/git/util-load.go:27 +0x7d
created by github.com/isacikgoz/gitbatch/pkg/git.LoadRepositoryEntities
    /Users/shibo/Code/go/src/github.com/isacikgoz/gitbatch/pkg/git/util-load.go:23 +0x158
exit status 2

Quickly threw together a possible fix, but haven't tested with other stuff yet so just opening an issue instead of pr: https://play.golang.org/p/oI9XpuE8-iw

isacikgoz commented 5 years ago

I tested this, it seems ok.


    stashBranchRegex := regexp.MustCompile(`^(.*?): `)
    stashMsgRegex := regexp.MustCompile(`WIP on \(?([^)]*)\)?`)

    stashBranchRegexMatch := stashBranchRegex.FindString(trimmed)
    branchName := stashBranchRegexMatch[:len(stashBranchRegexMatch)-2]

    branchMatches := stashMsgRegex.FindStringSubmatch(branchName)
    if len(branchMatches) >= 2 {
        branchName = stashBranchRegexMatch[:len(stashBranchRegexMatch)-2]
    }

Thanks for the issue btw, and very good of you for submitting the fix 👍 @ObviouslyGreen

isacikgoz commented 5 years ago

fixed it with the pr #44 , please let me know if you have still issues. Thanks for the feedback