Class: GitReclone

Inherits:
Object
  • Object
show all
Defined in:
lib/git-reclone-version.rb,
lib/git-reclone.rb

Overview

universal version tracking

Constant Summary collapse

Help =
<<-HELP
#{'git reclone'.red}: a git repo restoring tool

reclones from the remote listed first, overwriting your local copy.
to restore from a particular remote repository, specify the host:

    git reclone bitbucket # reclone using bitbucket
    git reclone github # reclone using github
HELP
Version =
"0.2.3"

Instance Method Summary collapse

Constructor Details

#initialize(test = false) ⇒ GitReclone

Returns a new instance of GitReclone.



15
16
17
18
19
# File 'lib/git-reclone.rb', line 15

def initialize(test=false)
  @pdelay = 0.01 # constant for arrow speed
  @testing = test
  @verify = !test
end

Instance Method Details

#fire(args = []) ⇒ Object



21
22
23
24
25
26
# File 'lib/git-reclone.rb', line 21

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_rootObject



53
54
55
# File 'lib/git-reclone.rb', line 53

def git_root
  %x{git rev-parse --show-toplevel}
end

#no_repo?Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/git-reclone.rb', line 48

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



44
45
46
# File 'lib/git-reclone.rb', line 44

def parse_arg(a)
  a.nil?? verify(remote) : verify(remote(a))
end

#parse_opt(o) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/git-reclone.rb', line 33

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



28
29
30
31
# File 'lib/git-reclone.rb', line 28

def pexit(msg)
  puts msg
  exit 1
end

#reclone(remote, root) ⇒ Object

overwrite the local copy of the repository with the remote one



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/git-reclone.rb', line 108

def reclone(remote, root)
  # remove the git repo from this computer
  if !@testing
    tree = Dir.glob("*", File::FNM_DOTMATCH).select {|d| not ['.','..'].include? d }
    FileUtils.rmtree (tree)
  end

  cloner = "git clone \"#{remote}\" \"#{root}\""

  puts "Recloned successfully.".green if system(cloner)
end

#reclonebannerObject



61
62
63
64
65
# File 'lib/git-reclone.rb', line 61

def reclonebanner
  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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/git-reclone.rb', line 73

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
    return errmsg
  else
    return r
  end
end

#remotesObject



57
58
59
# File 'lib/git-reclone.rb', line 57

def remotes
  %x{git remote -v}.split("\n").map { |r| r.split[1] }.uniq
end

#slowp(x) ⇒ Object



67
68
69
70
# File 'lib/git-reclone.rb', line 67

def slowp(x)
  sleep @pdelay
  printf x
end

#verify(r) ⇒ Object

show remote to user and confirm location (unless using -f)



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/git-reclone.rb', line 90

def verify(r)
  reclonebanner
  puts "Remote source:\t".red << r
  puts "Local target:\t".red << git_root

  if @verify
    puts "Warning: this will completely overwrite the local copy.".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 unless @testing
end