doc_upgrade_git.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/bash
  2. set -eu
  3. TARGET_INFO_FILE="$(realpath $1)"
  4. if [ ! -f $TARGET_INFO_FILE ]; then
  5. echo "File $TARGET_INFO_FILE doesn't exist. Creating new one"
  6. if [ "$2" == "" ]; then
  7. echo "Expected source folder as the second argument" >&2
  8. exit
  9. fi
  10. if [ ! -d "$2" ]; then
  11. echo "Expected $2 to be a directory" >&2
  12. exit
  13. fi
  14. FROM=$(realpath $2)
  15. GIT_ROOT=$(cd $FROM; git rev-parse --show-toplevel)
  16. HEAD=$(cd $FROM; git rev-parse HEAD)
  17. # Normalize FROM relativly to the git root
  18. GIT_FROM=${FROM#$GIT_ROOT/}
  19. echo "$HEAD;$GIT_FROM" > $TARGET_INFO_FILE
  20. echo "Filled in $TARGET_INFO_FILE"
  21. exit
  22. fi
  23. IFS=';' read -r BASE_REV GIT_FROM < "$TARGET_INFO_FILE"
  24. TO=$(dirname $(realpath "$TARGET_INFO_FILE"))
  25. HEAD_REV=$(cd $TO; git rev-parse HEAD)
  26. GIT_ROOT=$(cd $TO; git rev-parse --show-toplevel)
  27. FROM="$GIT_ROOT/$GIT_FROM"
  28. GIT_TO=${TO#$GIT_ROOT/}
  29. DATETIME=$(date '+%Y-%m-%d-%H-%M-%S')
  30. echo "Base revision: $BASE_REV"
  31. echo "Head revision: $HEAD_REV"
  32. echo "Git root: $GIT_ROOT"
  33. echo "Source: $GIT_FROM"
  34. echo "Target: $GIT_TO"
  35. if [ "$(cd $TO; git status -s -u | wc -l)" != "0" ]; then
  36. echo "Target $TO has uncommited changes" >&2
  37. exit
  38. fi
  39. cd $GIT_ROOT
  40. PATCH_FILE=$(mktemp)
  41. EXPORT_DIR=$(mktemp -d)
  42. echo "Use $EXPORT_DIR export dir"
  43. git clone --no-hardlinks "$GIT_ROOT/.git" "$EXPORT_DIR"
  44. (cd $EXPORT_DIR; git reset --hard $BASE_REV)
  45. rsync -r --delete --filter='. -' "$EXPORT_DIR/$GIT_FROM/" "$TO" << EOF
  46. + /*/
  47. + *.md
  48. + toc_*.yaml
  49. - /*
  50. EOF
  51. rm -rf "$EXPORT_DIR" || true
  52. git add -A $GIT_TO
  53. git diff --cached --binary -R --relative=$GIT_TO > $PATCH_FILE
  54. git reset --hard
  55. git clean -d -f
  56. git status
  57. rsync -r --delete --filter='. -' "$FROM/" "$TO" << EOF
  58. + /*/
  59. + *.md
  60. + toc_*.yaml
  61. P _assets/*
  62. - /*
  63. EOF
  64. patch -d "$TO" -p1 -N -E --no-backup-if-mismatch --merge -i $PATCH_FILE -t || echo "Patch has conflicts. Consider to review them before commit"
  65. if [ "$(cd $TO; git status -s -u | wc -l)" != "0" ]; then
  66. echo "$HEAD_REV;$GIT_FROM" > $TARGET_INFO_FILE
  67. else
  68. echo "Nothing changed"
  69. fi
  70. if [ -v KEEP_PATCH ]; then
  71. mv "$PATCH_FILE" "$TO/$DATETIME.patch"
  72. else
  73. rm "$PATCH_FILE"
  74. fi