diff --git a/backend/internal/api/handlers.go b/backend/internal/api/handlers.go index 5c85920..717094e 100644 --- a/backend/internal/api/handlers.go +++ b/backend/internal/api/handlers.go @@ -97,9 +97,9 @@ func SaveFile(fs *filesystem.FileSystem) http.HandlerFunc { return } - w.WriteHeader(http.StatusOK) - if _, err := w.Write([]byte("File saved successfully")); err != nil { - http.Error(w, "Failed to write response", http.StatusInternalServerError) + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(map[string]string{"message": "File saved successfully"}); err != nil { + http.Error(w, "Failed to encode response", http.StatusInternalServerError) } } } @@ -144,7 +144,7 @@ func GetSettings(db *db.DB) http.HandlerFunc { } } -func UpdateSettings(db *db.DB) http.HandlerFunc { +func UpdateSettings(db *db.DB, fs *filesystem.FileSystem) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var settings models.Settings if err := json.NewDecoder(r.Body).Decode(&settings); err != nil { @@ -165,6 +165,15 @@ func UpdateSettings(db *db.DB) http.HandlerFunc { return } + if settings.Settings.GitEnabled { + err := fs.SetupGitRepo(settings.Settings.GitURL, settings.Settings.GitUser, settings.Settings.GitToken) + if err != nil { + http.Error(w, "Failed to setup git repo", http.StatusInternalServerError) + } + } else { + fs.DisableGitRepo() + } + // Fetch the saved settings to return savedSettings, err := db.GetSettings(settings.UserID) if err != nil { @@ -203,9 +212,9 @@ func StageCommitAndPush(fs *filesystem.FileSystem) http.HandlerFunc { return } - w.WriteHeader(http.StatusOK) - if _, err := w.Write([]byte("Changes staged, committed, and pushed successfully")); err != nil { - http.Error(w, "Failed to write response", http.StatusInternalServerError) + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(map[string]string{"message": "Changes staged, committed, and pushed successfully"}); err != nil { + http.Error(w, "Failed to encode response", http.StatusInternalServerError) } } } diff --git a/backend/internal/api/routes.go b/backend/internal/api/routes.go index 7a9a6b8..761bab7 100644 --- a/backend/internal/api/routes.go +++ b/backend/internal/api/routes.go @@ -11,7 +11,7 @@ func SetupRoutes(r chi.Router, db *db.DB, fs *filesystem.FileSystem) { r.Route("/", func(r chi.Router) { r.Route("/settings", func(r chi.Router) { r.Get("/", GetSettings(db)) - r.Post("/", UpdateSettings(db)) + r.Post("/", UpdateSettings(db, fs)) }) r.Route("/files", func(r chi.Router) { r.Get("/", ListFiles(fs)) diff --git a/backend/internal/filesystem/filesystem.go b/backend/internal/filesystem/filesystem.go index e53f9dd..6fe21ad 100644 --- a/backend/internal/filesystem/filesystem.go +++ b/backend/internal/filesystem/filesystem.go @@ -42,6 +42,15 @@ func New(rootDir string, settings *models.Settings) *FileSystem { return fs } +func (fs *FileSystem) SetupGitRepo(gitURL string, gitUser string, gitToken string) error { + fs.GitRepo = gitutils.New(gitURL, gitUser, gitToken, fs.RootDir) + return fs.InitializeGitRepo() +} + +func (fs *FileSystem) DisableGitRepo() { + fs.GitRepo = nil; +} + func (fs *FileSystem) InitializeGitRepo() error { if fs.GitRepo == nil { return errors.New("git settings not configured") diff --git a/frontend/src/components/MainContent.js b/frontend/src/components/MainContent.js index d4835dd..2717ede 100644 --- a/frontend/src/components/MainContent.js +++ b/frontend/src/components/MainContent.js @@ -30,13 +30,21 @@ const MainContent = ({ selectedFile, handleFileSelect, handleLinkClick }) => { const handleSaveFile = useCallback( async (filePath, content) => { - const success = await handleSave(filePath, content); + let success = await handleSave(filePath, content); if (success) { setHasUnsavedChanges(false); + + if (settings.gitAutoCommit && settings.gitEnabled) { + const commitMessage = settings.gitCommitMsgTemplate.replace( + '${filename}', + filePath + ); + success = await handleCommitAndPush(commitMessage); + } } return success; }, - [handleSave, setHasUnsavedChanges] + [handleSave, setHasUnsavedChanges, settings, handleCommitAndPush] ); const handleCreateFile = useCallback(