mirror of
https://github.com/fiatjaf/nak.git
synced 2025-12-09 09:08:50 +00:00
Compare commits
2 Commits
16916d7d95
...
210cf66d5f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
210cf66d5f | ||
|
|
f9335b0ab4 |
405
git.go
405
git.go
@@ -8,6 +8,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"fiatjaf.com/nostr"
|
"fiatjaf.com/nostr"
|
||||||
"fiatjaf.com/nostr/nip19"
|
"fiatjaf.com/nostr/nip19"
|
||||||
@@ -92,6 +93,9 @@ aside from those, there is also:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var defaultOwner string
|
||||||
|
var defaultIdentifier string
|
||||||
|
|
||||||
// check if nip34.json already exists
|
// check if nip34.json already exists
|
||||||
existingConfig, err := readNip34ConfigFile("")
|
existingConfig, err := readNip34ConfigFile("")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -99,27 +103,11 @@ aside from those, there is also:
|
|||||||
if !c.Bool("force") && !c.Bool("interactive") {
|
if !c.Bool("force") && !c.Bool("interactive") {
|
||||||
return fmt.Errorf("nip34.json already exists, use --force to overwrite or --interactive to update")
|
return fmt.Errorf("nip34.json already exists, use --force to overwrite or --interactive to update")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// get repository base directory name for defaults
|
defaultIdentifier = existingConfig.Identifier
|
||||||
cwd, err := os.Getwd()
|
defaultOwner = existingConfig.Owner
|
||||||
if err != nil {
|
} else {
|
||||||
return fmt.Errorf("failed to get current directory: %w", err)
|
// extract info from nostr:// git remotes (this is just for migrating from ngit)
|
||||||
}
|
|
||||||
baseName := filepath.Base(cwd)
|
|
||||||
|
|
||||||
// get earliest unique commit
|
|
||||||
var earliestCommit string
|
|
||||||
if output, err := exec.Command("git", "rev-list", "--max-parents=0", "HEAD").Output(); err == nil {
|
|
||||||
earliest := strings.Split(strings.TrimSpace(string(output)), "\n")
|
|
||||||
if len(earliest) > 0 {
|
|
||||||
earliestCommit = earliest[0]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// extract clone URLs from nostr:// git remotes
|
|
||||||
// (this is just for migrating from ngit)
|
|
||||||
var defaultCloneURLs []string
|
|
||||||
if output, err := exec.Command("git", "remote", "-v").Output(); err == nil {
|
if output, err := exec.Command("git", "remote", "-v").Output(); err == nil {
|
||||||
remotes := strings.Split(strings.TrimSpace(string(output)), "\n")
|
remotes := strings.Split(strings.TrimSpace(string(output)), "\n")
|
||||||
for _, remote := range remotes {
|
for _, remote := range remotes {
|
||||||
@@ -128,17 +116,104 @@ aside from those, there is also:
|
|||||||
if len(parts) >= 2 {
|
if len(parts) >= 2 {
|
||||||
nostrURL := parts[1]
|
nostrURL := parts[1]
|
||||||
// parse nostr://npub.../relay_hostname/identifier
|
// parse nostr://npub.../relay_hostname/identifier
|
||||||
if owner, identifier, relays, err := parseRepositoryAddress(ctx, nostrURL); err == nil && len(relays) > 0 {
|
if remoteOwner, remoteIdentifier, relays, err := parseRepositoryAddress(ctx, nostrURL); err == nil && len(relays) > 0 {
|
||||||
relayURL := relays[0]
|
defaultIdentifier = remoteIdentifier
|
||||||
// convert to https://relay_hostname/npub.../identifier.git
|
defaultOwner = nip19.EncodeNpub(remoteOwner)
|
||||||
cloneURL := fmt.Sprintf("http%s/%s/%s.git",
|
|
||||||
relayURL[2:], nip19.EncodeNpub(owner), identifier)
|
|
||||||
defaultCloneURLs = appendUnique(defaultCloneURLs, cloneURL)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// get repository base directory name for defaults
|
||||||
|
if defaultIdentifier == "" {
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get current directory: %w", err)
|
||||||
|
}
|
||||||
|
defaultIdentifier = filepath.Base(cwd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// prompt for identifier first
|
||||||
|
var identifier string
|
||||||
|
if c.String("identifier") != "" {
|
||||||
|
identifier = c.String("identifier")
|
||||||
|
} else if c.Bool("interactive") {
|
||||||
|
if err := survey.AskOne(&survey.Input{
|
||||||
|
Message: "identifier",
|
||||||
|
Default: defaultIdentifier,
|
||||||
|
}, &identifier); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
identifier = defaultIdentifier
|
||||||
|
}
|
||||||
|
|
||||||
|
// prompt for owner pubkey
|
||||||
|
var owner nostr.PubKey
|
||||||
|
var ownerStr string
|
||||||
|
if c.String("owner") != "" {
|
||||||
|
owner, err = parsePubKey(ownerStr)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid owner pubkey: %w", err)
|
||||||
|
}
|
||||||
|
ownerStr = nip19.EncodeNpub(owner)
|
||||||
|
} else if c.Bool("interactive") {
|
||||||
|
for {
|
||||||
|
if err := survey.AskOne(&survey.Input{
|
||||||
|
Message: "owner (npub or hex)",
|
||||||
|
Default: defaultOwner,
|
||||||
|
}, &ownerStr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
owner, err = parsePubKey(ownerStr)
|
||||||
|
if err == nil {
|
||||||
|
ownerStr = nip19.EncodeNpub(owner)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("owner pubkey is required (use --owner or --interactive)")
|
||||||
|
}
|
||||||
|
|
||||||
|
// try to fetch existing repository announcement (kind 30617)
|
||||||
|
var fetchedRepo *nip34.Repository
|
||||||
|
if existingConfig.Identifier == "" {
|
||||||
|
log(" searching for existing events... ")
|
||||||
|
repo, _, err := fetchRepositoryAndState(ctx, owner, identifier, nil)
|
||||||
|
if err == nil && repo.Event.ID != nostr.ZeroID {
|
||||||
|
fetchedRepo = &repo
|
||||||
|
log("found one from %s.\n", repo.Event.CreatedAt.Time().Format(time.DateOnly))
|
||||||
|
} else {
|
||||||
|
log("none found.\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set config with fetched values or defaults
|
||||||
|
var config Nip34Config
|
||||||
|
if fetchedRepo != nil {
|
||||||
|
config = RepositoryToConfig(*fetchedRepo)
|
||||||
|
} else if existingConfig.Identifier != "" {
|
||||||
|
config = existingConfig
|
||||||
|
} else {
|
||||||
|
// get earliest unique commit
|
||||||
|
var earliestCommit string
|
||||||
|
if output, err := exec.Command("git", "rev-list", "--max-parents=0", "HEAD").Output(); err == nil {
|
||||||
|
earliestCommit = strings.TrimSpace(string(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
config = Nip34Config{
|
||||||
|
Identifier: identifier,
|
||||||
|
Owner: ownerStr,
|
||||||
|
Name: identifier,
|
||||||
|
Description: "",
|
||||||
|
Web: []string{},
|
||||||
|
GraspServers: []string{"gitnostr.com", "relay.ngit.dev"},
|
||||||
|
EarliestUniqueCommit: earliestCommit,
|
||||||
|
Maintainers: []string{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// helper to get value from flags, existing config, or default
|
// helper to get value from flags, existing config, or default
|
||||||
getValue := func(existingVal, flagVal, defaultVal string) string {
|
getValue := func(existingVal, flagVal, defaultVal string) string {
|
||||||
@@ -161,21 +236,84 @@ aside from those, there is also:
|
|||||||
return defaultVals
|
return defaultVals
|
||||||
}
|
}
|
||||||
|
|
||||||
config := Nip34Config{
|
// override with flags and existing config
|
||||||
Identifier: getValue(existingConfig.Identifier, c.String("identifier"), baseName),
|
config.Identifier = getValue(existingConfig.Identifier, c.String("identifier"), config.Identifier)
|
||||||
Name: getValue(existingConfig.Name, c.String("name"), baseName),
|
config.Name = getValue(existingConfig.Name, c.String("name"), config.Name)
|
||||||
Description: getValue(existingConfig.Description, c.String("description"), ""),
|
config.Description = getValue(existingConfig.Description, c.String("description"), config.Description)
|
||||||
Web: getSliceValue(existingConfig.Web, c.StringSlice("web"), []string{}),
|
config.Web = getSliceValue(existingConfig.Web, c.StringSlice("web"), config.Web)
|
||||||
Owner: getValue(existingConfig.Owner, c.String("owner"), ""),
|
config.Owner = getValue(existingConfig.Owner, c.String("owner"), config.Owner)
|
||||||
GraspServers: getSliceValue(existingConfig.GraspServers, c.StringSlice("grasp-servers"), []string{"gitnostr.com", "relay.ngit.dev"}),
|
config.GraspServers = getSliceValue(existingConfig.GraspServers, c.StringSlice("grasp-servers"), config.GraspServers)
|
||||||
EarliestUniqueCommit: getValue(existingConfig.EarliestUniqueCommit, c.String("earliest-unique-commit"), earliestCommit),
|
config.EarliestUniqueCommit = getValue(existingConfig.EarliestUniqueCommit, c.String("earliest-unique-commit"), config.EarliestUniqueCommit)
|
||||||
Maintainers: getSliceValue(existingConfig.Maintainers, c.StringSlice("maintainers"), []string{}),
|
config.Maintainers = getSliceValue(existingConfig.Maintainers, c.StringSlice("maintainers"), config.Maintainers)
|
||||||
}
|
|
||||||
|
|
||||||
if c.Bool("interactive") {
|
if c.Bool("interactive") {
|
||||||
if err := promptForConfig(&config); err != nil {
|
// prompt for name
|
||||||
|
if err := survey.AskOne(&survey.Input{
|
||||||
|
Message: "name",
|
||||||
|
Default: config.Name,
|
||||||
|
}, &config.Name); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prompt for description
|
||||||
|
if err := survey.AskOne(&survey.Input{
|
||||||
|
Message: "description",
|
||||||
|
Default: config.Description,
|
||||||
|
}, &config.Description); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// prompt for grasp servers
|
||||||
|
graspServers, err := promptForStringList("grasp servers", config.GraspServers, []string{
|
||||||
|
"gitnostr.com",
|
||||||
|
"relay.ngit.dev",
|
||||||
|
"pyramid.fiatjaf.com",
|
||||||
|
"git.shakespeare.dyi",
|
||||||
|
}, graspServerHost, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
config.GraspServers = graspServers
|
||||||
|
|
||||||
|
// prompt for web URLs
|
||||||
|
webURLs, err := promptForStringList("web URLs", config.Web, []string{
|
||||||
|
fmt.Sprintf("https://gitworkshop.dev/%s/%s",
|
||||||
|
nip19.EncodeNpub(nostr.MustPubKeyFromHex(config.Owner)),
|
||||||
|
config.Identifier,
|
||||||
|
),
|
||||||
|
}, func(s string) string {
|
||||||
|
return "http" + nostr.NormalizeURL(s)[2:]
|
||||||
|
}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
config.Web = webURLs
|
||||||
|
|
||||||
|
// prompt for earliest unique commit
|
||||||
|
if err := survey.AskOne(&survey.Input{
|
||||||
|
Message: "earliest unique commit",
|
||||||
|
Default: config.EarliestUniqueCommit,
|
||||||
|
}, &config.EarliestUniqueCommit); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prompt for maintainers
|
||||||
|
maintainers, err := promptForStringList("maintainers", config.Maintainers, []string{}, nil, func(s string) bool {
|
||||||
|
pk, err := parsePubKey(s)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if pk.Hex() == config.Owner {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
config.Maintainers = maintainers
|
||||||
|
|
||||||
|
log("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := config.Validate(); err != nil {
|
if err := config.Validate(); err != nil {
|
||||||
@@ -197,7 +335,7 @@ aside from those, there is also:
|
|||||||
|
|
||||||
log("edit %s if needed, then run %s to publish.\n",
|
log("edit %s if needed, then run %s to publish.\n",
|
||||||
color.CyanString("nip34.json"),
|
color.CyanString("nip34.json"),
|
||||||
color.CyanString("nak git announce"))
|
color.CyanString("nak git sync"))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
@@ -266,22 +404,7 @@ aside from those, there is also:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// write nip34.json inside cloned directory
|
// write nip34.json inside cloned directory
|
||||||
localConfig := Nip34Config{
|
localConfig := RepositoryToConfig(repo)
|
||||||
Identifier: repo.ID,
|
|
||||||
Name: repo.Name,
|
|
||||||
Description: repo.Description,
|
|
||||||
Web: repo.Web,
|
|
||||||
Owner: nip19.EncodeNpub(repo.Event.PubKey),
|
|
||||||
GraspServers: make([]string, 0, len(repo.Relays)),
|
|
||||||
EarliestUniqueCommit: repo.EarliestUniqueCommitID,
|
|
||||||
Maintainers: make([]string, 0, len(repo.Maintainers)),
|
|
||||||
}
|
|
||||||
for _, r := range repo.Relays {
|
|
||||||
localConfig.GraspServers = append(localConfig.GraspServers, nostr.NormalizeURL(r))
|
|
||||||
}
|
|
||||||
for _, m := range repo.Maintainers {
|
|
||||||
localConfig.Maintainers = append(localConfig.Maintainers, nip19.EncodeNpub(m))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := localConfig.Validate(); err != nil {
|
if err := localConfig.Validate(); err != nil {
|
||||||
return fmt.Errorf("invalid config: %w", err)
|
return fmt.Errorf("invalid config: %w", err)
|
||||||
@@ -423,8 +546,7 @@ aside from those, there is also:
|
|||||||
pushSuccesses := 0
|
pushSuccesses := 0
|
||||||
for _, relay := range repo.Relays {
|
for _, relay := range repo.Relays {
|
||||||
relayURL := nostr.NormalizeURL(relay)
|
relayURL := nostr.NormalizeURL(relay)
|
||||||
remoteName := "nip34/grasp/" + strings.TrimPrefix(relayURL, "wss://")
|
remoteName := gitRemoteName(relayURL)
|
||||||
remoteName = strings.TrimPrefix(remoteName, "ws://")
|
|
||||||
|
|
||||||
log("pushing to %s...\n", color.CyanString(remoteName))
|
log("pushing to %s...\n", color.CyanString(remoteName))
|
||||||
pushArgs := []string{"push", remoteName, fmt.Sprintf("%s:refs/heads/%s", localBranch, remoteBranch)}
|
pushArgs := []string{"push", remoteName, fmt.Sprintf("%s:refs/heads/%s", localBranch, remoteBranch)}
|
||||||
@@ -617,50 +739,45 @@ aside from those, there is also:
|
|||||||
|
|
||||||
func promptForStringList(
|
func promptForStringList(
|
||||||
name string,
|
name string,
|
||||||
existing []string,
|
|
||||||
defaults []string,
|
defaults []string,
|
||||||
|
alternatives []string,
|
||||||
normalize func(string) string,
|
normalize func(string) string,
|
||||||
validate func(string) bool,
|
validate func(string) bool,
|
||||||
) ([]string, error) {
|
) ([]string, error) {
|
||||||
options := make([]string, 0, len(defaults)+len(existing)+1)
|
options := make([]string, 0, len(defaults)+len(alternatives)+1)
|
||||||
options = append(options, defaults...)
|
options = append(options, defaults...)
|
||||||
options = append(options, "add another")
|
|
||||||
|
|
||||||
// add existing not in options
|
// add existing not in options
|
||||||
for _, item := range existing {
|
for _, item := range alternatives {
|
||||||
if !slices.Contains(options, item) {
|
if !slices.Contains(options, item) {
|
||||||
options = append(options, item)
|
options = append(options, item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
selected := make([]string, len(existing))
|
options = append(options, "add another")
|
||||||
copy(selected, existing)
|
|
||||||
|
selected := make([]string, len(defaults))
|
||||||
|
copy(selected, defaults)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
prompt := &survey.MultiSelect{
|
newSelected := []string{}
|
||||||
|
if err := survey.AskOne(&survey.MultiSelect{
|
||||||
Message: name,
|
Message: name,
|
||||||
Options: options,
|
Options: options,
|
||||||
Default: selected,
|
Default: selected,
|
||||||
PageSize: 20,
|
PageSize: 20,
|
||||||
}
|
}, &newSelected); err != nil {
|
||||||
|
|
||||||
if err := survey.AskOne(prompt, &selected); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
selected = newSelected
|
||||||
|
|
||||||
if slices.Contains(selected, "add another") {
|
if slices.Contains(selected, "add another") {
|
||||||
selected = slices.DeleteFunc(selected, func(s string) bool { return s == "add another" })
|
selected = slices.DeleteFunc(selected, func(s string) bool { return s == "add another" })
|
||||||
|
|
||||||
singular := name
|
|
||||||
if strings.HasSuffix(singular, "s") {
|
|
||||||
singular = singular[:len(singular)-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
newPrompt := &survey.Input{
|
|
||||||
Message: fmt.Sprintf("enter new %s", singular),
|
|
||||||
}
|
|
||||||
var newItem string
|
var newItem string
|
||||||
if err := survey.AskOne(newPrompt, &newItem); err != nil {
|
if err := survey.AskOne(&survey.Input{
|
||||||
|
Message: fmt.Sprintf("enter new %s", strings.TrimSuffix(name, "s")),
|
||||||
|
}, &newItem); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -690,97 +807,6 @@ func promptForStringList(
|
|||||||
return selected, nil
|
return selected, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func promptForConfig(config *Nip34Config) error {
|
|
||||||
log("\nenter repository details (use arrow keys to navigate, space to select/deselect, enter to confirm):\n\n")
|
|
||||||
|
|
||||||
// prompt for identifier
|
|
||||||
identifierPrompt := &survey.Input{
|
|
||||||
Message: "identifier",
|
|
||||||
Default: config.Identifier,
|
|
||||||
}
|
|
||||||
if err := survey.AskOne(identifierPrompt, &config.Identifier); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// prompt for name
|
|
||||||
namePrompt := &survey.Input{
|
|
||||||
Message: "name",
|
|
||||||
Default: config.Name,
|
|
||||||
}
|
|
||||||
if err := survey.AskOne(namePrompt, &config.Name); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// prompt for description
|
|
||||||
descPrompt := &survey.Input{
|
|
||||||
Message: "description",
|
|
||||||
Default: config.Description,
|
|
||||||
}
|
|
||||||
if err := survey.AskOne(descPrompt, &config.Description); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// prompt for owner
|
|
||||||
for {
|
|
||||||
ownerPrompt := &survey.Input{
|
|
||||||
Message: "owner (npub or hex)",
|
|
||||||
Default: config.Owner,
|
|
||||||
}
|
|
||||||
if err := survey.AskOne(ownerPrompt, &config.Owner); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if pubkey, err := parsePubKey(config.Owner); err == nil {
|
|
||||||
config.Owner = pubkey.Hex()
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// prompt for grasp servers
|
|
||||||
graspServers, err := promptForStringList("grasp servers", config.GraspServers, []string{
|
|
||||||
"gitnostr.com",
|
|
||||||
"relay.ngit.dev",
|
|
||||||
"pyramid.fiatjaf.com",
|
|
||||||
"git.shakespeare.dyi",
|
|
||||||
}, graspServerHost, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
config.GraspServers = graspServers
|
|
||||||
|
|
||||||
// prompt for web URLs
|
|
||||||
webURLs, err := promptForStringList("web URLs", config.Web, []string{
|
|
||||||
fmt.Sprintf("https://gitworkshop.dev/%s/%s",
|
|
||||||
nip19.EncodeNpub(nostr.MustPubKeyFromHex(config.Owner)),
|
|
||||||
config.Identifier,
|
|
||||||
),
|
|
||||||
}, func(s string) string {
|
|
||||||
return "http" + nostr.NormalizeURL(s)[2:]
|
|
||||||
}, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
config.Web = webURLs
|
|
||||||
|
|
||||||
// Prompt for maintainers
|
|
||||||
maintainers, err := promptForStringList("maintainers", config.Maintainers, []string{}, nil, func(s string) bool {
|
|
||||||
pk, err := parsePubKey(s)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if pk.Hex() == config.Owner {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
config.Maintainers = maintainers
|
|
||||||
|
|
||||||
log("\n")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func gitSync(ctx context.Context, signer nostr.Keyer) (nip34.Repository, *nip34.RepositoryState, error) {
|
func gitSync(ctx context.Context, signer nostr.Keyer) (nip34.Repository, *nip34.RepositoryState, error) {
|
||||||
// read current nip34.json
|
// read current nip34.json
|
||||||
localConfig, err := readNip34ConfigFile("")
|
localConfig, err := readNip34ConfigFile("")
|
||||||
@@ -914,7 +940,7 @@ func gitSync(ctx context.Context, signer nostr.Keyer) (nip34.Repository, *nip34.
|
|||||||
func fetchFromRemotes(ctx context.Context, targetDir string, repo nip34.Repository) {
|
func fetchFromRemotes(ctx context.Context, targetDir string, repo nip34.Repository) {
|
||||||
// fetch from each grasp remote
|
// fetch from each grasp remote
|
||||||
for _, grasp := range repo.Relays {
|
for _, grasp := range repo.Relays {
|
||||||
remoteName := "nip34/grasp/" + strings.Split(grasp, "/")[2]
|
remoteName := gitRemoteName(grasp)
|
||||||
|
|
||||||
logverbose("fetching from %s...\n", remoteName)
|
logverbose("fetching from %s...\n", remoteName)
|
||||||
fetchCmd := exec.Command("git", "fetch", remoteName)
|
fetchCmd := exec.Command("git", "fetch", remoteName)
|
||||||
@@ -940,14 +966,16 @@ func gitSetupRemotes(ctx context.Context, dir string, repo nip34.Repository) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete all nip34/grasp/ remotes
|
// delete all nip34/grasp/ remotes that we don't have anymore in repo
|
||||||
remotes := strings.Split(strings.TrimSpace(string(output)), "\n")
|
remotes := strings.Split(strings.TrimSpace(string(output)), "\n")
|
||||||
for i, remote := range remotes {
|
for i, remote := range remotes {
|
||||||
remote = strings.TrimSpace(remote)
|
remote = strings.TrimSpace(remote)
|
||||||
remotes[i] = remote
|
remotes[i] = remote
|
||||||
|
|
||||||
if strings.HasPrefix(remote, "nip34/grasp/") {
|
if strings.HasPrefix(remote, "nip34/grasp/") {
|
||||||
if !slices.Contains(repo.Relays, nostr.NormalizeURL(remote[12:])) {
|
graspURL := rebuildGraspURLFromRemote(remote)
|
||||||
|
|
||||||
|
if !slices.Contains(repo.Relays, nostr.NormalizeURL(graspURL)) {
|
||||||
delCmd := exec.Command("git", "remote", "remove", remote)
|
delCmd := exec.Command("git", "remote", "remove", remote)
|
||||||
if dir != "" {
|
if dir != "" {
|
||||||
delCmd.Dir = dir
|
delCmd.Dir = dir
|
||||||
@@ -961,7 +989,7 @@ func gitSetupRemotes(ctx context.Context, dir string, repo nip34.Repository) {
|
|||||||
|
|
||||||
// create new remotes for each grasp server
|
// create new remotes for each grasp server
|
||||||
for _, relay := range repo.Relays {
|
for _, relay := range repo.Relays {
|
||||||
remote := "nip34/grasp/" + strings.TrimPrefix(relay, "wss://")
|
remote := gitRemoteName(relay)
|
||||||
|
|
||||||
if !slices.Contains(remotes, remote) {
|
if !slices.Contains(remotes, remote) {
|
||||||
// construct the git URL
|
// construct the git URL
|
||||||
@@ -1367,8 +1395,6 @@ func figureOutBranches(c *cli.Command, refspec string, isPush bool) (
|
|||||||
return localBranch, remoteBranch, nil
|
return localBranch, remoteBranch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func graspServerHost(s string) string { return strings.SplitN(nostr.NormalizeURL(s), "/", 3)[2] }
|
|
||||||
|
|
||||||
type Nip34Config struct {
|
type Nip34Config struct {
|
||||||
Identifier string `json:"identifier"`
|
Identifier string `json:"identifier"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
@@ -1380,6 +1406,26 @@ type Nip34Config struct {
|
|||||||
Maintainers []string `json:"maintainers"`
|
Maintainers []string `json:"maintainers"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RepositoryToConfig(repo nip34.Repository) Nip34Config {
|
||||||
|
config := Nip34Config{
|
||||||
|
Identifier: repo.ID,
|
||||||
|
Name: repo.Name,
|
||||||
|
Description: repo.Description,
|
||||||
|
Web: repo.Web,
|
||||||
|
Owner: nip19.EncodeNpub(repo.Event.PubKey),
|
||||||
|
GraspServers: make([]string, 0, len(repo.Relays)),
|
||||||
|
EarliestUniqueCommit: repo.EarliestUniqueCommitID,
|
||||||
|
Maintainers: make([]string, 0, len(repo.Maintainers)),
|
||||||
|
}
|
||||||
|
for _, r := range repo.Relays {
|
||||||
|
config.GraspServers = append(config.GraspServers, graspServerHost(r))
|
||||||
|
}
|
||||||
|
for _, m := range repo.Maintainers {
|
||||||
|
config.Maintainers = append(config.Maintainers, nip19.EncodeNpub(m))
|
||||||
|
}
|
||||||
|
return config
|
||||||
|
}
|
||||||
|
|
||||||
func (localConfig Nip34Config) Validate() error {
|
func (localConfig Nip34Config) Validate() error {
|
||||||
_, err := parsePubKey(localConfig.Owner)
|
_, err := parsePubKey(localConfig.Owner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1430,3 +1476,18 @@ func (localConfig Nip34Config) ToRepository() nip34.Repository {
|
|||||||
|
|
||||||
return localRepo
|
return localRepo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func gitRemoteName(graspURL string) string {
|
||||||
|
host := graspServerHost(graspURL)
|
||||||
|
host = strings.Replace(host, ":", "__", 1)
|
||||||
|
return "nip34/grasp/" + host
|
||||||
|
}
|
||||||
|
|
||||||
|
func rebuildGraspURLFromRemote(remoteName string) string {
|
||||||
|
host := strings.TrimPrefix(remoteName, "nip34/grasp/")
|
||||||
|
return strings.Replace(host, "__", ":", 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func graspServerHost(s string) string {
|
||||||
|
return strings.SplitN(nostr.NormalizeURL(s), "/", 3)[2]
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user