@@ -3694,6 +3694,28 @@ static const char *reflog_message(struct replay_opts *opts,
36943694 return buf .buf ;
36953695}
36963696
3697+ static struct commit * lookup_label (struct repository * r , const char * label ,
3698+ int len , struct strbuf * buf )
3699+ {
3700+ struct commit * commit ;
3701+ struct object_id oid ;
3702+
3703+ strbuf_reset (buf );
3704+ strbuf_addf (buf , "refs/rewritten/%.*s" , len , label );
3705+ if (!read_ref (buf -> buf , & oid )) {
3706+ commit = lookup_commit_object (r , & oid );
3707+ } else {
3708+ /* fall back to non-rewritten ref or commit */
3709+ strbuf_splice (buf , 0 , strlen ("refs/rewritten/" ), "" , 0 );
3710+ commit = lookup_commit_reference_by_name (buf -> buf );
3711+ }
3712+
3713+ if (!commit )
3714+ error (_ ("could not resolve '%s'" ), buf -> buf );
3715+
3716+ return commit ;
3717+ }
3718+
36973719static int do_reset (struct repository * r ,
36983720 const char * name , int len ,
36993721 struct replay_opts * opts )
@@ -3725,19 +3747,20 @@ static int do_reset(struct repository *r,
37253747 oidcpy (& oid , & opts -> squash_onto );
37263748 } else {
37273749 int i ;
3750+ struct commit * commit ;
37283751
37293752 /* Determine the length of the label */
37303753 for (i = 0 ; i < len ; i ++ )
37313754 if (isspace (name [i ]))
37323755 break ;
37333756 len = i ;
37343757
3735- strbuf_addf (& ref_name , "refs/rewritten/%.*s" , len , name );
3736- if (get_oid (ref_name .buf , & oid ) &&
3737- get_oid (ref_name .buf + strlen ("refs/rewritten/" ), & oid )) {
3738- ret = error (_ ("could not read '%s'" ), ref_name .buf );
3758+ commit = lookup_label (r , name , len , & ref_name );
3759+ if (!commit ) {
3760+ ret = -1 ;
37393761 goto cleanup ;
37403762 }
3763+ oid = commit -> object .oid ;
37413764 }
37423765
37433766 setup_unpack_trees_porcelain (& unpack_tree_opts , "reset" );
@@ -3785,26 +3808,6 @@ static int do_reset(struct repository *r,
37853808 return ret ;
37863809}
37873810
3788- static struct commit * lookup_label (const char * label , int len ,
3789- struct strbuf * buf )
3790- {
3791- struct commit * commit ;
3792-
3793- strbuf_reset (buf );
3794- strbuf_addf (buf , "refs/rewritten/%.*s" , len , label );
3795- commit = lookup_commit_reference_by_name (buf -> buf );
3796- if (!commit ) {
3797- /* fall back to non-rewritten ref or commit */
3798- strbuf_splice (buf , 0 , strlen ("refs/rewritten/" ), "" , 0 );
3799- commit = lookup_commit_reference_by_name (buf -> buf );
3800- }
3801-
3802- if (!commit )
3803- error (_ ("could not resolve '%s'" ), buf -> buf );
3804-
3805- return commit ;
3806- }
3807-
38083811static int do_merge (struct repository * r ,
38093812 struct commit * commit ,
38103813 const char * arg , int arg_len ,
@@ -3852,7 +3855,7 @@ static int do_merge(struct repository *r,
38523855 k = strcspn (p , " \t\n" );
38533856 if (!k )
38543857 continue ;
3855- merge_commit = lookup_label (p , k , & ref_name );
3858+ merge_commit = lookup_label (r , p , k , & ref_name );
38563859 if (!merge_commit ) {
38573860 ret = error (_ ("unable to parse '%.*s'" ), k , p );
38583861 goto leave_merge ;
0 commit comments