git: nicer logs and fix announce to update only and all outdated relays.

This commit is contained in:
fiatjaf
2025-11-18 23:06:12 -03:00
parent ae3cb7c108
commit 51876f89c4
3 changed files with 40 additions and 28 deletions

63
git.go
View File

@@ -461,7 +461,7 @@ var gitPush = &cli.Command{
} }
if state.Event.ID != nostr.ZeroID { if state.Event.ID != nostr.ZeroID {
log("found state event: %s\n", state.Event.ID) logverbose("found state event: %s\n", state.Event.ID)
} }
// get commit for the local branch // get commit for the local branch
@@ -471,7 +471,7 @@ var gitPush = &cli.Command{
} }
currentCommit := strings.TrimSpace(string(res)) currentCommit := strings.TrimSpace(string(res))
log("pushing branch %s to remote branch %s, commit: %s\n", localBranch, remoteBranch, currentCommit) logverbose("pushing branch %s to remote branch %s, commit: %s\n", localBranch, remoteBranch, currentCommit)
// create a new state if we didn't find any // create a new state if we didn't find any
if state.Event.ID == nostr.ZeroID { if state.Event.ID == nostr.ZeroID {
@@ -493,12 +493,12 @@ var gitPush = &cli.Command{
} }
} }
state.Branches[remoteBranch] = currentCommit state.Branches[remoteBranch] = currentCommit
log("> setting branch %s to commit %s\n", remoteBranch, currentCommit) log("- setting branch %s to commit %s\n", color.CyanString(remoteBranch), color.CyanString(currentCommit))
// set the HEAD to the local branch if none is set // set the HEAD to the local branch if none is set
if state.HEAD == "" { if state.HEAD == "" {
state.HEAD = remoteBranch state.HEAD = remoteBranch
log("> setting HEAD to branch %s\n", remoteBranch) log("- setting HEAD to branch %s\n", color.CyanString(remoteBranch))
} }
// create and sign the new state event // create and sign the new state event
@@ -508,12 +508,12 @@ var gitPush = &cli.Command{
return fmt.Errorf("error signing state event: %w", err) return fmt.Errorf("error signing state event: %w", err)
} }
log("> publishing updated repository state %s\n", newStateEvent.ID) log("- publishing updated repository state to " + color.CyanString("%v", relays) + "\n")
for res := range sys.Pool.PublishMany(ctx, relays, newStateEvent) { for res := range sys.Pool.PublishMany(ctx, relays, newStateEvent) {
if res.Error != nil { if res.Error != nil {
log("(!) error publishing event to relay %s: %v\n", res.RelayURL, res.Error) log("! error publishing event to %s: %v\n", color.YellowString(res.RelayURL), res.Error)
} else { } else {
log("> published to relay %s\n", res.RelayURL) log("> published to %s\n", color.GreenString(res.RelayURL))
} }
} }
@@ -625,9 +625,15 @@ var gitAnnounce = &cli.Command{
} }
} }
// fetch repository announcement (30617) events // these are the relays where we'll publish the announcement to
var repo nip34.Repository
relays := append(sys.FetchOutboxRelays(ctx, ownerPk, 3), localConfig.GraspServers...) relays := append(sys.FetchOutboxRelays(ctx, ownerPk, 3), localConfig.GraspServers...)
for i := range relays {
relays[i] = nostr.NormalizeURL(relays[i])
}
// fetch repository announcement (30617) events
oks := make([]bool, len(relays))
var repo nip34.Repository
results := sys.Pool.FetchMany(ctx, relays, nostr.Filter{ results := sys.Pool.FetchMany(ctx, relays, nostr.Filter{
Kinds: []nostr.Kind{30617}, Kinds: []nostr.Kind{30617},
Tags: nostr.TagMap{ Tags: nostr.TagMap{
@@ -635,37 +641,40 @@ var gitAnnounce = &cli.Command{
}, },
Limit: 1, Limit: 1,
}, nostr.SubscriptionOptions{ }, nostr.SubscriptionOptions{
Label: "nak-git-announce", Label: "nak-git-announce",
CheckDuplicate: func(id nostr.ID, relay string) bool { return false }, // get the same event from multiple relays
}) })
for ie := range results { for ie := range results {
repo = nip34.ParseRepository(ie.Event) repo = nip34.ParseRepository(ie.Event)
// check if this is ok or the announcement in this relay needs to be updated
if repositoriesEqual(repo, localRepo) {
relayIdx := slices.Index(relays, ie.Relay.URL)
oks[relayIdx] = true
}
} }
// publish repository announcement if needed // publish repository announcement if needed
var needsAnnouncement bool if slices.Contains(oks, false) {
if repo.Event.ID == nostr.ZeroID {
log("no existing repository announcement found, will create one\n")
needsAnnouncement = true
} else if !repositoriesEqual(repo, localRepo) {
log("local repository config differs from published announcement, will update\n")
needsAnnouncement = true
}
if needsAnnouncement {
announcementEvent := localRepo.ToEvent() announcementEvent := localRepo.ToEvent()
if err := kr.SignEvent(ctx, &announcementEvent); err != nil { if err := kr.SignEvent(ctx, &announcementEvent); err != nil {
return fmt.Errorf("failed to sign announcement event: %w", err) return fmt.Errorf("failed to sign announcement event: %w", err)
} }
log("> publishing repository announcement %s\n", announcementEvent.ID) targets := make([]string, 0, len(oks))
for res := range sys.Pool.PublishMany(ctx, relays, announcementEvent) { for i, ok := range oks {
if res.Error != nil { if !ok {
log("(!) error publishing announcement to relay %s: %v\n", res.RelayURL, res.Error) targets = append(targets, relays[i])
} else { }
log("> published announcement to relay %s\n", res.RelayURL) }
log("- publishing repository announcement to " + color.CyanString("%v", targets) + "\n")
for res := range sys.Pool.PublishMany(ctx, targets, announcementEvent) {
if res.Error != nil {
log("! error publishing announcement to relay %s: %v\n", color.YellowString(res.RelayURL), res.Error)
} else {
log("> published announcement to relay %s\n", color.GreenString(res.RelayURL))
} }
} }
} else {
log("repository announcement is up to date\n")
} }
return nil return nil

3
go.mod
View File

@@ -19,6 +19,7 @@ require (
github.com/mattn/go-isatty v0.0.20 github.com/mattn/go-isatty v0.0.20
github.com/mattn/go-tty v0.0.7 github.com/mattn/go-tty v0.0.7
github.com/mdp/qrterminal/v3 v3.2.1 github.com/mdp/qrterminal/v3 v3.2.1
github.com/puzpuzpuz/xsync/v3 v3.5.1
github.com/stretchr/testify v1.10.0 github.com/stretchr/testify v1.10.0
github.com/urfave/cli/v3 v3.0.0-beta1 github.com/urfave/cli/v3 v3.0.0-beta1
golang.org/x/exp v0.0.0-20251113190631-e25ba8c21ef6 golang.org/x/exp v0.0.0-20251113190631-e25ba8c21ef6
@@ -46,6 +47,7 @@ require (
github.com/elnosh/gonuts v0.4.2 // indirect github.com/elnosh/gonuts v0.4.2 // indirect
github.com/fasthttp/websocket v1.5.12 // indirect github.com/fasthttp/websocket v1.5.12 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-git/go-git/v5 v5.16.3 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
github.com/hablullah/go-hijri v1.0.2 // indirect github.com/hablullah/go-hijri v1.0.2 // indirect
github.com/hablullah/go-juliandays v1.0.0 // indirect github.com/hablullah/go-juliandays v1.0.0 // indirect
@@ -58,7 +60,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/puzpuzpuz/xsync/v3 v3.5.1 // indirect
github.com/rs/cors v1.11.1 // indirect github.com/rs/cors v1.11.1 // indirect
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 // indirect github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 // indirect
github.com/tetratelabs/wazero v1.8.0 // indirect github.com/tetratelabs/wazero v1.8.0 // indirect

2
go.sum
View File

@@ -83,6 +83,8 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E= github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ= github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
github.com/go-git/go-git/v5 v5.16.3 h1:Z8BtvxZ09bYm/yYNgPKCzgWtaRqDTgIKRgIRHBfU6Z8=
github.com/go-git/go-git/v5 v5.16.3/go.mod h1:4Ge4alE/5gPs30F2H1esi2gPd69R0C39lolkucHBOp8=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=