|
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) |
|
d48ed9a9
»
|
jamis |
2008-05-31 |
report hostname with output... |
70 |
host = state[:channel][:host] |
| |
71 |
logger.info "[#{host} :: #{stream}] #{text}" |
|
93cc6338
»
|
jamis |
2007-06-05 |
Add mercurial SCM support |
72 |
case text |
| |
73 |
when /^user:/mi |
|
07d777b7
»
|
jamis |
2008-02-22 |
Add support for password pr... |
74 |
# support :scm_user for backwards compatibility of this module |
| |
75 |
if user = variable(:scm_username) || variable(:scm_user) |
| |
76 |
"#{user}\n" |
|
93cc6338
»
|
jamis |
2007-06-05 |
Add mercurial SCM support |
77 |
else |
|
07d777b7
»
|
jamis |
2008-02-22 |
Add support for password pr... |
78 |
raise "No variable :scm_username specified and Mercurial asked!\n" + |
|
93cc6338
»
|
jamis |
2007-06-05 |
Add mercurial SCM support |
79 |
"Prompt was: #{text}" |
| |
80 |
end |
|
07d777b7
»
|
jamis |
2008-02-22 |
Add support for password pr... |
81 |
when /\bpassword:/mi |
| |
82 |
unless pass = scm_password_or_prompt |
| |
83 |
# fall back on old behavior of erroring out with msg |
|
93cc6338
»
|
jamis |
2007-06-05 |
Add mercurial SCM support |
84 |
raise "No variable :scm_password specified and Mercurial asked!\n" + |
| |
85 |
"Prompt was: #{text}" |
| |
86 |
end |
|
07d777b7
»
|
jamis |
2008-02-22 |
Add support for password pr... |
87 |
"#{pass}\n" |
|
93cc6338
»
|
jamis |
2007-06-05 |
Add mercurial SCM support |
88 |
when /yes\/no/i |
|
e1d3027e
»
|
jamis |
2007-06-25 |
Make user input for yes/no ... |
89 |
"yes\n" |
|
93cc6338
»
|
jamis |
2007-06-05 |
Add mercurial SCM support |
90 |
end |
| |
91 |
end |
|
07d777b7
»
|
jamis |
2008-02-22 |
Add support for password pr... |
92 |
|
|
93cc6338
»
|
jamis |
2007-06-05 |
Add mercurial SCM support |
93 |
private |
| |
94 |
|
| |
95 |
# Fine grained mercurial commands |
| |
96 |
def clone(destination) |
| |
97 |
scm :clone, |
| |
98 |
verbose, |
| |
99 |
"--noupdate", # do not update to tip when cloning is done |
| |
100 |
repository, # clone which repository? |
| |
101 |
destination # and put the clone where? |
| |
102 |
end |
| |
103 |
|
| |
104 |
def pull(destination) |
| |
105 |
scm :pull, |
| |
106 |
verbose, |
| |
107 |
"--repository #{destination}", # pull changes into what? |
| |
108 |
repository # and pull the changes from? |
| |
109 |
end |
| |
110 |
|
| |
111 |
def update(changeset, destination) |
| |
112 |
scm :update, |
| |
113 |
verbose, |
| |
114 |
"--repository #{destination}", # update what? |
| |
115 |
"--clean", # ignore untracked changes |
| |
116 |
changeset # update to this changeset |
| |
117 |
end |
| |
118 |
|
| |
119 |
# verbosity configuration grokking :) |
| |
120 |
def verbose |
| |
121 |
case variable(:scm_verbose) |
| |
122 |
when nil: nil |
| |
123 |
when false: "--quiet" |
| |
124 |
else "--verbose" |
| |
125 |
end |
| |
126 |
end |
|
07d777b7
»
|
jamis |
2008-02-22 |
Add support for password pr... |
127 |
|
| |
128 |
# honor Cap 2.1+'s :scm_prefer_prompt if present |
| |
129 |
def scm_password_or_prompt |
| |
130 |
@scm_password_or_prompt ||= variable(:scm_password) || |
| |
131 |
(Capistrano::CLI.password_prompt("hg password: ") if variable(:scm_prefer_prompt)) |
| |
132 |
end |
|
93cc6338
»
|
jamis |
2007-06-05 |
Add mercurial SCM support |
133 |
|
| |
134 |
end |
| |
135 |
end |
| |
136 |
end |
| |
137 |
end |