subtree-subdir-helper.sh 1021 B

123456789101112131415161718192021222324252627282930313233
  1. #!/bin/bash
  2. set -e
  3. if [ "$(git rev-parse --show-prefix)" != "" ]; then
  4. echo "Must be in root of git repo!"
  5. exit
  6. fi
  7. if [ "${1}" = "" ] || [ "${2}" = "" ] || [ "${3}" = "" ] || [ "${4}" = "" ] || [ "${5}" = "" ]; then
  8. echo "Usage: <path> <repo url> <branch> <subdir> <action>"
  9. exit
  10. fi
  11. path="${1}"
  12. repo="${2}"
  13. branch="${3}"
  14. subdir="${4}"
  15. action="${5}"
  16. prev="$(git branch --show-current)"
  17. temp="$(rev <<< "${repo%/}" | cut -d/ -f1,2 | rev | tr / -)-${branch}"
  18. fetch="_fetch-${temp}"
  19. split="_split-${temp}-$(tr / - <<< "${subdir}")"
  20. git fetch --no-tags "${repo}" "${branch}:${fetch}"
  21. git checkout "${fetch}"
  22. exec 420>&1
  23. result="$(git subtree split -P "${subdir}" -b "${split}" 2>&1 | tee /proc/self/fd/420)"
  24. if grep "is not an ancestor of commit" <<< "$result" > /dev/null; then
  25. echo "Resetting split branch..."
  26. git branch -D "${split}"
  27. git subtree split -P "${subdir}" -b "${split}"
  28. fi
  29. git checkout "${prev}"
  30. git subtree "${action}" -P "${path}" "${split}" -m "${action^} ${path} from ${repo}"