Skip to content

Commit

Permalink
Add Commit method to Markdown and use that in Push
Browse files Browse the repository at this point in the history
  • Loading branch information
slashtechno authored Jun 26, 2024
1 parent d72a5c3 commit d7a3d08
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 33 deletions.
1 change: 0 additions & 1 deletion internal/platforms/blogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ func (b *Blogger) fetchNewPosts(options PushPullOptions) ([]PostData, error) {

// CleanMarkdownPosts takes a Markdown destination and using a Charm KV store, remove any posts that are deleted from contentDir
func (b Blogger) CleanMarkdownPosts(wg *sync.WaitGroup, interval time.Duration, kvClient *kv.KV, markdownDest *Markdown, options PushPullOptions, errChan chan<- error) {

}
func (b Blogger) GetName() string { return b.Name }
func (b Blogger) GetType() string { return "blogger" }
103 changes: 71 additions & 32 deletions internal/platforms/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-git/go-git/v5"
"github.com/goccy/go-yaml"
"github.com/gosimple/slug"
"github.com/slashtechno/cross-blogger/pkg/utils"
"github.com/spf13/afero"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/text"
Expand Down Expand Up @@ -102,48 +103,86 @@ func (m Markdown) Push(data PostData, options PushPullOptions) error {

// If the Git directory is set, commit + push the changes
if m.GitDir != "" {

// Open the git repository
// Clean up the Git directory path
dirPath = filepath.Clean(m.GitDir)
// No need to check if the directory exits since PlainOpen will return an error if a repos
// Open the repository
repo, err := git.PlainOpen(dirPath)
if err != nil {
return err
}
repoWorktree, err := repo.Worktree()
commitHash, err := m.Commit(slug, true)
if err != nil {
return err
}
// Get the relative path of filePath to dirPath
relativePath, err := filepath.Rel(dirPath, filePath)
if err != nil {
// Handle error, for example, return it
return err
}
log.Info("Committed and pushed changes", "hash", commitHash)

// Add the file
_, err = repoWorktree.Add(relativePath)
if err != nil {
return err
}
// Commit the changes
commitHash, err := repoWorktree.Commit("(Re-)publish "+slug+".md", &git.CommitOptions{})
if err != nil {
return err
}
log.Info("Committed changes", "hash", commitHash.String())
}
return nil

}

// Commit and optionally push the changes to the Git repository.
// If contentDir is not a subdirectory of the gitDir, error.
func (m Markdown) Commit(slug string, push bool) (hash string, err error) {
contentDir := m.ContentDir
gitDir := m.GitDir
filePath := filepath.Join(m.ContentDir, slug+".md")

// Clean the Git directory path
dirPath := filepath.Clean(gitDir)
// Clean the content directory path
contentDir = filepath.Clean(contentDir)
// Clean the file path
filePath = filepath.Clean(filePath)
// Make sure contentDir and gitDir are absolute paths
contentDir, err = filepath.Abs(contentDir)
if err != nil {
return "", err
}
gitDir, err = filepath.Abs(gitDir)
if err != nil {
return "", err
}
// Check if the contentDir is a subdirectory of the gitDir
// if !filepath.HasPrefix(contentDir, gitDir) {
// return "", fmt.Errorf("contentDir is not a subdirectory of gitDir")
// }
isSubdir, err := utils.IsSubdirectory(gitDir, contentDir)
if err != nil {
return "", err
}
if !isSubdir {
return "", fmt.Errorf("contentDir is not a subdirectory of gitDir")
}
// Open the repository
repo, err := git.PlainOpen(dirPath)
if err != nil {
return "", err
}
repoWorktree, err := repo.Worktree()
if err != nil {
return "", err
}
// Get the relative path of filePath to dirPath
relativePath, err := filepath.Rel(dirPath, filePath)
if err != nil {
// Handle error, for example, return it
return "", err
}

// Add the file
_, err = repoWorktree.Add(relativePath)
if err != nil {
return "", err
}
// Commit the changes
commitHash, err := repoWorktree.Commit("Update "+slug+".md", &git.CommitOptions{})
if err != nil {
return "", err
}
if push {
// Push the changes
err = repo.Push(&git.PushOptions{})
if err != nil {
return err
return "", err
}
}

return nil

return commitHash.String(), nil
}

func (m Markdown) Pull(options PushPullOptions) (PostData, error) {
// Get the file path
fs := afero.NewOsFs()
Expand Down
24 changes: 24 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package utils

import (
"path/filepath"
"strings"
)

// Check if a slice contains a string
func ContainsString(slice []string, item string) bool {
for _, a := range slice {
Expand Down Expand Up @@ -34,3 +39,22 @@ func DefaultInt(i, defaultValue int) int {
}
return i
}

// Function to check if one path is a subdirectory of another
func IsSubdirectory(parent, child string) (bool, error) {
// Clean and convert the parent path to an absolute path
parentPath, err := filepath.Abs(filepath.Clean(parent))
if err != nil {
return false, err
}
childPath, err := filepath.Abs(child)
if err != nil {
return false, err
}
// Ensure proper boundary matching by adding a trailing separator to the parent path
parentPathWithSep := parentPath + string(filepath.Separator)

// Use strings.HasPrefix to check if the child path is a subdirectory of the parent path
isSubdir := strings.HasPrefix(childPath, parentPathWithSep)
return isSubdir, nil
}

0 comments on commit d7a3d08

Please sign in to comment.