SourceStatus.cmake 3.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
#
# This file is part of Orfeo Toolbox
#
#     https://www.orfeo-toolbox.org/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

21 22 23
function(repository_status root_repo_dir result_var1)
  if(NOT EXISTS "${root_repo_dir}/.git")
    return()
24
  endif()
25 26

  find_package(Git)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  if(NOT GIT_FOUND)
    message(STATUS "git not found. Make sure git can be found in your PATH. (source status will not be reported)")
    set(${result_var1})
    return()
  endif()

  execute_process(COMMAND ${GIT_EXECUTABLE} config --get remote.origin.url
    WORKING_DIRECTORY ${root_repo_dir}
    OUTPUT_VARIABLE git_remote_origin_url
    OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
  if(git_remote_origin_url)
    get_filename_component(dir_name ${git_remote_origin_url} NAME)
  else()
    get_filename_component(dir_name ${root_repo_dir} NAME)
  endif()
42

43 44 45 46
  execute_process(COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%H --oneline
    WORKING_DIRECTORY ${root_repo_dir}
    OUTPUT_VARIABLE last_commit_id
    OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
47

48 49 50 51 52 53 54
  #git symbolic-ref --short -q HEAD does not work on older git version
  #rather than checking for git version. it is better to not use that
  #option and employ cmake stuff to do the work
  execute_process(COMMAND ${GIT_EXECUTABLE} symbolic-ref -q HEAD
    WORKING_DIRECTORY ${root_repo_dir}
    OUTPUT_VARIABLE git_symbolic_ref_output
    OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
55

56 57 58
  set(branch_name)
  if(git_symbolic_ref_output)
    get_filename_component(branch_name ${git_symbolic_ref_output} NAME)
59
  endif()
60

61 62 63 64
  set(return_msg "${dir_name} repository info: \n")
  set(return_msg "${return_msg} git log -1 --oneline     = [${last_commit_id}] \n")
  set(return_msg "${return_msg} git symbolic-ref -q HEAD = ${branch_name}")
  set(${result_var1} ${return_msg} PARENT_SCOPE)
65

66 67 68 69 70
  #print other information inside function call
  execute_process(COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%H
    WORKING_DIRECTORY ${root_repo_dir}
    OUTPUT_VARIABLE OTB_WC_REVISION
    OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
71 72 73 74

  message(STATUS "Repository status (${dir_name}) :")
  message(STATUS "  Repository revision is ${OTB_WC_REVISION}")

75 76 77
  execute_process(COMMAND ${GIT_EXECUTABLE} status -s
    WORKING_DIRECTORY ${root_repo_dir}
    OUTPUT_VARIABLE OTB_WC_STATUS
78
    TIMEOUT 20
79
    OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
80

81 82 83
  if(OTB_WC_STATUS)
    message(STATUS "  Local file modifications (${dir_name}) :")
    string(REPLACE "\n" "\n--     " OTB_WC_STATUS_PRETTYPRINT "    ${OTB_WC_STATUS}")
84
    message(STATUS "${OTB_WC_STATUS_PRETTYPRINT}")
85 86
  else()
    message(STATUS "  No files modified locally (${dir_name})")
87
  endif()
88 89

endfunction()