monotone

monotone Mtn Source Tree

Root/app_state.cc

1// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#include "base.hh"
11#include "vector.hh"
12
13#include "botan/pubkey.h"
14#include "botan/rsa.h"
15
16#include "app_state.hh"
17#include "charset.hh"
18#include "database.hh"
19#include "file_io.hh"
20#include "platform.hh"
21#include "sanity.hh"
22#include "ui.hh"
23#include "work.hh"
24
25using std::exception;
26using std::map;
27using std::string;
28using std::vector;
29using std::vector;
30
31app_state::app_state()
32 : db(system_path()),
33 keys(*this), work(db, lua),
34 branch_is_sticky(false),
35 mtn_automate_allowed(false),
36 project(*this)
37{
38 db.set_app(this);
39 lua.set_app(this);
40 keys.set_key_dir(opts.conf_dir / "keys");
41}
42
43app_state::~app_state()
44{
45}
46
47
48void
49app_state::allow_workspace()
50{
51 found_workspace = find_and_go_to_workspace(opts.root);
52
53 if (found_workspace)
54 {
55 bookkeeping_path dump_path;
56 work.get_local_dump_path(dump_path);
57
58 // The 'false' means that, e.g., if we're running checkout,
59 // then it's okay for dumps to go into our starting working
60 // dir's _MTN rather than the new workspace dir's _MTN.
61 global_sanity.set_dump_path(system_path(dump_path, false).as_external());
62 }
63 load_rcfiles();
64}
65
66void
67app_state::process_options()
68{
69 system_path database_option;
70 branch_name branch_option;
71 rsa_keypair_id key_option;
72 system_path keydir_option;
73
74 if (!found_workspace)
75 return;
76
77 work.check_ws_format();
78 work.get_ws_options(database_option, branch_option,
79 key_option, keydir_option);
80
81 // Workspace options are not to override the command line.
82 if (db.get_filename().as_internal().empty() && !database_option.as_internal().empty())
83 db.set_filename(database_option);
84
85 if (keys.get_key_dir().as_internal().empty() && !keydir_option.as_internal().empty())
86 set_key_dir(keydir_option);
87
88 if (opts.branchname().empty() && !branch_option().empty())
89 {
90 opts.branchname = branch_option;
91 branch_is_sticky = true;
92 }
93
94 L(FL("branch name is '%s'") % opts.branchname);
95
96 if (!opts.key_given)
97 opts.signing_key = key_option;
98}
99
100void
101app_state::write_options()
102{
103 system_path database_option;
104 branch_name branch_option;
105 rsa_keypair_id key_option;
106 system_path keydir_option;
107
108 database_option = db.get_filename();
109 keydir_option = keys.get_key_dir();
110
111 if (branch_is_sticky)
112 branch_option = opts.branchname;
113
114 if (opts.key_given)
115 key_option = opts.signing_key;
116
117 work.set_ws_options(database_option, branch_option,
118 key_option, keydir_option);
119}
120
121void
122app_state::require_workspace(string const & explanation)
123{
124 N(found_workspace,
125 F("workspace required but not found%s%s")
126 % (explanation.empty() ? "" : "\n") % explanation);
127 write_options();
128}
129
130void
131app_state::create_workspace(system_path const & new_dir)
132{
133 N(!new_dir.empty(), F("invalid directory ''"));
134
135 L(FL("creating workspace in %s") % new_dir);
136
137 mkdir_p(new_dir);
138 go_to_workspace(new_dir);
139 mark_std_paths_used();
140
141 N(!directory_exists(bookkeeping_root),
142 F("monotone bookkeeping directory '%s' already exists in '%s'")
143 % bookkeeping_root % new_dir);
144
145 L(FL("creating bookkeeping directory '%s' for workspace in '%s'")
146 % bookkeeping_root % new_dir);
147
148 mkdir_p(bookkeeping_root);
149
150 make_branch_sticky();
151
152 write_options();
153
154 work.write_ws_format();
155 work.blank_user_log();
156
157 if (lua.hook_use_inodeprints())
158 work.enable_inodeprints();
159
160 found_workspace = true;
161
162 bookkeeping_path dump_path;
163 work.get_local_dump_path(dump_path);
164 // The 'false' means that, e.g., if we're running checkout,
165 // then it's okay for dumps to go into our starting working
166 // dir's _MTN rather than the new workspace dir's _MTN.
167 global_sanity.set_dump_path(system_path(dump_path, false).as_external());
168
169 load_rcfiles();
170}
171
172void
173app_state::set_database(system_path const & filename)
174{
175 if (!filename.empty())
176 {
177 db.set_filename(filename);
178
179 if (found_workspace)
180 {
181 system_path database_option(filename);
182 branch_name branch_option;
183 rsa_keypair_id key_option;
184 system_path keydir_option;
185
186 work.set_ws_options(database_option, branch_option,
187 key_option, keydir_option);
188 }
189 }
190}
191
192void
193app_state::set_key_dir(system_path const & filename)
194{
195 if (!filename.empty())
196 {
197 keys.set_key_dir(filename);
198 }
199}
200
201void
202app_state::make_branch_sticky()
203{
204 branch_is_sticky = true;
205 if (found_workspace)
206 {
207 // Already have a workspace, can (must) write options directly,
208 // because no-one else will do so. If we don't have a workspace
209 // yet, then require_workspace (for instance) will call
210 // write_options when it finds one.
211 write_options();
212 }
213}
214
215project_t &
216app_state::get_project()
217{
218 return project;
219}
220
221// rc files are loaded after we've changed to the workspace so that
222// _MTN/monotonerc can be loaded between ~/.monotone/monotonerc and other
223// rcfiles.
224
225void
226app_state::load_rcfiles()
227{
228 // Built-in rc settings are defaults.
229
230 if (!opts.nostd)
231 lua.add_std_hooks();
232
233 // ~/.monotone/monotonerc overrides that, and
234 // _MTN/monotonerc overrides *that*.
235
236 if (!opts.norc)
237 {
238 system_path default_rcfile;
239 bookkeeping_path workspace_rcfile;
240 lua.default_rcfilename(default_rcfile);
241 lua.workspace_rcfilename(workspace_rcfile);
242 lua.load_rcfile(default_rcfile, false);
243 lua.load_rcfile(workspace_rcfile, false);
244 }
245
246 // Command-line rcfiles override even that.
247
248 for (args_vector::const_iterator i = opts.extra_rcfiles.begin();
249 i != opts.extra_rcfiles.end(); ++i)
250 {
251 lua.load_rcfile(*i);
252 }
253}
254
255// Local Variables:
256// mode: C++
257// fill-column: 76
258// c-file-style: "gnu"
259// indent-tabs-mode: nil
260// End:
261// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status