Class: GitReclone
- Inherits:
-
Object
- Object
- GitReclone
- Defined in:
- lib/git_reclone/version.rb,
lib/git_reclone.rb
Overview
universal version tracking
Constant Summary collapse
- HELP =
<<~HELP.freeze #{'git reclone'.red}: a git repo restoring tool replaces your local copy with a fresh clone from the remote. clones to a temp directory first, so your local copy is safe if the clone fails. to restore from a particular remote repository, specify the host: git reclone github # reclone using github git reclone bitbucket # reclone using bitbucket git reclone gitea # reclone using gitea git reclone gogs # reclone using gogs HELP
- VERSION =
'1.0.1'
Instance Method Summary collapse
- #current_branch ⇒ Object
- #fire(args = []) ⇒ Object
- #git_root ⇒ Object
-
#initialize(test = false) ⇒ GitReclone
constructor
A new instance of GitReclone.
- #no_repo? ⇒ Boolean
- #parse_arg(a) ⇒ Object
- #parse_opt(o) ⇒ Object
- #pexit(msg) ⇒ Object
-
#reclone(remote, root, branch = nil) ⇒ Object
overwrite the local copy of the repository with the remote one.
- #reclonebanner ⇒ Object
-
#remote(search = /.*/) ⇒ Object
trying to parse out which remote should be the new source.
- #remotes ⇒ Object
- #slowp(x) ⇒ Object
-
#verify(r) ⇒ Object
show remote to user and confirm location (unless using -f).
Constructor Details
#initialize(test = false) ⇒ GitReclone
Returns a new instance of GitReclone.
16 17 18 19 20 |
# File 'lib/git_reclone.rb', line 16 def initialize(test = false) @pdelay = 0.01 # constant for arrow speed @testing = test @verify = !test end |
Instance Method Details
#current_branch ⇒ Object
58 59 60 |
# File 'lib/git_reclone.rb', line 58 def current_branch `git rev-parse --abbrev-ref HEAD`.chomp end |
#fire(args = []) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/git_reclone.rb', line 22 def fire(args = []) opts = args.select { |a| a[0] == '-' } opts.each { |o| parse_opt o } exit 0 if @testing || opts.first parse_arg((args - opts).first) end |
#git_root ⇒ Object
54 55 56 |
# File 'lib/git_reclone.rb', line 54 def git_root `git rev-parse --show-toplevel` end |
#no_repo? ⇒ Boolean
49 50 51 52 |
# File 'lib/git_reclone.rb', line 49 def no_repo? `git status 2>&1`.split("\n").first == 'fatal: Not a git repository (or any of the parent directories): .git' end |
#parse_arg(a) ⇒ Object
45 46 47 |
# File 'lib/git_reclone.rb', line 45 def parse_arg(a) a.nil? ? verify(remote) : verify(remote(a)) end |
#parse_opt(o) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/git_reclone.rb', line 34 def parse_opt(o) case o when '--force', '-f' @verify = false when '--help', '-h' puts GitReclone::HELP when '--version', '-v' puts GitReclone::VERSION end end |
#pexit(msg) ⇒ Object
29 30 31 32 |
# File 'lib/git_reclone.rb', line 29 def pexit(msg) puts msg exit 1 end |
#reclone(remote, root, branch = nil) ⇒ Object
overwrite the local copy of the repository with the remote one
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/git_reclone.rb', line 116 def reclone(remote, root, branch = nil) # create a temporary directory for cloning temp_dir = Dir.mktmpdir('git-reclone-') begin # clone into temp directory (disable credential prompts) cloner = "GIT_TERMINAL_PROMPT=0 git clone \"#{remote}\" \"#{temp_dir}\" > /dev/null 2>&1" if system(cloner) # clone succeeded, now replace the local copy unless @testing tree = Dir.glob('*', File::FNM_DOTMATCH).reject { |d| ['.', '..'].include? d } FileUtils.rmtree(tree) # move contents from temp to root Dir.glob("#{temp_dir}/*", File::FNM_DOTMATCH).each do |item| next if ['.', '..'].include?(File.basename(item)) FileUtils.mv(item, root) end # restore original branch if it exists if branch && branch != 'HEAD' Dir.chdir(root) do system("git checkout #{branch} > /dev/null 2>&1") end end end puts 'Recloned successfully.'.green 'Recloned successfully.'.green else # clone failed puts 'Clone failed.'.red nil end ensure # always clean up temp directory FileUtils.rm_rf(temp_dir) end end |
#reclonebanner ⇒ Object
66 67 68 69 70 |
# File 'lib/git_reclone.rb', line 66 def 25.times { |x| slowp "\rpreparing| ".red << ('~' * x) << '#==>'.red } 25.times { |x| slowp "\rpreparing| ".red << (' ' * x) << ('~' * (25 - x)) << '#==>'.yellow } printf "\rREADY.".red << (' ' * 50) << "\n" end |
#remote(search = /.*/) ⇒ Object
trying to parse out which remote should be the new source
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/git_reclone.rb', line 78 def remote(search = /.*/) pexit 'Not currently in a git repository.'.yellow if no_repo? r = remotes.find { |gr| gr.match search } pexit 'No remotes found in this repository.'.yellow if remotes.nil? if r.nil? errmsg = "No remotes found that match #{search.to_s.red}. All remotes:\n" + remotes.join("\n") pexit errmsg errmsg else r end end |
#remotes ⇒ Object
62 63 64 |
# File 'lib/git_reclone.rb', line 62 def remotes `git remote -v`.split("\n").map { |r| r.split[1] }.uniq end |
#slowp(x) ⇒ Object
72 73 74 75 |
# File 'lib/git_reclone.rb', line 72 def slowp(x) sleep @pdelay printf x end |
#verify(r) ⇒ Object
show remote to user and confirm location (unless using -f)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/git_reclone.rb', line 95 def verify(r) puts "Remote source:\t".red << r puts "Local target:\t".red << git_root branch = current_branch puts "Current branch:\t".red << branch unless branch == 'HEAD' if @verify puts 'Warning: this will replace the local copy with a fresh clone from the remote.'.yellow printf 'Continue recloning local repo? [yN] '.yellow unless $stdin.gets.chomp.downcase[0] == 'y' puts 'Reclone aborted.'.green return end end reclone remote, git_root.chomp, branch unless @testing end |