copernicium 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/workspace.rb CHANGED
@@ -1,19 +1,7 @@
1
1
  # workspace module - linfeng and qiguang
2
2
 
3
- module Copernicium
4
- def writeFile(path, content)
5
- f = open(path, 'w')
6
- f.write(content)
7
- f.close
8
- end
9
-
10
- def readFile(path)
11
- f = open(path, 'r')
12
- txt = f.read
13
- f.close
14
- txt
15
- end
16
3
 
4
+ module Copernicium
17
5
  class FileObj
18
6
  attr_reader :path, :history
19
7
  def initialize(path, ids)
@@ -28,44 +16,80 @@ module Copernicium
28
16
  @path == rhs.path
29
17
  end
30
18
  end
19
+
20
+ # returns most recent file id in the snapshot it was saved in
21
+ def last
22
+ @history.last
23
+ end
24
+ end
25
+
26
+ # helper methods for file IO
27
+ def writeFile(path, content)
28
+ f = open(path, 'w')
29
+ f.write(content)
30
+ f.close
31
+ end
32
+
33
+ # helper methods for file IO
34
+ def readFile(path)
35
+ f = open(path, 'r')
36
+ txt = f.read
37
+ f.close
38
+ txt
31
39
  end
32
40
 
33
- class Workspace
34
- attr_reader :repos, :revlog
35
- def initialize(bname = 'master')
36
- @files = []
37
- @cwd = Dir.pwd
38
- @root = getroot
39
- Dir.chdir(@cwd)
40
- @root = @cwd if @root.nil?
41
- @cwd.sub!(@root, '.')
42
- @branch = bname
43
- @repo = Repos.new(@cwd)
44
- @revlog = RevLog.new(@cwd)
41
+ module Workspace
42
+ include Repos # needed for keeping track of history
43
+ def Workspace.setup(bname = 'master')
44
+ @@files = []
45
+ @@cwd = Dir.pwd
46
+ @@root = (noroot?? @@cwd : getroot)
47
+ @@copn = File.join(@@root, '.cn')
48
+ Dir.mkdir(@@copn) unless Dir.exist?(@@copn)
49
+ @@cwd.sub!(@@root, '.')
50
+ @@branch = bname
51
+ RevLog.setup @@root
52
+ Repos.setup @@root
53
+ end
45
54
 
46
- pexit 'Copernicium folder (.cn) not found.', 1 if @root.nil?
55
+ # create a new copernicium project
56
+ def Workspace.create_project(location = Dir.pwd)
57
+ Dir.mkdir location if !File.exists? location
58
+ Dir.chdir location
59
+ errmsg = 'Copernicium folder (.cn) not found, could not create.'.red
60
+ pexit errmsg, 1 if noroot?
47
61
  end
48
62
 
49
- # find where the root .cn folder is
63
+ # find the root .cn folder
50
64
  def getroot
65
+ cwd = Dir.pwd
51
66
  max = 0
52
- def notroot() Dir.pwd != '/' end
53
- def notcn() File.exists? File.join(Dir.pwd, '.cn') end
54
- while max < 10 && notroot && notcn
67
+ def more_folders() Dir.pwd != '/' end
68
+ def root_found() Dir.exist? File.join(Dir.pwd, '.cn') end
69
+ while max < 10 && more_folders && !root_found
55
70
  Dir.chdir(File.join(Dir.pwd, '..'))
56
71
  max += 1
57
72
  end
58
73
 
59
- if notcn # return where cn was found
60
- Dir.pwd
74
+ if root_found # return where cn was found
75
+ cnroot = Dir.pwd
76
+ Dir.chdir(cwd)
77
+ cnroot
61
78
  else # directory not found
79
+ Dir.chdir(cwd)
62
80
  nil
63
81
  end
64
82
  end
65
83
 
66
- def indexOf(x)
84
+ # tells us whether we are in a cn project or not
85
+ def noroot?
86
+ getroot.nil?
87
+ end
88
+
89
+ # workspace management
90
+ def Workspace.indexOf(x)
67
91
  index = -1
68
- @files.each_with_index do |e,i|
92
+ @@files.each_with_index do |e,i|
69
93
  if e.path == x
70
94
  index = i
71
95
  break
@@ -75,32 +99,34 @@ module Copernicium
75
99
  end
76
100
 
77
101
  # if include all the elements in list_files
78
- def include?(files)
102
+ def Workspace.include?(files)
79
103
  files.each { |x| return false if indexOf(x) == -1 }
80
104
  true
81
105
  end
82
106
 
83
- # get all files currently in workspace
84
- def ws_files
85
- Dir[ File.join(@root, '**', '*') ].reject { |p| File.directory? p }
107
+ # get all files currently in workspace, except folders and .cn/*
108
+ def Workspace.ws_files
109
+ Dir[ File.join(@@root, '**', '*') ].reject do |p|
110
+ File.directory? p || File.join(@@root,'.cn').include?(p) == false
111
+ end
86
112
  end
87
113
 
88
114
  # Clear the current workspace
89
- def clear
90
- @files.each{ |x| File.delete(x.path) }
91
- @files = []
115
+ def Workspace.clear
116
+ @@files.each{ |x| File.delete(x.path) }
117
+ @@files = []
92
118
  end
93
119
 
94
- # reset first: delete them from disk and reset @files
120
+ # reset first: delete them from disk and reset @@files
95
121
  # restore it with checkout() if we have had a branch name
96
122
  # or it is the initial state, no commit and no checkout
97
123
  # if list_files is nil, then rollback the list of files from the branch
98
124
  # or rollback to the entire branch head pointed
99
- def clean(comm)
100
- if comm.files.empty?
101
- clear # reset, checkout last commit
102
- checkout
103
- else # list_files are not nil
125
+ def Workspace.clean(comm)
126
+ if comm.files.empty? # reset, checkout last commit
127
+ Workspace.clear
128
+ Workspace.checkout
129
+ else # files are not nil
104
130
 
105
131
  # exit if the specified file is not in the workspace
106
132
  return -1 if (self.include? comm.files) == false
@@ -109,7 +135,7 @@ module Copernicium
109
135
  comm.files.each do |x|
110
136
  File.delete(x)
111
137
  idx = indexOf(x)
112
- @files.delete_at(idx) if !idx == -1
138
+ @@files.delete_at(idx) if !idx == -1
113
139
  end
114
140
 
115
141
  # if we have had a branch, first we get the latest snapshot of it
@@ -119,45 +145,46 @@ module Copernicium
119
145
  end
120
146
 
121
147
  # commit a list of files or the entire workspace to make a new snapshot
122
- def commit(comm)
123
- unless ws_files.empty?
124
- ws_files.each do |x|
148
+ def Workspace.commit(comm)
149
+ if comm.files.nil? # commit everything
150
+ Workspace.ws_files.each do |x|
125
151
  if indexOf(x) == -1
126
152
  content = readFile(x)
127
- hash = @revlog.add_file(x, content)
153
+ hash = RevLog.add_file(x, content)
128
154
  fobj = FileObj.new(x, [hash,])
129
- @files.push(fobj)
130
- else
155
+ @@files.push(fobj)
156
+ else # file exists
131
157
  content = readFile(x)
132
- hash = @revlog.add_file(x, content)
133
- if @files[indexOf(x)].history[-1] != hash
134
- @files[indexOf(x)].history << hash
158
+ hash = RevLog.add_file(x, content)
159
+ if @@files[indexOf(x)].history[-1] != hash
160
+ @@files[indexOf(x)].history << hash
135
161
  end
136
162
  end
137
163
  end
164
+ else # just commit certain files
138
165
  end
139
- @repo.make_snapshot(@files) # return snapshot id
166
+ Repos.make_snapshot(@@files) # return snapshot id
140
167
  end
141
168
 
142
- def checkout(comm = UIComm.new(rev: @branch))
169
+ def Workspace.checkout(comm)
143
170
  =begin
144
- # just support branches for now
171
+ # just support revisions for now
145
172
  # if argu is an Array Object, we assume it is a list of files to be added
146
- # # to the workspace # we add the list of files to @files regardless
173
+ # # to the workspace # we add the list of files to @@files regardless
147
174
  # whether it has been in # it. that means there may be multiple versions
148
175
  # of a file.
149
176
  unless comm.files.nil?
150
177
  list_files = comm.files
151
- returned_snapshot = @repo.get_snapshot(@repo.history.last)
178
+ returned_snapshot = Repos.get_snapshot(Repos.history.last)
152
179
  list_files_last_commit = returned_snapshot.files
153
180
  list_files_last_commit.each do |x|
154
181
  if list_files.include? x.path
155
- content = @revlog.get_file(x.history.last)
182
+ content = RevLog.get_file(x.history.last)
156
183
  idx = indexOf(x.path)
157
184
  if idx == -1
158
- @files << x
185
+ @@files << x
159
186
  else
160
- @files[idx] = x
187
+ @@files[idx] = x
161
188
  end
162
189
  writeFile(x.path, content)
163
190
  end
@@ -168,21 +195,42 @@ module Copernicium
168
195
 
169
196
  clear # reset workspace
170
197
 
171
- # we first get the last snapshot id of the branch, and then get the commit
198
+ # Dec. 3th, 2015 by Linfeng,
199
+ # for this command, the comm.rev should be a string representing the branch name
200
+ @@branch = comm.rev
201
+ Repos.update_branch(@@branch)
202
+
203
+ # if not snapshots exist, dont checkout
204
+ return -1 unless Repos.has_snapshots?
205
+
206
+ # if no snapshot files, dont checkout
207
+ snap = Repos.get_snapshot(comm.rev)
208
+ return -1 if snap.files.nil?
209
+
172
210
  # object and finally push all files of it to the # workspace
173
- @repo.get_snapshot(@repo.history.last).files.each do |file|
211
+ snap.files.each do |file|
174
212
  idx = indexOf(file.path)
213
+ puts file
175
214
  if idx == -1
176
- @files << file
215
+ @@files << file
177
216
  else
178
- @files[idx] = file
217
+ @@files[idx] = file
179
218
  end
180
- content = @revlog.get_file(file.history.last)
219
+ content = RevLog.get_file(file.history.last)
181
220
  writeFile(file.path, content)
182
221
  end
183
222
  end
184
223
 
185
- def status(comm)
224
+ # wrapper for Repos merge_snapshot, update workspace with result
225
+ def Workspace.merge(id)
226
+ # returns [{path => content}, [conflicting paths]]
227
+ Repos.merge_snapshot(id)
228
+
229
+ # todo update workspace with result
230
+ # todo return any conflicting files
231
+ end
232
+
233
+ def Workspace.status
186
234
  added = []
187
235
  edits = []
188
236
  remov = []
@@ -192,13 +240,13 @@ module Copernicium
192
240
  added << f
193
241
  else # changed file?
194
242
  x2 = readFile(f) # get the current version
195
- x1 = @revlog.get_file(@files[idx].history.last)
243
+ x1 = RevLog.get_file(@@files[idx].history.last)
196
244
  edits << f if x1 != x2
197
245
  end
198
246
  end
199
247
 
200
248
  # any deleted files from the last commit?
201
- @files.each { |f| remov << f.path unless (ws_files.include? f.path) }
249
+ @@files.each { |f| remov << f.path unless (ws_files.include? f.path) }
202
250
 
203
251
  [added, edits, remov]
204
252
  end
metadata CHANGED
@@ -1,113 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: copernicium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Team Copernicium
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-02 00:00:00.000000000 Z
11
+ date: 2015-12-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: diffy
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.0'
20
- - - '>='
20
+ - - ">="
21
21
  - !ruby/object:Gem::Version
22
22
  version: 3.0.7
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ~>
27
+ - - "~>"
28
28
  - !ruby/object:Gem::Version
29
29
  version: '3.0'
30
- - - '>='
30
+ - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 3.0.7
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: net-scp
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ~>
37
+ - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '1.2'
40
- - - '>='
40
+ - - ">="
41
41
  - !ruby/object:Gem::Version
42
42
  version: 1.2.1
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - ~>
47
+ - - "~>"
48
48
  - !ruby/object:Gem::Version
49
49
  version: '1.2'
50
- - - '>='
50
+ - - ">="
51
51
  - !ruby/object:Gem::Version
52
52
  version: 1.2.1
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: net-ssh
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
- - - ~>
57
+ - - "~>"
58
58
  - !ruby/object:Gem::Version
59
59
  version: '3.0'
60
- - - '>='
60
+ - - ">="
61
61
  - !ruby/object:Gem::Version
62
62
  version: 3.0.1
63
63
  type: :runtime
64
64
  prerelease: false
65
65
  version_requirements: !ruby/object:Gem::Requirement
66
66
  requirements:
67
- - - ~>
67
+ - - "~>"
68
68
  - !ruby/object:Gem::Version
69
69
  version: '3.0'
70
- - - '>='
70
+ - - ">="
71
71
  - !ruby/object:Gem::Version
72
72
  version: 3.0.1
73
73
  - !ruby/object:Gem::Dependency
74
74
  name: minitest
75
75
  requirement: !ruby/object:Gem::Requirement
76
76
  requirements:
77
- - - ~>
77
+ - - "~>"
78
78
  - !ruby/object:Gem::Version
79
79
  version: '5.8'
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: 5.8.1
83
83
  type: :development
84
84
  prerelease: false
85
85
  version_requirements: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ~>
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
89
  version: '5.8'
90
- - - '>='
90
+ - - ">="
91
91
  - !ruby/object:Gem::Version
92
92
  version: 5.8.1
93
93
  - !ruby/object:Gem::Dependency
94
94
  name: minitest-reporters
95
95
  requirement: !ruby/object:Gem::Requirement
96
96
  requirements:
97
- - - ~>
97
+ - - "~>"
98
98
  - !ruby/object:Gem::Version
99
99
  version: '1.1'
100
- - - '>='
100
+ - - ">="
101
101
  - !ruby/object:Gem::Version
102
102
  version: 1.1.4
103
103
  type: :development
104
104
  prerelease: false
105
105
  version_requirements: !ruby/object:Gem::Requirement
106
106
  requirements:
107
- - - ~>
107
+ - - "~>"
108
108
  - !ruby/object:Gem::Version
109
109
  version: '1.1'
110
- - - '>='
110
+ - - ">="
111
111
  - !ruby/object:Gem::Version
112
112
  version: 1.1.4
113
113
  description: A simple distributed version control system written in Ruby.
@@ -135,17 +135,17 @@ require_paths:
135
135
  - lib
136
136
  required_ruby_version: !ruby/object:Gem::Requirement
137
137
  requirements:
138
- - - '>='
138
+ - - ">="
139
139
  - !ruby/object:Gem::Version
140
140
  version: '0'
141
141
  required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  requirements:
143
- - - '>='
143
+ - - ">="
144
144
  - !ruby/object:Gem::Version
145
145
  version: '0'
146
146
  requirements: []
147
147
  rubyforge_project:
148
- rubygems_version: 2.4.8
148
+ rubygems_version: 2.4.5.1
149
149
  signing_key:
150
150
  specification_version: 4
151
151
  summary: Simple DVCS in Ruby.