Class: GitRepo
Instance Method Summary collapse
- #config(name) ⇒ Object
- #current_branch ⇒ Object
- #dirty? ⇒ Boolean
- #exists?(ref) ⇒ Boolean
- #fast_forward!(upstream) ⇒ Object
- #fetch!(remote) ⇒ Object
-
#git(*args) ⇒ Object
helper methods, left public in case other commands want to use them directly.
-
#initialize(dir) ⇒ GitRepo
constructor
A new instance of GitRepo.
- #log(nr) ⇒ Object
- #log_commit_messages(nr) ⇒ Object
- #log_git(*args) ⇒ Object
- #merge_base(ref_a, ref_b) ⇒ Object
- #raw_status ⇒ Object
- #rebase_preserving_merges!(upstream) ⇒ Object
- #rev_list(ref_a, ref_b) ⇒ Object
- #sha(ref) ⇒ Object
- #stash! ⇒ Object
- #stash_pop! ⇒ Object
- #status ⇒ Object
- #tracking_branch ⇒ Object
- #tracking_remote ⇒ Object
Constructor Details
Instance Method Details
#config(name) ⇒ Object
110 111 112 113 |
# File 'lib/git-smart/git_repo.rb', line 110 def config(name) remote = git('config', name).chomp remote.empty? ? nil : remote end |
#current_branch ⇒ Object
9 10 11 |
# File 'lib/git-smart/git_repo.rb', line 9 def current_branch File.read(File.join(@dir, ".git", "HEAD")).strip.sub(/^.*refs\/heads\//, '') end |
#dirty? ⇒ Boolean
69 70 71 |
# File 'lib/git-smart/git_repo.rb', line 69 def dirty? status.any? { |k,v| k != :untracked && v.any? } end |
#exists?(ref) ⇒ Boolean
40 41 42 43 |
# File 'lib/git-smart/git_repo.rb', line 40 def exists?(ref) git('rev-parse', ref) $?.success? end |
#fast_forward!(upstream) ⇒ Object
73 74 75 |
# File 'lib/git-smart/git_repo.rb', line 73 def fast_forward!(upstream) log_git('merge', '--ff-only', upstream) end |
#fetch!(remote) ⇒ Object
32 33 34 |
# File 'lib/git-smart/git_repo.rb', line 32 def fetch!(remote) log_git('fetch', remote) end |
#git(*args) ⇒ Object
helper methods, left public in case other commands want to use them directly
99 100 101 |
# File 'lib/git-smart/git_repo.rb', line 99 def git(*args) Dir.chdir(@dir) { SafeShell.execute('git', *args) } end |
#log(nr) ⇒ Object
89 90 91 |
# File 'lib/git-smart/git_repo.rb', line 89 def log(nr) git('log', '--oneline', '-n', nr.to_s).split("\n").map { |l| l.split(" ",2) } end |
#log_commit_messages(nr) ⇒ Object
93 94 95 |
# File 'lib/git-smart/git_repo.rb', line 93 def (nr) log(nr).map(&:last) end |
#log_git(*args) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/git-smart/git_repo.rb', line 103 def log_git(*args) puts "Executing: #{['git', *args].join(" ")}" output = git(*args) puts output.split("\n").map { |l| " #{l}" } output end |
#merge_base(ref_a, ref_b) ⇒ Object
36 37 38 |
# File 'lib/git-smart/git_repo.rb', line 36 def merge_base(ref_a, ref_b) git('merge-base', ref_a, ref_b).chomp end |
#raw_status ⇒ Object
49 50 51 |
# File 'lib/git-smart/git_repo.rb', line 49 def raw_status git('status', '-s') end |
#rebase_preserving_merges!(upstream) ⇒ Object
85 86 87 |
# File 'lib/git-smart/git_repo.rb', line 85 def rebase_preserving_merges!(upstream) log_git('rebase', '-p', upstream) end |
#rev_list(ref_a, ref_b) ⇒ Object
45 46 47 |
# File 'lib/git-smart/git_repo.rb', line 45 def rev_list(ref_a, ref_b) git('rev-list', "#{ref_a}..#{ref_b}").split("\n") end |
#sha(ref) ⇒ Object
13 14 15 16 |
# File 'lib/git-smart/git_repo.rb', line 13 def sha(ref) sha = git('rev-parse', ref).chomp sha.empty? ? nil : sha end |
#stash! ⇒ Object
77 78 79 |
# File 'lib/git-smart/git_repo.rb', line 77 def stash! log_git('stash') end |
#stash_pop! ⇒ Object
81 82 83 |
# File 'lib/git-smart/git_repo.rb', line 81 def stash_pop! log_git('stash', 'pop') end |
#status ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/git-smart/git_repo.rb', line 53 def status raw_status. split("\n"). map { |l| l.split(" ") }. group_by(&:first). map_values { |lines| lines.map(&:last) }. map_keys { |status| case status when /^M/; :modified when /^A/; :added when /^\?\?/; :untracked else raise GitSmart::UnexpectedOutput.new("Expected the output of git status to only have lines starting with A,M, or ??. Got: \n#{raw_status}") end } end |
#tracking_branch ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/git-smart/git_repo.rb', line 22 def tracking_branch key = "branch.#{current_branch}.merge" value = config(key) if value =~ /^refs\/heads\/(.*)$/ $1 else raise GitSmart::UnexpectedOutput.new("Expected the config of '#{key}' to be /refs/heads/branchname, got '#{value}'") end end |
#tracking_remote ⇒ Object
18 19 20 |
# File 'lib/git-smart/git_repo.rb', line 18 def tracking_remote config("branch.#{current_branch}.remote") end |