aboutsummaryrefslogtreecommitdiff
path: root/bpkg/rep-add.cxx
blob: 7adb8433f9f45d3ef47b7e1e72df73420fe0e1ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// file      : bpkg/rep-add.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bpkg/rep-add.hxx>

#include <bpkg/package.hxx>
#include <bpkg/package-odb.hxx>
#include <bpkg/database.hxx>
#include <bpkg/diagnostics.hxx>
#include <bpkg/manifest-utility.hxx>

using namespace std;
using namespace butl;

namespace bpkg
{
  int
  rep_add (const rep_add_options& o, cli::scanner& args)
  {
    tracer trace ("rep_add");

    dir_path c (o.directory ());
    l4 ([&]{trace << "configuration: " << c;});

    if (!args.more ())
      fail << "repository location argument expected" <<
        info << "run 'bpkg help rep-add' for more information";

    repository_location rl (
      parse_location (args.next (),
                      o.type_specified ()
                      ? optional<repository_type> (o.type ())
                      : nullopt));

    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 ());
    session s; // Repository dependencies can have cycles.

    // 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 repository " << rn;

    return 0;
  }
}