lib/braid/commands/update.rb in evilchelu-braid-0.4.0 vs lib/braid/commands/update.rb in evilchelu-braid-0.4.10
- old
+ new
@@ -1,79 +1,78 @@
module Braid
module Commands
class Update < Command
- def run(mirror, options = {})
- raise Braid::Git::LocalChangesPresent if invoke(:local_changes?)
+ def run(path, options = {})
+ bail_on_local_changes!
- in_work_branch do
- mirror ? update_one(mirror, options) : update_all
+ with_reset_on_error do
+ path ? update_one(path, options) : update_all(options)
end
end
protected
- def update_all
+ def update_all(options = {})
+ options.reject! { |k,v| %w(revision head).include?(k) }
msg "Updating all mirrors."
- config.mirrors.each do |mirror|
- update_one(mirror)
+ config.mirrors.each do |path|
+ update_one(path, options)
end
end
- def update_one(mirror, options = {})
- params = config.get(mirror)
- unless params
- msg "Mirror '#{mirror}/' does not exist. Skipping."
- return
- end
- local_branch = params["local_branch"]
+ def update_one(path, options = {})
+ mirror = config.get!(path)
- if check_for_lock(params, options)
- msg "Mirror '#{mirror}/' is locked to #{display_revision(params["type"], params["revision"])}. Skipping."
- return
+ # check options for lock modification
+ if mirror.locked?
+ if options["head"]
+ msg "Unlocking mirror '#{mirror.path}/'."
+ mirror.lock = nil
+ elsif !options["revision"]
+ msg "Mirror '#{mirror.path}/' is locked to #{display_revision(mirror, mirror.lock)}. Skipping."
+ return
+ end
end
- # unlock
- if params["revision"] && options["head"]
- msg "Unlocking mirror '#{mirror}/'."
- options["revision"] = nil
- end
+ mirror.fetch
- begin
- fetch_remote(params["type"], local_branch)
+ new_revision = validate_new_revision(mirror, options["revision"])
+ target_hash = determine_target_commit(mirror, new_revision)
- validate_revision_option(params, options)
- target = determine_target_commit(params, options)
-
- check_merge_status(target)
- rescue Braid::Commands::MirrorAlreadyUpToDate
- msg "Mirror '#{mirror}/' is already up to date. Skipping."
- update_revision(mirror, options["revision"])
+ if mirror.merged?(target_hash)
+ msg "Mirror '#{mirror.path}/' is already up to date. Skipping."
return
end
- msg "Updating #{params["type"]} mirror '#{mirror}/'."
+ diff = mirror.diff if mirror.squashed? # get diff before setting revision
- if params["squash"]
- invoke(:git_rm_r, mirror)
- invoke(:git_read_tree, target, mirror)
+ mirror.revision = new_revision
+ mirror.lock = new_revision if options["revision"]
+ config.update(mirror)
+
+ msg "Updating mirror '#{mirror.path}/'."
+ if mirror.squashed?
+ git.rm_r(mirror.path)
+ git.read_tree(target_hash, mirror.path)
+ unless diff.empty?
+ git.apply(diff, *(options["safe"] ? ["--reject"] : []))
+ end
else
- invoke(:git_merge_subtree, target)
+ git.merge_subtree(target_hash)
end
- update_revision(mirror, options["revision"])
add_config_file
- revision_message = " to " + (options["revision"] ? display_revision(params["type"], options["revision"]) : "HEAD")
- commit_message = "Update mirror '#{mirror}/'#{revision_message}."
- invoke(:git_commit, commit_message)
- end
+ revision_message = " to " + (options["revision"] ? display_revision(mirror) : "HEAD")
+ commit_message = "Update mirror '#{mirror.path}/'#{revision_message}"
+ git.commit(commit_message)
- private
- def check_for_lock(params, options)
- params["revision"] && !options["revision"] && !options["head"]
- end
-
- def update_revision(mirror, revision)
- config.update(mirror, { "revision" => revision })
+ rescue Operations::ShellExecutionError => error
+ if options["safe"]
+ msg "Caught shell error. Breaking."
+ exit(0)
+ else
+ raise error
+ end
end
end
end
end