git.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_ ./update trees -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_ ./update trees -f coreboot "$xtree"
  21. [ -z "$depend" ] || for d in $depend ; do
  22. printf "'%s' needs '%s'; grabbing '%s'\n" "$project" "$d" "$d"
  23. x_ ./update trees -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 && return 0
  36. remkdir "${tmpgit%/*}"
  37. git_prep "$url" "$bkup_url" "$PWD/config/$project/patches" "$loc"
  38. }
  39. git_prep()
  40. {
  41. _patchdir="$3" # $1 and $2 are gitrepo and gitrepo_backup
  42. _loc="$4"
  43. chkvars rev
  44. tmpclone "$1" "$2" "$tmpgit" "$rev" "$_patchdir"
  45. if singletree "$project" || [ $# -gt 4 ]; then
  46. prep_submodules "$_loc"
  47. fi
  48. [ "$project" = "coreboot" ] && [ -n "$xtree" ] && [ $# -gt 2 ] && \
  49. [ "$xtree" != "$tree" ] && link_crossgcc "$_loc"
  50. [ "$XBMK_RELEASE" = "y" ] && \
  51. [ "$_loc" != "$XBMK_CACHE/repo/$project" ] && \
  52. rmgit "$tmpgit"
  53. move_repo "$_loc"
  54. }
  55. prep_submodules()
  56. {
  57. [ -f "$mdir/module.list" ] && while read -r msrcdir; do
  58. fetch_submodule "$msrcdir"
  59. done < "$mdir/module.list"; return 0
  60. }
  61. fetch_submodule()
  62. {
  63. mcfgdir="$mdir/${1##*/}"
  64. eval `setvars "" subhash subrepo subrepo_bkup subfile subfile_bkup st`
  65. [ ! -f "$mcfgdir/module.cfg" ] || . "$mcfgdir/module.cfg" || \
  66. $err "! . $mcfgdir/module.cfg"
  67. for xt in repo file; do
  68. _seval="if [ -n \"\$sub$xt\" ] || [ -n \"\$sub${xt}_bkup\" ]"
  69. eval "$_seval; then st=\"\$st \$xt\"; fi"
  70. done
  71. st="${st# }" && [ "$st" = "repo file" ] && $err "$mdir: repo+file"
  72. [ -z "$st" ] && return 0 # subrepo/subfile not defined
  73. chkvars "sub${st}" "sub${st}_bkup" "subhash"
  74. [ "$st" = "file" ] && download "$subfile" "$subfile_bkup" \
  75. "$tmpgit/$1" "$subhash" && return 0
  76. rm -Rf "$tmpgit/$1" || $err "!rm '$mdir' '$1'"
  77. tmpclone "$subrepo" "$subrepo_bkup" "$tmpgit/$1" "$subhash" \
  78. "$mdir/${1##*/}/patches"
  79. }
  80. tmpclone()
  81. {
  82. [ "$repofail" = "y" ] && \
  83. printf "Cached clone failed; trying online.\n" 1>&2
  84. repofail="n"
  85. [ $# -lt 6 ] || rm -Rf "$3" || $err "git retry: !rm $3 ($1)"
  86. repodir="$XBMK_CACHE/repo/${1##*/}" && [ $# -gt 5 ] && repodir="$3"
  87. mkdir -p "$XBMK_CACHE/repo" || $err "!rmdir $XBMK_CACHE/repo"
  88. if [ -d "$repodir" ] && [ $# -lt 6 ]; then
  89. git -C "$repodir" pull || sleep 3 || git -C "$repodir" pull \
  90. || sleep 3 || git -C "$repodir" pull || :
  91. else
  92. git clone $1 "$repodir" || git clone $2 "$repodir" || \
  93. $err "!clone $1 $2 $repodir $4 $5"
  94. fi
  95. (
  96. [ $# -gt 5 ] || git clone "$repodir" "$3" || $err "!clone $repodir $3"
  97. git -C "$3" reset --hard "$4" || $err "!reset $1 $2 $3 $4 $5"
  98. git_am_patches "$3" "$5"
  99. ) || repofail="y"
  100. [ "$repofail" = "y" ] && [ $# -lt 6 ] && tmpclone $@ retry
  101. [ "$repofail" = "y" ] && $err "!clone $1 $2 $3 $4 $5"; :
  102. }
  103. git_am_patches()
  104. {
  105. for p in "$2/"*; do
  106. [ -L "$p" ] && continue; [ -e "$p" ] || continue
  107. [ -d "$p" ] && git_am_patches "$1" "$p" && continue
  108. [ ! -f "$p" ] || git -C "$1" am "$p" || $err "$1 $2: !am $p"
  109. done; return 0
  110. }
  111. link_crossgcc()
  112. {
  113. (
  114. x_ cd "$tmpgit/util" && x_ rm -Rf crossgcc
  115. ln -s "../../$xtree/util/crossgcc" crossgcc || $err "$1: !xgcc link"
  116. ) || $err "$1: !xgcc link"
  117. }
  118. move_repo()
  119. {
  120. [ "$1" = "${1%/*}" ] || x_ mkdir -p "${1%/*}"
  121. mv "$tmpgit" "$1" || $err "git_prep: !mv $tmpgit $1"
  122. }
  123. # can delete from multi- and single-tree projects.
  124. # called from script/trees when downloading sources.
  125. nuke()
  126. {
  127. e "config/${1%/}/nuke.list" f missing || while read -r nukefile; do
  128. rmf="src/${2%/}/$nukefile" && [ -L "$rmf" ] && continue
  129. e "$rmf" e missing || rm -Rf "$rmf" || $err "!rm $rmf, ${2%/}"
  130. done < "config/${1%/}/nuke.list"; return 0
  131. }