gitclone 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env sh
  2. # SPDX-FileCopyrightText: 2022 Caleb La Grange <thonkpeasant@protonmail.com>
  3. # SPDX-FileCopyrightText: 2022 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. # SPDX-FileCopyrightText: 2023 Leah Rowe <leah@libreboot.org>
  5. # SPDX-License-Identifier: GPL-3.0-only
  6. name=""
  7. revision=""
  8. location=""
  9. url=""
  10. bkup_url=""
  11. tmp_dir=""
  12. main()
  13. {
  14. if [ -z "${1+x}" ]; then
  15. err 'Error: name not set'
  16. fi
  17. name=${1}
  18. read_config
  19. verify_config
  20. clone_project
  21. # clean in case of failure
  22. rm -rf ${tmp_dir} >/dev/null 2>&1 || exit 1
  23. }
  24. read_config()
  25. {
  26. awkstr=" /\{.*${name}.*}{/ {flag=1;next} /\}/{flag=0} flag { print }"
  27. while read -r line ; do
  28. set ${line} >/dev/null 2>&1
  29. case ${line} in
  30. rev:*)
  31. revision=${2}
  32. ;;
  33. loc:*)
  34. location=${2}
  35. ;;
  36. url:*)
  37. url=${2}
  38. ;;
  39. bkup_url:*)
  40. bkup_url=${2}
  41. ;;
  42. esac
  43. done << EOF
  44. $(eval "awk '${awkstr}' resources/git/revisions")
  45. EOF
  46. }
  47. verify_config()
  48. {
  49. if [ -z "${revision+x}" ]; then
  50. err 'Error: revision not set'
  51. elif [ -z "${location+x}" ]; then
  52. err 'Error: location not set'
  53. elif [ -z "${url+x}" ]; then
  54. err 'Error: url not set'
  55. fi
  56. }
  57. clone_project()
  58. {
  59. tmp_dir=$(mktemp -dt "${name}_XXXXX")
  60. git clone ${url} ${tmp_dir} || git clone ${bkup_url} ${tmp_dir} \
  61. || err "ERROR: could not download ${name}"
  62. (
  63. cd ${tmp_dir} || exit 1
  64. git reset --hard ${revision} || err "Cannot reset revision"
  65. )
  66. patch_project
  67. if [ -d "${location}" ]; then
  68. rm -Rf ${location} || exit 1
  69. fi
  70. mv ${tmp_dir} ${location} && return 0
  71. printf "ERROR: Could not copy temp file to destination.\n"
  72. err " ${tmp_dir} > ${location} check permissions"
  73. }
  74. patch_project()
  75. {
  76. patchdir="resources/${name}/patches"
  77. for patchfile in ${PWD}/${patchdir}/*.patch ; do
  78. if [ ! -f "${patchfile}" ]; then
  79. continue
  80. fi
  81. (
  82. cd ${tmp_dir} || exit 1
  83. git am ${patchfile} || err "Cannot patch project: $name"
  84. )
  85. done
  86. }
  87. usage()
  88. {
  89. cat <<- EOF
  90. Usage: ./gitclone [name]
  91. Options:
  92. name: Module name as specified in resources/git/revisions
  93. EOF
  94. }
  95. err()
  96. {
  97. printf "${@}\n"
  98. usage
  99. exit 1
  100. }
  101. main $@