2626#include "remote.h"
2727#include "merge.h"
2828#include "diff_driver.h"
29+ #include "annotated_commit.h"
2930
3031#ifdef GIT_WIN32
3132# include "win32/w32_util.h"
@@ -1961,27 +1962,28 @@ int git_repository_set_head(
19611962 return error ;
19621963}
19631964
1964- int git_repository_set_head_detached (
1965- git_repository * repo ,
1966- const git_oid * commitish )
1965+ static int detach (git_repository * repo , const git_oid * id , const char * from )
19671966{
19681967 int error ;
19691968 git_buf log_message = GIT_BUF_INIT ;
19701969 git_object * object = NULL , * peeled = NULL ;
19711970 git_reference * new_head = NULL , * current = NULL ;
19721971
1973- assert (repo && commitish );
1972+ assert (repo && id );
19741973
19751974 if ((error = git_reference_lookup (& current , repo , GIT_HEAD_FILE )) < 0 )
19761975 return error ;
19771976
1978- if ((error = git_object_lookup (& object , repo , commitish , GIT_OBJ_ANY )) < 0 )
1977+ if ((error = git_object_lookup (& object , repo , id , GIT_OBJ_ANY )) < 0 )
19791978 goto cleanup ;
19801979
19811980 if ((error = git_object_peel (& peeled , object , GIT_OBJ_COMMIT )) < 0 )
19821981 goto cleanup ;
19831982
1984- if ((error = checkout_message (& log_message , current , git_oid_tostr_s (git_object_id (peeled )))) < 0 )
1983+ if (from == NULL )
1984+ from = git_oid_tostr_s (git_object_id (peeled ));
1985+
1986+ if ((error = checkout_message (& log_message , current , from )) < 0 )
19851987 goto cleanup ;
19861988
19871989 error = git_reference_create (& new_head , repo , GIT_HEAD_FILE , git_object_id (peeled ), true, git_buf_cstr (& log_message ));
@@ -1995,6 +1997,22 @@ int git_repository_set_head_detached(
19951997 return error ;
19961998}
19971999
2000+ int git_repository_set_head_detached (
2001+ git_repository * repo ,
2002+ const git_oid * commitish )
2003+ {
2004+ return detach (repo , commitish , NULL );
2005+ }
2006+
2007+ int git_repository_set_head_detached_from_annotated (
2008+ git_repository * repo ,
2009+ const git_annotated_commit * commitish )
2010+ {
2011+ assert (repo && commitish );
2012+
2013+ return detach (repo , git_annotated_commit_id (commitish ), commitish -> ref_name );
2014+ }
2015+
19982016int git_repository_detach_head (git_repository * repo )
19992017{
20002018 git_reference * old_head = NULL , * new_head = NULL , * current = NULL ;
0 commit comments