blob: bef620b572ee2f6a044930bf579d32fe53d9b36c [file] [log] [blame]
Soby Mathewb4c6df42022-11-09 11:13:29 +00001#
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()
Arunachalam Ganapathyd9e8e1b2024-04-19 16:27:14 +0100126
127#
128# Apply patches in @Git_Repo
129#
130# Args In:
131# @Git_Repo: Git repository
132# @Patch_Files_List: List of .patch files to apply
133#
134function(Git_Apply_Patches Git_Repo Patch_Files_List)
135 # use EXISTS as inside submodule .git file contains path to Git repository
136 if(GIT_NOT_FOUND OR NOT EXISTS "${Git_Repo}/.git")
137 message(FATAL_ERROR "${Git_Repo} not a git repository")
138 return()
139 endif()
140
141 # todo: Remove 'checkout' and 'clean' commands.
142 # These commands does a force reset and removes untracked files. If an user
143 # has some work in progress changes in 'Git_Repo', then Git_Apply_Patches will
144 # force delete the changes.
145 execute_process(
146 WORKING_DIRECTORY ${Git_Repo}
147 # removes changes that are not staged
148 COMMAND ${GIT_EXECUTABLE} checkout .
149 )
150 execute_process(
151 WORKING_DIRECTORY ${Git_Repo}
152 # removes changes that are not tracked
153 COMMAND ${GIT_EXECUTABLE} clean -f
154 )
155
156 # todo: For applying patches use -am option, this retains commit history.
157 foreach(PATCH_FILE IN LISTS Patch_Files_List)
158 execute_process(
159 WORKING_DIRECTORY ${Git_Repo}
160 COMMAND ${GIT_EXECUTABLE} apply --verbose ${PATCH_FILE}
161 RESULT_VARIABLE PATCH_STATUS
162 COMMAND_ECHO STDOUT
163 )
164
165 if(NOT PATCH_STATUS EQUAL 0)
166 message(FATAL_ERROR "Failed to apply patch ${PATCH_FILE} at ${Git_Repo}")
167 endif()
168 endforeach()
169endfunction()