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