Files
skillshare/internal/server/handler_git_discard.go
Willie 6fbb5e109b feat(ui): add Git Sync discard changes
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.
2026-10-01 10:17:47 +08:00

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"})
}