svn_git_sync 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env bash
  2. # svn_git_sync — sync current SVN working copy into a Git mirror
  3. # Usage:
  4. # svn_git_sync init [--history=N] [mirror_name]
  5. set -euo pipefail
  6. show_usage() {
  7. echo "Usage: $0 init [--history=N] [mirror_name]" >&2
  8. exit 1
  9. }
  10. subcommand="${1:-}"
  11. [[ -n "$subcommand" ]] || show_usage
  12. shift
  13. history_count=0
  14. mirror_arg=""
  15. parse_opts() {
  16. while [[ $# -gt 0 ]]; do
  17. case "$1" in
  18. --history=*)
  19. history_count="${1#*=}"
  20. shift
  21. ;;
  22. --history)
  23. history_count="${2:-}"
  24. shift 2
  25. ;;
  26. -h|--help)
  27. show_usage
  28. ;;
  29. *)
  30. mirror_arg="$1"
  31. shift
  32. break
  33. ;;
  34. esac
  35. done
  36. if [[ -n "$mirror_arg" && $# -gt 0 ]]; then
  37. echo "Unexpected extra arguments: $*" >&2
  38. show_usage
  39. fi
  40. }
  41. case "$subcommand" in
  42. init)
  43. parse_opts "$@"
  44. ;;
  45. -h|--help|help)
  46. show_usage
  47. ;;
  48. *)
  49. echo "Unknown command: $subcommand" >&2
  50. show_usage
  51. ;;
  52. esac
  53. SRC_DIR="$(pwd)"
  54. MIRROR_ROOT="${GIT_SVN_MIRROR_ROOT:-$HOME/git_svn_mirror}"
  55. # Derive a collision-resistant mirror name from the full path (unless overridden).
  56. raw_name="${mirror_arg:-$(pwd -P)}"
  57. BASE_NAME="$(printf "%s" "$raw_name" | sed 's#^[\\/]*##; s#[\\/]#_#g')"
  58. DEST="${MIRROR_ROOT}/${BASE_NAME}"
  59. echo "Exporting SVN working copy to $DEST (versioned files only)..."
  60. rm -rf -- "$DEST"
  61. mkdir -p "$MIRROR_ROOT"
  62. start_ts="$(date +%s.%N)"
  63. elapsed() {
  64. local now
  65. now="${1:-$(date +%s.%N)}"
  66. awk -v s="$start_ts" -v n="$now" 'BEGIN{split(s,sa,".");split(n,na,".");printf "%.1f", (na[1]-sa[1]) + (na[2]-sa[2])/1e9}'
  67. }
  68. simple_export() {
  69. # Optionally compute total items upfront for a rough percentage
  70. local total_items=""
  71. if total=$(svn list -R . 2>/dev/null | wc -l || true); then
  72. total_items="$total"
  73. fi
  74. svn export --quiet . "$DEST" &
  75. export_pid=$!
  76. local last_reported=-1
  77. while kill -0 "$export_pid" 2>/dev/null; do
  78. if [[ -d "$DEST" ]]; then
  79. current=$(find "$DEST" -mindepth 1 -print | wc -l)
  80. else
  81. current=0
  82. fi
  83. if [[ "$current" -ne "$last_reported" ]]; then
  84. elapsed_val="$(elapsed)"
  85. pct=""
  86. if [[ -n "$total_items" && "$total_items" -gt 0 ]]; then
  87. pct=$(awk -v c="$current" -v t="$total_items" 'BEGIN{printf(" (%.1f%%)", (c*100)/t)}')
  88. fi
  89. printf "[%ss] Exported %d items%s\n" "$elapsed_val" "$current" "$pct"
  90. last_reported="$current"
  91. fi
  92. sleep 1
  93. done
  94. wait "$export_pid"
  95. final_count=$(find "$DEST" -mindepth 1 -print | wc -l 2>/dev/null || echo 0)
  96. printf "[%ss] Exported %d items (done)\n" "$(elapsed)" "$final_count"
  97. }
  98. init_git_repo() {
  99. echo "[$(elapsed) s] Initializing git repository..."
  100. GIT_QUIET=1 git init >/dev/null
  101. git config init.defaultBranch trunk || true
  102. git config core.fileMode false || true
  103. git config core.autocrlf true || true
  104. git config core.safecrlf false || true
  105. # Seed ignores for common noise (pyc/cache)
  106. if [[ ! -e .gitignore ]]; then
  107. echo "[$(elapsed) s] Adding default ignores (.pyc/__pycache__)..."
  108. printf "*.pyc\n__pycache__/\n" > .gitignore
  109. fi
  110. }
  111. link_git_to_source() {
  112. echo "[$(elapsed) s] Linking git metadata to source working tree..."
  113. DEST_ABS="$(pwd -P)"
  114. cd "$SRC_DIR"
  115. if [[ -e .git || -L .git ]]; then
  116. echo "[$(elapsed)s] Replacing existing .git in $SRC_DIR..."
  117. rm -rf -- .git
  118. fi
  119. printf "gitdir: %s/.git\n" "$DEST_ABS" > .git
  120. echo "[$(elapsed)s] Linked .git to $DEST_ABS/.git"
  121. echo "[$(elapsed) s] Syncing working tree to repo content..."
  122. GIT_QUIET=1 git checkout -q -- .
  123. }
  124. stage_and_commit() {
  125. local msg="$1"
  126. echo "[$(elapsed) s] Staging files..."
  127. git add -A
  128. echo "[$(elapsed) s] Creating commit: $msg"
  129. GIT_QUIET=1 git commit -q -m "$msg" || true
  130. }
  131. # Validate history_count is numeric (default 0)
  132. if [[ -z "${history_count:-}" ]]; then
  133. history_count=0
  134. elif ! [[ "$history_count" =~ ^[0-9]+$ ]]; then
  135. echo "--history must be a non-negative integer" >&2
  136. exit 1
  137. fi
  138. if (( history_count == 0 )); then
  139. simple_export
  140. cd "$DEST"
  141. init_git_repo
  142. stage_and_commit "Import SVN snapshot"
  143. link_git_to_source
  144. exit 0
  145. fi
  146. # History mode: pull last N revisions into Git.
  147. head_rev="$(svn info --show-item revision 2>/dev/null || svn info | awk -F': ' '/^Revision:/ {print $2; exit}')"
  148. if [[ -z "$head_rev" ]]; then
  149. echo "Unable to determine HEAD revision" >&2
  150. exit 1
  151. fi
  152. start_rev=$(( head_rev - history_count + 1 ))
  153. if (( start_rev < 1 )); then start_rev=1; fi
  154. echo "Including last $history_count revisions (r${start_rev}..r${head_rev})..."
  155. STAGE_DIR="$DEST/.stage"
  156. mkdir -p "$DEST" "$STAGE_DIR"
  157. cd "$DEST"
  158. init_git_repo
  159. for rev in $(seq "$start_rev" "$head_rev"); do
  160. echo "[$(elapsed) s] Exporting r${rev}..."
  161. rm -rf -- "$STAGE_DIR"
  162. mkdir -p "$STAGE_DIR"
  163. svn export --quiet -r "$rev" "$SRC_DIR" "$STAGE_DIR"
  164. # Sync exported snapshot into repo working tree, preserving .git and .gitignore
  165. rsync -a --delete --exclude '.git' --exclude '.gitignore' "$STAGE_DIR"/ "$DEST"/
  166. # Ensure ignores persist
  167. if [[ ! -e .gitignore ]]; then
  168. printf "*.pyc\n__pycache__/\n" > .gitignore
  169. fi
  170. if [[ "$rev" -eq "$start_rev" ]]; then
  171. stage_and_commit "Import SVN r${rev}"
  172. else
  173. stage_and_commit "Update to SVN r${rev}"
  174. fi
  175. done
  176. link_git_to_source