From 35b092cf400a5f3fda788f5d74e9b030d81368dd Mon Sep 17 00:00:00 2001 From: Alexander_Kabui Date: Fri, 2 Aug 2024 15:30:53 +0300 Subject: add commit procedures --- web/view/markdown.scm | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'web') diff --git a/web/view/markdown.scm b/web/view/markdown.scm index 4c5f658..30c9f33 100644 --- a/web/view/markdown.scm +++ b/web/view/markdown.scm @@ -7,6 +7,8 @@ #:use-module (ice-9 string-fun) #:use-module (ice-9 textual-ports) #:use-module (ice-9 exceptions) + #:use-module (ice-9 popen) + #:use-module (ice-9 rdelim) #:use-module (sxml simple) #:use-module (web client) #:use-module (web uri) @@ -18,7 +20,8 @@ #:export (markdown-file->sxml markdown-github->sxml fetch-file - fetch-raw-file) + fetch-raw-file + commit-file) ) @@ -76,9 +79,53 @@ (define (git-invoke repo-path . args) (apply system* "git" "-C" repo-path args)) - (define (is-repo? repo-path) (let ((data (git-invoke repo-path "rev-parse")) ) (zero? data) ) ) + + +(define (get-latest-commit-sha1 repo-path) + (let* ((output-port (open-input-pipe (string-append "git -C " repo-path " log -n 1 --pretty=format:%H HEAD"))) + (commit-sha (read-line output-port))) + (close-port output-port) + commit-sha)) + + + + +(define (commit-file repo file-path content commit-message username email) + (if (is-repo? repo) + (match (file-exists? (string-append repo "/" file-path)) + (#t + (with-output-to-file (string-append repo "/" file-path) + (lambda () + (display content) + ) + ) + ;;prevent users from commit other people changes check for git add status + (let* ((git-add-file (git-invoke repo "add" file-path)) + (git-commit-file + (git-invoke repo "commit" + "-m" commit-message + "-m" " * Commit made via the GN Markdown Editor" + "--author" (format #f "~a <~a>" username email)) + ) + (git-commit-sha (get-latest-commit-sha1 repo)) + ) + ;; check if git add had an error ;; check git commit if had an error + ;;runs the risk of commit a different user changes check this ;; potential for collisions + (if (zero? git-commit-file) + `(("status" . "201") ("message" . "committed file successfully") ("content" . ,content) ("commit-sha" . ,git-commit-sha) ("commit-message" . ,commit-message) ) + `(("status" . "200") ("message" . "Nothing to commit, working tree clean") ("commit-sha" . ,git-commit-sha)) + ) + ) + ) + (#f + (throw 'system-error (string-append file-path " File does not exist Error")) + ) + ) + (throw 'system-error (string-append repo " Is not a git repo.")) + ) + ) -- cgit v1.2.3