git-remote-gcrypt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. #!/bin/sh
  2. #
  3. # git-remote-gcrypt
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) version 2 or any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. # See README.rst for usage instructions
  19. set -e # errexit
  20. set -f # noglob
  21. set -C # noclobber
  22. export GITCEPTION="${GITCEPTION:-}+" # Reuse $Gref except when stacked
  23. Gref="refs/gcrypt/gitception$GITCEPTION"
  24. Gref_rbranch="refs/heads/master"
  25. Packkey_bytes=63 # nbr random bytes for packfile keys, any >= 256 bit is ok
  26. Hashtype=SHA256 # SHA512 SHA384 SHA256 SHA224 supported.
  27. Manifestfile=91bd0c092128cf2e60e1a608c31e92caf1f9c1595f83f2890ef17c0e4881aa0a
  28. Hex40="[a-f0-9]"
  29. Hex40=$Hex40$Hex40$Hex40$Hex40$Hex40$Hex40$Hex40$Hex40
  30. Hex40=$Hex40$Hex40$Hex40$Hex40$Hex40 # Match SHA-1 hexdigest
  31. Did_find_repo= # yes for connected, no for no repo
  32. Localdir="${GIT_DIR:=.git}/remote-gcrypt"
  33. Tempdir=
  34. Repoid=
  35. Refslist=
  36. Packlist=
  37. Keeplist=
  38. Extnlist=
  39. Repack_limit=25
  40. Recipients=
  41. # compat/utility functions
  42. # xfeed: The most basic output function puts $1 into the stdin of $2..$#
  43. xfeed()
  44. {
  45. local input_=
  46. input_=$1; shift
  47. "$@" <<EOF
  48. $input_
  49. EOF
  50. }
  51. xecho() { xfeed "$*" cat; }
  52. xecho_n() { xecho "$@" | tr -d \\n ; } # kill newlines
  53. echo_git() { xecho "$@" ; } # Code clarity
  54. echo_info() { xecho "gcrypt:" "$@" >&2; }
  55. echo_die() { echo_info "$@" ; exit 1; }
  56. isnull() { case "$1" in "") return 0;; *) return 1;; esac; }
  57. isnonnull() { ! isnull "$1"; }
  58. iseq() { case "$1" in "$2") return 0;; *) return 1;; esac; }
  59. isnoteq() { ! iseq "$1" "$2"; }
  60. negate() { ! "$@"; }
  61. # Execute $@ or die
  62. pipefail()
  63. {
  64. "$@" || { echo_info "'$1' failed!"; kill $$; exit 1; }
  65. }
  66. isurl() { isnull "${2%%$1://*}"; }
  67. islocalrepo() { isnull "${1##/*}" && [ ! -e "$1/HEAD" ]; }
  68. xgrep() { command grep "$@" || : ; }
  69. # setvar is used for named return variables
  70. # $1 *must* be a valid variable name, $2 is any value
  71. #
  72. # Conventions
  73. # return variable names are passed with a @ prefix
  74. # return variable functions use f_ prefix local vars
  75. # return var consumers use r_ prefix vars (or Titlecase globals)
  76. setvar()
  77. {
  78. isnull "${1##@*}" || echo_die "Missing @ for return variable: $1"
  79. eval ${1#@}=\$2
  80. }
  81. Newline="
  82. "
  83. # $1 is return var, $2 is value appended with newline separator
  84. append_to()
  85. {
  86. local f_append_tmp_=
  87. eval f_append_tmp_=\$${1#@}
  88. isnull "$f_append_tmp_" || f_append_tmp_=$f_append_tmp_$Newline
  89. setvar "$1" "$f_append_tmp_$2"
  90. }
  91. # Pick words from each line
  92. # $1 return variable name
  93. # $2 input value
  94. pick_fields_1_2()
  95. {
  96. local f_ret= f_one= f_two=
  97. while read f_one f_two _ # from << here-document
  98. do
  99. f_ret="$f_ret$f_one $f_two$Newline"
  100. done <<EOF
  101. $2
  102. EOF
  103. setvar "$1" "${f_ret#$Newline}"
  104. }
  105. # Take all lines matching $2 (full line)
  106. # $1 return variable name
  107. # $2 filter word
  108. # $3 input value
  109. # if $1 is a literal `!', the match is reversed (and arguments shift)
  110. # we instead remove all lines matching
  111. filter_to()
  112. {
  113. local f_neg= f_line= f_ret= IFS=
  114. isnoteq "$1" "!" || { f_neg=negate; shift; }
  115. IFS=$Newline
  116. for f_line in $3
  117. do
  118. $f_neg isnonnull "${f_line##$2}" || f_ret=$f_ret$f_line$Newline
  119. done
  120. setvar "$1" "${f_ret%$Newline}"
  121. }
  122. # Output the number of lines in $1
  123. line_count()
  124. {
  125. local IFS=
  126. IFS=$Newline
  127. set -- $1
  128. xecho "$#"
  129. }
  130. ## gitception part
  131. # Fetch giturl $1, file $2
  132. gitception_get()
  133. {
  134. # Take care to preserve FETCH_HEAD
  135. local ret_=: obj_id= fet_head="$GIT_DIR/FETCH_HEAD"
  136. [ -e "$fet_head" ] && command mv -f "$fet_head" "$fet_head.$$~" || :
  137. git fetch -q -f "$1" "$Gref_rbranch:$Gref" >/dev/null &&
  138. obj_id="$(git ls-tree "$Gref" | xgrep -E '\b'"$2"'$' | awk '{print $3}')" &&
  139. isnonnull "$obj_id" && git cat-file blob "$obj_id" && ret_=: ||
  140. { ret_=false && : ; }
  141. [ -e "$fet_head.$$~" ] && command mv -f "$fet_head.$$~" "$fet_head" || :
  142. $ret_
  143. }
  144. anon_commit()
  145. {
  146. GIT_AUTHOR_NAME="root" GIT_AUTHOR_EMAIL="root@localhost" \
  147. GIT_AUTHOR_DATE="1356994801 -0400" GIT_COMMITTER_NAME="root" \
  148. GIT_COMMITTER_EMAIL="root@localhost" \
  149. GIT_COMMITTER_DATE="1356994801 -0400" \
  150. git commit-tree "$@" <<EOF
  151. Initial commit
  152. EOF
  153. }
  154. # Get 'tree' from $1, change file $2 to obj id $3
  155. update_tree()
  156. {
  157. local tab_=" "
  158. # $2 is a filename from the repo format
  159. (set +e;
  160. git ls-tree "$1" | xgrep -v -E '\b'"$2"'$';
  161. xecho "100644 blob $3$tab_$2"
  162. ) | git mktree
  163. }
  164. # Put giturl $1, file $2
  165. # depends on previous GET to set $Gref and depends on PUT_FINAL later
  166. gitception_put()
  167. {
  168. local obj_id= tree_id= commit_id=
  169. obj_id=$(git hash-object -w --stdin) &&
  170. tree_id=$(update_tree "$Gref" "$2" "$obj_id") &&
  171. commit_id=$(anon_commit "$tree_id") &&
  172. git update-ref "$Gref" "$commit_id"
  173. }
  174. # Remove giturl $1, file $2
  175. # depends on previous GET like put
  176. gitception_remove()
  177. {
  178. local tree_id= commit_id= tab_=" "
  179. # $2 is a filename from the repo format
  180. tree_id=$(git ls-tree "$Gref" | xgrep -v -E '\b'"$2"'$' | git mktree) &&
  181. commit_id=$(anon_commit "$tree_id") &&
  182. git update-ref "$Gref" "$commit_id"
  183. }
  184. gitception_new_repo()
  185. {
  186. local commit_id= empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
  187. # get any file to update Gref, and if it's not updated we create empty
  188. git update-ref -d "$Gref" || :
  189. gitception_get "$1" "x" 2>/dev/null >&2 || :
  190. git rev-parse -q --verify "$Gref" >/dev/null && return 0 ||
  191. commit_id=$(anon_commit "$empty_tree") &&
  192. git update-ref "$Gref" "$commit_id"
  193. }
  194. ## end gitception
  195. # Fetch repo $1, file $2, tmpfile in $3
  196. GET()
  197. {
  198. if isurl sftp "$1"
  199. then
  200. (exec 0>&-; curl -s -S -k "$1/$2") > "$3"
  201. elif isurl rsync "$1"
  202. then
  203. (exec 0>&-; rsync -I -W "${1#rsync://}"/"$2" "$3" >&2)
  204. elif islocalrepo "$1"
  205. then
  206. cat "$1/$2" > "$3"
  207. else
  208. gitception_get "${1#gitception://}" "$2" > "$3"
  209. fi
  210. }
  211. # Put repo $1, file $2 or fail, tmpfile in $3
  212. PUT()
  213. {
  214. if isurl sftp "$1"
  215. then
  216. curl -s -S -k --ftp-create-dirs -T "$3" "$1/$2"
  217. elif isurl rsync "$1"
  218. then
  219. rsync -I -W "$3" "${1#rsync://}"/"$2" >&2
  220. elif islocalrepo "$1"
  221. then
  222. cat >| "$1/$2" < "$3"
  223. else
  224. gitception_put "${1#gitception://}" "$2" < "$3"
  225. fi
  226. }
  227. # Put all PUT changes for repo $1 at once
  228. PUT_FINAL()
  229. {
  230. if isurl sftp "$1" || islocalrepo "$1" || isurl rsync "$1"
  231. then
  232. :
  233. else
  234. git push --quiet -f "${1#gitception://}" "$Gref:$Gref_rbranch"
  235. fi
  236. }
  237. # Put directory for repo $1
  238. PUTREPO()
  239. {
  240. if isurl sftp "$1"
  241. then
  242. :
  243. elif isurl rsync "$1"
  244. then
  245. rsync -q -r --exclude='*' "$Localdir/" "${1#rsync://}" >&2
  246. elif islocalrepo "$1"
  247. then
  248. mkdir -p "$1"
  249. else
  250. gitception_new_repo "${1#gitception://}"
  251. fi
  252. }
  253. # For repo $1, delete all newline-separated files in $2
  254. REMOVE()
  255. {
  256. local fn_=
  257. if isurl sftp "$1"
  258. then
  259. # FIXME
  260. echo_info "sftp: Ignore remove request $1/$2"
  261. elif isurl rsync "$1"
  262. then
  263. xfeed "$2" rsync -I -W -v -r --delete --include-from=- \
  264. --exclude='*' "$Localdir"/ "${1#rsync://}/" >&2
  265. elif islocalrepo "$1"
  266. then
  267. for fn_ in $2; do
  268. rm -f "$1"/"$fn_"
  269. done
  270. else
  271. for fn_ in $2; do
  272. gitception_remove "${1#gitception://}" "$fn_"
  273. done
  274. fi
  275. }
  276. CLEAN_FINAL()
  277. {
  278. if isurl sftp "$1" || islocalrepo "$1" || isurl rsync "$1"
  279. then
  280. :
  281. else
  282. git update-ref -d "$Gref" || :
  283. fi
  284. }
  285. ENCRYPT()
  286. {
  287. rungpg --batch --force-mdc --compress-algo none --trust-model=always --passphrase-fd 3 -c 3<<EOF
  288. $1
  289. EOF
  290. }
  291. DECRYPT()
  292. {
  293. rungpg -q --batch --no-default-keyring --secret-keyring /dev/null \
  294. --keyring /dev/null --passphrase-fd 3 -d 3<<EOF
  295. $1
  296. EOF
  297. }
  298. # Encrypt to recipients $1
  299. PRIVENCRYPT()
  300. {
  301. set -- $1
  302. if isnonnull "$Conf_signkey"; then
  303. set -- "$@" -u "$Conf_signkey"
  304. fi
  305. rungpg --compress-algo none --trust-model=always -se "$@"
  306. }
  307. # $1 is the match for good signature, $2 is the textual signers list
  308. PRIVDECRYPT()
  309. {
  310. local status_=
  311. exec 4>&1 &&
  312. status_=$(rungpg --status-fd 3 -q -d 3>&1 1>&4) &&
  313. xfeed "$status_" grep "^\[GNUPG:\] ENC_TO " >/dev/null &&
  314. (xfeed "$status_" grep -e "$1" >/dev/null || {
  315. echo_info "Failed to verify manifest signature!" &&
  316. echo_info "Only accepting signatories: ${2:-(none)}" &&
  317. return 1
  318. })
  319. }
  320. # Generate $1 random bytes
  321. genkey()
  322. {
  323. rungpg --armor --gen-rand 1 "$1"
  324. }
  325. gpg_hash()
  326. {
  327. local hash_=
  328. hash_=$(rungpg --with-colons --print-md "$1" | tr A-F a-f)
  329. hash_=${hash_#:*:}
  330. xecho "${hash_%:}"
  331. }
  332. rungpg()
  333. {
  334. # gpg will fail to run when there is no controlling tty,
  335. # due to trying to print messages to it, even if a gpg agent is set
  336. # up. --no-tty fixes this.
  337. if [ "x$GPG_AGENT_INFO" != "x" ]; then
  338. gpg --no-tty "$@"
  339. else
  340. gpg "$@"
  341. fi
  342. }
  343. # Pass the branch/ref by pipe to git
  344. safe_git_rev_parse()
  345. {
  346. git cat-file --batch-check 2>/dev/null |
  347. xgrep -v "missing" | cut -f 1 -d ' '
  348. }
  349. make_new_repo()
  350. {
  351. echo_info "Setting up new repository"
  352. PUTREPO "$URL"
  353. # Needed assumption: the same user should have no duplicate Repoid
  354. Repoid=":id:$(genkey 15)"
  355. iseq "${NAME#gcrypt::}" "$URL" ||
  356. git config "remote.$NAME.gcrypt-id" "$Repoid"
  357. echo_info "Remote ID is $Repoid"
  358. Extnlist="extn comment"
  359. }
  360. # $1 return var for goodsig match, $2 return var for signers text
  361. read_config()
  362. {
  363. local recp_= r_keyinfo= r_keyfpr= gpg_list= cap_= conf_part= good_sig= signers_=
  364. Conf_signkey=$(git config --get "remote.$NAME.gcrypt-signingkey" '.+' ||
  365. git config --path user.signingkey || :)
  366. conf_part=$(git config --get "remote.$NAME.gcrypt-participants" '.+' ||
  367. git config --get gcrypt.participants '.+' || :)
  368. # Figure out which keys we should encrypt to or accept signatures from
  369. if isnull "$conf_part" || iseq "$conf_part" simple
  370. then
  371. signers_="(default keyring)"
  372. Recipients="--throw-keyids --default-recipient-self"
  373. good_sig="^\[GNUPG:\] GOODSIG "
  374. setvar "$1" "$good_sig"
  375. setvar "$2" "$signers_"
  376. return 0
  377. fi
  378. for recp_ in $conf_part
  379. do
  380. gpg_list=$(rungpg --with-colons --fingerprint -k "$recp_")
  381. filter_to @r_keyinfo "pub*" "$gpg_list"
  382. filter_to @r_keyfpr "fpr*" "$gpg_list"
  383. isnull "$r_keyinfo" || isnonnull "${r_keyinfo##*"$Newline"*}" ||
  384. echo_info "WARNING: '$recp_' matches multiple keys, using one"
  385. isnull "$r_keyfpr" || isnonnull "${r_keyfpr##*"$Newline"*}" ||
  386. echo_info "WARNING: '$recp_' matches multiple fingerprints, using one"
  387. r_keyinfo=${r_keyinfo%%"$Newline"*}
  388. r_keyfpr=${r_keyfpr%%"$Newline"*}
  389. keyid_=$(xfeed "$r_keyinfo" cut -f 5 -d :)
  390. fprid_=$(xfeed "$r_keyfpr" cut -f 10 -d :)
  391. isnonnull "$fprid_" &&
  392. signers_="$signers_ $keyid_" &&
  393. append_to @good_sig "^\[GNUPG:\] VALIDSIG .*$fprid_$" || {
  394. echo_info "WARNING: Skipping missing key $recp_"
  395. continue
  396. }
  397. # Check 'E'ncrypt capability
  398. cap_=$(xfeed "$r_keyinfo" cut -f 12 -d :)
  399. iseq "${cap_#*E}" "$cap_" || Recipients="$Recipients -R $keyid_"
  400. done
  401. if isnull "$Recipients"
  402. then
  403. echo_info "You have not configured any keys you can encrypt to" \
  404. "for this repository"
  405. echo_info "Use ::"
  406. echo_info " git config gcrypt.participants YOURKEYID"
  407. exit 1
  408. fi
  409. setvar "$1" "$good_sig"
  410. setvar "$2" "$signers_"
  411. }
  412. ensure_connected()
  413. {
  414. local manifest_= r_repoid= r_name= url_frag= r_sigmatch= r_signers= \
  415. tmp_manifest=
  416. if isnonnull "$Did_find_repo"
  417. then
  418. return
  419. fi
  420. Did_find_repo=no
  421. read_config @r_sigmatch @r_signers
  422. iseq "${NAME#gcrypt::}" "$URL" || r_name=$NAME
  423. if isurl gitception "$URL" && isnonnull "$r_name"; then
  424. git config "remote.$r_name.url" "gcrypt::${URL#gitception://}"
  425. echo_info "Updated URL for $r_name, gitception:// -> ()"
  426. fi
  427. # Find the URL fragment
  428. url_frag=${URL##*"#"}
  429. isnoteq "$url_frag" "$URL" || url_frag=
  430. URL=${URL%"#$url_frag"}
  431. # manifestfile -- sha224 hash if we can, else the default location
  432. if isurl sftp "$URL" || islocalrepo "$URL" || isurl rsync "$URL"
  433. then
  434. # not for gitception
  435. isnull "$url_frag" ||
  436. Manifestfile=$(xecho_n "$url_frag" | gpg_hash SHA224)
  437. else
  438. isnull "$url_frag" || Gref_rbranch="refs/heads/$url_frag"
  439. fi
  440. Repoid=
  441. isnull "$r_name" ||
  442. Repoid=$(git config "remote.$r_name.gcrypt-id" || :)
  443. tmp_manifest="$Tempdir/maniF"
  444. GET "$URL" "$Manifestfile" "$tmp_manifest" 2>/dev/null || {
  445. echo_info "Repository not found: $URL"
  446. return 0
  447. }
  448. Did_find_repo=yes
  449. echo_info "Decrypting manifest"
  450. manifest_=$(PRIVDECRYPT "$r_sigmatch" "$r_signers" < "$tmp_manifest") &&
  451. isnonnull "$manifest_" ||
  452. echo_die "Failed to decrypt manifest!"
  453. rm -f "$tmp_manifest"
  454. filter_to @Refslist "$Hex40 *" "$manifest_"
  455. filter_to @Packlist "pack :*:* *" "$manifest_"
  456. filter_to @Keeplist "keep :*:*" "$manifest_"
  457. filter_to @Extnlist "extn *" "$manifest_"
  458. filter_to @r_repoid "repo *" "$manifest_"
  459. r_repoid=${r_repoid#repo }
  460. r_repoid=${r_repoid% *}
  461. if isnull "$Repoid"
  462. then
  463. echo_info "Remote ID is $r_repoid"
  464. Repoid=$r_repoid
  465. elif isnoteq "$r_repoid" "$Repoid"
  466. then
  467. echo_info "WARNING:"
  468. echo_info "WARNING: Remote ID has changed!"
  469. echo_info "WARNING: from $Repoid"
  470. echo_info "WARNING: to $r_repoid"
  471. echo_info "WARNING:"
  472. Repoid=$r_repoid
  473. else
  474. return 0
  475. fi
  476. isnull "$r_name" || git config "remote.$r_name.gcrypt-id" "$r_repoid"
  477. }
  478. # $1 is the hash type (SHA256 etc)
  479. # $2 the pack id
  480. # $3 the key
  481. get_verify_decrypt_pack()
  482. {
  483. local rcv_id= tmp_encrypted=
  484. tmp_encrypted="$Tempdir/packF"
  485. GET "$URL" "$2" "$tmp_encrypted" &&
  486. rcv_id=$(gpg_hash "$1" < "$tmp_encrypted") &&
  487. iseq "$rcv_id" "$2" || echo_die "Packfile $2 does not match digest!"
  488. DECRYPT "$3" < "$tmp_encrypted"
  489. rm -f "$tmp_encrypted"
  490. }
  491. # download all packlines (pack :SHA256:a32abc1231) from stdin (or die)
  492. # $1 destdir (when repack, else "")
  493. get_pack_files()
  494. {
  495. local pack_id= r_pack_key_line= htype_= pack_= key_=
  496. while IFS=': ' read -r _ htype_ pack_ # <<here-document
  497. do
  498. isnonnull "$pack_" || continue
  499. # Get the Packlist line with the key
  500. pack_id=":${htype_}:$pack_"
  501. filter_to @r_pack_key_line "pack $pack_id *" "$Packlist"
  502. key_=${r_pack_key_line#pack $pack_id }
  503. if isnonnull "${pack_##$Hex40*}" ||
  504. isnoteq "$htype_" SHA256 && isnoteq "$htype_" SHA224 &&
  505. isnoteq "$htype_" SHA384 && isnoteq "$htype_" SHA512
  506. then
  507. echo_die "Packline malformed: $pack_id"
  508. fi
  509. get_verify_decrypt_pack "$htype_" "$pack_" "$key_" | \
  510. if isnull "${1:-}"
  511. then
  512. # add to local pack list
  513. git index-pack -v --stdin >/dev/null
  514. xecho "pack $pack_id" >> "$Localdir/have_packs$GITCEPTION"
  515. else
  516. git index-pack -v --stdin "$1/${pack_}.pack" >/dev/null
  517. fi
  518. done
  519. }
  520. # Download and unpack remote packfiles
  521. # $1 return var for list of packfiles to delete
  522. repack_if_needed()
  523. {
  524. local n_= m_= kline_= r_line= r_keep_packlist= r_del_list=
  525. isnonnull "$Packlist" || return 0
  526. if isnonnull "${GCRYPT_FULL_REPACK:-}"
  527. then
  528. Keeplist=
  529. Repack_limit=0
  530. fi
  531. pick_fields_1_2 @r_del_list "$Packlist"
  532. n_=$(line_count "$Packlist")
  533. m_=$(line_count "$Keeplist")
  534. if iseq 0 "$(( $Repack_limit < ($n_ - $m_) ))"; then
  535. return
  536. fi
  537. echo_info "Repacking remote $NAME, ..."
  538. mkdir "$Tempdir/pack"
  539. # Split packages to keep and to repack
  540. if isnonnull "$Keeplist"; then
  541. while read -r _ kline_ _ # <<here-document
  542. do
  543. isnonnull "$kline_" || continue
  544. filter_to @r_line "pack $kline_ *" "$Packlist"
  545. append_to @r_keep_packlist "$r_line"
  546. filter_to ! @r_del_list "pack $kline_" "$r_del_list"
  547. done <<EOF
  548. $Keeplist
  549. EOF
  550. fi
  551. xfeed "$r_del_list" get_pack_files "$Tempdir/pack/"
  552. (set +f; pipefail git verify-pack -v "$Tempdir"/pack/*.idx) |
  553. grep -E '^[0-9a-f]{40}' | cut -f 1 -d ' '
  554. Packlist=$r_keep_packlist
  555. setvar "$1" "$r_del_list"
  556. }
  557. do_capabilities()
  558. {
  559. echo_git fetch
  560. echo_git push
  561. echo_git
  562. }
  563. do_list()
  564. {
  565. local obj_id= ref_name= line_=
  566. ensure_connected
  567. xecho "$Refslist" | while read line_
  568. do
  569. isnonnull "$line_" || break
  570. obj_id=${line_%% *}
  571. ref_name=${line_##* }
  572. echo_git "$obj_id" "$ref_name"
  573. if iseq "$ref_name" "refs/heads/master"
  574. then
  575. echo_git "@refs/heads/master HEAD"
  576. fi
  577. done
  578. # end with blank line
  579. echo_git
  580. }
  581. do_fetch()
  582. {
  583. # Download packs in the manifest that don't appear in have_packs
  584. local pneed_= premote_=
  585. ensure_connected
  586. # The `+` for $GITCEPTION is pointless but we will be safe for stacking
  587. pick_fields_1_2 @premote_ "$Packlist"
  588. if [ -s "$Localdir/have_packs+" ]
  589. then
  590. pneed_=$(xfeed "$premote_" xgrep -v -x -f "$Localdir/have_packs+")
  591. else
  592. pneed_=$premote_
  593. fi
  594. xfeed "$pneed_" get_pack_files
  595. echo_git # end with blank line
  596. }
  597. # do_push PUSHARGS (multiple lines like +src:dst, with both + and src opt.)
  598. do_push()
  599. {
  600. # Security protocol:
  601. # Each git packfile is encrypted and then named for the encrypted
  602. # file's hash. The manifest is updated with the pack id.
  603. # The manifest is encrypted.
  604. local r_revlist= pack_id= key_= obj_= src_= dst_= \
  605. r_pack_delete= tmp_encrypted= tmp_objlist= tmp_manifest=
  606. ensure_connected
  607. if iseq "$Did_find_repo" "no"
  608. then
  609. make_new_repo
  610. fi
  611. if isnonnull "$Refslist"
  612. then
  613. # mark all remote refs with ^<sha-1> (if sha-1 exists locally)
  614. r_revlist=$(xfeed "$Refslist" cut -f 1 -d ' ' |
  615. safe_git_rev_parse | sed -e 's/^\(.\)/^&/')
  616. fi
  617. while IFS=: read -r src_ dst_ # << +src:dst
  618. do
  619. src_=${src_#+}
  620. filter_to ! @Refslist "$Hex40 $dst_" "$Refslist"
  621. if isnonnull "$src_"
  622. then
  623. append_to @r_revlist "$src_"
  624. obj_=$(xfeed "$src_" safe_git_rev_parse)
  625. append_to @Refslist "$obj_ $dst_"
  626. fi
  627. done <<EOF
  628. $1
  629. EOF
  630. tmp_encrypted="$Tempdir/packP"
  631. tmp_objlist="$Tempdir/objlP"
  632. {
  633. xfeed "$r_revlist" git rev-list --objects --stdin --
  634. repack_if_needed @r_pack_delete
  635. } > "$tmp_objlist"
  636. # Only send pack if we have any objects to send
  637. if [ -s "$tmp_objlist" ]
  638. then
  639. key_=$(genkey "$Packkey_bytes")
  640. pack_id=$(export GIT_ALTERNATE_OBJECT_DIRECTORIES=$Tempdir;
  641. pipefail git pack-objects --stdout < "$tmp_objlist" |
  642. pipefail ENCRYPT "$key_" |
  643. tee "$tmp_encrypted" | gpg_hash "$Hashtype")
  644. append_to @Packlist "pack :${Hashtype}:$pack_id $key_"
  645. if isnonnull "$r_pack_delete"
  646. then
  647. append_to @Keeplist "keep :${Hashtype}:$pack_id 1"
  648. fi
  649. fi
  650. # Generate manifest
  651. echo_info "Encrypting to: $Recipients"
  652. echo_info "Requesting manifest signature"
  653. tmp_manifest="$Tempdir/maniP"
  654. PRIVENCRYPT "$Recipients" > "$tmp_manifest" <<EOF
  655. $Refslist
  656. $Packlist
  657. $Keeplist
  658. repo $Repoid
  659. $Extnlist
  660. EOF
  661. # Upload pack
  662. if [ -s "$tmp_objlist" ]
  663. then
  664. PUT "$URL" "$pack_id" "$tmp_encrypted"
  665. fi
  666. # Upload manifest
  667. PUT "$URL" "$Manifestfile" "$tmp_manifest"
  668. rm -f "$tmp_encrypted"
  669. rm -f "$tmp_objlist"
  670. rm -f "$tmp_manifest"
  671. # Delete packs
  672. if isnonnull "$r_pack_delete"; then
  673. REMOVE "$URL" "$(xecho "$r_pack_delete" | \
  674. while IFS=': ' read -r _ _ pack_
  675. do
  676. isnonnull "$pack_" || continue
  677. xecho "$pack_"
  678. done)"
  679. fi
  680. PUT_FINAL "$URL"
  681. # ok all updates
  682. while IFS=: read -r src_ dst_ # << +src:dst
  683. do
  684. echo_git "ok $dst_"
  685. done <<EOF
  686. $1
  687. EOF
  688. echo_git
  689. }
  690. cleanup_tmpfiles()
  691. {
  692. if isnonnull "${Tempdir%%*."$$"}"; then
  693. echo_die "Unexpected Tempdir value: $Tempdir"
  694. fi
  695. rm -r -f -- "${Tempdir}" >&2
  696. }
  697. setup()
  698. {
  699. mkdir -p "$Localdir"
  700. # Set up a subdirectory in /tmp
  701. temp_key=$(genkey 9 | tr '/' _)
  702. Tempdir="${TMPDIR:-/tmp}/git-remote-gcrypt-${temp_key}.$$"
  703. mkdir -m 700 "${Tempdir}"
  704. trap cleanup_tmpfiles EXIT
  705. trap 'exit 1' 1 2 3 15
  706. echo_info "Development version -- Repository format MAY CHANGE"
  707. }
  708. # handle git-remote-helpers protocol
  709. gcrypt_main_loop()
  710. {
  711. local input_= input_inner= r_args= temp_key=
  712. NAME=$1 # Remote name
  713. URL=$2 # Remote URL
  714. setup
  715. while read input_
  716. do
  717. case "$input_" in
  718. capabilities)
  719. do_capabilities
  720. ;;
  721. list|list\ for-push)
  722. do_list
  723. ;;
  724. fetch\ *)
  725. r_args=${input_##fetch }
  726. while read input_inner
  727. do
  728. case "$input_inner" in
  729. fetch*)
  730. r_args= #ignored
  731. ;;
  732. *)
  733. break
  734. ;;
  735. esac
  736. done
  737. do_fetch "$r_args"
  738. ;;
  739. push\ *)
  740. r_args=${input_##push }
  741. while read input_inner
  742. do
  743. case "$input_inner" in
  744. push\ *)
  745. append_to @r_args "${input_inner#push }"
  746. ;;
  747. *)
  748. break
  749. ;;
  750. esac
  751. done
  752. do_push "$r_args"
  753. ;;
  754. ?*)
  755. echo_die "Unknown input!"
  756. ;;
  757. *)
  758. CLEAN_FINAL "$URL"
  759. exit 0
  760. ;;
  761. esac
  762. done
  763. }
  764. if [ "x$1" = x--check ]
  765. then
  766. NAME=dummy-gcrypt-check
  767. URL=$2
  768. setup
  769. ensure_connected
  770. git remote remove $NAME 2>/dev/null || true
  771. if iseq "$Did_find_repo" "no"
  772. then
  773. exit 100
  774. fi
  775. else
  776. gcrypt_main_loop "$@"
  777. fi