mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
* fix(push): reject unpushed config history at root scope Untracking config.yaml at the tip still uploads its contents from earlier local commits. Refuse root-scope pushes before mutation and before upload when unpublished history adds or changes the file or its directory tree, without rewriting history. Share the check across the CLI and dashboard, return unpushed_config_history, and allow removals and config inherited unchanged from upstream merges. Cover first pushes, dry runs, push --pull, directory paths, renames, and merge commits, and document manual repair in all website locales. Fixes #366 * fix(push): check exactly what push sends for config history Review of the config history guard found two range problems: - The guard checked @{u}..HEAD, but a same-named upstream was pushed with a bare `git push`. With remote.pushDefault or push.default=matching that could send config-bearing commits to another remote, or other branches, that the guard never looked at. PushArgs now always names the upstream remote and branch (read from branch config, so names with "/" survive), so the checked range and the pushed range are the same. - Without an upstream every commit on HEAD counted as unpushed, so a config.yaml already published on origin followed by the "Keep config.yaml out of version control" commit was refused. The guard now checks HEAD --not --remotes=<push remote>. The refusal message now suggests rebasing from just before the oldest offending commit instead of @{u} or --root, which would rewrite commits the remote already has. * fix(push): check config history against the remote's live refs Local remote-tracking refs can be stale. When another machine force-pushes the remote to drop a leaked config.yaml commit, this clone's refs/remotes/<remote>/... still contains it, so the guard treated the leak as already published and HEAD:main, still a fast-forward, would publish it again. The guard now excludes only the objects the push remote's refs point to right now (git ls-remote), limited to objects this clone has. It falls back to local remote-tracking refs only when the remote cannot be reached, in which case the push fails anyway. Revisions go to git log on stdin as "^<id>" lines because git before 2.42 rejects "--not" there. * fix(push): screen config history against every push URL, without replace refs Two more ways the screened history could differ from what git push uploads: - `git ls-remote <remote>` queries the fetch URL, but git push sends to every remote.<name>.pushurl. A commit already on the fetch URL could still be new to a push URL. The guard now lists `git remote get-url --push --all` and excludes only objects that every push URL has. - With a refs/replace/* entry for an offending commit, git log read the sanitized replacement while the push pack carries the original. The log and the rebase-base lookup now run with --no-replace-objects. * fix(push): let pushes the remote will reject reach the pull step Our own review of the config history guard found two problems: - When another machine had pushed, the remote's tip was unknown locally, so no live ref survived the "objects this clone has" filter and the whole history counted as unpushed. A config.yaml published long ago, followed by the stop-tracking commit, then blocked `push --pull` before it could pull, and replaced the plain push's ErrPushRejected hint. A push URL whose destination branch tip this clone lacks rejects the push as non-fast-forward and receives nothing, so it is now left out, and the check passes when every push URL is like that. `push --pull` re-runs the check after pulling, with the tip known. - PushRemote read branch.<b>.remote even when @{u} no longer resolved, while PushArgs then pushed to origin. pushTarget now follows the same HasUpstream rule as PushArgs.
162 lines
6.5 KiB
Go
162 lines
6.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"skillshare/internal/config"
|
|
gitops "skillshare/internal/git"
|
|
"skillshare/internal/ui"
|
|
)
|
|
|
|
// hasGitDir reports whether dir contains a .git entry directly (not via a parent).
|
|
func hasGitDir(dir string) bool {
|
|
_, err := os.Stat(filepath.Join(dir, ".git"))
|
|
return err == nil
|
|
}
|
|
|
|
// resolveGitRoot returns the directory commit/push/pull should operate on.
|
|
//
|
|
// Option (a) mismatch handling: if the configured scope has no repo directly at
|
|
// its directory but another scope's directory does, the user changed git_root
|
|
// without relocating the repo. We print guidance and return an error so the
|
|
// caller aborts. When no stray repo is found we return the resolved directory and
|
|
// let the caller's existing repo check emit the standard "not a git repository"
|
|
// guidance (this also preserves the legacy case of a skills source nested inside
|
|
// an unrelated parent git repo).
|
|
func resolveGitRoot(cfg *config.Config, spinner *ui.Spinner) (string, error) {
|
|
// Reject an unknown git_root before resolving: ScopeDir silently falls back
|
|
// to the skills scope for any unrecognized value, which would make
|
|
// commit/push/pull operate on the wrong repository without warning.
|
|
if !config.ValidGitRoot(cfg.GitRoot) {
|
|
spinner.Fail("Invalid git_root")
|
|
ui.Note(fmt.Sprintf("git_root %q is not a valid scope", cfg.GitRoot))
|
|
ui.Note(fmt.Sprintf("valid values: %s (or leave empty for skills)", strings.Join(config.ValidGitRoots, ", ")))
|
|
return "", fmt.Errorf("invalid git_root %q", cfg.GitRoot)
|
|
}
|
|
|
|
root := cfg.EffectiveGitRoot()
|
|
if scope, dir, mismatch := cfg.GitRootMismatch(); mismatch {
|
|
configured := cfg.GitRoot
|
|
if configured == "" {
|
|
configured = "skills"
|
|
}
|
|
spinner.Fail("Git root mismatch")
|
|
ui.Note(fmt.Sprintf("git_root operates on: %s", root))
|
|
ui.Note(fmt.Sprintf("but the git repo lives at: %s (%s)", dir, scope))
|
|
ui.Note("Fix it with one of:")
|
|
ui.Note(fmt.Sprintf(" - skillshare init --git-root %s (start a fresh repo at the configured scope)", configured))
|
|
ui.Note(fmt.Sprintf(" - mv \"%s/.git\" \"%s/.git\" (move the existing repo over, keeps history)", dir, root))
|
|
ui.Note(fmt.Sprintf(" - set 'git_root: %s' in %s (keep using the existing repo)", scope, config.ConfigPath()))
|
|
return "", fmt.Errorf("git root mismatch (see guidance above)")
|
|
}
|
|
return root, nil
|
|
}
|
|
|
|
// rootSweepResult reports what the root-scope safety sweep changed or found.
|
|
type rootSweepResult struct {
|
|
configUntracked bool // config.yaml was (or, on dry-run, would be) untracked
|
|
ignoreRepair bool // dry-run: .gitignore would gain a config.yaml rule
|
|
nested []string // subdirs (relative) that have their own .git
|
|
dryRun bool // when true, the sweep made no changes (notice wording differs)
|
|
}
|
|
|
|
func (r rootSweepResult) hasNotice() bool {
|
|
return r.configUntracked || r.ignoreRepair || len(r.nested) > 0
|
|
}
|
|
|
|
// previewChanges adjusts a dry run's `git status --porcelain` output for the
|
|
// .gitignore repair a real run makes first: config.yaml drops out and
|
|
// .gitignore is listed as modified.
|
|
func (r rootSweepResult) previewChanges(changes string) string {
|
|
if !r.ignoreRepair {
|
|
return changes
|
|
}
|
|
kept := []string{}
|
|
hasGitignore := false
|
|
for _, line := range strings.Split(changes, "\n") {
|
|
path := strings.TrimSpace(strings.TrimLeft(line, "?MADRCU "))
|
|
switch {
|
|
case line == "", path == "config.yaml", strings.HasPrefix(path, "config.yaml/"):
|
|
continue
|
|
case path == ".gitignore":
|
|
hasGitignore = true
|
|
}
|
|
kept = append(kept, line)
|
|
}
|
|
if !hasGitignore {
|
|
kept = append(kept, " M .gitignore")
|
|
}
|
|
return strings.Join(kept, "\n")
|
|
}
|
|
|
|
// rootScopeSafetySweep guards a root-scope repo before staging. It detects
|
|
// nested git repos that would otherwise be committed as empty submodules and
|
|
// keeps skillshare's own config.yaml out of version control (the push-time net
|
|
// for repos not created via InitScopeRepo). It is a no-op (zero result) for
|
|
// non-root scopes.
|
|
//
|
|
// When dryRun is true the sweep is strictly read-only: it reports whether
|
|
// config.yaml WOULD be untracked (via IsConfigTracked) without writing
|
|
// .gitignore or running `git rm --cached`. On a real run the config mutation is
|
|
// best-effort — its failure is swallowed so the worst case is the prior
|
|
// behavior, never a blocked push. Nested-repo detection is always read-only.
|
|
// Push checks unpushed config history separately before this sweep: removing
|
|
// config.yaml from the index cannot remove it from earlier commits.
|
|
func rootScopeSafetySweep(cfg *config.Config, dir string, dryRun bool) rootSweepResult {
|
|
res := rootSweepResult{dryRun: dryRun}
|
|
if cfg.GitRoot != "root" {
|
|
return res
|
|
}
|
|
if nested, err := gitops.NestedRepos(dir); err == nil {
|
|
res.nested = nested
|
|
}
|
|
if dryRun {
|
|
res.configUntracked = gitops.IsConfigTracked(dir)
|
|
res.ignoreRepair = gitops.ConfigGitignoreNeedsRepair(dir)
|
|
} else if removed, err := gitops.EnsureConfigUntracked(dir); err == nil {
|
|
res.configUntracked = removed
|
|
}
|
|
return res
|
|
}
|
|
|
|
// printNotices reports the sweep outcome. Callers stop any active spinner first
|
|
// so the warning box renders cleanly.
|
|
func (r rootSweepResult) printNotices(dir string) {
|
|
if r.configUntracked {
|
|
if r.dryRun {
|
|
fmt.Println(" Would remove config.yaml from version control")
|
|
ui.Note("It holds machine-specific paths")
|
|
} else {
|
|
ui.Success("Removed config.yaml from version control")
|
|
ui.Note("Kept on disk; it holds machine-specific paths")
|
|
}
|
|
}
|
|
if r.ignoreRepair && !r.configUntracked {
|
|
fmt.Println(" Would add config.yaml to .gitignore")
|
|
ui.Note("It holds machine-specific paths")
|
|
}
|
|
if len(r.nested) > 0 {
|
|
lines := []string{"These directories have their own .git and upload as EMPTY submodules:"}
|
|
for _, sub := range r.nested {
|
|
lines = append(lines, " - "+sub)
|
|
}
|
|
lines = append(lines,
|
|
"",
|
|
"Their files will NOT be tracked. Disable each nested repo, then retry:",
|
|
fmt.Sprintf(" mv \"%s/<dir>/.git\" \"%s/<dir>/.git.disabled\"", dir, dir),
|
|
"(or use one-click disable on the web UI Git Sync page)",
|
|
)
|
|
ui.WarningBox("Nested git repositories found", lines...)
|
|
}
|
|
}
|
|
|
|
// errNestedRepos blocks commit/push when a root-scope repo contains nested git
|
|
// repositories. They would upload as empty submodules (silent data loss), so
|
|
// the operation aborts until the user disables them. printNotices renders the
|
|
// per-directory remediation box; this carries only the short headline for the
|
|
// CLI's error line and a non-zero exit code.
|
|
var errNestedRepos = fmt.Errorf("nested git repositories must be disabled first (see guidance above)")
|