Module: Copernicium::Driver

Includes:
PushPull, Workspace
Defined in:
lib/ui.rb

Overview

main driver for the command line user interface

Instance Method Summary collapse

Methods included from PushPull

UICommandParser, connect, connection_failure, fetch, pclone, ppull, ppush, transfer

Methods included from Workspace

checkout, checkout_file, clean, clear, commit, commit_file, create_project, #getroot, indexOf, merge, #more_folders, #noroot?, #root_found, setup, status, working_files

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, 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, merge, setup, setup_tester, updatelog

Instance Method Details

#branch(args) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/ui.rb', line 138

def branch(args)
  branch = args.shift
  if branch.nil? # show all branches
    puts "Current: ".grn + Repos.current
    Repos.branches.each { |br| puts 'Branch: ' + br }
    puts "Total: ".grn + Repos.branches.length.to_s

  elsif branch == '-c' # create a new branch
    branch = args.first # get from the user
    branch = get "new branch name" if branch.nil?
    create_branch branch

  elsif branch == '-r' # rename branch
    newname = args.first # get if not specified
    newname = get "new name for current branch" if newname.nil?
    oldname = Repos.current
    create_branch newname
    Repos.delete_branch oldname
    puts "Deleted branch '#{oldname}'".grn
    puts "Renamed branch '#{oldname}' to '#{newname}'".grn

  elsif branch == '-d' # delete branch
    branch = args.first # If not specified, get
    branch = get "branch to delete" if branch.nil?
    if branch == Repos.current
      puts "Cannot delete the current branch!".red
    else # Delete the specified branch
      Repos.delete_branch branch
      puts "Deleted branch '#{branch}'".grn
    end

  elsif Repos.has_branch? branch # switch branch (branch <branch name>)
    Repos.update_branch branch
    puts 'Current: '.grn + Repos.current
    Workspace.checkout

  else # create it, switch to it
    Repos.create_branch branch
    Repos.update_branch branch
    Workspace.checkout
  end
end

#checkout(args) ⇒ Object

Take in a revision (snaptshot) id or branch Doesnt support file checkouts at this time



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/ui.rb', line 213

def checkout(args)
  if args.empty?
    rev = get 'branch or commit id'
  else
    rev = args.shift
    files = args unless args.empty?
  end

  # if 'head' keyword, grab the head
  if rev == 'head'
    rev = Repos.current_head
  elsif Repos.has_branch? rev
    branch = rev
    rev = Repos.history(rev).last
  end

  # call workspace checkout the given / branch
  Workspace.checkout(UIComm.new(rev: rev, files: files))
  Repos.update_branch branch unless branch.nil?
end

#clean(args = []) ⇒ Object



234
235
236
237
238
# File 'lib/ui.rb', line 234

def clean(args = [])
  ui = UIComm.new(command: 'clean', files: args)
  Workspace.clean(ui)
  ui
end

#clonecn(args) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/ui.rb', line 181

def clonecn(args)
  user = args.first
  host = args.last
  user = get 'username for push' if user.nil?
  host = get 'host path (<host:/dir/of/repo>)' if host.nil? || user == host
  comm = UIComm.new(command: 'clone', repo: host, opts: user)
  PushPull.UICommandParser(comm)
  return comm
end

#commit(args) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/ui.rb', line 240

def commit(args)
  messflag = args.find_index('-m')
  if messflag.nil?
    message = get 'commit message'
  elsif messflag == 0 # commit everything
    # mash everything after -m into a string
    message = args[1..-1].join ' '
  else # commit only some files
    files = args[0..messflag - 1]
  end

  # specified the -m flag, but didnt give anything
  message = get 'commit message' if message.nil?

  # perform the commit, with workspace
  ui = UIComm.new(command: 'commit', files: files, cmt_msg: message)
  puts 'New commit: '.grn + Workspace.commit(ui)
  ui
end

#create_branch(branch) ⇒ Object

create and switch to a new branch



132
133
134
135
136
# File 'lib/ui.rb', line 132

def create_branch(branch)
  new_branch_hash = Repos.make_branch branch
  Repos.update_branch branch
  puts "Created branch #{branch} ".grn + " with head #{new_branch_hash}"
end

#get(info) ⇒ Object

Get some info from the user when they dont specify it



101
102
103
104
# File 'lib/ui.rb', line 101

def get(info)
  puts "Note: #{info} not specified. Enter #{info} to continue.".yel
  gets.chomp # read a line from user, and return it
end

#history(args) ⇒ Object



260
261
262
263
264
# File 'lib/ui.rb', line 260

def history(args)
  Repos.current_snaps.reverse_each do |snap|
    puts (snap.time + ' | ') .grn + (snap.id + ' | ').yel + snap.msg
  end
end

#init(args) ⇒ Object

create a new copernicium repository



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

def init(args)
  if args.empty?
    root = Workspace.create_project
  else # init into a folder
    root = Workspace.create_project args.first
  end
  puts "Created Copernicium repo: ".grn + root
  UIComm.new(command: 'init', opts: args)
end

#merge(args) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/ui.rb', line 266

def merge(args)
  if args.empty?
    rev = get 'branch to merge'
  else
    rev = args.first
  end

  # If rev is a branch name, resolve it to a rev ID.
  if Repos.has_branch? rev
    rev = (Repos.history rev).last
    conflicts = Workspace.merge(rev)
    unless conflicts.nil?
      conflicts.each { |conflict| puts 'Conflict: '.red + conflict }
    end
  else # branch not found
    puts 'Branch not found: '.red + rev
  end
end

#pexit(msg, sig) ⇒ Object

Print and exit with a specific code



95
96
97
98
# File 'lib/ui.rb', line 95

def pexit(msg, sig)
  puts msg
  exit sig
end

#pull(args) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/ui.rb', line 201

def pull(args)
  user = args.first
  host = args.last
  user = get 'username for push' if user.nil?
  host = get 'host path (<host:/dir/of/repo>)' if host.nil? || user == host
  comm = UIComm.new(command: 'pull', repo: host, opts: user)
  PushPull.UICommandParser(comm)
  return comm
end

#push(args) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/ui.rb', line 191

def push(args)
  user = args.first
  host = args.last
  user = get 'username for push' if user.nil?
  host = get 'host path (<host:/dir/of/repo>)' if host.nil? || user == host
  comm = UIComm.new(command: 'push', repo: host, opts: user)
  PushPull.UICommandParser(comm)
  return comm
end

#run(args) ⇒ Object

Executes the required action for a given user command.

Parameters:

* args - an array containing the tokenized argv from the user
For instance: "cn hello world" -> ['hello', 'world']

Return value:

A UIComm object containing details of the command to be
executed by the respective backend module.


40
41
42
43
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ui.rb', line 40

def run(args)

  # if no arguments given show help information
  pexit HELP_BANNER, 0 if args.empty?

  # get first command
  cmd = args.shift

  # if -v flag givem show version
  pexit VERSION, 0 if cmd == '-v'

  # if no arguments given show help information
  pexit COMMAND_BANNER, 0 if (cmd == '-h' || cmd == 'help')

  # create the cn project, else already in one
  if cmd == 'init'
    noroot?? init(args) : puts(IN_REPO_WARNING.yel, getroot)
  elsif cmd == 'clone' # allow cloning a new repo
      clonecn args
  elsif noroot? # if not in a repo, warn them, tell how to create
    puts NO_REPO_WARNING.yel
  else # now, assume we are in a copernicum project
    Workspace.setup

    # Handle all other commands
    case cmd
    when 'status'
      status args
    when 'history'
      history args
    when 'branch'
      branch args
    when 'clean'
      clean args
    when 'commit'
      commit args
    when 'checkout'
      checkout args
    when 'merge'
      merge args
    when 'push'
      push args
    when 'pull'
      pull args
    when 'update'
      update args
    when 'init'
      # fall through - init handled above, before case statement
    else # handle an unrecognized argument, show help and exit
      pexit "Unrecognized command #{cmd}\n" + COMMAND_BANNER, 1
    end
  end # case
end

#status(args) ⇒ Object

show the current repos status



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ui.rb', line 118

def status(args)
  st = Workspace.status
  if st.all?(&:empty?)
    puts "No changes since last commit | ".grn +
      (Repos.current_snaps.last.time + ' | ').yel +
      Repos.current_snaps.last.msg
  else
    st[0].each { |f| puts "Added:   ".grn + f }
    st[1].each { |f| puts "Edited:  ".yel + f }
    st[2].each { |f| puts "Removed: ".red + f }
  end
end

#update(args) ⇒ Object



285
286
287
288
289
290
291
292
# File 'lib/ui.rb', line 285

def update(args)
  if args.empty?
    username = get 'user to update to'
  else
    username = args.first
  end
  Repos.update(UIComm.new(command: 'update', opts: username))
end