@@ -49,24 +49,38 @@ function(get_backport_patch_hash patch_path patch_hash)
49
49
endfunction ()
50
50
51
51
# 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 )
53
53
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 " )
55
55
execute_process (
56
- COMMAND ${GIT_EXECUTABLE} branch --contains ${patch_hash}
56
+ COMMAND ${GIT_EXECUTABLE} merge-base --is-ancestor ${patch_hash} HEAD
57
57
WORKING_DIRECTORY ${repo_dir}
58
- OUTPUT_VARIABLE patch_in_branches
58
+ RESULT_VARIABLE patch_not_in_branches
59
+ OUTPUT_QUIET
59
60
ERROR_QUIET
60
61
)
61
- if (NOT patch_in_branches )
62
+ if (patch_not_in_branches )
62
63
set (patch_in_branch False PARENT_SCOPE ) # The patch is not present in local branch
63
64
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
70
84
endif ()
71
85
endfunction ()
72
86
@@ -100,23 +114,30 @@ function(apply_patches repo_dir patches_dirs base_revision target_branch ret)
100
114
message (STATUS "[OPENCL-CLANG] Is not a git repo" )
101
115
elseif (patches_needed ) # The target branch doesn't exist
102
116
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 ()
110
130
execute_process ( # Create the target branch
111
131
COMMAND ${GIT_EXECUTABLE} checkout -b ${target_branch} ${base_revision}
112
132
WORKING_DIRECTORY ${repo_dir}
113
133
RESULT_VARIABLE ret_check_out
134
+ ERROR_STRIP_TRAILING_WHITESPACE
114
135
ERROR_VARIABLE checkout_log
115
136
OUTPUT_QUIET
116
137
)
117
- message (STATUS "[OPENCL-CLANG] ${checkout_log} " )
138
+ message (STATUS "[OPENCL-CLANG] ${checkout_log} which starts from ref : ${base_revision} " )
118
139
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 )
120
141
if (${patch_in_branch} )
121
142
message (STATUS "[OPENCL-CLANG] Patch ${patch} is already in local branch - ignore patching" )
122
143
else ()
0 commit comments