aboutsummaryrefslogtreecommitdiff
path: root/bpkg/pkg-status.cxx
blob: b4b8de627cd5434e298b3ce6655dabde40f8f841 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// file      : bpkg/pkg-status.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bpkg/pkg-status.hxx>

#include <iostream>   // cout

#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
{
  struct package
  {
    package_name                 name;
    bpkg::version                version;    // Empty if unspecified.
    shared_ptr<selected_package> selected;   // NULL if none selected.
    optional<version_constraint> constraint; // Version constraint, if any.
  };
  using packages = vector<package>;

  // If recursive or immediate is true, then print status for dependencies
  // indented by two spaces.
  //
  static void
  pkg_status (const pkg_status_options& o,
              database& db,
              const packages& pkgs,
              string& indent,
              bool recursive,
              bool immediate)
  {
    tracer trace ("pkg_status");

    for (const package& p: pkgs)
    {
      l4 ([&]{trace << "package " << p.name << "; version " << p.version;});

      // Can't be both.
      //
      assert (p.version.empty () || !p.constraint);

      const shared_ptr<selected_package>& s (p.selected);

      // Look for available packages.
      //
      // Some of them are only available to upgrade/downgrade as dependencies.
      //
      struct apkg
      {
        shared_ptr<available_package> package;
        bool build;
      };
      vector<apkg> apkgs;

      // A package with this name is known in available packages potentially
      // for build.
      //
      bool known (false);
      bool build (false);
      {
        shared_ptr<repository_fragment> root (
          db.load<repository_fragment> (""));

        using query = query<available_package>;

        query q (query::id.name == p.name);
        {
          auto r (db.query<available_package> (q));
          known = !r.empty ();
          build = filter_one (root, move (r)).first != nullptr;
        }

        if (known)
        {
          // If the user specified the version, then only look for that
          // specific version (we still do it since there might be other
          // revisions).
          //
          if (!p.version.empty ())
            q = q && compare_version_eq (query::id.version,
                                         canonical_version (p.version),
                                         p.version.revision.has_value (),
                                         false /* iteration */);

          // And if we found an existing package, then only look for versions
          // greater than to what already exists unless we were asked to show
          // old versions.
          //
          // Note that for a system wildcard version we will always show all
          // available versions (since it is 0).
          //
          if (s != nullptr && !o.old_available ())
            q = q && query::id.version > canonical_version (s->version);

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

          // Packages that are in repositories that were explicitly added to
          // the configuration and their complements, recursively, are also
          // available to build.
          //
          for (shared_ptr<available_package> ap:
                 pointer_result (
                   db.query<available_package> (q)))
          {
            bool build (filter (root, ap));
            apkgs.push_back (apkg {move (ap), build});
          }
        }
      }

      cout << indent;

      // Selected.
      //

      // Hold package status.
      //
      if (s != nullptr)
      {
        if (s->hold_package && !o.no_hold () && !o.no_hold_package ())
          cout << '!';
      }

      // If the package name is selected, then print its exact spelling.
      //
      cout << (s != nullptr ? s->name : p.name);

      if (o.constraint () && p.constraint)
        cout << ' ' << *p.constraint;

      cout << ' ';

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

        if (s->substate != package_substate::none)
          cout << ',' << s->substate;

        cout << ' ';

        if (s->hold_version && !o.no_hold () && !o.no_hold_version ())
          cout << '!';

        cout << s->version_string ();
      }

      // Available.
      //
      bool available (false);
      if (known)
      {
        // Available from the system.
        //
        // The idea is that in the future we will try to auto-discover a
        // system version and then print that. For now we just say "maybe
        // available from the system" even if the version was specified by
        // the user. We will later compare it if the user did specify the
        // version.
        //
        string sys;
        if (o.system ())
        {
          sys = "?";
          available = true;
        }

        // Get rid of stubs.
        //
        for (auto i (apkgs.begin ()); i != apkgs.end (); ++i)
        {
          if (i->package->stub ())
          {
            // All the rest are stubs so bail out.
            //
            apkgs.erase (i, apkgs.end ());
            break;
          }

          available = true;
        }

        if (available)
        {
          cout << (s != nullptr ? " " : "") << "available";

          for (const apkg& a: apkgs)
          {
            const version& v (a.package->version);

            // Show the currently selected version in parenthesis.
            //
            bool cur (s != nullptr &&  v == s->version);

            cout << ' '
                 << (cur ? "(" : a.build ? "" : "[")
                 << v
                 << (cur ? ")" : a.build ? "" : "]");
          }

          if (!sys.empty ())
            cout << ' '
                 << (build ? "" : "[")
                 << "sys:" << sys
                 << (build ? "" : "]");
        }
      }

      if (s == nullptr && !available)
      {
        cout << "unknown";

        // Print the user's version if specified.
        //
        if (!p.version.empty ())
          cout << ' ' << p.version;
      }

      cout << endl;

      if (recursive || immediate)
      {
        // Collect and recurse.
        //
        packages dpkgs;
        if (s != nullptr)
        {
          for (const auto& pair: s->prerequisites)
          {
            shared_ptr<selected_package> d (pair.first.load ());
            const optional<version_constraint>& c (pair.second);
            dpkgs.push_back (package {d->name, version (), move (d), c});
          }
        }

        if (!dpkgs.empty ())
        {
          indent += "  ";
          pkg_status (o,
                      db,
                      dpkgs,
                      indent,
                      recursive,
                      false /* immediate */);
          indent.resize (indent.size () - 2);
        }
      }
    }
  }

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

    if (o.immediate () && o.recursive ())
      fail << "both --immediate|-i and --recursive|-r specified";

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

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

    packages pkgs;
    {
      using query = query<selected_package>;

      if (args.more ())
      {
        while (args.more ())
        {
          const char* arg (args.next ());
          package p {parse_package_name (arg),
                     parse_package_version (arg,
                                            false /* allow_wildcard */,
                                            false /* fold_zero_revision */),
                     nullptr  /* selected */,
                     nullopt  /* constraint */};

          // Search in the packages that already exist in this configuration.
          //
          {
            query q (query::name == p.name);

            if (!p.version.empty ())
              q = q && compare_version_eq (query::version,
                                           canonical_version (p.version),
                                           p.version.revision.has_value (),
                                           false /* iteration */);

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

          pkgs.push_back (move (p));
        }
      }
      else
      {
        // Find all held packages.
        //
        for (shared_ptr<selected_package> s:
               pointer_result (
                 db.query<selected_package> (query::hold_package)))
        {
          pkgs.push_back (package {s->name, version (), move (s), nullopt});
        }

        if (pkgs.empty ())
        {
          info << "no held packages in the configuration";
          return 0;
        }
      }
    }

    string indent;
    pkg_status (o, db, pkgs, indent, o.recursive (), o.immediate ());

    t.commit ();
    return 0;
  }
}