summaryrefslogtreecommitdiffstats
path: root/bdep/status.cxx
blob: c31f156afe8a3422acc9df83bb800766b799b1be (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
// file      : bdep/status.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bdep/status.hxx>

#include <iostream> // cout

#include <bdep/project.hxx>
#include <bdep/database.hxx>
#include <bdep/diagnostics.hxx>

#include <bdep/fetch.hxx>

using namespace std;

namespace bdep
{
  static void
  cmd_status (const cmd_status_options& o,
              const dir_path& prj,
              const dir_path& cfg,
              const strings& pkgs,
              bool fetch)
  {
    // Shallow fetch the project to make sure we show latest iterations and
    // pick up any new repositories.
    //
    // We do it in a separate command for the same reason as in sync.
    //
    if (fetch)
      run_bpkg (3,
                o,
                "fetch",
                "-d", cfg,
                "--shallow",
                "dir:" + prj.string ());

    // Don't show the hold status since the only packages that will normally
    // be held are the project's. But do show dependency constraints.
    //
    run_bpkg (2,
              o,
              "status",
              "-d", cfg,
              "--no-hold",
              "--constraint",
              (o.old_available () ? "--old-available" : nullptr),
              (o.immediate () ? "--immediate" :
               o.recursive () ? "--recursive" : nullptr),
              pkgs);
  }

  static void
  cmd_status (const cmd_status_options& o,
              const dir_path& prj,
              const shared_ptr<configuration>& c,
              const package_locations& ps,
              bool fetch)
  {
    assert (!c->packages.empty ());

    // If no packages were explicitly specified, then we print the status for
    // all that have been initialized in the configuration.
    //
    strings pkgs;

    if (ps.empty ())
    {
      for (const package_state& p: c->packages)
        pkgs.push_back (p.name.string ());
    }
    else
    {
      for (const package_location& p: ps)
        pkgs.push_back (p.name.string ());
    }

    cmd_status (o, prj, c->path, pkgs, fetch);
  }

  int
  cmd_status (const cmd_status_options& o, cli::scanner& args)
  {
    tracer trace ("status");

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

    // We have two pretty different modes: project package status and
    // dependency package status (have arguments).
    //
    strings dep_pkgs;
    for (; args.more (); dep_pkgs.push_back (args.next ())) ;

    // The same ignore/load story as in sync.
    //
    project_packages pp (
      find_project_packages (o,
                             !dep_pkgs.empty () /* ignore_packages */,
                             false              /* load_packages   */));

    const dir_path& prj (pp.project);

    database db (open (prj, trace));

    transaction t (db.begin ());
    configurations cfgs (find_configurations (o, prj, t));
    t.commit ();

    // If specified, verify packages are present in each configuration.
    //
    if (!pp.packages.empty ())
      verify_project_packages (pp, cfgs);

    // Print status in each configuration skipping empty ones.
    //
    bool first (true);
    for (const shared_ptr<configuration>& c: cfgs)
    {
      if (c->packages.empty ())
      {
        if (verb)
          info << "skipping empty configuration " << *c;

        continue;
      }

      // If we are printing multiple configurations, separate them with a
      // blank line and print the configuration name/directory.
      //
      if (verb && cfgs.size () > 1)
      {
        cout << (first ? "" : "\n")
             << "in configuration " << *c << ':' << endl;

        first = false;
      }

      bool fetch (o.fetch () || o.fetch_full ());

      if (fetch)
        cmd_fetch (o, prj, c, o.fetch_full ());

      if (dep_pkgs.empty ())
        cmd_status (o, prj, c, pp.packages, !fetch);
      else
        cmd_status (o, prj, c->path, dep_pkgs, !fetch);
    }

    return 0;
  }
}