default to git mod date, fall back to fs metadata

Amolith created

Change summary

README.md | 20 ++++++++++++--------
main.go   | 26 +++++++++++++++++++++-----
2 files changed, 33 insertions(+), 13 deletions(-)

Detailed changes

README.md 🔗

@@ -29,29 +29,33 @@ _Generate cover images from Hugo posts_
   - **Example:** `p2c -i content/posts/post.md -o public/post/cover.png`
 
 ```
-❯ p2c -h
+$ p2c -h
 
 Usage: p2c [options]
 
 Options:
   -h, --help                Show the help message
   -i, --input string        Path to input Markdown
-  -m, --metasize int        Size of font for meta information (default 40)
+  -M, --metasize int        Size of font for meta information (default 40)
   -o, --output string       Path to output PNG
-  -p, --posttitlesize int   Size of font for post title (default 60)
-  -s, --sitetitlesize int   Size of font for site title (default 50)
+  -t, --posttitle string    Title displayed in the generated image
+  -P, --posttitlesize int   Size of font for post title (default 60)
+  -S, --sitetitlesize int   Size of font for site title (default 50)
+  -s, --subtitle string     Subtitle displayed in the generated image
 
-example: p2c -i content/posts/post.md -o public/post/cover.png
+example: p2c -i input.md -o output.png
 
 p2c is meant for use with Hugo.
 
 It looks at...
-- Markdown file's frontmatter fields for
+- The Markdown file's frontmatter fields for
   - title
   - date
-- Site's config.{toml/yaml/yml} fields for
+- The site's config.{toml/yaml/yml} fields for
   - title (site title)
-- The file's metadata for the last modified date
+- The file's last modification date according to
+  the git history, falling back to the filesystem
+  metadata if git history is not available
 ```
 
 [goreportcard-badge]: https://goreportcard.com/badge/git.sr.ht/~amolith/p2c

main.go 🔗

@@ -12,6 +12,7 @@ import (
 	"image/color"
 	"image/png"
 	"os"
+	"os/exec"
 	"strings"
 
 	"gioui.org/font/opentype"
@@ -328,7 +329,9 @@ It looks at...
   - date
 - The site's config.{toml/yaml/yml} fields for
   - title (site title)
-- The file's metadata for the last modified date
+- The file's last modification date according to
+  the git history, falling back to the filesystem
+  metadata if git history is not available
 
 `)
 }
@@ -439,14 +442,27 @@ func getSiteTitle() string {
 	return t.Title
 }
 
-// Get the date the post was last edited using the file metadata
+// Get the date the post was last edited using the git log and falling back to
+// the file's last modified date
 func getEditDate(input string) string {
-	fileInfo, err := os.Stat(input)
+	_, err := os.Stat(".git")
 	if err != nil {
-		fmt.Println("Error: Could not get file info")
+		fileInfo, err := os.Stat(input)
+		if err != nil {
+			fmt.Println("Error: Could not get file info")
+			fmt.Println(err)
+			os.Exit(1)
+		}
+
+		return fileInfo.ModTime().Format("2006-01-02")
+	}
+	cmd := exec.Command("git", "log", "-1", "--format=%ad", "--date=short", input)
+	out, err := cmd.Output()
+	if err != nil {
+		fmt.Println("Error: Could not get git log")
 		fmt.Println(err)
 		os.Exit(1)
 	}
 
-	return fileInfo.ModTime().Format("2006-01-02")
+	return strings.TrimSpace(string(out))
 }