Adding files to the details page. - staticgit - A git static site generator, the site you are viewing now!
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit 3ae097a4ff6feb3c58b3e5c5aae916616d5bddf6
 (DIR) parent 89f13ce268930a8a931f4a82312653c96ac8d8ca
 (HTM) Author: Jay Scott <me@jay.scot>
       Date:   Sat, 13 Jul 2024 20:47:16 +0100
       
       Adding files to the details page.
       
       Diffstat:
         M main.go                             |      50 +++++++++++++++++++++++++++++--
       
       1 file changed, 48 insertions(+), 2 deletions(-)
       ---
 (DIR) diff --git a/main.go b/main.go
       @@ -61,7 +61,7 @@ const (
                          <img src="{{.IconPath}}logo.png" alt="" width="32" height="32" />
                  </td>
              <td>
       -            <span class="desc">{{.Title}}</span>
       +              <span class="desc">{{.Title}}</span>
                  </td>
            </tr>
          </table>
       @@ -78,7 +78,7 @@ const (
        {{define "content"}}
          <pre>{{.ReadmeContent}}</pre>
        
       -  <h2>Commit History</h2>
       +  <h2 id="commits">Commit History</h2>
          <hr>
          <table>
            <thead>
       @@ -101,9 +101,19 @@ const (
                        <td style="color: green;">+{{.Added}}</td>
                <td style="color: red;">-{{.Removed}}</td>
              </tr>
       +
              {{end}}
            </tbody>
          </table>
       +
       +  <h2 id="files">Files</h2>
       +  <hr>
       +  <ul>
       +    {{range .Files}}
       +    <li>{{.}}</li>
       +    {{end}}
       +  </ul>
       +
        {{end}}
        `
        
       @@ -186,6 +196,11 @@ func generateRepo(repoName, repoPath, outputDir string) error {
                        return fmt.Errorf("failed to get commit history: %w", err)
                }
        
       +        files, err := getFiles(repo)
       +        if err != nil {
       +                return fmt.Errorf("failed to get repository files: %w", err)
       +        }
       +
                outputPath := filepath.Join(outputDir, "index.html")
        
                f, err := os.Create(outputPath)
       @@ -199,12 +214,14 @@ func generateRepo(repoName, repoPath, outputDir string) error {
                        IconPath      string
                        RepoName      string
                        ReadmeContent string
       +                Files         []string
                        Commits       []CommitInfo
                }{
                        Title:         "git clone git@git.jay.scot:" + repoName,
                        IconPath:      "../",
                        RepoName:      repoName,
                        ReadmeContent: readme,
       +                Files:         files,
                        Commits:       commits,
                })
        }
       @@ -264,6 +281,35 @@ func getDescription(repoPath string) string {
                return strings.TrimSpace(string(description))
        }
        
       +func getFiles(repo *git.Repository) ([]string, error) {
       +        ref, err := repo.Head()
       +        if err != nil {
       +                return nil, fmt.Errorf("failed to get HEAD reference: %w", err)
       +        }
       +
       +        commit, err := repo.CommitObject(ref.Hash())
       +        if err != nil {
       +                return nil, fmt.Errorf("failed to get commit object: %w", err)
       +        }
       +
       +        tree, err := commit.Tree()
       +        if err != nil {
       +                return nil, fmt.Errorf("failed to get tree: %w", err)
       +        }
       +
       +        var files []string
       +        err = tree.Files().ForEach(func(f *object.File) error {
       +                files = append(files, f.Name)
       +                return nil
       +        })
       +        if err != nil {
       +                return nil, fmt.Errorf("failed to iterate over files: %w", err)
       +        }
       +
       +        sort.Strings(files)
       +        return files, nil
       +}
       +
        func getReadme(repoPath string) (string, error) {
                readmeFiles := []string{"README.md", "README.txt", "README"}
                repo, err := git.PlainOpen(repoPath)