git.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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)"
  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. while true; do
  28. eval "$(setvars "" rev tree)"
  29. _xm="fetch_config ${project}/${_target}"
  30. load_target_config "${_target}"
  31. [ "${_target}" = "${tree}" ] && break
  32. _target="${tree}"
  33. done
  34. }
  35. load_target_config()
  36. {
  37. [ -f "$cfgsdir/$1/target.cfg" ] || err "$1: target.cfg missing"
  38. [ -f "${cfgsdir}/${1}/seen" ] && \
  39. err "${_xm} check: infinite loop in tree definitions"
  40. . "$cfgsdir/$1/target.cfg" || err "load_target_config !$cfgsdir/$1"
  41. touch "$cfgsdir/$1/seen" || err "load_config $cfgsdir/$1: !mk seen"
  42. }
  43. prepare_new_tree()
  44. {
  45. printf "Creating %s tree %s (%s)\n" "$project" "$tree" "$_target"
  46. cp -R "src/${project}/${project}" "${tmpgit}" || \
  47. err "prepare_new_tree ${project}/${tree}: can't make tmpclone"
  48. git_prep "$PWD/$cfgsdir/$tree/patches" "src/$project/$tree" "update"
  49. }
  50. fetch_project_repo()
  51. {
  52. scan_config "${project}" "config/git" "err"
  53. [ -z "${loc+x}" ] && err "fetch_project_repo $project: loc not set"
  54. [ -z "${url+x}" ] && err "fetch_project_repo $project: url not set"
  55. clone_project
  56. [ -z "${depend}" ] || for d in ${depend} ; do
  57. x_ ./update trees -f ${d}
  58. done
  59. rm -Rf "${tmpgit}" || err "fetch_repo: !rm -Rf ${tmpgit}"
  60. }
  61. clone_project()
  62. {
  63. loc="${loc#src/}"
  64. loc="src/${loc}"
  65. if [ -d "${loc}" ]; then
  66. printf "%s already exists, so skipping download\n" "$loc" 1>&2
  67. return 0
  68. fi
  69. git clone $url "$tmpgit" || git clone $bkup_url "$tmpgit" \
  70. || err "clone_project: could not download ${project}"
  71. git_prep "$PWD/config/$project/patches" "$loc"
  72. }
  73. git_prep()
  74. {
  75. _patchdir="$1"
  76. _loc="$2"
  77. [ -z "${rev+x}" ] && err "git_prep $_loc: rev not set"
  78. git -C "$tmpgit" reset --hard $rev || err "git -C $_loc: !reset $rev"
  79. git_am_patches "$tmpgit" "$_patchdir" || err "!am $_loc $_patchdir"
  80. [ $# -lt 3 ] || [ ! -f "$tmpgit/.gitmodules" ] || \
  81. git -C "$tmpgit" submodule update --init || err "prep $_loc: !mod"
  82. [ "$_loc" = "${_loc%/*}" ] || x_ mkdir -p "${_loc%/*}"
  83. mv "$tmpgit" "$_loc" || err "git_prep: !mv $tmpgit $_loc"
  84. }
  85. git_am_patches()
  86. {
  87. for _patch in "$2/"*; do
  88. [ -L "$_patch" ] || [ ! -f "$_patch" ] || git -C "$1" am \
  89. "$_patch" || err "git_am $1 $2: !git am $_patch"; continue
  90. done
  91. for _patches in "$2/"*; do
  92. [ ! -L "$_patches" ] && [ -d "$_patches" ] && \
  93. git_am_patches "$1" "$_patches"; continue
  94. done
  95. }