jamis / capistrano

Remote multi-server automation tool. This repository is no longer being actively maintained. Please ask on the mailing list to find someone who has a well-maintained fork. Thanks!

This URL has Read+Write access

capistrano / lib / capistrano / recipes / deploy / scm / mercurial.rb
93cc6338 » jamis 2007-06-05 Add mercurial SCM support 1 # Copyright 2007 Matthew Elder <sseses@gmail.com>
2 # based on work by Tobias Luetke
3
4 require 'capistrano/recipes/deploy/scm/base'
5
6 module Capistrano
7 module Deploy
8 module SCM
9
10 # Implements the Capistrano SCM interface for the Mercurial revision
11 # control system (http://www.selenic.com/mercurial/).
12 # Latest updates at http://tackletechnology.org/oss/cap2-mercurial
13 class Mercurial < Base
14 # Sets the default command name for this SCM. Users may override this
15 # by setting the :scm_command variable.
16 default_command "hg"
17
18 # For mercurial HEAD == tip except that it bases this assumption on what
19 # tip is in the current repository (so push before you deploy)
20 def head
21 "tip"
22 end
23
24 # Clone the repository and update to the specified changeset.
25 def checkout(changeset, destination)
26 clone(destination) + " && " + update(changeset, destination)
27 end
28
29 # Pull from the repository and update to the specified changeset.
30 def sync(changeset, destination)
31 pull(destination) + " && " + update(changeset, destination)
32 end
33
34 # One day we will have hg archive, although i think its not needed
35 def export(revision, destination)
36 raise NotImplementedError, "`diff' is not implemented by #{self.class.name}" +
37 "use checkout strategy"
38 end
39
40 # Compute the difference between the two changesets +from+ and +to+
41 # as a unified diff.
42 def diff(from, to=nil)
43 scm :diff,
44 "--rev #{from}",
45 (to ? "--rev #{to}" : nil)
46 end
47
48 # Return a log of all changes between the two specified changesets,
49 # +from+ and +to+, inclusive or the log for +from+ if +to+ is omitted.
50 def log(from, to=nil)
51 scm :log,
52 verbose,
53 "--rev #{from}" +
54 (to ? ":#{to}" : "")
55 end
56
57 # Translates a tag to a changeset if needed or just returns changeset.
58 def query_revision(changeset)
59 cmd = scm :log,
60 verbose,
61 "-r #{changeset}",
62 "--template '{node|short}'"
63 yield cmd
64 end
65
66 # Determine response for SCM prompts
67 # user/pass can come from ssh and http distribution methods
68 # yes/no is for when ssh asks you about fingerprints
69 def handle_data(state, stream, text)
70 logger.info "[#{stream}] #{text}"
71 case text
72 when /^user:/mi
07d777b7 » jamis 2008-02-22 Add support for password pr... 73 # support :scm_user for backwards compatibility of this module
74 if user = variable(:scm_username) || variable(:scm_user)
75 "#{user}\n"
93cc6338 » jamis 2007-06-05 Add mercurial SCM support 76 else
07d777b7 » jamis 2008-02-22 Add support for password pr... 77 raise "No variable :scm_username specified and Mercurial asked!\n" +
93cc6338 » jamis 2007-06-05 Add mercurial SCM support 78 "Prompt was: #{text}"
79 end
07d777b7 » jamis 2008-02-22 Add support for password pr... 80 when /\bpassword:/mi
81 unless pass = scm_password_or_prompt
82 # fall back on old behavior of erroring out with msg
93cc6338 » jamis 2007-06-05 Add mercurial SCM support 83 raise "No variable :scm_password specified and Mercurial asked!\n" +
84 "Prompt was: #{text}"
85 end
07d777b7 » jamis 2008-02-22 Add support for password pr... 86 "#{pass}\n"
93cc6338 » jamis 2007-06-05 Add mercurial SCM support 87 when /yes\/no/i
e1d3027e » jamis 2007-06-25 Make user input for yes/no ... 88 "yes\n"
93cc6338 » jamis 2007-06-05 Add mercurial SCM support 89 end
90 end
07d777b7 » jamis 2008-02-22 Add support for password pr... 91
93cc6338 » jamis 2007-06-05 Add mercurial SCM support 92 private
93
94 # Fine grained mercurial commands
95 def clone(destination)
96 scm :clone,
97 verbose,
98 "--noupdate", # do not update to tip when cloning is done
99 repository, # clone which repository?
100 destination # and put the clone where?
101 end
102
103 def pull(destination)
104 scm :pull,
105 verbose,
106 "--repository #{destination}", # pull changes into what?
107 repository # and pull the changes from?
108 end
109
110 def update(changeset, destination)
111 scm :update,
112 verbose,
113 "--repository #{destination}", # update what?
114 "--clean", # ignore untracked changes
115 changeset # update to this changeset
116 end
117
118 # verbosity configuration grokking :)
119 def verbose
120 case variable(:scm_verbose)
121 when nil: nil
122 when false: "--quiet"
123 else "--verbose"
124 end
125 end
07d777b7 » jamis 2008-02-22 Add support for password pr... 126
127 # honor Cap 2.1+'s :scm_prefer_prompt if present
128 def scm_password_or_prompt
129 @scm_password_or_prompt ||= variable(:scm_password) ||
130 (Capistrano::CLI.password_prompt("hg password: ") if variable(:scm_prefer_prompt))
131 end
93cc6338 » jamis 2007-06-05 Add mercurial SCM support 132
133 end
134 end
135 end
136 end