diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2018-08-06 21:24:27 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2018-08-07 15:10:44 +0300 |
commit | 7c665d965c0ebb259849d5032faa0854c6ae94f2 (patch) | |
tree | f646f43295e0d4f07e9d64ef8ddd9d42083dcced | |
parent | 8c1b8e19c68a4093c62e64d1716f484b87acac9d (diff) |
Add git utility functions
-rw-r--r-- | libbutl/git.cxx | 89 | ||||
-rw-r--r-- | libbutl/git.mxx | 46 |
2 files changed, 135 insertions, 0 deletions
diff --git a/libbutl/git.cxx b/libbutl/git.cxx new file mode 100644 index 0000000..bb5f542 --- /dev/null +++ b/libbutl/git.cxx @@ -0,0 +1,89 @@ +// file : libbutl/git.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef __cpp_modules +#include <libbutl/git.mxx> +#endif + +// C includes. + +#include <cassert> + +#ifndef __cpp_lib_modules +#include <string> + +#include <cstddef> // size_t +#endif + +// Other includes. + +#ifdef __cpp_modules +module butl.git; + +// Only imports additional to interface. +#ifdef __clang__ +#ifdef __cpp_lib_modules +import std.core; +#endif +import butl.path; +import butl.optional; +import butl.standard_version +#endif + +import butl.utility; // digit() +import butl.filesystem; // entry_exists() +#else +#include <libbutl/utility.mxx> +#include <libbutl/optional.mxx> +#include <libbutl/filesystem.mxx> +#include <libbutl/standard-version.mxx> +#endif + +using namespace std; + +namespace butl +{ + bool + git_repository (const dir_path& d) + { + // .git can be either a directory or a file in case of a submodule or a + // separate working tree. + // + return entry_exists (d / ".git", + true /* follow_symlinks */, + true /* ignore_errors */); + } + + optional<standard_version> + git_version (const string& s) + { + // There is some variety across platforms in the version + // representation. + // + // Linux: git version 2.14.3 + // MacOS: git version 2.10.1 (Apple Git-78) + // MinGit: git version 2.16.1.windows.1 + // + // We will consider the first 3 version components that follows the + // common 'git version ' prefix. + // + const size_t b (12); + if (s.compare (0, b, "git version ") == 0) + { + size_t i (b); + size_t n (0); + for (char c; i != s.size () && (digit (c = s[i]) || c == '.'); ++i) + { + if (c == '.' && ++n == 3) + break; + } + + // Returns nullopt if fail to parse. + // + return parse_standard_version (string (s, b, i - b)); + } + + return nullopt; + } +} diff --git a/libbutl/git.mxx b/libbutl/git.mxx new file mode 100644 index 0000000..b2023a6 --- /dev/null +++ b/libbutl/git.mxx @@ -0,0 +1,46 @@ +// file : libbutl/git.mxx -*- C++ -*- +// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef __cpp_modules +#pragma once +#endif + +// C includes. + +#ifndef __cpp_lib_modules +#include <string> +#endif + +// Other includes. + +#ifdef __cpp_modules +export module butl.curl; +#ifdef __cpp_lib_modules +import std.core; +#endif +import butl.path; +import butl.optional; +import butl.standard_version +#else +#include <libbutl/path.mxx> +#include <libbutl/optional.mxx> +#include <libbutl/standard-version.mxx> +#endif + +#include <libbutl/export.hxx> + +LIBBUTL_MODEXPORT namespace butl +{ + // Return true if the specified directory is a git repository root (contains + // the .git filesystem entry). + // + LIBBUTL_SYMEXPORT bool + git_repository (const dir_path&); + + // Try to parse the line printed by the 'git --version' command. Return git + // version if succeed, nullopt otherwise. + // + LIBBUTL_SYMEXPORT optional<standard_version> + git_version (const std::string&); +} |