Module: Copernicium::Workspace

Includes:
Repos
Included in:
Driver
Defined in:
lib/workspace.rb

Overview

todo - @@files really should be a Hash, with paths as keys, then rather than using indices.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Repos

#branches, #current, #current_files, #current_head, #current_snaps, delete_branch, delete_snapshot, diff_snapshots, diffset, get_branch, get_snapshot, has_branch?, has_snapshots?, hasher, history, make_branch, make_snapshot, merge_history, merge_snapshot, setup_tester, sort_history, update, update_branch, update_history, update_snap

Methods included from RevLog

add_file, delete_file, diff_files, get_file, #hash_array, hash_file, history, setup_tester, updatelog

Class Method Details

.checkout(comm = UIComm.new) ⇒ Object

takes in a snapshot id, sets workspace to that snapshot



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/workspace.rb', line 132

def Workspace.checkout(comm = UIComm.new)
  comm.rev = Repos.current_head if comm.rev.nil?
  if ! Repos.has_snapshots? # dont checkout
    puts 'No snapshots yet! Commit something before checkout.'.red
  elsif comm.files.nil? # checkout everything
    Repos.get_snapshot(comm.rev).files.each do |file|
      Workspace.checkout_file file
    end
  else # just checkout given files
    Repos.get_snapshot(comm.rev).files.select do |f|
      comm.files.include? f.path
    end .each do |file|
      Workspace.checkout_file file
    end
  end
end

.checkout_file(file) ⇒ Object

take in file object, restore



124
125
126
127
128
129
# File 'lib/workspace.rb', line 124

def Workspace.checkout_file(file)
  idx = indexOf(file.path)
  idx.nil?? @@files << file : @@files[idx] = file
  Dir.mkdir file.base unless Dir.exist? file.base
  File.write(file.path, RevLog.get_file(file.last))
end

.clean(comm = UIComm.new) ⇒ Object

reset repo back to a given state



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/workspace.rb', line 150

def Workspace.clean(comm = UIComm.new)
  if comm.files.nil? # reset everything
    Workspace.clear
  else # files are not nil
    comm.files.each do |x|
      idx = indexOf(x)
      if idx.nil?
        puts "Cannot clean #{x}:".yel + " does not exist in snapshot"
      else
        @@files.delete_at(idx)
        File.delete(x)
      end
    end
  end
  Workspace.checkout comm # cleanse state
end

.clearObject

Clear the workspace



118
119
120
121
# File 'lib/workspace.rb', line 118

def Workspace.clear
  @@files.each{ |x| File.delete(x.path) }
  @@files = []
end

.commit(comm = UIComm.new) ⇒ Object

commit a list of files or the entire workspace to make a new snapshot



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/workspace.rb', line 184

def Workspace.commit(comm = UIComm.new)
  if comm.files.nil? # commit everything
    Workspace.status.each do |st|
      st.each { |x| Workspace.commit_file x }
    end
  else comm.files.each do |x|
      if File.exist? x
        Workspace.commit_file(x)
      else
        puts "Cannot commit #{x}:".yel + " file does not exist"
      end
    end
  end
  # todo - handle case of no changes, dont make a snapshot
  Repos.make_snapshot(@@files, comm.cmt_msg) # return snapshot id
end

.commit_file(x) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/workspace.rb', line 167

def Workspace.commit_file(x)
  puts 'Committing: '.grn + x
  added, edits, remov = Workspace.status
  if added.include? x
    hash = RevLog.add_file(x, File.read(x))
    @@files.push(FileObj.new x, [hash])
  elsif edits.include? x
    hash = RevLog.add_file(x, File.read(x))
    @@files[indexOf x].history << hash
  elsif remov.include? x
    @@files.delete_at(indexOf x)
  else
    puts 'No changes: '.yel + x
  end
end

.create_project(location = Dir.pwd, branch = 'master') ⇒ Object

create a new copernicium project



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/workspace.rb', line 44

def Workspace.create_project(location = Dir.pwd, branch = 'master')
  Dir.mkdir location unless Dir.exist? location
  Dir.chdir location

  # create our copernicium folders
  @@copn = File.join('.', '.cn')
  @@snap = File.join(@@copn, 'snap')
  @@revs = File.join(@@copn, 'revs')
  @@head = File.join(@@copn, 'branch')
  @@hist = File.join(@@copn, 'history')
  Dir.mkdir(@@copn) unless Dir.exist?(@@copn)
  Dir.mkdir(@@snap) unless Dir.exist?(@@snap)
  Dir.mkdir(@@revs) unless Dir.exist?(@@revs)

  # make default branch, history
  hist = YAML.dump({branch => []})
  File.write @@head, branch
  File.write @@hist, hist

  if Dir.exist?(@@copn)
    location # return where we made the repo
  else # something has gone horribly wrong
    raise 'Could not create or find a Copernicium folder (.cn).'.red
  end
end

.indexOf(x) ⇒ Object

WORKSPACE MANAGEMENT



100
101
102
103
104
105
# File 'lib/workspace.rb', line 100

def Workspace.indexOf(x)
  @@files.each_with_index do |f, i|
    return i if f.path == x
  end
  nil # x is not included in @@files
end

.merge(id) ⇒ Object

wrapper for Repos merge_snapshot, update workspace with result returns [=> content, [conflicting paths]] todo update workspace with result todo return any conflicting files



205
206
207
# File 'lib/workspace.rb', line 205

def Workspace.merge(id)
  Repos.merge_snapshot(id)
end

.setupObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/workspace.rb', line 30

def Workspace.setup
  @@cwd = Dir.pwd
  @@root = (noroot?? @@cwd : getroot)
  @@root.sub!(@@cwd, '.')
  @@copn = File.join(@@root, '.cn')
  @@snap = File.join(@@copn, 'snap')
  @@revs = File.join(@@copn, 'revs')
  RevLog.setup @@root
  Repos.setup @@root
  @@files = Repos.current_files
  @@branch = Repos.current
end

.statusObject



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/workspace.rb', line 209

def Workspace.status
  added = []
  edits = []
  remov = []
  working_files.each do |f|
    idx = indexOf(f)
    if idx.nil? # new file
      added << f
    else # changed file
      edits << f if File.read(f) != RevLog.get_file(@@files[idx].last)
    end
  end

  # any deleted files from the last commit?
  remov = @@files.map(&:path) - working_files

  [added, edits, remov]
end

.working_filesObject

get array of filenamess currently in workspace, except folders and .cn/* todo - include files and files that start with a dot (.)



109
110
111
112
113
114
115
# File 'lib/workspace.rb', line 109

def Workspace.working_files
  (Dir[ File.join(@@root, '**', '*') ].reject do |p|
    File.directory? p || p.include?(@@copn)
  end).map do |p|
    p.sub!(/^\.\//, '') # delete leading './'
  end
end

Instance Method Details

#getrootObject

PROJECT HELPERS

find the root .cn folder, or return nil if it doesnt exist



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/workspace.rb', line 74

def getroot
  cwd = Dir.pwd
  max = 0
  def more_folders() Dir.pwd != '/' end
  def root_found() Dir.exist? File.join(Dir.pwd, '.cn') end
  while max < 10 && more_folders && !root_found
    Dir.chdir(File.join(Dir.pwd, '..'))
    max += 1
  end

  if root_found # return where cn was found
    cnroot = Dir.pwd
    Dir.chdir(cwd)
    cnroot
  else # directory not found
    Dir.chdir(cwd)
    nil
  end
end

#more_foldersObject



77
# File 'lib/workspace.rb', line 77

def more_folders() Dir.pwd != '/' end

#noroot?Boolean

tells us whether we are in a cn project or not

Returns:

  • (Boolean)


95
# File 'lib/workspace.rb', line 95

def noroot?() getroot.nil? end

#root_foundObject



78
# File 'lib/workspace.rb', line 78

def root_found() Dir.exist? File.join(Dir.pwd, '.cn') end