🤖🚫 AI-free content. This post is 100% written by a human, as is everything on my blog. Enjoy!

Скрипт для переноса пустых папок из SVN в git

July 6, 2009, revised February 14, 2016 in Software

Update 2016-02-14: оказывается, этот скрипт больше не нужен! Используй опцию --preserve-empty-dirs при вызове команды git svn clone, и папки перенесутся автоматически.

Update 2016-02-09: наконец, исправил совместимость с Ruby 1.9.

Баг: .gitignore заставляет гит игнорировать вложенные категории. Потому надо создавать пустой .gitignore и потом вручную прописывать игнор там, где надо

#!/usr/bin/ruby
#
# script to extract empty dir messages from git-svn log and create the missing directories
# Leonid Shevtsov <leonid@shevtsov.me>
#
# TODO parameterize the script
# TODO make it edit revisions instead of just creating directories in the current commit

prefix = 'trunk/' # TODO make this a parameter
branch = 'trunk'  # TODO make this a parameter

dirs = []
`grep empty_dir < .git/svn/#{branch}/unhandled.log`.each_line do |str|
  matchdata = /(\+|-)empty_dir: #{prefix}(.+)/.match str

  if matchdata[1] == '+'
    dirs <<  matchdata[2]
  else
    dirs.delete matchdata[2]
  end
end

dirs.uniq!

dirs.each do |dir|
  curdir = '.'
  dir.split('/').each do |subdir|
    curdir += '/'+subdir
    next if File.directory? curdir
    if File.file? curdir
      puts "#{curdir}: is a file and cannot be created as a directory"
    else
      Dir.mkdir curdir
      File.open curdir+'/.gitignore','w' do |f|
        f.write "*\n!.gitignore\n"
      end
      puts "#{curdir}/.gitignore: created"
    end
  end
end

puts "Done! Now run git status and check which files need to be kept"

Подробнее о переносе SVN-репозитария в git

Buy me a coffee Понравился пост? Купи мне кофе