Skip to content

Commit cb86468

Browse files
committed
git sha reporting in version strings
1 parent bafd711 commit cb86468

9 files changed

+199
-14
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,14 @@ install(FILES AUTHORS.txt LICENSE.txt
547547
# generate the pdal_defines.h header
548548
#------------------------------------------------------------------------------
549549

550+
# from http://stackoverflow.com/questions/1435953/how-can-i-pass-git-sha1-to-compiler-as-definition-using-cmake
551+
include(GetGitRevisionDescription)
552+
get_git_head_revision(GIT_REFSPEC GIT_SHA1)
553+
554+
configure_file(
555+
"${PROJECT_SOURCE_DIR}/gitsha.cpp.in"
556+
"${PROJECT_SOURCE_DIR}/src/gitsha.cpp")
557+
550558
set(pdal_defines_h_in "${CMAKE_CURRENT_SOURCE_DIR}/pdal_defines.h.in")
551559
set(pdal_defines_h "${CMAKE_CURRENT_BINARY_DIR}/include/pdal/pdal_defines.h")
552560
configure_file(${pdal_defines_h_in} ${pdal_defines_h})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# - Returns a version string from Git
2+
#
3+
# These functions force a re-configure on each git commit so that you can
4+
# trust the values of the variables in your build system.
5+
#
6+
# get_git_head_revision(<refspecvar> <hashvar> [<additional arguments to git describe> ...])
7+
#
8+
# Returns the refspec and sha hash of the current head revision
9+
#
10+
# git_describe(<var> [<additional arguments to git describe> ...])
11+
#
12+
# Returns the results of git describe on the source tree, and adjusting
13+
# the output so that it tests false if an error occurs.
14+
#
15+
# git_get_exact_tag(<var> [<additional arguments to git describe> ...])
16+
#
17+
# Returns the results of git describe --exact-match on the source tree,
18+
# and adjusting the output so that it tests false if there was no exact
19+
# matching tag.
20+
#
21+
# Requires CMake 2.6 or newer (uses the 'function' command)
22+
#
23+
# Original Author:
24+
# 2009-2010 Ryan Pavlik <[email protected]> <[email protected]>
25+
# http://academic.cleardefinition.com
26+
# Iowa State University HCI Graduate Program/VRAC
27+
#
28+
# Copyright Iowa State University 2009-2010.
29+
# Distributed under the Boost Software License, Version 1.0.
30+
# (See accompanying file LICENSE_1_0.txt or copy at
31+
# http://www.boost.org/LICENSE_1_0.txt)
32+
33+
if(__get_git_revision_description)
34+
return()
35+
endif()
36+
set(__get_git_revision_description YES)
37+
38+
# We must run the following at "include" time, not at function call time,
39+
# to find the path to this module rather than the path to a calling list file
40+
get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
41+
42+
function(get_git_head_revision _refspecvar _hashvar)
43+
set(GIT_PARENT_DIR "${CMAKE_SOURCE_DIR}")
44+
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
45+
while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
46+
set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
47+
get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
48+
if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
49+
# We have reached the root directory, we are not in git
50+
set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
51+
set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
52+
return()
53+
endif()
54+
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
55+
endwhile()
56+
set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
57+
if(NOT EXISTS "${GIT_DATA}")
58+
file(MAKE_DIRECTORY "${GIT_DATA}")
59+
endif()
60+
61+
if(NOT EXISTS "${GIT_DIR}/HEAD")
62+
return()
63+
endif()
64+
set(HEAD_FILE "${GIT_DATA}/HEAD")
65+
configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
66+
67+
configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
68+
"${GIT_DATA}/grabRef.cmake"
69+
@ONLY)
70+
include("${GIT_DATA}/grabRef.cmake")
71+
72+
set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
73+
set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
74+
endfunction()
75+
76+
function(git_describe _var)
77+
if(NOT GIT_FOUND)
78+
find_package(Git QUIET)
79+
endif()
80+
get_git_head_revision(refspec hash)
81+
if(NOT GIT_FOUND)
82+
set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
83+
return()
84+
endif()
85+
if(NOT hash)
86+
set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
87+
return()
88+
endif()
89+
90+
# TODO sanitize
91+
#if((${ARGN}" MATCHES "&&") OR
92+
# (ARGN MATCHES "||") OR
93+
# (ARGN MATCHES "\\;"))
94+
# message("Please report the following error to the project!")
95+
# message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
96+
#endif()
97+
98+
#message(STATUS "Arguments to execute_process: ${ARGN}")
99+
100+
execute_process(COMMAND
101+
"${GIT_EXECUTABLE}"
102+
describe
103+
${hash}
104+
${ARGN}
105+
WORKING_DIRECTORY
106+
"${CMAKE_SOURCE_DIR}"
107+
RESULT_VARIABLE
108+
res
109+
OUTPUT_VARIABLE
110+
out
111+
ERROR_QUIET
112+
OUTPUT_STRIP_TRAILING_WHITESPACE)
113+
if(NOT res EQUAL 0)
114+
set(out "${out}-${res}-NOTFOUND")
115+
endif()
116+
117+
set(${_var} "${out}" PARENT_SCOPE)
118+
endfunction()
119+
120+
function(git_get_exact_tag _var)
121+
git_describe(out --exact-match ${ARGN})
122+
set(${_var} "${out}" PARENT_SCOPE)
123+
endfunction()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#
2+
# Internal file for GetGitRevisionDescription.cmake
3+
#
4+
# Requires CMake 2.6 or newer (uses the 'function' command)
5+
#
6+
# Original Author:
7+
# 2009-2010 Ryan Pavlik <[email protected]> <[email protected]>
8+
# http://academic.cleardefinition.com
9+
# Iowa State University HCI Graduate Program/VRAC
10+
#
11+
# Copyright Iowa State University 2009-2010.
12+
# Distributed under the Boost Software License, Version 1.0.
13+
# (See accompanying file LICENSE_1_0.txt or copy at
14+
# http://www.boost.org/LICENSE_1_0.txt)
15+
16+
set(HEAD_HASH)
17+
18+
file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024)
19+
20+
string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS)
21+
if(HEAD_CONTENTS MATCHES "ref")
22+
# named branch
23+
string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}")
24+
if(EXISTS "@GIT_DIR@/${HEAD_REF}")
25+
configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
26+
elseif(EXISTS "@GIT_DIR@/logs/${HEAD_REF}")
27+
configure_file("@GIT_DIR@/logs/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
28+
set(HEAD_HASH "${HEAD_REF}")
29+
endif()
30+
else()
31+
# detached HEAD
32+
configure_file("@GIT_DIR@/HEAD" "@GIT_DATA@/head-ref" COPYONLY)
33+
endif()
34+
35+
if(NOT HEAD_HASH)
36+
file(READ "@GIT_DATA@/head-ref" HEAD_HASH LIMIT 1024)
37+
string(STRIP "${HEAD_HASH}" HEAD_HASH)
38+
endif()

gitsha.cpp.in

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <pdal/gitsha.h>
2+
#define GIT_SHA1 "@GIT_SHA1@"
3+
const char g_GIT_SHA1[] = GIT_SHA1;

include/pdal/gitsha.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern const char g_GIT_SHA1[];

src/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(PDAL_BASE_HPP
3232
${PDAL_HEADERS_DIR}/FilterIterator.hpp
3333
${PDAL_HEADERS_DIR}/GDALUtils.hpp
3434
${PDAL_HEADERS_DIR}/GlobalEnvironment.hpp
35+
${PDAL_HEADERS_DIR}/gitsha.h
3536
${PDAL_HEADERS_DIR}/Log.hpp
3637
${PDAL_HEADERS_DIR}/Metadata.hpp
3738
${PDAL_HEADERS_DIR}/MultiFilter.hpp
@@ -70,6 +71,7 @@ set(PDAL_BASE_CPP
7071
FileUtils.cpp
7172
Filter.cpp
7273
FilterIterator.cpp
74+
gitsha.cpp
7375
GlobalEnvironment.cpp
7476
Log.cpp
7577
Metadata.cpp

src/drivers/soci/Reader.cpp

+12-14
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ boost::uint64_t Reader::getNumPoints() const
8686
::soci::statement q = (m_session->prepare << query_oss.str(), ::soci::into(count, ind));
8787
q.execute();
8888

89-
if (ind == ::soci::i_null)
90-
return 0;
89+
// if (ind == ::soci::i_null)
90+
// return 0;
9191

9292
if (count < 0)
9393
{
@@ -218,8 +218,8 @@ pdal::Schema Reader::fetchSchema(std::string const& query) const
218218
::soci::row r;
219219
::soci::statement clouds = (m_session->prepare << q, ::soci::into(r, ind));
220220
clouds.execute();
221-
if (ind == ::soci::i_null)
222-
return pdal::Schema();
221+
// if (ind == ::soci::i_null)
222+
// return pdal::Schema();
223223

224224
bool bDidRead = clouds.fetch();
225225
// if (!bDidRead)
@@ -430,7 +430,7 @@ BufferPtr IteratorBase::fetchPointBuffer( boost::int32_t const& cloud_id,
430430
else
431431
{
432432
std::stringstream query;
433-
433+
434434
Schema schema = Schema::from_xml(schema_xml);
435435

436436
BufferPtr output = BufferPtr(new PointBuffer(schema, capacity));
@@ -457,12 +457,12 @@ boost::uint32_t IteratorBase::myReadBlocks(PointBuffer& user_buffer)
457457

458458
bool bDidRead = blocks.fetch();
459459

460-
if (ind == ::soci::i_null)
461-
{
462-
// We have no points to return
463-
getReader().log()->get(logDEBUG) << "Query returned no points" << std::endl;
464-
return 0;
465-
}
460+
// if (ind == ::soci::i_null)
461+
// {
462+
// // We have no points to return
463+
// getReader().log()->get(logDEBUG) << "Query returned no points" << std::endl;
464+
// return 0;
465+
// }
466466

467467
//
468468
// size_t size = block.size();
@@ -472,7 +472,6 @@ boost::uint32_t IteratorBase::myReadBlocks(PointBuffer& user_buffer)
472472
// }
473473
if (!m_active_buffer)
474474
{
475-
// std::cout << "block: " << block << std::endl;
476475
m_active_buffer = fetchPointBuffer( block.get<int>("cloud_id"),
477476
block.get<std::string>("schema"),
478477
user_buffer.getCapacity());
@@ -518,8 +517,7 @@ boost::uint32_t IteratorBase::myReadBlocks(PointBuffer& user_buffer)
518517
getReader().log()->get(logDEBUG3) << "IteratorBase::myReadBlocks: current_cloud_id: "
519518
<< current_cloud_id << " m_active_cloud_id: "
520519
<< m_active_cloud_id << std::endl;
521-
std::string schema;
522-
m_active_buffer = fetchPointBuffer(current_cloud_id, schema, user_buffer.getCapacity());
520+
m_active_buffer = fetchPointBuffer(current_cloud_id, block.get<std::string>("schema"), user_buffer.getCapacity());
523521

524522
m_active_cloud_id = current_cloud_id;
525523
return user_buffer.getNumPoints();

src/gitsha.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <pdal/gitsha.h>
2+
#define GIT_SHA1 "bafd7116beb486a41f4eef99dbdb91ec5e47715b"
3+
const char g_GIT_SHA1[] = GIT_SHA1;

src/pdal_config.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
#include <sstream>
4747
#include <pdal/pdal_defines.h>
48+
#include <pdal/gitsha.h>
4849

4950
#ifdef PDAL_HAVE_LIBGEOTIFF
5051
#include <geotiff.h>
@@ -123,6 +124,8 @@ std::string GetFullVersionString()
123124
{
124125
std::ostringstream os;
125126

127+
128+
126129
#ifdef PDAL_HAVE_LIBGEOTIFF
127130
os << " GeoTIFF "
128131
<< (LIBGEOTIFF_VERSION / 1000) << '.'
@@ -144,6 +147,12 @@ std::string GetFullVersionString()
144147
std::string info(os.str());
145148
os.str("");
146149
os << "PDAL " << PDAL_VERSION_STRING;
150+
151+
std::ostringstream revs;
152+
revs << g_GIT_SHA1;
153+
154+
os << " (" << revs.str().substr(0, 6) <<")";
155+
147156
if (!info.empty())
148157
{
149158
os << " with" << info;

0 commit comments

Comments
 (0)