Class: Greenhouse::Projects::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/greenhouse/projects/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



7
8
9
10
# File 'lib/greenhouse/projects/repository.rb', line 7

def method_missing(meth, *args)
  return git.send(meth, *args) if git.respond_to?(meth)
  super
end

Instance Attribute Details

#localObject Also known as: path

Returns the value of attribute local.



4
5
6
# File 'lib/greenhouse/projects/repository.rb', line 4

def local
  @local
end

#remoteObject

Returns the value of attribute remote.



4
5
6
# File 'lib/greenhouse/projects/repository.rb', line 4

def remote
  @remote
end

Instance Method Details

#aheadObject

Return any unpushed local changes on all branches/remotes



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/greenhouse/projects/repository.rb', line 102

def ahead
  unsynced.select do |branch|
    lbranch = git.object(branch[0].name)
    rbranch = git.object("#{branch[1].name}/#{branch[0].name}")
    lcommit = lbranch.log.first
    rcommit = rbranch.log.first
    
    !rbranch.log.map(&:sha).include?(lcommit.sha) && lbranch.log.map(&:sha).include?(rcommit.sha)
    
    #lcommit.date <= rcommit.date
  end
end

#ahead?Boolean

Check whether there are unpushed local changes on all branches/remotes

Returns:

  • (Boolean)


97
98
99
# File 'lib/greenhouse/projects/repository.rb', line 97

def ahead?
  !ahead.empty?
end

#behindObject

Return any unpulled changes on all branches/remotes



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/greenhouse/projects/repository.rb', line 121

def behind
  unsynced.select do |branch|
    lbranch = git.object(branch[0].name)
    rbranch = git.object("#{branch[1].name}/#{branch[0].name}")
    lcommit = lbranch.log.first
    rcommit = rbranch.log.first
    
    rbranch.log.map(&:sha).include?(lcommit.sha) && !lbranch.log.map(&:sha).include?(rcommit.sha)
    
    #lcommit.date >= rcommit.date
  end
end

#behind?Boolean

Check if there are any unpulled changes on all branches/remotes

Returns:

  • (Boolean)


116
117
118
# File 'lib/greenhouse/projects/repository.rb', line 116

def behind?
  !behind.empty?
end

#changedObject



42
43
44
# File 'lib/greenhouse/projects/repository.rb', line 42

def changed
  git.status.changed.select { |name,file| !git.diff('HEAD', '--').path(name).to_s.empty? }
end

#changed?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/greenhouse/projects/repository.rb', line 38

def changed?
  !changed.empty?
end

#changes(include_untracked = true) ⇒ Object

Get a list of all local changes (modified, added, deleted & untracked)



31
32
33
34
35
36
# File 'lib/greenhouse/projects/repository.rb', line 31

def changes(include_untracked=true)
  raise "Repository does not exist: #{@local}" unless cloned?
  changes = changed.merge(git.status.added).merge(git.status.deleted)
  changes.merge!(untracked) if include_untracked
  changes
end

#changes?(untracked = true) ⇒ Boolean

Check whether there are any uncommited local changes

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/greenhouse/projects/repository.rb', line 25

def changes?(untracked=true)
  raise "Repository does not exist: #{@local}" unless cloned?
  !changes(untracked).empty?
end

#cloneObject

Clone the remote into the local path



13
14
15
16
# File 'lib/greenhouse/projects/repository.rb', line 13

def clone
  raise "Repository already exists: #{@local}" if cloned?
  Git.clone(@remote, @local.split("/").last)
end

#cloned?Boolean Also known as: exists?

Check if the remote has been cloned locally

Returns:

  • (Boolean)


19
20
21
# File 'lib/greenhouse/projects/repository.rb', line 19

def cloned?
  File.exists?(@local) && @git ||= Git.open(@local)
end

#destroyObject

Remove the local repository



178
179
180
# File 'lib/greenhouse/projects/repository.rb', line 178

def destroy
  FileUtils.rm_rf @local
end

#divergedObject



138
139
140
141
142
143
144
145
146
147
# File 'lib/greenhouse/projects/repository.rb', line 138

def diverged
  unsynced.select do |branch|
    lbranch = git.object(branch[0].name)
    rbranch = git.object("#{branch[1].name}/#{branch[0].name}")
    lcommit = lbranch.log.first
    rcommit = rbranch.log.first
    
    !rbranch.log.map(&:sha).include?(lcommit.sha) && !lbranch.log.map(&:sha).include?(rcommit.sha)
  end
end

#diverged?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/greenhouse/projects/repository.rb', line 134

def diverged?
  !diverged.empty?
end

#gitObject



182
183
184
185
# File 'lib/greenhouse/projects/repository.rb', line 182

def git
  @git ||= Git.open(@local)
  @git
end

#othersObject

Return the results of ‘ls-files –others` to list ignored/untracked files



71
72
73
74
75
# File 'lib/greenhouse/projects/repository.rb', line 71

def others
  git.chdir do
    return `git ls-files --others`.split("\n")
  end
end

#out_of_syncObject



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/greenhouse/projects/repository.rb', line 157

def out_of_sync
  unsynced.select do |branch|
    lbranch = git.object(branch[0].name)
    rbranch = git.object("#{branch[1].name}/#{branch[0].name}")
    lcommit = lbranch.log.first
    rcommit = rbranch.log.first
    
    (rbranch.log.map(&:sha).include?(lcommit.sha) && !lbranch.log.map(&:sha).include?(rcommit.sha)) ||
    (!rbranch.log.map(&:sha).include?(lcommit.sha) && !lbranch.log.map(&:sha).include?(rcommit.sha))
  end
end

#out_of_sync?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/greenhouse/projects/repository.rb', line 153

def out_of_sync?
  behind? || diverged?
end

#pop_stashObject



173
174
175
# File 'lib/greenhouse/projects/repository.rb', line 173

def pop_stash
  git.chdir { `git stash pop 2>&1` }
end

#stagedObject



54
55
56
# File 'lib/greenhouse/projects/repository.rb', line 54

def staged
  changed.merge(git.status.added).merge(git.status.deleted).delete_if { |name,file| file.sha_index.empty? || file.sha_index == '0000000000000000000000000000000000000000' }
end

#staged?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/greenhouse/projects/repository.rb', line 58

def staged?
  !staged.empty?
end

#stashObject



169
170
171
# File 'lib/greenhouse/projects/repository.rb', line 169

def stash
  git.chdir { `git stash 2>&1` }
end

#synced?Boolean

Check whether local branches are synced with the remotes

Returns:

  • (Boolean)


78
79
80
# File 'lib/greenhouse/projects/repository.rb', line 78

def synced?
  unsynced.empty?
end

#unstagedObject



62
63
64
# File 'lib/greenhouse/projects/repository.rb', line 62

def unstaged
  changed.merge(untracked).select { |name,file| file.sha_index.empty? || file.sha_index == '0000000000000000000000000000000000000000' }
end

#unstaged?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/greenhouse/projects/repository.rb', line 66

def unstaged?
  !unstaged.empty?
end

#unsyncedObject

Return any unsynced branches for all remotes



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/greenhouse/projects/repository.rb', line 83

def unsynced
  branches = []
  git.branches.local.each do |branch|
    git.remotes.each do |remote|
      lcommit = git.object(branch.name).log.first
      rcommit = git.object("#{remote.name}/#{branch.name}").log.first
      next if lcommit.sha == rcommit.sha
      branches << [branch, remote]
    end
  end
  branches
end

#untrackedObject



46
47
48
# File 'lib/greenhouse/projects/repository.rb', line 46

def untracked
  git.status.untracked.select { |name,file| !name.match(/\Atmp\/.*\Z/) } # temporary hack to avoid untracked tmp files, since they're not being properly ignored(?)
end

#untracked?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/greenhouse/projects/repository.rb', line 50

def untracked?
  !untracked.empty?
end

#up_to_date?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/greenhouse/projects/repository.rb', line 149

def up_to_date?
  !out_of_sync?
end