1 #!/usr/bin/env ruby
     
2 
     3 # Required modules from Ruby Std-Lib (not gems)
     
4 require 'fileutils' # For mkdir_p, cp, mv
     
5 
     6 if ARGV.length < 1
     
7   puts
     
8   puts "Usage: ratmyrepo.rb '<description>'"
     
9   puts "(Run this from the repo dir!)"
    
10   puts
    
11   exit 1
    
12 end
    
13 
    14 repo_dir = Dir.pwd
    
15 description = ARGV[0]
    
16 
    17 repo_name = File.basename(repo_dir)
    
18 
    19 puts "Rattin' up repo '#{repo_name}'"
    
20 puts "  Source: '#{repo_dir}'"
    
21 puts "  Description: '#{description}'"
    
22 
    23 # The relative git repo (probably in proj/ directory)
    
24 git_dir = "#{repo_dir}/.git"
    
25 git_desc_path = "#{git_dir}/description"
    
26 
    27 if !Dir.exist?(git_dir)
    
28   puts "ERROR: Could not find #{git_dir}."
    
29   exit 1
    
30 end
    
31 
    32 # Write repo description
    
33 puts "  Writing description to #{git_desc_path}..."
    
34 File.write(git_desc_path, description)
    
35 wrote_desc = File.read(git_desc_path)
    
36 if wrote_desc == description
    
37   puts "  (Write verified!)"
    
38 else
    
39   puts "ERROR: Written description, '#{wrote_desc}' does not match!"
    
40   puts "       Check #{git_desc_path}?"
    
41   exit 1
    
42 end
    
43 
    44 # Create bare repo
    
45 bare_dir = "#{ENV['HOME']}/repos/#{repo_name}.git"
    
46 
    47 puts "Time to create bare Git repo at '#{bare_dir}'..."
    
48 if !Dir.exist?(bare_dir)
    
49   answer = nil
    
50   until answer == 'y' or answer == 'n'
    
51     print "Proceed? (y/n) "
    
52     answer = $stdin.gets.chomp
    
53   end
    
54   if answer == 'y'
    
55     # Create dir(s)! mkdir_p creates subdirs as needed
    
56     puts "Creating '#{bare_dir}'..."
    
57     FileUtils.mkdir_p(bare_dir)
    
58     # No need to print anything here since Git says what it's doing:
    
59     `git clone --bare #{repo_dir} #{bare_dir}`
    
60   else
    
61     puts "Okay, exiting!"
    
62     exit 1
    
63   end
    
64 else
    
65   puts "ERROR: Bare dir already exists! Have you done this before?"
    
66   exit 1
    
67 end
    
68 
    69 puts "Bare repo created at '#{bare_dir}'. Listing:"
    
70 puts ""
    
71 system "ls -al #{bare_dir}"
    
72 puts ""
    
73 
    74 # Add comment to git config and have myself edit file manually...
    
75 git_config_path = "#{git_dir}/config"
    
76 origin_block = "
    
77 #[remote \"origin\"]
    
78 #    url = #{bare_dir}
    
79 #    fetch = +refs/heads/*:refs/remotes/origin/*
    
80 #[branch \"main\"]
    
81 #    remote = origin
    
82 #    merge = refs/heads/main
    
83 "
    
84 File.write(git_config_path, origin_block, mode: 'a')
    
85 puts "Wrote a replacement 'origin' block as a comment to '#{git_config_path}'."
    
86 puts "Hit [Enter] to edit the file to use the new origin."
    
87 $stdin.gets # waits for line of input
    
88 system "#{ENV['EDITOR']} #{git_config_path}"
    
89 
    90 # That should do it
    
91 puts "All done:"
    
92 puts
    
93 puts "  * Description added."
    
94 puts "  * Bare repo created."
    
95 puts "  * Repo origin updated."
    
96 puts
    
97 puts "Things to try next:"
    
98 puts
    
99 puts "  * cd #{repo_dir}"
   
100 puts "  * git push"
   
101 puts "  * git pull"
   
102 puts "  * reporat"
   
103 puts "  * or reporat && rat pub"
   
104 puts
   
105 puts "Enjoy!"