Skip to content

Commit

Permalink
Properly handle git-describe pre-release levels
Browse files Browse the repository at this point in the history
  • Loading branch information
shlevy committed Aug 26, 2016
1 parent 67c8189 commit 11fa580
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ of `nix-exec`:
* `version.major`: The major version number
* `version.minor`: The minor version number
* `version.patchlevel`: The version patchlevel.
* `version.prelevel`: If present, the version pre-release level

unsafe-perform-io
------------------
Expand Down
54 changes: 36 additions & 18 deletions src/nix-exec-lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,10 @@ struct exploded_version {
nix::NixInt major;
nix::NixInt minor;
nix::NixInt patch;
size_t pre_off;
};

enum class acc_tag : char { major, minor, patch };
enum class acc_tag { major, minor, patch };

static constexpr nix::NixInt char_to_digit(char c) {
return c == '0' ?
Expand Down Expand Up @@ -391,7 +392,7 @@ template <size_t N> static constexpr
(str[off] == '.' ?
explode_version_impl( str
, off + 1
, exploded_version{acc.major, 0, -1}
, exploded_version{acc.major, 0, -1, 0}
, acc_tag::minor
) :
explode_version_impl( str
Expand All @@ -401,6 +402,7 @@ template <size_t N> static constexpr
)
, -1
, -1
, 0
}
, tag
)
Expand All @@ -409,7 +411,7 @@ template <size_t N> static constexpr
(str[off] == '.' ?
explode_version_impl( str
, off + 1
, exploded_version{acc.major, acc.minor, 0}
, exploded_version{acc.major, acc.minor, 0, 0}
, acc_tag::patch
) :
explode_version_impl( str
Expand All @@ -419,27 +421,36 @@ template <size_t N> static constexpr
str[off]
)
, -1
, 0
}
, tag
)
) :
(tag == acc_tag::patch ?
(off + 2 == N ?
(str[off] == '-' ?
exploded_version{ acc.major
, acc.minor
, 10 * acc.patch + char_to_digit(str[off])
, acc.patch
, off
} :
explode_version_impl( str
, off + 1
, exploded_version{ acc.major
, acc.minor
, 10 * acc.patch + char_to_digit(
str[off]
)
}
, tag
)
) :
(off + 2 == N ?
exploded_version{ acc.major
, acc.minor
, 10 * acc.patch + char_to_digit(str[off])
, 0
} :
explode_version_impl( str
, off + 1
, exploded_version{ acc.major
, acc.minor
, 10 * acc.patch + char_to_digit(
str[off]
)
, 0
}
, tag
)
)) :
throw std::domain_error("internal error")
))) :
throw std::out_of_range("not enough dots in version");
Expand All @@ -449,13 +460,13 @@ template <size_t N>
static constexpr exploded_version explode_version(const char(&str)[N]) {
return explode_version_impl( str
, 0
, exploded_version{0, -1, -1}
, exploded_version{0, -1, -1, 0}
, acc_tag::major
);
}

static void setup_version(EvalState & state, Value & v) {
state.mkAttrs(v, 3);
state.mkAttrs(v, 4);

constexpr auto version = explode_version(VERSION);
static_assert( version.major > 0
Expand All @@ -464,6 +475,8 @@ static void setup_version(EvalState & state, Value & v) {
, "invalid exploded version"
);

state.mkAttrs(v, version.pre_off == 0 ? 3 : 4);

auto & major = *state.allocAttr(v, state.symbols.create("major"));
mkInt(major, version.major);

Expand All @@ -473,6 +486,11 @@ static void setup_version(EvalState & state, Value & v) {
auto & patch = *state.allocAttr(v, state.symbols.create("patchlevel"));
mkInt(patch, version.patch);

if (version.pre_off != 0) {
auto & pre = *state.allocAttr(v, state.symbols.create("prelevel"));
mkStringNoCopy(pre, VERSION + version.pre_off + 1);
}

v.attrs->sort();
}

Expand Down

0 comments on commit 11fa580

Please sign in to comment.