1#
2# SPDX-License-Identifier: BSD-3-Clause
3# SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
4#
5
6#
7# Returns:
8# @FileList_Out:        All files in the Git repo in list format. Empty list
9#                       on error
10#
11function(Git_Get_All_Files FileList_Out)
12  if (GIT_NOT_FOUND OR NOT IS_DIRECTORY .git)
13    set(${FileList_Out} "" PARENT_SCOPE)
14    return()
15  endif()
16
17  execute_process(
18    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
19    COMMAND ${GIT_EXECUTABLE} ls-files
20    OUTPUT_VARIABLE git_ls_files
21    RESULT_VARIABLE git_rc
22    OUTPUT_STRIP_TRAILING_WHITESPACE
23    )
24
25  # convert string to list
26  if(NOT "${git_ls_files}" STREQUAL "")
27    string(REPLACE "\n" ";" all_files ${git_ls_files})
28  else()
29    set(all_files "")
30  endif()
31
32  set(${FileList_Out} ${all_files} PARENT_SCOPE)
33endfunction()
34
35#
36# Returns:
37# @CommitIdList_Out:    All commit ids in current branch between HEAD and
38#                       upstream tracking branch in List format. Empty list
39#                       on error
40#
41function(Git_Get_Pending_Commits CommitIdList_Out)
42  if (GIT_NOT_FOUND OR NOT IS_DIRECTORY .git)
43    set(${CommitIdList_Out} "" PARENT_SCOPE)
44    return()
45  endif()
46
47  # Get the upstream branch the current (local) branch is tracking
48  execute_process(
49    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
50    COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref --symbolic-full-name @{u}
51    OUTPUT_VARIABLE git_upstream_branch
52    RESULT_VARIABLE git_rc
53    OUTPUT_STRIP_TRAILING_WHITESPACE
54    )
55
56  if ("${git_upstream_branch}" STREQUAL "")
57    message(STATUS "Warning: Upstream branch not set. Trying \"origin/main\"")
58    set(git_upstream_branch "origin/main")
59  endif()
60
61  # Get the merge base
62  execute_process(
63    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
64    COMMAND ${GIT_EXECUTABLE} merge-base HEAD ${git_upstream_branch}
65    OUTPUT_VARIABLE git_merge_base
66    RESULT_VARIABLE git_rc
67    OUTPUT_STRIP_TRAILING_WHITESPACE
68    )
69
70  if("${git_merge_base}" STREQUAL "")
71    set(${CommitIdList_Out} "" PARENT_SCOPE)
72    return()
73  endif()
74
75  # Get list of commits between $merge_base and HEAD
76  execute_process(
77    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
78    COMMAND ${GIT_EXECUTABLE} rev-list --no-merges "${git_merge_base}..HEAD"
79    OUTPUT_VARIABLE git_rev_output
80    RESULT_VARIABLE git_rc
81    OUTPUT_STRIP_TRAILING_WHITESPACE
82    )
83
84  # convert to list
85  if(NOT "${git_rev_output}" STREQUAL "")
86    string(REPLACE "\n" ";" git_rev_list ${git_rev_output})
87  else()
88    set(git_rev_list "")
89  endif()
90
91  set(${CommitIdList_Out} ${git_rev_list} PARENT_SCOPE)
92endfunction()
93
94#
95# Args In:
96# @CommitId_In:         Commit's SHA
97#
98# Returns:
99# @FileList_Out:        Files Added or Modified or Deleted by the @CommitId_In
100#                       in list format. Empty list on error
101#
102function(Git_Get_Files_In_Commit CommitId_In FileList_Out)
103  if (GIT_NOT_FOUND OR NOT IS_DIRECTORY .git OR "${CommitId_In}" STREQUAL "")
104    set(${FileList_Out} "" PARENT_SCOPE)
105    return()
106  endif()
107
108  execute_process(
109    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
110    # Get list of files that are Added or Renamed or Modified by this commit
111    COMMAND ${GIT_EXECUTABLE} show --diff-filter=ARM --pretty=format: --name-only ${CommitId_In}
112    OUTPUT_VARIABLE git_files
113    RESULT_VARIABLE git_rc
114    OUTPUT_STRIP_TRAILING_WHITESPACE
115    )
116
117  # convert string to list
118  if(NOT "${git_files}" STREQUAL "")
119    string(REPLACE "\n" ";" source_files ${git_files})
120  else()
121    set(source_files "")
122  endif()
123
124  set(${FileList_Out} ${source_files} PARENT_SCOPE)
125endfunction()
126