git.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "" _target rev _xm loc url bkup_url depend tree_depend xtree \
  5. mdir subhash subrepo subrepo_bkup subfile subfile_bkup)"
  6. fetch_project_trees()
  7. {
  8. _target="$target"
  9. [ ! -d "src/$project/$project" ] && x_ mkdir -p "src/$project" \
  10. && fetch_project_repo "$project"
  11. fetch_config
  12. e "src/$project/$tree" d || prepare_new_tree; return 0
  13. }
  14. fetch_config()
  15. {
  16. rm -f "$cfgsdir/"*/seen || $err "fetch_config $cfgsdir: !rm seen"
  17. eval "$(setvars "" xtree tree_depend)"
  18. while true; do
  19. eval "$(setvars "" rev tree)"
  20. _xm="fetch_config $project/$_target"
  21. load_target_config "$_target"
  22. [ "$_target" = "$tree" ] && break
  23. _target="$tree"
  24. done
  25. [ -n "$tree_depend" ] && [ "$tree_depend" != "$tree" ] && \
  26. x_ ./update trees -f "$project" "$tree_depend"; return 0
  27. }
  28. load_target_config()
  29. {
  30. [ -f "$cfgsdir/$1/target.cfg" ] || $err "$1: target.cfg missing"
  31. [ -f "$cfgsdir/$1/seen" ] && $err "$_xm cfg: infinite loop in trees"
  32. . "$cfgsdir/$1/target.cfg" || $err "load_target_config !$cfgsdir/$1"
  33. touch "$cfgsdir/$1/seen" || $err "load_config $cfgsdir/$1: !mk seen"
  34. }
  35. prepare_new_tree()
  36. {
  37. printf "Creating %s tree %s (%s)\n" "$project" "$tree" "$_target"
  38. git_prep "src/$project/$project" "src/$project/$project" \
  39. "$PWD/$cfgsdir/$tree/patches" "src/$project/$tree" "update"
  40. nuke "$project/$tree" "$project/$tree"
  41. }
  42. fetch_project_repo()
  43. {
  44. eval "$(setvars "" xtree tree_depend)"
  45. scan_config "$project" "config/git"
  46. [ -z "${loc+x}" ] && $err "fetch_project_repo $project: loc not set"
  47. [ -z "${url+x}" ] && $err "fetch_project_repo $project: url not set"
  48. [ -n "$xtree" ] && [ ! -d "src/coreboot/$xtree" ] && \
  49. x_ ./update trees -f coreboot "$xtree"
  50. [ -z "$depend" ] || for d in $depend ; do
  51. printf "'%s' needs dependency '%s'; grabbing '%s' now\n" \
  52. "$project" "$d" "$d"
  53. x_ ./update trees -f $d
  54. done
  55. clone_project
  56. for x in config/git/*; do
  57. [ -f "$x" ] && nuke "${x##*/}" "src/${x##*/}" 2>/dev/null; \
  58. continue
  59. done
  60. }
  61. clone_project()
  62. {
  63. loc="${loc#src/}"
  64. loc="src/$loc"
  65. printf "Downloading project '%s' to '%s'\n" "$project" "$loc"
  66. e "$loc" d && return 0
  67. remkdir "${tmpgit%/*}"
  68. git_prep "$url" "$bkup_url" "$PWD/config/$project/patches" "$loc"
  69. }
  70. git_prep()
  71. {
  72. _patchdir="$3" # $1 and $2 are gitrepo and gitrepo_backup
  73. _loc="$4"
  74. [ -z "${rev+x}" ] && $err "git_prep $_loc: rev not set"
  75. tmpclone "$1" "$2" "$tmpgit" "$rev" "$_patchdir"
  76. if singletree "$project" || [ $# -gt 4 ]; then
  77. prep_submodules "$_loc"
  78. fi
  79. [ "$project" = "coreboot" ] && [ -n "$xtree" ] && [ $# -gt 2 ] && \
  80. [ "$xtree" != "$tree" ] && link_crossgcc "$_loc"
  81. [ "$xbmk_release" = "y" ] && [ "$_loc" != "src/$project/$project" ] \
  82. && rmgit "$tmpgit"
  83. move_repo "$_loc"
  84. }
  85. prep_submodules()
  86. {
  87. mdir="$PWD/config/submodule/$project"
  88. [ -n "$tree" ] && mdir="$mdir/$tree"
  89. [ -f "$mdir/module.list" ] && while read -r msrcdir; do
  90. fetch_submodule "$msrcdir"
  91. done < "$mdir/module.list"; return 0
  92. }
  93. fetch_submodule()
  94. {
  95. mcfgdir="$mdir/${1##*/}"
  96. eval "$(setvars "" subhash subrepo subrepo_bkup subfile subfile_bkup)"
  97. [ ! -f "$mcfgdir/module.cfg" ] || . "$mcfgdir/module.cfg" || \
  98. $err "! . $mcfgdir/module.cfg"
  99. st=""
  100. for _st in repo file; do
  101. _seval="if [ -n \"\$sub$_st\" ] || [ -n \"\$sub${_st}_bkup\" ]"
  102. eval "$_seval; then st=\"\$st \$_st\"; fi"
  103. done
  104. st="${st# }"
  105. [ "$st" = "repo file" ] && $err "$mdir: repo/file both defined"
  106. [ -z "$st" ] && return 0 # subrepo/subfile not defined
  107. for mvar in "sub${st}" "sub${st}_bkup" "subhash"; do
  108. eval "[ -n \"\$$mvar\" ] || $err \"$1, $mdir: $mvar unset\""
  109. done
  110. if [ "$st" = "repo" ]; then
  111. rm -Rf "$tmpgit/$1" || $err "!rm '$mdir' '$1'"
  112. tmpclone "$subrepo" "$subrepo_bkup" "$tmpgit/$1" "$subhash" \
  113. "$mdir/${1##*/}/patches"
  114. else
  115. download "$subfile" "$subfile_bkup" "$tmpgit/$1" "$subhash"
  116. fi
  117. }
  118. tmpclone()
  119. {
  120. git clone $1 "$3" || git clone $2 "$3" || $err "!clone $1 $2 $3 $4 $5"
  121. git -C "$3" reset --hard "$4" || $err "!reset $1 $2 $3 $4 $5"
  122. git_am_patches "$3" "$5"
  123. }
  124. git_am_patches()
  125. {
  126. for _patch in "$2/"*; do
  127. [ -L "$_patch" ] || [ ! -f "$_patch" ] || git -C "$1" am \
  128. "$_patch" || $err "$1 $2: !git am $_patch"; continue
  129. done
  130. for _patches in "$2/"*; do
  131. [ ! -L "$_patches" ] && [ -d "$_patches" ] && \
  132. git_am_patches "$1" "$_patches"; continue
  133. done
  134. }
  135. link_crossgcc()
  136. {
  137. (
  138. x_ cd "$tmpgit/util" && x_ rm -Rf crossgcc
  139. ln -s "../../$xtree/util/crossgcc" crossgcc || $err "$1: !xgcc link"
  140. ) || $err "$1: !xgcc link"
  141. }
  142. move_repo()
  143. {
  144. [ "$1" = "${1%/*}" ] || x_ mkdir -p "${1%/*}"
  145. mv "$tmpgit" "$1" || $err "git_prep: !mv $tmpgit $1"
  146. }
  147. # can delete from multi- and single-tree projects.
  148. # called from script/trees when downloading sources.
  149. nuke()
  150. {
  151. e "config/${1%/}/nuke.list" f missing || while read -r nukefile; do
  152. rmf="src/${2%/}/$nukefile" && [ -L "$rmf" ] && continue
  153. e "$rmf" e missing || rm -Rf "$rmf" || $err "!rm $rmf, ${2%/}"
  154. done < "config/${1%/}/nuke.list"; return 0
  155. }