.subtree-subdir-helper.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. set -e
  3. bash .utils/.check-workdir.sh
  4. if [ "${1}" = "" ] || [ "${2}" = "" ] || [ "${3}" = "" ] || [ "${4}" = "" ] || [ "${5}" = "" ]; then
  5. echo "Usage: <path> <repo url> <branch> <subdir> <action>"
  6. exit
  7. fi
  8. path="${1}"
  9. repo="${2}"
  10. branch="${3}"
  11. subdir="${4}"
  12. action="${5}"
  13. prevbranch="$(git branch --show-current)"
  14. temp="$(rev <<< "${repo%/}" | cut -d/ -f1,2 | rev | tr / -)-$(tr / - <<< "${branch}")"
  15. fetch="_fetch-${temp}"
  16. split="_split-${temp}-$(tr / - <<< "${subdir}")"
  17. git fetch --no-tags "${repo}" "${branch}:${fetch}"
  18. cache="${path}/.subtree-cache/${split}"
  19. hash="$(git rev-parse ${fetch})"
  20. skip=false
  21. if [ -f "${cache}" ]; then
  22. if git diff --quiet "$(<${cache})" "${hash}" -- "${subdir}"; then
  23. skip=true
  24. fi
  25. fi
  26. ok=true
  27. if $skip; then
  28. echo "No updates, skipping expensive subtree split."
  29. else
  30. git checkout "${fetch}"
  31. exec {capture}>&1
  32. result="$(git subtree split -P "${subdir}" -b "${split}" 2>&1 | tee /proc/self/fd/$capture)"
  33. if grep "is not an ancestor of commit" <<< "$result" > /dev/null; then
  34. echo "Resetting split branch..."
  35. git branch -D "${split}"
  36. git subtree split -P "${subdir}" -b "${split}"
  37. fi
  38. if grep "^fatal: " <<< "$result" > /dev/null; then
  39. ok=false
  40. fi
  41. git checkout "${prevbranch}"
  42. if $ok; then
  43. prevhead="$(git rev-parse HEAD)"
  44. exec {capture}>&1
  45. result="$(git subtree "${action}" -P "${path}" "${split}" -m "${action^} ${path} from ${repo}" 2>&1 | tee /proc/self/fd/$capture)"
  46. cleanmerge=false
  47. if git diff --quiet && git diff --cached --quiet && git merge HEAD &> /dev/null; then
  48. cleanmerge=true
  49. fi
  50. bash .utils/.check-merge.sh "${path}" "${repo}" "${result}"
  51. if [ "${prevhead}" = "$(git rev-parse HEAD)" ] && ! $cleanmerge; then
  52. # Not a clean merge, and merge was aborted, don't save cache
  53. ok=false
  54. fi
  55. fi
  56. fi
  57. if $ok; then
  58. mkdir -p "${path}/.subtree-cache"
  59. echo "${hash}" > "${cache}"
  60. fi