subtree-subdir-helper.sh 961 B

12345678910111213141516171819202122232425262728
  1. #!/bin/bash
  2. set -e
  3. if [ "${1}" = "" ] || [ "${2}" = "" ] || [ "${3}" = "" ] || [ "${4}" = "" ] || [ "${5}" = "" ]; then
  4. echo "Usage: <path> <repo> <branch> <subdir> <action>"
  5. exit
  6. fi
  7. path="${1}"
  8. repo="${2}"
  9. branch="${3}"
  10. subdir="${4}"
  11. action="${5}"
  12. prev="$(git branch --show-current)"
  13. temp="$(rev <<< "${repo%/}" | cut -d/ -f1,2 | rev | tr / -)-${branch}"
  14. fetch="_fetch-${temp}"
  15. split="_split-${temp}-$(tr / - <<< "${subdir}")"
  16. git fetch --no-tags "${repo}" "${branch}:${fetch}"
  17. git checkout "${fetch}"
  18. git subtree split -P "${subdir}" -b "${split}"
  19. git checkout "${prev}"
  20. exec 69>&1
  21. result="$(git subtree "${action}" -P "${path}" "${split}" -m "${action^} ${path} from ${repo}" 2>&1 | tee /proc/self/fd/69)"
  22. if grep "refusing to merge unrelated histories" <<< "$result" > /dev/null; then
  23. echo "History at ${repo} is unrelated, using squash..."
  24. git subtree "${action}" -P "${path}" "${split}" -m "${action^} ${path} from ${repo}" --squash
  25. fi