Mahir Git dalam 30 Hari

Hari 29: Cara Mengimport Projek Subversion ke Repositori Git

Banyak organisasi masih menggunakan Subversion (SVN) tetapi ingin berhijrah ke Git. Artikel ini menerangkan cara mengimport repositori SVN ke Git sambil mengekalkan sejarah.

Mengapa Berhijrah dari SVN ke Git?

Kelebihan Git:

  • Aliran kerja teragih
  • Operasi yang lebih pantas (setempat)
  • Pencabangan dan cantuman yang lebih baik
  • Kerja luar talian
  • Aliran kerja yang lebih fleksibel
  • Alat dan ekosistem yang lebih baik

Cabaran:

  • Keluk pembelajaran
  • Konsep yang berbeza
  • Perubahan aliran kerja
  • Penyesuaian pasukan

Strategi Migrasi

1. Import Pantas (Mudah)

Pantas tetapi kehilangan sebahagian 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. Import yang Betul (Disyorkan)

Mengekalkan pengarang, cabang, dan 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. Menggunakan svn2git (Paling Mudah)

Pasang svn2git:

# On Mac
brew install svn2git

# On Linux
gem install svn2git

Cipta fail pengarang:

# 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>

Laksanakan migrasi:

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

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

Proses Migrasi Langkah demi Langkah

Fasa 1: Penyediaan

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

Fasa 2: Uji Migrasi

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

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

Fasa 3: Cipta Fail Pengarang

Ekstrak pengarang SVN:

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

Tukar kepada format Git:

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

Fasa 4: Laksanakan Migrasi

# 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

Fasa 5: Push ke Remote Git

# 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

Mengendalikan Kes Khas

Repositori Besar

Untuk repositori besar, import secara berperingkat:

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

Mengabaikan Laluan (Paths)

Cipta .gitignore berdasarkan senarai abaikan SVN:

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

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

Fail Binari

SVN dan Git mengendalikan binari secara berbeza:

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

Jambatan SVN (git-svn)

Teruskan menggunakan SVN semasa beralih:

# 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

Aliran Kerja:

# Pull from SVN
git svn rebase

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

# Push to SVN
git svn dcommit

Tugas Selepas Migrasi

1. Sahkan Migrasi

# 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. Kemas Kini CI/CD

  • Kemas kini skrip binaan
  • Tukar URL VCS
  • Kemas kini saluran pengerahan (deployment pipelines)
  • Uji semua proses automatik

3. Latih Pasukan

  • Bengkel asas Git
  • Dokumentasi aliran kerja baharu
  • Latihan amali
  • Sokongan semasa peralihan

4. Kemas Kini Dokumentasi

  • Kemas kini README
  • Tukar arahan checkout
  • Kemas kini garis panduan sumbangan
  • Dokumenkan aliran kerja baharu

Perbandingan: Perintah SVN vs 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

Penyelesaian Masalah

Masalah: Commit Hilang

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

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

Masalah: Ralat Pemetaan Pengarang

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

Masalah: Direktori .git yang Besar

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

# Consider shallow clone for very old history

Ringkasan Hari Ini

Berhijrah dari SVN ke Git memerlukan perancangan dan pelaksanaan yang teliti. Menggunakan alat yang betul seperti svn2git dan mengekalkan pemetaan pengarang memastikan peralihan yang lancar. Luangkan masa untuk menguji migrasi, mengesahkan hasil, dan melatih pasukan anda tentang aliran kerja Git.

Izinkan saya menyusun semula perintah dan parameter Git yang dipelajari hari ini:

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