Class: TicGit::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ticgit/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_dir, opts = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ticgit/base.rb', line 15

def initialize(git_dir, opts = {})
  @git = Git.open(find_repo(git_dir))
  @logger = opts[:logger] || Logger.new(STDOUT)
  
  proj = Ticket.clean_string(@git.dir.path)
  
  @tic_dir = opts[:tic_dir] || '~/.ticgit'
  @tic_working = opts[:working_directory] || File.expand_path(File.join(@tic_dir, proj, 'working'))
  @tic_index = opts[:index_file] || File.expand_path(File.join(@tic_dir, proj, 'index'))

  # load config file
  @config_file = File.expand_path(File.join(@tic_dir, proj, 'config.yml'))
  if File.exists?(config_file)
    @config = YAML.load(File.read(config_file))
  else
    @config = {}
  end
  
  @state = File.expand_path(File.join(@tic_dir, proj, 'state'))
  
  if File.exists?(@state)
    load_state
  else
    reset_ticgit
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/ticgit/base.rb', line 12

def config
  @config
end

#config_fileObject (readonly)

Returns the value of attribute config_file.



13
14
15
# File 'lib/ticgit/base.rb', line 13

def config_file
  @config_file
end

#current_ticketObject (readonly)

saved in state



11
12
13
# File 'lib/ticgit/base.rb', line 11

def current_ticket
  @current_ticket
end

#gitObject (readonly)

Returns the value of attribute git.



9
10
11
# File 'lib/ticgit/base.rb', line 9

def git
  @git
end

#last_ticketsObject (readonly)

saved in state



11
12
13
# File 'lib/ticgit/base.rb', line 11

def last_tickets
  @last_tickets
end

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/ticgit/base.rb', line 9

def logger
  @logger
end

#stateObject (readonly)

Returns the value of attribute state.



13
14
15
# File 'lib/ticgit/base.rb', line 13

def state
  @state
end

#tic_indexObject (readonly)

Returns the value of attribute tic_index.



10
11
12
# File 'lib/ticgit/base.rb', line 10

def tic_index
  @tic_index
end

#tic_workingObject (readonly)

Returns the value of attribute tic_working.



10
11
12
# File 'lib/ticgit/base.rb', line 10

def tic_working
  @tic_working
end

#ticketsObject (readonly)

saved in state



11
12
13
# File 'lib/ticgit/base.rb', line 11

def tickets
  @tickets
end

Instance Method Details

#comment_add(ticket_id, comment, options = {}) ⇒ Object



228
229
# File 'lib/ticgit/base.rb', line 228

def comment_add(ticket_id, comment, options = {})
end

#comment_list(ticket_id) ⇒ Object



231
232
# File 'lib/ticgit/base.rb', line 231

def comment_list(ticket_id)
end

#find_repo(dir) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/ticgit/base.rb', line 42

def find_repo(dir)
  full = File.expand_path(dir)
  ENV["GIT_WORKING_DIR"] || loop do
    return full if File.directory?(File.join(full, ".git"))
    raise NoRepoFound if full == full=File.dirname(full)
  end
end

#in_branch(branch_exists = true) ⇒ Object

temporarlily switches to ticgit branch for tic work



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/ticgit/base.rb', line 270

def in_branch(branch_exists = true)
  needs_checkout = false
  if !File.directory?(@tic_working)
    FileUtils.mkdir_p(@tic_working)
    needs_checkout = true
  end
  if !File.exists?('.hold')
    needs_checkout = true
  end
  
  old_current = git.lib.branch_current
  begin
    git.lib.change_head_branch('ticgit')
    git.with_index(@tic_index) do          
      git.with_working(@tic_working) do |wd|
        git.lib.checkout('ticgit') if needs_checkout && branch_exists
        yield wd
      end
    end
  ensure
    git.lib.change_head_branch(old_current)
  end
end

#init_ticgit_branch(ticgit_branch = false) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
# File 'lib/ticgit/base.rb', line 257

def init_ticgit_branch(ticgit_branch = false)
  @logger.info 'creating ticgit repo branch'
  
  in_branch(ticgit_branch) do          
    new_file('.hold', 'hold')
    if !ticgit_branch
      git.add
      git.commit('creating the ticgit branch')
    end
  end
end

#load_stateObject



57
58
59
60
61
62
# File 'lib/ticgit/base.rb', line 57

def load_state
  # read in the internals
  if(File.exists?(@state))
    @tickets, @last_tickets, @current_ticket = File.open(@state) { |f| Marshal.load(f) } rescue nil
  end      
end

#load_ticketsObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/ticgit/base.rb', line 238

def load_tickets
  @tickets = {}

  bs = git.lib.branches_all.map { |b| b[0] }
  init_ticgit_branch(bs.include?('ticgit')) if !(bs.include?('ticgit') && File.directory?(@tic_working))
  
  tree = git.lib.full_tree('ticgit')
  tree.each do |t|
    data, file = t.split("\t")
    mode, type, sha = data.split(" ")
    tic = file.split('/')
    if tic.size == 2  # directory depth
      ticket, info = tic
      @tickets[ticket] ||= { 'files' => [] }
      @tickets[ticket]['files'] << [info, sha]
    end
  end
end

#new_file(name, contents) ⇒ Object



294
295
296
297
298
# File 'lib/ticgit/base.rb', line 294

def new_file(name, contents)
  File.open(name, 'w') do |f|
    f.puts contents
  end
end

#reset_ticgitObject



71
72
73
74
# File 'lib/ticgit/base.rb', line 71

def reset_ticgit
  load_tickets
  save_state
end

#save_stateObject



50
51
52
53
54
55
# File 'lib/ticgit/base.rb', line 50

def save_state
  # marshal dump the internals
  File.open(@state, 'w') { |f| Marshal.dump([@tickets, @last_tickets, @current_ticket], f) } rescue nil
  # save config file
  File.open(@config_file, 'w') { |f| f.write(config.to_yaml) }
end

#tic_statesObject



234
235
236
# File 'lib/ticgit/base.rb', line 234

def tic_states
  ['open', 'resolved', 'invalid', 'hold']
end

#ticket_assign(new_assigned = nil, ticket_id = nil) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/ticgit/base.rb', line 212

def ticket_assign(new_assigned = nil, ticket_id = nil)
  if t = ticket_revparse(ticket_id)
    ticket = TicGit::Ticket.open(self, t, @tickets[t])
    ticket.change_assigned(new_assigned)
    reset_ticgit
  end
end

#ticket_change(new_state, ticket_id = nil) ⇒ Object



202
203
204
205
206
207
208
209
210
# File 'lib/ticgit/base.rb', line 202

def ticket_change(new_state, ticket_id = nil)
  if t = ticket_revparse(ticket_id)
    if tic_states.include?(new_state)
      ticket = TicGit::Ticket.open(self, t, @tickets[t])
      ticket.change_state(new_state)
      reset_ticgit
    end
  end
end

#ticket_checkout(ticket_id) ⇒ Object



220
221
222
223
224
225
226
# File 'lib/ticgit/base.rb', line 220

def ticket_checkout(ticket_id)
  if t = ticket_revparse(ticket_id)
    ticket = TicGit::Ticket.open(self, t, @tickets[t])
    @current_ticket = ticket.ticket_name
    save_state
  end
end

#ticket_comment(comment, ticket_id = nil) ⇒ Object

returns new Ticket



77
78
79
80
81
82
83
# File 'lib/ticgit/base.rb', line 77

def ticket_comment(comment, ticket_id = nil)
  if t = ticket_revparse(ticket_id)        
    ticket = TicGit::Ticket.open(self, t, @tickets[t])
    ticket.add_comment(comment)
    reset_ticgit
  end
end

#ticket_list(options = {}) ⇒ Object

returns array of Tickets



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
# File 'lib/ticgit/base.rb', line 86

def ticket_list(options = {})
  ts = []
  @last_tickets = []
  @config['list_options'] ||= {}
  
  @tickets.to_a.each do |name, t|
    ts << TicGit::Ticket.open(self, name, t)
  end

  if name = options[:saved]
     if c = config['list_options'][name]
       options = c.merge(options)
     end
  end   
  
  if options[:list]
    # TODO : this is a hack and i need to fix it
    config['list_options'].each do |name, opts|
      puts name + "\t" + opts.inspect
    end
    return false
  end   

  # SORTING
  if field = options[:order]
    field, type = field.split('.')
    case field
    when 'assigned'
      ts = ts.sort { |a, b| a.assigned <=> b.assigned }
    when 'state'
      ts = ts.sort { |a, b| a.state <=> b.state }
    when 'date'
      ts = ts.sort { |a, b| a.opened <=> b.opened }
    end    
    ts = ts.reverse if type == 'desc'
  else
    # default list
    ts = ts.sort { |a, b| a.opened <=> b.opened }
  end

  if options.size == 0
    # default list
    options[:state] = 'open'
  end
  
  # :tag, :state, :assigned
  if t = options[:tag]
    ts = ts.select { |tag| tag.tags.include?(t) }
  end
  if s = options[:state]
    ts = ts.select { |tag| tag.state =~ /#{s}/ }
  end
  if a = options[:assigned]
    ts = ts.select { |tag| tag.assigned =~ /#{a}/ }
  end
  
  if save = options[:save]
    options.delete(:save)
    @config['list_options'][save] = options
  end
  
  @last_tickets = ts.map { |t| t.ticket_name }
  # :save

  save_state
  ts
end

#ticket_new(title, options = {}) ⇒ Object

returns new Ticket



65
66
67
68
69
# File 'lib/ticgit/base.rb', line 65

def ticket_new(title, options = {})
  t = TicGit::Ticket.create(self, title, options)
  reset_ticgit
  TicGit::Ticket.open(self, t.ticket_name, @tickets[t.ticket_name])
end

#ticket_recent(ticket_id = nil) ⇒ Object

returns recent ticgit activity uses the git logs for this



164
165
166
167
168
169
170
171
# File 'lib/ticgit/base.rb', line 164

def ticket_recent(ticket_id = nil)      
  if ticket_id
    t = ticket_revparse(ticket_id) 
    return git.log.object('ticgit').path(t)
  else 
    return git.log.object('ticgit')
  end
end

#ticket_revparse(ticket_id) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ticgit/base.rb', line 173

def ticket_revparse(ticket_id)
  if ticket_id
    if /^[0-9]*$/ =~ ticket_id
      if t = @last_tickets[ticket_id.to_i - 1]
        return t
      end
    else
      # partial or full sha
      if ch = @tickets.select { |name, t| t['files'].assoc('TICKET_ID')[1] =~ /^#{ticket_id}/ }
        return ch.first[0]
      end
    end
  elsif(@current_ticket)
    return @current_ticket
  end
end

#ticket_show(ticket_id = nil) ⇒ Object

returns single Ticket



155
156
157
158
159
160
# File 'lib/ticgit/base.rb', line 155

def ticket_show(ticket_id = nil)      
  # ticket_id can be index of last_tickets, partial sha or nil => last ticket
  if t = ticket_revparse(ticket_id)
    return TicGit::Ticket.open(self, t, @tickets[t])
  end
end

#ticket_tag(tag, ticket_id = nil, options = {}) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/ticgit/base.rb', line 190

def ticket_tag(tag, ticket_id = nil, options = {})
  if t = ticket_revparse(ticket_id)    
    ticket = TicGit::Ticket.open(self, t, @tickets[t])
    if options[:remove]
      ticket.remove_tag(tag)
    else
      ticket.add_tag(tag)
    end
    reset_ticgit
  end
end