aboutsummaryrefslogtreecommitdiff
path: root/bpkg/pkg-status.cxx
blob: dbd2bd07b43248bb4605ea4abcd0959a070b108b (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// file      : bpkg/pkg-status.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bpkg/pkg-status>

#include <iostream>   // cout
#include <functional> // function

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

using namespace std;
using namespace butl;

namespace bpkg
{
  void
  pkg_status (const pkg_status_options& o, cli::scanner& args)
  {
    tracer trace ("pkg_status");

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

    if (!args.more ())
      fail << "package name argument expected" <<
        info << "run 'bpkg help pkg-status' for more information";

    string n (args.next ());

    version v;
    if (args.more ())
      v = parse_version (args.next ());

    database db (open (c, trace));
    transaction t (db.begin ());
    session s;

    level4 ([&]{trace << "package " << n << "; version " << v;});

    // First search in the packages that already exist in this configuration.
    //
    shared_ptr<package> p;
    {
      using query = query<package>;
      query q (query::name == n);

      if (!v.empty ())
        q = q && query::version == v;

      p = db.query_one<package> (q);
    }

    // Now look for available packages. If the user specified the version
    // explicitly and we found the corresponding existing package, then
    // no need to look for it in available packages.
    //
    vector<shared_ptr<available_package>> aps;
    if (p == nullptr || v.empty ())
    {
      using query = query<available_package>;

      query q (query::id.name == n);

      // If we found an existing package, then only look for versions
      // greater than what already exists.
      //
      if (p != nullptr)
        q = q && query::id.version > p->version;
      else if (!v.empty ())
        //
        // Otherwise, if the user specified the version, then only look for
        // that specific version.
        //
        q = q && query::id.version == v;

      q += order_by_version_desc (query::id.version);

      // Only consider packages that are in repositories that were
      // explicitly added to the configuration and their complements,
      // transitively. While we could maybe come up with a (barely
      // comprehensible) view/query to achieve this, doing it on the
      // "client side" is definitely more straightforward.
      //
      shared_ptr<repository> root (db.load<repository> (""));

      for (shared_ptr<available_package> ap:
             pointer_result (db.query<available_package> (q)))
      {
        function<bool (const shared_ptr<repository>&)> find =
          [&ap, &find](const shared_ptr<repository>& r) -> bool
        {
          const auto& cs (r->complements);

          for (const package_location& pl: ap->locations)
          {
            // First check all the complements without loading them.
            //
            if (cs.find (pl.repository) != cs.end ())
              return true;

            // If not found, then load the complements and check them
            // recursively.
            //
            for (lazy_shared_ptr<repository> cr: cs)
            {
              if (find (cr.load ()))
                return true;
            }
          }

          return false;
        };

        level4 ([&]{trace << "available " << ap->version;});

        if (find (root))
          aps.push_back (ap);
      }
    }

    t.commit ();

    bool found (false);

    if (p != nullptr)
    {
      cout << p->state;

      // Also print the version of the package unless the user specified it.
      //
      if (v.empty ())
        cout << " " << p->version;

      found = true;
    }

    if (!aps.empty ())
    {
      cout << (found ? "; " : "") << "available";

      // If the user specified the version, then there will be only one
      // entry.
      //
      if (v.empty ())
      {
        for (shared_ptr<available_package> ap: aps)
          cout << ' ' << ap->version;
      }

      found = true;
    }

    if (!found)
      cout << "unknown";

    cout << endl;
  }
}