fix pre delete dirmd set dir bug and implement last git sha and commit#

This commit is contained in:
2026-09-02 11:43:45 +01:00
parent 89b7eb78a7
commit 93d830d500
5 changed files with 72 additions and 14 deletions
+37
View File
@@ -0,0 +1,37 @@
package git
import (
"fmt"
"os/exec"
"strings"
)
type CommitInfo struct {
ShortSHA string
Message string
}
func GetCommitInfo(rootDir string) *CommitInfo {
cmd := exec.Command("git", "-C", rootDir, "log", "-1", "--format=%h %s")
output, err := cmd.Output()
if err != nil {
return nil
}
parts := strings.SplitN(strings.TrimSpace(string(output)), " ", 2)
if len(parts) < 2 {
return nil
}
return &CommitInfo{
ShortSHA: parts[0],
Message: parts[1],
}
}
func FormatCommitLine(info *CommitInfo) string {
if info == nil {
return ""
}
return fmt.Sprintf("> Commit: %s (%s)\n", info.ShortSHA, info.Message)
}
+13 -10
View File
@@ -9,6 +9,7 @@ import (
"time"
"unicode/utf8"
"git.flo.fo/FLO/dirmd/internal/git"
"git.flo.fo/FLO/dirmd/internal/tree"
"git.flo.fo/FLO/dirmd/internal/walker"
)
@@ -38,9 +39,10 @@ func Write(result *walker.Result, outputPath string, appendMode bool, singleFile
fmt.Fprintf(&sb, "# %s [ROOT]\n", h1Path)
// Add timestamp only if creating a new file (not append mode)
if !appendMode {
fmt.Fprintf(&sb, "> Generated: %s\n\n", time.Now().Format(time.RFC3339))
fmt.Fprintf(&sb, "> Generated: %s\n", time.Now().Format(time.RFC3339))
sb.WriteString(git.FormatCommitLine(result.Commit))
sb.WriteString("\n")
}
if !singleFile {
@@ -116,7 +118,7 @@ func WriteSlices(result *walker.Result, outputDir, repoName string) ([]string, e
rootPath := filepath.Join(outputDir, indexBasename)
generated = append(generated, rootPath)
if err := writeRootSlice(rootPath, absRoot, rootName, repoName, rootFiles, topLevelDirs, result.OmitTest); err != nil {
if err := writeRootSlice(rootPath, absRoot, rootName, repoName, rootFiles, topLevelDirs, result.OmitTest, result.Commit); err != nil {
return nil, fmt.Errorf("failed to write root slice: %w", err)
}
@@ -151,7 +153,7 @@ func WriteSlices(result *walker.Result, outputDir, repoName string) ([]string, e
}
}
if err := writeDirSlice(slicePath, absDir, absRoot, dirName, treeEntries, dirEntries, indexBasename, result.OmitTest); err != nil {
if err := writeDirSlice(slicePath, absDir, absRoot, dirName, treeEntries, dirEntries, indexBasename, result.OmitTest, result.Commit); err != nil {
return nil, fmt.Errorf("failed to write slice %s: %w", sliceName, err)
}
}
@@ -159,7 +161,7 @@ func WriteSlices(result *walker.Result, outputDir, repoName string) ([]string, e
return generated, nil
}
func writeRootSlice(path, absRoot, rootName, repoName string, rootFiles []walker.Entry, topLevelDirs map[string][]walker.Entry, omitTest bool) error {
func writeRootSlice(path, absRoot, rootName, repoName string, rootFiles []walker.Entry, topLevelDirs map[string][]walker.Entry, omitTest bool, commit *git.CommitInfo) error {
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("cannot create root file: %w", err)
@@ -169,8 +171,9 @@ func writeRootSlice(path, absRoot, rootName, repoName string, rootFiles []walker
var sb strings.Builder
fmt.Fprintf(&sb, "# %s [ROOT]\n", absRoot)
// Timestamp for new file creation
fmt.Fprintf(&sb, "> Generated: %s\n\n", time.Now().Format(time.RFC3339))
fmt.Fprintf(&sb, "> Generated: %s\n", time.Now().Format(time.RFC3339))
sb.WriteString(git.FormatCommitLine(commit))
sb.WriteString("\n")
treeLines := buildTreeWithRefs(rootName, repoName, topLevelDirs, rootFiles)
sb.WriteString("## Directory Tree\n\n")
@@ -238,7 +241,7 @@ func writeRootSlice(path, absRoot, rootName, repoName string, rootFiles []walker
return err
}
func writeDirSlice(path, absDir, absRoot, dirName string, treeEntries, fileEntries []walker.Entry, rootFileName string, omitTest bool) error {
func writeDirSlice(path, absDir, absRoot, dirName string, treeEntries, fileEntries []walker.Entry, rootFileName string, omitTest bool, commit *git.CommitInfo) error {
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("cannot create slice file: %w", err)
@@ -248,8 +251,8 @@ func writeDirSlice(path, absDir, absRoot, dirName string, treeEntries, fileEntri
var sb strings.Builder
fmt.Fprintf(&sb, "# %s [SLICE]\n", absDir)
// Timestamp for new file creation
fmt.Fprintf(&sb, "> Generated: %s\n\n", time.Now().Format(time.RFC3339))
fmt.Fprintf(&sb, "> Generated: %s\n", time.Now().Format(time.RFC3339))
sb.WriteString(git.FormatCommitLine(commit))
fmt.Fprintf(&sb, "Root document: %s\n\n", rootFileName)
sb.WriteString("## Directory Tree\n\n")
+9
View File
@@ -9,6 +9,7 @@ import (
"git.flo.fo/FLO/dirmd/internal/config"
"git.flo.fo/FLO/dirmd/internal/filter"
"git.flo.fo/FLO/dirmd/internal/git"
)
type Entry struct {
@@ -21,6 +22,7 @@ type Result struct {
AbsRoot string
SingleFile bool
OmitTest bool
Commit *git.CommitInfo
}
func Run(cfg *config.Config) (*Result, error) {
@@ -53,11 +55,15 @@ func processSingleFile(cfg *config.Config) (*Result, error) {
return nil, fmt.Errorf("file %s filtered out", cfg.AbsRoot)
}
// For single files, try to get git info from the parent directory
commitInfo := git.GetCommitInfo(filepath.Dir(cfg.AbsRoot))
return &Result{
Entries: []Entry{{RelPath: filepath.Base(cfg.AbsRoot), Size: info.Size()}},
AbsRoot: filepath.Dir(cfg.AbsRoot),
SingleFile: true,
OmitTest: cfg.OmitTest,
Commit: commitInfo,
}, nil
}
@@ -73,6 +79,9 @@ func walkDirectory(cfg *config.Config) (*Result, error) {
result := &Result{AbsRoot: cfg.AbsRoot, OmitTest: cfg.OmitTest}
// Fetch git info for the root directory
result.Commit = git.GetCommitInfo(cfg.AbsRoot)
err := filepath.WalkDir(cfg.AbsRoot, func(path string, d fs.DirEntry, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "warning: %v\n", err)