git-remote-gcrypt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. Conf_pubish_participants=$(git config --get --bool "remote.$NAME.gcrypt-publish-participants" '.+' ||
  369. git config --get --bool gcrypt.publish-participants || :)
  370. # Figure out which keys we should encrypt to or accept signatures from
  371. if isnull "$conf_part" || iseq "$conf_part" simple
  372. then
  373. signers_="(default keyring)"
  374. Recipients="--throw-keyids --default-recipient-self"
  375. good_sig="^\[GNUPG:\] GOODSIG "
  376. setvar "$1" "$good_sig"
  377. setvar "$2" "$signers_"
  378. return 0
  379. fi
  380. for recp_ in $conf_part
  381. do
  382. gpg_list=$(rungpg --with-colons --fingerprint -k "$recp_")
  383. filter_to @r_keyinfo "pub*" "$gpg_list"
  384. filter_to @r_keyfpr "fpr*" "$gpg_list"
  385. isnull "$r_keyinfo" || isnonnull "${r_keyinfo##*"$Newline"*}" ||
  386. echo_info "WARNING: '$recp_' matches multiple keys, using one"
  387. isnull "$r_keyfpr" || isnonnull "${r_keyfpr##*"$Newline"*}" ||
  388. echo_info "WARNING: '$recp_' matches multiple fingerprints, using one"
  389. r_keyinfo=${r_keyinfo%%"$Newline"*}
  390. r_keyfpr=${r_keyfpr%%"$Newline"*}
  391. keyid_=$(xfeed "$r_keyinfo" cut -f 5 -d :)
  392. fprid_=$(xfeed "$r_keyfpr" cut -f 10 -d :)
  393. isnonnull "$fprid_" &&
  394. signers_="$signers_ $keyid_" &&
  395. append_to @good_sig "^\[GNUPG:\] VALIDSIG .*$fprid_$" || {
  396. echo_info "WARNING: Skipping missing key $recp_"
  397. continue
  398. }
  399. # Check 'E'ncrypt capability
  400. cap_=$(xfeed "$r_keyinfo" cut -f 12 -d :)
  401. if ! iseq "${cap_#*E}" "$cap_"; then
  402. if [ "$Conf_pubish_participants" = true ]; then
  403. Recipients="$Recipients -r $keyid_"
  404. else
  405. Recipients="$Recipients -R $keyid_"
  406. fi
  407. fi
  408. done
  409. if isnull "$Recipients"
  410. then
  411. echo_info "You have not configured any keys you can encrypt to" \
  412. "for this repository"
  413. echo_info "Use ::"
  414. echo_info " git config gcrypt.participants YOURKEYID"
  415. exit 1
  416. fi
  417. setvar "$1" "$good_sig"
  418. setvar "$2" "$signers_"
  419. }
  420. ensure_connected()
  421. {
  422. local manifest_= r_repoid= r_name= url_frag= r_sigmatch= r_signers= \
  423. tmp_manifest=
  424. if isnonnull "$Did_find_repo"
  425. then
  426. return
  427. fi
  428. Did_find_repo=no
  429. read_config @r_sigmatch @r_signers
  430. iseq "${NAME#gcrypt::}" "$URL" || r_name=$NAME
  431. if isurl gitception "$URL" && isnonnull "$r_name"; then
  432. git config "remote.$r_name.url" "gcrypt::${URL#gitception://}"
  433. echo_info "Updated URL for $r_name, gitception:// -> ()"
  434. fi
  435. # Find the URL fragment
  436. url_frag=${URL##*"#"}
  437. isnoteq "$url_frag" "$URL" || url_frag=
  438. URL=${URL%"#$url_frag"}
  439. # manifestfile -- sha224 hash if we can, else the default location
  440. if isurl sftp "$URL" || islocalrepo "$URL" || isurl rsync "$URL"
  441. then
  442. # not for gitception
  443. isnull "$url_frag" ||
  444. Manifestfile=$(xecho_n "$url_frag" | gpg_hash SHA224)
  445. else
  446. isnull "$url_frag" || Gref_rbranch="refs/heads/$url_frag"
  447. fi
  448. Repoid=
  449. isnull "$r_name" ||
  450. Repoid=$(git config "remote.$r_name.gcrypt-id" || :)
  451. tmp_manifest="$Tempdir/maniF"
  452. GET "$URL" "$Manifestfile" "$tmp_manifest" 2>/dev/null || {
  453. echo_info "Repository not found: $URL"
  454. return 0
  455. }
  456. Did_find_repo=yes
  457. echo_info "Decrypting manifest"
  458. manifest_=$(PRIVDECRYPT "$r_sigmatch" "$r_signers" < "$tmp_manifest") &&
  459. isnonnull "$manifest_" ||
  460. echo_die "Failed to decrypt manifest!"
  461. rm -f "$tmp_manifest"
  462. filter_to @Refslist "$Hex40 *" "$manifest_"
  463. filter_to @Packlist "pack :*:* *" "$manifest_"
  464. filter_to @Keeplist "keep :*:*" "$manifest_"
  465. filter_to @Extnlist "extn *" "$manifest_"
  466. filter_to @r_repoid "repo *" "$manifest_"
  467. r_repoid=${r_repoid#repo }
  468. r_repoid=${r_repoid% *}
  469. if isnull "$Repoid"
  470. then
  471. echo_info "Remote ID is $r_repoid"
  472. Repoid=$r_repoid
  473. elif isnoteq "$r_repoid" "$Repoid"
  474. then
  475. echo_info "WARNING:"
  476. echo_info "WARNING: Remote ID has changed!"
  477. echo_info "WARNING: from $Repoid"
  478. echo_info "WARNING: to $r_repoid"
  479. echo_info "WARNING:"
  480. Repoid=$r_repoid
  481. else
  482. return 0
  483. fi
  484. isnull "$r_name" || git config "remote.$r_name.gcrypt-id" "$r_repoid"
  485. }
  486. # $1 is the hash type (SHA256 etc)
  487. # $2 the pack id
  488. # $3 the key
  489. get_verify_decrypt_pack()
  490. {
  491. local rcv_id= tmp_encrypted=
  492. tmp_encrypted="$Tempdir/packF"
  493. GET "$URL" "$2" "$tmp_encrypted" &&
  494. rcv_id=$(gpg_hash "$1" < "$tmp_encrypted") &&
  495. iseq "$rcv_id" "$2" || echo_die "Packfile $2 does not match digest!"
  496. DECRYPT "$3" < "$tmp_encrypted"
  497. rm -f "$tmp_encrypted"
  498. }
  499. # download all packlines (pack :SHA256:a32abc1231) from stdin (or die)
  500. # $1 destdir (when repack, else "")
  501. get_pack_files()
  502. {
  503. local pack_id= r_pack_key_line= htype_= pack_= key_=
  504. while IFS=': ' read -r _ htype_ pack_ # <<here-document
  505. do
  506. isnonnull "$pack_" || continue
  507. # Get the Packlist line with the key
  508. pack_id=":${htype_}:$pack_"
  509. filter_to @r_pack_key_line "pack $pack_id *" "$Packlist"
  510. key_=${r_pack_key_line#pack $pack_id }
  511. if isnonnull "${pack_##$Hex40*}" ||
  512. isnoteq "$htype_" SHA256 && isnoteq "$htype_" SHA224 &&
  513. isnoteq "$htype_" SHA384 && isnoteq "$htype_" SHA512
  514. then
  515. echo_die "Packline malformed: $pack_id"
  516. fi
  517. get_verify_decrypt_pack "$htype_" "$pack_" "$key_" | \
  518. if isnull "${1:-}"
  519. then
  520. # add to local pack list
  521. git index-pack -v --stdin >/dev/null
  522. xecho "pack $pack_id" >> "$Localdir/have_packs$GITCEPTION"
  523. else
  524. git index-pack -v --stdin "$1/${pack_}.pack" >/dev/null
  525. fi
  526. done
  527. }
  528. # Download and unpack remote packfiles
  529. # $1 return var for list of packfiles to delete
  530. repack_if_needed()
  531. {
  532. local n_= m_= kline_= r_line= r_keep_packlist= r_del_list=
  533. isnonnull "$Packlist" || return 0
  534. if isnonnull "${GCRYPT_FULL_REPACK:-}"
  535. then
  536. Keeplist=
  537. Repack_limit=0
  538. fi
  539. pick_fields_1_2 @r_del_list "$Packlist"
  540. n_=$(line_count "$Packlist")
  541. m_=$(line_count "$Keeplist")
  542. if iseq 0 "$(( $Repack_limit < ($n_ - $m_) ))"; then
  543. return
  544. fi
  545. echo_info "Repacking remote $NAME, ..."
  546. mkdir "$Tempdir/pack"
  547. # Split packages to keep and to repack
  548. if isnonnull "$Keeplist"; then
  549. while read -r _ kline_ _ # <<here-document
  550. do
  551. isnonnull "$kline_" || continue
  552. filter_to @r_line "pack $kline_ *" "$Packlist"
  553. append_to @r_keep_packlist "$r_line"
  554. filter_to ! @r_del_list "pack $kline_" "$r_del_list"
  555. done <<EOF
  556. $Keeplist
  557. EOF
  558. fi
  559. xfeed "$r_del_list" get_pack_files "$Tempdir/pack/"
  560. (set +f; pipefail git verify-pack -v "$Tempdir"/pack/*.idx) |
  561. grep -E '^[0-9a-f]{40}' | cut -f 1 -d ' '
  562. Packlist=$r_keep_packlist
  563. setvar "$1" "$r_del_list"
  564. }
  565. do_capabilities()
  566. {
  567. echo_git fetch
  568. echo_git push
  569. echo_git
  570. }
  571. do_list()
  572. {
  573. local obj_id= ref_name= line_=
  574. ensure_connected
  575. xecho "$Refslist" | while read line_
  576. do
  577. isnonnull "$line_" || break
  578. obj_id=${line_%% *}
  579. ref_name=${line_##* }
  580. echo_git "$obj_id" "$ref_name"
  581. if iseq "$ref_name" "refs/heads/master"
  582. then
  583. echo_git "@refs/heads/master HEAD"
  584. fi
  585. done
  586. # end with blank line
  587. echo_git
  588. }
  589. do_fetch()
  590. {
  591. # Download packs in the manifest that don't appear in have_packs
  592. local pneed_= premote_=
  593. ensure_connected
  594. # The `+` for $GITCEPTION is pointless but we will be safe for stacking
  595. pick_fields_1_2 @premote_ "$Packlist"
  596. if [ -s "$Localdir/have_packs+" ]
  597. then
  598. pneed_=$(xfeed "$premote_" xgrep -v -x -f "$Localdir/have_packs+")
  599. else
  600. pneed_=$premote_
  601. fi
  602. xfeed "$pneed_" get_pack_files
  603. echo_git # end with blank line
  604. }
  605. # do_push PUSHARGS (multiple lines like +src:dst, with both + and src opt.)
  606. do_push()
  607. {
  608. # Security protocol:
  609. # Each git packfile is encrypted and then named for the encrypted
  610. # file's hash. The manifest is updated with the pack id.
  611. # The manifest is encrypted.
  612. local r_revlist= pack_id= key_= obj_= src_= dst_= \
  613. r_pack_delete= tmp_encrypted= tmp_objlist= tmp_manifest=
  614. ensure_connected
  615. if iseq "$Did_find_repo" "no"
  616. then
  617. make_new_repo
  618. fi
  619. if isnonnull "$Refslist"
  620. then
  621. # mark all remote refs with ^<sha-1> (if sha-1 exists locally)
  622. r_revlist=$(xfeed "$Refslist" cut -f 1 -d ' ' |
  623. safe_git_rev_parse | sed -e 's/^\(.\)/^&/')
  624. fi
  625. while IFS=: read -r src_ dst_ # << +src:dst
  626. do
  627. src_=${src_#+}
  628. filter_to ! @Refslist "$Hex40 $dst_" "$Refslist"
  629. if isnonnull "$src_"
  630. then
  631. append_to @r_revlist "$src_"
  632. obj_=$(xfeed "$src_" safe_git_rev_parse)
  633. append_to @Refslist "$obj_ $dst_"
  634. fi
  635. done <<EOF
  636. $1
  637. EOF
  638. tmp_encrypted="$Tempdir/packP"
  639. tmp_objlist="$Tempdir/objlP"
  640. {
  641. xfeed "$r_revlist" git rev-list --objects --stdin --
  642. repack_if_needed @r_pack_delete
  643. } > "$tmp_objlist"
  644. # Only send pack if we have any objects to send
  645. if [ -s "$tmp_objlist" ]
  646. then
  647. key_=$(genkey "$Packkey_bytes")
  648. pack_id=$(export GIT_ALTERNATE_OBJECT_DIRECTORIES=$Tempdir;
  649. pipefail git pack-objects --stdout < "$tmp_objlist" |
  650. pipefail ENCRYPT "$key_" |
  651. tee "$tmp_encrypted" | gpg_hash "$Hashtype")
  652. append_to @Packlist "pack :${Hashtype}:$pack_id $key_"
  653. if isnonnull "$r_pack_delete"
  654. then
  655. append_to @Keeplist "keep :${Hashtype}:$pack_id 1"
  656. fi
  657. fi
  658. # Generate manifest
  659. echo_info "Encrypting to: $Recipients"
  660. echo_info "Requesting manifest signature"
  661. tmp_manifest="$Tempdir/maniP"
  662. PRIVENCRYPT "$Recipients" > "$tmp_manifest" <<EOF
  663. $Refslist
  664. $Packlist
  665. $Keeplist
  666. repo $Repoid
  667. $Extnlist
  668. EOF
  669. # Upload pack
  670. if [ -s "$tmp_objlist" ]
  671. then
  672. PUT "$URL" "$pack_id" "$tmp_encrypted"
  673. fi
  674. # Upload manifest
  675. PUT "$URL" "$Manifestfile" "$tmp_manifest"
  676. rm -f "$tmp_encrypted"
  677. rm -f "$tmp_objlist"
  678. rm -f "$tmp_manifest"
  679. # Delete packs
  680. if isnonnull "$r_pack_delete"; then
  681. REMOVE "$URL" "$(xecho "$r_pack_delete" | \
  682. while IFS=': ' read -r _ _ pack_
  683. do
  684. isnonnull "$pack_" || continue
  685. xecho "$pack_"
  686. done)"
  687. fi
  688. PUT_FINAL "$URL"
  689. # ok all updates
  690. while IFS=: read -r src_ dst_ # << +src:dst
  691. do
  692. echo_git "ok $dst_"
  693. done <<EOF
  694. $1
  695. EOF
  696. echo_git
  697. }
  698. cleanup_tmpfiles()
  699. {
  700. if isnonnull "${Tempdir%%*."$$"}"; then
  701. echo_die "Unexpected Tempdir value: $Tempdir"
  702. fi
  703. rm -r -f -- "${Tempdir}" >&2
  704. }
  705. setup()
  706. {
  707. mkdir -p "$Localdir"
  708. # Set up a subdirectory in /tmp
  709. temp_key=$(genkey 9 | tr '/' _)
  710. Tempdir="${TMPDIR:-/tmp}/git-remote-gcrypt-${temp_key}.$$"
  711. mkdir -m 700 "${Tempdir}"
  712. trap cleanup_tmpfiles EXIT
  713. trap 'exit 1' 1 2 3 15
  714. echo_info "Development version -- Repository format MAY CHANGE"
  715. }
  716. # handle git-remote-helpers protocol
  717. gcrypt_main_loop()
  718. {
  719. local input_= input_inner= r_args= temp_key=
  720. NAME=$1 # Remote name
  721. URL=$2 # Remote URL
  722. setup
  723. while read input_
  724. do
  725. case "$input_" in
  726. capabilities)
  727. do_capabilities
  728. ;;
  729. list|list\ for-push)
  730. do_list
  731. ;;
  732. fetch\ *)
  733. r_args=${input_##fetch }
  734. while read input_inner
  735. do
  736. case "$input_inner" in
  737. fetch*)
  738. r_args= #ignored
  739. ;;
  740. *)
  741. break
  742. ;;
  743. esac
  744. done
  745. do_fetch "$r_args"
  746. ;;
  747. push\ *)
  748. r_args=${input_##push }
  749. while read input_inner
  750. do
  751. case "$input_inner" in
  752. push\ *)
  753. append_to @r_args "${input_inner#push }"
  754. ;;
  755. *)
  756. break
  757. ;;
  758. esac
  759. done
  760. do_push "$r_args"
  761. ;;
  762. ?*)
  763. echo_die "Unknown input!"
  764. ;;
  765. *)
  766. CLEAN_FINAL "$URL"
  767. exit 0
  768. ;;
  769. esac
  770. done
  771. }
  772. if [ "x$1" = x--check ]
  773. then
  774. NAME=dummy-gcrypt-check
  775. URL=$2
  776. setup
  777. ensure_connected
  778. git remote remove $NAME 2>/dev/null || true
  779. if iseq "$Did_find_repo" "no"
  780. then
  781. exit 100
  782. fi
  783. else
  784. gcrypt_main_loop "$@"
  785. fi