@@ -244,6 +244,176 @@ int git_hook_execute_io(git_buf *io, git_repository *repo, const char *hook_name
244
244
245
245
/** High-level hook calls */
246
246
247
+ int git_hook_call_pre_commit (git_repository * repo )
248
+ {
249
+ assert (repo );
250
+
251
+ return git_hook_execute (repo , "pre-commit" , NULL );
252
+ }
253
+
254
+ static int commit_message_file (git_buf * msg_path , git_repository * repo , const char * message , const char * file )
255
+ {
256
+ int err = 0 ;
257
+ git_buf msg_buf = GIT_BUF_INIT ;
258
+
259
+ assert (msg_path && repo && message && file );
260
+
261
+ git_buf_attach_notowned (& msg_buf , message , strlen (message ));
262
+
263
+ if ((err = git_repository_item_path (msg_path , repo , GIT_REPOSITORY_ITEM_GITDIR )) != 0 )
264
+ goto cleanup ;
265
+
266
+ if ((err = git_buf_joinpath (msg_path , git_buf_cstr (msg_path ), file )) != 0 )
267
+ goto cleanup ;
268
+
269
+ if ((err = git_futils_writebuffer (& msg_buf , git_buf_cstr (msg_path ), O_CREAT |O_WRONLY , 0766 )) != 0 )
270
+ goto cleanup ;
271
+
272
+ cleanup :
273
+ git_buf_dispose (& msg_buf );
274
+ if (err != 0 ) {
275
+ git_buf_dispose (msg_path );
276
+ }
277
+
278
+ return err ;
279
+ }
280
+
281
+ int prepare_commit_msg__message (git_buf * msg_path , git_repository * repo , const char * message );
282
+ int prepare_commit_msg__template (git_buf * msg_path , git_repository * repo , const char * template_file );
283
+ int prepare_commit_msg__commit (git_buf * msg_path , git_repository * repo , const git_oid * oid );
284
+
285
+ int git_hook_call_prepare_commit_message (git_repository * repo , const char * mode , ...)
286
+ {
287
+ va_list args ;
288
+ int err = 0 ;
289
+ git_buf msg_path = GIT_BUF_INIT ;
290
+
291
+ assert (repo && mode );
292
+
293
+ va_start (args , mode );
294
+
295
+ if (git__strcmp (mode , GIT_HOOK_PREPARE_COMMIT_MSG_MESSAGE ) == 0 ) {
296
+ char * message = va_arg (args , char * );
297
+
298
+ if (message ) {
299
+ if ((err = prepare_commit_msg__message (& msg_path , repo , message )) != 0 )
300
+ goto cleanup ;
301
+ } else {
302
+ if ((err = prepare_commit_msg__template (& msg_path , repo , NULL )) != 0 )
303
+ goto cleanup ;
304
+
305
+ mode = GIT_HOOK_PREPARE_COMMIT_MSG_TEMPLATE ;
306
+ }
307
+ } else if (git__strcmp (mode , GIT_HOOK_PREPARE_COMMIT_MSG_TEMPLATE ) == 0 ) {
308
+ if ((err = prepare_commit_msg__template (& msg_path , repo , va_arg (args , char * ))) != 0 )
309
+ goto cleanup ;
310
+ } else if (git__strcmp (mode , GIT_HOOK_PREPARE_COMMIT_MSG_MERGE ) == 0 ) {
311
+
312
+ } else if (git__strcmp (mode , GIT_HOOK_PREPARE_COMMIT_MSG_SQUASH ) == 0 ) {
313
+
314
+ } else if (git__strcmp (mode , GIT_HOOK_PREPARE_COMMIT_MSG_COMMIT ) == 0 ) {
315
+ if ((err = prepare_commit_msg__commit (& msg_path , repo , va_arg (args , const git_oid * ))) != 0 )
316
+ goto cleanup ;
317
+ } else {
318
+ git_error_set (GIT_ERROR_HOOK , "prepare-commit-msg: unhandled mode %s" , mode );
319
+ err = -1 ;
320
+ goto cleanup ;
321
+ }
322
+
323
+ err = git_hook_execute (repo , "prepare-commit-msg" , git_buf_cstr (& msg_path ), mode , NULL );
324
+
325
+ p_unlink (git_buf_cstr (& msg_path ));
326
+
327
+ cleanup :
328
+ git_buf_dispose (& msg_path );
329
+
330
+ va_end (args );
331
+
332
+ return err ;
333
+ }
334
+
335
+ int prepare_commit_msg__message (git_buf * msg_path , git_repository * repo , const char * message )
336
+ {
337
+ assert (repo );
338
+
339
+ if (message == NULL ) {
340
+ return prepare_commit_msg__template (msg_path , repo , NULL );
341
+ }
342
+
343
+ return commit_message_file (msg_path , repo , message , "COMMIT_MSG" );
344
+ }
345
+
346
+ int prepare_commit_msg__template (git_buf * msg_path , git_repository * repo , const char * template_file )
347
+ {
348
+ int err = 0 ;
349
+ git_buf tpl_path = GIT_BUF_INIT ;
350
+ git_buf tpl_msg = GIT_BUF_INIT ;
351
+ git_config * conf ;
352
+
353
+ assert (repo );
354
+
355
+ if (template_file == NULL ) {
356
+ git_repository_config__weakptr (& conf , repo );
357
+ git_config_get_path (& tpl_path , conf , "commit.template" );
358
+ } else {
359
+ git_buf_puts (& tpl_path , template_file );
360
+ }
361
+
362
+ git_futils_readbuffer (& tpl_msg , git_buf_cstr (& tpl_path ));
363
+ git_buf_dispose (& tpl_path );
364
+
365
+ err = commit_message_file (msg_path , repo , git_buf_cstr (& tpl_msg ), "COMMIT_MSG" );
366
+ git_buf_dispose (& tpl_msg );
367
+
368
+ return err ;
369
+ }
370
+
371
+ int prepare_commit_msg__commit (git_buf * msg_path , git_repository * repo , const git_oid * oid )
372
+ {
373
+ int err = 0 ;
374
+ git_commit * commit ;
375
+
376
+ assert (repo );
377
+
378
+ if (oid == NULL ) {
379
+ return -1 ;
380
+ }
381
+
382
+ if ((err = git_commit_lookup (& commit , repo , oid )) != 0 )
383
+ return -1 ;;
384
+
385
+ err = commit_message_file (msg_path , repo , git_commit_message_raw (commit ), "COMMIT_MSG" );
386
+ git_commit_free (commit );
387
+
388
+ return err ;
389
+ }
390
+
391
+ int git_hook_call_commit_msg (git_repository * repo , const char * message )
392
+ {
393
+ int err = 0 ;
394
+ git_buf msg_path = GIT_BUF_INIT ;
395
+
396
+ assert (repo && message );
397
+
398
+ if ((err = commit_message_file (& msg_path , repo , message , "COMMIT_MSG" )) != 0 )
399
+ goto cleanup ;
400
+
401
+ err = git_hook_execute (repo , "commit-msg" , git_buf_cstr (& msg_path ), NULL );
402
+
403
+ p_unlink (git_buf_cstr (& msg_path ));
404
+
405
+ cleanup :
406
+ git_buf_dispose (& msg_path );
407
+ return err ;
408
+ }
409
+
410
+ int git_hook_call_post_commit (git_repository * repo )
411
+ {
412
+ assert (repo );
413
+
414
+ return git_hook_execute (repo , "post-commit" , NULL );
415
+ }
416
+
247
417
int git_hook_call_pre_rebase (git_repository * repo , const git_annotated_commit * upstream , const git_annotated_commit * rebased )
248
418
{
249
419
int err = 0 ;
0 commit comments