aboutsummaryrefslogtreecommitdiff
path: root/bpkg/rep-add.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2015-09-21 18:08:39 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2015-09-21 18:08:39 +0200
commit221f0250fcd7cba4fa4b5e4fd6c0d410eb6e5811 (patch)
tree3ea5eca558f612fcc9b27db2778d4bfa47e38556 /bpkg/rep-add.cxx
parent9792fc9d137b4dd702360ac0242f9a7a26e675c2 (diff)
Implement rep-add command
Diffstat (limited to 'bpkg/rep-add.cxx')
-rw-r--r--bpkg/rep-add.cxx82
1 files changed, 82 insertions, 0 deletions
diff --git a/bpkg/rep-add.cxx b/bpkg/rep-add.cxx
new file mode 100644
index 0000000..803c68d
--- /dev/null
+++ b/bpkg/rep-add.cxx
@@ -0,0 +1,82 @@
+// file : bpkg/rep-add.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <bpkg/rep-add>
+
+#include <stdexcept> // invalid_argument
+
+#include <bpkg/types>
+#include <bpkg/package>
+#include <bpkg/package-odb>
+#include <bpkg/utility>
+#include <bpkg/database>
+#include <bpkg/diagnostics>
+
+using namespace std;
+using namespace butl;
+
+namespace bpkg
+{
+ void
+ rep_add (const rep_add_options& o, cli::scanner& args)
+ {
+ tracer trace ("rep_add");
+
+ dir_path c (o.directory ());
+ level4 ([&]{trace << "configuration: " << c;});
+
+ if (!args.more ())
+ fail << "repository location argument expected" <<
+ info << "run 'bpkg help rep-add' for more information";
+
+ // Figure out the repository location.
+ //
+ const char* s (args.next ());
+ repository_location rl;
+ try
+ {
+ rl = repository_location (s, repository_location ());
+
+ if (rl.relative ()) // Throws if location is empty.
+ rl = repository_location (
+ dir_path (s).complete ().normalize ().string ());
+ }
+ catch (const invalid_argument& e)
+ {
+ fail << "invalid repository location '" << s << "': " << e.what ();
+ }
+
+ const string& rn (rl.canonical_name ());
+
+ // Create the new repository and add is as a complement to the root.
+ //
+ database db (open (c, trace));
+ transaction t (db.begin ());
+
+ // It is possible that this repository is already in the database.
+ // For example, it might be a prerequisite of one of the already
+ // added repository.
+ //
+ shared_ptr<repository> r (db.find<repository> (rl.canonical_name ()));
+
+ if (r == nullptr)
+ {
+ r.reset (new repository (rl));
+ db.persist (r);
+ }
+
+ shared_ptr<repository> root (db.load<repository> (""));
+
+ if (!root->complements.insert (lazy_shared_ptr<repository> (db, r)).second)
+ {
+ fail << rn << " is already a repository of this configuration";
+ }
+
+ db.update (root);
+ t.commit ();
+
+ if (verb)
+ text << "added " << rn;
+ }
+}