git: allow gitSync to not fail if the state is broken.

This commit is contained in:
fiatjaf
2025-11-25 08:54:41 -03:00
parent 73d80203a0
commit e04861fcee

29
git.go
View File

@@ -302,7 +302,7 @@ aside from those, there is also:
fetchFromRemotes(ctx, targetDir, repo) fetchFromRemotes(ctx, targetDir, repo)
// if we have a state with a HEAD, try to reset to it // if we have a state with a HEAD, try to reset to it
if state.Event.ID != nostr.ZeroID && state.HEAD != "" { if state != nil && state.HEAD != "" {
if headCommit, ok := state.Branches[state.HEAD]; ok { if headCommit, ok := state.Branches[state.HEAD]; ok {
// check if we have that commit // check if we have that commit
checkCmd := exec.Command("git", "cat-file", "-e", headCommit) checkCmd := exec.Command("git", "cat-file", "-e", headCommit)
@@ -703,7 +703,7 @@ func gitSync(ctx context.Context, signer nostr.Keyer) (nip34.Repository, *nip34.
// fetch repository announcement and state from relays // fetch repository announcement and state from relays
repo, state, err := fetchRepositoryAndState(ctx, owner, localConfig.Identifier, localConfig.GraspServers) repo, state, err := fetchRepositoryAndState(ctx, owner, localConfig.Identifier, localConfig.GraspServers)
if err != nil { if err != nil && repo.Event.ID == nostr.ZeroID {
log("couldn't fetch repository metadata (%s), will publish now\n", err) log("couldn't fetch repository metadata (%s), will publish now\n", err)
// create a local repository object from config and publish it // create a local repository object from config and publish it
localRepo := localConfig.ToRepository() localRepo := localConfig.ToRepository()
@@ -735,6 +735,15 @@ func gitSync(ctx context.Context, signer nostr.Keyer) (nip34.Repository, *nip34.
return repo, nil, fmt.Errorf("no signer provided to publish repository (run 'nak git sync' with the '--sec' flag)") return repo, nil, fmt.Errorf("no signer provided to publish repository (run 'nak git sync' with the '--sec' flag)")
} }
} else { } else {
if err != nil {
if _, ok := err.(StateErr); ok {
// some error with the state, just do nothing and proceed
} else {
// actually fail with this error we don't know about
return repo, nil, err
}
}
// check if local config differs from remote announcement // check if local config differs from remote announcement
// construct local repo from config for comparison // construct local repo from config for comparison
localRepo := localConfig.ToRepository() localRepo := localConfig.ToRepository()
@@ -955,7 +964,7 @@ func fetchRepositoryAndState(
} }
// fetch repository state (30618) // fetch repository state (30618)
var stateErr error var stateErr *StateErr
for ie := range sys.Pool.FetchMany(ctx, repo.Relays, nostr.Filter{ for ie := range sys.Pool.FetchMany(ctx, repo.Relays, nostr.Filter{
Kinds: []nostr.Kind{30618}, Kinds: []nostr.Kind{30618},
Authors: []nostr.PubKey{pubkey}, Authors: []nostr.PubKey{pubkey},
@@ -966,18 +975,18 @@ func fetchRepositoryAndState(
}, nostr.SubscriptionOptions{Label: "nak-git"}) { }, nostr.SubscriptionOptions{Label: "nak-git"}) {
if state == nil || ie.Event.CreatedAt > state.CreatedAt { if state == nil || ie.Event.CreatedAt > state.CreatedAt {
state_ := nip34.ParseRepositoryState(ie.Event) state_ := nip34.ParseRepositoryState(ie.Event)
state = &state_
if state.HEAD == "" { if state_.HEAD == "" {
stateErr = fmt.Errorf("state is missing HEAD") stateErr = &StateErr{"state is missing HEAD"}
continue continue
} }
if _, ok := state.Branches[state.HEAD]; !ok { if _, ok := state_.Branches[state_.HEAD]; !ok {
stateErr = fmt.Errorf("state is missing commit for HEAD branch '%s'", state.HEAD) stateErr = &StateErr{fmt.Sprintf("state is missing commit for HEAD branch '%s'", state_.HEAD)}
continue continue
} }
stateErr = nil stateErr = nil
state = &state_
} }
} }
if stateErr != nil { if stateErr != nil {
@@ -987,6 +996,10 @@ func fetchRepositoryAndState(
return repo, state, nil return repo, state, nil
} }
type StateErr struct{ string }
func (s StateErr) Error() string { return string(s.string) }
func findGitRoot(startDir string) string { func findGitRoot(startDir string) string {
if startDir == "" { if startDir == "" {
var err error var err error