เชี่ยวชาญ Git ใน 30 วัน

วันที่ 29: วิธีนำเข้าโปรเจกต์ Subversion เข้าสู่ Git Repository

หลายองค์กรยังคงใช้ Subversion (SVN) แต่อยากย้ายไปใช้ Git บทความนี้จะอธิบายวิธีนำเข้า SVN Repository ไปยัง Git โดยรักษาประวัติไว้

ทำไมต้องย้ายจาก SVN ไป Git?

ข้อดีของ Git:

  • Workflow แบบกระจายศูนย์ (Distributed)
  • การทำงานเร็วกว่า (ในเครื่อง)
  • Branch และ Merge ที่ดีกว่า
  • ทำงานแบบ Offline ได้
  • Workflow ยืดหยุ่นกว่า
  • เครื่องมือและระบบนิเวศที่ดีกว่า

ความท้าทาย:

  • ต้องใช้เวลาเรียนรู้
  • แนวคิดที่แตกต่างกัน
  • การเปลี่ยนแปลง Workflow
  • การปรับตัวของทีม

กลยุทธ์การย้าย

1. นำเข้าอย่างรวดเร็ว (แบบง่าย)

รวดเร็ว แต่สูญเสีย Metadata ของ SVN บางส่วน:

# Clone entire SVN repository
git svn clone http://svn.example.com/repo

# Or specify trunk/branches/tags structure
git svn clone http://svn.example.com/repo --trunk=trunk --branches=branches --tags=tags

2. นำเข้าอย่างถูกต้อง (แนะนำ)

รักษาผู้เขียน Branch และ Tag ไว้:

# Create authors file
# Map SVN users to Git users
echo "svnuser1 = John Doe <john@example.com>" >> authors.txt
echo "svnuser2 = Jane Smith <jane@example.com>" >> authors.txt

# Clone with authors file
git svn clone http://svn.example.com/repo \
  --authors-file=authors.txt \
  --trunk=trunk \
  --branches=branches \
  --tags=tags \
  --no-metadata

3. ใช้ svn2git (ง่ายที่สุด)

ติดตั้ง svn2git:

# On Mac
brew install svn2git

# On Linux
gem install svn2git

สร้างไฟล์ Authors:

# Generate authors list from SVN
svn log -q http://svn.example.com/repo | \
  awk -F '|' '/^r/ {print $2}' | \
  sort -u > authors.txt

# Edit authors.txt to add email addresses
# Format: svnuser = Full Name <email@example.com>

ดำเนินการย้าย:

# Create new directory
mkdir git-repo
cd git-repo

# Run migration
svn2git http://svn.example.com/repo \
  --authors authors.txt

กระบวนการย้ายทีละขั้นตอน

ระยะที่ 1: การเตรียมการ

1. Identify SVN repository structure
2. Create authors mapping file
3. Decide on Git repository structure
4. Plan branch/tag strategy
5. Communicate with team

ระยะที่ 2: ทดสอบการย้าย

# Do test migration first
git svn clone http://svn.example.com/repo test-migration

# Verify:
- Commit history
- Branches
- Tags
- File contents

ระยะที่ 3: สร้างไฟล์ Authors

ดึงรายชื่อผู้เขียนจาก SVN:

svn log -q http://svn.example.com/repo | \
  grep "^r" | \
  awk '{print $3}' | \
  sort -u > svn-authors.txt

แปลงเป็นรูปแบบของ Git:

# authors.txt
jdoe = John Doe <john.doe@example.com>
jsmith = Jane Smith <jane.smith@example.com>
admin = Administrator <admin@example.com>

ระยะที่ 4: ดำเนินการย้าย

# Clone SVN repository
git svn clone http://svn.example.com/repo \
  --authors-file=authors.txt \
  --stdlayout \
  migrated-repo

cd migrated-repo

# Convert SVN tags to Git tags
git for-each-ref refs/remotes/tags | cut -d / -f 4- | \
while read tagname; do
  git tag "$tagname" "refs/remotes/tags/$tagname"
  git branch -r -d "tags/$tagname"
done

# Convert SVN branches to Git branches
git for-each-ref refs/remotes/origin | cut -d / -f 4- | \
while read branchname; do
  git branch "$branchname" "refs/remotes/origin/$branchname"
done

# Remove SVN remote
git remote rm origin

ระยะที่ 5: Push ไปยัง Git Remote

# Add Git remote
git remote add origin https://github.com/your-org/repo.git

# Push all branches
git push -u origin --all

# Push all tags
git push -u origin --tags

การจัดการกรณีพิเศษ

Repository ขนาดใหญ่

สำหรับ Repository ขนาดใหญ่ ให้นำเข้าแบบค่อยเป็นค่อยไป:

# Start from revision 1000
git svn clone http://svn.example.com/repo \
  -r 1000:HEAD \
  --authors-file=authors.txt

การละเว้น Path

สร้าง .gitignore จาก SVN Ignores:

# Extract SVN ignores
git svn show-ignore > .gitignore

# Commit
git add .gitignore
git commit -m "Add .gitignore from SVN"

ไฟล์ Binary

SVN และ Git จัดการไฟล์ Binary ต่างกัน:

# For large binary files, consider Git LFS
git lfs install
git lfs track "*.psd"
git lfs track "*.zip"

SVN Bridge (git-svn)

ใช้ SVN ต่อไปในระหว่างการเปลี่ยนผ่าน:

# Clone with git-svn
git svn clone http://svn.example.com/repo

# Work normally with Git locally

# Get SVN updates
git svn rebase

# Push to SVN
git svn dcommit

Workflow:

# Pull from SVN
git svn rebase

# Make changes
git add .
git commit -m "Changes"

# Push to SVN
git svn dcommit

งานหลังการย้าย

1. ตรวจสอบการย้าย

# Check commit count
git log --oneline | wc -l

# Compare with SVN
# Should match or be close

# Check specific files
git log -- path/to/file.txt
svn log path/to/file.txt

# Verify branches
git branch -a

# Verify tags
git tag

2. อัปเดต CI/CD

  • อัปเดต Build Script
  • เปลี่ยน URL ของ VCS
  • อัปเดต Deployment Pipeline
  • ทดสอบกระบวนการอัตโนมัติทั้งหมด

3. ฝึกอบรมทีม

  • เวิร์กช็อปพื้นฐาน Git
  • เอกสาร Workflow ใหม่
  • ฝึกปฏิบัติจริง
  • ให้การสนับสนุนระหว่างการเปลี่ยนผ่าน

4. อัปเดตเอกสาร

  • อัปเดต README
  • เปลี่ยนคำแนะนำการ Checkout
  • อัปเดตแนวทางการมีส่วนร่วม
  • บันทึก Workflow ใหม่เป็นเอกสาร

เปรียบเทียบ: คำสั่ง SVN เทียบกับ Git

# Checkout
SVN: svn checkout
Git: git clone

# Update
SVN: svn update
Git: git pull

# Commit
SVN: svn commit
Git: git commit + git push

# Status
SVN: svn status
Git: git status

# Log
SVN: svn log
Git: git log

# Branch
SVN: svn copy .../trunk .../branches/name
Git: git checkout -b name

# Tag
SVN: svn copy .../trunk .../tags/v1.0
Git: git tag v1.0

# Merge
SVN: svn merge
Git: git merge

# Diff
SVN: svn diff
Git: git diff

การแก้ไขปัญหา

ปัญหา: Commit หายไป

# Check SVN revision range
git svn log | head -n 20

# Re-clone from specific revision
git svn clone -r START:HEAD ...

ปัญหา: ข้อผิดพลาดในการ Map ผู้เขียน

# Add missing authors to authors.txt
# Re-run migration

ปัญหา: โฟลเดอร์ .git ขนาดใหญ่

# Garbage collection
git gc --aggressive --prune=now

# Consider shallow clone for very old history

สรุปของวันนี้

การย้ายจาก SVN ไป Git ต้องอาศัยการวางแผนและการดำเนินการอย่างรอบคอบ การใช้เครื่องมือที่เหมาะสมอย่าง svn2git และการรักษา Author Mapping ไว้จะช่วยให้การเปลี่ยนผ่านเป็นไปอย่างราบรื่น ใช้เวลาทดสอบการย้าย ตรวจสอบผลลัพธ์ และฝึกอบรมทีมของคุณเกี่ยวกับ Workflow ของ Git

ขอผมเรียบเรียงคำสั่งและพารามิเตอร์ Git ที่เรียนรู้ในวันนี้อีกครั้ง:

  • git svn clone
  • git svn rebase
  • git svn dcommit
  • git svn show-ignore
  • git for-each-ref
  • git gc –aggressive