mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
Let users discard uncommitted source changes after confirmation, with dry-run support. Restore tracked files to HEAD and remove untracked files while preserving ignored files, nested repositories, and root-scope config.yaml. Guard against ancestor repositories and repositories without a first commit. Cover the API and confirmation flow with regression tests and sync all UI locales and Git Sync documentation.
57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"skillshare/internal/git"
|
|
)
|
|
|
|
func (s *Server) handleGitDiscard(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
if s.IsProjectMode() {
|
|
writeError(w, http.StatusBadRequest, "Git Sync is only available in global mode")
|
|
return
|
|
}
|
|
var body struct {
|
|
DryRun bool `json:"dryRun"`
|
|
}
|
|
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
|
|
if !errors.Is(err, errBodyTooLarge) {
|
|
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
|
}
|
|
return
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
src, ok := s.gitSource(w)
|
|
if !ok {
|
|
return
|
|
}
|
|
// Never discover an ancestor repo and discard outside the configured source.
|
|
if _, err := os.Stat(filepath.Join(src, ".git")); err != nil || !git.IsRepo(src) {
|
|
writeError(w, http.StatusBadRequest, "source directory is not a git repository root")
|
|
return
|
|
}
|
|
if _, err := git.GetCurrentHash(src); err != nil {
|
|
writeError(w, http.StatusBadRequest, "cannot discard changes before the first commit")
|
|
return
|
|
}
|
|
args := map[string]any{"dry_run": body.DryRun, "scope": "ui", "git_root": s.cfg.GitRoot}
|
|
if body.DryRun {
|
|
s.writeOpsLog("discard", "ok", start, args, "")
|
|
writeJSON(w, pushResponse{Success: true, Message: "dry run: would restore tracked files to HEAD and remove untracked files", DryRun: true})
|
|
return
|
|
}
|
|
if err := git.DiscardChanges(src, s.cfg.GitRoot == "root"); err != nil {
|
|
s.writeOpsLog("discard", "error", start, args, err.Error())
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
s.writeOpsLog("discard", "ok", start, args, "")
|
|
writeJSON(w, pushResponse{Success: true, Message: "changes discarded"})
|
|
}
|