Create ref file from log files if no ref files were fetched

This commit is contained in:
Tillie Kottmann 2020-11-21 19:35:23 +01:00
parent 5f27228b62
commit a2f0e88e53
2 changed files with 26 additions and 0 deletions

View File

@ -252,12 +252,37 @@ func FetchGit(baseUrl, baseDir string) error {
}
gitLogsDir := utils.Url(baseDir, ".git/logs")
if utils.Exists(gitLogsDir) {
refLogPrefix := utils.Url(gitLogsDir, "refs") + "/"
if err := filepath.Walk(gitLogsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files = append(files, path)
if strings.HasPrefix(path, refLogPrefix) {
refName := strings.TrimPrefix(path, refLogPrefix)
fmt.Println(refName)
filePath := utils.Url(gitRefsDir, refName)
if !utils.Exists(filePath) {
fmt.Println("[-] Generating ref file for", refName)
content, err := ioutil.ReadFile(path)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
}
// Find the last reflog entry and extract the obj hash and write that to the ref file
logObjs := refLogRegex.FindAllSubmatch(content, -1)
lastEntryObj := logObjs[len(logObjs)-1][1]
utils.CreateParentFolders(filePath)
if err := ioutil.WriteFile(filePath, lastEntryObj, os.ModePerm); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
}
}
}
}
return nil
}); err != nil {

View File

@ -9,6 +9,7 @@ var phpSuffix = []byte{'.', 'p', 'h', 'p'}
var (
packRegex = regexp.MustCompile(`(?m)pack-([a-f0-9]{40})\.pack`)
objRegex = regexp.MustCompile(`(?m)(^|\s)([a-f0-9]{40})($|\s)`)
refLogRegex = regexp.MustCompile(`(?m)^(?:[a-f0-9]{40}) ([a-f0-9]{40}) .*$`)
stdErrRegex = regexp.MustCompile(`error: unable to read sha1 file of (.+?) \(.*`)
)
var (