@@ -162,17 +162,64 @@ static int update_head_to_default(git_repository *repo)
162
162
return error ;
163
163
}
164
164
165
+ static int update_remote_head (
166
+ git_repository * repo ,
167
+ git_remote * remote ,
168
+ git_buf * target ,
169
+ const char * reflog_message )
170
+ {
171
+ git_refspec * refspec ;
172
+ git_reference * remote_head = NULL ;
173
+ git_buf remote_head_name = GIT_BUF_INIT ;
174
+ git_buf remote_branch_name = GIT_BUF_INIT ;
175
+ int error ;
176
+
177
+ /* Determine the remote tracking ref name from the local branch */
178
+ refspec = git_remote__matching_refspec (remote , git_buf_cstr (target ));
179
+
180
+ if (refspec == NULL ) {
181
+ git_error_set (GIT_ERROR_NET , "the remote's default branch does not fit the refspec configuration" );
182
+ error = GIT_EINVALIDSPEC ;
183
+ goto cleanup ;
184
+ }
185
+
186
+ if ((error = git_refspec_transform (
187
+ & remote_branch_name ,
188
+ refspec ,
189
+ git_buf_cstr (target ))) < 0 )
190
+ goto cleanup ;
191
+
192
+ if ((error = git_buf_printf (& remote_head_name ,
193
+ "%s%s/%s" ,
194
+ GIT_REFS_REMOTES_DIR ,
195
+ git_remote_name (remote ),
196
+ GIT_HEAD_FILE )) < 0 )
197
+ goto cleanup ;
198
+
199
+ error = git_reference_symbolic_create (
200
+ & remote_head ,
201
+ repo ,
202
+ git_buf_cstr (& remote_head_name ),
203
+ git_buf_cstr (& remote_branch_name ),
204
+ true,
205
+ reflog_message );
206
+
207
+ cleanup :
208
+ git_reference_free (remote_head );
209
+ git_buf_dispose (& remote_branch_name );
210
+ git_buf_dispose (& remote_head_name );
211
+ return error ;
212
+ }
213
+
165
214
static int update_head_to_remote (
166
215
git_repository * repo ,
167
216
git_remote * remote ,
168
217
const char * reflog_message )
169
218
{
170
219
int error = 0 ;
171
220
size_t refs_len ;
172
- git_refspec * refspec ;
173
221
const git_remote_head * remote_head , * * refs ;
174
222
const git_oid * remote_head_id ;
175
- git_buf remote_branch_name = GIT_BUF_INIT ;
176
223
git_buf branch = GIT_BUF_INIT ;
177
224
178
225
if ((error = git_remote_ls (& refs , & refs_len , remote )) < 0 )
@@ -195,19 +242,7 @@ static int update_head_to_remote(
195
242
goto cleanup ;
196
243
}
197
244
198
- refspec = git_remote__matching_refspec (remote , git_buf_cstr (& branch ));
199
-
200
- if (refspec == NULL ) {
201
- git_error_set (GIT_ERROR_NET , "the remote's default branch does not fit the refspec configuration" );
202
- error = GIT_EINVALIDSPEC ;
203
- goto cleanup ;
204
- }
205
-
206
- /* Determine the remote tracking ref name from the local branch */
207
- if ((error = git_refspec_transform (
208
- & remote_branch_name ,
209
- refspec ,
210
- git_buf_cstr (& branch ))) < 0 )
245
+ if ((error = update_remote_head (repo , remote , & branch , reflog_message )) < 0 )
211
246
goto cleanup ;
212
247
213
248
error = update_head_to_new_branch (
@@ -217,7 +252,6 @@ static int update_head_to_remote(
217
252
reflog_message );
218
253
219
254
cleanup :
220
- git_buf_dispose (& remote_branch_name );
221
255
git_buf_dispose (& branch );
222
256
223
257
return error ;
0 commit comments