svn_git_init 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # From inside the SVN subdir you want to extract:
  2. # Discover URLs (works on older svn too)
  3. REPO_ROOT="$(svn info --show-item repos-root-url 2>/dev/null || svn info | awk -F': ' '/^Repository Root:/ {print $2; exit}')"
  4. CURR_URL="$(svn info --show-item url 2>/dev/null || svn info | awk -F': ' '/^URL:/ {print $2; exit}')"
  5. # Find FIRST and HEAD that touched *this* path
  6. FIRST_REV="$(svn log -q --stop-on-copy "$CURR_URL" | awk '/^r[0-9]+/ {rev=$1} END {sub(/^r/,"",rev); print rev}')"
  7. [[ -z "$FIRST_REV" ]] && FIRST_REV=1
  8. HEAD_REV="$(svn info -r HEAD "$CURR_URL" 2>/dev/null | awk -F': ' '/^Revision:/ {print $2; exit}')"
  9. [[ -z "$HEAD_REV" ]] && HEAD_REV="$(svn log -q -r HEAD "$CURR_URL" | awk '/^r[0-9]+/ {sub(/^r/,"",$1); print $1; exit}')"
  10. TOTAL=$(( HEAD_REV - FIRST_REV + 1 ))
  11. (( TOTAL > 0 )) || { echo "Nothing to import (HEAD < FIRST)"; exit 0; }
  12. TARGET_DIR="trunk-git"
  13. rm -rf -- "$TARGET_DIR"
  14. # Init against the *subdir* URL (no ignore-paths needed)
  15. git svn init --no-minimize-url --no-metadata "$CURR_URL" "$TARGET_DIR"
  16. cd "$TARGET_DIR"
  17. # Optional cosmetics
  18. git config init.defaultBranch trunk || true
  19. # Stream fetch with live percentage (no --verbose needed)
  20. echo "Importing SVN history with progress..."
  21. git svn fetch -r "${FIRST_REV}:HEAD" 2>&1 | \
  22. awk -v first="$FIRST_REV" -v last="$HEAD_REV" -v total="$TOTAL" '
  23. BEGIN { imported=0 }
  24. { print $0
  25. if ($1 ~ /^r[0-9]+$/ && $2 == "=") {
  26. rev = substr($1,2)
  27. if (rev >= first && rev <= last) {
  28. imported++
  29. pct = int((imported*100)/total)
  30. printf("\r[%3d%%] Imported %d/%d revisions (r%d)", pct, imported, total, rev) > "/dev/stderr"
  31. fflush("/dev/stderr")
  32. }
  33. }
  34. }
  35. END { printf("\n") > "/dev/stderr" }
  36. '
  37. # Optional: make this subdir the repo root (should already be, since we targeted the subdir URL)
  38. # If you still see extra path components, you can use:
  39. # if command -v git-filter-repo >/dev/null 2>&1; then
  40. # git filter-repo --force
  41. # fi
  42. # Rename branch to trunk (if needed)
  43. curr_branch="$(git symbolic-ref --short HEAD 2>/dev/null || true)"
  44. if [[ -n "$curr_branch" && "$curr_branch" != "trunk" ]]; then
  45. git branch -m trunk || true
  46. fi