|
1 | 1 | #!/bin/bash |
2 | 2 | # This script helps build conda package with proper version extraction from git |
3 | 3 |
|
4 | | -# Get the git describe tag (e.g. v0.4.0) |
| 4 | +# Get git information for versioning |
| 5 | +# Get full git describe output (tag-commits-hash) if on an untagged commit |
| 6 | +export GIT_DESCRIBE=$(git describe --tags 2>/dev/null || echo "") |
| 7 | + |
| 8 | +# Get just the most recent tag |
5 | 9 | export GIT_DESCRIBE_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") |
| 10 | + |
| 11 | +# Get commit info |
6 | 12 | export GIT_FULL_HASH=$(git rev-parse HEAD) |
7 | 13 | export SHORT_HASH=${GIT_FULL_HASH:0:7} |
8 | 14 |
|
9 | 15 | # Generate a date-based version component |
10 | 16 | export BUILD_DATE=$(date +"%Y%m%d") |
11 | 17 |
|
12 | | -# Always use a version that includes commit info |
13 | | -if [ -z "$GIT_DESCRIBE_TAG" ]; then |
14 | | - # No tag exists, use only commit hash |
15 | | - export VERSION="0.0.0.dev${BUILD_DATE}+${SHORT_HASH}" |
16 | | - echo "No git tag found, using version: $VERSION" |
17 | | -else |
| 18 | +# Check if we're exactly on a tag |
| 19 | +if git describe --exact-match --tags HEAD >/dev/null 2>&1; then |
| 20 | + # We are exactly on a tag, use just the tag version |
| 21 | + TAG_VERSION=$GIT_DESCRIBE_TAG |
| 22 | + |
18 | 23 | # Remove 'v' prefix from tag for conda versioning |
19 | | - if [[ $GIT_DESCRIBE_TAG == v* ]]; then |
20 | | - export GIT_DESCRIBE_TAG="${GIT_DESCRIBE_TAG:1}" |
| 24 | + if [[ $TAG_VERSION == v* ]]; then |
| 25 | + TAG_VERSION="${TAG_VERSION:1}" |
| 26 | + fi |
| 27 | + |
| 28 | + export VERSION="$TAG_VERSION" |
| 29 | + echo "On exact tag commit, using version: $VERSION" |
| 30 | +else |
| 31 | + # Not on a tag, use tag-commits-hash format (convert to PEP 440 compatible version) |
| 32 | + if [ -z "$GIT_DESCRIBE" ]; then |
| 33 | + # No tag exists at all, fallback to dev version |
| 34 | + export VERSION="0.0.0.dev${BUILD_DATE}+${SHORT_HASH}" |
| 35 | + echo "No git tag found at all, using version: $VERSION" |
| 36 | + else |
| 37 | + # Process the git describe output to create PEP 440 compatible version |
| 38 | + # git describe format is typically: tag-N-gHASH (e.g., v1.2.3-5-g123abc) |
| 39 | + # Extract parts from git describe output |
| 40 | + TAG=$(echo $GIT_DESCRIBE | sed -E 's/^(v?[0-9]+\.[0-9]+\.[0-9]+)(-.*)?$/\1/') |
| 41 | + # Remove 'v' prefix from tag if present |
| 42 | + if [[ $TAG == v* ]]; then |
| 43 | + TAG="${TAG:1}" |
| 44 | + fi |
| 45 | + # Extract commit count since tag (N from tag-N-gHASH) |
| 46 | + COMMITS=$(echo $GIT_DESCRIBE | sed -E 's/^v?[0-9]+\.[0-9]+\.[0-9]+-([0-9]+)-.*/\1/' 2>/dev/null || echo "0") |
| 47 | + # Format as PEP 440 compatible version: tag.devN+hash |
| 48 | + export VERSION="${TAG}.dev${COMMITS}+${SHORT_HASH}" |
| 49 | + echo "Not on exact tag commit, using version: $VERSION" |
21 | 50 | fi |
22 | | - |
23 | | - # Use tag + commit hash |
24 | | - export VERSION="${GIT_DESCRIBE_TAG}.dev${BUILD_DATE}+${SHORT_HASH}" |
25 | | - echo "Using version: $VERSION" |
26 | 51 | fi |
27 | 52 |
|
28 | 53 | # Export the VERSION to environment for conda-build to use |
|
0 commit comments