-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
publish.sh
executable file
·184 lines (163 loc) · 4.29 KB
/
publish.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
usage()
{
echo "
Usage: git nostr create <relay> [<publickey>] [options]
ex: git nostr publish --secretkey 8asfgasrg...xxxasrgqwt34 --relay ws://nostr.nostrin.gs --chunksize 50
publish a commit patch and a manifest to nostr and return the message manifest id
Options:
--secretkey nostr secret key (noscl keygen) or even (noscl key-gen|grep private|awk '{print$3}')
--relay nostr relay url
--chunksize length of base64 characters per message
--commitid commit id for publishing single commit nostr
"
}
PRIVATE="0"
CHUNKSIZE="32768"
SECRETKEY=""
RELAY=""
COMMITID=""
MANIFEST=""
SUCCESSMSGS=""
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h | --help)
usage
;;
--commitid)
COMMITID="$2"
shift
shift
;;
--relay)
RELAY="$2"
shift
shift
;;
--secretkey)
SECRETKEY="$2"
shift
shift
;;
--chunksize)
CHUNKSIZE="$2"
shift
shift
;;
*)
if [ -z "$COMMITID" ]; then
COMMITID="$1"
fi
shift
;;
esac
done
if [ "$SECRETKEY" = "" ]; then
SECRETKEY=`git config nostr.secretkey`
fi
if [ "$RELAY" = "" ]; then
RELAY=`git config nostr.relay`
fi
if [ "$SECRETKEY" = "" ]; then
usage
fi
if [ "$RELAY" = "" ]; then
usage
fi
if [ "$COMMITID" = "" ]; then
usage
fi
PUBLICKEY=`nostril --sec $SECRETKEY | jq --raw-output .pubkey`
queryManifestForAuthor () {
nostril query --kinds 7777 --authors $PUBLICKEY|
websocat $RELAY|
jq -c --raw-output '.[] '|
grep "git-nostr-publish"|
tee >(jq --raw-output .id) > /dev/null |
awk -v relay="$RELAY" '{system("nostril query -i "$1"| websocat "relay )}'|
jq '.[]'|jq -c|grep content| jq --raw-output .content|
awk '{system("echo \""$1"\" | base64 -D")}'|
jq -s 'sort_by(.sort)'
}
CHECKALREADYPUBLISHED=`queryManifestForAuthor | jq -r --arg commitid "$COMMITID" '.[] | select(.commitid == $commitid) | .id'`
if [ "$CHECKALREADYPUBLISHED" != "" ]; then
echo $COMMITID
exit 1
fi
patchesToBase64WithSortHints() {
git log --reverse |
grep --line-buffered "^commit " |
sed 's/.* //g' |
awk '{system("echo \
`git show "$1" --no-patch --oneline --no-abbrev-commit|cat|sed \"s/ .*$//g\"`\
`git format-patch --root --stdout --no-stat --minimal --full-index --break-rewrites -1 "$1"|base64`\
`git show "$1" --no-patch --oneline --no-abbrev-commit|cat|sed \"s/^[^ ]* //\"|base64`\
")}'|
nl -nrz|
awk -v chunksize="$CHUNKSIZE" '{system("echo "$3"|base64 -d|base64 -b "chunksize"|grep .| nl -nrz | sed \"s/^/"$1" "$2" "$4" /g\"")}'
# $1 commitchunksort
# $2 commitid
# $3 description base64
# $4 commitsort
# $5 content base64
}
base64WithSortHintsToNostrMessages() {
awk \
-v secretkey=$SECRETKEY \
'{system(" \
nostril \
--envelope \
--sec "secretkey" \
--kind 7777 \
--tag sort "$1$4" \
--tag commitid "$2" \
--tag description \"`echo "$3"|base64 -D`\" \
--content "$5 \
)}'
}
if [ "$SECRETKEY" = "" ]; then
echo
echo "ERROR: create messages with secret key, then you can relay them"
usage
exit 1
fi
if [ "$COMMITID" != "" ]; then
SUCCESSMSGS=`patchesToBase64WithSortHints |\
tee \
>(base64WithSortHintsToNostrMessages |\
grep --line-buffered "$COMMITID" | websocat "$RELAY" )\
>/dev/null |\
jq -c .|grep OK | jq --raw-output .[1]
`
fi
MANIFESTID=""
RETRIES=0
while [ "$MANIFESTID" = "" ]
do
sleep $((1 + 3 * $RETRIES))
RETRIES=$(( $RETRIES + 1 ))
if [ $RETRIES = 3 ]
then
RETRIES=0
MANIFESTID="failed"
echo "$COMMITID failed"
fi
MANIFEST=`echo $SUCCESSMSGS |sed 's/ /\n/g' | grep --line-buffered .|
awk -v relay="$RELAY" '{system(\
"nostril query -i "$1" | websocat "relay"|\
jq -c .|grep EVENT |jq -c .[]|grep "$1)}'|
jq --raw-output '{
id: .id,
commitid: .tags[] | select(.[0] == "commitid") | .[1],
sort: .tags[] | select(.[0] == "sort") | .[1],
description: .tags[] | select(.[0] == "description") | .[1],
}'| base64
`
MANIFESTID=`nostril --envelope --sec "$SECRETKEY" --kind 7777 --tag purpose "git-nostr-publish" --content "$MANIFEST"|\
tee\
>(websocat "$RELAY" | jq -c .|grep OK | jq --raw-output .[1])\
>/dev/null`
done
echo $COMMITID