about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlexander_Kabui2024-08-02 15:30:53 +0300
committerAlexander_Kabui2024-08-02 19:49:32 +0300
commit35b092cf400a5f3fda788f5d74e9b030d81368dd (patch)
tree896b092283918b734f6bc3a8abf6b2c4a00f0d3c
parentc5df803f4fc46db9813076f4e27c0dac697581a0 (diff)
downloadgn-guile-35b092cf400a5f3fda788f5d74e9b030d81368dd.tar.gz
add commit procedures
-rw-r--r--web/view/markdown.scm51
1 files changed, 49 insertions, 2 deletions
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."))
+      )
+  )