Skip to content

Commit ca75b48

Browse files
authored
Merge pull request #212 from lwesiers/ocl-open-100
Added mechanism for validating sha1/tag/branch during patching llvm/clang/spirv-translator
2 parents 1024459 + c8884a5 commit ca75b48

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

cmake/modules/CMakeFunctions.cmake

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,38 @@ function(get_backport_patch_hash patch_path patch_hash)
4949
endfunction()
5050

5151
# Checks if the the patch is present in current local branch
52-
function(is_backport_patch_present patch_path repo_dir base_branch patch_in_branch)
52+
function(is_backport_patch_present patch_path repo_dir patch_in_branch)
5353
get_backport_patch_hash(${patch_path} patch_hash)
54-
message(STATUS "[OPENCL-CLANG] Checking if patch ${patch_hash} is present in branch ${base_branch}")
54+
message(STATUS "[OPENCL-CLANG] Checking if patch ${patch_hash} is present in repository")
5555
execute_process(
56-
COMMAND ${GIT_EXECUTABLE} branch --contains ${patch_hash}
56+
COMMAND ${GIT_EXECUTABLE} merge-base --is-ancestor ${patch_hash} HEAD
5757
WORKING_DIRECTORY ${repo_dir}
58-
OUTPUT_VARIABLE patch_in_branches
58+
RESULT_VARIABLE patch_not_in_branches
59+
OUTPUT_QUIET
5960
ERROR_QUIET
6061
)
61-
if(NOT patch_in_branches)
62+
if(patch_not_in_branches)
6263
set(patch_in_branch False PARENT_SCOPE) # The patch is not present in local branch
6364
else()
64-
string(FIND ${patch_in_branches} ${base_branch} match_branch)
65-
if(${match_branch} EQUAL -1)
66-
set(patch_in_branch False PARENT_SCOPE) # The patch is not present in local branch
67-
else()
68-
set(patch_in_branch True PARENT_SCOPE) # The patch is not present in local branch
69-
endif()
65+
set(patch_in_branch True PARENT_SCOPE) # The patch is not present in local branch
66+
endif()
67+
endfunction()
68+
69+
# Validates if given SHA1/tag/branch name exists in local repo
70+
function(is_valid_revision repo_dir revision return_val)
71+
message(STATUS "[OPENCL-CLANG] Validating ${revision} in repository")
72+
# Check if we have under revision existing branch/tag/SHA1 in this repo
73+
execute_process(
74+
COMMAND ${GIT_EXECUTABLE} log -1 ${revision}
75+
WORKING_DIRECTORY ${repo_dir}
76+
RESULT_VARIABLE output_var
77+
ERROR_QUIET
78+
OUTPUT_QUIET
79+
)
80+
if(${output_var} EQUAL 0)
81+
set(${return_val} True PARENT_SCOPE) # this tag/branch/sha1 exists in repo
82+
else()
83+
set(${return_val} False PARENT_SCOPE) # this tag/branch/sha1 not exists in repo
7084
endif()
7185
endfunction()
7286

@@ -100,23 +114,30 @@ function(apply_patches repo_dir patches_dirs base_revision target_branch ret)
100114
message(STATUS "[OPENCL-CLANG] Is not a git repo")
101115
elseif(patches_needed) # The target branch doesn't exist
102116
list(SORT patches)
103-
execute_process( # Take current branch name
104-
COMMAND ${GIT_EXECUTABLE} symbolic-ref --short HEAD
105-
WORKING_DIRECTORY ${repo_dir}
106-
OUTPUT_VARIABLE base_branch
107-
ERROR_QUIET
108-
)
109-
message(STATUS "[OPENCL-CLANG] Base branch : ${base_branch}")
117+
is_valid_revision(${repo_dir} ${base_revision} exists_base_rev)
118+
119+
if(NOT ${exists_base_rev})
120+
execute_process( # take SHA1 from HEAD
121+
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
122+
WORKING_DIRECTORY ${repo_dir}
123+
OUTPUT_VARIABLE repo_head
124+
OUTPUT_STRIP_TRAILING_WHITESPACE
125+
ERROR_QUIET
126+
)
127+
message(STATUS "[OPENCL-CLANG] ref ${base_revision} not exists in repository, using current HEAD:${repo_head}")
128+
set(base_revision ${repo_head})
129+
endif()
110130
execute_process( # Create the target branch
111131
COMMAND ${GIT_EXECUTABLE} checkout -b ${target_branch} ${base_revision}
112132
WORKING_DIRECTORY ${repo_dir}
113133
RESULT_VARIABLE ret_check_out
134+
ERROR_STRIP_TRAILING_WHITESPACE
114135
ERROR_VARIABLE checkout_log
115136
OUTPUT_QUIET
116137
)
117-
message(STATUS "[OPENCL-CLANG] ${checkout_log}")
138+
message(STATUS "[OPENCL-CLANG] ${checkout_log} which starts from ref : ${base_revision}")
118139
foreach(patch ${patches})
119-
is_backport_patch_present(${patch} ${repo_dir} ${base_branch} patch_in_branch)
140+
is_backport_patch_present(${patch} ${repo_dir} patch_in_branch)
120141
if(${patch_in_branch})
121142
message(STATUS "[OPENCL-CLANG] Patch ${patch} is already in local branch - ignore patching")
122143
else()

0 commit comments

Comments
 (0)