mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
* fix(pull): keep the local config.yaml when a later pull brings a tracked copy #357 refuses a root-scope first pull when the remote tracks config.yaml, but once upstream is set, a pull that merges in a remote-tracked copy still replaced this machine's config: Git treats the ignored file as expendable. Snapshot config.yaml around later pulls (CLI pull, push --pull, and the dashboard), restore it, and warn so the user can untrack it with push. Refs #353 * fix(git): untrack config.yaml after push --pull and warn on identical copies push --pull ran the root safety sweep before integrating the remote, so a pull that brought in a remote-tracked config.yaml was pushed back as-is: the remote kept tracking it and the next first pull elsewhere was refused. Re-run EnsureConfigUntracked after the pull and commit the removal before pushing. The warning also depended on the pull changing the local bytes, so a tracked copy identical to this machine's went unreported. KeepLocalConfig's restore now reports whether the repo tracks config.yaml after the pull. * fix(git): only restore config.yaml when the pull could have touched it KeepLocalConfig wrote its snapshot back after every root-scope pull, so an edit made to an untracked config.yaml while the pull ran was silently replaced by the stale copy. Git only rewrites config.yaml when it is tracked before or after the pull, so restore only in that case. * fix(git): restore config.yaml only when the pull changed its index entry Gating the restore on "tracked before or after" still overwrote edits made during a pull when config.yaml was already tracked and the pull left it alone. Compare config.yaml's index entry (mode, blob, stage) before and after the pull instead; a merge, reset or conflict that touches the file changes it, and anything else keeps the current file. * fix(git): warn about a tracked config.yaml only when the upstream tracks it The warning followed the local index, so a config.yaml tracked only by a local, unpushed commit was reported as tracked by the remote, and the suggested push would publish it in history. Check the upstream tree with RemoteTracksConfig("@{u}") instead. * fix(git): restore config.yaml after a pull that aborts its merge A merge that brought in a tracked config.yaml and conflicted elsewhere was aborted, which deleted the file again and returned the index entry to its pre-pull state, so restore saw no change and the local config was lost. Also restore when the snapshotted file is missing after the pull. * fix(git): keep config.yaml ignored when a later rule negates it EnsureConfigUntracked only checked that a "config.yaml" line existed, so a pulled .gitignore with a later "!config.yaml" left the file unignored and the following `git add -A` tracked this machine's copy again. Check the effective ignore state and append an overriding entry when needed. * fix: commit post-pull .gitignore repairs and keep config.yaml's mode push --pull committed only when EnsureConfigUntracked removed an index entry, so a pulled .gitignore that stopped ignoring config.yaml was repaired locally but never pushed, leaving the tree dirty. Commit whenever the safety step leaves changes. KeepLocalConfig skipped the restore when the bytes matched, so a checkout that rewrote config.yaml with the default mode dropped this machine's restrictive permissions. Compare the mode too, and chmod after writing since WriteFile's mode is masked by umask. * fix(git): restore config.yaml over a tracked config.yaml directory A pull that checks out a tracked config.yaml/ tree replaces the ignored local file with a directory, which os.Remove cannot delete, so restore failed and the local config stayed lost. Remove a directory there before writing the snapshot back; its contents are in Git. EnsureConfigUntracked now untracks recursively so push can remove that tree too. * fix: keep post-checkout config edits and preview .gitignore repairs Restore wrote the snapshot back whenever the pull changed config.yaml's index entry, even when a newer copy had been written after Git checked it out. Skip the restore when the file on disk no longer matches the blob the pull checked out. A dry-run push or commit only previewed untracking config.yaml, so when a later "!config.yaml" rule un-ignored it the preview listed config.yaml to commit while a real run repairs .gitignore instead. Preview that repair. * fix: scope the post-pull safety commit and keep post-checkout symlinks The post-pull safety step in push --pull ran `git add -A`, so an edit made by another process while the pull ran was committed and pushed under the config-safety message. Stage only .gitignore on top of the config.yaml removal and commit the index as staged. The post-checkout edit check skipped symlinks, so a dotfiles manager that replaced config.yaml with a newer link after the checkout had it replaced by the stale snapshot. Treat a link where Git checked out a file, or a different link target, as a newer local edit. * fix: preview explicit config.yaml entries and refuse other .gitignore edits A dry run previewed the .gitignore repair only when config.yaml was not ignored at all, but a real run also appends the explicit entry when a broader rule such as *.yaml ignores it. Preview whenever the entry is missing too. The post-pull safety commit staged the whole .gitignore, so an edit made by another process while the pull ran was pushed with it. Refuse unless the file only gained config.yaml lines over its staged version. * fix: report config.yaml restore failures instead of dropping them integrateRemote declared the KeepLocalConfig result with :=, shadowing the named err that the deferred restore joins its failure into, so a failed restore was swallowed and pull or push --pull went on to sync targets with another machine's config.yaml. The dashboard pull replaced the pull's own error with a bare 500 on a restore failure, losing the conflict response and the ops log entry; join the errors so both are reported.
116 lines
3.1 KiB
Go
116 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
|
|
"skillshare/internal/config"
|
|
"skillshare/internal/oplog"
|
|
"skillshare/internal/ui"
|
|
)
|
|
|
|
// checkGitWorktree verifies source is a git worktree without requiring a remote.
|
|
func checkGitWorktree(sourcePath string, spinner *ui.Spinner) error {
|
|
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
|
|
cmd.Dir = sourcePath
|
|
if err := cmd.Run(); err != nil {
|
|
spinner.Fail("Source is not a git repository")
|
|
ui.Note("Run: skillshare init")
|
|
return fmt.Errorf("not a git repository")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func cmdCommit(args []string) error {
|
|
if wantsHelp(args) {
|
|
printCommitHelp()
|
|
return nil
|
|
}
|
|
|
|
start := time.Now()
|
|
opts := parsePushArgs(args)
|
|
if opts.pull {
|
|
return fmt.Errorf("--pull is only supported by push: skillshare push --pull")
|
|
}
|
|
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return fmt.Errorf("config not found: run 'skillshare init' first")
|
|
}
|
|
|
|
spinner := ui.StartSpinner("Checking repository...")
|
|
source, err := resolveGitRoot(cfg, spinner)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := checkGitWorktree(source, spinner); err != nil {
|
|
return err
|
|
}
|
|
|
|
sweep := rootScopeSafetySweep(cfg, source, opts.dryRun)
|
|
if sweep.hasNotice() {
|
|
spinner.Stop()
|
|
sweep.printNotices(source)
|
|
if len(sweep.nested) > 0 {
|
|
return errNestedRepos // would commit empty submodules — abort
|
|
}
|
|
spinner = ui.StartSpinner("Checking changes...")
|
|
}
|
|
|
|
changes, err := getGitChanges(source)
|
|
if err != nil {
|
|
spinner.Fail("Failed to check git status")
|
|
return err
|
|
}
|
|
changes = sweep.previewChanges(changes)
|
|
if changes == "" {
|
|
spinner.Stop()
|
|
ui.Done(ui.MarkNone, "Nothing to commit", 0)
|
|
return nil
|
|
}
|
|
|
|
if opts.dryRun {
|
|
spinner.Stop()
|
|
files := strings.Split(changes, "\n")
|
|
ui.Row(ui.MarkNone, "Commit", "would commit "+plural(len(files), "file")+ui.DimText(" · "+opts.message), ui.RowWidth("Commit"))
|
|
for _, line := range files {
|
|
ui.Note(porcelainChange(line))
|
|
}
|
|
fmt.Println()
|
|
ui.DryRun()
|
|
return nil
|
|
}
|
|
|
|
if err := stageAndCommit(source, opts.message, spinner); err != nil {
|
|
return err
|
|
}
|
|
|
|
spinner.Stop()
|
|
files := strings.Split(changes, "\n")
|
|
ui.Row(ui.MarkOK, "Commit", plural(len(files), "file")+ui.DimText(" · "+opts.message)+ui.Took(time.Since(start)), ui.RowWidth("Commit"))
|
|
ui.Next("skillshare push", "share it with your other machines")
|
|
|
|
e := oplog.NewEntry("commit", "ok", time.Since(start))
|
|
e.Args = map[string]any{"message": opts.message}
|
|
oplog.WriteWithLimit(config.ConfigPath(), oplog.OpsFile, e, logMaxEntries()) //nolint:errcheck
|
|
|
|
return nil
|
|
}
|
|
|
|
func printCommitHelp() {
|
|
printHelp("skillshare commit [options]", "Create a local git commit for source skills without pushing.",
|
|
helpGroup{title: "Options", rows: []helpRow{
|
|
{"-m, --message <msg>", "Commit message (default: \"Update skills\")"},
|
|
{"-n, --dry-run", "Preview changes without applying"},
|
|
}},
|
|
helpExamples(
|
|
helpRow{"skillshare commit", "Commit with default message"},
|
|
helpRow{"skillshare commit -m \"Update skill\"", "Commit with custom message"},
|
|
helpRow{"skillshare commit --dry-run", "Preview what would happen"},
|
|
),
|
|
)
|
|
}
|