Class: Ohajiki::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/ohajiki/repo.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir_path) ⇒ Repo

Returns a new instance of Repo.



19
20
21
22
23
24
25
# File 'lib/ohajiki/repo.rb', line 19

def initialize(dir_path)
  @dir_path = dir_path
  in_dir do
    @repo = Grit::Repo.new('.')
    @git = Grit::Git.new('./.git')
  end
end

Class Method Details

.exist?(dir_path) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/ohajiki/repo.rb', line 7

def exist?(dir_path)
  File.exist? File.join(dir_path, '.git')
end

.init(dir_path, &block) ⇒ Object



11
12
13
14
15
16
# File 'lib/ohajiki/repo.rb', line 11

def init(dir_path, &block)
  Grit::Repo.init(dir_path)
  repo = self.new dir_path
  repo.instance_eval &block 
  repo
end

Instance Method Details

#add_stage!Object



72
73
74
75
76
# File 'lib/ohajiki/repo.rb', line 72

def add_stage! 
  in_dir do
    @repo.add('.')
  end
end

#added_countObject



100
101
102
103
104
105
106
# File 'lib/ohajiki/repo.rb', line 100

def added_count
  in_dir do
    @repo.status.select { |file|
      file.untracked
    }.size
  end
end

#changed_countObject



92
93
94
95
96
97
98
# File 'lib/ohajiki/repo.rb', line 92

def changed_count
  in_dir do
    @repo.status.select { |file|
      file.untracked || ['A', 'M', 'D'].include?(file.type)
    }.size 
  end
end

#commit_all!Object



78
79
80
81
82
# File 'lib/ohajiki/repo.rb', line 78

def commit_all!
  in_dir do
    @repo.commit_all("#{changed_count} files updated")
  end
end

#file_added?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/ohajiki/repo.rb', line 88

def file_added?
  added_count > 0
end

#file_changed?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/ohajiki/repo.rb', line 84

def file_changed?
  changed_count > 0
end

#in_dir(&block) ⇒ Object



27
28
29
30
31
# File 'lib/ohajiki/repo.rb', line 27

def in_dir(&block)
  Dir.chdir(File.expand_path(@dir_path, __FILE__)) do
    block.call
  end
end

#latest?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ohajiki/repo.rb', line 37

def latest?
  remote_head_id == local_head_id
end

#local_head_idObject



60
61
62
63
64
# File 'lib/ohajiki/repo.rb', line 60

def local_head_id
  in_dir do
    @git.native('rev-parse', {}, 'HEAD').strip
  end
end

#pull!Object



41
42
43
44
45
# File 'lib/ohajiki/repo.rb', line 41

def pull!
  in_dir do
    @git.native('pull', {}, 'origin', 'master')
  end
end

#push!Object



66
67
68
69
70
# File 'lib/ohajiki/repo.rb', line 66

def push!
  in_dir do
    @git.native('push', {:raise => true}, 'origin', 'master') 
  end
end

#remote_add(name, url) ⇒ Object



33
34
35
# File 'lib/ohajiki/repo.rb', line 33

def remote_add(name, url)
  @repo.remote_add(name, url)
end

#remote_head_idObject



53
54
55
56
57
58
# File 'lib/ohajiki/repo.rb', line 53

def remote_head_id
  in_dir do
    h = @git.native('ls-remote', {}, 'origin', 'HEAD')
    h.empty? ? '' : h.split("\t")[0].strip
  end
end

#reset_hard_head!Object



47
48
49
50
51
# File 'lib/ohajiki/repo.rb', line 47

def reset_hard_head!
  in_dir do
    @git.native('reset', {:hard => true}, 'HEAD')
  end
end