git.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # SPDX-License-Identifier: GPL-3.0-or-later
  2. # Copyright (c) 2020-2021,2023-2024 Leah Rowe <leah@libreboot.org>
  3. # Copyright (c) 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  4. eval `setvars "" loc url bkup_url subfile subhash subrepo subrepo_bkup \
  5. depend subfile_bkup repofail`
  6. fetch_targets()
  7. {
  8. [ -n "$tree_depend" ] && [ "$tree_depend" != "$tree" ] && \
  9. x_ ./mk -f "$project" "$tree_depend"
  10. e "src/$project/$tree" d && return 0
  11. printf "Creating %s tree %s\n" "$project" "$tree"
  12. git_prep "$loc" "$loc" "$PWD/$configdir/$tree/patches" \
  13. "src/$project/$tree" u; nuke "$project/$tree" "$project/$tree"
  14. }
  15. fetch_project()
  16. {
  17. eval `setvars "" xtree tree_depend`
  18. eval `setcfg "config/git/$project/pkg.cfg"`
  19. chkvars url
  20. [ -n "$xtree" ] && x_ ./mk -f coreboot "$xtree"
  21. [ -z "$depend" ] || for d in $depend ; do
  22. printf "'%s' needs '%s'; grabbing '%s'\n" "$project" "$d" "$d"
  23. x_ ./mk -f $d
  24. done
  25. clone_project
  26. for x in config/git/*; do
  27. [ -d "$x" ] && nuke "${x##*/}" "src/${x##*/}" 2>/dev/null
  28. done; return 0
  29. }
  30. clone_project()
  31. {
  32. loc="$XBMK_CACHE/repo/$project" && singletree "$project" && \
  33. loc="src/$project"
  34. printf "Downloading project '%s' to '%s'\n" "$project" "$loc"
  35. e "$loc" d missing && remkdir "${tmpgit%/*}" && git_prep \
  36. "$url" "$bkup_url" "$PWD/config/$project/patches" "$loc"; :
  37. }
  38. git_prep()
  39. {
  40. _patchdir="$3"; _loc="$4" # $1 and $2 are gitrepo and gitrepo_backup
  41. chkvars rev; tmpclone "$1" "$2" "$tmpgit" "$rev" "$_patchdir"
  42. if singletree "$project" || [ $# -gt 4 ]; then
  43. prep_submodules "$_loc"; fi
  44. [ "$project" = "coreboot" ] && [ -n "$xtree" ] && [ $# -gt 2 ] && \
  45. [ "$xtree" != "$tree" ] && link_crossgcc "$_loc"
  46. [ "$XBMK_RELEASE" = "y" ] && \
  47. [ "$_loc" != "$XBMK_CACHE/repo/$project" ] && rmgit "$tmpgit"
  48. move_repo "$_loc"
  49. }
  50. prep_submodules()
  51. {
  52. [ -f "$mdir/module.list" ] && while read -r msrcdir; do
  53. fetch_submodule "$msrcdir"
  54. done < "$mdir/module.list"; return 0
  55. }
  56. fetch_submodule()
  57. {
  58. mcfgdir="$mdir/${1##*/}"
  59. eval `setvars "" subhash subrepo subrepo_bkup subfile subfile_bkup st`
  60. [ ! -f "$mcfgdir/module.cfg" ] || . "$mcfgdir/module.cfg" || \
  61. $err "! . $mcfgdir/module.cfg"
  62. for xt in repo file; do
  63. _seval="if [ -n \"\$sub$xt\" ] || [ -n \"\$sub${xt}_bkup\" ]"
  64. eval "$_seval; then st=\"\$st \$xt\"; fi"
  65. done
  66. st="${st# }" && [ "$st" = "repo file" ] && $err "$mdir: repo+file"
  67. [ -z "$st" ] && return 0 # subrepo/subfile not defined
  68. chkvars "sub${st}" "sub${st}_bkup" "subhash"
  69. [ "$st" = "file" ] && download "$subfile" "$subfile_bkup" \
  70. "$tmpgit/$1" "$subhash" && return 0
  71. rm -Rf "$tmpgit/$1" || $err "!rm '$mdir' '$1'"
  72. tmpclone "$subrepo" "$subrepo_bkup" "$tmpgit/$1" "$subhash" \
  73. "$mdir/${1##*/}/patches"
  74. }
  75. livepull="n"
  76. tmpclone()
  77. {
  78. [ "$repofail" = "y" ] && \
  79. printf "Cached clone failed; trying online.\n" 1>&2 && livepull="y"
  80. repofail="n"
  81. [ $# -lt 6 ] || rm -Rf "$3" || $err "git retry: !rm $3 ($1)"
  82. repodir="$XBMK_CACHE/repo/${1##*/}" && [ $# -gt 5 ] && repodir="$3"
  83. mkdir -p "$XBMK_CACHE/repo" || $err "!rmdir $XBMK_CACHE/repo"
  84. if [ "$livepull" = "y" ] && [ ! -d "$repodir" ]; then
  85. git clone $1 "$repodir" || git clone $2 "$repodir" || \
  86. $err "!clone $1 $2 $repodir $4 $5" #
  87. elif [ -d "$repodir" ] && [ $# -lt 6 ]; then
  88. git -C "$repodir" pull || sleep 3 || git -C "$repodir" pull \
  89. || sleep 3 || git -C "$repodir" pull || :
  90. fi
  91. (
  92. [ $# -gt 5 ] || git clone "$repodir" "$3" || $err "!clone $repodir $3"
  93. git -C "$3" reset --hard "$4" || $err "!reset $1 $2 $3 $4 $5"
  94. git_am_patches "$3" "$5"
  95. ) || repofail="y"
  96. [ "$repofail" = "y" ] && [ $# -lt 6 ] && tmpclone $@ retry
  97. [ "$repofail" = "y" ] && $err "!clone $1 $2 $3 $4 $5"; :
  98. }
  99. git_am_patches()
  100. {
  101. for p in "$2/"*; do
  102. [ -L "$p" ] && continue; [ -e "$p" ] || continue
  103. [ -d "$p" ] && git_am_patches "$1" "$p" && continue
  104. [ ! -f "$p" ] || git -C "$1" am "$p" || $err "$1 $2: !am $p"
  105. done; return 0
  106. }
  107. link_crossgcc()
  108. {
  109. (
  110. x_ cd "$tmpgit/util" && x_ rm -Rf crossgcc
  111. ln -s "../../$xtree/util/crossgcc" crossgcc || $err "$1: !xgcc link"
  112. ) || $err "$1: !xgcc link"
  113. }
  114. move_repo()
  115. {
  116. [ "$1" = "${1%/*}" ] || x_ mkdir -p "${1%/*}"
  117. mv "$tmpgit" "$1" || $err "git_prep: !mv $tmpgit $1"
  118. }
  119. # can delete from multi- and single-tree projects.
  120. # called from script/trees when downloading sources.
  121. nuke()
  122. {
  123. e "config/${1%/}/nuke.list" f missing || while read -r nukefile; do
  124. rmf="src/${2%/}/$nukefile" && [ -L "$rmf" ] && continue
  125. e "$rmf" e missing || rm -Rf "$rmf" || $err "!rm $rmf, ${2%/}"
  126. done < "config/${1%/}/nuke.list"; return 0
  127. }