aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/version/snapshot.cxx
blob: 3a885b5249801cff678038e3e4f17142938f05a5 (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
// file      : libbuild2/version/snapshot.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/version/snapshot.hxx>

#include <libbuild2/filesystem.hxx>

using namespace std;

namespace build2
{
  namespace version
  {
    snapshot
    extract_snapshot_git (const dir_path&);

    static const path git (".git");

    snapshot
    extract_snapshot (const scope& rs)
    {
      // Resolve the path symlink components to make sure that if we are
      // extracting snapshot for a subproject which is symlinked from the git
      // submodule, then we end up with a root of the git submodule repository
      // rather than the containing repository root.
      //
      dir_path d (rs.src_path ());

      try
      {
        d.realize ();
      }
      catch (const invalid_path&) // Some component doesn't exist.
      {
        return snapshot ();
      }
      catch (const system_error& e)
      {
        fail << "unable to obtain real path for " << d << ": " << e;
      }

      for (; !d.empty (); d = d.directory ())
      {
        // .git can be either a directory or a file in case of a submodule.
        //
        if (butl::entry_exists (d / git,
                                true /* follow_symlinks */,
                                true /* ignore_errors */))
          return extract_snapshot_git (d);
      }

      return snapshot ();
    }
  }
}