parse git config to get remote names for branches

This commit is contained in:
maia tillie arson crimew 2021-10-21 01:36:09 +02:00
parent 9d7d6f58e7
commit 9cd04c613d
3 changed files with 49 additions and 8 deletions

1
go.mod
View File

@ -9,4 +9,5 @@ require (
github.com/phuslu/log v1.0.75
github.com/spf13/cobra v1.1.1
github.com/valyala/fasthttp v1.16.0
gopkg.in/ini.v1 v1.63.2
)

2
go.sum
View File

@ -350,6 +350,8 @@ gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8X
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.63.2 h1:tGK/CyBg7SMzb60vP1M03vNZ3VDu3wGQJwn7Sxi9r3c=
gopkg.in/ini.v1 v1.63.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=

View File

@ -1,15 +1,18 @@
package workers
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
"sync"
"github.com/deletescape/goop/internal/jobtracker"
"github.com/deletescape/goop/internal/utils"
"github.com/phuslu/log"
"github.com/valyala/fasthttp"
"gopkg.in/ini.v1"
)
var refRegex = regexp.MustCompile(`(?m)(refs(/[a-zA-Z0-9\-\.\_\*]+)+)`)
@ -54,16 +57,34 @@ func findRefWork(c *fasthttp.Client, baseUrl, baseDir, path string, jt *jobtrack
content, err := ioutil.ReadFile(targetFile)
if err != nil {
log.Error().Str("file", targetFile).Err(err).Msg("error while reading file")
return
}
for _, ref := range refRegex.FindAll(content, -1) {
jt.AddJob(utils.Url(".git", string(ref)))
jt.AddJob(utils.Url(".git/logs", string(ref)))
}
if path == ".git/config" || path == ".git/FETCH_HEAD" {
// TODO check the actual origin instead of just assuming origin here
if path == ".git/FETCH_HEAD" {
// TODO figure out actual remote instead of just assuming origin here (if possible)
for _, branch := range branchRegex.FindAllSubmatch(content, -1) {
jt.AddJob(utils.Url(".git/refs/remotes/origin", string(branch[1])))
jt.AddJob(utils.Url(".git/logs/refs/remotes/origin", string(branch[1])))
jt.AddJob(fmt.Sprintf(".git/refs/remotes/origin/%s", branch[1]))
jt.AddJob(fmt.Sprintf(".git/logs/refs/remotes/origin/%s", branch[1]))
}
}
if path == ".git/config" {
cfg, err := ini.Load(content)
if err != nil {
log.Error().Str("file", targetFile).Err(err).Msg("failed to parse git config")
return
}
for _, sec := range cfg.Sections() {
if strings.HasPrefix(sec.Name(), "branch ") {
parts := strings.SplitN(sec.Name(), " ", 2)
branch := strings.Trim(parts[1], `"`)
remote := sec.Key("remote").String()
jt.AddJob(fmt.Sprintf(".git/refs/remotes/%s/%s", remote, branch))
jt.AddJob(fmt.Sprintf(".git/logs/refs/remotes/%s/%s", remote, branch))
}
}
}
return
@ -107,11 +128,28 @@ func findRefWork(c *fasthttp.Client, baseUrl, baseDir, path string, jt *jobtrack
jt.AddJob(utils.Url(".git", string(ref)))
jt.AddJob(utils.Url(".git/logs", string(ref)))
}
if path == ".git/config" || path == ".git/FETCH_HEAD" {
// TODO check the actual origin instead of just assuming origin here
if path == ".git/FETCH_HEAD" {
// TODO figure out actual remote instead of just assuming origin here (if possible)
for _, branch := range branchRegex.FindAllSubmatch(body, -1) {
jt.AddJob(utils.Url(".git/refs/remotes/origin", string(branch[1])))
jt.AddJob(utils.Url(".git/logs/refs/remotes/origin", string(branch[1])))
jt.AddJob(fmt.Sprintf(".git/refs/remotes/origin/%s", branch[1]))
jt.AddJob(fmt.Sprintf(".git/logs/refs/remotes/origin/%s", branch[1]))
}
}
if path == ".git/config" {
cfg, err := ini.Load(body)
if err != nil {
log.Error().Str("file", targetFile).Err(err).Msg("failed to parse git config")
return
}
for _, sec := range cfg.Sections() {
if strings.HasPrefix(sec.Name(), "branch ") {
parts := strings.SplitN(sec.Name(), " ", 2)
branch := strings.Trim(parts[1], `"`)
remote := sec.Key("remote").String()
jt.AddJob(fmt.Sprintf(".git/refs/remotes/%s/%s", remote, branch))
jt.AddJob(fmt.Sprintf(".git/logs/refs/remotes/%s/%s", remote, branch))
}
}
}
}