git.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # SPDX-License-Identifier: GPL-3.0-or-later
  2. # SPDX-FileCopyrightText: 2020,2021,2023,2024 Leah Rowe <leah@libreboot.org>
  3. # SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  4. # This file is only used by update/project/trees
  5. eval "$(setvars "" _target rev _xm loc url bkup_url depend tree_depend xtree)"
  6. fetch_project_trees()
  7. {
  8. _target="${target}"
  9. [ -d "src/${project}/${project}" ] || fetch_from_upstream
  10. fetch_config
  11. if [ -d "src/${project}/${tree}" ]; then
  12. printf "download/%s %s (%s): exists\n" \
  13. "${project}" "${tree}" "${_target}" 1>&2
  14. return 0
  15. fi
  16. prepare_new_tree
  17. }
  18. fetch_from_upstream()
  19. {
  20. [ -d "src/${project}/${project}" ] && return 0
  21. x_ mkdir -p "src/${project}"
  22. fetch_project_repo "${project}"
  23. }
  24. fetch_config()
  25. {
  26. rm -f "${cfgsdir}/"*/seen || err "fetch_config ${cfgsdir}: !rm seen"
  27. eval "$(setvars "" xtree tree_depend)"
  28. while true; do
  29. eval "$(setvars "" rev tree)"
  30. _xm="fetch_config ${project}/${_target}"
  31. load_target_config "${_target}"
  32. [ "${_target}" = "${tree}" ] && break
  33. _target="${tree}"
  34. done
  35. [ -n "$tree_depend" ] && [ "$tree_depend" != "$tree" ] && \
  36. x_ ./update trees -f "$project" "$tree_depend"; return 0
  37. }
  38. load_target_config()
  39. {
  40. [ -f "$cfgsdir/$1/target.cfg" ] || err "$1: target.cfg missing"
  41. [ -f "${cfgsdir}/${1}/seen" ] && \
  42. err "${_xm} check: infinite loop in tree definitions"
  43. . "$cfgsdir/$1/target.cfg" || err "load_target_config !$cfgsdir/$1"
  44. touch "$cfgsdir/$1/seen" || err "load_config $cfgsdir/$1: !mk seen"
  45. }
  46. prepare_new_tree()
  47. {
  48. printf "Creating %s tree %s (%s)\n" "$project" "$tree" "$_target"
  49. cp -R "src/${project}/${project}" "${tmpgit}" || \
  50. err "prepare_new_tree ${project}/${tree}: can't make tmpclone"
  51. git_prep "$PWD/$cfgsdir/$tree/patches" "src/$project/$tree" "update"
  52. }
  53. fetch_project_repo()
  54. {
  55. eval "$(setvars "" xtree tree_depend)"
  56. scan_config "${project}" "config/git" "err"
  57. [ -z "${loc+x}" ] && err "fetch_project_repo $project: loc not set"
  58. [ -z "${url+x}" ] && err "fetch_project_repo $project: url not set"
  59. clone_project
  60. [ -z "${depend}" ] || for d in ${depend} ; do
  61. x_ ./update trees -f ${d}
  62. done
  63. rm -Rf "${tmpgit}" || err "fetch_repo: !rm -Rf ${tmpgit}"
  64. }
  65. clone_project()
  66. {
  67. loc="${loc#src/}"
  68. loc="src/${loc}"
  69. if [ -d "${loc}" ]; then
  70. printf "%s already exists, so skipping download\n" "$loc" 1>&2
  71. return 0
  72. fi
  73. git clone $url "$tmpgit" || git clone $bkup_url "$tmpgit" \
  74. || err "clone_project: could not download ${project}"
  75. git_prep "$PWD/config/$project/patches" "$loc"
  76. }
  77. git_prep()
  78. {
  79. _patchdir="$1"
  80. _loc="$2"
  81. [ -z "${rev+x}" ] && err "git_prep $_loc: rev not set"
  82. git -C "$tmpgit" reset --hard $rev || err "git -C $_loc: !reset $rev"
  83. git_am_patches "$tmpgit" "$_patchdir" || err "!am $_loc $_patchdir"
  84. if [ "$project" != "coreboot" ] || [ $# -gt 2 ]; then
  85. [ ! -f "$tmpgit/.gitmodules" ] || git -C "$tmpgit" submodule \
  86. update --init --checkout || err "git_prep $_loc: !submod"
  87. if [ "$project" = "coreboot" ] && [ -n "$xtree" ] && \
  88. [ "$xtree" != "$tree" ]; then
  89. (
  90. cd "$tmpgit/util" || err "prep $_loc: !cd $tmpgit/util"
  91. rm -Rf crossgcc || err "prep $_loc: !rm xgcc"
  92. ln -s "../../$xtree/util/crossgcc" crossgcc || \
  93. err "prep $_loc: can't create xgcc symlink"
  94. ) || err "prep $_loc: can't create xgcc symlink"
  95. fi
  96. fi
  97. [ "$_loc" = "${_loc%/*}" ] || x_ mkdir -p "${_loc%/*}"
  98. mv "$tmpgit" "$_loc" || err "git_prep: !mv $tmpgit $_loc"
  99. [ -n "$xtree" ] && [ ! -d "src/coreboot/$xtree" ] && \
  100. x_ ./update project trees -f coreboot "$xtree"; return 0
  101. }
  102. git_am_patches()
  103. {
  104. for _patch in "$2/"*; do
  105. [ -L "$_patch" ] || [ ! -f "$_patch" ] || git -C "$1" am \
  106. "$_patch" || err "git_am $1 $2: !git am $_patch"; continue
  107. done
  108. for _patches in "$2/"*; do
  109. [ ! -L "$_patches" ] && [ -d "$_patches" ] && \
  110. git_am_patches "$1" "$_patches"; continue
  111. done
  112. }