Skip to content

Instantly share code, notes, and snippets.

@yudai
Created April 4, 2013 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yudai/5310346 to your computer and use it in GitHub Desktop.
Save yudai/5310346 to your computer and use it in GitHub Desktop.
Rewrite .git files to use a relative path in them
#!/usr/bin/env ruby
require 'fileutils'
require 'pathname'
def ensure_relative_gitdir()
%x{git ls-files --error-unmatch --stage}.split("\n").each do |line|
value = line.split(" ")
if value[0] == "160000"
FileUtils.cd(value[3]) do
gitfile = ".git"
if File.file?(gitfile) && open(gitfile).read.match(/^gitdir: (.*)$/) && Pathname.new($1).absolute?
%x{git config --local core.worktree #{Pathname.new(FileUtils.pwd).relative_path_from(Pathname.new($1))}}
open(gitfile, "w") do |file|
file.puts("gitdir: #{Pathname.new($1).relative_path_from(Pathname.new(FileUtils.pwd))}")
end
ensure_relative_gitdir()
end
end
end
end
end
ensure_relative_gitdir()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment